]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/panes/options.js
Merge pull request #402 from retropc/reqs
[irc/quakenet/qwebirc.git] / js / ui / panes / options.js
1 qwebirc.ui.supportsFocus = function() {
2 var ua = navigator.userAgent;
3 if(!$defined(ua))
4 return [true];
5
6 if(Browser.Engine.ipod || ua.indexOf("Konqueror") != -1)
7 return [false, false];
8
9 return [true];
10 }
11
12 /**
13 * Note that options are settable by the uioptions url arg by default unless you specifiy
14 * settableByURL...
15 */
16 qwebirc.config.DEFAULT_OPTIONS = [
17 [1, "BEEP_ON_MENTION", "Beep on activity", true],
18 [16, "NOTIFICATIONS", "Emit HTML5 notifications on activity", false, {
19 enabled: function() {
20 if(!("Notification" in window))
21 return [false, false]; /* [disabled, default_value] */
22 return [true];
23 },
24 applyChanges: function(value, ui) {
25 if(ui.setNotifications)
26 ui.setNotifications(value);
27 }
28 }],
29 [7, "FLASH_ON_MENTION", "Flash titlebar when nick mentioned or on query activity", true, {
30 enabled: qwebirc.ui.supportsFocus
31 }],
32 [2, "DEDICATED_MSG_WINDOW", "Send privmsgs to dedicated messages window", false],
33 [4, "DEDICATED_NOTICE_WINDOW", "Send notices to dedicated message window", false],
34 [3, "NICK_OV_STATUS", "Show status (@/+) before nicknames in channel lines", true],
35 /* 5 and 6 are reserved */
36 [8, "LASTPOS_LINE", "Show a last position indicator for each window", true, {
37 enabled: qwebirc.ui.supportsFocus
38 }],
39 [9, "NICK_COLOURS", "Automatically colour nicknames", false],
40 [10, "HIDE_JOINPARTS", "Hide JOINS/PARTS/QUITS", false],
41 [11, "STYLE_HUE", "Adjust user interface hue", function(ui) {
42 return {class_: qwebirc.config.HueOption, default_: ui.__styleValues.hue};
43 }, {
44 applyChanges: function(value, ui) {
45 ui.setModifiableStylesheetValues({hue: value});
46 }
47 }],
48 [12, "QUERY_ON_NICK_CLICK", "Query on nickname click in channel", false],
49 [13, "SHOW_NICKLIST", "Show nickname list in channels", qwebirc.util.deviceHasKeyboard()],
50 [14, "SHOW_TIMESTAMPS", "Show timestamps", true], /* we rely on the hue update */
51 [15, "SIDE_TABS", "Show tabs on the side", false, {
52 enabled: function() {
53 if(Browser.Engine.trident && Browser.Engine.version < 8)
54 return [false, false]; /* [disabled, default_value] */
55 return [true];
56 },
57 applyChanges: function(value, ui) {
58 ui.setSideTabs(value);
59 }
60 }]
61 ];
62
63 qwebirc.config.QUAKENET_OPTIONS = [
64 [5, "ACCEPT_SERVICE_INVITES", "Automatically join channels when invited by Q", false, {
65 settableByURL: false
66 }],
67 [6, "USE_HIDDENHOST", "Hide your hostmask when authed to Q (+x)", true, {
68 settableByURL: false
69 }]
70 ];
71
72 qwebirc.config.DefaultOptions = null;
73
74 qwebirc.config.Input = new Class({
75 initialize: function(parent, option, position, parentObject) {
76 this.option = option;
77 this.value = option.value;
78 this.enabled = this.option.enabled;
79 this.position = position;
80 this.parentElement = parent;
81 this.parentObject = parentObject;
82
83 this.render();
84 },
85 createInput: function(type, parent, name, selected, id) {
86 if(!$defined(parent))
87 parent = this.parentElement;
88
89 return qwebirc.util.createInput(type, parent, name, selected, this.option.id);
90 },
91 FE: function(element, parent) {
92 var n = new Element(element);
93 if(!$defined(parent))
94 parent = this.parentElement;
95
96 parent.appendChild(n);
97 return n;
98 },
99 focus: function() {
100 this.mainElement.focus();
101 },
102 render: function() {
103 this.event("render", this.mainElement);
104 },
105 applyChanges: function() {
106 this.event("applyChanges", [this.get(), this.parentObject.optionObject.ui]);
107 },
108 event: function(name, x) {
109 if(!$defined(this.option.extras))
110 return;
111 var t = this.option.extras[name];
112 if(!$defined(t))
113 return;
114
115 t.pass(x, this)();
116 },
117 cancel: function() {
118 }
119 });
120
121 qwebirc.config.TextInput = new Class({
122 Extends: qwebirc.config.Input,
123 render: function() {
124 var i = this.createInput("text");
125 this.mainElement = i;
126
127 i.value = this.value;
128 i.disabled = !this.enabled;
129
130 this.parent();
131 },
132 get: function() {
133 return this.mainElement.value;
134 }
135 });
136
137 qwebirc.config.HueInput = new Class({
138 Extends: qwebirc.config.Input,
139 wide: true,
140 render: function() {
141 var i = new Element("div");
142 i.addClass("qwebirc-optionspane");
143 i.addClass("hue-slider");
144 this.parentElement.appendChild(i);
145
146 var k = new Element("div");
147 k.addClass("knob");
148 if(Browser.Engine.trident) {
149 k.setStyle("top", "0px");
150 k.setStyle("background-color", "black");
151 }
152
153 i.appendChild(k);
154
155 var slider = new Slider(i, k, {steps: 36, range: [0, 369], wheel: true});
156 slider.set(this.value);
157 this.startValue = this.value;
158
159 slider.addEvent("change", function(step) {
160 this.value = step;
161 this.applyChanges();
162 }.bind(this));
163 this.mainElement = i;
164
165 if(!this.enabled)
166 slider.detach();
167
168 this.parent();
169 },
170 get: function() {
171 return this.value;
172 },
173 cancel: function() {
174 this.value = this.startValue;
175 this.applyChanges();
176 }
177 });
178
179 qwebirc.config.CheckInput = new Class({
180 Extends: qwebirc.config.Input,
181 render: function() {
182 var i = this.createInput("checkbox", null, null, null, this.id);
183 this.mainElement = i;
184
185 i.checked = this.value;
186 i.disabled = !this.enabled;
187
188 this.parent();
189 },
190 get: function() {
191 return this.mainElement.checked;
192 }
193 });
194
195 qwebirc.config.RadioInput = new Class({
196 Extends: qwebirc.config.Input,
197 render: function() {
198 var value = this.option.options;
199
200 this.elements = [];
201
202 for(var i=0;i<value.length;i++) {
203 var d = this.FE("div", this.parentObject);
204 var e = this.createInput("radio", d, "options_radio" + this.position, i == this.option.position);
205 this.elements.push(e);
206 e.disabled = !this.enabled;
207
208 if(i == 0)
209 this.mainElement = e;
210
211 d.appendChild(document.createTextNode(value[i][0]));
212 };
213 this.parent();
214 },
215 get: function() {
216 for(var i=0;i<this.elements.length;i++) {
217 var x = this.elements[i];
218 if(x.checked) {
219 this.option.position = i;
220 return this.option.options[i][1];
221 }
222 }
223 }
224 });
225
226 qwebirc.config.Option = new Class({
227 initialize: function(optionId, prefix, label, default_, extras) {
228 this.prefix = prefix;
229 this.label = label;
230 this.default_ = default_;
231 this.optionId = optionId;
232 this.extras = extras;
233
234 if($defined(extras) && $defined(extras.enabled)) {
235 var enabledResult = extras.enabled();
236 this.enabled = enabledResult[0];
237
238 if(!enabledResult[0] && enabledResult.length > 1)
239 this.default_ = enabledResult[1];
240 } else {
241 this.enabled = true;
242 }
243
244 if($defined(extras) && $defined(extras.settableByURL)) {
245 this.settableByURL = extras.settableByURL;
246 } else {
247 this.settableByURL = true;
248 }
249 },
250 setSavedValue: function(x) {
251 if(this.enabled)
252 this.value = x;
253 }
254 });
255
256 qwebirc.config.RadioOption = new Class({
257 Extends: qwebirc.config.Option,
258 Element: qwebirc.config.RadioInput,
259 initialize: function(optionId, prefix, label, default_, extras, options) {
260 this.options = options.map(function(x) {
261 if(typeof(x) == "string")
262 return [x, x];
263 return x;
264 });
265 this.defaultposition = default_;
266
267 this.parent(optionId, prefix, label, this.options[default_][1], extras);
268 },
269 setSavedValue: function(x) {
270 for(var i=0;i<this.options.length;i++) {
271 var y = this.options[i][1];
272 if(x == y) {
273 this.position = i;
274 this.value = x;
275 return;
276 }
277 }
278 this.position = this.defaultposition;
279 this.value = this.default_;
280 }
281 });
282
283 qwebirc.config.TextOption = new Class({
284 Extends: qwebirc.config.Option,
285 Element: qwebirc.config.TextInput
286 });
287
288 qwebirc.config.CheckOption = new Class({
289 Extends: qwebirc.config.Option,
290 Element: qwebirc.config.CheckInput
291 });
292
293 qwebirc.config.HueOption = new Class({
294 Extends: qwebirc.config.Option,
295 Element: qwebirc.config.HueInput
296 });
297
298 qwebirc.ui.Options = new Class({
299 initialize: function(ui) {
300 this.ui = ui;
301
302 if(!$defined(qwebirc.config.DefaultOptions))
303 this.__configureDefaults();
304
305 this.optionList = qwebirc.config.DefaultOptions.slice();
306 this.optionHash = {};
307
308 this._setup();
309 this.optionList.forEach(function(x) {
310 x.setSavedValue(this._get(x));
311 this.optionHash[x.prefix] = x;
312 this[x.prefix] = x.value;
313 }.bind(this));
314 },
315 __configureDefaults: function() {
316 var combined = qwebirc.config.DEFAULT_OPTIONS.slice(0);
317
318 var xo = null;
319 if(this.ui.options.networkName == "QuakeNet") /* HACK */
320 xo = qwebirc.config.QUAKENET_OPTIONS;
321
322 if(xo)
323 for(var i=0;i<xo.length;i++)
324 combined.push(xo[i]);
325
326 qwebirc.config.DefaultOptions = combined.map(function(x) {
327 var optionId = x[0];
328 var prefix = x[1];
329 var label = x[2];
330 var default_ = x[3];
331 var moreextras = x[4];
332 var extras = x[5];
333
334 var stype = typeof(default_);
335 if(stype == "number") {
336 return new qwebirc.config.RadioOption(optionId, prefix, label, default_, moreextras, extra);
337 } else {
338 var type;
339 if(stype == "boolean") {
340 type = qwebirc.config.CheckOption;
341 } else if(stype == "function") {
342 var options = default_.call(this, this.ui);
343 type = options.class_;
344 default_ = options.default_;
345 } else {
346 type = qwebirc.config.TextOption;
347 }
348 return new type(optionId, prefix, label, default_, moreextras);
349 }
350 }, this);
351 },
352 setValue: function(option, value) {
353 this.optionHash[option.prefix].value = value;
354 this[option.prefix] = value;
355 },
356 setValueByPrefix: function(prefix, value) {
357 this.optionHash[prefix].value = value;
358 this[prefix] = value;
359 },
360 getOptionList: function() {
361 return this.optionList;
362 },
363 _get: function(x) {
364 return x.default_;
365 },
366 _setup: function() {
367 },
368 flush: function() {
369 }
370 });
371
372 qwebirc.ui.OptionsPane = new Class({
373 Implements: [Events],
374 initialize: function(parentElement, optionObject) {
375 this.parentElement = parentElement;
376 this.optionObject = optionObject;
377
378 this.createElements();
379 },
380 createElements: function() {
381 var FE = function(element, parent) {
382 var n = new Element(element);
383 parent.appendChild(n);
384 return n;
385 };
386
387 var t = FE("table", this.parentElement);
388 var tb = FE("tbody", t);
389
390 this.boxList = [];
391
392 var optList = this.optionObject.getOptionList();
393 for(var i=0;i<optList.length;i++) {
394 var x = optList[i];
395
396 var row = FE("tr", tb);
397 var cell = FE("td", row);
398
399 x.id = qwebirc.util.generateID();
400 var ele = new x.Element(cell, x, i, this);
401 this.boxList.push([x, ele]);
402 var eleElement = cell.lastChild;
403 if(ele.wide)
404 cell.removeChild(eleElement);
405
406 var label = new Element("label", {"for": x.id});
407 cell.appendChild(label);
408
409 var eleParent;
410 if(ele.wide) {
411 label.set("text", x.label + ":");
412 cell.appendChild(eleElement);
413 } else {
414 label.set("text", x.label);
415 }
416 }
417
418 var r = FE("tr", tb);
419 var cella = FE("td", r);
420 var save = qwebirc.util.createInput("submit", cella);
421 save.value = "Save";
422
423 save.addEvent("click", function() {
424 this.save();
425 this.fireEvent("close");
426 }.bind(this));
427
428 var cancel = qwebirc.util.createInput("submit", cella);
429 cancel.value = "Cancel";
430 cancel.addEvent("click", function() {
431 this.cancel();
432 this.fireEvent("close");
433 }.bind(this));
434 },
435 save: function() {
436 this.boxList.forEach(function(x) {
437 var option = x[0];
438 var box = x[1];
439 this.optionObject.setValue(option, box.get());
440 }.bind(this));
441 this.boxList.forEach(function(x) {
442 x[1].applyChanges();
443 }.bind(this));
444 this.optionObject.flush();
445 },
446 cancel: function() {
447 this.boxList.forEach(function(x) {
448 x[1].cancel();
449 }.bind(this));
450 }
451 });
452
453 qwebirc.ui.CookieOptions = new Class({
454 Extends: qwebirc.ui.Options,
455 _setup: function() {
456 this.__cookie = new Hash.Cookie("opt1", {duration: 3650, autoSave: false});
457 },
458 _get: function(x) {
459 var v = this.__cookie.get(x.optionId);
460 if(!$defined(v))
461 return x.default_;
462
463 return v;
464 },
465 flush: function() {
466 this.__cookie.erase();
467 this._setup();
468
469 this.getOptionList().forEach(function(x) {
470 this.__cookie.set(x.optionId, x.value);
471 }.bind(this));
472 this.__cookie.save();
473 }
474 });
475
476 qwebirc.ui.SuppliedArgOptions = new Class({
477 Extends: qwebirc.ui.CookieOptions,
478 initialize: function(ui, arg) {
479 var p = new QHash();
480
481 if($defined(arg) && arg != "" && arg.length > 2) {
482 var checksum = arg.substr(arg.length - 2, 2);
483 var decoded = qwebirc.util.b64Decode(arg.substr(0, arg.length - 2));
484
485 if(decoded && (new qwebirc.util.crypto.MD5().digest(decoded).slice(0, 2) == checksum)) {
486 var p2 = qwebirc.util.parseURI("?" + decoded);
487 p2.each(function(k, v) {
488 p.put(k, JSON.decode(v, true));
489 });
490 }
491 }
492
493 this.parsedOptions = p;
494 this.parent(ui);
495 },
496 _get: function(x) {
497 if(x.settableByURL !== true)
498 return this.parent(x);
499
500 var opt = this.parsedOptions.get(String(x.optionId));
501 if(!$defined(opt))
502 return this.parent(x);
503
504 return opt;
505 },
506 serialise: function() {
507 var result = [];
508 this.getOptionList().forEach(function(x) {
509 if(x.settableByURL && x.default_ != x.value)
510 result.push(x.optionId + "=" + JSON.encode(x.value));
511 }.bind(this));
512
513 var raw = result.join("&");
514 var checksum = new qwebirc.util.crypto.MD5().digest(raw).slice(0, 2);
515 return (qwebirc.util.b64Encode(raw)).replaceAll("=", "") + checksum;
516 }
517 });
518
519 qwebirc.ui.DefaultOptionsClass = new Class({
520 Extends: qwebirc.ui.SuppliedArgOptions
521 });