consul/ui/javascripts/app/controllers.js

349 lines
10 KiB
JavaScript
Raw Normal View History

2014-06-05 17:24:03 +00:00
App.ApplicationController = Ember.ObjectController.extend({
updateCurrentPath: function() {
App.set('currentPath', this.get('currentPath'));
}.observes('currentPath')
});
2014-04-25 17:49:36 +00:00
App.DcController = Ember.Controller.extend({
// Whether or not the dropdown menu can be seen
2014-04-25 20:24:36 +00:00
isDropdownVisible: false,
2014-04-30 22:05:44 +00:00
datacenter: function() {
2014-08-20 23:51:40 +00:00
return this.get('content');
2014-04-30 22:05:44 +00:00
}.property('Content'),
2014-04-25 20:24:36 +00:00
checks: function() {
var nodes = this.get('nodes');
2014-08-20 23:51:40 +00:00
var checks = Ember.A();
2014-04-25 20:24:36 +00:00
// Combine the checks from all of our nodes
// into one.
nodes.forEach(function(item) {
2014-08-20 23:51:40 +00:00
checks = checks.concat(item.Checks);
2014-04-25 20:24:36 +00:00
});
2014-08-20 23:51:40 +00:00
return checks;
2014-04-30 22:05:44 +00:00
}.property('nodes'),
// Returns the total number of failing checks.
//
// We treat any non-passing checks as failing
//
totalChecksFailing: function() {
2014-08-20 23:51:40 +00:00
var checks = this.get('checks');
return (checks.filterBy('Status', 'critical').get('length') +
2014-08-20 23:51:40 +00:00
checks.filterBy('Status', 'warning').get('length'));
2014-04-30 22:05:44 +00:00
}.property('nodes'),
2014-04-25 20:24:36 +00:00
//
// Returns the human formatted message for the button state
//
2014-04-25 20:24:36 +00:00
checkMessage: function() {
2014-08-20 23:51:40 +00:00
var checks = this.get('checks');
var failingChecks = this.get('totalChecksFailing');
var passingChecks = checks.filterBy('Status', 'passing').get('length');
2014-04-25 20:24:36 +00:00
2014-08-20 23:51:40 +00:00
if (this.get('hasFailingChecks') === true) {
return failingChecks + ' failing';
2014-04-25 20:24:36 +00:00
} else {
return passingChecks + ' passing';
2014-04-25 20:24:36 +00:00
}
2014-04-30 22:05:44 +00:00
}.property('nodes'),
2014-04-25 20:24:36 +00:00
//
//
//
checkStatus: function() {
2014-08-20 23:51:40 +00:00
if (this.get('hasFailingChecks') === true) {
return "failing";
} else {
return "passing";
}
}.property('nodes'),
//
// Boolean if the datacenter has any failing checks.
//
2014-04-25 20:24:36 +00:00
hasFailingChecks: function() {
2014-08-20 23:51:40 +00:00
var failingChecks = this.get('totalChecksFailing');
2014-05-01 17:19:43 +00:00
return (failingChecks > 0);
2014-04-30 22:05:44 +00:00
}.property('nodes'),
2014-04-25 20:24:36 +00:00
actions: {
// Hide and show the dropdown menu
2014-04-25 20:24:36 +00:00
toggle: function(item){
this.toggleProperty('isDropdownVisible');
2014-05-01 16:21:32 +00:00
},
// Just hide the dropdown menu
hideDrop: function(item){
this.set('isDropdownVisible', false);
2014-04-25 20:24:36 +00:00
}
}
2014-08-20 23:51:40 +00:00
});
2014-04-25 17:49:36 +00:00
KvBaseController = Ember.ObjectController.extend({
getParentKeyRoute: function() {
if (this.get('isRoot')) {
return this.get('rootKey');
}
return this.get("parentKey");
},
transitionToNearestParent: function(parent) {
var controller = this;
var rootKey = controller.get('rootKey');
var dc = controller.get('dc').get('datacenter');
Ember.$.ajax({
url: ('/v1/kv/' + parent + '?keys&c=' + dc),
type: 'GET'
}).then(function(data) {
controller.transitionToRoute('kv.show', parent);
}).fail(function(response) {
if (response.status === 404) {
controller.transitionToRoute('kv.show', rootKey);
}
});
controller.set('isLoading', false);
}
});
// Add mixins
App.KvShowController = KvBaseController.extend(Ember.Validations.Mixin);
App.KvShowController.reopen({
2014-04-30 22:05:44 +00:00
needs: ["dc"],
dc: Ember.computed.alias("controllers.dc"),
isLoading: false,
actions: {
// Creates the key from the newKey model
// set on the route.
createKey: function() {
this.set('isLoading', true);
2014-04-30 20:30:14 +00:00
var controller = this;
var newKey = controller.get('newKey');
var parentKey = controller.get('parentKey');
var grandParentKey = controller.get('grandParentKey');
var dc = controller.get('dc').get('datacenter');
// If we don't have a previous model to base
// on our parent, or we're not at the root level,
// add the prefix
2014-08-20 23:51:40 +00:00
if (parentKey !== undefined && parentKey !== "/") {
newKey.set('Key', (parentKey + newKey.get('Key')));
}
// Put the Key and the Value retrieved from the form
2014-04-30 20:30:14 +00:00
Ember.$.ajax({
2014-04-30 22:05:44 +00:00
url: ("/v1/kv/" + newKey.get('Key') + '?dc=' + dc),
2014-04-30 20:30:14 +00:00
type: 'PUT',
data: newKey.get('Value')
}).then(function(response) {
// transition to the right place
2014-08-20 23:51:40 +00:00
if (newKey.get('isFolder') === true) {
2014-05-04 21:05:00 +00:00
controller.transitionToRoute('kv.show', newKey.get('Key'));
} else {
2014-05-04 21:05:00 +00:00
controller.transitionToRoute('kv.edit', newKey.get('Key'));
}
2014-08-20 23:51:40 +00:00
controller.set('isLoading', false);
2014-04-30 20:30:14 +00:00
}).fail(function(response) {
// Render the error message on the form if the request failed
2014-08-20 23:51:40 +00:00
controller.set('errorMessage', 'Received error while processing: ' + response.statusText);
2014-04-30 20:30:14 +00:00
});
2014-05-05 00:12:24 +00:00
},
deleteFolder: function() {
this.set('isLoading', true);
var controller = this;
var dc = controller.get('dc').get('datacenter');
var grandParent = controller.get('grandParentKey');
2014-05-05 00:12:24 +00:00
// Delete the folder
Ember.$.ajax({
url: ("/v1/kv/" + controller.get('parentKey') + '?recurse&dc=' + dc),
2014-05-05 00:12:24 +00:00
type: 'DELETE'
}).then(function(response) {
controller.transitionToNearestParent(grandParent);
2014-05-05 00:12:24 +00:00
}).fail(function(response) {
// Render the error message on the form if the request failed
2014-08-20 23:51:40 +00:00
controller.set('errorMessage', 'Received error while processing: ' + response.statusText);
});
}
}
});
App.KvEditController = KvBaseController.extend({
isLoading: false,
needs: ["dc"],
dc: Ember.computed.alias("controllers.dc"),
actions: {
// Updates the key set as the model on the route.
updateKey: function() {
this.set('isLoading', true);
var dc = this.get('dc').get('datacenter');
2014-04-30 20:30:14 +00:00
var key = this.get("model");
var controller = this;
// Put the key and the decoded (plain text) value
// from the form.
2014-04-30 20:30:14 +00:00
Ember.$.ajax({
url: ("/v1/kv/" + key.get('Key') + '?dc=' + dc),
2014-04-30 20:30:14 +00:00
type: 'PUT',
data: key.get('valueDecoded')
}).then(function(response) {
// If success, just reset the loading state.
2014-08-20 23:51:40 +00:00
controller.set('isLoading', false);
2014-04-30 20:30:14 +00:00
}).fail(function(response) {
// Render the error message on the form if the request failed
2014-08-20 23:51:40 +00:00
controller.set('errorMessage', 'Received error while processing: ' + response.statusText);
});
2014-04-30 14:16:50 +00:00
},
2014-05-04 23:03:47 +00:00
cancelEdit: function() {
this.set('isLoading', true);
this.transitionToRoute('kv.show', this.getParentKeyRoute());
2014-05-04 23:03:47 +00:00
this.set('isLoading', false);
},
2014-04-30 14:16:50 +00:00
deleteKey: function() {
this.set('isLoading', true);
2014-05-05 00:12:24 +00:00
2014-04-30 20:30:14 +00:00
var controller = this;
var dc = controller.get('dc').get('datacenter');
var key = controller.get("model");
var parent = controller.getParentKeyRoute();
2014-04-30 20:30:14 +00:00
// Delete the key
2014-04-30 20:30:14 +00:00
Ember.$.ajax({
url: ("/v1/kv/" + key.get('Key') + '?dc=' + dc),
2014-04-30 20:30:14 +00:00
type: 'DELETE'
}).then(function(data) {
controller.transitionToNearestParent(parent);
2014-04-30 20:30:14 +00:00
}).fail(function(response) {
// Render the error message on the form if the request failed
2014-08-20 23:51:40 +00:00
controller.set('errorMessage', 'Received error while processing: ' + response.statusText);
});
}
}
});
2014-05-30 18:26:11 +00:00
ItemBaseController = Ember.ArrayController.extend({
2014-06-05 17:24:03 +00:00
needs: ["dc", "application"],
queryParams: ["filter", "status", "condensed"],
dc: Ember.computed.alias("controllers.dc"),
condensed: true,
filter: "", // default
status: "any status", // default
statuses: ["any status", "passing", "failing"],
2014-06-05 17:24:03 +00:00
isShowingItem: function() {
var currentPath = this.get('controllers.application.currentPath');
return (currentPath === "dc.nodes.show" || currentPath === "dc.services.show");
}.property('controllers.application.currentPath'),
filteredContent: function() {
var filter = this.get('filter');
var status = this.get('status');
var items = this.get('items').filter(function(item, index, enumerable){
return item.get('filterKey').toLowerCase().match(filter.toLowerCase());
});
switch (status) {
case "passing":
2014-08-20 23:51:40 +00:00
return items.filterBy('hasFailingChecks', false);
case "failing":
2014-08-20 23:51:40 +00:00
return items.filterBy('hasFailingChecks', true);
default:
2014-08-20 23:51:40 +00:00
return items;
}
}.property('filter', 'status', 'items.@each'),
actions: {
toggleCondensed: function() {
2014-08-20 23:51:40 +00:00
this.set('condensed', !this.get('condensed'));
}
}
2014-05-30 18:26:11 +00:00
});
App.NodesShowController = Ember.ObjectController.extend({
needs: ["dc"],
dc: Ember.computed.alias("controllers.dc"),
actions: {
invalidateSession: function(sessionId) {
this.set('isLoading', true);
var controller = this;
var node = controller.get('model');
var dc = controller.get('dc').get('datacenter');
if (window.confirm("Are you sure you want to invalidate this session?")) {
// Delete the session
Ember.$.ajax({
url: ("/v1/session/destroy/" + sessionId + '?dc=' + dc),
type: 'PUT'
}).then(function(response) {
return Ember.$.getJSON('/v1/session/node/' + node.Node + '?dc=' + dc).then(function(data) {
2014-08-20 23:51:40 +00:00
controller.set('sessions', data);
});
}).fail(function(response) {
// Render the error message on the form if the request failed
2014-08-20 23:51:40 +00:00
controller.set('errorMessage', 'Received error while processing: ' + response.statusText);
});
}
}
}
});
App.NodesController = ItemBaseController.extend({
items: Ember.computed.alias("nodes"),
});
App.ServicesController = ItemBaseController.extend({
items: Ember.computed.alias("services"),
});
2014-08-21 21:44:17 +00:00
App.AclsIndexController = Ember.ArrayController.extend({
needs: ["dc", "application"],
queryParams: ["filter"],
dc: Ember.computed.alias("controllers.dc"),
isShowingItem: function() {
var currentPath = this.get('controllers.application.currentPath');
return (currentPath === "dc.acls.show");
}.property('controllers.application.currentPath'),
filteredContent: function() {
var filter = this.get('filter');
var items = this.get('items').filter(function(item, index, enumerable){
// First try to match on the name
var nameMatch = item.get('Name').toLowerCase().match(filter.toLowerCase());
if (nameMatch.length > 0) {
return nameMatch;
// Otherwise match on the ID
} else {
return item.get('ID').toLowerCase().match(filter.toLowerCase());
}
});
}.property('filter', 'items.@each'),
});
App.AclsShowController = Ember.ArrayController.extend({
needs: ["dc"],
dc: Ember.computed.alias("controllers.dc"),
});