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