]> jfr.im git - irc/quakenet/qwebirc.git/blame - js/ui/panes/options.js
Fixes issue 49, also option widgets can now have a default disabled value, rather...
[irc/quakenet/qwebirc.git] / js / ui / panes / options.js
CommitLineData
ebb21d2e
CP
1qwebirc.config.CHECK_BOX = 1;
2qwebirc.config.TEXT_BOX = 2;
3qwebirc.config.RADIO_BUTTONS = 3;
4
5qwebirc.config.DEFAULT_OPTIONS = [
aeb8c784 6 [1, "BEEP_ON_MENTION", "Beep when nick mentioned or on query activity (requires Flash)", true, {
2fb3b3b0
CP
7 enabled: function() {
8 if(!$defined(Browser.Plugins.Flash) || Browser.Plugins.Flash.version < 8)
9 return [false, false]; /* [disabled, default_value] */
10 return [true];
fb71087a
CP
11 },
12 get: function(value, ui) {
127631e0
CP
13 if(ui.setBeepOnMention)
14 ui.setBeepOnMention(value);
fb71087a
CP
15 }
16 }],
2fb3b3b0
CP
17 [7, "FLASH_ON_MENTION", "Flash titlebar when nick mentioned or on query activity", true, {
18 enabled: function() {
19 var ua = navigator.userAgent;
20 if(!$defined(ua))
21 return [true];
22
23 if(Browser.Engine.ipod || ua.indexOf("KHTML") != -1)
24 return [false, false];
25
26 return [true];
27 }
28 }],
7af2d0c1
CP
29 [2, "DEDICATED_MSG_WINDOW", "Send privmsgs to dedicated messages window", false],
30 [4, "DEDICATED_NOTICE_WINDOW", "Send notices to dedicated message window", false],
326478c2 31 [3, "NICK_OV_STATUS", "Show status (@/+) before nicknames in nicklist", true],
de4250ad 32 [5, "ACCEPT_SERVICE_INVITES", "Automatically join channels when invited by Q", true],
eeea9109 33 [6, "USE_HIDDENHOST", "Hide your hostmask when authed to Q (+x)", true]
ebb21d2e
CP
34];
35
36qwebirc.config.DefaultOptions = null;
37
c38a0240 38qwebirc.config.Input = new Class({
fb71087a 39 initialize: function(parent, option, position, parentObject) {
c38a0240
CP
40 this.option = option;
41 this.value = option.value;
2fb3b3b0 42 this.enabled = this.option.enabled;
c38a0240 43 this.position = position;
fb71087a
CP
44 this.parentElement = parent;
45 this.parentObject = parentObject;
c38a0240
CP
46
47 this.render();
48 },
a1e826c7
CP
49 createInput: function(type, parent, name, selected) {
50 if(!$defined(parent))
51 parent = this.parentElement;
52
53 return qwebirc.util.createInput(type, parent, name, selected);
54 },
c38a0240
CP
55 FE: function(element, parent) {
56 var n = new Element(element);
57 if(!$defined(parent))
fb71087a 58 parent = this.parentElement;
c38a0240
CP
59
60 parent.appendChild(n);
61 return n;
62 },
63 focus: function() {
64 this.mainElement.focus();
fb71087a
CP
65 },
66 render: function() {
67 this.event("render", this.mainElement);
68 },
69 get: function(value) {
70 this.event("get", [value, this.parentObject.optionObject.ui]);
71 return value;
72 },
73 event: function(name, x) {
74 if(!$defined(this.option.extras))
75 return;
76 var t = this.option.extras[name];
77 if(!$defined(t))
78 return;
79
80 t.pass(x, this)();
c38a0240
CP
81 }
82});
83
84qwebirc.config.TextInput = new Class({
85 Extends: qwebirc.config.Input,
86 render: function() {
a1e826c7 87 var i = this.createInput("text");
c38a0240
CP
88 this.mainElement = i;
89
c38a0240 90 i.value = this.value;
2fb3b3b0
CP
91 i.disabled = !this.enabled;
92
fb71087a 93 this.parent();
c38a0240
CP
94 },
95 get: function() {
fb71087a 96 return this.parent(this.mainElement.value);
c38a0240
CP
97 }
98});
99
100qwebirc.config.CheckInput = new Class({
101 Extends: qwebirc.config.Input,
102 render: function() {
a1e826c7 103 var i = this.createInput("checkbox");
c38a0240
CP
104 this.mainElement = i;
105
c38a0240 106 i.checked = this.value;
2fb3b3b0
CP
107 i.disabled = !this.enabled;
108
fb71087a 109 this.parent();
c38a0240
CP
110 },
111 get: function() {
fb71087a 112 return this.parent(this.mainElement.checked);
c38a0240
CP
113 }
114});
115
116qwebirc.config.RadioInput = new Class({
117 Extends: qwebirc.config.Input,
118 render: function() {
119 var value = this.option.options;
120
121 this.elements = [];
2fb3b3b0 122
c38a0240
CP
123 for(var i=0;i<value.length;i++) {
124 var d = this.FE("div", this.parentObject);
a1e826c7 125 var e = this.createInput("radio", d, "options_radio" + this.position, i == this.option.position);
c38a0240 126 this.elements.push(e);
2fb3b3b0
CP
127 e.disabled = !this.enabled;
128
c38a0240
CP
129 if(i == 0)
130 this.mainElement = e;
131
132 d.appendChild(document.createTextNode(value[i][0]));
133 };
fb71087a 134 this.parent();
c38a0240
CP
135 },
136 get: function() {
137 for(var i=0;i<this.elements.length;i++) {
138 var x = this.elements[i];
139 if(x.checked) {
140 this.option.position = i;
fb71087a 141 return this.parent(this.option.options[i][1]);
c38a0240
CP
142 }
143 }
144 }
145});
146
ebb21d2e 147qwebirc.config.Option = new Class({
fb71087a 148 initialize: function(optionId, prefix, label, default_, extras) {
c38a0240 149 this.prefix = prefix;
ebb21d2e 150 this.label = label;
c38a0240
CP
151 this.default_ = default_;
152 this.optionId = optionId;
fb71087a 153 this.extras = extras;
2fb3b3b0
CP
154
155 if($defined(extras) && $defined(extras.enabled)) {
156 var enabledResult = extras.enabled();
157 this.enabled = enabledResult[0];
158
159 if(!enabledResult[0] && enabledResult.length > 1)
160 this.default_ = enabledResult[1];
161 } else {
162 this.enabled = true;
163 }
c38a0240
CP
164 },
165 setSavedValue: function(x) {
166 this.value = x;
ebb21d2e
CP
167 }
168});
169
c38a0240
CP
170qwebirc.config.RadioOption = new Class({
171 Extends: qwebirc.config.Option,
172 Element: qwebirc.config.RadioInput,
fb71087a 173 initialize: function(optionId, prefix, label, default_, extras, options) {
c38a0240
CP
174 this.options = options.map(function(x) {
175 if(typeof(x) == "string")
176 return [x, x];
177 return x;
178 });
179 this.defaultposition = default_;
180
fb71087a 181 this.parent(optionId, prefix, label, this.options[default_][1], extras);
c38a0240
CP
182 },
183 setSavedValue: function(x) {
184 for(var i=0;i<this.options.length;i++) {
185 var y = this.options[i][1];
186 if(x == y) {
187 this.position = i;
188 this.value = x;
189 return;
190 }
191 }
192 this.position = this.defaultposition;
193 this.value = this.default_;
194 }
195});
196
197qwebirc.config.TextOption = new Class({
198 Extends: qwebirc.config.Option,
199 Element: qwebirc.config.TextInput
200});
201
202qwebirc.config.CheckOption = new Class({
203 Extends: qwebirc.config.Option,
204 Element: qwebirc.config.CheckInput
205});
206
ebb21d2e 207qwebirc.ui.Options = new Class({
fb71087a 208 initialize: function(ui) {
ebb21d2e
CP
209 if(!$defined(qwebirc.config.DefaultOptions))
210 this.__configureDefaults();
211
c38a0240
CP
212 this.optionList = qwebirc.config.DefaultOptions.slice();
213 this.optionHash = {}
fb71087a 214 this.ui = ui;
c38a0240
CP
215
216 this._setup();
217 this.optionList.forEach(function(x) {
218 x.setSavedValue(this._get(x));
219 this.optionHash[x.prefix] = x;
220 this[x.prefix] = x.value;
ebb21d2e
CP
221 }.bind(this));
222 },
223 __configureDefaults: function() {
c38a0240
CP
224 qwebirc.config.DefaultOptions = qwebirc.config.DEFAULT_OPTIONS.map(function(x) {
225 var optionId = x[0];
226 var prefix = x[1];
227 var label = x[2];
228 var default_ = x[3];
fb71087a
CP
229 var moreextras = x[4];
230 var extras = x[5];
ebb21d2e 231
c38a0240
CP
232 var stype = typeof(default_);
233 if(stype == "number") {
fb71087a 234 return new qwebirc.config.RadioOption(optionId, prefix, label, default_, moreextras, extra);
ebb21d2e 235 } else {
c38a0240
CP
236 var type;
237 if(stype == "boolean") {
238 type = qwebirc.config.CheckOption;
239 } else {
240 type = qwebirc.config.TextOption;
241 }
fb71087a 242 return new type(optionId, prefix, label, default_, moreextras);
ebb21d2e 243 }
ebb21d2e 244 });
c38a0240
CP
245 },
246 setValue: function(option, value) {
247 this.optionHash[option.prefix].value = value;
248 this[option.prefix] = value;
249 },
250 getOptionList: function() {
251 return this.optionList;
252 },
253 _get: function(x) {
254 return x.default_;
255 },
256 _setup: function() {
257 },
258 flush: function() {
ebb21d2e
CP
259 }
260});
261
262qwebirc.ui.OptionsPane = new Class({
c38a0240
CP
263 Implements: [Events],
264 initialize: function(parentElement, optionObject) {
ebb21d2e 265 this.parentElement = parentElement;
c38a0240 266 this.optionObject = optionObject;
ebb21d2e
CP
267
268 this.createElements();
269 },
270 createElements: function() {
271 var FE = function(element, parent) {
272 var n = new Element(element);
273 parent.appendChild(n);
274 return n;
275 };
ebb21d2e
CP
276
277 var t = FE("table", this.parentElement);
278 var tb = FE("tbody", t);
279
c38a0240
CP
280 this.boxList = [];
281
282 var optList = this.optionObject.getOptionList();
283 for(var i=0;i<optList.length;i++) {
284 var x = optList[i];
285
ebb21d2e
CP
286 var row = FE("tr", tb);
287 var cella = FE("td", row);
c38a0240
CP
288 cella.set("text", x.label + ":");
289
ebb21d2e 290 var cellb = FE("td", row);
fb71087a 291 this.boxList.push([x, new x.Element(cellb, x, i, this)]);
c38a0240 292
c38a0240
CP
293 }
294
295 var r = FE("tr", tb);
296 var cella = FE("td", r);
297 var cellb = FE("td", r);
a1e826c7 298 var save = qwebirc.util.createInput("submit", cellb);
c38a0240
CP
299 save.value = "Save";
300
301 save.addEvent("click", function() {
302 this.save();
303 this.fireEvent("close");
304 }.bind(this));
305
a1e826c7 306 var cancel = qwebirc.util.createInput("submit", cellb);
c38a0240
CP
307 cancel.value = "Cancel";
308 cancel.addEvent("click", function() {
309 this.fireEvent("close");
310 }.bind(this));
311 },
312 save: function() {
313 this.boxList.forEach(function(x) {
314 var option = x[0];
315 var box = x[1];
316 this.optionObject.setValue(option, box.get());
ebb21d2e 317 }.bind(this));
c38a0240 318 this.optionObject.flush();
ebb21d2e
CP
319 }
320});
c38a0240
CP
321
322qwebirc.ui.CookieOptions = new Class({
323 Extends: qwebirc.ui.Options,
324 _setup: function() {
325 this.__cookie = new Hash.Cookie("opt1", {duration: 3650, autoSave: false});
326 },
327 _get: function(x) {
328 var v = this.__cookie.get(x.optionId);
329 if(!$defined(v))
330 return x.default_;
331
332 return v;
333 },
334 flush: function() {
335 this.__cookie.erase();
336 this._setup();
337
338 this.getOptionList().forEach(function(x) {
339 this.__cookie.set(x.optionId, x.value);
340 }.bind(this));
341 this.__cookie.save();
342 }
343});
344
345qwebirc.ui.DefaultOptionsClass = new Class({
346 Extends: qwebirc.ui.CookieOptions
347});