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