]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/panes/options.js
Add nicklist hiding feature.
[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 (requires Flash)", true, {
18 enabled: function() {
19 if(!$defined(Browser.Plugins.Flash) || Browser.Plugins.Flash.version < 8)
20 return [false, false]; /* [disabled, default_value] */
21 return [true];
22 },
23 get: function(value, ui) {
24 if(ui.setBeepOnMention)
25 ui.setBeepOnMention(value);
26 }
27 }],
28 [7, "FLASH_ON_MENTION", "Flash titlebar when nick mentioned or on query activity", true, {
29 enabled: qwebirc.ui.supportsFocus
30 }],
31 [2, "DEDICATED_MSG_WINDOW", "Send privmsgs to dedicated messages window", false],
32 [4, "DEDICATED_NOTICE_WINDOW", "Send notices to dedicated message window", false],
33 [3, "NICK_OV_STATUS", "Show status (@/+) before nicknames in channel lines", true],
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 }],
40 [8, "LASTPOS_LINE", "Show a last position indicator for each window", true, {
41 enabled: qwebirc.ui.supportsFocus
42 }],
43 [9, "NICK_COLOURS", "Automatically colour nicknames", false],
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 }],
52 [12, "QUERY_ON_NICK_CLICK", "Query on nickname click in channel", false],
53 [13, "SHOW_NICKLIST", "Show nickname list in channels", true]
54 ];
55
56 qwebirc.config.DefaultOptions = null;
57
58 qwebirc.config.Input = new Class({
59 initialize: function(parent, option, position, parentObject) {
60 this.option = option;
61 this.value = option.value;
62 this.enabled = this.option.enabled;
63 this.position = position;
64 this.parentElement = parent;
65 this.parentObject = parentObject;
66
67 this.render();
68 },
69 createInput: function(type, parent, name, selected, id) {
70 if(!$defined(parent))
71 parent = this.parentElement;
72
73 return qwebirc.util.createInput(type, parent, name, selected, this.option.id);
74 },
75 FE: function(element, parent) {
76 var n = new Element(element);
77 if(!$defined(parent))
78 parent = this.parentElement;
79
80 parent.appendChild(n);
81 return n;
82 },
83 focus: function() {
84 this.mainElement.focus();
85 },
86 render: function() {
87 this.event("render", this.mainElement);
88 },
89 get: function(value) {
90 this.event("get", [value, this.parentObject.optionObject.ui]);
91 return value;
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)();
101 },
102 cancel: function() {
103 }
104 });
105
106 qwebirc.config.TextInput = new Class({
107 Extends: qwebirc.config.Input,
108 render: function() {
109 var i = this.createInput("text");
110 this.mainElement = i;
111
112 i.value = this.value;
113 i.disabled = !this.enabled;
114
115 this.parent();
116 },
117 get: function() {
118 return this.parent(this.mainElement.value);
119 }
120 });
121
122 qwebirc.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");
128 this.parentElement.appendChild(i);
129
130 var k = new Element("div");
131 k.addClass("knob");
132 if(Browser.Engine.trident) {
133 k.setStyle("top", "0px");
134 k.setStyle("background-color", "black");
135 }
136
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;
145 this.get();
146 }.bind(this));
147 this.mainElement = i;
148
149 if(!this.enabled)
150 slider.detach();
151
152 this.parent();
153 },
154 get: function() {
155 return this.parent(this.value);
156 },
157 cancel: function() {
158 this.value = this.startValue;
159 this.get();
160 }
161 });
162
163 qwebirc.config.CheckInput = new Class({
164 Extends: qwebirc.config.Input,
165 render: function() {
166 var i = this.createInput("checkbox", null, null, null, this.id);
167 this.mainElement = i;
168
169 i.checked = this.value;
170 i.disabled = !this.enabled;
171
172 this.parent();
173 },
174 get: function() {
175 return this.parent(this.mainElement.checked);
176 }
177 });
178
179 qwebirc.config.RadioInput = new Class({
180 Extends: qwebirc.config.Input,
181 render: function() {
182 var value = this.option.options;
183
184 this.elements = [];
185
186 for(var i=0;i<value.length;i++) {
187 var d = this.FE("div", this.parentObject);
188 var e = this.createInput("radio", d, "options_radio" + this.position, i == this.option.position);
189 this.elements.push(e);
190 e.disabled = !this.enabled;
191
192 if(i == 0)
193 this.mainElement = e;
194
195 d.appendChild(document.createTextNode(value[i][0]));
196 };
197 this.parent();
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;
204 return this.parent(this.option.options[i][1]);
205 }
206 }
207 }
208 });
209
210 qwebirc.config.Option = new Class({
211 initialize: function(optionId, prefix, label, default_, extras) {
212 this.prefix = prefix;
213 this.label = label;
214 this.default_ = default_;
215 this.optionId = optionId;
216 this.extras = extras;
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;
226 }
227
228 if($defined(extras) && $defined(extras.settableByURL)) {
229 this.settableByURL = extras.settableByURL;
230 } else {
231 this.settableByURL = true;
232 }
233 },
234 setSavedValue: function(x) {
235 if(this.enabled)
236 this.value = x;
237 }
238 });
239
240 qwebirc.config.RadioOption = new Class({
241 Extends: qwebirc.config.Option,
242 Element: qwebirc.config.RadioInput,
243 initialize: function(optionId, prefix, label, default_, extras, options) {
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
251 this.parent(optionId, prefix, label, this.options[default_][1], extras);
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
267 qwebirc.config.TextOption = new Class({
268 Extends: qwebirc.config.Option,
269 Element: qwebirc.config.TextInput
270 });
271
272 qwebirc.config.CheckOption = new Class({
273 Extends: qwebirc.config.Option,
274 Element: qwebirc.config.CheckInput
275 });
276
277 qwebirc.config.HueOption = new Class({
278 Extends: qwebirc.config.Option,
279 Element: qwebirc.config.HueInput
280 });
281
282 qwebirc.ui.Options = new Class({
283 initialize: function(ui) {
284 if(!$defined(qwebirc.config.DefaultOptions))
285 this.__configureDefaults();
286
287 this.optionList = qwebirc.config.DefaultOptions.slice();
288 this.optionHash = {}
289 this.ui = ui;
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;
296 }.bind(this));
297 },
298 __configureDefaults: function() {
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];
304 var moreextras = x[4];
305 var extras = x[5];
306
307 var stype = typeof(default_);
308 if(stype == "number") {
309 return new qwebirc.config.RadioOption(optionId, prefix, label, default_, moreextras, extra);
310 } else {
311 var type;
312 if(stype == "boolean") {
313 type = qwebirc.config.CheckOption;
314 } else if(stype == "function") {
315 var options = default_();
316 type = options.class_;
317 default_ = options.default_;
318 } else {
319 type = qwebirc.config.TextOption;
320 }
321 return new type(optionId, prefix, label, default_, moreextras);
322 }
323 });
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() {
338 }
339 });
340
341 qwebirc.ui.OptionsPane = new Class({
342 Implements: [Events],
343 initialize: function(parentElement, optionObject) {
344 this.parentElement = parentElement;
345 this.optionObject = optionObject;
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 };
355
356 var t = FE("table", this.parentElement);
357 var tb = FE("tbody", t);
358
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
365 var row = FE("tr", tb);
366 var cella = FE("td", row);
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 + ":");
372
373 var cellb = FE("td", row);
374 this.boxList.push([x, new x.Element(cellb, x, i, this)]);
375
376 }
377
378 var r = FE("tr", tb);
379 var cella = FE("td", r);
380 var cellb = FE("td", r);
381 var save = qwebirc.util.createInput("submit", cellb);
382 save.value = "Save";
383
384 save.addEvent("click", function() {
385 this.save();
386 this.fireEvent("close");
387 }.bind(this));
388
389 var cancel = qwebirc.util.createInput("submit", cellb);
390 cancel.value = "Cancel";
391 cancel.addEvent("click", function() {
392 this.cancel();
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());
401 }.bind(this));
402 this.optionObject.flush();
403 },
404 cancel: function() {
405 this.boxList.forEach(function(x) {
406 x[1].cancel();
407 }.bind(this));
408 }
409 });
410
411 qwebirc.ui.CookieOptions = new Class({
412 Extends: qwebirc.ui.Options,
413 _setup: function() {
414 this.__cookie = new Hash.Cookie("opt1", {duration: 3650, autoSave: false});
415 },
416 _get: function(x) {
417 var v = this.__cookie.get(x.optionId);
418 if(!$defined(v))
419 return x.default_;
420
421 return v;
422 },
423 flush: function() {
424 this.__cookie.erase();
425 this._setup();
426
427 this.getOptionList().forEach(function(x) {
428 this.__cookie.set(x.optionId, x.value);
429 }.bind(this));
430 this.__cookie.save();
431 }
432 });
433
434 qwebirc.ui.SuppliedArgOptions = new Class({
435 Extends: qwebirc.ui.CookieOptions,
436 initialize: function(ui, arg) {
437 var p = {};
438
439 if($defined(arg) && arg != "" && arg.length > 2) {
440 var checksum = arg.substr(arg.length - 2, 2);
441 var decoded = qwebirc.util.b64Decode(arg.substr(0, arg.length - 2));
442
443 if(decoded && (new qwebirc.util.crypto.MD5().digest(decoded).slice(0, 2) == checksum))
444 p = qwebirc.util.parseURI("?" + decoded);
445 }
446
447 this.parsedOptions = p;
448 this.parent(ui);
449 },
450 _get: function(x) {
451 if(x.settableByURL !== true)
452 return this.parent(x);
453
454 var opt = this.parsedOptions[x.optionId];
455 if(!$defined(opt))
456 return this.parent(x);
457
458 return opt;
459 },
460 serialise: function() {
461 var result = [];
462 this.getOptionList().forEach(function(x) {
463 if(x.settableByURL && x.default_ != x.value)
464 result.push(x.optionId + "=" + x.value);
465 }.bind(this));
466
467 var raw = result.join("&");
468 var checksum = new qwebirc.util.crypto.MD5().digest(raw).slice(0, 2);
469 return (qwebirc.util.b64Encode(raw)).replaceAll("=", "") + checksum;
470 }
471 });
472
473 qwebirc.ui.DefaultOptionsClass = new Class({
474 Extends: qwebirc.ui.SuppliedArgOptions
475 });