@progress/kendo-ui
Version:
This package is part of the [Kendo UI for jQuery](http://www.telerik.com/kendo-ui) suite.
3 lines (2 loc) • 16.5 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("kendo.core.js"),require("kendo.buttongroup.js"),require("kendo.icons.js"),require("kendo.toolbar.js"),require("kendo.binder.js"),require("kendo.dropdownlist.js"),require("kendo.datepicker.js"),require("kendo.numerictextbox.js")):"function"==typeof define&&define.amd?define(["exports","kendo.core.min","kendo.buttongroup.min","kendo.icons.min","kendo.toolbar.min","kendo.binder.min","kendo.dropdownlist.min","kendo.datepicker.min","kendo.numerictextbox.min"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).kendo=e.kendo||{},e.kendo._globals=e.kendo._globals||{},e.kendo._globals.Filter={}))}(this,(function(e){var t={number:0,boolean:!1,string:"",date:""},o=({mainFilterLogicLabel:e,uid:t})=>`<div class='k-filter-toolbar'><div class='k-toolbar' aria-label='${e}' id='${t}'></div></div>`,i=({filterLogicLabel:e})=>`<li class='k-filter-item' role='treeitem'><div class='k-filter-toolbar'><div role='toolbar' aria-label='${e}' class='k-toolbar'></div></div></li>`,r=({filterExpressionLabel:e,uid:t})=>`<li class='k-filter-item' role='treeitem'><div class='k-filter-toolbar'><div role='group' aria-label='${e}' class='k-toolbar' id='${t}'></div></div></li>`;!function(e){var s=window.kendo,l=s.ui,a=s.guid,n=s.ns,d=s.keys,p=l.Widget,f=l.ButtonGroup,u=":kendoFocusable",c="change",m="tabindex",b=".kendoFilter",g="Is equal to",h="Is not equal to",v={number:function(t,{field:o}){e(`<input tabindex='-1' id='${a()}' type='text' aria-label='${o}' title='${o}' data-${n}role='numerictextbox' data-${n}bind='value: value'/>`).appendTo(t)},string:function(t,{field:o}){e(`<span class='k-textbox k-input k-input-md k-rounded-md k-input-solid'><input tabindex='-1' id='${a()}' type='text' aria-label='${o}' title='${o}' class='k-input-inner' data-${s.ns}bind='value: value'/></span>`).appendTo(t)},boolean:function(t,{field:o}){e(`<input tabindex='-1' id='${a()}' class='k-checkbox k-checkbox-md k-rounded-md' aria-label='${o}' data-${n}role='checkbox' data-${n}bind='checked: value' type='checkbox'>`).appendTo(t)},date:function(t,{field:o}){e(`<input tabindex='-1' id='${a()}' type='text' aria-label='${o}' title='${o}' data-${n}role='datepicker' data-${n}bind='value: value'/>`).appendTo(t)}},k=f.extend({init:function(e,t){f.fn.init.call(this,e,t)},options:{name:"FilterButtonGroup"},value:function(e){if(void 0===e)return this._value;this._value=e,f.fn._select.call(this,this.wrapper.find("[value='"+e+"']")),this.trigger(c)},_select:function(t){-1!==t&&this.value(e(t).attr("value"))}}),_=p.extend({init:function(t,o){var i,r=this;p.fn.init.call(r,t,o),r.element=e(t).addClass("k-filter"),r.dataSource=o.dataSource,r.operators=e.extend(r.options.operators,o.operators),r._getFieldsInfo(),r._modelChangeHandler=r._modelChange.bind(r),r._renderMain(),o.expression&&r._addExpressionTree(r.filterModel),r._renderApplyButton(),r.options.expressionPreview&&(r._previewContainer||(r._previewContainer=e('<div class="k-filter-preview"></div>').insertAfter(r.element.children().eq(0))),i=r._createPreview(r.filterModel.toJSON()),r._previewContainer.html(i)),r._attachEvents(),r.hasCustomOperators();var s=e(r.element).find(".k-filter-toolbar > .k-toolbar");s.attr(m,-1),s.find(u).attr(m,-1),s.eq(0).attr(m,0)},events:[c],options:{name:"Filter",dataSource:null,expression:null,applyButton:!1,fields:[],mainLogic:"and",messages:{and:"And",or:"Or",apply:"Apply",close:"Close",addExpression:"Add Expression",fields:"Fields",filterExpressionLabel:"filter expression",filterLogicLabel:"filter logic",filterAriaLabel:"filter component",mainFilterLogicLabel:"main filter logic",operators:"Operators",addGroup:"Add Group"},operators:{string:{eq:g,neq:h,startswith:"Starts with",contains:"Contains",doesnotcontain:"Does not contain",endswith:"Ends with",isnull:"Is null",isnotnull:"Is not null",isempty:"Is empty",isnotempty:"Is not empty",isnullorempty:"Has no value",isnotnullorempty:"Has value"},number:{eq:g,neq:h,gte:"Is greater than or equal to",gt:"Is greater than",lte:"Is less than or equal to",lt:"Is less than",isnull:"Is null",isnotnull:"Is not null"},date:{eq:g,neq:h,gte:"Is after or equal to",gt:"Is after",lte:"Is before or equal to",lt:"Is before",isnull:"Is null",isnotnull:"Is not null"},boolean:{eq:g,neq:h}}},applyFilter:function(){var e=this.filterModel.toJSON();this._hasCustomOperators&&this._mapOperators(e),this._hasFieldsFilter(e.filters||[])?(this._removeEmptyGroups(e.filters),this.dataSource.filter(e)):this.dataSource.filter({})},destroy:function(){this.element.off(b),s.destroy(this.element.find(".k-filter-group-main")),this._previewContainer=null,this._applyButton=null,this._modelChangeHandler=null,p.fn.destroy.call(this)},setOptions:function(e){s.deepExtend(this.options,e),this.destroy(),this.element.empty(),this.init(this.element,this.options)},getOptions:function(){var t=e.extend(!0,{},this.options);return delete t.dataSource,t.expression=this.filterModel.toJSON(),t},_addExpressionTree:function(e){if(e.filters)for(var t=this.element.find("[id="+e.uid+"]"),o=0;o<e.filters.length;o++)e.filters[o].logic?this._addGroup(t,e.filters[o]):this._addExpression(t,e.filters[o]),e.filters[o].filters&&this._addExpressionTree(e.filters[o])},_click:function(t){var o=this;t.preventDefault();var i=e(t.currentTarget),r=i.data("command");"x"==r?o._removeExpression(i.closest(".k-toolbar")):"expression"==r?o._addExpression(i.closest(".k-toolbar")):"group"==r?o._addGroup(i.closest(".k-toolbar")):"apply"==r&&o.applyFilter()},_keydown:function(t){var o=this,i=e(t.target),r=t.keyCode,s=i.closest(".k-toolbar"),l=i.is(".k-toolbar");if(r===d.UP&&l)t.preventDefault(),o._focusToolbar(s,"prev");else if(r==d.DOWN&&l)t.preventDefault(),o._focusToolbar(s,"next");else if(r==d.ESC)t.stopPropagation(),o._focusToolbar(s);else if(r==d.ENTER&&l){s.find(".k-toolbar-item").eq(0).attr(m,0).trigger("focus")}},_attachEvents:function(){var e=this,t=e._click.bind(e),o=e._keydown.bind(e);e.element.on("click"+b,"button.k-button",t).on("keydown"+b,".k-filter-toolbar > .k-toolbar, .k-filter-toolbar > .k-toolbar .k-toolbar-item",o)},_focusToolbar:function(t,o,i){var r=t,s=e(this.element).find(".k-filter-toolbar > .k-toolbar");if(s.attr(m,-1),s.find(u).attr(m,-1),"next"==o){let e=Math.min(s.length-1,i||s.index(t)+1);r=s.eq(e)}else if("prev"==o){let e=Math.max(0,i||s.index(t)-1);r=s.eq(e)}r.attr(m,0).trigger("focus")},_addExpression:function(t,o){var i,l=this,a=t.attr("id"),d=t.closest(".k-filter-toolbar").next("ul.k-filter-lines"),p=o?l._fields[o.field]:l._defaultField,f="";o?i=o:((i=y(l.filterModel,a)).filters||i.set("filters",[]),i=l._addNewModel(i.filters,p)),d.length||(d=e("<ul class='k-filter-lines' role='group'></ul>").appendTo(t.closest("li")));var u={fields:l._fields,operators:l.operators[p.type],close:l.options.messages.close,fieldsLabel:l.options.messages.fields,uid:i.uid,ns:s.ns,filterExpressionLabel:l.options.messages.filterExpressionLabel},c=(f=e(s.template(r)(u)).appendTo(d)).find(".k-toolbar").first(),m=p.operators&&p.operators[p.type]?p.operators[p.type]:this.operators[p.type];c.kendoToolBar({resizable:!1,items:[{type:"component",component:"DropDownList",element:`<select data-${n}bind="value: field" title='${l.options.messages.fields}' aria-label='${l.options.messages.fields}' data-auto-width='true'></select>`,attributes:{class:"k-filter-field"},componentOptions:{title:l.options.messages.fields,dataTextField:"text",dataValueField:"value",dataSource:Object.keys(l._fields||{}).map((e=>({value:l._fields[e].name,text:l._fields[e].label})))}},{type:"component",component:"DropDownList",element:`<select data-${n}bind="value: operator" aria-label='${l.options.messages.operators}' title='${l.options.messages.operators}'></select>`,attributes:{class:"k-filter-operator"},componentOptions:{title:l.options.messages.operators,dataTextField:"text",dataValueField:"value",dataSource:Object.keys(m||{}).map((e=>({value:e,text:m[e].text||m[e]})))}},{attributes:{class:"k-filter-value"},template:" "},{type:"button",icon:"x",fillMode:"flat",attributes:{"data-command":"x",title:u.close,"aria-label":u.close}}]}),l._addExpressionControls(f.find(".k-toolbar"),p,i),o||l._expressionChange()},_addExpressionControls:function(e,t,o){var i=e.find(".k-toolbar-item.k-filter-operator"),r=e.find(".k-toolbar-item.k-filter-value");r.attr("ref-toolbar-tool",""),s.destroy(r),r.empty(),this._bindOperators(i,t),this._appendEditor(r,t),this._bindModel(e,o),this._showHideEditor(e,o),e.find(u).attr(m,-1)},_bindOperators:function(e,t){var o=t.operators&&t.operators[t.type]?t.operators[t.type]:this.operators[t.type],i=e.find("select[data-role=dropdownlist]").getKendoDropDownList();i&&i.setDataSource(Object.keys(o||{}).map((e=>({value:e,text:o[e].text||o[e]}))))},_appendEditor:function(t,o){s.isFunction(o.editor)?o.editor(t,e.extend(!0,{},{field:o.name})):e(s.template(o.editor)({ns:s.ns,field:o.name,id:s.guid()})).appendTo(t)},_addNewModel:function(e,t){var o,i,r=t.type,s=t.operators;return s||(s=this.options.operators),i=Object.keys(s[r])[0],e.push({field:t.name}),(o=e[e.length-1]).set("value",t.defaultValue),o.set("operator",i),o},_addGroup:function(t,o){let r,l=this,a=l.filterModel,n=t.attr("id"),d=t.closest(".k-filter-toolbar").next("ul.k-filter-lines");o?a=o:(a=y(a,n),a.filters||a.set("filters",[]),a.filters.push({logic:l.options.mainLogic}),a=a.filters[a.filters.length-1]),d.length||(d=e("<ul class='k-filter-lines' role='group'></ul>").appendTo(t.closest("li")));let p={operators:{and:l.options.messages.and,or:l.options.messages.or},addExpression:l.options.messages.addExpression,addGroup:l.options.messages.addGroup,close:l.options.messages.close,ns:s.ns,filterLogicLabel:l.options.messages.filterLogicLabel,filterModel:a};r=e(s.template(i)(p)).appendTo(d);let f=r.find(".k-toolbar");l._initGroupToolBar(f,p),l._bindModel(f,a),o||l._expressionChange()},_bindModel:function(e,t){e.attr("id",t.uid),t.bind("change",this._modelChangeHandler),s.bind(e,t),e.parent().attr(s.attr("stop"),!0)},_createPreview:function(e){var t,o,i="",r=!1,l=this._hasFieldsFilter(e.filters||[]),a="";if(!e.filters||!e.filters.length||!l)return"";i+='<span class="k-filter-preview-bracket">(</span>';for(var n=0;n<e.filters.length;n++)(t=e.filters[n]).filters&&((a=this._createPreview(t))&&(r&&(i+='<span class="k-filter-preview-operator"> '+e.logic.toLocaleUpperCase()+" </span>"),r=!0),i+=a),t.field&&(o=this._fields[t.field],r&&(i+='<span class="k-filter-preview-operator"> '+e.logic.toLocaleUpperCase()+" </span>"),r=!0,i+='<span class="k-filter-preview-field">'+o.label+"</span>",i+='<span class="k-filter-preview-criteria"> '+this._getOperatorText(t.field,t.operator),t.operator.indexOf("is")<0?(i+=" </span>",i+="<span class='k-filter-preview-value'>'"+s.htmlEncode(o.previewFormat?s.toString(t.value,o.previewFormat):t.value)+"'</span>"):i+="</span>");return i+='<span class="k-filter-preview-bracket">)</span>'},_expressionChange:function(){var e=this,t=e.filterModel.toJSON(),o="";e.options.expressionPreview&&(o=e._createPreview(t),e._previewContainer.html(o)),e.trigger(c,{expression:t})},_getOperatorText:function(e,t){var o=this._fields[e].type,i=this._fields[e].operators;return i||(i=this.options.operators),i[o][t].text||i[o][t]},_addField:function(o,i){var r=this;o=e.extend(!0,{},{name:o.name||i,editor:o.editorTemplate||v[o.type||"string"],defaultValue:o.defaultValue||!1===o.defaultValue||0===o.defaultValue?o.defaultValue:t[o.type||"string"],type:o.type||"string",label:o.label||o.name||i,operators:o.operators,previewFormat:o.previewFormat}),r._fields[o.name]=o,r._defaultField||(r._defaultField=o)},_getFieldsInfo:function(){var e,t=this,o=t.options.fields.length?t.options.fields:(t.options.dataSource.options.schema.model||{}).fields;if(t._fields={},Array.isArray(o))for(var i=0;i<o.length;i++)e=o[i],t._addField(e);else for(var r in o)e=o[r],t._addField(e,r)},_hasFieldsFilter:function(e,t){t=!!t;for(var o=0;o<e.length;o++)if(e[o].filters&&(t=this._hasFieldsFilter(e[o].filters,t)),e[o].field)return!0;return t},_removeEmptyGroups:function(e){if(e)for(var t=e.length-1;t>=0;t--)e[t].logic&&!e[t].filters||e[t].filters&&!this._hasFieldsFilter(e[t].filters)?e.splice(t,1):e[t].filters&&this._removeEmptyGroups(e[t].filters)},_modelChange:function(e){var t=this,o=t.element.find("[id="+e.sender.uid+"]");if(t._showHideEditor(o,e.sender),"field"===e.field){var i=e.sender.field,r=e.sender.parent(),s=t._fields[i],l=t._addNewModel(r,s);e.sender.unbind("change",t._modelChangeHandler),r.remove(e.sender),t._addExpressionControls(o,s,l),t._expressionChange()}else"filters"!==e.field&&t._expressionChange()},_renderMain:function(){let t=this;var i;e((i=t.options.messages.filterAriaLabel,`<ul class='k-filter-container' role='tree' aria-label='${i}'><li class='k-filter-group-main' role='treeitem'></li></ul>`)).appendTo(t.element),t.options.expression?t.filterModel=s.observable(t.options.expression):t.filterModel=s.observable({logic:t.options.mainLogic});let r={operators:{and:t.options.messages.and,or:t.options.messages.or},addExpression:t.options.messages.addExpression,addGroup:t.options.messages.addGroup,close:t.options.messages.close,uid:t.filterModel.uid,ns:s.ns,mainFilterLogicLabel:t.options.messages.mainFilterLogicLabel,filterModel:t.filterModel},l=e(s.template(o)(r));l.appendTo(t.element.find("li").first());let a=l.find(".k-toolbar").first();t._initGroupToolBar(a,r),t._bindModel(a,t.filterModel)},_initGroupToolBar:function(t,o){let i=this;t.kendoToolBar({resizable:!1,toggle:function(t){let o=e(t.target).closest(".k-toolbar").attr("id"),r=y(i.filterModel,o);r&&r.set("logic",e(t.target).data("logic"))},items:[{type:"buttonGroup",selection:"single",buttons:[{text:i.options.messages.and,group:"mainlogic",togglable:!0,selected:"and"===o.filterModel.get("logic"),attributes:{"data-logic":"and"}},{text:i.options.messages.or,group:"mainlogic",togglable:!0,selected:"or"===o.filterModel.get("logic"),attributes:{"data-logic":"or"}}]},{type:"button",icon:"filter-add-expression",attributes:{"data-command":"expression",title:o.addExpression,"aria-label":o.addExpression}},{type:"button",icon:"filter-add-group",attributes:{"data-command":"group",title:o.addGroup,"aria-label":o.addGroup}},{type:"button",icon:"x",fillMode:"flat",attributes:{"data-command":"x",title:o.close,"aria-label":o.close}}]})},_removeExpression:function(t){var o,i,r=this,l=t.attr("id"),a=t.closest("li"),n=-1;if(a.hasClass("k-filter-group-main"))a=a.find(".k-filter-lines"),r.filterModel.filters&&(r.filterModel.filters.empty(),delete r.filterModel.filters);else{n=e(r.element).find(".k-filter-toolbar > .k-toolbar").index(t),o=(i=y(r.filterModel,l)).parent(),i.unbind("change",r._modelChangeHandler),o.remove(i),o.length||delete o.parent().filters,a.siblings().length||(a=a.parent())}s.destroy(a),a.remove(),r._expressionChange(),n>-1&&r._focusToolbar(t,"next",n)},_renderApplyButton:function(){var t=this;t.options.applyButton&&(t._applyButton||(t._applyButton=e(s.format('<button type="button" data-command="apply" aria-label="{0}" title="{0}" class="k-button k-button-md k-rounded-md k-button-solid k-button-solid-base k-filter-apply"><span class="k-button-text">{0}</span></button>',t.options.messages.apply)).appendTo(t.element)))},_showHideEditor:function(e,t){if(!t.logic){var o=t.operator,i=e.find(".k-toolbar-item.k-filter-value");"isnull"==o||"isnotnull"==o||"isempty"==o||"isnotempty"==o||"isnullorempty"==o||"isnotnullorempty"==o?i.hide():i.show()}},_mapOperators:function(e){var t=this;e.filters&&e.filters.forEach((function(e){if(e.filters)t._mapOperators(e);else{var o,i=t._fields[e.field],r=i.type;(o=i.operators&&i.operators[r][e.operator]?i.operators[r][e.operator]:t.operators[r][e.operator])&&(e.operator=o.handler||e.operator)}}))},hasCustomOperators:function(){var t=e.extend(!0,{},this.operators);for(var o in this._fields)t=e.extend(!0,{},t,this._fields[o].operators);this._hasCustomOperators=x(t)}});function x(e){for(var t in e){var o=e[t];if(o.handler&&"function"==typeof o.handler||"object"==typeof o&&null!==o&&x(o))return!0}return!1}function y(e,t){if(e.uid===t)return e;if(e.filters)for(var o=0;o<e.filters.length;o++){var i=y(e.filters[o],t);if(i)return i}}l.plugin(_),l.plugin(k)}(window.kendo.jQuery);var s=kendo;e.__meta__={id:"filter",name:"Filter",category:"web",depends:["core","buttongroup","icons","toolbar"]},e.default=s,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=kendo.filter.min.js.map