~ubuntu-branches/ubuntu/saucy/rabbitmq-server/saucy-proposed

« back to all changes in this revision

Viewing changes to plugins-src/rabbitmq-management/priv/www/js/main.js

  • Committer: Package Import Robot
  • Author(s): Emile Joubert
  • Date: 2012-11-19 11:42:31 UTC
  • mfrom: (0.2.18) (0.1.32 sid)
  • Revision ID: package-import@ubuntu.com-20121119114231-hvapkn4akng09etr
Tags: 3.0.0-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
$(document).ready(function() {
2
 
    setup_global_vars();
3
 
    setup_constant_events();
4
 
    update_vhosts();
5
 
    update_interval();
6
 
    setup_extensions();
 
2
    replace_content('outer', format('login', {}));
 
3
    start_app_login();
7
4
});
8
5
 
9
6
function dispatcher_add(fun) {
19
16
    }
20
17
}
21
18
 
 
19
function start_app_login() {
 
20
    app = new Sammy.Application(function () {
 
21
        this.put('#/login', function() {
 
22
            username = this.params['username'];
 
23
            password = this.params['password'];
 
24
            var b64 = b64_encode_utf8(username + ':' + password);
 
25
            document.cookie = 'auth=' + encodeURIComponent(b64);
 
26
            check_login();
 
27
        });
 
28
    });
 
29
    app.run();
 
30
    if (get_cookie('auth') != '') {
 
31
        check_login();
 
32
    }
 
33
}
 
34
 
 
35
function check_login() {
 
36
    var user = JSON.parse(sync_get('/whoami'));
 
37
    if (user == false) {
 
38
        document.cookie = 'auth=; expires=Thu, 01 Jan 1970 00:00:00 GMT';
 
39
        replace_content('login-status', '<p>Login failed</p>');
 
40
    }
 
41
    else {
 
42
        replace_content('outer', format('layout', {}));
 
43
        setup_global_vars(user);
 
44
        setup_constant_events();
 
45
        update_vhosts();
 
46
        update_interval();
 
47
        setup_extensions();
 
48
    }
 
49
}
 
50
 
22
51
function start_app() {
23
 
    app = $.sammy(dispatcher);
 
52
    app.unload();
 
53
    // Oh boy. Sammy uses various different methods to determine if
 
54
    // the URL hash has changed. Unsurprisingly this is a native event
 
55
    // in modern browsers, and falls back to an icky polling function
 
56
    // in MSIE. But it looks like there's a bug. The polling function
 
57
    // should get installed when the app is started. But it's guarded
 
58
    // behind if (Sammy.HashLocationProxy._interval != null). And of
 
59
    // course that's not specific to the application; it's pretty
 
60
    // global. So we need to manually clear that in order for links to
 
61
    // work in MSIE.
 
62
    // Filed as https://github.com/quirkey/sammy/issues/171
 
63
    //
 
64
    // Note for when we upgrade: HashLocationProxy has become
 
65
    // DefaultLocationProxy in later versions, but otherwise the issue
 
66
    // remains.
 
67
    Sammy.HashLocationProxy._interval = null;
 
68
    app = new Sammy.Application(dispatcher);
24
69
    app.run();
25
70
    var url = this.location.toString();
26
71
    if (url.indexOf('#') == -1) {
70
115
 
71
116
function setup_extensions() {
72
117
    var extensions = JSON.parse(sync_get('/extensions'));
 
118
    extension_count = extensions.length;
73
119
    for (var i in extensions) {
74
120
        var extension = extensions[i];
75
121
        dynamic_load(extension.javascript);
76
122
    }
77
 
    extension_count = extensions.length;
78
123
}
79
124
 
80
125
function dynamic_load(filename) {
122
167
    }
123
168
}
124
169
 
 
170
function update_manual(div, query) {
 
171
    var path;
 
172
    var template;
 
173
    if (query == 'memory') {
 
174
        path = current_reqs['node'] + '?memory=true';
 
175
        template = 'memory';
 
176
    }
 
177
    var data = JSON.parse(sync_get(path));
 
178
 
 
179
    replace_content(div, format(template, data));
 
180
    postprocess_partial();
 
181
}
 
182
 
125
183
function render(reqs, template, highlight) {
126
184
    current_template = template;
127
185
    current_reqs = reqs;
132
190
function update() {
133
191
    clearInterval(timer);
134
192
    with_update(function(html) {
 
193
            update_navigation();
135
194
            replace_content('main', html);
136
195
            postprocess();
137
196
            postprocess_partial();
164
223
    }
165
224
}
166
225
 
 
226
function update_navigation() {
 
227
    var l1 = '';
 
228
    var l2 = '';
 
229
    var descend = null;
 
230
 
 
231
    for (var k in NAVIGATION) {
 
232
        var val = NAVIGATION[k];
 
233
        var path = val;
 
234
        while (!leaf(path)) {
 
235
            path = path[keys(path)[0]];
 
236
        }
 
237
        var selected = false;
 
238
        if (contains_current_highlight(val)) {
 
239
            selected = true;
 
240
            if (!leaf(val)) {
 
241
                descend = nav(val);
 
242
            }
 
243
        }
 
244
        if (show(path)) {
 
245
            l1 += '<li><a href="' + nav(path) + '"' +
 
246
                (selected ? ' class="selected"' : '') + '>' + k + '</a></li>';
 
247
        }
 
248
    }
 
249
 
 
250
    if (descend) {
 
251
        l2 = obj_to_ul(descend);
 
252
        $('#main').addClass('with-rhs');
 
253
    }
 
254
    else {
 
255
        $('#main').removeClass('with-rhs');
 
256
    }
 
257
 
 
258
    replace_content('tabs', l1);
 
259
    replace_content('rhs', l2);
 
260
}
 
261
 
 
262
function nav(pair) {
 
263
    return pair[0];
 
264
}
 
265
 
 
266
function show(pair) {
 
267
    return !pair[1] || user_administrator;
 
268
}
 
269
 
 
270
function leaf(pair) {
 
271
    return typeof(nav(pair)) == 'string';
 
272
}
 
273
 
 
274
function contains_current_highlight(val) {
 
275
    if (leaf(val)) {
 
276
        return current_highlight == nav(val);
 
277
    }
 
278
    else {
 
279
        var b = false;
 
280
        for (var k in val) {
 
281
            b |= contains_current_highlight(val[k]);
 
282
        }
 
283
        return b;
 
284
    }
 
285
}
 
286
 
 
287
function obj_to_ul(val) {
 
288
    var res = '<ul>';
 
289
    for (var k in val) {
 
290
        res += '<li>';
 
291
        var obj = val[k];
 
292
        if (leaf(obj) && show(obj)) {
 
293
            res += '<a href="' + nav(obj) + '"' +
 
294
                (current_highlight == nav(obj) ? ' class="selected"' : '') +
 
295
                '>' + k + '</a>';
 
296
        }
 
297
        else {
 
298
            res += obj_to_ul(nav(obj));
 
299
        }
 
300
        res += '</li>';
 
301
    }
 
302
    return res + '</ul>';
 
303
}
 
304
 
167
305
function full_refresh() {
168
306
    store_pref('position', x_position() + ',' + y_position());
169
307
    location.reload();
208
346
    for (k in reqs) {
209
347
        var req = reqs[k];
210
348
        var req2;
211
 
        if (req in VHOST_QUERIES && current_vhost != '') {
 
349
        if (vhost_query(req) && current_vhost != '') {
212
350
            req2 = req + '/' + esc(current_vhost);
213
351
        }
214
352
        else {
226
364
    return reqs2;
227
365
}
228
366
 
 
367
function vhost_query(req) {
 
368
    for (i in VHOST_QUERIES) {
 
369
        var query = VHOST_QUERIES[i];
 
370
        if (req.match(query)) return true;
 
371
    }
 
372
    return false;
 
373
}
 
374
 
229
375
function show_popup(type, text) {
230
376
    var cssClass = '.form-popup-' + type;
231
377
    function hide() {
241
387
}
242
388
 
243
389
function postprocess() {
244
 
    $('a').removeClass('selected');
245
 
    $('a[href="' + current_highlight + '"]').addClass('selected');
246
390
    $('form.confirm').submit(function() {
247
391
            return confirm("Are you sure? This object cannot be recovered " +
248
392
                           "after deletion.");
267
411
            setTimeout('app.run()');
268
412
            return false;
269
413
        });
 
414
    $('.update-manual').click(function() {
 
415
            update_manual($(this).attr('for'), $(this).attr('query'));
 
416
        });
270
417
    $('input, select').die();
271
418
    $('.multifield input').live('blur', function() {
272
419
            update_multifields();
331
478
                    }
332
479
                });
333
480
            if (!empty_found) {
334
 
                $(this).append('<p><input type="text" name="' + name + '_' +
335
 
                               (largest_id + 1) +
 
481
                var prefix = name + '_' + (largest_id + 1);
 
482
                var type_part;
 
483
                if ($(this).hasClass('string-only')) {
 
484
                    type_part = '<input type="hidden" name="' + prefix +
 
485
                        '_mftype" value="string"/>';
 
486
                } else {
 
487
                    type_part = '<select name="' + prefix +
 
488
                        '_mftype">' +
 
489
                        '<option value="string">String</option>' +
 
490
                        '<option value="number">Number</option>' +
 
491
                        '<option value="boolean">Boolean</option>' +
 
492
                        '</select>';
 
493
                }
 
494
                $(this).append('<p><input type="text" name="' + prefix +
336
495
                               '_mfkey" value=""/> = ' +
337
 
                               '<input type="text" name="' + name + '_' +
338
 
                               (largest_id + 1) +
339
 
                               '_mfvalue" value=""/></p>');
 
496
                               '<input type="text" name="' + prefix +
 
497
                               '_mfvalue" value=""/> ' + type_part + '</p>');
340
498
            }
341
499
        });
342
500
}
470
628
    replace_content('status', html);
471
629
}
472
630
 
 
631
function auth_header() {
 
632
    return "Basic " + decodeURIComponent(get_cookie('auth'));
 
633
}
 
634
 
473
635
function with_req(method, path, body, fun) {
474
636
    var json;
475
637
    var req = xmlHttpRequest();
476
638
    req.open(method, 'api' + path, true );
 
639
    req.setRequestHeader('authorization', auth_header());
477
640
    req.onreadystatechange = function () {
478
641
        if (req.readyState == 4) {
479
642
            if (check_bad_response(req, true)) {
514
677
    var req = xmlHttpRequest();
515
678
    req.open(type, 'api' + path, false);
516
679
    req.setRequestHeader('content-type', 'application/json');
 
680
    req.setRequestHeader('authorization', auth_header());
517
681
    try {
518
682
        if (type == 'GET')
519
683
            req.send(null);
598
762
    for (key in params0) {
599
763
        var match = key.match(/([a-z]*)_([0-9]*)_mfkey/);
600
764
        var match2 = key.match(/[a-z]*_[0-9]*_mfvalue/);
601
 
        if (match == null && match2 == null) {
 
765
        var match3 = key.match(/[a-z]*_[0-9]*_mftype/);
 
766
        if (match == null && match2 == null && match3 == null) {
602
767
            params[key] = params0[key];
603
768
        }
604
769
        else if (match == null) {
613
778
            if (params0[key] != "") {
614
779
                var k = params0[key];
615
780
                var v = params0[name + '_' + id + '_mfvalue'];
616
 
                params[name][k] = v;
 
781
                var t = params0[name + '_' + id + '_mftype'];
 
782
                if (t == 'boolean') {
 
783
                    if (v != 'true' && v != 'false')
 
784
                        throw(k + ' must be "true" or "false"; got ' + v);
 
785
                    params[name][k] = (v == 'true');
 
786
                }
 
787
                else if (t == 'number') {
 
788
                    var n = parseFloat(v);
 
789
                    if (isNaN(n))
 
790
                        throw(k + ' must be a number; got ' + v);
 
791
                    params[name][k] = n;
 
792
                }
 
793
                else {
 
794
                    params[name][k] = v;
 
795
                }
617
796
            }
618
797
        }
619
798
    }
666
845
    return params;
667
846
}
668
847
 
 
848
function put_parameter(sammy, mandatory_keys, num_keys, bool_keys) {
 
849
    for (var i in sammy.params) {
 
850
        if (i === 'length' || !sammy.params.hasOwnProperty(i)) continue;
 
851
        if (sammy.params[i] == '' && mandatory_keys.indexOf(i) == -1) {
 
852
            delete sammy.params[i];
 
853
        }
 
854
        else if (num_keys.indexOf(i) != -1) {
 
855
            sammy.params[i] = parseInt(sammy.params[i]);
 
856
        }
 
857
        else if (bool_keys.indexOf(i) != -1) {
 
858
            sammy.params[i] = sammy.params[i] == 'true';
 
859
        }
 
860
    }
 
861
    var params = {"component": sammy.params.component,
 
862
                  "vhost":     sammy.params.vhost,
 
863
                  "name":      sammy.params.name,
 
864
                  "value":     params_magic(sammy.params)};
 
865
    delete params.value.vhost;
 
866
    delete params.value.component;
 
867
    delete params.value.name;
 
868
    sammy.params = params;
 
869
    if (sync_put(sammy, '/parameters/:component/:vhost/:name')) update();
 
870
}
 
871
 
 
872
function put_policy(sammy, mandatory_keys, num_keys, bool_keys) {
 
873
    for (var i in sammy.params) {
 
874
        if (i === 'length' || !sammy.params.hasOwnProperty(i)) continue;
 
875
        if (sammy.params[i] == '' && mandatory_keys.indexOf(i) == -1) {
 
876
            delete sammy.params[i];
 
877
        }
 
878
        else if (num_keys.indexOf(i) != -1) {
 
879
            sammy.params[i] = parseInt(sammy.params[i]);
 
880
        }
 
881
        else if (bool_keys.indexOf(i) != -1) {
 
882
            sammy.params[i] = sammy.params[i] == 'true';
 
883
        }
 
884
    }
 
885
    if (sync_put(sammy, '/policies/:vhost/:name')) update();
 
886
}
 
887
 
669
888
function debug(str) {
670
889
    $('<p>' + str + '</p>').appendTo('#debug');
671
890
}
691
910
    return res;
692
911
}
693
912
 
 
913
// Our base64 library takes a string that is really a byte sequence,
 
914
// and will throw if given a string with chars > 255 (and hence not
 
915
// DTRT for chars > 127). So encode a unicode string as a UTF-8
 
916
// sequence of "bytes".
 
917
function b64_encode_utf8(str) {
 
918
    return base64.encode(encode_utf8(str));
 
919
}
 
920
 
 
921
// encodeURIComponent handles utf-8, unescape does not. Neat!
 
922
function encode_utf8(str) {
 
923
  return unescape(encodeURIComponent(str));
 
924
}
 
925
 
694
926
(function($){
695
927
    $.fn.extend({
696
928
        center: function () {
701
933
            });
702
934
        }
703
935
    });
704
 
})(jQuery);
 
 
b'\\ No newline at end of file'
 
936
})(jQuery);