multi.min.js 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*! multi.js 03-12-2018 */
  2. var multi = function() {
  3. var e = function(e, t, n) {
  4. var a = e.options[t.target.getAttribute("multi-index")];
  5. if (!a.disabled) {
  6. a.selected = !a.selected;
  7. var i, d, r, l = n.limit;
  8. if (l > -1) {
  9. for (var s = 0, o = 0; o < e.options.length; o++) e.options[o].selected && s++;
  10. if (s === l) {
  11. this.disabled_limit = !0, "function" == typeof n.limit_reached && n.limit_reached();
  12. for (o = 0; o < e.options.length; o++) {
  13. (c = e.options[o]).selected || c.setAttribute("disabled", !0)
  14. }
  15. } else if (this.disabled_limit) {
  16. for (o = 0; o < e.options.length; o++) {
  17. var c;
  18. "false" === (c = e.options[o]).getAttribute("data-origin-disabled") && c.removeAttribute("disabled")
  19. }
  20. this.disabled_limit = !1
  21. }
  22. }
  23. i = "change", d = e, (r = document.createEvent("HTMLEvents")).initEvent(i, !1, !0), d.dispatchEvent(r)
  24. }
  25. },
  26. t = function(e, t) {
  27. if (e.wrapper.selected.innerHTML = "", e.wrapper.non_selected.innerHTML = "", t.non_selected_header && t.selected_header) {
  28. var n = document.createElement("div"),
  29. a = document.createElement("div");
  30. n.className = "header", a.className = "header", n.innerText = t.non_selected_header, a.innerText = t.selected_header, e.wrapper.non_selected.appendChild(n), e.wrapper.selected.appendChild(a)
  31. }
  32. if (e.wrapper.search) var i = e.wrapper.search.value;
  33. for (var d = null, r = null, l = 0; l < e.options.length; l++) {
  34. var s = e.options[l],
  35. o = s.value,
  36. c = s.textContent || s.innerText,
  37. p = document.createElement("a");
  38. if (p.tabIndex = 0, p.className = "item-1", p.innerHTML = c, p.setAttribute("role", "button"), p.setAttribute("data-value", o), p.setAttribute("multi-index", l), s.disabled && (p.className += " disabled"), s.selected) {
  39. p.className += " selected";
  40. var u = p.cloneNode(!0);
  41. e.wrapper.selected.appendChild(u)
  42. }
  43. if ("OPTGROUP" == s.parentNode.nodeName && s.parentNode != r) {
  44. if (r = s.parentNode, (d = document.createElement("div")).className = "item-group-1", s.parentNode.label) {
  45. var m = document.createElement("span");
  46. m.innerHTML = s.parentNode.label, m.className = "group-label", d.appendChild(m)
  47. }
  48. e.wrapper.non_selected.appendChild(d)
  49. }
  50. s.parentNode == e && (d = null, r = null), (!i || i && c.toLowerCase().indexOf(i.toLowerCase()) > -1) && (null != d ? d.appendChild(p) : e.wrapper.non_selected.appendChild(p))
  51. }
  52. };
  53. return function(n, a) {
  54. if ((a = void 0 !== a ? a : {}).enable_search = void 0 === a.enable_search || a.enable_search, a.search_placeholder = void 0 !== a.search_placeholder ? a.search_placeholder : "Search...", a.non_selected_header = void 0 !== a.non_selected_header ? a.non_selected_header : null, a.selected_header = void 0 !== a.selected_header ? a.selected_header : null, a.limit = void 0 !== a.limit ? parseInt(a.limit) : -1, isNaN(a.limit) && (a.limit = -1), null == n.dataset.multijs && "SELECT" == n.nodeName && n.multiple) {
  55. n.style.display = "none", n.setAttribute("data-multijs", !0);
  56. var i = document.createElement("div");
  57. if (i.className = "multi-wrapper", a.enable_search) {
  58. var d = document.createElement("input");
  59. d.className = "search-input", d.type = "text", d.setAttribute("placeholder", a.search_placeholder), d.addEventListener("input", function() {
  60. t(n, a)
  61. }), i.appendChild(d), i.search = d
  62. }
  63. var r = document.createElement("div");
  64. r.className = "non-selected-wrapper";
  65. var l = document.createElement("div");
  66. l.className = "selected-wrapper", i.addEventListener("click", function(t) {
  67. t.target.getAttribute("multi-index") && e(n, t, a)
  68. }), i.addEventListener("keypress", function(t) {
  69. var i = 32 === t.keyCode || 13 === t.keyCode;
  70. t.target.getAttribute("multi-index") && i && (t.preventDefault(), e(n, t, a))
  71. }), i.appendChild(r), i.appendChild(l), i.non_selected = r, i.selected = l, n.wrapper = i, n.parentNode.insertBefore(i, n.nextSibling);
  72. for (var s = 0; s < n.options.length; s++) {
  73. var o = n.options[s];
  74. o.setAttribute("data-origin-disabled", o.disabled)
  75. }
  76. t(n, a), n.addEventListener("change", function() {
  77. t(n, a)
  78. })
  79. }
  80. }
  81. }();
  82. "undefined" != typeof jQuery && function(e) {
  83. e.fn.multi = function(t) {
  84. return t = void 0 !== t ? t : {}, this.each(function() {
  85. var n = e(this);
  86. multi(n.get(0), t)
  87. })
  88. }
  89. }(jQuery);