]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/panes/options.js
add side tabs
[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 when nick mentioned or on query activity", true, {
18 applyChanges: function(value, ui) {
19 if(ui.setBeepOnMention)
20 ui.setBeepOnMention(value);
21 }
22 }],
23 [7, "FLASH_ON_MENTION", "Flash titlebar when nick mentioned or on query activity", true, {
24 enabled: qwebirc.ui.supportsFocus
25 }],
26 [2, "DEDICATED_MSG_WINDOW", "Send privmsgs to dedicated messages window", false],
27 [4, "DEDICATED_NOTICE_WINDOW", "Send notices to dedicated message window", false],
28 [3, "NICK_OV_STATUS", "Show status (@/+) before nicknames in channel lines", true],
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 }],
35 [8, "LASTPOS_LINE", "Show a last position indicator for each window", true, {
36 enabled: qwebirc.ui.supportsFocus
37 }],
38 [9, "NICK_COLOURS", "Automatically colour nicknames", false],
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 }, {
43 applyChanges: function(value, ui) {
44 ui.setModifiableStylesheetValues({hue: value});
45 }
46 }],
47 [12, "QUERY_ON_NICK_CLICK", "Query on nickname click in channel", false],
48 [13, "SHOW_NICKLIST", "Show nickname list in channels", true],
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 }]
60 ];
61
62 qwebirc.config.DefaultOptions = null;
63
64 qwebirc.config.Input = new Class({
65 initialize: function(parent, option, position, parentObject) {
66 this.option = option;
67 this.value = option.value;
68 this.enabled = this.option.enabled;
69 this.position = position;
70 this.parentElement = parent;
71 this.parentObject = parentObject;
72
73 this.render();
74 },
75 createInput: function(type, parent, name, selected, id) {
76 if(!$defined(parent))
77 parent = this.parentElement;
78
79 return qwebirc.util.createInput(type, parent, name, selected, this.option.id);
80 },
81 FE: function(element, parent) {
82 var n = new Element(element);
83 if(!$defined(parent))
84 parent = this.parentElement;
85
86 parent.appendChild(n);
87 return n;
88 },
89 focus: function() {
90 this.mainElement.focus();
91 },
92 render: function() {
93 this.event("render", this.mainElement);
94 },
95 applyChanges: function() {
96 this.event("applyChanges", [this.get(), this.parentObject.optionObject.ui]);
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)();
106 },
107 cancel: function() {
108 }
109 });
110
111 qwebirc.config.TextInput = new Class({
112 Extends: qwebirc.config.Input,
113 render: function() {
114 var i = this.createInput("text");
115 this.mainElement = i;
116
117 i.value = this.value;
118 i.disabled = !this.enabled;
119
120 this.parent();
121 },
122 get: function() {
123 return this.mainElement.value;
124 }
125 });
126
127 qwebirc.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");
133 this.parentElement.appendChild(i);
134
135 var k = new Element("div");
136 k.addClass("knob");
137 if(Browser.Engine.trident) {
138 k.setStyle("top", "0px");
139 k.setStyle("background-color", "black");
140 }
141
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;
150 this.applyChanges();
151 }.bind(this));
152 this.mainElement = i;
153
154 if(!this.enabled)
155 slider.detach();
156
157 this.parent();
158 },
159 get: function() {
160 return this.value;
161 },
162 cancel: function() {
163 this.value = this.startValue;
164 this.applyChanges();
165 }
166 });
167
168 qwebirc.config.CheckInput = new Class({
169 Extends: qwebirc.config.Input,
170 render: function() {
171 var i = this.createInput("checkbox", null, null, null, this.id);
172 this.mainElement = i;
173
174 i.checked = this.value;
175 i.disabled = !this.enabled;
176
177 this.parent();
178 },
179 get: function() {
180 return this.mainElement.checked;
181 }
182 });
183
184 qwebirc.config.RadioInput = new Class({
185 Extends: qwebirc.config.Input,
186 render: function() {
187 var value = this.option.options;
188
189 this.elements = [];
190
191 for(var i=0;i<value.length;i++) {
192 var d = this.FE("div", this.parentObject);
193 var e = this.createInput("radio", d, "options_radio" + this.position, i == this.option.position);
194 this.elements.push(e);
195 e.disabled = !this.enabled;
196
197 if(i == 0)
198 this.mainElement = e;
199
200 d.appendChild(document.createTextNode(value[i][0]));
201 };
202 this.parent();
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;
209 return this.option.options[i][1];
210 }
211 }
212 }
213 });
214
215 qwebirc.config.Option = new Class({
216 initialize: function(optionId, prefix, label, default_, extras) {
217 this.prefix = prefix;
218 this.label = label;
219 this.default_ = default_;
220 this.optionId = optionId;
221 this.extras = extras;
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;
231 }
232
233 if($defined(extras) && $defined(extras.settableByURL)) {
234 this.settableByURL = extras.settableByURL;
235 } else {
236 this.settableByURL = true;
237 }
238 },
239 setSavedValue: function(x) {
240 if(this.enabled)
241 this.value = x;
242 }
243 });
244
245 qwebirc.config.RadioOption = new Class({
246 Extends: qwebirc.config.Option,
247 Element: qwebirc.config.RadioInput,
248 initialize: function(optionId, prefix, label, default_, extras, options) {
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
256 this.parent(optionId, prefix, label, this.options[default_][1], extras);
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
272 qwebirc.config.TextOption = new Class({
273 Extends: qwebirc.config.Option,
274 Element: qwebirc.config.TextInput
275 });
276
277 qwebirc.config.CheckOption = new Class({
278 Extends: qwebirc.config.Option,
279 Element: qwebirc.config.CheckInput
280 });
281
282 qwebirc.config.HueOption = new Class({
283 Extends: qwebirc.config.Option,
284 Element: qwebirc.config.HueInput
285 });
286
287 qwebirc.ui.Options = new Class({
288 initialize: function(ui) {
289 if(!$defined(qwebirc.config.DefaultOptions))
290 this.__configureDefaults();
291
292 this.optionList = qwebirc.config.DefaultOptions.slice();
293 this.optionHash = {}
294 this.ui = ui;
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;
301 }.bind(this));
302 },
303 __configureDefaults: function() {
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];
309 var moreextras = x[4];
310 var extras = x[5];
311
312 var stype = typeof(default_);
313 if(stype == "number") {
314 return new qwebirc.config.RadioOption(optionId, prefix, label, default_, moreextras, extra);
315 } else {
316 var type;
317 if(stype == "boolean") {
318 type = qwebirc.config.CheckOption;
319 } else if(stype == "function") {
320 var options = default_();
321 type = options.class_;
322 default_ = options.default_;
323 } else {
324 type = qwebirc.config.TextOption;
325 }
326 return new type(optionId, prefix, label, default_, moreextras);
327 }
328 });
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() {
343 }
344 });
345
346 qwebirc.ui.OptionsPane = new Class({
347 Implements: [Events],
348 initialize: function(parentElement, optionObject) {
349 this.parentElement = parentElement;
350 this.optionObject = optionObject;
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 };
360
361 var t = FE("table", this.parentElement);
362 var tb = FE("tbody", t);
363
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
370 var row = FE("tr", tb);
371 var cella = FE("td", row);
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 + ":");
377
378 var cellb = FE("td", row);
379 this.boxList.push([x, new x.Element(cellb, x, i, this)]);
380
381 }
382
383 var r = FE("tr", tb);
384 var cella = FE("td", r);
385 var cellb = FE("td", r);
386 var save = qwebirc.util.createInput("submit", cellb);
387 save.value = "Save";
388
389 save.addEvent("click", function() {
390 this.save();
391 this.fireEvent("close");
392 }.bind(this));
393
394 var cancel = qwebirc.util.createInput("submit", cellb);
395 cancel.value = "Cancel";
396 cancel.addEvent("click", function() {
397 this.cancel();
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());
406 }.bind(this));
407 this.boxList.forEach(function(x) {
408 x[1].applyChanges();
409 }.bind(this));
410 this.optionObject.flush();
411 },
412 cancel: function() {
413 this.boxList.forEach(function(x) {
414 x[1].cancel();
415 }.bind(this));
416 }
417 });
418
419 qwebirc.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
442 qwebirc.ui.SuppliedArgOptions = new Class({
443 Extends: qwebirc.ui.CookieOptions,
444 initialize: function(ui, arg) {
445 var p = new QHash();
446
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
451 if(decoded && (new qwebirc.util.crypto.MD5().digest(decoded).slice(0, 2) == checksum)) {
452 var p2 = qwebirc.util.parseURI("?" + decoded);
453 p2.each(function(k, v) {
454 p.put(k, JSON.decode(v, true));
455 });
456 }
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
466 var opt = this.parsedOptions.get(String(x.optionId));
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)
476 result.push(x.optionId + "=" + JSON.encode(x.value));
477 }.bind(this));
478
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;
482 }
483 });
484
485 qwebirc.ui.DefaultOptionsClass = new Class({
486 Extends: qwebirc.ui.SuppliedArgOptions
487 });