forked from Asana/Chrome-Extension-Example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options_page.js
80 lines (72 loc) · 2.42 KB
/
options_page.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
var init = function() {
fillOptions();
$("#reset_button").click(resetOptions);
};
// Restores select box state to saved value from localStorage.
var fillOptions = function() {
var options = Asana.Options.loadOptions();
$("#asana_host_port_input").val(options.asana_host_port);
fillDomainsInBackground(options);
};
var fillDomainsInBackground = function(opt_options) {
var options = opt_options || Asana.Options.loadOptions();
Asana.ServerModel.workspaces(function(workspaces) {
$("#domains_group").html("");
workspaces.forEach(function(domain) {
$("#domains_group").append(
'<label><input name="default_domain_id" type="radio" id="default_domain_id-' +
domain.id + '" key="' + domain.id + '"/>' + domain.name + '</label><br/>');
});
var default_domain_element = $("#default_domain_id-" + options.default_domain_id);
if (default_domain_element[0]) {
default_domain_element.attr("checked", "checked");
} else {
$("#domains_group").find("input")[0].checked = true;
}
$("#domains_group").find("input").change(onChange);
}, function(error_response) {
$("#domains_group").html(
'<div>Error loading workspaces. Verify the following:<ul>' +
'<li>Asana Host is configured correctly.</li>' +
'<li>You are <a target="_blank" href="' +
Asana.Options.loginUrl() +
'">logged in</a>.</li>' +
'<li>You have access to the Asana API.</li></ul>');
});
};
var onChange = function() {
setSaveEnabled(true);
};
var setSaveEnabled = function(enabled) {
var button = $("#save_button");
if (enabled) {
button.removeClass("disabled");
button.addClass("enabled");
button.click(saveOptions);
} else {
button.removeClass("enabled");
button.addClass("disabled");
button.unbind('click');
}
};
var resetOptions = function() {
Asana.Options.resetOptions();
fillOptions();
setSaveEnabled(false);
};
var saveOptions = function() {
var asana_host_port = $("#asana_host_port_input").val();
var default_domain_input = $("input[@name='default_domain_id']:checked");
Asana.Options.saveOptions({
asana_host_port: asana_host_port,
default_domain_id: default_domain_input
? default_domain_input.attr("key")
: 0
});
setSaveEnabled(false);
$("#status").html("Options saved.");
setTimeout(function() {
$("#status").html("");
}, 3000);
fillDomainsInBackground();
};