1jQuery(function() { 2 3 function sort_list($list) { 4 var elements = []; 5 $list.find("> li").each(function() { 6 var $this = jQuery(this); 7 elements.push([$this.text(), $this]); 8 }); 9 elements.sort(function(a, b) { return (a[0]).localeCompare(b[0]) }); 10 for (i in elements) { 11 var $li = elements[i][1]; 12 $list.append($li); 13 } 14 } 15 16 function group_by_letters($list) { 17 var lists = {}; 18 19 $list.find("> li").each(function() { 20 var $this = jQuery(this); 21 var text = $this.text().trim(); 22 var firstLetter = text.charAt(0); 23 if (!lists.hasOwnProperty(firstLetter)) { 24 lists[firstLetter] = []; 25 } 26 lists[firstLetter].push($this); 27 }); 28 29 var $levelDiv = $list.closest("div[class^=level]"); 30 if ($levelDiv.length === 0) { 31 var headerLevel = 1; 32 } else { 33 var level = $levelDiv.attr("class").substring("level".length); 34 var headerLevel = parseInt(level) + 1; 35 } 36 37 var $parent = $list.parent(); 38 jQuery.each(lists, function (header, elements) { 39 jQuery(document.createElement("h" + headerLevel)).text(header).appendTo($parent); 40 jQuery($list[0].cloneNode()).appendTo($parent).append(elements); 41 }); 42 $list.remove(); 43 } 44 45 function init() { 46 jQuery(".plugin__alphalist2").each(function() { 47 var $this = jQuery(this); 48 var classes = this.className.split(/\s+/); 49 50 $this.find("ol, ul").each(function () { 51 var $this = jQuery(this); 52 sort_list($this); 53 if (jQuery.inArray("group_by_letters", classes) !== -1) { 54 group_by_letters($this); 55 } 56 }); 57 }); 58 } 59 60 jQuery(init); 61 62}); 63