mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-18 05:52:15 +00:00
1 line
15 KiB
JavaScript
1 line
15 KiB
JavaScript
!function(G,b){function t(t,e){var i,s,o=this;e=e||{},this.$element=G(t),this.options=G.extend(!0,{},G.fn.ajaxSelectPicker.defaults,e),this.LOG_ERROR=1,this.LOG_WARNING=2,this.LOG_INFO=3,this.LOG_DEBUG=4,this.lastRequest=!1,this.previousQuery="",this.query="",this.request=!1;var l=[{from:"ajaxResultsPreHook",to:"preprocessData"},{from:"ajaxSearchUrl",to:{ajax:{url:"{{{value}}}"}}},{from:"ajaxOptions",to:"ajax"},{from:"debug",to:function(t){var e={};e.log=Boolean(o.options[t.from])?o.LOG_DEBUG:0,o.options=G.extend(!0,{},o.options,e),delete o.options[t.from],o.log(o.LOG_WARNING,'Deprecated option "'+t.from+'". Update code to use:',e)}},{from:"mixWithCurrents",to:"preserveSelected"},{from:"placeHolderOption",to:{locale:{emptyTitle:"{{{value}}}"}}}];l.length&&G.map(l,function(t){if(o.options[t.from])if(G.isPlainObject(t.to))o.replaceValue(t.to,"{{{value}}}",o.options[t.from]),o.options=G.extend(!0,{},o.options,t.to),o.log(o.LOG_WARNING,'Deprecated option "'+t.from+'". Update code to use:',t.to),delete o.options[t.from];else if(G.isFunction(t.to))t.to.apply(o,[t]);else{var e={};e[t.to]=o.options[t.from],o.options=G.extend(!0,{},o.options,e),o.log(o.LOG_WARNING,'Deprecated option "'+t.from+'". Update code to use:',e),delete o.options[t.from]}});var n=this.$element.data();function a(t,e){return e.toLowerCase()}n.searchUrl&&(o.log(o.LOG_WARNING,'Deprecated attribute name: "data-search-url". Update markup to use: \' data-abs-ajax-url="'+n.searchUrl+"\" '"),this.options.ajax.url=n.searchUrl);var r=function(t,e,i){var s=[].concat(t),o=s.length,l=i||{};if(o){var n=s.shift();l[n]=r(s,e,l[n])}return o?l:e},p=Object.keys(n).filter(/./.test.bind(new RegExp("^abs[A-Z]")));if(p.length){var c={},h=["locale"];for(i=0,s=p.length;i<s;i++){var u=p[i].replace(/^abs([A-Z])/,a).replace(/([A-Z])/g,"-$1").toLowerCase(),g=u.split("-");if(g[0]&&1<g.length&&-1!==h.indexOf(g[0])){for(var d=[g.shift()],f="",v=0;v<g.length;v++)f+=0===v?g[v]:g[v].charAt(0).toUpperCase()+g[v].slice(1);d.push(f),g=d}this.log(this.LOG_DEBUG,'Processing data attribute "data-abs-'+u+'":',n[p[i]]),r(g,n[p[i]],c)}this.options=G.extend(!0,{},this.options,c),this.log(this.LOG_DEBUG,"Merged in the data attribute options: ",c,this.options)}if(this.selectpicker=n.selectpicker,!this.selectpicker)return this.log(this.LOG_ERROR,"Cannot instantiate an AjaxBootstrapSelect instance without selectpicker first being initialized!"),null;if(!this.options.ajax.url)return this.log(this.LOG_ERROR,'Option "ajax.url" must be set! Options:',this.options),null;if(this.locale=G.extend(!0,{},G.fn.ajaxSelectPicker.locale),this.options.langCode=this.options.langCode||b.navigator.userLanguage||b.navigator.language||"en",!this.locale[this.options.langCode]){var O=this.options.langCode;this.options.langCode="en";var y=O.split("-");for(i=0,s=y.length;i<s;i++){var m=y.join("-");if(m.length&&this.locale[m]){this.options.langCode=m;break}y.pop()}this.log(this.LOG_WARNING,'Unknown langCode option: "'+O+'". Using the following langCode instead: "'+this.options.langCode+'".')}this.locale[this.options.langCode]=G.extend(!0,{},this.locale[this.options.langCode],this.options.locale),this.list=new b.AjaxBootstrapSelectList(this),this.list.refresh(),setTimeout(function(){o.init()},500)}t.prototype.init=function(){var s,o=this;this.options.preserveSelected&&this.selectpicker.$menu.off("click",".actions-btn").on("click",".actions-btn",function(t){o.selectpicker.options.liveSearch?o.selectpicker.$searchbox.focus():o.selectpicker.$button.focus(),t.preventDefault(),t.stopPropagation(),G(this).is(".bs-select-all")?(null===o.selectpicker.$lis&&(o.selectpicker.$lis=o.selectpicker.$menu.find("li")),o.$element.find("option:enabled").prop("selected",!0),G(o.selectpicker.$lis).not(".disabled").addClass("selected")):(null===o.selectpicker.$lis&&(o.selectpicker.$lis=o.selectpicker.$menu.find("li")),o.$element.find("option:enabled").prop("selected",!1),G(o.selectpicker.$lis).not(".disabled").removeClass("selected")),o.selectpicker.render(),o.selectpicker.$element.change()}),this.selectpicker.$searchbox.attr("placeholder",this.t("searchPlaceholder")).off("input propertychange"),this.selectpicker.$searchbox.on(this.options.bindEvent,function(t){var e=o.selectpicker.$searchbox.val();if(o.log(o.LOG_DEBUG,'Bind event fired: "'+o.options.bindEvent+'", keyCode:',t.keyCode,t),o.options.cache||(o.options.ignoredKeys[13]="enter"),o.options.ignoredKeys[t.keyCode])o.log(o.LOG_DEBUG,"Key ignored.");else if(e.length<o.options.minLength)o.list.setStatus(o.t("statusTooShort"));else if(clearTimeout(s),e.length||(o.options.clearOnEmpty&&o.list.destroy(),o.options.emptyRequest)){if(o.previousQuery=o.query,o.query=e,o.options.cache&&13!==t.keyCode){var i=o.list.cacheGet(o.query);if(i)return o.list.setStatus(i.length?"":o.t("statusNoResults")),o.list.replaceOptions(i),void o.log(o.LOG_INFO,"Rebuilt options from cached data.")}s=setTimeout(function(){o.lastRequest&&o.lastRequest.jqXHR&&G.isFunction(o.lastRequest.jqXHR.abort)&&o.lastRequest.jqXHR.abort(),o.request=new b.AjaxBootstrapSelectRequest(o),o.request.jqXHR.always(function(){o.lastRequest=o.request,o.request=!1})},o.options.requestDelay||300)}})},t.prototype.log=function(t,e){if(b.console&&this.options.log){if("number"!=typeof this.options.log)switch("string"==typeof this.options.log&&(this.options.log=this.options.log.toLowerCase()),this.options.log){case!0:case"debug":this.options.log=this.LOG_DEBUG;break;case"info":this.options.log=this.LOG_INFO;break;case"warn":case"warning":this.options.log=this.LOG_WARNING;break;default:case!1:case"error":this.options.log=this.LOG_ERROR}if(t<=this.options.log){var i=[].slice.apply(arguments,[2]);switch(t){case this.LOG_DEBUG:t="debug";break;case this.LOG_INFO:t="info";break;case this.LOG_WARNING:t="warn";break;default:case this.LOG_ERROR:t="error"}var s="["+t.toUpperCase()+"] AjaxBootstrapSelect:";"string"==typeof e?i.unshift(s+" "+e):(i.unshift(e),i.unshift(s)),b.console[t].apply(b.console,i)}}},t.prototype.replaceValue=function(i,s,o,l){var n=this;l=G.extend({recursive:!0,depth:!1,limit:!1},l),G.each(i,function(t,e){if(!1!==l.limit&&"number"==typeof l.limit&&l.limit<=0)return!1;G.isArray(i[t])||G.isPlainObject(i[t])?(l.recursive&&!1===l.depth||l.recursive&&"number"==typeof l.depth&&0<l.depth)&&n.replaceValue(i[t],s,o,l):e===s&&(!1!==l.limit&&"number"==typeof l.limit&&l.limit--,i[t]=o)})},t.prototype.t=function(t,e){return e=e||this.options.langCode,this.locale[e]&&this.locale[e].hasOwnProperty(t)?this.locale[e][t]:(this.log(this.LOG_WARNING,"Unknown translation key:",t),t)},b.AjaxBootstrapSelect=b.AjaxBootstrapSelect||t;function e(i){var s=this;this.$status=G(i.options.templates.status).hide().appendTo(i.selectpicker.$menu);var t=i.t("statusInitialized");t&&t.length&&this.setStatus(t),this.cache={},this.plugin=i,this.selected=[],this.title=null,this.selectedTextFormat=i.selectpicker.options.selectedTextFormat;var o=[];i.$element.find("option").each(function(){var t=G(this),e=t.attr("value");o.push({value:e,text:t.text(),class:t.attr("class")||"",data:t.data()||{},preserved:i.options.preserveSelected,selected:!!t.attr("selected")})}),this.cacheSet("",o),i.options.preserveSelected&&(s.selected=o,i.$element.on("change.abs.preserveSelected",function(t){var e=i.$element.find(":selected");s.selected=[],i.selectpicker.multiple||(e=e.last()),e.each(function(){var t=G(this),e=t.attr("value");s.selected.push({value:e,text:t.text(),class:t.attr("class")||"",data:t.data()||{},preserved:!0,selected:!0})}),s.replaceOptions(s.cacheGet(s.plugin.query))}))}e.prototype.build=function(t){var e,i,s=t.length,o=G("<select/>"),l=G("<optgroup/>").attr("label",this.plugin.t("currentlySelected"));for(this.plugin.log(this.plugin.LOG_DEBUG,"Building the select list options from data:",t),i=0;i<s;i++){var n=t[i],a=G("<option/>").appendTo(n.preserved?l:o);if(n.hasOwnProperty("divider"))a.attr("data-divider","true");else for(e in a.val(n.value).text(n.text).attr("title",n.text),n.class.length&&a.attr("class",n.class),n.disabled&&a.attr("disabled",!0),n.selected&&!this.plugin.selectpicker.multiple&&o.find(":selected").prop("selected",!1),n.selected&&a.attr("selected",!0),n.data)n.data.hasOwnProperty(e)&&a.attr("data-"+e,n.data[e])}l.find("option").length&&l["before"===this.plugin.options.preserveSelectedPosition?"prependTo":"appendTo"](o);var r=o.html();return this.plugin.log(this.plugin.LOG_DEBUG,r),r},e.prototype.cacheGet=function(t,e){var i=this.cache[t]||e;return this.plugin.log(this.LOG_DEBUG,"Retrieving cache:",t,i),i},e.prototype.cacheSet=function(t,e){this.cache[t]=e,this.plugin.log(this.LOG_DEBUG,"Saving to cache:",t,e)},e.prototype.destroy=function(){this.replaceOptions(),this.plugin.list.setStatus(),this.plugin.log(this.plugin.LOG_DEBUG,"Destroyed select list.")},e.prototype.refresh=function(t){this.plugin.selectpicker.$menu.css("minHeight",0),this.plugin.selectpicker.$menu.find("> .inner").css("minHeight",0);var e=this.plugin.t("emptyTitle");!this.plugin.$element.find("option").length&&e&&e.length?this.setTitle(e):(this.title||"static"!==this.selectedTextFormat&&this.selectedTextFormat!==this.plugin.selectpicker.options.selectedTextFormat)&&this.restoreTitle(),this.plugin.selectpicker.refresh(),this.plugin.selectpicker.findLis(),t&&(this.plugin.log(this.plugin.LOG_DEBUG,"Triggering Change"),this.plugin.$element.trigger("change.$")),this.plugin.log(this.plugin.LOG_DEBUG,"Refreshed select list.")},e.prototype.replaceOptions=function(t){var e,i,s,o="",l=[],n=[],a=[];if(t=t||[],this.selected&&this.selected.length){for(this.plugin.log(this.plugin.LOG_INFO,"Processing preserved selections:",this.selected),i=(n=[].concat(this.selected,t)).length,e=0;e<i;e++)(s=n[e]).hasOwnProperty("value")&&-1===a.indexOf(s.value+"")?(a.push(s.value+""),l.push(s)):this.plugin.log(this.plugin.LOG_DEBUG,"Duplicate item found, ignoring.");t=l}t.length&&(o=this.plugin.list.build(t)),this.plugin.$element.html(o),this.refresh(),this.plugin.log(this.plugin.LOG_DEBUG,"Replaced options with data:",t)},e.prototype.restore=function(){var t=this.plugin.list.cacheGet(this.plugin.previousQuery);return t&&this.plugin.list.replaceOptions(t)&&this.plugin.log(this.plugin.LOG_DEBUG,"Restored select list to the previous query: ",this.plugin.previousQuery),this.plugin.log(this.plugin.LOG_DEBUG,"Unable to restore select list to the previous query:",this.plugin.previousQuery),!1},e.prototype.restoreTitle=function(){this.plugin.request||(this.plugin.selectpicker.options.selectedTextFormat=this.selectedTextFormat,this.title?this.plugin.$element.attr("title",this.title):this.plugin.$element.removeAttr("title"),this.title=null)},e.prototype.setTitle=function(t){this.plugin.request||(this.title=this.plugin.$element.attr("title"),this.plugin.selectpicker.options.selectedTextFormat="static",this.plugin.$element.attr("title",t))},e.prototype.setStatus=function(t){(t=t||"").length?this.$status.html(t).show():this.$status.html("").hide()},b.AjaxBootstrapSelectList=b.AjaxBootstrapSelectList||e;function i(t){function e(t){return function(){s.plugin.log(s.plugin.LOG_INFO,"Invoking AjaxBootstrapSelectRequest."+t+" callback:",arguments),s[t].apply(s,arguments),s.callbacks[t]&&(s.plugin.log(s.plugin.LOG_INFO,"Invoking ajax."+t+" callback:",arguments),s.callbacks[t].apply(s,arguments))}}var i,s=this,o=["beforeSend","success","error","complete"],l=o.length;for(this.plugin=t,this.options=G.extend(!0,{},t.options.ajax),this.callbacks={},i=0;i<l;i++){var n=o[i];this.options[n]&&G.isFunction(this.options[n])&&(this.callbacks[n]=this.options[n]),this.options[n]=e(n)}this.options.data&&G.isFunction(this.options.data)&&(this.options.data=this.options.data.apply(this)||{q:"{{{q}}}"}),this.plugin.replaceValue(this.options.data,"{{{q}}}",this.plugin.query),this.jqXHR=G.ajax(this.options)}i.prototype.beforeSend=function(t){this.plugin.list.destroy(),this.plugin.list.setStatus(this.plugin.t("statusSearching"))},i.prototype.complete=function(t,e){if("abort"!==e){var i=this.plugin.list.cacheGet(this.plugin.query);if(i){if(!i.length)return this.plugin.list.destroy(),this.plugin.list.setStatus(this.plugin.t("statusNoResults")),void this.plugin.log(this.plugin.LOG_INFO,"No results were returned.");this.plugin.list.setStatus()}this.plugin.list.refresh(!0)}},i.prototype.error=function(t,e,i){"abort"!==e&&(this.plugin.list.cacheSet(this.plugin.query),this.plugin.options.clearOnError&&this.plugin.list.destroy(),this.plugin.list.setStatus(this.plugin.t("errorText")),this.plugin.options.restoreOnError&&(this.plugin.list.restore(),this.plugin.list.setStatus()))},i.prototype.process=function(t){var e,i,s,o,l,n,a=[],r=[];if(this.plugin.log(this.plugin.LOG_INFO,"Processing raw data for:",this.plugin.query,t),l=t,G.isFunction(this.plugin.options.preprocessData)&&(this.plugin.log(this.plugin.LOG_DEBUG,"Invoking preprocessData callback:",this.plugin.options.processData),null!=(s=this.plugin.options.preprocessData.apply(this,[l]))&&!1!==s&&(l=s)),!G.isArray(l))return this.plugin.log(this.plugin.LOG_ERROR,'The data returned is not an Array. Use the "preprocessData" callback option to parse the results and construct a proper array for this plugin.',l),!1;for(i=l.length,e=0;e<i;e++)o=l[e],this.plugin.log(this.plugin.LOG_DEBUG,"Processing item:",o),G.isPlainObject(o)&&(o.hasOwnProperty("divider")||o.hasOwnProperty("data")&&G.isPlainObject(o.data)&&o.data.divider?(this.plugin.log(this.plugin.LOG_DEBUG,"Item is a divider, ignoring provided data."),a.push({divider:!0})):o.hasOwnProperty("value")?-1===r.indexOf(o.value+"")?(r.push(o.value+""),o=G.extend({text:o.value,class:"",data:{},disabled:!1,selected:!1},o),a.push(o)):this.plugin.log(this.plugin.LOG_DEBUG,"Duplicate item found, ignoring."):this.plugin.log(this.plugin.LOG_DEBUG,'Data item must have a "value" property, skipping.'));if(n=[].concat(a),G.isFunction(this.plugin.options.processData)&&(this.plugin.log(this.plugin.LOG_DEBUG,"Invoking processData callback:",this.plugin.options.processData),null!=(s=this.plugin.options.processData.apply(this,[n]))&&!1!==s)){if(!G.isArray(s))return this.plugin.log(this.plugin.LOG_ERROR,"The processData callback did not return an array.",s),!1;n=s}return this.plugin.list.cacheSet(this.plugin.query,n),this.plugin.log(this.plugin.LOG_INFO,"Processed data:",n),n},i.prototype.success=function(t,e,i){if(!G.isArray(t)&&!G.isPlainObject(t))return this.plugin.log(this.plugin.LOG_ERROR,"Request did not return a JSON Array or Object.",t),void this.plugin.list.destroy();var s=this.process(t);this.plugin.list.replaceOptions(s)},b.AjaxBootstrapSelectRequest=b.AjaxBootstrapSelectRequest||i,G.fn.ajaxSelectPicker=function(t){return this.each(function(){G(this).data("AjaxBootstrapSelect")||G(this).data("AjaxBootstrapSelect",new b.AjaxBootstrapSelect(this,t))})},G.fn.ajaxSelectPicker.locale={},G.fn.ajaxSelectPicker.defaults={ajax:{url:null,type:"POST",dataType:"json",data:{q:"{{{q}}}"}},minLength:0,bindEvent:"keyup",cache:!0,clearOnEmpty:!0,clearOnError:!0,emptyRequest:!1,ignoredKeys:{9:"tab",16:"shift",17:"ctrl",18:"alt",27:"esc",37:"left",39:"right",38:"up",40:"down",91:"meta"},langCode:null,locale:null,log:"error",preprocessData:function(){},preserveSelected:!0,preserveSelectedPosition:"after",processData:function(){},requestDelay:300,restoreOnError:!1,templates:{status:'<div class="status"></div>'}},G.fn.ajaxSelectPicker.locale["en-US"]={currentlySelected:"Currently Selected",emptyTitle:"Select and begin typing",errorText:"Unable to retrieve results",searchPlaceholder:"Search...",statusInitialized:"Start typing a search query",statusNoResults:"No Results",statusSearching:"Searching...",statusTooShort:"Please enter more characters"},G.fn.ajaxSelectPicker.locale.en=G.fn.ajaxSelectPicker.locale["en-US"]}(jQuery,window); |