consul/ui/javascripts/app/routes.js

253 lines
7.1 KiB
JavaScript
Raw Normal View History

2014-04-23 18:51:06 -04:00
//
2014-05-01 10:24:41 -04:00
// Superclass to be used by all of the main routes below.
2014-04-23 18:51:06 -04:00
//
2014-04-23 14:01:42 -04:00
App.BaseRoute = Ember.Route.extend({
2014-05-04 23:07:41 +02:00
rootKey: '',
2014-05-04 23:05:00 +02:00
getParentAndGrandparent: function(key) {
2014-05-04 23:07:41 +02:00
var parentKey = this.rootKey,
grandParentKey = this.rootKey,
2014-05-04 23:05:00 +02:00
parts = key.split('/');
2014-05-04 23:05:00 +02:00
if (parts.length > 0) {
parts.pop();
parentKey = parts.join("/") + "/";
2014-05-04 23:05:00 +02:00
}
2014-05-04 23:05:00 +02:00
if (parts.length > 1) {
parts.pop();
grandParentKey = parts.join("/") + "/";
}
2014-05-05 02:58:27 +02:00
return {
parent: parentKey,
grandParent: grandParentKey,
isRoot: parentKey === '/'
}
},
removeDuplicateKeys: function(keys, matcher) {
// Loop over the keys
keys.forEach(function(item, index) {
if (item.get('Key') == matcher) {
// If we are in a nested folder and the folder
// name matches our position, remove it
keys.splice(index, 1);
}
});
return keys;
},
2014-04-29 13:06:26 -04:00
actions: {
// Used to link to keys that are not objects,
// like parents and grandParents
2014-04-29 13:06:26 -04:00
linkToKey: function(key) {
2014-05-04 23:07:41 +02:00
if (key.slice(-1) === '/' || key === this.rootKey) {
2014-04-29 13:06:26 -04:00
this.transitionTo('kv.show', key)
} else {
this.transitionTo('kv.edit', key)
}
}
}
2014-04-21 16:36:50 -04:00
});
2014-04-23 18:51:06 -04:00
//
// The route for choosing datacenters, typically the first route loaded.
//
App.IndexRoute = App.BaseRoute.extend({
// Retrieve the list of datacenters
2014-04-30 14:02:20 -04:00
model: function(params) {
2014-05-01 11:42:40 -04:00
return Ember.$.getJSON('/v1/catalog/datacenters').then(function(data) {
2014-04-30 14:02:20 -04:00
return data
})
2014-04-27 10:42:21 -04:00
},
2014-04-30 16:30:14 -04:00
afterModel: function(model, transition) {
// If we only have one datacenter, jump
// straight to it and bypass the global
// view
2014-04-30 16:30:14 -04:00
if (model.get('length') === 1) {
this.transitionTo('services', model[0]);
2014-04-23 14:01:42 -04:00
}
}
});
// The parent route for all resources. This keeps the top bar
// functioning, as well as the per-dc requests.
2014-04-25 13:49:36 -04:00
App.DcRoute = App.BaseRoute.extend({
model: function(params) {
// Return a promise hash to retreieve the
// dcs and nodes used in the header
2014-04-30 16:30:14 -04:00
return Ember.RSVP.hash({
dc: params.dc,
dcs: Ember.$.getJSON('/v1/catalog/datacenters'),
nodes: Ember.$.getJSON('/v1/internal/ui/nodes?dc=' + params.dc).then(function(data) {
2014-04-30 16:30:14 -04:00
objs = [];
2014-04-30 14:02:20 -04:00
// Merge the nodes into a list and create objects out of them
2014-04-30 16:30:14 -04:00
data.map(function(obj){
objs.push(App.Node.create(obj));
});
2014-04-25 16:24:36 -04:00
2014-04-30 16:30:14 -04:00
return objs;
})
2014-04-30 14:02:20 -04:00
});
},
2014-04-30 16:30:14 -04:00
setupController: function(controller, models) {
controller.set('content', models.dc);
controller.set('nodes', models.nodes);
controller.set('dcs', models.dcs);
controller.set('isDropdownVisible', false);
},
2014-04-23 14:01:42 -04:00
});
2014-04-29 14:49:07 -04:00
App.KvIndexRoute = App.BaseRoute.extend({
2014-04-29 13:06:26 -04:00
beforeModel: function() {
2014-05-04 23:07:41 +02:00
this.transitionTo('kv.show', this.rootKey)
2014-04-29 13:06:26 -04:00
}
});
App.KvShowRoute = App.BaseRoute.extend({
model: function(params) {
2014-05-04 23:05:00 +02:00
var key = params.key;
2014-04-30 18:05:44 -04:00
var dc = this.modelFor('dc').dc;
2014-04-28 18:23:01 -04:00
2014-04-30 18:31:45 -04:00
// Return a promise has with the ?keys for that namespace
// and the original key requested in params
return Ember.RSVP.hash({
key: key,
keys: Ember.$.getJSON('/v1/kv/' + key + '?keys&seperator=' + '/&dc=' + dc).then(function(data) {
objs = [];
data.map(function(obj){
objs.push(App.Key.create({Key: obj}));
});
return objs;
})
2014-04-30 15:02:31 -04:00
});
2014-04-28 18:23:01 -04:00
},
2014-04-30 18:31:45 -04:00
setupController: function(controller, models) {
var key = models.key;
var parentKeys = this.getParentAndGrandparent(key);
models.keys = this.removeDuplicateKeys(models.keys, models.key);
2014-04-30 18:05:44 -04:00
2014-04-30 18:31:45 -04:00
controller.set('content', models.keys);
controller.set('parentKey', parentKeys.parent);
controller.set('grandParentKey', parentKeys.grandParent);
2014-05-05 02:58:27 +02:00
controller.set('isRoot', parentKeys.isRoot);
controller.set('newKey', App.Key.create());
2014-04-29 13:06:26 -04:00
}
});
App.KvEditRoute = App.BaseRoute.extend({
model: function(params) {
2014-05-04 23:05:00 +02:00
var key = params.key;
2014-04-30 18:05:44 -04:00
var dc = this.modelFor('dc').dc;
var parentKeys = this.getParentAndGrandparent(key)
2014-04-30 15:02:31 -04:00
// Return a promise hash to get the data for both columns
2014-04-30 16:30:14 -04:00
return Ember.RSVP.hash({
key: Ember.$.getJSON('/v1/kv/' + key + '?dc=' + dc).then(function(data) {
// Convert the returned data to a Key
2014-04-30 16:30:14 -04:00
return App.Key.create().setProperties(data[0]);
}),
keys: keysPromise = Ember.$.getJSON('/v1/kv/' + parentKeys.parent + '?keys&seperator=' + '/' + '&dc=' + dc).then(function(data) {
2014-04-30 16:30:14 -04:00
objs = [];
data.map(function(obj){
objs.push(App.Key.create({Key: obj}));
});
return objs;
}),
2014-04-30 15:02:31 -04:00
});
2014-04-29 13:06:26 -04:00
},
2014-04-29 13:34:13 -04:00
2014-04-30 16:30:14 -04:00
setupController: function(controller, models) {
var key = models.key;
var parentKeys = this.getParentAndGrandparent(key.get('Key'));
models.keys = this.removeDuplicateKeys(models.keys, parentKeys.parent);
2014-04-30 18:31:45 -04:00
controller.set('content', models.key);
controller.set('parentKey', parentKeys.parent);
controller.set('grandParentKey', parentKeys.grandParent);
2014-04-30 22:16:12 -04:00
controller.set('siblings', models.keys);
controller.set('rootKey', this.rootKey);
2014-05-05 02:58:27 +02:00
controller.set('isRoot', parentKeys.isRoot);
2014-04-28 18:23:01 -04:00
}
});
2014-04-23 14:01:42 -04:00
App.ServicesRoute = App.BaseRoute.extend({
2014-04-30 14:02:20 -04:00
model: function(params) {
2014-04-30 18:05:44 -04:00
var dc = this.modelFor('dc').dc
// Return a promise to retrieve all of the services
2014-04-30 18:05:44 -04:00
return Ember.$.getJSON('/v1/internal/ui/services?dc=' + dc).then(function(data) {
2014-04-30 14:02:20 -04:00
objs = [];
data.map(function(obj){
objs.push(App.Service.create(obj));
});
return objs
});
},
setupController: function(controller, model) {
2014-04-30 14:02:20 -04:00
controller.set('services', model);
}
});
2014-04-24 15:18:11 -04:00
App.ServicesShowRoute = App.BaseRoute.extend({
model: function(params) {
2014-04-30 18:05:44 -04:00
var dc = this.modelFor('dc').dc
// Here we just use the built-in health endpoint, as it gives us everything
// we need.
2014-04-30 18:05:44 -04:00
return Ember.$.getJSON('/v1/health/service/' + params.name + '?dc=' + dc).then(function(data) {
2014-04-30 14:02:20 -04:00
objs = [];
data.map(function(obj){
objs.push(App.Node.create(obj));
});
return objs;
});
2014-04-30 23:39:41 -04:00
}
2014-04-21 16:36:50 -04:00
});
2014-04-23 14:01:42 -04:00
App.NodesShowRoute = App.BaseRoute.extend({
2014-04-24 15:18:11 -04:00
model: function(params) {
2014-04-30 18:05:44 -04:00
var dc = this.modelFor('dc').dc
// Return a promise hash of the node and nodes
2014-04-30 15:25:31 -04:00
return Ember.RSVP.hash({
2014-04-30 18:05:44 -04:00
node: Ember.$.getJSON('/v1/internal/ui/node/' + params.name + '?dc=' + dc).then(function(data) {
2014-04-30 15:25:31 -04:00
return App.Node.create(data)
}),
2014-04-30 18:05:44 -04:00
nodes: Ember.$.getJSON('/v1/internal/ui/node/' + params.name + '?dc=' + dc).then(function(data) {
2014-04-30 15:25:31 -04:00
return App.Node.create(data)
})
2014-04-30 14:02:20 -04:00
});
2014-04-24 15:18:11 -04:00
},
2014-04-30 15:25:31 -04:00
setupController: function(controller, models) {
controller.set('content', models.node);
2014-04-24 15:18:11 -04:00
//
// Since we have 2 column layout, we need to also display the
2014-04-24 15:33:53 -04:00
// list of nodes on the left. Hence setting the attribute
// {{nodes}} on the controller.
2014-04-24 15:18:11 -04:00
//
2014-04-30 15:25:31 -04:00
controller.set('nodes', models.nodes);
2014-04-24 15:18:11 -04:00
}
});
App.NodesRoute = App.BaseRoute.extend({
2014-04-30 14:02:20 -04:00
model: function(params) {
2014-04-30 18:05:44 -04:00
var dc = this.modelFor('dc').dc
// Return a promise containing the nodes
2014-04-30 18:05:44 -04:00
return Ember.$.getJSON('/v1/internal/ui/nodes?dc=' + dc).then(function(data) {
2014-04-30 14:02:20 -04:00
objs = [];
data.map(function(obj){
objs.push(App.Node.create(obj));
});
return objs
});
},
2014-04-24 15:18:11 -04:00
setupController: function(controller, model) {
2014-04-30 14:02:20 -04:00
controller.set('nodes', model);
2014-04-24 15:18:11 -04:00
}
});