(function (root, factory) { if (typeof define == 'function' && define.amd) { define( factory ); } else if (typeof module === 'object' && module.exports) { module.exports = factory(); } else { root.VanillaTree = factory(); } }(this, function () { "use strict"; // Look at the Balalaika https://github.com/finom/balalaika var $=function(n,e,k,h,p,m,l,b,d,g,f,c){c=function(a,b){return new c.i(a,b)};c.i=function(a,d){k.push.apply(this,a?a.nodeType||a==n?[a]:""+a===a?/' + this.placeholder + '' } else if( p = this.tree.querySelector( '.vtree-placeholder' ) ) { this.tree.removeChild( p ); } return this; }, getLeaf: function( id, notThrow ) { var leaf = $( '[data-vtree-id="' + id + '"]', this.tree )[ 0 ]; if( !notThrow && !leaf ) throw Error( 'No VanillaTree leaf with id "' + id + '"' ) return leaf; }, getChildList: function( id ) { var list, parent; if( id ) { parent = this.getLeaf( id ); if( !( list = $( 'ul', parent )[ 0 ] ) ) { list = parent.appendChild( create( 'ul', { className: 'vtree-subtree' }) ); } } else { list = this.tree; } return list; }, add: function( options ) { // see https://github.com/finom/vanillatree/issues/8 if(this.getLeaf( options.id,true )){return;}// don't add leaves that already exist var id, leaf = create( 'li', { className: 'vtree-leaf' }), parentList = this.getChildList( options.parent ); leaf.setAttribute( 'data-vtree-id', id = options.id || Math.random() ); leaf.appendChild( create( 'span', { className: 'vtree-toggle' }) ); leaf.appendChild( create( 'a', { className: 'vtree-leaf-label', innerHTML: options.label }) ); parentList.appendChild( leaf ); if( parentList !== this.tree ) { parentList.parentNode.classList.add( 'vtree-has-children' ); } this.leafs[ id ] = options; if( !options.opened ) { this.close( id ); } if( options.selected ) { this.select( id ); } return this._placeholder()._dispatch( 'add', id ); }, move: function( id, parentId ) { var leaf = this.getLeaf( id ), oldParent = leaf.parentNode, newParent = this.getLeaf( parentId, true ); if( newParent ) { newParent.classList.add( 'vtree-has-children' ); } this.getChildList( parentId ).appendChild( leaf ); oldParent.parentNode.classList.toggle( 'vtree-has-children', !!oldParent.children.length ); return this._dispatch( 'move', id ); }, remove: function( id ) { var leaf = this.getLeaf( id ), oldParent = leaf.parentNode; oldParent.removeChild( leaf ); oldParent.parentNode.classList.toggle( 'vtree-has-children', !!oldParent.children.length ); return this._placeholder()._dispatch( 'remove', id ); }, open: function( id ) { this.getLeaf( id ).classList.remove( 'closed' ); return this._dispatch( 'open', id ); }, close: function( id ) { this.getLeaf( id ).classList.add( 'closed' ); return this._dispatch( 'close', id ); }, toggle: function( id ) { return this[ this.getLeaf( id ).classList.contains( 'closed' ) ? 'open' : 'close' ]( id ); }, select: function( id ) { var leaf = this.getLeaf( id ); if( !leaf.classList.contains( 'vtree-selected' ) ) { $( 'li.vtree-leaf', this.tree ).forEach( function( leaf ) { leaf.classList.remove( 'vtree-selected' ); }); leaf.classList.add( 'vtree-selected' ); this._dispatch( 'select', id ); } return this; } }; return Tree; // Look at the Balalaika https://github.com/finom/balalaika }));