-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsettings.js
More file actions
172 lines (169 loc) · 5.55 KB
/
settings.js
File metadata and controls
172 lines (169 loc) · 5.55 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
var Settings = Class.extend ({
controller: undefined,
suspects: [],
user: undefined,
password: undefined,
isConfigured: null,
selectedIndex: undefined,
use_advanced_setings: undefined,
//FIXME implemented method let save the settings just for one URL
//TODO to save the settings global one have to use this approach
//http://stackoverflow.com/questions/12284952/access-extension-data-on-other-pages
init: function(controller) {
this.controller = controller;
this.addPropertyChangeListener (function (key, namespace, value) {
if (key == "github_user"){
this.user = value.newValue;
} else if (key == "github_pwd"){
this.password = value.newValue;
} else if (key == "use_advanced_setings"){
this.isConfigured = ((value.newValue != null) && (value.newValue != "null"));
this.use_advanced_setings = value.newValue;
} else if (key == "selected_index") {
this.selectedIndex = JSON.parse(value.newValue);
}
});
},
showConfig: function (callback) {
console.log("show/hide config...");
if ($(".config_window").length == 0){
$("body").prepend('<div class="config_window"></div>');
var left = (window.innerWidth - 200)/2;
var top = (window.innerHeight - 200)/2;
var ref = this;
$(".config_window").css({ 'position': 'fixed',
'background': '#CCC',
'border': '1px solid #ccc',
'width': '164px',
'z-index': '100',
'left': left + 'px',
'top': top + 'px',
'padding': '20px',
'padding-top': '10px',
});
//controls
$(".config_window").append('<h2>Settings</h2>');
$(".config_window").append('<b><input type="checkbox" id="use_advanced_setings" value="advanced">Advanced</b></br></br>');
$("#use_advanced_setings").click(this.actionAdvancedSettings);
$(".config_window").append('<b>Github user</b></br>');
$(".config_window").append('<b><input type="text" id="github_user"></b></br>');
$(".config_window").append('<b>Github password</b></br>');
$(".config_window").append('<b><input type="password" id="github_pwd"></b></br>');
$(".config_window").append('<button id="config_save_settings">Save</button>');
$("#config_save_settings").click(function() {
ref.actionSave ();
if (callback != undefined) callback();
});
$(".config_window").append('<button id="config_cancel_settings">cancel</button>');
$("#config_cancel_settings").click(function () {
ref.actionCancel();
if (callback != undefined) callback();
});
this.updateUI();
}else{
$(".config_window").remove();
}
return $(".config_window");
},
actionAdvancedSettings: function () {
if (!$("#use_advanced_setings").attr('checked')){
$("#github_user").attr('disabled', 'disabled');
$("#github_pwd").attr('disabled', 'disabled');
} else {
$("#github_user").removeAttr('disabled');
$("#github_pwd").removeAttr('disabled');
}
},
actionSave: function () {
console.log("saving settings...");
this.setValue("github_user", $("#github_user").val());
this.setValue("github_pwd", $("#github_pwd").val());
this.setValue("use_advanced_setings", ($("#use_advanced_setings").attr('checked') == 'checked'));
showConfig();
},
actionCancel: function () {
console.log("canceling settings...");
showConfig();
},
getValue: function (key, callBack) {
chrome.storage.local.get(key, function (items) {
callBack(items[key]);
});
},
setValue: function (key, value) {
var obj = {};
obj[key] = value;
chrome.storage.local.set(obj);
},
addPropertyChangeListener: function (propertyChanged) {
chrome.storage.onChanged.addListener(function(changes, namespace) {
for (key in changes) {
var storageChange = changes[key];
console.log('Storage key "%s" in namespace "%s" changed. ' +
'Old value was "%s", new value is "%s".',
key,
namespace,
storageChange.oldValue,
storageChange.newValue);
propertyChanged(key, namespace, storageChange);
}
});
},
updateUI: function (callBack) {
console.log("loading settings...");
if (this.use_advanced_setings){
$("#github_user").removeAttr('disabled');
$("#github_pwd").removeAttr('disabled');
$("#use_advanced_setings").attr('checked', 'checked');
$("#github_user").val(this.user);
$("#github_pwd").val(this.password);
} else {
$("#use_advanced_setings").removeAttr('checked');
$("#github_user").val("");
$("#github_pwd").val("");
$("#github_user").attr('disabled', 'disabled');
$("#github_pwd").attr('disabled', 'disabled');
}
if (callBack != undefined){
callBack();
}
},
preloadsettings: function (callBack, keys) {
if (keys == undefined) {
console.log("preloading settings...");
keys = ["use_advanced_setings", "github_user", "github_pwd", "selected_index"];
this.preloadsettings(callBack, keys);
} else {
if (keys.length == 0) {
console.log("DONE!");
callBack();
} else {
var key = keys.shift();
var ref = this;
this.getValue(key, function (val) {
console.log ("settings:" + key + "=" + val);
if (key == "github_user"){
ref.user = val;
} else if (key == "github_pwd"){
ref.password = val;
} else if (key == "use_advanced_setings"){
ref.isConfigured = ((val != null) && (val != "null"));
ref.use_advanced_setings = val;
} else if (key == "selected_index") {
ref.selectedIndex = (val!=undefined?JSON.parse(val):undefined);
}
ref.preloadsettings(callBack, keys);
});
}
}
},
isConfigured: function () {
return this.isConfigured;
},
getUser: function () {
return this.user;
},
getPwd: function () {
return this.password;
}
});