-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.js
103 lines (78 loc) · 2.75 KB
/
settings.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
var controller;
window.onload = function () {
var page = new settingsPage(false);
page.initialise();
//cache the controller from the background page
controller = chrome.extension.getBackgroundPage().controller;
};
//Allows different display targets to be used via constants
var displayTypes = Object.freeze({
CONSOLE: "CONSOLE",
PAGE: "PAGE"
});
//manages the settings page
var settingsPage = (function () {
//constructor
function settingsPage (shouldClear) {
//testing only
if (shouldClear) {
evadi.blabr.data.clearSettings();
}
//user setting for display target
this.display = ko.observable(displayTypes.PAGE);
//keyboard shortcuts for this application
this.shortcuts = ko.observableArray();
//flag used to show a saved message to the user
this.settingsSaved = ko.observable(false);
//selects console as an output method
this.selectConsole = function () {
this.display(displayTypes.CONSOLE);
};
//selects page as an output method
this.selectPage = function () {
this.display(displayTypes.PAGE);
};
//initialise the knockoutjs bindings
ko.applyBindings(this);
}
//initialise the settings page
settingsPage.prototype.initialise = function () {
//read user settings and apply them
var _this = this;
evadi.blabr.data.getSettings(function (settings) {
_this.applySettings(settings);
});
//show the keyboard shortcuts associated with this application
evadi.blabr.shortcuts.getAll(function (commands) {
if (commands && commands.length > 1) {
commands.shift();
_this.shortcuts(commands);
}
});
};
//takes the raw settings object retrieved from data provider and applies the values
settingsPage.prototype.applySettings = function (settings) {
//set properties of this class based on the settings supplied
if (settings) {
this.display(settings.display);
}
};
//handles the UI element for saving settings - passes on the data provider
settingsPage.prototype.saveSettings = function () {
//read state of the page and save the settings
var _this = this;
//seralise this class to be saved
var settings = ko.toJSON(this);
//save the settings object
evadi.blabr.data.saveSettings(settings, function() {
_this.settingsSaved(true);
//inform the controller of the new settings
controller.updateSettings(ko.mapping.toJS(_this));
//display a message to the user indicating settings have been saved
window.setTimeout(function () {
_this.settingsSaved(false);
}, 2000);
});
};
return settingsPage;
})();