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