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