var basketMountHandler = xb.core.object.extend( silkMountDOMNode, { factory: function( mount, domNode ) { return new basketMountHandler( mount, domNode ); }, ctor: function( mount, domNode ) { silkMountDOMNode.prototype.ctor.call( this, mount, domNode ); var self = this; this.domNodeBasket = $( this.domNode ).parents( ".silk-object.basket" ); this.domNodeNote = $( this.domNodeBasket ).find( ".silk-object.note" ); this.domNodeIcon = $( this.domNodeBasket ).find( ".silk-object.icon" ); this.domNodeCounter = $( this.domNodeBasket ).find( ".silk-elm.count" ); this.domNodeTotalPrice = $( this.domNodeBasket ).find( ".silk-object.basket-summary .silk-elm.price" ); console.error( "icon", this.domNodeIcon ); $( this.domNodeIcon ).on( "click", function( evt ) { $( self.domNodeBasket ).toggleClass( "open" ); } ); $( this.domNodeBasket ).on( "click", function( evt ) { var action = 0; // console.log( "*click*", evt.target ); if ( $( evt.target ).hasClass( "silk-elm" ) ) { if ( $( evt.target ).hasClass( "plus" ) ) { action = 1; } else if ( $( evt.target ).hasClass( "minus" ) ) { action = -1; } } if ( action !== 0 ) { var item = $( evt.target ).parents( ".silk-object.item" ); if ( item.length ) { // console.log( "Action", action, "for", item[ 0 ] ); var product = new silkNodeStruct( item[ 0 ] ); self.mount.modProduct( product.parseDOM(), action ); } } } ); silk.events.add( { element: window, on: [ "scroll", "resize" ], method: function() { self.reposition(); } } ); window.setTimeout( function() { self.reposition(); } , 250 ); }, reposition: function() { var cRectParent = this.domNodeBasket[ 0 ].parentNode.getBoundingClientRect(); var cRectNote = this.domNodeNote[ 0 ].getBoundingClientRect(); var cRectNoteParent = this.domNodeNote[ 0 ].parentNode.getBoundingClientRect(); if ( cRectParent.height / 2 + cRectParent.top < 0 ) { this.domNodeBasket.addClass( "floating" ); } else { this.domNodeBasket.removeClass( "floating" ); } var pos = cRectNote.width / -2 + 15; var marginRight = 24; // voor scrollbar e.d. //console.log( cRectNoteParent ); //console.log( cRectNoteParent.left + cRectNote.width, window.innerWidth ); var min = -cRectNoteParent.left; var max = window.innerWidth - ( cRectNoteParent.left + cRectNote.width + marginRight ); if ( pos < min ) { pos = min; } if ( pos > max ) { pos = max; } this.domNodeNote.css( "left", pos + "px" ); this.domNodeNote.css( "max-height", ( ( window.innerHeight - cRectNote.top ) - 20 + "px" ) ); }, render: function( data, reset ) { silkMountDOMNode.prototype.render.call( this, data, reset ); var count = 0; var totalPrice = 0; var formatPrice = function( price ) { return ( new Number( price ) ).toLocaleString( undefined, { minimumIntegerDigits: 1, minimumFractionDigits: 2, maximumFractionDigits: 2, useGrouping: false } ) }; this.each( function() { var amount = this.get( "count" ); if ( amount !== null ) { count += new Number( amount.getValue( true ) ); } var price = this.get( "count-x-price" ); if ( price ) { totalPrice += new Number( price.getValue( true ) ); price.setValue( formatPrice( price.getValue( true ) ) ); } } ); $( this.domNodeBasket ).each( function() { if ( count > 0 ) { $( this ).addClass( "filled" ); } else { $( this ).removeClass( "filled" ); } } ); $( this.domNodeCounter ).each( function() { this.innerText = count; } ); $( this.domNodeTotalPrice ).each( function() { this.innerText = formatPrice( totalPrice ); } ); this.reposition(); } } ); var basketProductHandler = xb.core.object.extend( silkNodeStruct, { ctor: function( domNode, mount ) { silkNodeStruct.prototype.ctor.call( this, domNode ); this.childNodes = this.parseDOM().childNodes; mount.addProduct( this ); } } ); var basketMount = xb.core.object.extend( silkNodeMount, { DOMNodeHandler: basketMountHandler, ctor: function( domNode, name, resource ) { var self = this; silkNodeMount.prototype.ctor.call( this, domNode, name, resource ); }, onLoad: function() { var self = this; // console.log( "onLoad@basketMount", this.resource.data[ this.name ] ); this.display(); $( document.body ).on( "click", function( evt ) { if ( evt.target.parentNode && $( evt.target.parentNode ).hasClass( "silk-elm" ) && $( evt.target.parentNode ).hasClass( "basket" ) ) { $( evt.target ).parents( ".silk-object.product" ).each( function() { ( new basketProductHandler( this, self ) ); } ); evt.preventDefault(); } } ); }, lookup: function( values ) { var data = this.resource.data[ this.name ]; for ( var i = 0, il = data.length; i < il; i++ ) { // console.log( "testing", data[ i ], values ); if ( data[ i ][ "title" ] == values[ "title" ] && data[ i ][ "sub-title" ] == values[ "sub-title" ] ) { break; } } return i; }, modProduct: function( product, amount ) { var values = product.getValue(); var data = this.resource.data[ this.name ]; var i = this.lookup( values ); if ( i < data.length ) { data[ i ].count += amount; data[ i ][ "count-x-price" ] = data[ i ].count * data[ i ].price; if ( data[ i ].count <= 0 ) { data.splice( i, 1 ); } } this.resource.save(); this.display(); }, addProduct: function( product ) { var values = product.getValue(); var data = this.resource.data[ this.name ]; var i = this.lookup( values ); if ( i == data.length ) { values[ "price" ] = ( new Number( values[ "integral" ] ) ) + ( ( 1 / 100 ) * values[ "fractional" ] ); values[ "count" ] = 1; values[ "count-x-price" ] = values.count * values.price; data.push( values ); } else { data[ i ].count += 1; data[ i ][ "count-x-price" ] = data[ i ].count * data[ i ].price; if ( data[ i ].count <= 0 ) { data.splice( i, 1 ); } } this.resource.save(); this.display(); }, display: function() { var data = this.resource.data[ this.name ]; if ( data instanceof Array ) { this.render( data, true ); } else { console.error( "basketMount::display()", this.name, this.resource.data, "data is not an array" ); } } } ); var basketResource = xb.core.object.extend( silkResource, { handlers: { "*": basketMount }, ctor: function( config ) { silkResource.prototype.ctor.call( this, config ); }, init: function() { var self = this; this.data = []; console.log( "basketResource::init" ); var params = {}; // window.setTimeout( function() { var data = {}; for ( var n in self.mounts ) { var values = Cookies.getJSON( n ); if ( !( values instanceof Array ) ) { values = []; } data[ n ] = values; } self.load( data ); }, 1 ); }, save: function() { for ( var n in this.mounts ) { Cookies.set( n, this.data[ n ], { path: "/" } ); } } } );