UNPKG

@vdt-jquery/jquery-datatreeview

Version:

A jQuery plugin that makes it easy to create a tree view for your tree-like data. It allows you to create a tree view on the fly for a tree of data-objects while leaving you in full control of where the data comes from.

1 lines 7.31 kB
(function(n){function t(t,r){let u=this;this.element=t;this.options=r;this.valueProperty=r.getValueProperty(this.element);this.textProperty=r.getTextProperty(this.element);this.selectedProperty=r.getSelectedProperty(this.element);this.childrenProperty=r.getChildrenProperty(this.element);this.fieldName=r.getFieldName(this.element);this.hasFreehandSelection=r.hasFreehandSelection(this.element);this.isDisabled=!1;this.toggleOptions=r.getToggleOptions(this.element);this.element.children().hide();this.element.addClass("datatreeview");this.list=this.createElement("<ul>","datatreeview-list",this.options.getListAttributes());this.element.append(this.list);let f=r.isCollapsed(this.element);n.each(this.options.data,function(n,t){u.createNode(u.list,t,f)});this.options.isDisabled(this.element)&&this.disable();this.list.on("click",".datatreeview-toggler",this,i.togglerClick);this.list.on("change",".datatreeview-field",this,i.inputChange)}n.fn.datatreeview=function(i,r){return n.isFunction(i)&&(r=i,i=null),n(this).each(function(){let u=n(this).data("datatreeview");if(!u){if(i&&i.data){if(!n.isArray(i.data))throw'datatreeview error: expected option "data" to be an array';}else throw'datatreeview error: expected required option "data"';let r=n.extend({},n.fn.datatreeview.defaults,i);u=new t(n(this),r);n(this).data("datatreeview",u)}n.isFunction(r)&&r.bind(u)(u)})};n.fn.datatreeview.defaults={getValueProperty:function(t){return n(t).data("value-property")||"value"},getTextProperty:function(t){return n(t).data("text-property")||"text"},getSelectedProperty:function(t){return n(t).data("selected-property")||"selected"},getChildrenProperty:function(t){return n(t).data("children-property")||"children"},getFieldName:function(t){return n(t).data("field-name")},hasFreehandSelection:function(t){return n(t).data("freehand-select")!==undefined&&n(t).data("freehand-select")!=!1},isDisabled:function(t){return n(t).data("disabled")!==undefined&&n(t).data("disabled")!=!1},getToggleOptions:function(t){var i=parseInt(n(t).data("toggle-duration")),r=n(t).data("toggle-easing");return(isNaN(i)||i<0)&&(i=0),{duration:i,easing:r}},isCollapsed:function(t){return n(t).data("collapsed")!==undefined&&n(t).data("collapsed")!=!1},getListAttributes:function(){return{}},getNodeAttributes:function(){return{}},getNodeContentAttributes:function(){return{}},getInputAttributes:function(){return{}},getLabelAttributes:function(){return{}},getTogglerAttributes:function(){return{}}};t.prototype.createElement=function(t,i){let r=n.extend.apply({},Array.prototype.slice.call(arguments,2));return n(t,r).addClass(i)};t.prototype.createList=function(t,i,r){let f=this,u=this.createElement("<ul>","datatreeview-list",this.options.getListAttributes()),e=this.createElement("<div>","datatreeview-toggler",this.options.getTogglerAttributes());r&&(t.addClass("datatreeview-node-collapsed"),u.hide());t.find(".datatreeview-node-content").prepend(e);t.append(u);n.each(i,function(n,t){f.createNode(u,t,r)})};t.prototype.createNode=function(n,t,i){let r=this.createElement("<li>","datatreeview-node",this.options.getNodeAttributes()),f=this.createElement("<div>","datatreeview-node-content",this.options.getNodeContentAttributes()),u=this.createElement("<input>","datatreeview-field",{type:"checkbox",name:this.fieldName,value:t[this.valueProperty]},this.options.getInputAttributes()),s=this.createElement("<label>","datatreeview-text",this.options.getLabelAttributes()).text(t[this.textProperty]).prepend(u),e=t[this.childrenProperty]&&t[this.childrenProperty].length>0,o=this.hasFreehandSelection||!e;f.append(s);r.append(f);r.data("node-data",t);n.append(r);e&&this.createList(r,t[this.childrenProperty],i);o&&t[this.selectedProperty]&&u.prop("checked",!0);o||r.find(".datatreeview-list input.datatreeview-field:checked").length!==r.find(".datatreeview-list input.datatreeview-field").length||u.prop("checked",!0)};t.prototype.remove=function(){this.list.remove();this.element.removeClass("datatreeview");this.element.removeData("datatreeview");this.element.children().show()};t.prototype.getSelectedNodes=function(){return this.element.find("input.datatreeview-field:checked").closest("li.datatreeview-node")};t.prototype.getSelectedData=function(){return this.getSelectedNodes().map(function(){return n(this).data("node-data")}).get()};t.prototype.getSelectedValues=function(){let n=this;return this.getSelectedData().map(function(t){return t[n.valueProperty]})};t.prototype.setSelectedNodes=function(t){t=n(t);this.hasFreehandSelection||(t=t.not(":has(li)"));this.list.find("li.datatreeview-node").not(t).find("> .datatreeview-node-content > label.datatreeview-text > input.datatreeview-field").prop("checked",!1);n(t).find("> .datatreeview-node-content > label.datatreeview-text > input.datatreeview-field").prop("checked",!0);this.hasFreehandSelection||this.list.find("li.datatreeview-node:has(li.datatreeview-node):not(:has(li.datatreeview-node:not(:has(li.datatreeview-node)) input.datatreeview-field:not(:checked)))").find("> .datatreeview-node-content > label.datatreeview-text > input.datatreeview-field").prop("checked",!0);this.triggerSelectionChanged()};t.prototype.triggerSelectionChanged=function(){this.element.trigger("datatreeview.selectionChanged",[this.getSelectedData(),this.getSelectedValues()])};t.prototype.setSelectedData=function(t){let i=this.list.find("li.datatreeview-node").filter(function(){return t(n(this).data("node-data"))});this.setSelectedNodes(i)};t.prototype.setSelectedValues=function(n){let t=this;this.setSelectedData(function(i){return n.indexOf(i[t.valueProperty])>-1})};t.prototype.enable=function(){this.isDisabled&&(this.isDisabled=!1,this.element.find("input.datatreeview-field").prop("disabled",this.isDisabled),this.element.removeClass("datatreeview-disabled"))};t.prototype.disable=function(){this.isDisabled||(this.isDisabled=!0,this.element.find("input.datatreeview-field").prop("disabled",this.isDisabled),this.element.addClass("datatreeview-disabled"))};t.prototype.collapseNodes=function(t){t=n(t).not(".datatreeview-node-collapsed");t.length>0&&(t.addClass("datatreeview-node-collapsed"),t.children(".datatreeview-list").slideToggle(this.toggleOptions),this.element.trigger("datatreeview.nodesCollapsed",[t]))};t.prototype.expandNodes=function(t){t=n(t).filter(".datatreeview-node-collapsed");t.length>0&&(t.removeClass("datatreeview-node-collapsed"),t.children(".datatreeview-list").slideToggle(this.toggleOptions),this.element.trigger("datatreeview.nodesExpanded",[t]))};let i={togglerClick:function(t){let i=n(t.target).closest("li");i.hasClass("datatreeview-node-collapsed")?t.data.expandNodes(i):t.data.collapseNodes(i)},inputChange:function(t){if(!t.data.hasFreehandSelection){let r=n(t.target).is(":checked"),i=n(t.target).closest("li.datatreeview-node");i.find("input.datatreeview-field").prop("checked",r);r?i.parents("li.datatreeview-node").find("> .datatreeview-node-content > label.datatreeview-text > input.datatreeview-field").prop("checked",function(){return n(this).closest("li.datatreeview-node").find("input.datatreeview-field").not(this).not(":checked").length==0}):i.parents("li.datatreeview-node").find("> .datatreeview-node-content> label.datatreeview-text > input.datatreeview-field").prop("checked",!1)}t.data.triggerSelectionChanged()}}})(jQuery);