UNPKG

spincycle

Version:

A reactive message router and object manager that lets clients subscribe to object property changes on the server

194 lines (172 loc) 7.5 kB
<link rel="import" href="../../bower_components/polymer/polymer.html"> <link rel="import" href="../../bower_components/paper-listbox/paper-listbox.html"> <link rel="import" href="../../bower_components/paper-item/paper-item.html"> <link rel="import" href="../../bower_components/iron-icons/maps-icons.html"> <link rel="import" href="../../bower_components/iron-icons/iron-icons.html"> <link rel="import" href="../../bower_components/paper-button/paper-button.html"> <dom-module id="spin-elementlist"> <template> <style> </style> <div> <content></content> </div> </template> <script> Polymer( { is: 'spin-elementlist', properties: { client: {type: 'Object', observer: 'onStuff'}, user: {type: 'Object', observer: 'onStuff'}, spintype: {type: String, observer: 'onStuff'}, list: {type: Array, notify: true, value: []}, filteruser: {type: Boolean, value: false}, filterids: {type: Array}, captions: {type: Object, observer: 'onStuff'}, query: {type: Object, observer: 'onStuff'}, loading: {type: String, notify: true} }, ready: function () { console.log('spin-elementlist ready') this.subscriptions = {} this.getModelList() }, attached: function () { }, onStuff: function () { console.log('spin-elementlist onStuff -------- spintype='+this.spintype+' client='+this.client+' user='+this.user) if (this.spintype && this.client && this.captions && this.user) { this.getModelList() } }, getModelList: function () { console.log( '---spin-elementlist get all elements. this.filteruser = ' + this.filteruser + ' this.user = ' + this.user + ' query = ' + this.query ) //console.dir( this.query ) var q = {sort: 'name'} if (this.filteruser == true && this.user) { q = {sort: 'name', property: 'createdBy', value: this.user.id} } q.limit = 20 if (this.query) { q = this.query } console.log( 'spin-elementlist query is' ) console.dir( q ) this.loading = 'block' this.client.emitMessage( { target: '_list' + this.spintype + 's', query: q } ).then( function ( newlist ) { this.loading = 'none' console.log( 'spin-elementlist for ' + this.spintype + ' fetchpage got ' + newlist.length + ' items back' ) console.dir( newlist ) newlist = this.filterIds( newlist ) newlist.sort( function ( a, b ) { return a.modifiedAt == b.modifiedAt ? 0 : a.modifiedAt < b.modifiedAt ? 1 : -1 } ) this.list = newlist newlist.forEach( function ( el ) { //console.log('spin-elementlist calling on: for '+el.id+' '+el.type) this.client.on( el.id, el.type, function ( newobj ) { console.log( 'spin-elementlist got object from on: ' + newobj.id+' '+newobj.type ) this.replaceInList( this.list, newobj.id, newobj ) }.bind( this ), true ) }.bind( this ) ) this.registerPopulationListener() }.bind( this ) ) }, replaceInList: function ( list, id, objWithId ) { console.log( 'replaceInList called with list of ' + list.length + ' elements, id = ' + id + ' and objWithId ' + objWithId ) var idx = -1 list.forEach( function ( el, i ) { //console.log('checking if '+el.id+' == '+id) if (el.id == id) { //console.log( 'found match with old object at index ' + i ) idx = i } } ) if (idx > -1) { //console.log( 'splicing away at index ' + idx ) if (objWithId) { this.splice( 'list', idx, 1, objWithId ) } else { this.splice( 'list', idx, 1 ) } } else { if (objWithId) { //console.log( 'pushing new element with no prior found id to list' ) this.push('list', objWithId ) } } //console.log('list is now') //console.dir(this.list) }, filterIds: function ( newlist ) { var rv = newlist if (this.filterids && this.filterids.length > 0) { this.filterids.forEach( function ( fid ) { this.replaceInList( rv, fid ) } ) } return rv }, registerPopulationListener: function () { if (this.listenerid) { this.client.deRegisterPopulationChangesSubscriber( { type: this.spintype, listenerid: this.listenerid } ) } this.client.registerPopulationChangeSubscriber( { type: this.spintype, cb: function ( popchange ) { console.log( 'spin-elementlist --------------------population change callback for ' + this.spintype ) console.dir( popchange ) if (popchange.removed) { this.replaceInList( this.list, popchange.removed.id ) } else { this.replaceInList( this.list, popchange.added.id, popchange.added ) } console.log( 'elementlist is now..' ) console.dir( this.list ) }.bind( this ) } ).then( function ( listenid ) { this.listenerid = listenid }.bind( this ) ) }, onSelect: function ( e ) { } } ); </script> </dom-module>