altermap /_attachments/js/nodes.js

Language Javascript Lines 340
MD5 Hash 992d377b5d70d7b1a7c015d4d0b6955f
Repository https://bitbucket.org/nicoechaniz/altermap View Raw File View Project SPDX
| Open JSFiddle
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
Backbone.couch_connector.config.db_name = "altermap";
Backbone.couch_connector.config.ddoc_name = "altermap";
Backbone.couch_connector.config.global_changes = false;

// must update backbone-couch connector to support these settings then delete per collection changes setting.
//Backbone.couch_connector.config.single_feed = true;
//Backbone.couch_connector.config.global_changes = true;

// Enables Mustache.js-like templating.
_.templateSettings = {
    interpolate : /\{\{(.+?)\}\}/g
};

var DEBUG = true;

var CURRENT_NODE = undefined;
var CURRENT_NETWORK = undefined;
var CURRENT_ZONE = undefined;

var WifiLink = Backbone.Model.extend({
    url : function() {
        return this.id ? '/wifilinks/' + this.id : '/wifilinks';
    },   
});

var WifiLinksCollection = Backbone.Collection.extend({
    db : {
      changes : true
    },
    url: "/wifilinks",
    model: WifiLink,
});

var LinkLineView = Backbone.View.extend({
    initialize : function(){
        _.bindAll(this, 'render', 'remove', '_nodeFromMAC');
        this.model.on('remove', this.remove);
    },
    _nodeFromMAC: function (macaddr){
        var iface = interfaces.where({'macaddr': macaddr})[0];
        if (iface != undefined ){
            var device = devices.get(iface.get('device_id'));
            var node = nodes.where({'_id': device.get('node_id')})[0]
            return node;
        }
    },

    render: function(){
        var source_node = this._nodeFromMAC(this.model.get('macaddr'));
        var target_node = this._nodeFromMAC(this.model.get('station'));
        if (source_node != undefined && target_node != undefined){
            this.model.source_coords = source_node.get('coords');
            this.model.target_coords = target_node.get('coords');
            if (this.model.line == undefined){
                this.model.line = map.displayLinkLine(this.model);
            }
        }
    },
    remove: function(){
        map.removeLinkLine(this.model.line);
    },
})

var WifiLinksView =  Backbone.View.extend({
    initialize: function(collection){
        _.bindAll(this, 'addLine', 'refresh');
        wifilinks.on("reset", this.refresh, this);
        wifilinks.on("add", this.addLine);
    },
    addLine : function(wifilink){
        if (wifilink.line == undefined){
            var view = new LinkLineView({model: wifilink});
            view.render();
        }
    },
    refresh: function(){
        wifilinks.each(this.addLine);
    }
});


var Network = Backbone.Model.extend({
    url : function() {
        return this.id ? '/networks/' + this.id : '/networks';
    },   
});

var NetworksCollection = Backbone.Collection.extend({
    url: "/networks",
    model: Network,
});


var NetworkSelectView = Backbone.View.extend({
    el: $('#network-select'),
    template:  $("#network-selection-template").html(),
    events: {
        "change": "changeSelection"
    },

    initialize: function(collection){
        _.bindAll(this, 'render', 'changeSelection');
        this.collection = collection
        this.collection.on("reset", this.render);
        this.collection.on("add", this.render);
    },

    render: function(){
        content = Mustache.to_html(this.template, {networks: this.collection.toJSON()});
        $(this.el).html(content);
    },
    changeSelection: function(){
        CURRENT_NET = this.collection.get($(this.el).val());
        CURRENT_ZONE = zones.where({'network_id': CURRENT_NET.id})[0];
        if (CURRENT_ZONE == undefined){
            CURRENT_NET = undefined;
            if (DEBUG == true ) {alert('Selected network has no zones defined');}
        }
        else {
            console.log("CZ: "+ CURRENT_ZONE.id);
            var zone_nodes = null;
            nodes.fetch({success: function(){
                zone_nodes = nodes.where({'zone_id': CURRENT_ZONE.id});
                nodes.reset(zone_nodes);
                wifilinks.fetch();
            }});
        }
    }

});

var Zone = Backbone.Model.extend({
    url : function() {
        return this.id ? '/zones/' + this.id : '/zones';
    },   
});

var ZonesCollection = Backbone.Collection.extend({
    url: "/zones",
    model: Zone,
});

var Node = Backbone.Model.extend({
    url : function() {
        // POST to '/nodes' and PUT to '/nodes/:id'
        return this.id ? '/nodes/' + this.id : '/nodes';
    },
});

var NodesCollection = Backbone.Collection.extend({
    db : {
      changes : true
    },
    url: "/nodes",
    model: Node,
});

var Device = Backbone.Model.extend({
    url : function() {
        return this.id ? '/devices/' + this.id : '/devices';
    },   
});

var DevicesCollection = Backbone.Collection.extend({
    db : {
      changes : true
    },
    url: "/devices",
    model: Device,
});

var Interface = Backbone.Model.extend({
    url : function() {
        return this.id ? '/interfaces/' + this.id : '/interfaces';
    },   
});

var InterfacesCollection = Backbone.Collection.extend({
    db : {
      changes : true
    },
    url: "/interfaces",
    model: Interface,
});

var NodeRowView = Backbone.View.extend({
    tagName: "div",
    className: "node-row",
    template : _.template($("#node-row-template").html()),

    events: {
        "click .node-row a": 'selectNode',
    },

    initialize : function(){
        _.bindAll(this, 'render', 'selectNode', 'remove');
        this.model.on('change', this.render);
        this.model.on('remove', this.remove);
    },
    render: function(){
        var content = this.model.toJSON();
        $(this.el).html(this.template(content));
        return this;
    },
    remove: function(){
        $(this.el).remove();
    },
    selectNode: function(){
        map.selectNodeMarker(this.model.marker);
    },
})

var NodeListView = Backbone.View.extend({
    el: $('#nodelist'),

    initialize: function(collection){
        _.bindAll(this, 'refresh', 'addRow');
        nodes.on("reset", this.refresh);
        nodes.on("add", this.addRow);
    },
    addRow : function(node){
        console.log("addrow "+ node);
        var view = new NodeRowView({model: node});
        var rendered = view.render().el;
        $(this.el).append(rendered);
    },

    refresh: function(){
        $("#nodelist").html("");
        nodes.each(this.addRow);
    }
});

var NodeMarkerView = Backbone.View.extend({
    initialize : function(){
        _.bindAll(this, 'render', 'remove');
        this.model.on('remove', this.remove);
//            this.model.on('change', this.render);
    },
    render: function(){
        if (this.model.marker == undefined){
            this.model.marker = map.displayNodeMarker(this.model);
        }
    },
    remove: function(){
        map.removeNodeMarker(this.model.marker);
    },
})

var NodeMarkersView =  Backbone.View.extend({
    initialize: function(collection){
        _.bindAll(this, 'addMarker', 'refresh');
        nodes.on("reset", this.refresh);
        nodes.on("add", this.addMarker);
    },
    addMarker: function(node){
        if (node.marker == undefined){
            var view = new NodeMarkerView({model: node});
            view.render();
        }
    },
    delMarker: function(node){
        node.marker = undefined;
    },

    refresh: function(){
        map.resetMarkers();
        nodes.each(this.delMarker)
        nodes.each(this.addMarker);
    }
});


var AddNodeView = Backbone.View.extend({
    el: $("#toolbox"),
    initialize: function(){
        _.bindAll(this, 'addNewNode', 'render');
        this.render()
    },
    render: function(){
        var template = _.template( $("#new-node-template").html(), {} );
        $(this.el).html( template );
    },
    events: {
        "click input#add-node": 'addNewNode',
    },
    addNewNode: function(e){
        zone = zones.at(0);
        if (DEBUG == true && zone == undefined){ alert("No zone is active"); return;}
        new_node = new Node({name: $('#new-node-name').val(), zone_id: zone.id});
        CURRENT_NODE = new_node;
        map.drawNodeMarker(new_node);
    },

});

var networks = new NetworksCollection();
var zones = new ZonesCollection()
var nodes = new NodesCollection();
var devices = new DevicesCollection();
var interfaces = new InterfacesCollection();
var wifilinks = new WifiLinksCollection();

var NodesAppRouter = Backbone.Router.extend({
    routes: {
        "nodes/:id": "selectNode",
    },

    initialize : function(){
        networks.fetch({success: function(){
            console.log('networks loaded');
            zones.fetch({success: function(){
                console.log('zones loaded');
                nodes.fetch({success: function(){
                    console.log('nodes loaded');
                    devices.fetch({success: function(){
                        console.log('devices loaded');
                        interfaces.fetch({success: function(){
                            console.log('interfaces loaded');
                            wifilinks.fetch({success: function(){
                                console.log('wifilinks loaded');
                            }});
                        }});
                    }});
                }});
            }});
        }});

        new NetworkSelectView(networks);
        nodeListView = new NodeListView(nodes);
        new NodeMarkersView(nodes);
        new AddNodeView();
        new WifiLinksView(wifilinks);

    },
    selectNode: function(node_id){
        console.log('select by URL');
    },
});
Back to Top