]> jfr.im git - irc/quakenet/qwebirc.git/blame - js/ui/panes/options.js
add spinner to connect dialog
[irc/quakenet/qwebirc.git] / js / ui / panes / options.js
CommitLineData
85449eee
CP
1qwebirc.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
c0f2f367
CP
12/**
13 * Note that options are settable by the uioptions url arg by default unless you specifiy
14 * settableByURL...
15 */
ebb21d2e 16qwebirc.config.DEFAULT_OPTIONS = [
f63006ab 17 [1, "BEEP_ON_MENTION", "Beep on activity", true, {
82cf3f26 18 applyChanges: function(value, ui) {
127631e0
CP
19 if(ui.setBeepOnMention)
20 ui.setBeepOnMention(value);
fb71087a
CP
21 }
22 }],
f63006ab
CP
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 }],
2fb3b3b0 34 [7, "FLASH_ON_MENTION", "Flash titlebar when nick mentioned or on query activity", true, {
85449eee 35 enabled: qwebirc.ui.supportsFocus
2fb3b3b0 36 }],
7af2d0c1
CP
37 [2, "DEDICATED_MSG_WINDOW", "Send privmsgs to dedicated messages window", false],
38 [4, "DEDICATED_NOTICE_WINDOW", "Send notices to dedicated message window", false],
9aea28e8 39 [3, "NICK_OV_STATUS", "Show status (@/+) before nicknames in channel lines", true],
13afa38f 40 /* 5 and 6 are reserved */
85449eee
CP
41 [8, "LASTPOS_LINE", "Show a last position indicator for each window", true, {
42 enabled: qwebirc.ui.supportsFocus
43 }],
0a5b0904 44 [9, "NICK_COLOURS", "Automatically colour nicknames", false],
889ecb33 45 [10, "HIDE_JOINPARTS", "Hide JOINS/PARTS/QUITS", false],
ffb5ea9a
CP
46 [11, "STYLE_HUE", "Adjust user interface hue", function(ui) {
47 return {class_: qwebirc.config.HueOption, default_: ui.__styleValues.hue};
889ecb33 48 }, {
82cf3f26 49 applyChanges: function(value, ui) {
6f8a20df 50 ui.setModifiableStylesheetValues({hue: value});
889ecb33 51 }
cbef082a 52 }],
5f464ed5 53 [12, "QUERY_ON_NICK_CLICK", "Query on nickname click in channel", false],
22538fb7 54 [13, "SHOW_NICKLIST", "Show nickname list in channels", qwebirc.util.deviceHasKeyboard()],
a4a71818
CP
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 }]
ebb21d2e
CP
66];
67
13afa38f
CP
68qwebirc.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
ebb21d2e
CP
77qwebirc.config.DefaultOptions = null;
78
c38a0240 79qwebirc.config.Input = new Class({
fb71087a 80 initialize: function(parent, option, position, parentObject) {
c38a0240
CP
81 this.option = option;
82 this.value = option.value;
2fb3b3b0 83 this.enabled = this.option.enabled;
c38a0240 84 this.position = position;
fb71087a
CP
85 this.parentElement = parent;
86 this.parentObject = parentObject;
c38a0240
CP
87
88 this.render();
89 },
a9cbd00d 90 createInput: function(type, parent, name, selected, id) {
a1e826c7
CP
91 if(!$defined(parent))
92 parent = this.parentElement;
93
a9cbd00d 94 return qwebirc.util.createInput(type, parent, name, selected, this.option.id);
a1e826c7 95 },
c38a0240
CP
96 FE: function(element, parent) {
97 var n = new Element(element);
98 if(!$defined(parent))
fb71087a 99 parent = this.parentElement;
c38a0240
CP
100
101 parent.appendChild(n);
102 return n;
103 },
104 focus: function() {
105 this.mainElement.focus();
fb71087a
CP
106 },
107 render: function() {
108 this.event("render", this.mainElement);
109 },
82cf3f26
CP
110 applyChanges: function() {
111 this.event("applyChanges", [this.get(), this.parentObject.optionObject.ui]);
fb71087a
CP
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)();
889ecb33
CP
121 },
122 cancel: function() {
c38a0240
CP
123 }
124});
125
126qwebirc.config.TextInput = new Class({
127 Extends: qwebirc.config.Input,
128 render: function() {
a1e826c7 129 var i = this.createInput("text");
c38a0240
CP
130 this.mainElement = i;
131
c38a0240 132 i.value = this.value;
2fb3b3b0
CP
133 i.disabled = !this.enabled;
134
fb71087a 135 this.parent();
c38a0240
CP
136 },
137 get: function() {
82cf3f26 138 return this.mainElement.value;
c38a0240
CP
139 }
140});
141
889ecb33
CP
142qwebirc.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");
889ecb33
CP
148 this.parentElement.appendChild(i);
149
150 var k = new Element("div");
151 k.addClass("knob");
1f7d5f9e
CP
152 if(Browser.Engine.trident) {
153 k.setStyle("top", "0px");
154 k.setStyle("background-color", "black");
155 }
156
889ecb33
CP
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;
82cf3f26 165 this.applyChanges();
889ecb33
CP
166 }.bind(this));
167 this.mainElement = i;
168
169 if(!this.enabled)
170 slider.detach();
171
172 this.parent();
173 },
174 get: function() {
82cf3f26 175 return this.value;
889ecb33
CP
176 },
177 cancel: function() {
178 this.value = this.startValue;
82cf3f26 179 this.applyChanges();
889ecb33
CP
180 }
181});
182
c38a0240
CP
183qwebirc.config.CheckInput = new Class({
184 Extends: qwebirc.config.Input,
185 render: function() {
a9cbd00d 186 var i = this.createInput("checkbox", null, null, null, this.id);
c38a0240
CP
187 this.mainElement = i;
188
c38a0240 189 i.checked = this.value;
2fb3b3b0
CP
190 i.disabled = !this.enabled;
191
fb71087a 192 this.parent();
c38a0240
CP
193 },
194 get: function() {
82cf3f26 195 return this.mainElement.checked;
c38a0240
CP
196 }
197});
198
199qwebirc.config.RadioInput = new Class({
200 Extends: qwebirc.config.Input,
201 render: function() {
202 var value = this.option.options;
203
204 this.elements = [];
2fb3b3b0 205
c38a0240
CP
206 for(var i=0;i<value.length;i++) {
207 var d = this.FE("div", this.parentObject);
a1e826c7 208 var e = this.createInput("radio", d, "options_radio" + this.position, i == this.option.position);
c38a0240 209 this.elements.push(e);
2fb3b3b0
CP
210 e.disabled = !this.enabled;
211
c38a0240
CP
212 if(i == 0)
213 this.mainElement = e;
214
215 d.appendChild(document.createTextNode(value[i][0]));
216 };
fb71087a 217 this.parent();
c38a0240
CP
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;
82cf3f26 224 return this.option.options[i][1];
c38a0240
CP
225 }
226 }
227 }
228});
229
ebb21d2e 230qwebirc.config.Option = new Class({
fb71087a 231 initialize: function(optionId, prefix, label, default_, extras) {
c38a0240 232 this.prefix = prefix;
ebb21d2e 233 this.label = label;
c38a0240
CP
234 this.default_ = default_;
235 this.optionId = optionId;
fb71087a 236 this.extras = extras;
2fb3b3b0
CP
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;
c0f2f367
CP
246 }
247
248 if($defined(extras) && $defined(extras.settableByURL)) {
249 this.settableByURL = extras.settableByURL;
250 } else {
251 this.settableByURL = true;
252 }
c38a0240
CP
253 },
254 setSavedValue: function(x) {
01343497
CP
255 if(this.enabled)
256 this.value = x;
1f7d5f9e 257 }
ebb21d2e
CP
258});
259
c38a0240
CP
260qwebirc.config.RadioOption = new Class({
261 Extends: qwebirc.config.Option,
262 Element: qwebirc.config.RadioInput,
fb71087a 263 initialize: function(optionId, prefix, label, default_, extras, options) {
c38a0240
CP
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
fb71087a 271 this.parent(optionId, prefix, label, this.options[default_][1], extras);
c38a0240
CP
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
287qwebirc.config.TextOption = new Class({
288 Extends: qwebirc.config.Option,
289 Element: qwebirc.config.TextInput
290});
291
292qwebirc.config.CheckOption = new Class({
293 Extends: qwebirc.config.Option,
294 Element: qwebirc.config.CheckInput
295});
296
889ecb33
CP
297qwebirc.config.HueOption = new Class({
298 Extends: qwebirc.config.Option,
299 Element: qwebirc.config.HueInput
300});
301
ebb21d2e 302qwebirc.ui.Options = new Class({
fb71087a 303 initialize: function(ui) {
ffb5ea9a
CP
304 this.ui = ui;
305
ebb21d2e
CP
306 if(!$defined(qwebirc.config.DefaultOptions))
307 this.__configureDefaults();
308
c38a0240 309 this.optionList = qwebirc.config.DefaultOptions.slice();
ffb5ea9a
CP
310 this.optionHash = {};
311
c38a0240
CP
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;
ebb21d2e
CP
317 }.bind(this));
318 },
319 __configureDefaults: function() {
13afa38f
CP
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) {
c38a0240
CP
331 var optionId = x[0];
332 var prefix = x[1];
333 var label = x[2];
334 var default_ = x[3];
fb71087a
CP
335 var moreextras = x[4];
336 var extras = x[5];
ebb21d2e 337
c38a0240
CP
338 var stype = typeof(default_);
339 if(stype == "number") {
fb71087a 340 return new qwebirc.config.RadioOption(optionId, prefix, label, default_, moreextras, extra);
ebb21d2e 341 } else {
c38a0240
CP
342 var type;
343 if(stype == "boolean") {
344 type = qwebirc.config.CheckOption;
b076b3ab 345 } else if(stype == "function") {
ffb5ea9a 346 var options = default_.call(this, this.ui);
889ecb33 347 type = options.class_;
b076b3ab
CP
348 default_ = options.default_;
349 } else {
350 type = qwebirc.config.TextOption;
c38a0240 351 }
fb71087a 352 return new type(optionId, prefix, label, default_, moreextras);
ebb21d2e 353 }
ffb5ea9a 354 }, this);
c38a0240
CP
355 },
356 setValue: function(option, value) {
357 this.optionHash[option.prefix].value = value;
358 this[option.prefix] = value;
359 },
ffb5ea9a
CP
360 setValueByPrefix: function(prefix, value) {
361 this.optionHash[prefix].value = value;
362 this[prefix] = value;
363 },
c38a0240
CP
364 getOptionList: function() {
365 return this.optionList;
366 },
367 _get: function(x) {
368 return x.default_;
369 },
370 _setup: function() {
371 },
372 flush: function() {
ebb21d2e
CP
373 }
374});
375
376qwebirc.ui.OptionsPane = new Class({
c38a0240
CP
377 Implements: [Events],
378 initialize: function(parentElement, optionObject) {
ebb21d2e 379 this.parentElement = parentElement;
c38a0240 380 this.optionObject = optionObject;
ebb21d2e
CP
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 };
ebb21d2e
CP
390
391 var t = FE("table", this.parentElement);
392 var tb = FE("tbody", t);
393
c38a0240
CP
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
ebb21d2e
CP
400 var row = FE("tr", tb);
401 var cella = FE("td", row);
a9cbd00d
CP
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 + ":");
c38a0240 407
ebb21d2e 408 var cellb = FE("td", row);
fb71087a 409 this.boxList.push([x, new x.Element(cellb, x, i, this)]);
c38a0240 410
c38a0240
CP
411 }
412
413 var r = FE("tr", tb);
414 var cella = FE("td", r);
415 var cellb = FE("td", r);
a1e826c7 416 var save = qwebirc.util.createInput("submit", cellb);
c38a0240
CP
417 save.value = "Save";
418
419 save.addEvent("click", function() {
420 this.save();
421 this.fireEvent("close");
422 }.bind(this));
423
a1e826c7 424 var cancel = qwebirc.util.createInput("submit", cellb);
c38a0240
CP
425 cancel.value = "Cancel";
426 cancel.addEvent("click", function() {
889ecb33 427 this.cancel();
c38a0240
CP
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());
ebb21d2e 436 }.bind(this));
82cf3f26
CP
437 this.boxList.forEach(function(x) {
438 x[1].applyChanges();
ebb21d2e 439 }.bind(this));
c38a0240 440 this.optionObject.flush();
889ecb33
CP
441 },
442 cancel: function() {
443 this.boxList.forEach(function(x) {
444 x[1].cancel();
445 }.bind(this));
ebb21d2e
CP
446 }
447});
c38a0240
CP
448
449qwebirc.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
c0f2f367
CP
472qwebirc.ui.SuppliedArgOptions = new Class({
473 Extends: qwebirc.ui.CookieOptions,
474 initialize: function(ui, arg) {
ea29e3d7 475 var p = new QHash();
c0f2f367 476
8d1217eb
CP
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
10d70bfb
CP
481 if(decoded && (new qwebirc.util.crypto.MD5().digest(decoded).slice(0, 2) == checksum)) {
482 var p2 = qwebirc.util.parseURI("?" + decoded);
ea29e3d7
CP
483 p2.each(function(k, v) {
484 p.put(k, JSON.decode(v, true));
485 });
10d70bfb 486 }
c0f2f367
CP
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
ea29e3d7 496 var opt = this.parsedOptions.get(String(x.optionId));
c0f2f367
CP
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)
10d70bfb 506 result.push(x.optionId + "=" + JSON.encode(x.value));
c0f2f367
CP
507 }.bind(this));
508
8d1217eb
CP
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;
c0f2f367
CP
512 }
513});
514
c38a0240 515qwebirc.ui.DefaultOptionsClass = new Class({
c0f2f367 516 Extends: qwebirc.ui.SuppliedArgOptions
c38a0240 517});