|
1 | 1 | /*! |
2 | | - * Select2-to-Tree 0.8.0 |
| 2 | + * Select2-to-Tree 1.0.0 |
3 | 3 | * https://github.com/clivezhg/select2-to-tree |
4 | 4 | */ |
5 | 5 | (function ($) { |
6 | 6 | $.fn.select2ToTree = function (options) { |
7 | | - var defaults = { |
8 | | - theme: "default" |
9 | | - }; |
10 | | - var opts = $.extend(defaults, options); |
| 7 | + var opts = $.extend({}, options); |
| 8 | + |
11 | 9 | if (opts.treeData) { |
12 | 10 | buildSelect(opts.treeData, this); |
13 | 11 | } |
| 12 | + |
14 | 13 | opts.templateResult = function (data, container) { |
15 | 14 | var $iteme = $("<span class='item-label'></span>").text(data.text); |
16 | 15 | if (data.element) { |
|
21 | 20 | container.setAttribute("data-pup", ele.getAttribute("data-pup")); |
22 | 21 | } |
23 | 22 | if ($(container).hasClass("non-leaf")) { |
24 | | - return $('<span class="expand-collapse"></span>' + $("<div></div").append($iteme).html()); |
| 23 | + return $('<span class="expand-collapse" onmouseup="expColMouseupHandler(event);"></span>' + $("<div></div").append($iteme).html()); |
25 | 24 | } |
26 | 25 | } |
27 | 26 | return $iteme; |
28 | 27 | }; |
29 | 28 |
|
30 | | - var expandCollapseTime = 0; |
| 29 | + window.expColMouseupHandler = function (evt) { |
| 30 | + toggleSubOptions(evt.target || evt.srcElement); |
| 31 | + /* prevent Select2 from doing "select2:selecting","select2:unselecting","select2:closing" */ |
| 32 | + evt.stopPropagation ? evt.stopPropagation() : evt.cancelBubble = true; |
| 33 | + evt.preventDefault ? evt.preventDefault() : evt.returnValue = false; |
| 34 | + } |
31 | 35 |
|
32 | 36 | var s2inst = this.select2(opts); |
33 | | - s2inst.on("select2:selecting select2:unselecting", function (evt) { |
34 | | - if (Date.now() - expandCollapseTime < 160) { |
35 | | - evt.preventDefault(); |
36 | | - } |
37 | | - }); |
38 | | - |
39 | | - s2inst.on("select2:closing", function (evt) { // If the clicked is the selected, no 'selecting' will be fired, and 'closing' will be raised |
40 | | - if (Date.now() - expandCollapseTime < 160) { |
41 | | - evt.preventDefault(); |
42 | | - } |
43 | | - }); |
44 | 37 |
|
45 | 38 | s2inst.on("select2:open", function (evt) { |
46 | 39 | var s2data = s2inst.data("select2"); |
47 | | - // The 'click' event will be after 'select2:selecting' |
48 | 40 | s2data.$dropdown.addClass("s2-to-tree"); |
49 | 41 | s2data.$dropdown.find(".searching-result").removeClass("searching-result"); |
50 | | - s2data.$dropdown.off("mousedown", ".expand-collapse", mousedownHandler); |
51 | | - s2data.$dropdown.on("mousedown", ".expand-collapse", mousedownHandler); |
52 | 42 | var $allsch = s2data.$dropdown.find(".select2-search__field").add( s2data.$container.find(".select2-search__field") ); |
53 | 43 | $allsch.off("input", inputHandler); |
54 | 44 | $allsch.on("input", inputHandler); |
55 | 45 | }); |
56 | | - function mousedownHandler(evt) { |
57 | | - toggleSubOptions(evt.target); |
58 | | - evt.stopPropagation(); |
59 | | - expandCollapseTime = Date.now(); |
60 | | - } |
| 46 | + |
| 47 | + /* Show search result options even if they are collapsed */ |
61 | 48 | function inputHandler(evt) { |
62 | 49 | var s2data = s2inst.data("select2"); |
63 | 50 | if ($(this).val().trim().length > 0) { |
|
71 | 58 | return s2inst; |
72 | 59 | }; |
73 | 60 |
|
74 | | - function buildSelect(treeData, $el) { |
| 61 | + function buildSelect(treeData, $el) { // build Select options according to Select-to-Tree specification |
75 | 62 | function buildOptions(dataArr, curLevel, pup) { |
76 | 63 | for (var i = 0; i < dataArr.length; i++) { |
77 | | - var data = dataArr[i]; |
| 64 | + var data = dataArr[i] || {}; |
78 | 65 | var $opt = $("<option></option>"); |
79 | | - $opt.text(data[treeData.labelFld || "name"]); |
| 66 | + $opt.text(data[treeData.labelFld || "text"]); |
80 | 67 | $opt.val(data[treeData.valFld || "id"]); |
81 | 68 | if($opt.val() === "") { |
82 | 69 | $opt.prop("disabled", true); |
|
108 | 95 |
|
109 | 96 | function showHideSub(ele) { |
110 | 97 | var curEle = ele; |
| 98 | + var $options = $(ele).parent(".select2-results__options"); |
111 | 99 | var shouldShow = true; |
112 | 100 | do { |
113 | | - var pup = $(curEle).attr("data-pup"); |
| 101 | + var pup = ($(curEle).attr("data-pup") || "").replace(/'/g, "\\'"); |
114 | 102 | curEle = null; |
115 | 103 | if (pup) { |
116 | | - var pupEle = $(".select2-container li.select2-results__option[data-val='" + pup + "']"); |
| 104 | + var pupEle = $options.find(".select2-results__option[data-val='" + pup + "']"); |
117 | 105 | if (pupEle.length > 0) { |
118 | 106 | if (!pupEle.eq(0).hasClass("opened")) { |
119 | 107 | $(ele).removeClass("showme"); |
|
126 | 114 | } while (curEle); |
127 | 115 | if (shouldShow) $(ele).addClass("showme"); |
128 | 116 |
|
129 | | - var val = $(ele).attr("data-val"); |
130 | | - $(".select2-container li.select2-results__option[data-pup='" + val + "']").each(function () { |
| 117 | + var val = ($(ele).attr("data-val") || "").replace(/'/g, "\\'"); |
| 118 | + $options.find(".select2-results__option[data-pup='" + val + "']").each(function () { |
131 | 119 | showHideSub(this); |
132 | 120 | }); |
133 | 121 | } |
134 | | - |
135 | 122 | })(jQuery); |
0 commit comments