github.com/nsqio/nsq@v1.3.0/nsqadmin/static/js/views/app.js (about) 1 var $ = require('jquery'); 2 3 window.jQuery = $; 4 var bootstrap = require('bootstrap'); //eslint-disable-line no-unused-vars 5 var bootbox = require('bootbox'); 6 7 var AppState = require('../app_state'); 8 var Pubsub = require('../lib/pubsub'); 9 var Router = require('../router'); 10 11 var BaseView = require('./base'); 12 var HeaderView = require('./header'); 13 var TopicsView = require('./topics'); 14 var TopicView = require('./topic'); 15 var ChannelView = require('./channel'); 16 var LookupView = require('./lookup'); 17 var NodesView = require('./nodes'); 18 var NodeView = require('./node'); 19 var CounterView = require('./counter'); 20 21 var NodeModel = require('../models/node'); 22 var TopicModel = require('../models/topic'); 23 var ChannelModel = require('../models/channel'); 24 25 var AppView = BaseView.extend({ 26 // not a fan of setting a view's el to an existing element on the page 27 // for the top-level AppView, it seems appropriate, however 28 el: '#container', 29 30 events: { 31 'click .link': 'onLinkClick', 32 'click .tombstone-link': 'onTombstoneClick' 33 }, 34 35 initialize: function() { 36 BaseView.prototype.initialize.apply(this, arguments); 37 38 this.listenTo(Pubsub, 'topics:show', this.showTopics); 39 this.listenTo(Pubsub, 'topic:show', this.showTopic); 40 this.listenTo(Pubsub, 'channel:show', this.showChannel); 41 this.listenTo(Pubsub, 'lookup:show', this.showLookup); 42 this.listenTo(Pubsub, 'nodes:show', this.showNodes); 43 this.listenTo(Pubsub, 'node:show', this.showNode); 44 this.listenTo(Pubsub, 'counter:show', this.showCounter); 45 46 this.listenTo(Pubsub, 'view:ready', function() { 47 $('.rate').each(function(i, el) { 48 var $el = $(el); 49 var interval = AppState.get('STATSD_INTERVAL'); 50 var q = { 51 'target': $el.attr('target'), 52 'from': '-' + (2 * interval) + 'sec', 53 'until': '-' + interval + 'sec', 54 'format': 'json', 55 }; 56 var formatRate = function(data) { 57 if (data[0] === null || 58 data[0]['datapoints'][0] === null || 59 data[0]['datapoints'][0][0] < 0) { 60 return 'N/A'; 61 } else { 62 return (data[0]['datapoints'][0][0] / interval).toFixed(2); 63 } 64 }; 65 $.ajax({ 66 url: AppState.get('GRAPHITE_URL') + '/render', 67 data: q, 68 dataType: 'jsonp', 69 jsonp: 'jsonp' 70 }) 71 .done(function(data) { $el.html(formatRate(data)); }) 72 .fail(function() { $el.html('ERROR'); }); 73 }); 74 }); 75 this.render(); 76 }, 77 78 postRender: function() { 79 this.appendSubview(new HeaderView()); 80 }, 81 82 showView: function(f) { 83 window.scrollTo(0, 0); 84 if (this.currentView) { 85 this.currentView.remove(); 86 } 87 this.currentView = f(); 88 this.appendSubview(this.currentView); 89 }, 90 91 showTopics: function() { 92 this.showView(function() { 93 return new TopicsView(); 94 }); 95 }, 96 97 showTopic: function(topic) { 98 this.showView(function() { 99 var model = new TopicModel({'name': topic, 'isAdmin': AppState.get('IS_ADMIN')}); 100 return new TopicView({'model': model}); 101 }); 102 }, 103 104 showChannel: function(topic, channel) { 105 this.showView(function() { 106 var model = new ChannelModel({ 107 'topic': topic, 108 'name': channel, 109 'isAdmin': AppState.get('IS_ADMIN') 110 }); 111 return new ChannelView({'model': model}); 112 }); 113 }, 114 115 showLookup: function() { 116 this.showView(function() { 117 return new LookupView({'isAdmin': AppState.get('IS_ADMIN')}); 118 }); 119 }, 120 121 showNodes: function() { 122 this.showView(function() { 123 return new NodesView(); 124 }); 125 }, 126 127 showNode: function(node) { 128 this.showView(function() { 129 var model = new NodeModel({'name': node}); 130 return new NodeView({'model': model}); 131 }); 132 }, 133 134 showCounter: function() { 135 this.showView(function() { 136 return new CounterView(); 137 }); 138 }, 139 140 onLinkClick: function(e) { 141 if (e.ctrlKey || e.metaKey) { 142 // allow ctrl+click to open in a new tab 143 return; 144 } 145 e.preventDefault(); 146 e.stopPropagation(); 147 Router.navigate($(e.currentTarget).attr('href'), {'trigger': true}); 148 }, 149 150 onTombstoneClick: function(e) { 151 e.preventDefault(); 152 e.stopPropagation(); 153 var nodeName = $(e.target).data('node'); 154 var topicName = $(e.target).data('topic'); 155 var txt = 'Are you sure you want to <strong>tombstone</strong> <em>' + nodeName + '</em>?'; 156 bootbox.confirm(txt, function(result) { 157 if (result !== true) { 158 return; 159 } 160 var node = new NodeModel({ 161 'name': nodeName 162 }); 163 node.tombstoneTopic(topicName) 164 .done(function() { window.location.reload(true); }) 165 .fail(this.handleAJAXError.bind(this)); 166 }.bind(this)); 167 } 168 }); 169 170 module.exports = AppView;