2010-09-08 23:27:34 +08:00
|
|
|
var statistics_level;
|
2010-09-30 20:19:54 +08:00
|
|
|
var user_administrator;
|
2010-10-28 00:47:15 +08:00
|
|
|
var nodes_interesting;
|
2010-09-08 23:27:34 +08:00
|
|
|
|
2010-07-12 22:04:42 +08:00
|
|
|
$(document).ready(function() {
|
2010-09-30 20:19:54 +08:00
|
|
|
statistics_level = JSON.parse(sync_get('/overview')).statistics_level;
|
|
|
|
var user = JSON.parse(sync_get('/whoami'));
|
|
|
|
replace_content('login', '<p>User: <b>' + user.name + '</b></p>');
|
|
|
|
user_administrator = user.administrator;
|
2010-10-28 00:47:15 +08:00
|
|
|
nodes_interesting = JSON.parse(sync_get('/nodes')).length > 1;
|
2010-10-08 00:23:11 +08:00
|
|
|
setup_constant_events();
|
|
|
|
update_vhosts();
|
2010-08-23 23:32:17 +08:00
|
|
|
app.run();
|
2010-10-14 20:15:26 +08:00
|
|
|
set_timer_interval(5000);
|
2010-07-13 20:03:36 +08:00
|
|
|
var url = this.location.toString();
|
2010-08-23 23:32:17 +08:00
|
|
|
if (url.indexOf('#') == -1) {
|
|
|
|
this.location = url + '#/';
|
|
|
|
}
|
2010-07-12 22:04:42 +08:00
|
|
|
});
|
|
|
|
|
2010-10-08 00:23:11 +08:00
|
|
|
function setup_constant_events() {
|
|
|
|
$('#update-every').change(function() {
|
|
|
|
var interval = $(this).val();
|
|
|
|
if (interval == '') interval = null;
|
|
|
|
set_timer_interval(interval);
|
|
|
|
});
|
|
|
|
$('#show-vhost').change(function() {
|
|
|
|
current_vhost = $(this).val();
|
|
|
|
update();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function update_vhosts() {
|
|
|
|
var vhosts = JSON.parse(sync_get('/vhosts'));
|
|
|
|
var select = $('#show-vhost').get(0);
|
|
|
|
select.options.length = vhosts.length + 1;
|
|
|
|
var index = 0;
|
|
|
|
for (var i = 0; i < vhosts.length; i++) {
|
|
|
|
var vhost = vhosts[i].name;
|
|
|
|
select.options[i + 1] = new Option(vhost);
|
|
|
|
if (vhost == current_vhost) index = i + 1;
|
|
|
|
}
|
|
|
|
select.selectedIndex = index;
|
|
|
|
}
|
|
|
|
|
2010-08-23 23:32:17 +08:00
|
|
|
var app = $.sammy(dispatcher);
|
|
|
|
function dispatcher() {
|
|
|
|
var sammy = this;
|
|
|
|
function path(p, r, t) {
|
|
|
|
sammy.get(p, function() {
|
|
|
|
render(r, t, p);
|
|
|
|
});
|
|
|
|
}
|
2010-10-28 00:47:15 +08:00
|
|
|
path('#/', {'overview': '/overview',
|
|
|
|
'applications': '/applications',
|
|
|
|
'nodes': '/nodes'}, 'overview');
|
2010-08-27 00:42:03 +08:00
|
|
|
|
2010-10-12 00:05:06 +08:00
|
|
|
path('#/connections', {'connections': '/connections'}, 'connections');
|
2010-08-25 22:11:30 +08:00
|
|
|
this.get('#/connections/:name', function() {
|
2010-10-14 23:55:34 +08:00
|
|
|
var name = esc(this.params['name']);
|
|
|
|
render({'connection': '/connections/' + name,
|
|
|
|
'channels': '/connections/' + name + '/channels'},
|
|
|
|
'connection', '#/connections');
|
2010-08-24 01:32:33 +08:00
|
|
|
});
|
2010-08-25 22:11:30 +08:00
|
|
|
this.del('#/connections', function() {
|
2010-09-09 01:00:32 +08:00
|
|
|
if (sync_delete(this, '/connections/:name'))
|
|
|
|
go_to('#/connections');
|
2010-08-25 22:11:30 +08:00
|
|
|
return false;
|
|
|
|
});
|
2010-08-27 00:42:03 +08:00
|
|
|
|
2010-08-31 23:44:12 +08:00
|
|
|
path('#/channels', {'channels': '/channels'}, 'channels');
|
2010-09-03 00:06:59 +08:00
|
|
|
this.get('#/channels/:name', function() {
|
|
|
|
render({'channel': '/channels/' + esc(this.params['name'])}, 'channel',
|
|
|
|
'#/channels');
|
|
|
|
});
|
2010-08-27 00:42:03 +08:00
|
|
|
|
2010-08-31 23:44:12 +08:00
|
|
|
path('#/exchanges', {'exchanges': '/exchanges', 'vhosts': '/vhosts'}, 'exchanges');
|
2010-08-27 00:42:03 +08:00
|
|
|
this.get('#/exchanges/:vhost/:name', function() {
|
2010-09-29 23:43:38 +08:00
|
|
|
var path = '/exchanges/' + esc(this.params['vhost']) + '/' + esc(this.params['name']);
|
|
|
|
render({'exchange': path,
|
2010-10-14 18:21:33 +08:00
|
|
|
'bindings_source': path + '/bindings/source',
|
|
|
|
'bindings_destination': path + '/bindings/destination'},
|
2010-10-14 01:21:11 +08:00
|
|
|
'exchange', '#/exchanges');
|
2010-08-27 00:42:03 +08:00
|
|
|
});
|
|
|
|
this.put('#/exchanges', function() {
|
2010-09-09 01:00:32 +08:00
|
|
|
if (sync_put(this, '/exchanges/:vhost/:name'))
|
|
|
|
update();
|
2010-08-27 00:42:03 +08:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
this.del('#/exchanges', function() {
|
2010-09-09 01:00:32 +08:00
|
|
|
if (sync_delete(this, '/exchanges/:vhost/:name'))
|
|
|
|
go_to('#/exchanges');
|
2010-08-27 00:42:03 +08:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2010-08-31 23:44:12 +08:00
|
|
|
path('#/queues', {'queues': '/queues', 'vhosts': '/vhosts'}, 'queues');
|
2010-08-27 17:55:32 +08:00
|
|
|
this.get('#/queues/:vhost/:name', function() {
|
2010-09-02 00:26:51 +08:00
|
|
|
var path = '/queues/' + esc(this.params['vhost']) + '/' + esc(this.params['name']);
|
|
|
|
render({'queue': path,
|
|
|
|
'bindings': path + '/bindings'}, 'queue', '#/queues');
|
2010-08-27 17:55:32 +08:00
|
|
|
});
|
|
|
|
this.put('#/queues', function() {
|
2010-09-09 01:00:32 +08:00
|
|
|
if (sync_put(this, '/queues/:vhost/:name'))
|
|
|
|
update();
|
2010-08-27 17:55:32 +08:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
this.del('#/queues', function() {
|
2010-10-01 00:49:28 +08:00
|
|
|
if (this.params['mode'] == 'delete') {
|
|
|
|
if (sync_delete(this, '/queues/:vhost/:name'))
|
|
|
|
go_to('#/queues');
|
|
|
|
}
|
|
|
|
else if (this.params['mode'] == 'purge') {
|
|
|
|
if (sync_delete(this, '/queues/:vhost/:name/contents')) {
|
2010-10-15 01:09:36 +08:00
|
|
|
error_popup('info', "Queue purged");
|
|
|
|
update_partial();
|
2010-10-01 00:49:28 +08:00
|
|
|
}
|
|
|
|
}
|
2010-08-27 17:55:32 +08:00
|
|
|
return false;
|
|
|
|
});
|
2010-08-27 00:42:03 +08:00
|
|
|
|
2010-09-02 00:26:51 +08:00
|
|
|
this.post('#/bindings', function() {
|
2010-10-14 18:21:33 +08:00
|
|
|
if (sync_post(this, '/bindings/:vhost/e/:source/:destination_type/:destination'))
|
2010-09-09 01:00:32 +08:00
|
|
|
update();
|
2010-09-02 00:26:51 +08:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
this.del('#/bindings', function() {
|
2010-10-14 18:21:33 +08:00
|
|
|
if (sync_delete(this, '/bindings/:vhost/e/:source/:destination_type/:destination/:properties_key'))
|
2010-09-09 01:00:32 +08:00
|
|
|
update();
|
2010-09-02 00:26:51 +08:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2010-10-12 00:05:06 +08:00
|
|
|
path('#/vhosts', {'vhosts': '/vhosts'}, 'vhosts');
|
2010-08-24 01:32:33 +08:00
|
|
|
this.get('#/vhosts/:id', function() {
|
2010-09-29 23:43:38 +08:00
|
|
|
render({'vhost': '/vhosts/' + esc(this.params['id']),
|
|
|
|
'permissions': '/vhosts/' + esc(this.params['id']) + '/permissions',
|
|
|
|
'users': '/users/'},
|
|
|
|
'vhost', '#/vhosts');
|
2010-08-24 01:32:33 +08:00
|
|
|
});
|
|
|
|
this.put('#/vhosts', function() {
|
2010-10-08 00:23:11 +08:00
|
|
|
if (sync_put(this, '/vhosts/:name')) {
|
|
|
|
update_vhosts();
|
2010-09-09 01:00:32 +08:00
|
|
|
update();
|
2010-10-08 00:23:11 +08:00
|
|
|
}
|
2010-08-24 01:32:33 +08:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
this.del('#/vhosts', function() {
|
2010-10-08 00:23:11 +08:00
|
|
|
if (sync_delete(this, '/vhosts/:name')) {
|
|
|
|
update_vhosts();
|
2010-09-09 01:00:32 +08:00
|
|
|
go_to('#/vhosts');
|
2010-10-08 00:23:11 +08:00
|
|
|
}
|
2010-08-24 01:32:33 +08:00
|
|
|
return false;
|
|
|
|
});
|
2010-08-27 00:42:03 +08:00
|
|
|
|
2010-10-12 00:05:06 +08:00
|
|
|
path('#/users', {'users': '/users'}, 'users');
|
2010-08-24 01:32:33 +08:00
|
|
|
this.get('#/users/:id', function() {
|
2010-08-31 23:44:12 +08:00
|
|
|
render({'user': '/users/' + esc(this.params['id']),
|
2010-09-02 00:45:05 +08:00
|
|
|
'permissions': '/users/' + esc(this.params['id']) + '/permissions',
|
2010-08-31 23:44:12 +08:00
|
|
|
'vhosts': '/vhosts/'}, 'user',
|
2010-08-24 01:32:33 +08:00
|
|
|
'#/users');
|
|
|
|
});
|
|
|
|
this.put('#/users', function() {
|
2010-09-09 01:00:32 +08:00
|
|
|
if (sync_put(this, '/users/:username'))
|
|
|
|
update();
|
2010-08-24 01:32:33 +08:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
this.del('#/users', function() {
|
2010-09-09 01:00:32 +08:00
|
|
|
if (sync_delete(this, '/users/:username'))
|
|
|
|
go_to('#/users');
|
2010-08-24 01:32:33 +08:00
|
|
|
return false;
|
2010-08-23 23:32:17 +08:00
|
|
|
});
|
2010-08-27 00:42:03 +08:00
|
|
|
|
2010-08-25 00:13:47 +08:00
|
|
|
this.put('#/permissions', function() {
|
2010-09-09 01:00:32 +08:00
|
|
|
if (sync_put(this, '/permissions/:vhost/:username'))
|
|
|
|
update();
|
2010-08-25 00:13:47 +08:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
this.del('#/permissions', function() {
|
2010-09-09 01:00:32 +08:00
|
|
|
if (sync_delete(this, '/permissions/:vhost/:username'))
|
|
|
|
update();
|
2010-08-25 00:13:47 +08:00
|
|
|
return false;
|
|
|
|
});
|
2010-09-28 01:08:41 +08:00
|
|
|
this.get('#/import-succeeded', function() {
|
|
|
|
render({}, 'import-succeeded', '#/overview');
|
|
|
|
});
|
2010-08-23 23:32:17 +08:00
|
|
|
}
|
|
|
|
|
2010-08-24 01:32:33 +08:00
|
|
|
function go_to(url) {
|
|
|
|
this.location = url;
|
|
|
|
}
|
|
|
|
|
2010-08-23 23:32:17 +08:00
|
|
|
var current_template;
|
|
|
|
var current_reqs;
|
|
|
|
var current_highlight;
|
2010-10-08 00:23:11 +08:00
|
|
|
var current_vhost = '';
|
2010-10-12 00:05:06 +08:00
|
|
|
var current_sort;
|
|
|
|
var current_sort_reverse = false;
|
2010-09-29 22:59:39 +08:00
|
|
|
var timer;
|
|
|
|
var timer_interval;
|
|
|
|
|
|
|
|
function set_timer_interval(interval) {
|
|
|
|
timer_interval = interval;
|
2010-10-14 20:15:26 +08:00
|
|
|
reset_timer();
|
|
|
|
}
|
|
|
|
|
|
|
|
function reset_timer() {
|
2010-09-29 22:59:39 +08:00
|
|
|
clearInterval(timer);
|
|
|
|
if (timer_interval != null) {
|
2010-09-29 23:56:03 +08:00
|
|
|
timer = setInterval('partial_update()', timer_interval);
|
2010-09-29 22:59:39 +08:00
|
|
|
}
|
|
|
|
}
|
2010-07-13 00:11:42 +08:00
|
|
|
|
2010-08-23 23:32:17 +08:00
|
|
|
function render(reqs, template, highlight) {
|
|
|
|
current_template = template;
|
|
|
|
current_reqs = reqs;
|
|
|
|
current_highlight = highlight;
|
2010-09-29 23:56:03 +08:00
|
|
|
update();
|
2010-09-29 22:59:39 +08:00
|
|
|
}
|
|
|
|
|
2010-09-29 23:56:03 +08:00
|
|
|
function update() {
|
2010-09-29 22:59:39 +08:00
|
|
|
clearInterval(timer);
|
2010-10-08 00:23:11 +08:00
|
|
|
with_update(function(html) {
|
2010-09-29 22:59:39 +08:00
|
|
|
replace_content('main', html);
|
|
|
|
postprocess();
|
2010-10-12 00:05:06 +08:00
|
|
|
postprocess_partial();
|
2010-10-14 20:15:26 +08:00
|
|
|
reset_timer();
|
2010-09-29 22:59:39 +08:00
|
|
|
});
|
2010-07-13 20:03:36 +08:00
|
|
|
}
|
|
|
|
|
2010-09-29 23:56:03 +08:00
|
|
|
function partial_update() {
|
2010-09-29 22:59:39 +08:00
|
|
|
if ($('.updatable').length > 0) {
|
2010-10-08 00:23:11 +08:00
|
|
|
with_update(function(html) {
|
2010-09-29 22:59:39 +08:00
|
|
|
replace_content('scratch', html);
|
|
|
|
var befores = $('#main .updatable');
|
|
|
|
var afters = $('#scratch .updatable');
|
|
|
|
if (befores.length != afters.length) {
|
|
|
|
throw("before/after mismatch");
|
|
|
|
}
|
|
|
|
for (var i = 0; i < befores.length; i++) {
|
|
|
|
befores[i].innerHTML = afters[i].innerHTML;
|
|
|
|
}
|
2010-10-12 00:05:06 +08:00
|
|
|
postprocess_partial();
|
2010-09-29 22:59:39 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-08 00:23:11 +08:00
|
|
|
function with_update(fun) {
|
2010-10-12 00:05:06 +08:00
|
|
|
with_reqs(apply_state(current_reqs), [], function(json) {
|
2010-09-08 23:27:34 +08:00
|
|
|
json.statistics_level = statistics_level;
|
2010-08-23 23:32:17 +08:00
|
|
|
var html = format(current_template, json);
|
2010-09-29 22:59:39 +08:00
|
|
|
fun(html);
|
2010-08-31 23:44:12 +08:00
|
|
|
update_status('ok');
|
2010-08-24 01:32:33 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-10-12 00:05:06 +08:00
|
|
|
var VHOST_QUERIES = map(['/queues', '/exchanges']);
|
|
|
|
var SORT_QUERIES = map(['/connections', '/channels', '/vhosts', '/users',
|
|
|
|
'/queues', '/exchanges']);
|
|
|
|
|
|
|
|
function map(list) {
|
|
|
|
var res = {};
|
|
|
|
for (i in list) {
|
|
|
|
res[list[i]] = '';
|
2010-10-08 00:23:11 +08:00
|
|
|
}
|
2010-10-12 00:05:06 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
function apply_state(reqs) {
|
|
|
|
var reqs2 = {};
|
|
|
|
for (k in reqs) {
|
|
|
|
var req = reqs[k];
|
|
|
|
var req2;
|
|
|
|
if (req in VHOST_QUERIES && current_vhost != '') {
|
|
|
|
req2 = req + '/' + esc(current_vhost);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
req2 = req;
|
2010-10-08 00:23:11 +08:00
|
|
|
}
|
|
|
|
|
2010-10-12 00:05:06 +08:00
|
|
|
var qs = '';
|
|
|
|
if (req in SORT_QUERIES && current_sort != null) {
|
|
|
|
qs = '?sort=' + current_sort +
|
|
|
|
'&sort_reverse=' + current_sort_reverse;
|
|
|
|
}
|
|
|
|
|
|
|
|
reqs2[k] = req2 + qs;
|
2010-10-08 00:23:11 +08:00
|
|
|
}
|
2010-10-12 00:05:06 +08:00
|
|
|
return reqs2;
|
2010-10-08 00:23:11 +08:00
|
|
|
}
|
|
|
|
|
2010-10-15 01:09:36 +08:00
|
|
|
function error_popup(type, text) {
|
2010-10-16 01:11:42 +08:00
|
|
|
var cssClass = '.form-popup-' + type;
|
2010-10-15 01:09:36 +08:00
|
|
|
function hide() {
|
2010-10-16 01:11:42 +08:00
|
|
|
$(cssClass).slideUp(200, function() {
|
2010-10-15 01:09:36 +08:00
|
|
|
$(this).remove();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
hide();
|
2010-10-16 01:11:42 +08:00
|
|
|
$('h1').after(format('error-popup', {'type': type, 'text': text}));
|
|
|
|
$(cssClass).center().slideDown(200);
|
|
|
|
$(cssClass + ' span').click(hide);
|
2010-09-09 01:00:32 +08:00
|
|
|
}
|
|
|
|
|
2010-08-24 01:32:33 +08:00
|
|
|
function postprocess() {
|
|
|
|
$('a').removeClass('selected');
|
|
|
|
$('a[href="' + current_highlight + '"]').addClass('selected');
|
2010-08-24 17:24:50 +08:00
|
|
|
$('form.confirm').submit(function() {
|
|
|
|
return confirm("Are you sure? This object cannot be recovered " +
|
|
|
|
"after deletion.");
|
|
|
|
});
|
2010-08-24 17:57:19 +08:00
|
|
|
$('div.section h2, div.section-hidden h2').click(function() {
|
2010-09-29 01:27:09 +08:00
|
|
|
$(this).next().slideToggle(100);
|
|
|
|
$(this).toggleClass("toggled");
|
2010-08-24 17:57:19 +08:00
|
|
|
});
|
2010-08-25 17:46:08 +08:00
|
|
|
$('label').map(function() {
|
|
|
|
if ($(this).attr('for') == '') {
|
|
|
|
var id = 'auto-label-' + Math.floor(Math.random()*1000000000);
|
|
|
|
var input = $(this).parents('tr').first().find('input, select');
|
2010-09-29 20:22:46 +08:00
|
|
|
if (input.attr('id') == '') {
|
|
|
|
$(this).attr('for', id);
|
|
|
|
input.attr('id', id);
|
|
|
|
}
|
2010-08-25 17:46:08 +08:00
|
|
|
}
|
|
|
|
});
|
2010-09-29 20:22:46 +08:00
|
|
|
$('#download-configuration').click(function() {
|
|
|
|
var path = '/api/all-configuration?download=' +
|
|
|
|
esc($('#download-filename').val());
|
2010-10-01 01:28:28 +08:00
|
|
|
window.location = path;
|
|
|
|
setTimeout('app.run()');
|
2010-09-29 20:22:46 +08:00
|
|
|
return false;
|
|
|
|
});
|
2010-10-12 21:08:17 +08:00
|
|
|
$('.multifield input').live('blur', function() {
|
|
|
|
update_multifields();
|
|
|
|
});
|
2010-09-30 20:19:54 +08:00
|
|
|
if (! user_administrator) {
|
|
|
|
$('.administrator-only').remove();
|
|
|
|
}
|
2010-10-12 21:08:17 +08:00
|
|
|
update_multifields();
|
2010-08-07 00:52:26 +08:00
|
|
|
}
|
|
|
|
|
2010-10-12 00:05:06 +08:00
|
|
|
function postprocess_partial() {
|
|
|
|
$('.sort').click(function() {
|
|
|
|
var sort = $(this).attr('sort');
|
|
|
|
if (current_sort == sort) {
|
|
|
|
current_sort_reverse = ! current_sort_reverse;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
current_sort = sort;
|
|
|
|
current_sort_reverse = false;
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-10-12 21:08:17 +08:00
|
|
|
function update_multifields() {
|
|
|
|
$('.multifield').each(function(index) {
|
|
|
|
var largest_id = 0;
|
|
|
|
var empty_found = false;
|
|
|
|
var name = $(this).attr('id');
|
|
|
|
$('input[name$="_mfkey"]').each(function(index) {
|
|
|
|
var match = $(this).attr('name').
|
|
|
|
match(/[a-z]*_([0-9]*)_mfkey/);
|
|
|
|
var id = parseInt(match[1]);
|
|
|
|
largest_id = Math.max(id, largest_id);
|
|
|
|
var key = $(this).val();
|
|
|
|
var value = $(this).next('input').val();
|
|
|
|
if (key == '' && value == '') {
|
|
|
|
if (empty_found) {
|
|
|
|
$(this).parent().remove();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
empty_found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!empty_found) {
|
|
|
|
$(this).append('<p><input type="text" name="' + name + '_' +
|
|
|
|
(largest_id + 1) +
|
|
|
|
'_mfkey" value=""/> = ' +
|
|
|
|
'<input type="text" name="' + name + '_' +
|
|
|
|
(largest_id + 1) +
|
|
|
|
'_mfvalue" value=""/></p>');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-08-07 00:52:26 +08:00
|
|
|
function with_reqs(reqs, acc, fun) {
|
2010-09-02 19:58:34 +08:00
|
|
|
if (keys(reqs).length > 0) {
|
|
|
|
var key = keys(reqs)[0];
|
2010-08-31 23:44:12 +08:00
|
|
|
with_req('/api' + reqs[key], function(resp) {
|
|
|
|
acc[key] = jQuery.parseJSON(resp.responseText);
|
|
|
|
var remainder = {};
|
|
|
|
for (var k in reqs) {
|
|
|
|
if (k != key) remainder[k] = reqs[k];
|
|
|
|
}
|
|
|
|
with_reqs(remainder, acc, fun);
|
2010-08-07 00:52:26 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fun(acc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-13 00:31:16 +08:00
|
|
|
function replace_content(id, html) {
|
|
|
|
$("#" + id).empty();
|
|
|
|
$(html).appendTo("#" + id);
|
|
|
|
}
|
|
|
|
|
2010-07-12 22:04:42 +08:00
|
|
|
function format(template, json) {
|
|
|
|
try {
|
2010-08-27 23:18:41 +08:00
|
|
|
var tmpl = new EJS({url: 'js/tmpl/' + template + '.ejs'});
|
2010-07-17 00:38:06 +08:00
|
|
|
return tmpl.render(json);
|
2010-07-12 22:04:42 +08:00
|
|
|
} catch (err) {
|
2010-09-29 22:59:39 +08:00
|
|
|
clearInterval(timer);
|
2010-08-23 23:32:17 +08:00
|
|
|
debug(err['name'] + ": " + err['message']);
|
2010-07-12 22:04:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-31 23:44:12 +08:00
|
|
|
function update_status(status) {
|
2010-07-13 00:31:16 +08:00
|
|
|
var text;
|
|
|
|
if (status == 'ok')
|
2010-08-31 23:44:12 +08:00
|
|
|
text = "Last update: " + new Date();
|
2010-07-13 00:31:16 +08:00
|
|
|
else if (status == 'error')
|
2010-08-31 23:44:12 +08:00
|
|
|
text = "Error: could not connect to server at " + new Date();
|
2010-09-29 22:59:39 +08:00
|
|
|
else
|
|
|
|
throw("Unknown status " + status);
|
2010-07-13 00:31:16 +08:00
|
|
|
|
2010-07-17 00:38:06 +08:00
|
|
|
var html = format('status', {status: status, text: text});
|
2010-07-13 00:31:16 +08:00
|
|
|
replace_content('status', html);
|
|
|
|
}
|
|
|
|
|
2010-07-12 22:04:42 +08:00
|
|
|
function with_req(path, fun) {
|
|
|
|
var json;
|
2010-09-03 20:23:04 +08:00
|
|
|
var req = xmlHttpRequest();
|
2010-07-12 22:04:42 +08:00
|
|
|
req.open( "GET", path, true );
|
|
|
|
req.onreadystatechange = function () {
|
|
|
|
if (req.readyState == 4) {
|
|
|
|
if (req.status == 200) {
|
2010-08-31 23:44:12 +08:00
|
|
|
fun(req);
|
2010-07-12 22:04:42 +08:00
|
|
|
}
|
2010-07-13 00:31:16 +08:00
|
|
|
else if (req.status == 408) {
|
2010-08-31 23:44:12 +08:00
|
|
|
update_status('timeout');
|
2010-07-13 00:31:16 +08:00
|
|
|
}
|
2010-09-03 20:23:04 +08:00
|
|
|
else if (req.status == 0) { // Non-MSIE: could not connect
|
|
|
|
update_status('error');
|
|
|
|
}
|
|
|
|
else if (req.status > 12000) { // MSIE: could not connect
|
2010-08-31 23:44:12 +08:00
|
|
|
update_status('error');
|
2010-07-13 00:11:42 +08:00
|
|
|
}
|
2010-07-24 00:24:01 +08:00
|
|
|
else if (req.status == 404) {
|
|
|
|
var html = format('404', {});
|
|
|
|
replace_content('main', html);
|
|
|
|
}
|
2010-07-12 22:04:42 +08:00
|
|
|
else {
|
2010-08-23 23:32:17 +08:00
|
|
|
debug("Got response code " + req.status);
|
2010-09-29 22:59:39 +08:00
|
|
|
clearInterval(timer);
|
2010-07-12 22:04:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
req.send(null);
|
|
|
|
}
|
2010-07-13 20:03:36 +08:00
|
|
|
|
2010-09-08 23:27:34 +08:00
|
|
|
function sync_get(path) {
|
|
|
|
return sync_req('GET', [], path);
|
|
|
|
}
|
|
|
|
|
2010-08-25 01:46:30 +08:00
|
|
|
function sync_put(sammy, path_template) {
|
2010-09-09 01:00:32 +08:00
|
|
|
return sync_req('PUT', sammy.params, path_template);
|
2010-08-25 01:46:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function sync_delete(sammy, path_template) {
|
2010-09-09 01:00:32 +08:00
|
|
|
return sync_req('DELETE', sammy.params, path_template);
|
2010-08-25 01:46:30 +08:00
|
|
|
}
|
|
|
|
|
2010-09-02 00:26:51 +08:00
|
|
|
function sync_post(sammy, path_template) {
|
2010-09-09 01:00:32 +08:00
|
|
|
return sync_req('POST', sammy.params, path_template);
|
2010-09-02 00:26:51 +08:00
|
|
|
}
|
|
|
|
|
2010-10-12 21:08:17 +08:00
|
|
|
function sync_req(type, params0, path_template) {
|
|
|
|
var params = collapse_multifields(params0);
|
2010-10-15 01:09:36 +08:00
|
|
|
var path;
|
|
|
|
try {
|
|
|
|
path = fill_path_template(path_template, params);
|
|
|
|
} catch (e) {
|
2010-10-16 01:11:42 +08:00
|
|
|
error_popup('warn', e);
|
2010-10-15 01:09:36 +08:00
|
|
|
return false;
|
|
|
|
}
|
2010-09-03 20:23:04 +08:00
|
|
|
var req = xmlHttpRequest();
|
2010-08-31 21:22:52 +08:00
|
|
|
req.open(type, '/api' + path, false);
|
2010-08-24 01:32:33 +08:00
|
|
|
req.setRequestHeader('content-type', 'application/json');
|
2010-09-03 20:23:04 +08:00
|
|
|
try {
|
2010-09-29 14:18:23 +08:00
|
|
|
if (type == 'GET')
|
|
|
|
req.send(null);
|
|
|
|
else
|
|
|
|
req.send(JSON.stringify(params));
|
2010-09-03 20:23:04 +08:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
if (e.number == 0x80004004) {
|
|
|
|
// 0x80004004 means "Operation aborted."
|
|
|
|
// http://support.microsoft.com/kb/186063
|
|
|
|
// MSIE6 appears to do this in response to HTTP 204.
|
|
|
|
}
|
|
|
|
}
|
2010-08-24 01:32:33 +08:00
|
|
|
|
2010-10-15 01:09:36 +08:00
|
|
|
if (req.status == 400 || req.status == 404) {
|
|
|
|
var reason = JSON.parse(req.responseText).reason;
|
|
|
|
if (typeof(reason) != 'string') reason = JSON.stringify(reason);
|
2010-10-16 01:11:42 +08:00
|
|
|
error_popup('warn', reason);
|
2010-09-09 01:00:32 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-03 20:23:04 +08:00
|
|
|
// 1223 == 204 - see http://www.enhanceie.com/ie/bugs.asp
|
|
|
|
// MSIE7 and 8 appear to do this in response to HTTP 204.
|
|
|
|
if (req.status >= 400 && req.status != 1223) {
|
2010-08-25 00:13:47 +08:00
|
|
|
debug("Got response code " + req.status + " with body " +
|
|
|
|
req.responseText);
|
2010-08-24 01:32:33 +08:00
|
|
|
}
|
2010-09-08 23:27:34 +08:00
|
|
|
|
2010-09-09 01:00:32 +08:00
|
|
|
if (type == 'GET')
|
|
|
|
return req.responseText;
|
|
|
|
else
|
|
|
|
return true;
|
2010-08-24 01:32:33 +08:00
|
|
|
}
|
2010-07-13 20:03:36 +08:00
|
|
|
|
2010-08-25 01:46:30 +08:00
|
|
|
function fill_path_template(template, params) {
|
|
|
|
var re = /:[a-zA-Z_]*/g;
|
|
|
|
return template.replace(re, function(m) {
|
2010-10-15 01:09:36 +08:00
|
|
|
var str = esc(params[m.substring(1)]);
|
|
|
|
if (str == '') {
|
|
|
|
throw(m.substring(1) + " is required");
|
|
|
|
}
|
|
|
|
return str;
|
2010-08-25 01:46:30 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-10-12 21:08:17 +08:00
|
|
|
// Better suggestions appreciated
|
|
|
|
var INTEGER_ARGUMENTS = map(['x-expires']);
|
|
|
|
|
|
|
|
function collapse_multifields(params0) {
|
|
|
|
var params = {};
|
|
|
|
for (key in params0) {
|
|
|
|
var match = key.match(/([a-z]*)_([0-9]*)_mfkey/);
|
|
|
|
var match2 = key.match(/[a-z]*_[0-9]*_mfvalue/);
|
|
|
|
if (match == null && match2 == null) {
|
|
|
|
params[key] = params0[key];
|
|
|
|
}
|
|
|
|
else if (match == null) {
|
|
|
|
// Do nothing, value is handled below
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var name = match[1];
|
|
|
|
var id = match[2];
|
|
|
|
if (params[name] == undefined) {
|
|
|
|
params[name] = {};
|
|
|
|
}
|
|
|
|
if (params0[key] != "") {
|
|
|
|
var k = params0[key];
|
|
|
|
var v = params0[name + '_' + id + '_mfvalue'];
|
|
|
|
if (k in INTEGER_ARGUMENTS) {
|
|
|
|
v = parseInt(v);
|
|
|
|
}
|
|
|
|
params[name][k] = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2010-07-13 20:03:36 +08:00
|
|
|
function debug(str) {
|
|
|
|
$('<p>' + str + '</p>').appendTo('#debug');
|
2010-09-02 19:58:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function keys(obj) {
|
|
|
|
var ks = [];
|
|
|
|
for (var k in obj) {
|
|
|
|
ks.push(k);
|
|
|
|
}
|
|
|
|
return ks;
|
2010-09-03 20:23:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't use the jQuery AJAX support, it seemss to have trouble reporting
|
|
|
|
// server-down type errors.
|
|
|
|
function xmlHttpRequest() {
|
|
|
|
var res;
|
|
|
|
try {
|
|
|
|
res = new XMLHttpRequest();
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
res = new ActiveXObject("Microsoft.XMLHttp");
|
|
|
|
}
|
|
|
|
return res;
|
2010-09-09 01:00:32 +08:00
|
|
|
}
|
2010-10-16 01:11:42 +08:00
|
|
|
|
|
|
|
(function($){
|
|
|
|
$.fn.extend({
|
|
|
|
center: function () {
|
|
|
|
return this.each(function() {
|
|
|
|
var top = ($(window).height() - $(this).outerHeight()) / 2;
|
|
|
|
var left = ($(window).width() - $(this).outerWidth()) / 2;
|
|
|
|
$(this).css({margin:0, top: (top > 0 ? top : 0)+'px', left: (left > 0 ? left : 0)+'px'});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})(jQuery);
|