silk.postings.categories = xb.core.object.extend( { } ); silk.postings.categories.mount = xb.core.object.extend( silk.node.mount, { ctor: function( domNode, name, resource ) { this.DOMNodeHandler = silk.postings.categories.mount.handler; this.$id = 0; this.indexed = {}; silk.node.mount.prototype.ctor.call( this, domNode, name, resource ); }, onLoad: function() { var self = this; self.getCategories(); window.addEventListener( "hashchange", function() { self.resource.select(); } ); }, onDOMChange: function( handler, before ) { var data = this.resource.data[ this.fieldName ]; //data.onDOMChange( handler, before ); var changes = data.handleDOMChanges( handler, before ); data.delete( changes.removed ); data.update( changes.mutations ); if ( changes.added.length ) { } data.add( changes.added ); data.reorder( changes.reorder[ 0 ], changes.reorder[ 1 ] ); }, select: function( selection ) { selection.categories = this.getCategories( selection.categories ); return selection; }, getCategories: function( categories ) { var hashes = window.location.hash.split( "#" ); var data = this.resource.data[ this.fieldName ]; var categories = ( typeof( categories ) === "object" ) ? categories : {}; var result = {}; var link = document.createElement( "a" ); var e = true; for ( var i = 0, il = data.set.length; i < il; i++ ) { var category = data.set[ i ]; if ( typeof( category[ "category-name" ].href ) === "string" ) { link.href = category[ "category-name" ].href; var tag = link.hash.substr( 1 ); var offset = hashes.indexOf( tag ); if ( typeof( categories[ category[ "category-id" ] ] ) !== "undefined" || offset >= 0 ) { result[ category[ "category-id" ] ] = category; e = false; } } } if ( e ) { result[ "-1" ] = data.index[ "-1" ]; if ( typeof( result[ "-1" ] ) === "undefined" ) { result[ "-1" ] = { }; } } console.warn( "silk.postings.categories.getCategories", result ); return result; }, save: function( data ) { for ( var i = 0, il = this.nodes.length; i < il; i++ ) { $( this.nodes[ i ].domNode ) .find( "[data-silk-new]" ) .each( function() { this.removeAttribute( "data-silk-new" ); } ) ; } return this.resource.data[ this.fieldName ].set; } } ); silk.postings.categories.mount.handler = xb.core.object.extend( silk.node.mount.DOMNode, { factory: function( mount, domNode ) { console.warn( "domNode", domNode ); return new silk.postings.categories.mount.handler( mount, domNode ); }, ctor: function( mount, domNode ) { this.rendered = false; silk.node.mount.DOMNode.prototype.ctor.call( this, mount, domNode ); }, display: function( data, reset ) { silk.node.mount.DOMNode.prototype.display.call( this, data, reset ); var resource = this.mount.resource; var selection = resource.selection; if ( typeof( selection.categories ) === "object" ) { var count = null; if ( resource.mounts.view instanceof silk.node.mount ) { count = {}; var view = resource.mounts.view; var list = view.getData().getArray(); for ( var i = 0, il = list.length; i < il; i++ ) { var catString = list[ i ][ "posting-categories" ]; if ( typeof( catString ) !== "string" ) { catString = ""; } var cats = catString.split( "," ); for ( var j = 0, jl = cats.length; j < jl; j++ ) { var c = "" + cats[ j ]; if ( typeof( count[ c ] ) === "undefined" ) { count[ c ] = 1; } else { count[ c ]++; } } } } $( this.domNode ) .find( ".silk-id.category-id" ) .each( function() { var active = false; var id = this.innerText; var domNode = $( this ).parents( ".silk-object.postings-category, .silk-list.postings-categories > .silk-listItem " ); var domNodeName = $( domNode ).find( ".silk-elm.category-name > a" ); var name = null; if ( domNodeName.length ) { name = domNodeName[ 0 ].hash.substr( 1 ); } if ( typeof( selection.categories[ id ] ) !== "undefined" ) { active = true; } if ( typeof( count ) !== null ) { if ( ( id !== "-1" ) && typeof( count[ id ] ) === "undefined" ) { domNode.addClass( "empty" ); } else { domNode.removeClass( "empty" ); } } //if ( name !== null ) { // domNode[ 0 ].setAttribute( "id", name ); //} else { // domNode[ 0 ].removeAttribute( "id" ); //} if ( active ) { domNode.addClass( "active" ); } else { domNode.removeClass( "active" ); } } ) ; } }, onDOMChange: function( mutations, dt, mObserver ) { silk.node.mount.DOMNode.prototype.onDOMChange.call( this, mutations, dt, mObserver ); }, } );