diff --git a/lib/config-loader.js b/lib/config-loader.js index b2f8942..da3d703 100644 --- a/lib/config-loader.js +++ b/lib/config-loader.js @@ -2,6 +2,7 @@ var fs = require('fs') , path = require('path') , _loadTypes = require('./type-loader') , InternalResources = require('./resources/internal-resources') + , InternalDeployments = require('./resources/internal-deployments') , Files = require('./resources/files') , ClientLib = require('./resources/client-lib') , Dashboard = require('./resources/dashboard') @@ -174,6 +175,7 @@ function addInternalResources(server, basepath, resources, fn) { new Files('', { config: { 'public': publicFolder }, server: server }) , new ClientLib('dpd.js', { config: { resources: resources }, server: server}) , new InternalResources('__resources', {config: {configPath: basepath}, server: server}) + , new InternalDeployments('__deployments', {server: server}) , new Dashboard('dashboard', {server: server}) ]; async.forEach(internals, loadResourceExtras, function(err) { diff --git a/lib/resources/dashboard/deployments.html b/lib/resources/dashboard/deployments.html index 3f1878c..9511368 100644 --- a/lib/resources/dashboard/deployments.html +++ b/lib/resources/dashboard/deployments.html @@ -2,30 +2,13 @@
- \ No newline at end of file + + + \ No newline at end of file diff --git a/lib/resources/dashboard/js/deployments.js b/lib/resources/dashboard/js/deployments.js index e69de29..c471eb2 100644 --- a/lib/resources/dashboard/js/deployments.js +++ b/lib/resources/dashboard/js/deployments.js @@ -0,0 +1,44 @@ +(function() { + + var scope = { + deployments: null + }; + + var dpdDeployments = dpd('__deployments'); + var deploymentTemplate = _.template($('#deployment-template').html()); + + loadDeployments(); + + $('#deployment-list').on('click', '.component-item', onClickDeployment); + + function loadDeployments() { + dpdDeployments.get(function(deployments, error) { + scope.deployments = deployments; + renderDeployments(); + }); + } + + function onClickDeployment() { + var href = $(this).find('.manage-btn').attr('href'); + location.href = href; + } + + function renderDeployments() { + if (scope.deployments === null || scope.deployments.length) { + $('#deployments-empty').hide(); + } else { + $('#deployments-empty').show(); + } + + if (scope.deployments) { + $('#deployment-list').empty(); + scope.deployments.forEach(function(d, i) { + $('#deployment-list').append(deploymentTemplate({ + deployment: d, + index: i + })); + }); + } + } + +})(); \ No newline at end of file diff --git a/lib/resources/dashboard/stylesheets/deployments.less b/lib/resources/dashboard/stylesheets/deployments.less index 4642b17..5751ab9 100644 --- a/lib/resources/dashboard/stylesheets/deployments.less +++ b/lib/resources/dashboard/stylesheets/deployments.less @@ -8,6 +8,11 @@ border-top-color: #555 } + .empty { + margin-top: 20px; + font-style: italic; + } + .component-item-header { .actions { visibility: hidden; diff --git a/lib/resources/internal-deployments.js b/lib/resources/internal-deployments.js new file mode 100644 index 0000000..7cb5019 --- /dev/null +++ b/lib/resources/internal-deployments.js @@ -0,0 +1,61 @@ +var util = require('util') + , Resource = require('../resource') + , fs = require('fs') + , path = require('path') + , q = require('q') + , qutil = require('../util/qutil'); + +function InternalDeployments() { + Resource.apply(this, arguments); + this.deploymentsFile = this.config.deploymentsFile || './.dpd/deployments.json'; +} +util.inherits(InternalDeployments, Resource); +module.exports = InternalDeployments; + +InternalDeployments.prototype.handle = function(ctx, next) { + if (!ctx.req.isRoot) { + ctx.done({statusCode: 401, message: "Not Allowed"}); + return; + } + + if (ctx.method === "GET" && ctx.url === "/") { + this.getList(ctx, next); + } else { + next(); + } +}; + +InternalDeployments.prototype.getList = function(ctx, next) { + var self = this; + var fileQ = q.ninvoke(fs, 'readFile', self.deploymentsFile, 'utf-8'); + + var listQ = fileQ.then(function(file) { + var deploymentsJson = JSON.parse(file); + var list = []; + Object.keys(deploymentsJson).forEach(function(k) { + if (k !== 'sid' && k !== 'user') { + deploymentsJson[k].id = k; + list.push(deploymentsJson[k]); + } + }); + + list.sort(function(a, b) { + return a.id.localeCompare(b.id); + }); + + return list; + }, function(err) { + if (err.code === "ENOENT") { + return []; + } else { + throw err; + } + }); + + listQ.then(function(list) { + ctx.done(null, list); + }, function(err) { + ctx.done(err); + }); + +}; \ No newline at end of file diff --git a/lib/util/qutil.js b/lib/util/qutil.js new file mode 100644 index 0000000..112bea3 --- /dev/null +++ b/lib/util/qutil.js @@ -0,0 +1,22 @@ +var q = require('q'); + +exports.cinvoke = function(obj, func) { + var args = Array.prototype.slice.call(arguments, 2); + + var d = q.defer(); + + if (typeof func !== 'function') { + func = obj[func]; + } + + var callback = function(result) { + d.resolve(result); + }; + + args.push(callback); + + func.apply(obj, args); + + return d.promise; + +}; \ No newline at end of file