]> jfr.im git - irc/quakenet/qwebirc.git/blame - js/ui/panes/options.js
Add dynamic setting of UI options in URL.
[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
CP
22 },
23 get: 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 }, {
48 get: function(value, ui) {
49 ui.setModifiableStylesheetValues(value, 0, 0);
50 }
51 }]
ebb21d2e
CP
52];
53
54qwebirc.config.DefaultOptions = null;
55
c38a0240 56qwebirc.config.Input = new Class({
fb71087a 57 initialize: function(parent, option, position, parentObject) {
c38a0240
CP
58 this.option = option;
59 this.value = option.value;
2fb3b3b0 60 this.enabled = this.option.enabled;
c38a0240 61 this.position = position;
fb71087a
CP
62 this.parentElement = parent;
63 this.parentObject = parentObject;
c38a0240
CP
64
65 this.render();
66 },
a1e826c7
CP
67 createInput: function(type, parent, name, selected) {
68 if(!$defined(parent))
69 parent = this.parentElement;
70
71 return qwebirc.util.createInput(type, parent, name, selected);
72 },
c38a0240
CP
73 FE: function(element, parent) {
74 var n = new Element(element);
75 if(!$defined(parent))
fb71087a 76 parent = this.parentElement;
c38a0240
CP
77
78 parent.appendChild(n);
79 return n;
80 },
81 focus: function() {
82 this.mainElement.focus();
fb71087a
CP
83 },
84 render: function() {
85 this.event("render", this.mainElement);
86 },
87 get: function(value) {
88 this.event("get", [value, this.parentObject.optionObject.ui]);
89 return value;
90 },
91 event: function(name, x) {
92 if(!$defined(this.option.extras))
93 return;
94 var t = this.option.extras[name];
95 if(!$defined(t))
96 return;
97
98 t.pass(x, this)();
889ecb33
CP
99 },
100 cancel: function() {
c38a0240
CP
101 }
102});
103
104qwebirc.config.TextInput = new Class({
105 Extends: qwebirc.config.Input,
106 render: function() {
a1e826c7 107 var i = this.createInput("text");
c38a0240
CP
108 this.mainElement = i;
109
c38a0240 110 i.value = this.value;
2fb3b3b0
CP
111 i.disabled = !this.enabled;
112
fb71087a 113 this.parent();
c38a0240
CP
114 },
115 get: function() {
fb71087a 116 return this.parent(this.mainElement.value);
c38a0240
CP
117 }
118});
119
889ecb33
CP
120qwebirc.config.HueInput = new Class({
121 Extends: qwebirc.config.Input,
122 render: function() {
123 var i = new Element("div");
124 i.addClass("qwebirc-optionspane");
125 i.addClass("hue-slider");
889ecb33
CP
126 this.parentElement.appendChild(i);
127
128 var k = new Element("div");
129 k.addClass("knob");
1f7d5f9e
CP
130 if(Browser.Engine.trident) {
131 k.setStyle("top", "0px");
132 k.setStyle("background-color", "black");
133 }
134
889ecb33
CP
135 i.appendChild(k);
136
137 var slider = new Slider(i, k, {steps: 36, range: [0, 369], wheel: true});
138 slider.set(this.value);
139 this.startValue = this.value;
140
141 slider.addEvent("change", function(step) {
142 this.value = step;
143 this.get();
144 }.bind(this));
145 this.mainElement = i;
146
147 if(!this.enabled)
148 slider.detach();
149
150 this.parent();
151 },
152 get: function() {
153 return this.parent(this.value);
154 },
155 cancel: function() {
156 this.value = this.startValue;
157 this.get();
158 }
159});
160
c38a0240
CP
161qwebirc.config.CheckInput = new Class({
162 Extends: qwebirc.config.Input,
163 render: function() {
a1e826c7 164 var i = this.createInput("checkbox");
c38a0240
CP
165 this.mainElement = i;
166
c38a0240 167 i.checked = this.value;
2fb3b3b0
CP
168 i.disabled = !this.enabled;
169
fb71087a 170 this.parent();
c38a0240
CP
171 },
172 get: function() {
fb71087a 173 return this.parent(this.mainElement.checked);
c38a0240
CP
174 }
175});
176
177qwebirc.config.RadioInput = new Class({
178 Extends: qwebirc.config.Input,
179 render: function() {
180 var value = this.option.options;
181
182 this.elements = [];
2fb3b3b0 183
c38a0240
CP
184 for(var i=0;i<value.length;i++) {
185 var d = this.FE("div", this.parentObject);
a1e826c7 186 var e = this.createInput("radio", d, "options_radio" + this.position, i == this.option.position);
c38a0240 187 this.elements.push(e);
2fb3b3b0
CP
188 e.disabled = !this.enabled;
189
c38a0240
CP
190 if(i == 0)
191 this.mainElement = e;
192
193 d.appendChild(document.createTextNode(value[i][0]));
194 };
fb71087a 195 this.parent();
c38a0240
CP
196 },
197 get: function() {
198 for(var i=0;i<this.elements.length;i++) {
199 var x = this.elements[i];
200 if(x.checked) {
201 this.option.position = i;
fb71087a 202 return this.parent(this.option.options[i][1]);
c38a0240
CP
203 }
204 }
205 }
206});
207
ebb21d2e 208qwebirc.config.Option = new Class({
fb71087a 209 initialize: function(optionId, prefix, label, default_, extras) {
c38a0240 210 this.prefix = prefix;
ebb21d2e 211 this.label = label;
c38a0240
CP
212 this.default_ = default_;
213 this.optionId = optionId;
fb71087a 214 this.extras = extras;
2fb3b3b0
CP
215
216 if($defined(extras) && $defined(extras.enabled)) {
217 var enabledResult = extras.enabled();
218 this.enabled = enabledResult[0];
219
220 if(!enabledResult[0] && enabledResult.length > 1)
221 this.default_ = enabledResult[1];
222 } else {
223 this.enabled = true;
c0f2f367
CP
224 }
225
226 if($defined(extras) && $defined(extras.settableByURL)) {
227 this.settableByURL = extras.settableByURL;
228 } else {
229 this.settableByURL = true;
230 }
c38a0240
CP
231 },
232 setSavedValue: function(x) {
01343497
CP
233 if(this.enabled)
234 this.value = x;
1f7d5f9e 235 }
ebb21d2e
CP
236});
237
c38a0240
CP
238qwebirc.config.RadioOption = new Class({
239 Extends: qwebirc.config.Option,
240 Element: qwebirc.config.RadioInput,
fb71087a 241 initialize: function(optionId, prefix, label, default_, extras, options) {
c38a0240
CP
242 this.options = options.map(function(x) {
243 if(typeof(x) == "string")
244 return [x, x];
245 return x;
246 });
247 this.defaultposition = default_;
248
fb71087a 249 this.parent(optionId, prefix, label, this.options[default_][1], extras);
c38a0240
CP
250 },
251 setSavedValue: function(x) {
252 for(var i=0;i<this.options.length;i++) {
253 var y = this.options[i][1];
254 if(x == y) {
255 this.position = i;
256 this.value = x;
257 return;
258 }
259 }
260 this.position = this.defaultposition;
261 this.value = this.default_;
262 }
263});
264
265qwebirc.config.TextOption = new Class({
266 Extends: qwebirc.config.Option,
267 Element: qwebirc.config.TextInput
268});
269
270qwebirc.config.CheckOption = new Class({
271 Extends: qwebirc.config.Option,
272 Element: qwebirc.config.CheckInput
273});
274
889ecb33
CP
275qwebirc.config.HueOption = new Class({
276 Extends: qwebirc.config.Option,
277 Element: qwebirc.config.HueInput
278});
279
ebb21d2e 280qwebirc.ui.Options = new Class({
fb71087a 281 initialize: function(ui) {
ebb21d2e
CP
282 if(!$defined(qwebirc.config.DefaultOptions))
283 this.__configureDefaults();
284
c38a0240
CP
285 this.optionList = qwebirc.config.DefaultOptions.slice();
286 this.optionHash = {}
fb71087a 287 this.ui = ui;
c38a0240
CP
288
289 this._setup();
290 this.optionList.forEach(function(x) {
291 x.setSavedValue(this._get(x));
292 this.optionHash[x.prefix] = x;
293 this[x.prefix] = x.value;
ebb21d2e
CP
294 }.bind(this));
295 },
296 __configureDefaults: function() {
c38a0240
CP
297 qwebirc.config.DefaultOptions = qwebirc.config.DEFAULT_OPTIONS.map(function(x) {
298 var optionId = x[0];
299 var prefix = x[1];
300 var label = x[2];
301 var default_ = x[3];
fb71087a
CP
302 var moreextras = x[4];
303 var extras = x[5];
ebb21d2e 304
c38a0240
CP
305 var stype = typeof(default_);
306 if(stype == "number") {
fb71087a 307 return new qwebirc.config.RadioOption(optionId, prefix, label, default_, moreextras, extra);
ebb21d2e 308 } else {
c38a0240
CP
309 var type;
310 if(stype == "boolean") {
311 type = qwebirc.config.CheckOption;
b076b3ab 312 } else if(stype == "function") {
889ecb33
CP
313 var options = default_();
314 type = options.class_;
b076b3ab
CP
315 default_ = options.default_;
316 } else {
317 type = qwebirc.config.TextOption;
c38a0240 318 }
fb71087a 319 return new type(optionId, prefix, label, default_, moreextras);
ebb21d2e 320 }
ebb21d2e 321 });
c38a0240
CP
322 },
323 setValue: function(option, value) {
324 this.optionHash[option.prefix].value = value;
325 this[option.prefix] = value;
326 },
327 getOptionList: function() {
328 return this.optionList;
329 },
330 _get: function(x) {
331 return x.default_;
332 },
333 _setup: function() {
334 },
335 flush: function() {
ebb21d2e
CP
336 }
337});
338
339qwebirc.ui.OptionsPane = new Class({
c38a0240
CP
340 Implements: [Events],
341 initialize: function(parentElement, optionObject) {
ebb21d2e 342 this.parentElement = parentElement;
c38a0240 343 this.optionObject = optionObject;
ebb21d2e
CP
344
345 this.createElements();
346 },
347 createElements: function() {
348 var FE = function(element, parent) {
349 var n = new Element(element);
350 parent.appendChild(n);
351 return n;
352 };
ebb21d2e
CP
353
354 var t = FE("table", this.parentElement);
355 var tb = FE("tbody", t);
356
c38a0240
CP
357 this.boxList = [];
358
359 var optList = this.optionObject.getOptionList();
360 for(var i=0;i<optList.length;i++) {
361 var x = optList[i];
362
ebb21d2e
CP
363 var row = FE("tr", tb);
364 var cella = FE("td", row);
c38a0240
CP
365 cella.set("text", x.label + ":");
366
ebb21d2e 367 var cellb = FE("td", row);
fb71087a 368 this.boxList.push([x, new x.Element(cellb, x, i, this)]);
c38a0240 369
c38a0240
CP
370 }
371
372 var r = FE("tr", tb);
373 var cella = FE("td", r);
374 var cellb = FE("td", r);
a1e826c7 375 var save = qwebirc.util.createInput("submit", cellb);
c38a0240
CP
376 save.value = "Save";
377
378 save.addEvent("click", function() {
379 this.save();
380 this.fireEvent("close");
381 }.bind(this));
382
a1e826c7 383 var cancel = qwebirc.util.createInput("submit", cellb);
c38a0240
CP
384 cancel.value = "Cancel";
385 cancel.addEvent("click", function() {
889ecb33 386 this.cancel();
c38a0240
CP
387 this.fireEvent("close");
388 }.bind(this));
389 },
390 save: function() {
391 this.boxList.forEach(function(x) {
392 var option = x[0];
393 var box = x[1];
394 this.optionObject.setValue(option, box.get());
ebb21d2e 395 }.bind(this));
c38a0240 396 this.optionObject.flush();
889ecb33
CP
397 },
398 cancel: function() {
399 this.boxList.forEach(function(x) {
400 x[1].cancel();
401 }.bind(this));
ebb21d2e
CP
402 }
403});
c38a0240
CP
404
405qwebirc.ui.CookieOptions = new Class({
406 Extends: qwebirc.ui.Options,
407 _setup: function() {
408 this.__cookie = new Hash.Cookie("opt1", {duration: 3650, autoSave: false});
409 },
410 _get: function(x) {
411 var v = this.__cookie.get(x.optionId);
412 if(!$defined(v))
413 return x.default_;
414
415 return v;
416 },
417 flush: function() {
418 this.__cookie.erase();
419 this._setup();
420
421 this.getOptionList().forEach(function(x) {
422 this.__cookie.set(x.optionId, x.value);
423 }.bind(this));
424 this.__cookie.save();
425 }
426});
427
c0f2f367
CP
428qwebirc.ui.SuppliedArgOptions = new Class({
429 Extends: qwebirc.ui.CookieOptions,
430 initialize: function(ui, arg) {
431 var p = {};
432
433 if($defined(arg) && arg != "") {
434 var decoded = qwebirc.util.b64Decode(arg);
435 if(decoded)
436 p = qwebirc.util.parseURI("?" + decoded);
437 }
438
439 this.parsedOptions = p;
440 this.parent(ui);
441 },
442 _get: function(x) {
443 if(x.settableByURL !== true)
444 return this.parent(x);
445
446 var opt = this.parsedOptions[x.optionId];
447 if(!$defined(opt))
448 return this.parent(x);
449
450 return opt;
451 },
452 serialise: function() {
453 var result = [];
454 this.getOptionList().forEach(function(x) {
455 if(x.settableByURL && x.default_ != x.value)
456 result.push(x.optionId + "=" + x.value);
457 }.bind(this));
458
459 return qwebirc.util.b64Encode(result.join("&")).replaceAll("=", "");
460 }
461});
462
c38a0240 463qwebirc.ui.DefaultOptionsClass = new Class({
c0f2f367 464 Extends: qwebirc.ui.SuppliedArgOptions
c38a0240 465});