123 |
- /*! jquery-cascading-dropdown 1.2.9 | (c) 2019 Dzulqarnain Nasir <dzul1983@gmail.com> | MIT */
- !function(a,s){"use strict";var t={usePost:!1,useJson:!1};function n(e,n){this.el=a(e.selector,n.el),this.parent=n,this.options=a.extend({},t,e),this.name=this.options.paramName||this.el.attr("name"),this.requiredDropdowns=e.requires&&e.requires.length?a(e.requires.join(","),n.el):null,this.isLoadingClassName=this.options.isLoadingClassName||n.options.isLoadingClassName||"cascading-dropdown-loading"}function o(e,n){this.el=a(e),this.options=a.extend({selectBoxes:[]},n),this._init()}n.prototype={_create:function(){var t=this;t.pending=0,t.initialised=!1,t.initialState=t.el.clone(!0),t.el.data("plugin_cascadingDropdown",this),t.originalDropdownItems=t.el.children("option"),"function"==typeof t.options.onChange&&t.el.change(function(e){var n=t._requirementsMet()&&t.el[0].value;t.options.onChange.call(t,e,t.el.val(),t.getRequiredValues(),n)}),"function"==typeof t.options.onEnable&&t.el.on("enabled",function(e){t.options.onEnable.call(t,e,t.el.val())}),"function"==typeof t.options.onDisable&&t.el.on("disabled",function(e){t.options.onDisable.call(t,e,t.el.val())}),t.requiredDropdowns&&t.requiredDropdowns.change(function(){t.update()}),t._initSource(),t.update()},_destroy:function(){this.el.replaceWith(this.initialState).removeData("plugin_cascadingDropdown")},enable:function(){this.el.attr("disabled")!==s&&this.el.removeAttr("disabled").triggerHandler("enabled")},disable:function(){this.el.attr("disabled")===s&&this.el.attr("disabled","disabled").triggerHandler("disabled")},_requirementsMet:function(){var e=this;return!e.requiredDropdowns||(e.options.requireAll?e.requiredDropdowns.filter(function(){return!!a(this).val()}).length==e.options.requires.length:0<e.requiredDropdowns.filter(function(){return!!a(this).val()}).length)},_initSource:function(){var t=this;if(a.isArray(t.options.source))this.source=function(e,n){n(a.map(t.options.source,function(e){return{label:e.label||e.value||e,value:e.value||e.label||e,selected:e.selected}}))};else if("string"==typeof t.options.source){var i=t.options.source;this.source=function(e,n){t.xhr&&t.xhr.abort(),t.xhr=a.ajax({url:i,data:t.options.useJson?JSON.stringify(e):e,dataType:t.options.useJson?"json":s,type:t.options.usePost?"post":"get",contentType:"application/json; charset=utf-8",success:function(e){n(e)},error:function(){n([])}})}}else this.source=t.options.source},getRequiredValues:function(){var n={};return this.requiredDropdowns&&a.each(this.requiredDropdowns,function(){var e=a(this).data("plugin_cascadingDropdown");e.name&&(n[e.name]=e.el.val())}),n},update:function(){var e=this;if(e.disable(),!e._requirementsMet())return e.setSelected(0),e._triggerReady(),e.el;if(!e.source)return e.enable(),e._triggerReady(),e.el;e.el.val("").change();var n=e.getRequiredValues();return e.pending++,e.el.addClass(e.isLoadingClassName),e.source(n,e._response()),e.el},_response:function(e){var n=this;return function(e){n._renderItems(e),n.pending--,n.pending||n.el.removeClass(n.isLoadingClassName)}},_renderItems:function(e){var r=this;if(r.el.find("option, optgroup").remove(),r.el.append(r.originalDropdownItems),e){var s=[];a.isArray(e)?a.each(e,function(e,n){r.el.append(r._renderItem(n)),n.selected&&s.push(n.value.toString())}):a.each(e,function(e,n){var t=[];t.push('<optgroup label="'+e+'">');for(var i=0;i<n.length;i++){var o=n[i];t.push(r._renderItem(o)),o.selected&&s.push(o.value.toString())}t.push("</optgroup>"),r.el.append(t.join(""))}),r.enable(),s.length&&r.setSelected(s),r._triggerReady()}else r._triggerReady()},_renderItem:function(e){return'<option value="'+e.value+'"'+(e.selected?" selected":"")+">"+e.label+"</option>"},_triggerReady:function(){this.initialised||(this.options.selected&&this.setSelected(this.options.selected),this.initialised=!0,this.el.triggerHandler("ready"))},setSelected:function(e,n){var t=this,i=t.el.find("option");void 0===n&&(n=!0);var o,r=[];return r=a.isArray(e)?e:r.concat(e),t.el.is("[multiple]")?o=r.map(function(e){return"number"==typeof e&&e!==s&&-1<e&&e<i.length?i[e].value:e}):(o=r[0],"number"==typeof r[0]&&r[0]!==s&&-1<r[0]&&r[0]<i.length&&(o=i[r[0]].value)),t.el.val(o),n&&t.el.change(),t.el}},o.prototype={_init:function(){var t=this;t.pending=0,t.dropdowns=[];var i=a(a.map(t.options.selectBoxes,function(e){return e.selector}).join(","),t.el),o=0;"function"==typeof t.options.onReady&&i.bind("ready",function e(n){++o==i.length&&(i.unbind("ready",e),t.options.onReady.call(t,n,t.getValues()))}),"function"==typeof t.options.onChange&&i.bind("change",function(e){t.options.onChange.call(t,e,t.getValues())}),a.each(t.options.selectBoxes,function(){var e=new n(this,t);t.dropdowns.push(e),e._create()})},destroy:function(){return a.each(this.dropdowns,function(e,n){n._destroy()}),this.el.removeData("plugin_cascadingDropdown"),this.el},getValues:function(){var t={};return a.each(this.dropdowns,function(e,n){n.name&&(t[n.name]=n.el.val())}),t}},a.fn.cascadingDropdown=function(e){var n=a(this),t=arguments,i=n.data("plugin_cascadingDropdown");if("object"==typeof e||!e)return!i&&n.data("plugin_cascadingDropdown",new o(this,e));if("string"==typeof e)if(i){if(i[e])return i[e].apply(i,Array.prototype.slice.call(t,1))}else a.error("Cannot call method "+e+" before init.");else a.error("Method "+e+" does not exist in jQuery.cascadingDropdown")}}(jQuery);
- //# sourceMappingURL=jquery.cascadingdropdown.min.js.map
|