Commit 03cfb3d1 authored by Thomas Lechauve's avatar Thomas Lechauve

New feature: Bang an instance

parent abb1aea3
...@@ -100,7 +100,9 @@ ...@@ -100,7 +100,9 @@
return this.each(function () { return this.each(function () {
$(this).slapos('discovery', function (access) { $(this).slapos('discovery', function (access) {
if (access.hasOwnProperty(methodName)) { if (access.hasOwnProperty(methodName)) {
var url = args.url || access[methodName].url; var url = access[methodName].url.replace(/{(\w+)}/, function (matchedText, $1) {
return "" + args[$1]
});
$.extend(args, {'url': url}); $.extend(args, {'url': url});
$this.slapos('request', $this.slapos('request',
access[methodName].method, access[methodName].method,
...@@ -129,13 +131,22 @@ ...@@ -129,13 +131,22 @@
}, },
instanceInfo: function (url, args) { instanceInfo: function (url, args) {
url = decodeURIComponent(url); $.extend(args, {'instance_url': decodeURIComponent(url)});
$.extend(args, {'url': url});
return $(this).slapos('prepareRequest', 'instance_info', args); return $(this).slapos('prepareRequest', 'instance_info', args);
}, },
instanceRequest: function (args) { instanceRequest: function (args) {
return $(this).slapos('prepareRequest', 'request_instance', args); return $(this).slapos('prepareRequest', 'request_instance', args);
},
instanceBang: function (url, args) {
$.extend(args, {'instance_url': decodeURIComponent(url)});
return $(this).slapos('prepareRequest', 'instance_bang', args);
},
instanceCertificate: function (url, args) {
$.extend(args, {'instance_url': decodeURIComponent(url)});
return $(this).slapos('prepareRequest', 'instance_certificate', args);
} }
}; };
......
...@@ -39,6 +39,15 @@ ...@@ -39,6 +39,15 @@
</article> </article>
</script> </script>
<script id="form.bang.instance" type="text/html">
<form class="form-horizontal" id="form-bang">
<label>Why do you want to bang this instance ?</label>
<input class="input-large" name="log" type="text">
<button class="btn btn-primary" type="submit" name="submit">Bang</button>
<button class="btn">Cancel</button>
</form>
</script>
<script id="form.new.instance" type="text/html"> <script id="form.new.instance" type="text/html">
<article> <article>
<form class="form-horizontal"> <form class="form-horizontal">
...@@ -51,7 +60,7 @@ ...@@ -51,7 +60,7 @@
</div> </div>
</div> </div>
<div class="control-group"> <div class="control-group">
<label class="control-label">Software release</label> ><label class="control-label">Software release</label>
<div class="controls"> <div class="controls">
<input type="text" class="input-xlarge" required name="software_release"/> <input type="text" class="input-xlarge" required name="software_release"/>
</div> </div>
...@@ -73,15 +82,17 @@ ...@@ -73,15 +82,17 @@
<script id="instance" type="text/html"> <script id="instance" type="text/html">
{{! Service page template }} {{! Service page template }}
<article> <div class="subnav">
<ul class="nav nav-pills">
{{# actions }}
<li><a href="{{ url }}">{{ name }}</a></li>
{{/ actions}}
</ul>
</div>
<form class="form-horizontal" id="instance-form"> <form class="form-horizontal" id="instance-form">
<fieldset> <fieldset>
<legend> <legend>
{{ title }} {{ title }}
<div class="pull-right">
<div class="btn">Bang</div>
<div class="btn">Destroy</div>
</div>
</legend> </legend>
<div class="control-group"> <div class="control-group">
<label class="control-label">Reference</label> <label class="control-label">Reference</label>
...@@ -133,7 +144,6 @@ ...@@ -133,7 +144,6 @@
</textarea> </textarea>
</fieldset> </fieldset>
</form> </form>
</article>
</script> </script>
<script id="instance.stop_requested" type="text/html"> <script id="instance.stop_requested" type="text/html">
...@@ -229,6 +239,7 @@ ...@@ -229,6 +239,7 @@
<script type="text/javascript" src="static/js/jquery-1.7.2.js"></script> <script type="text/javascript" src="static/js/jquery-1.7.2.js"></script>
<script type="text/javascript" src="static/js/bootstrap-alert.js"></script> <script type="text/javascript" src="static/js/bootstrap-alert.js"></script>
<script type="text/javascript" src="static/js/bootstrap-modal.js"></script>
<script type="text/javascript" src="static/js/spin.js"></script> <script type="text/javascript" src="static/js/spin.js"></script>
<script type="text/javascript" src="static/js/jquery-spin.js"></script> <script type="text/javascript" src="static/js/jquery-spin.js"></script>
<script type="text/javascript" src="static/js/ICanHaz.min.js"></script> <script type="text/javascript" src="static/js/ICanHaz.min.js"></script>
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
var routes = { var routes = {
'/instance' : 'requestInstance', '/instance' : 'requestInstance',
'/instance/:url' : 'showInstance', '/instance/:url' : 'showInstance',
'/instance/:url/bang' : 'showBangInstance',
'/computers' : 'listComputers', '/computers' : 'listComputers',
'/instances' : 'listInstances', '/instances' : 'listInstances',
'/invoices' : 'listInvoices' '/invoices' : 'listInvoices'
...@@ -25,6 +26,21 @@ ...@@ -25,6 +26,21 @@
}); });
}, },
/* Based on ben alman code
* https://raw.github.com/cowboy/jquery-misc/master/jquery.ba-serializeobject.js
*/
serializeObject = function () {
var obj = {};
$.each($(this).serializeArray(), function (i, o) {
var k = o.name,
v = o.value;
obj[k] = obj[k] === undefined ? v
: $.isArray(obj[k]) ? obj[k].concat(v)
: [obj[k], v];
});
return obj;
},
getDate = function () { getDate = function () {
var today = new Date(); var today = new Date();
return [today.getFullYear(), today.getMonth(), today.getDay()].join('/') + return [today.getFullYear(), today.getMonth(), today.getDay()].join('/') +
...@@ -100,7 +116,7 @@ ...@@ -100,7 +116,7 @@
}, },
genInstanceUrl: function (uri) { genInstanceUrl: function (uri) {
return location.href.split('#')[0] + '#/instance/' + encodeURIComponent(uri); return $.genHash(['instance', encodeURIComponent(uri)]);
}, },
extractInstanceURIFromHref: function () { extractInstanceURIFromHref: function () {
...@@ -108,7 +124,13 @@ ...@@ -108,7 +124,13 @@
}, },
extractInstanceURIFromHashtag: function () { extractInstanceURIFromHashtag: function () {
return decodeURIComponent(window.location.hash.split('/').pop()); var loc = window.location.hash.split('/'),
i = $.inArray("instance", loc);
return (i !== -1 && loc.length > i) ? decodeURIComponent(loc[i + 1]) : "";
},
genBangUrl: function (uri) {
return $.genHash(["instance", encodeURIComponent(uri), "bang"]);
}, },
authenticate: function (data) { authenticate: function (data) {
...@@ -143,6 +165,9 @@ ...@@ -143,6 +165,9 @@
$(this).slapos('instanceInfo', uri, { $(this).slapos('instanceInfo', uri, {
success: function (infos) { success: function (infos) {
infos.status = $(this).vifib('getRender', 'instance.' + infos.status); infos.status = $(this).vifib('getRender', 'instance.' + infos.status);
infos.actions = [
{name: "Bang", url: methods.genBangUrl(decodeURIComponent(uri))}
];
$(this).vifib('render', 'instance', infos); $(this).vifib('render', 'instance', infos);
var form = $(this).find("#instance-form"); var form = $(this).find("#instance-form");
form.vifib('prepareForm'); form.vifib('prepareForm');
...@@ -158,6 +183,7 @@ ...@@ -158,6 +183,7 @@
prepareForm: function () { prepareForm: function () {
$(this).vifib('bindStopStartButtons'); $(this).vifib('bindStopStartButtons');
$("#bangInstance").click();
$(this).vifib('refresh', methods.refreshInstanceForm, 30); $(this).vifib('refresh', methods.refreshInstanceForm, 30);
}, },
...@@ -185,6 +211,31 @@ ...@@ -185,6 +211,31 @@
return false; return false;
}, },
showBangInstance: function () {
var statusCode = {
400: bad_request,
401: redirect,
402: payment,
404: notFound,
500: serverError,
};
return this.each(function () {
$(this).vifib("render", 'form.bang.instance');
$(this).find("#form-bang").submit(function () {
var data = $(this).serializeObject(),
uri = methods.extractInstanceURIFromHashtag();
$(this).slapos('instanceBang', uri, {
data: data,
statusCode: statusCode,
success: function () {
$.redirect(['instance', encodeURIComponent(uri)]);
}
});
return false;
});
});
},
changeStatusInstance: function (status) { changeStatusInstance: function (status) {
var uri = methods.extractInstanceURIFromHashtag(), var uri = methods.extractInstanceURIFromHashtag(),
data = $(this).vifib('extractInstanceInfo'); data = $(this).vifib('extractInstanceInfo');
...@@ -325,7 +376,6 @@ ...@@ -325,7 +376,6 @@
popup: function (message, state) { popup: function (message, state) {
state = state || 'error'; state = state || 'error';
console.log($(this));
return this.each(function () { return this.each(function () {
$(this).prepend(ich.error({ $(this).prepend(ich.error({
'message': message, 'message': message,
...@@ -363,6 +413,21 @@ ...@@ -363,6 +413,21 @@
$.error( 'Method ' + method + ' does not exist on jQuery.vifib' ); $.error( 'Method ' + method + ' does not exist on jQuery.vifib' );
} }
}; };
/* Thanks to Ben Alman
* https://raw.github.com/cowboy/jquery-misc/master/jquery.ba-serializeobject.js
*/
$.fn.serializeObject = function(){
var obj = {};
$.each( this.serializeArray(), function(i,o){
var n = o.name,
v = o.value;
obj[n] = obj[n] === undefined ? v
: $.isArray( obj[n] ) ? obj[n].concat( v )
: [ obj[n], v ];
});
return obj;
};
})(jQuery); })(jQuery);
$('#main').vifib(); $('#main').vifib();
...@@ -138,6 +138,11 @@ ...@@ -138,6 +138,11 @@
instanceRequest: function (args) { instanceRequest: function (args) {
return $(this).slapos('prepareRequest', 'request_instance', args); return $(this).slapos('prepareRequest', 'request_instance', args);
}, },
instanceBang: function (url, args) {
$.extend(args, {'instance_url': decodeURIComponent(url)});
return $(this).slapos('prepareRequest', 'instance_bang', args);
},
instanceCertificate: function (url, args) { instanceCertificate: function (url, args) {
$.extend(args, {'instance_url': decodeURIComponent(url)}); $.extend(args, {'instance_url': decodeURIComponent(url)});
......
...@@ -34,7 +34,7 @@ $.extractAuth = function (hashTag) { ...@@ -34,7 +34,7 @@ $.extractAuth = function (hashTag) {
}; };
$.genHash = function(url) { $.genHash = function(url) {
return '/' + url.join('/'); return '#/' + url.join('/');
}; };
/* Pub / Sub Pattern /* Pub / Sub Pattern
...@@ -55,10 +55,10 @@ $.publish = function() { ...@@ -55,10 +55,10 @@ $.publish = function() {
// Event Handlers // Event Handlers
$.hashHandler = function(){ $.publish('urlChange', $.parseHash(window.location.hash.substr(1))); }; $.hashHandler = function(){ $.publish('urlChange', $.parseHash(window.location.hash.substr(1))); };
$.redirectHandler = function(e, url){ window.location.hash = $.genHash([url]); }; $.redirectHandler = function(e, url){ window.location.hash = $.genHash(url); };
// redirections manager // redirections manager
$.redirect = function(url){ $.publish('redirect', url); }; $.redirect = function(url){ $.publish('redirect', [url]); };
$.subscribe('redirect', $.redirectHandler) $.subscribe('redirect', $.redirectHandler)
$(window).bind('hashchange', $.hashHandler); $(window).bind('hashchange', $.hashHandler);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment