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