UNPKG

lpio

Version:

The last dashboard app you'll ever need

104 lines (91 loc) 3.39 kB
<link rel="import" href="../../bower_components/polymer/polymer.html"> <link rel="import" href="http-connect.html"> <link rel="import" href="../lp-imports/jquery.html"> <link rel="import" href="../lp-imports/socketio.html"> <link rel="import" href="../lp-imports/underscore.html"> <script> window.LinchPinBehaviors = window.LinchPinBehaviors || {}; /** * * @polymerBehavior LinchPinBehaviors.RegisterData * * Uses http-connect behavior * **/ LinchPinBehaviors.RegisterDataImpl = { properties: { dataNodes: { type: Object, readonly: true }, socket: { type: Object, readonly: true, value: null } }, ready: function(){ var self = this; console.log("Connecting to "+this.endpoint); if(self.socket===null){ self.socket = io.connect(this.endpoint,{'forceNew': true }); self.socket.on('connect', self._onConnect.bind(self)); self.socket.on('reconnect', self._onReconnect.bind(self)); self.socket.on('update', self._onUpdate.bind(self)); self.socket.on('disconnect', self._onDisconnect.bind(self)); } }, _onReconnect: function(){ console.log("Reconnected to server"); }, _onConnect: function() { console.log("Connected ..."); this.registerData(); if(this.onConnect && typeof this.onConnect == 'function' ){ this.onConnect(); } }, _onDisconnect: function(){ console.log("disconnected"); if(this.onDisconnect && typeof this.onDisconnect == 'function' ){ this.onDisconnect(); } }, _onUpdate: function(update){ var self = this; _.keys(update).forEach(function(key){ if(self.dataNodes.hasOwnProperty(key)){ self.dataNodes[key].forEach(function(node){ node.data = update[key]; }); } }) }, registerData: function(){ this.getDataNodes(); var dataIds = {dataIds: _.keys(this.dataNodes)}; if(this.socket!= null){ console.log("Registered:"+ JSON.stringify(dataIds)); this.socket.emit('registerDataIds',dataIds); } }, getDataNodes: function(){ var self = this; var data_list = this.querySelectorAll('* [lp-data-id]'); var data_array = Array.prototype.slice.call(data_list); this.dataNodes = {}; data_array.forEach(function(lizard){ var id = lizard.getAttribute("lp-data-id"); if(id!== null){ if(!self.dataNodes.hasOwnProperty(id)){ self.dataNodes[id] = []; } self.dataNodes[id].push(lizard); } }); console.log("Registering data:"); console.log(_.keys(self.dataNodes)); } }; LinchPinBehaviors.RegisterData = [LinchPinBehaviors.HttpConnect,LinchPinBehaviors.RegisterDataImpl]; </script>