-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathpqselect.min.js
More file actions
34 lines (30 loc) · 15.5 KB
/
Copy pathpqselect.min.js
File metadata and controls
34 lines (30 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*!
* ParamQuery Select v2.2.0
*
* Copyright (c) 2015-2026 Paramvir Dhindsa (http://paramquery.com)
* Released under MIT License
*
*/
(function(factory) {
if (typeof module === "object" && module.exports) {
// CommonJS / Node
const jq = require("jquery");
const pq = factory(jq, {});
// Default export
module.exports = pq;
// Named exports
module.exports.pq = pq;
module.exports.jQuery = jq;
module.exports.$ = jq;
} else {
// Browser globals
if (typeof window !== "undefined" && window.jQuery) {
window.pq = factory(window.jQuery, window.pq || {});
} else {
throw new Error("pq requires jQuery to be loaded first.");
}
}
})(function($, pq) {
"use strict";$.support.touch="ontouchend"in document;var fn={};fn.options=function(){var e=$.ui.keyCode;return{radio:!1,singlePlaceholder:"Select an option",checkbox:!1,displayText:"{0} of {1} selected",maxDisplay:4,maxHeight:300,maxSelect:0,multiplePlaceholder:"Select options",selectallText:"Select All",closeOnWindowScroll:!0,closeOnWindowResize:!0,itemCls:"pq-select-item ui-corner-all ui-state-default",bootstrap:{on:!1,btn:"btn btn-default",popupCont:"panel panel-default",selectCls:"label label-info",itemCls:"label label-default",closeIcon:"glyphicon glyphicon-remove",searchIcon:"glyphicon glyphicon-search",hoverCls:""},position:{my:"left top",at:"left bottom",collision:"flipfit"},kcOpen:[e.DOWN,e.ENTER,e.UP],deselect:!0,hoverCls:"pq-state-hover",optionsResizable:{handles:"e,s"},search:!0,searchRule:"contain",_selectCls:"pq-state-select",selectCls:"ui-state-highlight",width:null,rowHt:25,maxSelectReach:null,maxSelectExceed:null,valIndx:"val",textIndx:"text"}}(),fn.destroy=function(){for(var e in this._super(),this)delete this[e]},fn._setOption=function(e,t){this._super(e,t),"disabled"==e?(t&&this.close(),this.$button[t?"addClass":"removeClass"]("ui-state-disabled")):"maxHeight"==e?(this.$popup.css({maxHeight:t,height:""}),this.setMenuHt(),this.renderView()):"data"==e&&(this.data=this.options.data,this.refresh())},$.widget("paramquery.pqSelect",fn),pq.select=function(e,t){return $(e).pqSelect(t).data("ui.pqSelect")},(fn=$.paramquery.pqSelect.prototype)._setButtonWidth=function(){if(this.isSelect){var e=this.element;e.show();var t=this.options,n=1*(n=t.width?t.width:e[0].offsetWidth)==n?n+"px":n;e.hide(),this.$button[0].style.width=n}},fn.val=function(){var e=this.options,t=e.valIndx,n=e.textIndx,i=this.data.filter((function(e){return e.selected})).map((function(e){var i=e[t];return null==i?e[n]:i}));return this.multiple?i:i[0]},fn._create=function(){var e,t=this,n=t.options,i=t.element,s=t.isSelect=i.is("select"),o=n.bootstrap,l=o.on,a=i.attr("name"),c=i[0].hasAttribute("multiple"),p=t.eventNamespace;l&&(n.selectCls+=" "+o.selectCls,n.itemCls+=" "+o.itemCls),s?(e=$(["<div class='pq-select-button pq-no-capture pq-border-2 ",l?o.btn:"ui-state-default ui-widget ui-corner-all","' "," data-name='",a,"' tabindex='0'>","</div>"].join("")),i.after(e),e.attr("name",a),e.addClass(i.attr("class")),i.addClass("pq-select")):(t.divClass=i.attr("class")||"",(e=i).addClass("pq-select-button pq-no-capture pq-border-2 ui-state-default ui-widget ui-corner-all")),e.html("<span class='pq-icon "+(l?"caret":"ui-icon ui-icon-triangle-1-s")+"'></span><div class='pq-select-text'></div>"),t.$button=e,t.multiple=!!c,t.selector=".pq-select-option.ui-state-enable:visible",t._setButtonWidth(),n.data?t.data=n.data:t._extractData(),t._createPopup(),t._createMenu(),e.on({click:function(e){if(!n.disabled){var i=$(this).parent(".pq-select-item"),s=parseInt(i.attr("data-id"));return t.select(s,!1),t.setText(),t.setSelectAllState(),t.focus(),t.triggerChange(),!1}}},".pq-item-close"),e.on({click:function(e){if(!n.disabled)return t.toggle(),!1},focus:function(e){n.disabled||$(this).addClass("ui-state-hover")},blur:function(e){$(this).removeClass("ui-state-hover")},mousedown:function(e){n.disabled||$(this).addClass("ui-state-active")},mouseup:function(e){n.disabled||$(this).removeClass("ui-state-active")},keydown:function(e){if(!n.disabled){var i=e.keyCode,s=n.kcOpen,o=$.ui.keyCode;-1!==$.inArray(i,s)?t.open():i===o.ESCAPE?t.close():i===o.SPACE&&t.toggle()}}}),t.setText(),t.setSelectAllState(),$(window).on("resize"+p,t.onWindowResize.bind(t)).on("scroll"+p,t.onWindowScroll.bind(t))},fn.onWindowResize=function(e){var t=e.originalEvent;t&&"mousemove"==t.type||!this.options.closeOnWindowResize||this.close()},fn.onWindowScroll=function(){this.options.closeOnWindowScroll&&!this._preventClose&&this.close()},fn.focus=function(){var e=this,t=e.$search;$.support.touch||(t[0]?t.focus():e.$popup.focus(),e._highlight())},fn.getOption=function(e){return $("#"+this.getOptionId(e))},fn.getOptionId=function(e){return"pq-option-"+this.uuid+"-"+e},fn._move=function(e,t,n){var i,s,o=this,l=o.optionIndxV,a=null==l?-1:l,c=o.options,p=o.$menu[0].offsetHeight,r=o.dataV,u=r.length,d=c.rowHt,h=function(e){return!r[e].disabled&&!r[e].pq_title},f=Math.floor((p-17)/d);if(n)i=e?u-1:0;else if(t)i=e?a+f:a-f;else if(e){for(s=a+1;s<u;s++)if(h(s)){i=s;break}}else for(s=a-1;s>=0;s--)if(h(s)){i=s;break}i<0&&(i=0),i>=u&&(i=u-1),o.focusOption(i)},fn.focusOption=function(e){var t,n=this,i=n.options.rowHt,s=e*i,o=s+i,l=n.$menu,a=l[0],c=a.scrollTop,p=a.offsetHeight,r=function(){n._highlight(e)};s<c?t=s:o>c+p&&(t=o-p),null!=t?(l.one("scroll",(function(){r()})),a.scrollTop=t):r()},fn._onkeydown=function(e){var t,n=e.keyCode,i=this,s=i.optionIndxV,o=$.ui.keyCode;return n===o.DOWN||n===o.UP?(i._move(n===o.DOWN),t=!1):n===o.PAGE_DOWN||n===o.PAGE_UP?(i._move(n==o.PAGE_DOWN,!0),t=!1):n==o.HOME||n==o.END?(i._move(n==o.END,!1,!0),t=!1):n===o.TAB?(i.close(),t=!1):n===o.ESCAPE?i.close():n===o.ENTER&&null!=s&&(i.getOption(s).trigger("label_changed"),t=!1),t},fn.search=function(e){for(var t=this.data,n=this.options,i=n.searchRule,s=n.textIndx,o="contain"===i,l=0,a=t.length;l<a;l++){var c=t[l],p=(c[s]+"").toUpperCase().indexOf(e);c.searchIndx=null,-1===p||!1===o&&p>0?c.hidden=!0:(c.hidden=!1,c.searchIndx=p)}},fn._onkeyupsearch=function(e){var t=$(e.target),n=this,i=$.trim(t.val()).toUpperCase();(i||n.searchText)&&i!=n.searchText&&(n.searchText=i,n.search(i),n._createMenu(),n.renderView(),n.positionPopup(),n.setSelectAllState())},fn.onChange=function(e){var t=this,n=t.dataV[e],i=n.pq_indx,s=!n.selected,o=t.options,l=t.multiple,a=o.maxSelect,c=t.selectIndx;if(l){if(s&&a&&c.length>=a)return t._trigger("maxSelectExceed",null,{option:t.$options[i]}),t.focus(),!1}else if(c.length){var p=c[0];if(i===p)return!1;s&&t.select(p,!1)}t.select(i,s),t.setText(),t.setSelectAllState(),l?a&&c.length>=a?!1!==t._trigger("maxSelectReach",null,{option:t.$options[i]})&&t.close():t.focus():t.close(),t.triggerChange()},fn.setSelectAllState=function(){var e=this.$popup.find(".pq-select-all input"),t=e[0];if(t){for(var n,i,s=this.dataV,o=0,l=0,a=0,c=s.length;a<c;a++)(i=s[a]).disabled||i.pq_title||(o++,i.selected&&l++);n=o===l,e.prop("checked",n),t.indeterminate=!n&&l>0}},fn.getInstance=function(){return{select:this}},fn.select=function(e,t){var n,i=this,s=i.selectIndx,o=i.data,l=i.options,a=o[e],c=i.$options,p=c?$(c[e]):$(),r=i.getOption(a.pq_indxV),u=r.find("input");r[t?"addClass":"removeClass"](l._selectCls+" "+l.selectCls),u.prop("checked",t),a.selected=t,i.multiple?(n=s.indexOf(e),t?-1==n&&s.push(e):n>-1&&s.splice(n,1),p.prop("selected",t)):t?(s.length&&s[0]!=e&&(o[s[0]].selected=!1),s[0]=e,p.prop("selected",t)):(i.selectIndx=[0],o[0].selected=!0,(c?$(c[0]):$()).prop("selected",!0))},fn.triggerChange=function(){this._trigger("change"),this.element.trigger("change")},fn._extractData=function(){var e,t,n=this.data=[],i=this.element,s=i.find("option,optgroup"),o=!1,l=!1;this.$options=i.find("option");for(var a=0,c=s.length;a<c;a++){var p=s[a],r=$(p);if("optgroup"==p.nodeName.toLowerCase())t=r.attr("label"),o=!0,l=r.prop("disabled");else{var u=r.prop("selected"),d=r.prop("disabled");!d&&o&&(d=l),e=r.text(),n.push({selected:u,disabled:d,text:e,val:r.val(),optgroup:t})}}this.grouping=o},fn.exportData=function(){return JSON.stringify(this.data,null,2)},fn.refresh=function(){this.search(""),this._setButtonWidth(),this._createPopup(),this._createMenu(),this.setText(),this.setSelectAllState()},fn.refreshData=function(){this._extractData(),this.refresh()},fn._createPopup=function(){var e=this,t=this.options,n=t.bootstrap,i=n.on,s=e.multiple,o=e.$button.css("font-size"),l="",a="";s&&t.selectallText&&!t.maxSelect&&(a=["<label class='pq-select-all ui-widget-header ui-corner-all'>","<span class='ui-icon ui-icon-close ",i?n.closeIcon:"","'></span>","<input type='checkbox' >",t.selectallText,"</label>"].join("")),t.search&&(l=["<div class='pq-select-search-div ui-corner-all'>","<span class='ui-icon ui-icon-search ",i?n.searchIcon:"","'></span>","<div class='pq-select-search-div1'>","<input type='text' class='pq-select-search-input' autocomplete='off' style='height:",t.rowHt-2,"px;'/>","</div>","</div>"].join(""));var c=$(["<div class='pq-select-popup-cont ",i?n.popupCont:"","'>","<div class='pq-select-popup ui-widget-content ui-corner-all' tabindex='-1'>",a,l,"<div class='pq-select-menu'><div style='margin:0;padding:0;' ></div></div>","</div><div class='pq-select-shadow-fix'></div></div>"].join("")),p=c.children(".pq-select-popup");e.$menu=p.find(".pq-select-menu").on("scroll",e.onScroll.bind(e)).on("mousemove",e.onMouseMove.bind(e)),c.css({"font-family":this.$button.css("font-family"),"font-size":0==parseInt(o)?"inherit":o}),p.css("maxHeight",t.maxHeight).on({keydown:function(t){return e._onkeydown(t)}}).on({mouseenter:function(t){e._highlight(e.getIndxV(this.id))},label_changed:function(t){var n=this.id;if(n)return e.onChange(e.getIndxV(n))}},".pq-select-option.ui-state-enable"),p.find(".ui-icon-close").on({click:function(t){return e.close(),!1}}),s&&t.checkbox||!s&&t.radio?p.on({click:function(){$(this).closest("label").trigger("label_changed")}},"label.pq-select-option.ui-state-enable input"):p.on({click:function(){$(this).trigger("label_changed")}},"label.pq-select-option.ui-state-enable"),e.$popupCont&&e.$popupCont.remove(),e.$popupCont=c,p.resizable($.extend(!0,{stop:e.onResized.bind(e)},t.optionsResizable)),e.$popup=p,e.$search=p.find(".pq-select-search-input").on({keyup:function(t){return e._onkeyupsearch(t)}}),e.$selectAll=p.find(".pq-select-all").on({change:e.onSelectAll.bind(e)},"input"),$(document.body).append(c)},fn.onSelectAll=function(e){for(var t,n=this,i=!!$(e.target).prop("checked"),s=n.dataV,o=0,l=n.$options;o<s.length;o++)(t=s[o]).disabled||t.selected===i||(t.selected=i,l&&$(l[t.pq_indx]).prop("selected",i));n._createMenu(),n.renderView(),n.setText(),n.focus(),n.triggerChange()},fn.onResized=function(e,t){this.width=t.size.width,this.option("maxHeight",t.size.height)},fn.getIndxV=function(e){return 1*e.split("-")[3]},fn.getIndx=function(e){return this.dataV[this.getIndxV(e)].pq_indx},fn._createMenu=function(){var e,t,n=this,i=n.data,s=n.options,o=s.rowHt,l=s.textIndx,a=n.selectIndx=[],c=n.dataV=[],p=0,r=n.$menu.children();i.forEach((function(n,i){n.pq_indx=i,e=n.optgroup,0==i&&""==n[l]||(n.selected&&a.push(i),t===e||n.hidden||(c[p++]={pq_title:!0,text:e},t=e),n.hidden||(n.pq_indxV=p,c[p++]=n))})),n._initV=n._finalV=null,r.html(""),r.height(c.length*o),n.setMenuHt()},fn.onMouseMove=function(){this.preventClose()},fn.preventClose=function(){var e=this;e._preventClose=!0,clearTimeout(e.mmtimerID),e.mmtimerID=setTimeout((function(){e._preventClose=!1}),2e3)},fn.getViewPortIndx=function(){var e=this.$menu[0],t=this.options.rowHt,n=e.scrollTop;return[Math.floor(n/t),Math.floor((n+e.offsetHeight)/t)]},fn.renderView=function(){var e,t,n,i=this,s=i.uuid,o=i.getViewPortIndx(),l=o[0],a=o[1],c=i._initV,p=i._finalV,r=null!=c,u=i.options,d=u.textIndx,h=u.search?(i.searchText||"").length:0,f=" "+u._selectCls+" "+u.selectCls+" ",g=i.multiple?u.checkbox?'type="checkbox"':"":u.radio?'type="radio"':"",m=[],v=g?"pq-left-input":i.grouping?"pq-left-group":"",x=u.rowHt,b=function(e,t){return"style='position:absolute;top:"+e*x+"px;width:100%;left:0;height:"+x+"px;overflow:hidden;"+(t||"")+"'"},$=i.dataV;if(r)for(t=c;t<=p;t++)(t<l||t>a)&&i.getOption(t).remove();for(t=l;t<=a;t++)if(n=$[t]){var C=n.disabled,_=n.selected,q=n[d];if(r&&t>=c&&t<=p)continue;if(n.pq_title)m.push("<label ",b(t,n.style)," class='pq-select-optgroup'"," id='",i.getOptionId(t),"'>","<span>",q,"</span>","</label>");else{var w=_?' checked="checked" ':"",y=_?f:"",I=C?' disabled="disabled" ':"",S=C?"ui-state-disabled":"ui-state-enable";h&&(e=n.searchIndx,q=q.substr(0,e)+"<span class='pq-select-search-highlight'>"+q.substr(e,h)+"</span>"+q.substr(e+h,q.length)),m.push("<label ",b(t,n.style)," class='pq-select-option ",y,S,"'"," id='",i.getOptionId(t),"'>",g?"<input name='pq-select-input-"+s+"' "+g+" "+w+I+" >":"","<span class='",v,"'>",q,"</span>","</label>")}}i._initV=l,i._finalV=a,i.$menu.children().append(m.join(""))},fn.onScroll=function(){this.renderView(),this.preventClose()},fn._highlight=function(e){var t=this,n=t.options.hoverCls,i=t._initV,s=t._finalV,o=function(e){return null!=e&&e>=i&&e<=s},l=t.optionIndxV,a=null!=(e=o(e)?e:o(l)?l:null)?t.getOption(e):t.$menu.find(".pq-select-option.ui-state-enable:visible:first"),c=a[0];c&&(e=t.getIndxV(c.id),null!=l&&t.getOption(l).removeClass(n),a.addClass(n),t.optionIndxV=e,t._trigger("focused",{label:c,rowIndx:e}))},fn._setPopupWidth=function(){this.$popupCont.width(this.width||this.$button[0].offsetWidth)},fn.positionPopup=function(){var e=this.options,t=this.$button,n=$.extend({of:t},e.position),i=this.$popupCont;this._setPopupWidth(),i.position(n)},fn.isOpen=function(){return!(!this.$popupCont||"block"!=this.$popupCont.css("display"))},fn.setMenuHt=function(){var e=this,t=e.options,n=t.rowHt,i=(e.$popup.find(".pq-select-search-div").outerHeight(!0)||0)+(e.$selectAll.outerHeight(!0)||0),s=e.dataV.length*n,o=Math.min(s+i,t.maxHeight)-i;e.$menu.css("height",o+"px")},fn.open=function(){var e=this,t=e.$popupCont;if(e.isOpen())return!1;t.show(),e.positionPopup(),e.setMenuHt(),e.renderView(),$(document).on("mousedown"+e.eventNamespace,(function(t){var n=$(t.target);n.closest(e.$popupCont)[0]||n.closest(e.$button)[0]||e.close()})),e.focus()},fn.setText=function(){var e=this,t=e.$button,n=t.find(".pq-select-text"),i=e.element,s=e.options,o=s.textIndx,l=s.deselect,a=e.data,c=s.itemCls,p=s.bootstrap,r=p.on?p.closeIcon:"ui-icon ui-icon-close",u=function(e){return l?["<span class='",c,"' data-id = '",e,"'>","<span class='pq-item-close ",r,"'></span>","<span class='pq-select-item-text'>",a[e][o],"</span>","</span>"].join(""):a[e][o]},d=e.selectIndx;if(e.multiple){t.addClass("pq-select-multiple");var h=d.length,f=s.maxDisplay,g=a.length;if(h>0)if(h<=f){for(var m=[],v=0;v<h;v++){var x=d[v];m.push(u(x))}b=l?m.join(""):m.join(", ")}else b=(b=(b=s.displayText).replace("{0}",d.length)).replace("{1}",g);else(b=i.attr("data-placeholder"))||(b=s.multiplePlaceholder)}else{var b;t.addClass("pq-select-single"),n.css("maxWidth",t.width()-16),null!=(b=null!=(x=d[0])?a[x][o]:"")&&""!==b?b=u(x):(b=i.attr("data-placeholder"))||(b=s.singlePlaceholder)}n.html(b),e.multiple||n.find(".pq-select-item-text").css({maxWidth:t.width()-35}),setTimeout((function(){e.uuid&&e.positionPopup()}))},fn.close=function(e){this.isOpen()&&(!1!==(e=e||{}).focus&&this.$button.focus(),this.$popupCont.hide()),$(document).off(this.eventNamespace)},fn.toggle=function(){this.isOpen()?this.close():this.open()},fn.disable=function(){this.option({disabled:!0})},fn.enable=function(){this.option({disabled:!1})},fn._destroy=function(){var e=this.$button,t=this.eventNamespace;this.$popupCont.remove(),this.isSelect?(e.remove(),this.element.removeClass("pq-select").show()):(e.empty(),e.off(),e.attr("class",this.divClass)),$(document).off(t),$(window).off(t)};
return pq;
});