linbei/static/bootstrap.dropdown.cascade/js/bootstrap-cascader-dcbf0e32...

3 lines
6.8 KiB
JavaScript

!function(i){"use strict";var l={splitChar:" ",btnCls:"btn-default",placeHolder:"请选择",dropUp:!1,lazy:!1,openOnHover:!1,onChange:i.noop,selectable:function(e){return e&&e.loaded&&(!e.children||e.children.length<=0||e.selectable)}},n='<div class="btn-group bootstrap-cascader"></div>',t='<button class="btn dropdown-toggle bs-placeholder" type="button"> <span class="filter-option pull-left"></span> <span class="caret icon-arrow-down"></span> <span class="icon-cross iconfont icon-jiaochacross78"></span> </button>',s='<ul class="dropdown-menu"></ul>',d='<li> <a href="javascript:"> <span class="text"></span> <span class="iconfont icon-ico-right-arrow item-right-arrow"></span> <span class="iconfont icon-loading item-loading"></span> <span class="iconfont icon-error"></span> </a> </li>',p=function(e){return e?String(e).replace(/&/g,"&amp;").replace(/>/g,"&gt;").replace(/</g,"&lt;").replace(/"/g,"&quot;"):""},o=function(e,o){if(e&&e.children&&!(e.children.length<=0)){var c=this;this.selectItem=function(e,a){o.params.selectable.call(o,e)&&o.selectItem(a)},this.setItemOpened=function(e,a){c.panelEl.children("li").removeClass("open"),a.addClass("open"),e.loaded&&0==e.children.length&&a.addClass("no-child")},this.selectItemByCode=function(e){c.panelEl.children("li[code="+p(e)+"]").trigger("selectItem")},this.setSelected=function(e,a){var l=c.panelEl.children("li[code="+p(e.code||e.c)+"]");l.children("a").addClass("selected"),a&&c.setItemOpened(e,l),c.scrollToOpened()},this.isMatchedSelectedItems=function(e){for(var a=e,l=e.level-1;0<=l;l--){var n=o.selectedItems[l],t=n.code||n.c,s=a.originData;if(t!=(s.code||s.c))return!1;a=a.parent}return!0},this.scrollToOpened=function(){var e=c.panelEl,a=e.find("li a.selected").parent();if(0<a.size()){var l=e.scrollTop(),n=a.position().top+l;l<n&&(n-=(e.height()-a.height())/2,e.scrollTop(n))}};var r=function(e,a,l){if(o.params.lazy&&!1===e.loaded){a.addClass("loading");var n=e.code||e.c;c.loadingItem&&(c.loadingItem=!1),c.loadingItem=n,o.loadData(e).then(function(){c.loadingItem==n&&o.refreshPanels(e.level+1,e)},function(){c.loadingItem==n&&a.addClass("load-error")}).always(function(){c.loadingItem==n&&(c.setItemOpened(e,a),c.loadingItem=!1),a.removeClass("loading")})}else o.refreshPanels(e.level+1,e),c.setItemOpened(e,a),l&&c.selectItem(e,a)};c.panelEl=i(s).appendTo(o.el),c.data=e;var a=o.panels[o.panels.length-1];if(a){var l=a.panelEl,n=Number((l.css("left")||"").replace("px",""));c.panelEl.css({left:n+l.outerWidth()})}o.panels.push(c),0<o.panels.length&&(o.el.children(".dropdown-menu").removeClass("first-child last-child"),o.panels[0].panelEl.addClass("first-child"),o.panels[o.panels.length-1].panelEl.addClass("last-child")),i.each(e.children,function(e,a){var l=i(d).appendTo(c.panelEl),n=a.originData,t=n.name||n.n,s=n.code||n.c;l.data("cascaderItem",a).attr("code",s).attr("title",t).find(".text").text(t),l.bind("selectItem click",function(){r(a,l,!0)}).bind("openDropdown",function(){r(a,l)}),o.params.openOnHover&&l.bind("mouseover",function(){o.openTimeout&&clearTimeout(o.openTimeout),o.openTimeout=setTimeout(function(){r(a,l)},o.params.lazy?200:100)}),a.loaded&&(!a.children||a.children.length<=0)&&l.addClass("no-child"),o.selectedItems.length>=a.level&&c.isMatchedSelectedItems(a)&&(o.selectedItems.length>a.level?r(a,l,!1):i.each(o.selectedItems,function(e,a){o.panels[e].setSelected(a,e<o.selectedItems.length-1)}))}),this.destroy=function(){c.panelEl.remove()}}},c=function(e){var c=this;for(var a in e=e||{},l)"undefined"==typeof e[a]&&(e[a]=l[a]);c.params=e,c.initialized=!1,c.selectedItems=[],c.readonly=!1,c.params.value instanceof Array&&(c.selectedItems=[]);var r=function(e,s){var o=c.params.lazy;i.each(e,function(e,a){var l=a.code||a.c,n=a.data||a.d,t={childMap:{},children:[],loaded:!o||!1===a.hasChild,level:s.level+1,parent:s,originData:a};s.children.push(t),s.childMap[l]=t,n&&0<n.length&&r(n,t)})},d=function(e){c.btn.attr("title",e).children(".filter-option").text(e),0<c.getValue().length?c.btn.removeClass("bs-placeholder").addClass("selected"):c.btn.addClass("bs-placeholder").removeClass("selected")};c.setValue=function(e){c.clearValue();var l=[],a=c.getValue;i.each(e,function(e,a){c.selectedItems.push({code:a.code||a.c,name:a.name||a.n}),l.push(a.name||a.n)}),d(l.join(c.params.splitChar)),c.updateViewBySelected(),c.params.onChange(a,e)},c.refreshPanels=function(e,a){var l=c.panels.splice(e-1,c.panels.length);l&&i.each(l,function(e,a){a.destroy()}),new o(a,c)},c.selectItem=function(e){c.el.find("li a").removeClass("selected"),e.children("a").addClass("selected"),c.el.find("li.open a").addClass("selected");var a=e.data("cascaderItem"),l=[],n=c.getValue();for(c.selectedItems=[];a.parent;){var t=a.originData,s=t.code||t.c,o=t.name||t.n;c.selectedItems.unshift({code:s,name:o}),l.unshift(o),a=a.parent}d(l.join(c.params.splitChar)),c.close(),c.params.onChange(n,c.getValue())},c.close=function(){c.el.removeClass("open"),c.updateViewBySelected()},c.updateViewBySelected=function(){0<c.selectedItems.length&&c.panels[0].selectItemByCode(c.selectedItems[0].code||c.selectedItems[0].c)},c.open=function(){c.readonly||(c.el.toggleClass("open"),i.each(c.panels,function(e,a){a.scrollToOpened()}))},c.getValue=function(){return c.selectedItems.slice()},c.clearValue=function(){c.readonly||(c.selectedItems=[],c.el.find(".dropdown-menu li a").removeClass("selected"),d(c.params.placeHolder))},c.setReadonly=function(e){e=!1!==e,(c.readonly=e)?(c.el.addClass("readonly"),c.btn.addClass("disabled")):(c.el.removeClass("readonly"),c.btn.removeClass("disabled"))},c.isReadonly=function(){return c.readonly},c.loadData=function(a){var l=[];if(a)for(var e=a;e.parent;)l.unshift(e.originData),e=e.parent;else a=c.data;var n=i.Deferred();return c.params.loadData.call(c,l,function(e){e?(0<e.length&&((!l||l.length<=0)&&(c.data.originData=e),r(e,a)),a.loaded=!0,n.resolve(e)):n.reject()}),n.promise()},c.data={childMap:{},children:[],loaded:!1,level:0},c.panels=[],c.cols=[],c.el=i(n),c.params.replace?(c.el.insertAfter(e.el),e.el.remove()):c.el.appendTo(e.el),e.el=c.el,c.btn=i(t).addClass(e.btnCls).click(function(){c.open()}).appendTo(c.el),c.btn.children(".icon-cross").click(function(e){c.clearValue(),e.preventDefault(),e.stopPropagation()}),d(e.placeHolder),c.params.readonly&&c.setReadonly(!0),c.loadData().always(function(){c.refreshPanels(1,c.data),c.params.value&&c.setValue(c.params.value)}),i("html").click(function(e){if(c.el.hasClass("open")){var a=i(e.target).parents(".bootstrap-cascader");0==a.size()?c.close():a[0]!=c.el[0]&&c.close()}})};i.fn.bsCascader=function(l){var n=arguments;return this.each(function(){if(this){var e=i(this),a=e.data("bsCascader");a||(l=i.extend({el:e,value:e.val()?e.val().split(l.splitChar||" "):""},l),a=new c(l),e.data("bsCascader",a)),"string"==typeof l&&a[l].apply(a,Array.prototype.slice.call(n,1))}})}}(jQuery);
//# sourceMappingURL=bootstrap-cascader-dcbf0e3207.min.js.map