PageRenderTime 29ms CodeModel.GetById 1ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/_attachments/js/nodes.js

https://bitbucket.org/nicoechaniz/altermap
JavaScript | 339 lines | 291 code | 42 blank | 6 comment | 23 complexity | 992d377b5d70d7b1a7c015d4d0b6955f MD5 | raw file
  1Backbone.couch_connector.config.db_name = "altermap";
  2Backbone.couch_connector.config.ddoc_name = "altermap";
  3Backbone.couch_connector.config.global_changes = false;
  4
  5// must update backbone-couch connector to support these settings then delete per collection changes setting.
  6//Backbone.couch_connector.config.single_feed = true;
  7//Backbone.couch_connector.config.global_changes = true;
  8
  9// Enables Mustache.js-like templating.
 10_.templateSettings = {
 11    interpolate : /\{\{(.+?)\}\}/g
 12};
 13
 14var DEBUG = true;
 15
 16var CURRENT_NODE = undefined;
 17var CURRENT_NETWORK = undefined;
 18var CURRENT_ZONE = undefined;
 19
 20var WifiLink = Backbone.Model.extend({
 21    url : function() {
 22        return this.id ? '/wifilinks/' + this.id : '/wifilinks';
 23    },   
 24});
 25
 26var WifiLinksCollection = Backbone.Collection.extend({
 27    db : {
 28      changes : true
 29    },
 30    url: "/wifilinks",
 31    model: WifiLink,
 32});
 33
 34var LinkLineView = Backbone.View.extend({
 35    initialize : function(){
 36        _.bindAll(this, 'render', 'remove', '_nodeFromMAC');
 37        this.model.on('remove', this.remove);
 38    },
 39    _nodeFromMAC: function (macaddr){
 40        var iface = interfaces.where({'macaddr': macaddr})[0];
 41        if (iface != undefined ){
 42            var device = devices.get(iface.get('device_id'));
 43            var node = nodes.where({'_id': device.get('node_id')})[0]
 44            return node;
 45        }
 46    },
 47
 48    render: function(){
 49        var source_node = this._nodeFromMAC(this.model.get('macaddr'));
 50        var target_node = this._nodeFromMAC(this.model.get('station'));
 51        if (source_node != undefined && target_node != undefined){
 52            this.model.source_coords = source_node.get('coords');
 53            this.model.target_coords = target_node.get('coords');
 54            if (this.model.line == undefined){
 55                this.model.line = map.displayLinkLine(this.model);
 56            }
 57        }
 58    },
 59    remove: function(){
 60        map.removeLinkLine(this.model.line);
 61    },
 62})
 63
 64var WifiLinksView =  Backbone.View.extend({
 65    initialize: function(collection){
 66        _.bindAll(this, 'addLine', 'refresh');
 67        wifilinks.on("reset", this.refresh, this);
 68        wifilinks.on("add", this.addLine);
 69    },
 70    addLine : function(wifilink){
 71        if (wifilink.line == undefined){
 72            var view = new LinkLineView({model: wifilink});
 73            view.render();
 74        }
 75    },
 76    refresh: function(){
 77        wifilinks.each(this.addLine);
 78    }
 79});
 80
 81
 82var Network = Backbone.Model.extend({
 83    url : function() {
 84        return this.id ? '/networks/' + this.id : '/networks';
 85    },   
 86});
 87
 88var NetworksCollection = Backbone.Collection.extend({
 89    url: "/networks",
 90    model: Network,
 91});
 92
 93
 94var NetworkSelectView = Backbone.View.extend({
 95    el: $('#network-select'),
 96    template:  $("#network-selection-template").html(),
 97    events: {
 98        "change": "changeSelection"
 99    },
100
101    initialize: function(collection){
102        _.bindAll(this, 'render', 'changeSelection');
103        this.collection = collection
104        this.collection.on("reset", this.render);
105        this.collection.on("add", this.render);
106    },
107
108    render: function(){
109        content = Mustache.to_html(this.template, {networks: this.collection.toJSON()});
110        $(this.el).html(content);
111    },
112    changeSelection: function(){
113        CURRENT_NET = this.collection.get($(this.el).val());
114        CURRENT_ZONE = zones.where({'network_id': CURRENT_NET.id})[0];
115        if (CURRENT_ZONE == undefined){
116            CURRENT_NET = undefined;
117            if (DEBUG == true ) {alert('Selected network has no zones defined');}
118        }
119        else {
120            console.log("CZ: "+ CURRENT_ZONE.id);
121            var zone_nodes = null;
122            nodes.fetch({success: function(){
123                zone_nodes = nodes.where({'zone_id': CURRENT_ZONE.id});
124                nodes.reset(zone_nodes);
125                wifilinks.fetch();
126            }});
127        }
128    }
129
130});
131
132var Zone = Backbone.Model.extend({
133    url : function() {
134        return this.id ? '/zones/' + this.id : '/zones';
135    },   
136});
137
138var ZonesCollection = Backbone.Collection.extend({
139    url: "/zones",
140    model: Zone,
141});
142
143var Node = Backbone.Model.extend({
144    url : function() {
145        // POST to '/nodes' and PUT to '/nodes/:id'
146        return this.id ? '/nodes/' + this.id : '/nodes';
147    },
148});
149
150var NodesCollection = Backbone.Collection.extend({
151    db : {
152      changes : true
153    },
154    url: "/nodes",
155    model: Node,
156});
157
158var Device = Backbone.Model.extend({
159    url : function() {
160        return this.id ? '/devices/' + this.id : '/devices';
161    },   
162});
163
164var DevicesCollection = Backbone.Collection.extend({
165    db : {
166      changes : true
167    },
168    url: "/devices",
169    model: Device,
170});
171
172var Interface = Backbone.Model.extend({
173    url : function() {
174        return this.id ? '/interfaces/' + this.id : '/interfaces';
175    },   
176});
177
178var InterfacesCollection = Backbone.Collection.extend({
179    db : {
180      changes : true
181    },
182    url: "/interfaces",
183    model: Interface,
184});
185
186var NodeRowView = Backbone.View.extend({
187    tagName: "div",
188    className: "node-row",
189    template : _.template($("#node-row-template").html()),
190
191    events: {
192        "click .node-row a": 'selectNode',
193    },
194
195    initialize : function(){
196        _.bindAll(this, 'render', 'selectNode', 'remove');
197        this.model.on('change', this.render);
198        this.model.on('remove', this.remove);
199    },
200    render: function(){
201        var content = this.model.toJSON();
202        $(this.el).html(this.template(content));
203        return this;
204    },
205    remove: function(){
206        $(this.el).remove();
207    },
208    selectNode: function(){
209        map.selectNodeMarker(this.model.marker);
210    },
211})
212
213var NodeListView = Backbone.View.extend({
214    el: $('#nodelist'),
215
216    initialize: function(collection){
217        _.bindAll(this, 'refresh', 'addRow');
218        nodes.on("reset", this.refresh);
219        nodes.on("add", this.addRow);
220    },
221    addRow : function(node){
222        console.log("addrow "+ node);
223        var view = new NodeRowView({model: node});
224        var rendered = view.render().el;
225        $(this.el).append(rendered);
226    },
227
228    refresh: function(){
229        $("#nodelist").html("");
230        nodes.each(this.addRow);
231    }
232});
233
234var NodeMarkerView = Backbone.View.extend({
235    initialize : function(){
236        _.bindAll(this, 'render', 'remove');
237        this.model.on('remove', this.remove);
238//            this.model.on('change', this.render);
239    },
240    render: function(){
241        if (this.model.marker == undefined){
242            this.model.marker = map.displayNodeMarker(this.model);
243        }
244    },
245    remove: function(){
246        map.removeNodeMarker(this.model.marker);
247    },
248})
249
250var NodeMarkersView =  Backbone.View.extend({
251    initialize: function(collection){
252        _.bindAll(this, 'addMarker', 'refresh');
253        nodes.on("reset", this.refresh);
254        nodes.on("add", this.addMarker);
255    },
256    addMarker: function(node){
257        if (node.marker == undefined){
258            var view = new NodeMarkerView({model: node});
259            view.render();
260        }
261    },
262    delMarker: function(node){
263        node.marker = undefined;
264    },
265
266    refresh: function(){
267        map.resetMarkers();
268        nodes.each(this.delMarker)
269        nodes.each(this.addMarker);
270    }
271});
272
273
274var AddNodeView = Backbone.View.extend({
275    el: $("#toolbox"),
276    initialize: function(){
277        _.bindAll(this, 'addNewNode', 'render');
278        this.render()
279    },
280    render: function(){
281        var template = _.template( $("#new-node-template").html(), {} );
282        $(this.el).html( template );
283    },
284    events: {
285        "click input#add-node": 'addNewNode',
286    },
287    addNewNode: function(e){
288        zone = zones.at(0);
289        if (DEBUG == true && zone == undefined){ alert("No zone is active"); return;}
290        new_node = new Node({name: $('#new-node-name').val(), zone_id: zone.id});
291        CURRENT_NODE = new_node;
292        map.drawNodeMarker(new_node);
293    },
294
295});
296
297var networks = new NetworksCollection();
298var zones = new ZonesCollection()
299var nodes = new NodesCollection();
300var devices = new DevicesCollection();
301var interfaces = new InterfacesCollection();
302var wifilinks = new WifiLinksCollection();
303
304var NodesAppRouter = Backbone.Router.extend({
305    routes: {
306        "nodes/:id": "selectNode",
307    },
308
309    initialize : function(){
310        networks.fetch({success: function(){
311            console.log('networks loaded');
312            zones.fetch({success: function(){
313                console.log('zones loaded');
314                nodes.fetch({success: function(){
315                    console.log('nodes loaded');
316                    devices.fetch({success: function(){
317                        console.log('devices loaded');
318                        interfaces.fetch({success: function(){
319                            console.log('interfaces loaded');
320                            wifilinks.fetch({success: function(){
321                                console.log('wifilinks loaded');
322                            }});
323                        }});
324                    }});
325                }});
326            }});
327        }});
328
329        new NetworkSelectView(networks);
330        nodeListView = new NodeListView(nodes);
331        new NodeMarkersView(nodes);
332        new AddNodeView();
333        new WifiLinksView(wifilinks);
334
335    },
336    selectNode: function(node_id){
337        console.log('select by URL');
338    },
339});