Built files from Bizgaze WebServer
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

select2totree.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*!
  2. * Select2-to-Tree 1.1.1
  3. * https://github.com/clivezhg/select2-to-tree
  4. */
  5. (function ($) {
  6. $.fn.select2ToTree = function (options) {
  7. var opts = $.extend({}, options);
  8. if (opts.treeData) {
  9. buildSelect(opts.treeData, this);
  10. }
  11. opts.closeOnSelect = false;
  12. opts.placeholder = 'Select';
  13. opts._templateResult = opts.templateResult;
  14. opts.templateResult = function (data, container) {
  15. var label = data.text;
  16. if (typeof opts._templateResult === "function") {
  17. label = opts._templateResult(data, container);
  18. }
  19. var $iteme = $("<span class='item-label'></span>").append(label);
  20. if (data.element) {
  21. var ele = data.element;
  22. container.setAttribute("data-val", ele.value);
  23. if (ele.className) container.className += " " + ele.className;
  24. if (ele.getAttribute("data-pup")) {
  25. container.setAttribute("data-pup", ele.getAttribute("data-pup"));
  26. }
  27. let isChecked = '';
  28. if ($(data.element).parent().val().length) {
  29. const selectedValues = $(data.element).parent().val();
  30. const valIndex = selectedValues.findIndex(x => x === data.id);
  31. valIndex != -1 ? isChecked = 'checked' : '';
  32. } else {
  33. data.selected ? isChecked = 'checked' : '';
  34. }
  35. //if ($(container).hasClass("non-leaf")) {
  36. return $.merge($(`<span class="expand-collapse" onmouseup="expColMouseupHandler(event);" onclick="event.stopPropagation();"><input type="checkbox" onchange="s2TreeChkHandler(event)" name="s2-to-tree-checkbox" class="s2-to-tree-checkbox" ${isChecked}></span>`), $iteme);
  37. //}
  38. }
  39. return $iteme;
  40. };
  41. window.expColMouseupHandler = function (evt) {
  42. toggleSubOptions(evt.target || evt.srcElement);
  43. /* prevent Select2 from doing "select2:selecting","select2:unselecting","select2:closing" */
  44. evt.stopPropagation ? evt.stopPropagation() : evt.cancelBubble = true;
  45. evt.preventDefault ? evt.preventDefault() : evt.returnValue = false;
  46. }
  47. var s2inst = this.select2(opts);
  48. s2inst.on("select2:open", function (evt) {
  49. var s2data = s2inst.data("select2");
  50. s2data.$dropdown.addClass("s2-to-tree");
  51. s2data.$dropdown.removeClass("searching-result");
  52. var $allsch = s2data.$dropdown.find(".select2-search__field").add(s2data.$container.find(".select2-search__field") );
  53. $allsch.off("input", inputHandler);
  54. $allsch.on("input", inputHandler);
  55. s2data.dropdown.$dropdown.off('click').click(function (e) {
  56. e.stopPropagation();
  57. });
  58. });
  59. s2inst.on("select2:select select2:unselect", function (e) {
  60. const data = e.params.data;
  61. if (data.selected) {
  62. $(`#${data._resultId}`).find('.s2-to-tree-checkbox').prop('checked', true);
  63. } else {
  64. $(`#${data._resultId}`).find('.s2-to-tree-checkbox').prop('checked', false);
  65. }
  66. });
  67. window.s2TreeChkHandler = function (evt) {
  68. const selectedValues = s2inst.val();
  69. const selectedVal = s2inst.find(`option[value="${$(evt.target).parents('.select2-results__option').data('val')}"]`).val();
  70. if ($(evt.target).is(':checked')) {
  71. selectedValues.push(selectedVal);
  72. } else {
  73. selectedValues.splice($.inArray(selectedVal, selectedValues), 1);
  74. }
  75. s2inst.val(selectedValues).trigger('change');
  76. }
  77. /* Show search result options even if they are collapsed */
  78. function inputHandler(evt) {
  79. var s2data = s2inst.data("select2");
  80. if ($(this).val().trim().length > 0) {
  81. s2data.$dropdown.addClass("searching-result");
  82. }
  83. else {
  84. s2data.$dropdown.removeClass("searching-result");
  85. }
  86. }
  87. return s2inst;
  88. };
  89. /* Build the Select Option elements */
  90. function buildSelect(treeData, $el) {
  91. /* Support the object path (eg: `item.label`) for 'valFld' & 'labelFld' */
  92. function readPath(object, path) {
  93. var currentPosition = object;
  94. for (var j = 0; j < path.length; j++) {
  95. var currentPath = path[j];
  96. if (currentPosition[currentPath]) {
  97. currentPosition = currentPosition[currentPath];
  98. continue;
  99. }
  100. return 'MISSING';
  101. }
  102. return currentPosition;
  103. }
  104. function buildOptions(dataArr, curLevel, pup) {
  105. var labelPath;
  106. if (treeData.labelFld && treeData.labelFld.split('.').length> 1){
  107. labelPath = treeData.labelFld.split('.');
  108. }
  109. var idPath;
  110. if (treeData.valFld && treeData.valFld.split('.').length > 1) {
  111. idPath = treeData.valFld.split('.');
  112. }
  113. for (var i = 0; i < dataArr.length; i++) {
  114. var data = dataArr[i] || {};
  115. var $opt = $("<option></option>");
  116. if (labelPath) {
  117. $opt.text(readPath(data, labelPath));
  118. } else {
  119. $opt.text(data[treeData.labelFld || "text"]);
  120. }
  121. if (idPath) {
  122. $opt.val(readPath(data, idPath));
  123. } else {
  124. $opt.val(data[treeData.valFld || "id"]);
  125. }
  126. if (data[treeData.selFld || "selected"] && String(data[treeData.selFld || "selected"]) === "true") {
  127. $opt.prop("selected", data[treeData.selFld || "selected"]);
  128. }
  129. if($opt.val() === "") {
  130. $opt.prop("disabled", true);
  131. $opt.val(getUniqueValue());
  132. }
  133. $opt.addClass("l" + curLevel);
  134. if (pup) $opt.attr("data-pup", pup);
  135. $el.append($opt);
  136. var inc = data[treeData.incFld || "inc"];
  137. if (inc && inc.length > 0) {
  138. $opt.addClass("non-leaf");
  139. buildOptions(inc, curLevel+1, $opt.val());
  140. }
  141. } // end 'for'
  142. } // end 'buildOptions'
  143. buildOptions(treeData.dataArr, 1, "");
  144. if (treeData.dftVal) $el.val(treeData.dftVal);
  145. }
  146. var uniqueIdx = 1;
  147. function getUniqueValue() {
  148. return "autoUniqueVal_" + uniqueIdx++;
  149. }
  150. function toggleSubOptions(target) {
  151. $(target.parentNode).toggleClass("opened");
  152. showHideSub(target.parentNode);
  153. }
  154. function showHideSub(ele) {
  155. var curEle = ele;
  156. var $options = $(ele).parent(".select2-results__options");
  157. var shouldShow = true;
  158. do {
  159. var pup = ($(curEle).attr("data-pup") || "").replace(/'/g, "\\'");
  160. curEle = null;
  161. if (pup) {
  162. var pupEle = $options.find(".select2-results__option[data-val='" + pup + "']");
  163. if (pupEle.length > 0) {
  164. if (!pupEle.eq(0).hasClass("opened")) { // hide current node if any parent node is collapsed
  165. $(ele).removeClass("showme");
  166. shouldShow = false;
  167. break;
  168. }
  169. curEle = pupEle[0];
  170. }
  171. }
  172. } while (curEle);
  173. if (shouldShow) $(ele).addClass("showme");
  174. var val = ($(ele).attr("data-val") || "").replace(/'/g, "\\'");
  175. $options.find(".select2-results__option[data-pup='" + val + "']").each(function () {
  176. showHideSub(this);
  177. });
  178. }
  179. })(jQuery);