You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
8 lines
3.9 KiB
8 lines
3.9 KiB
4 years ago
|
/*
|
||
|
* Fuel UX Tree
|
||
|
* https://github.com/ExactTarget/fuelux
|
||
|
*
|
||
|
* Copyright (c) 2012 ExactTarget
|
||
|
* Licensed under the MIT license.
|
||
|
*/
|
||
|
(function(b,d){var a=b.fn.tree;var c=function(f,e){this.$element=b(f);this.options=b.extend({},b.fn.tree.defaults,e);this.$element.on("click",".tree-item",b.proxy(function(g){this.selectItem(g.currentTarget)},this));this.$element.on("click",".tree-folder-header",b.proxy(function(g){this.selectFolder(g.currentTarget)},this));this.render()};c.prototype={constructor:c,render:function(){this.populate(this.$element)},populate:function(g){var f=this;var h=g.parent();var e=h.find(".tree-loader:eq(0)");e.show();this.options.dataSource.data(g.data(),function(i){e.hide();b.each(i.data,function(j,m){var k;if(m.type==="folder"){k=f.$element.find(".tree-folder:eq(0)").clone().show();k.find(".tree-folder-name").html(m.name);k.find(".tree-loader").html(f.options.loadingHTML);var n=k.find(".tree-folder-header");n.data(m);if("icon-class" in m){n.find("i").addClass(m["icon-class"])}}else{if(m.type==="item"){k=f.$element.find(".tree-item:eq(0)").clone().show();k.find(".tree-item-name").html(m.name);k.data(m);if("additionalParameters" in m&&"item-selected" in m.additionalParameters&&m.additionalParameters["item-selected"]==true){k.addClass("tree-selected");k.find("i").removeClass(f.options["unselected-icon"]).addClass(f.options["selected-icon"])}}}var l=m.dataAttributes||[];b.each(l,function(o,p){switch(o){case"class":case"classes":case"className":k.addClass(p);break;default:k.attr(o,p);break}});if(g.hasClass("tree-folder-header")){h.find(".tree-folder-content:eq(0)").append(k)}else{g.append(k)}});f.$element.trigger("loaded",h)})},selectItem:function(g){if(this.options.selectable==false){return}var f=b(g);var i=this.$element.find(".tree-selected");var h=[];if(this.options.multiSelect){b.each(i,function(k,l){var j=b(l);if(j[0]!==f[0]){h.push(b(l).data())}})}else{if(i[0]!==f[0]){i.removeClass("tree-selected").find("i").removeClass(this.options["selected-icon"]).addClass(this.options["unselected-icon"]);h.push(f.data())}}var e="selected";if(f.hasClass("tree-selected")){e="unselected";f.removeClass("tree-selected");f.find("i").removeClass(this.options["selected-icon"]).addClass(this.options["unselected-icon"])}else{f.addClass("tree-selected");f.find("i").removeClass(this.options["unselected-icon"]).addClass(this.options["selected-icon"]);if(this.options.multiSelect){h.push(f.data())}}if(h.length){this.$element.trigger("selected",{info:h})}f.trigger("updated",{info:h,item:f,eventType:e})},selectFolder:function(g){var m=b(g);var j=m.parent();var f=j.find(".tree-folder-content");var h=f.eq(0);var k="."+b.trim(this.options["close-icon"].replace(/\s/g,"."));var e,l,i;if(m.find(k).length){e="opened";l=this.options["close-icon"];i=this.options["open-icon"];h.show();if(!f.children().length){this.populate(m)}}else{k="."+b.trim(this.options["open-icon"].replace(/\s/g,"."));e="closed";l=this.options["open-icon"];i=this.options["close-icon"];h.hide();if(!this.options.cacheItems){h.empty()}}j.find(k).eq(0).removeClass(l).addClass(i);this.$element.trigger(e,m.data())},selectedItems:function(){var f=this.$element.find(".tree-selected");var e=[];b.each(f,function(g,h){e.push(b(h).data())});return e},collapse:function(){var e=this.options.cacheItems;this.$element.find("."+this.options["open-icon"]).each(function(){var g=b(this).removeClass(this.options["open-icon"]).addClass(this.options["close-icon"]);var f=g.parent().parent();var h=f.children(".tree-folder-content");h.hide();if(!e){h.empty()}})}};b.fn.tree=function(g){var f=Array.prototype.slice.call(arguments,1);var h;var e=this.each(function(){var k=b(this);var j=k.data("tree");var i=typeof g==="object"&&g;if(!j){k.data("tree",(j=new c(this,i)))}if(typeof g==="string"){h=j[g].apply(j,f)}});return(h===d)?e:h};b.fn.tree.defaults={multiSelect:false,loadingHTML:"<div>Loading...</div>",cacheItems:true};b.fn.tree.Constructor=c;b.fn.tree.noConflict=function(){b.fn.tree=a;return this}})(window.jQuery);
|