/
horizon.js
110 lines (95 loc) · 3.68 KB
/
horizon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* This is the base Horizon JavaScript object. There is only ever one of these
* loaded (referenced as horizon with a lower-case h) which happens immediately
* after the definition below.
*
* Scripts that are dependent on functionality defined in the Horizon object
* must be included after this script in templates/base.html.
*/
var Horizon = function() {
var horizon = {};
var initFunctions = [];
/* Use the addInitFunction() function to add initialization code which must
* be called on DOM ready. This is useful for adding things like event
* handlers or any other initialization functions which should preceed user
* interaction but rely on DOM readiness.
*/
horizon.addInitFunction = function(fn) {
initFunctions.push(fn);
};
/* Call all initialization functions and clear the queue. */
horizon.init = function() {
// Load client-side template fragments and compile them.
horizon.templates.compile_templates();
// Bind event handlers to confirm dangerous actions.
$("body").on("click", "form .btn-danger", function (evt) {
horizon.datatables.confirm(this);
evt.preventDefault();
});
// Bind dismiss(x) handlers for alert messages.
$(".alert").alert();
$.each(initFunctions, function(ind, fn) {
fn();
});
// Prevent multiple executions, just in case.
initFunctions = [];
};
/* Namespace for core functionality related to DataTables. */
horizon.datatables = {
update: function () {
var rows_to_update = $('tr.status_unknown');
if (rows_to_update.length) {
// Trigger the update handler.
var $updaters = rows_to_update.find('.ajax-update');
$updaters.click();
// Poll until there are no rows in an "unknown" state on the page.
setTimeout(horizon.datatables.update, $updaters.attr('data-update-interval'));
}
}
};
/* Generates a confirmation modal dialog for the given action. */
horizon.datatables.confirm = function (action) {
var $action = $(action),
action_string, title, body, modal, form;
action_string = $action.text();
title = "Confirm " + action_string;
body = "Please confirm your selection. This action cannot be undone.";
modal = horizon.modals.create(title, body, action_string);
modal.modal('show');
modal.find('.btn-primary').click(function (evt) {
form = $action.closest('form');
form.append("<input type='hidden' name='" + $action.attr('name') + "' value='" + $action.attr('value') + "'/>");
form.submit();
modal.modal('hide');
return false;
});
return modal;
};
/* Namespace for core functionality related to client-side templating. */
horizon.templates = {
template_ids: ["#modal_template"],
compiled_templates: {}
};
/* Pre-loads and compiles the client-side templates. */
horizon.templates.compile_templates = function () {
$.each(horizon.templates.template_ids, function (ind, template_id) {
horizon.templates.compiled_templates[template_id] = Hogan.compile($(template_id).text());
});
};
/* Namespace for core functionality related to modal dialogs. */
horizon.modals = {};
/* Creates a modal dialog from the client-side template. */
horizon.modals.create = function (title, body, confirm, cancel) {
if (!cancel) {
cancel = "Cancel";
}
var template = horizon.templates.compiled_templates["#modal_template"],
params = {title: title, body: body, confirm: confirm, cancel: cancel},
modal = $(template.render(params)).appendTo("body");
return modal;
};
return horizon;
};
// Create the one and only horizon object.
var horizon = Horizon();
// Call init on DOM ready.
$(document).ready(horizon.init);