]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/panes/options.js
Some tweaks to wording in embedded wizard.
[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 ];
53
54 qwebirc.config.DefaultOptions = null;
55
56 qwebirc.config.Input = new Class({
57 initialize: function(parent, option, position, parentObject) {
58 this.option = option;
59 this.value = option.value;
60 this.enabled = this.option.enabled;
61 this.position = position;
62 this.parentElement = parent;
63 this.parentObject = parentObject;
64
65 this.render();
66 },
67 createInput: function(type, parent, name, selected, id) {
68 if(!$defined(parent))
69 parent = this.parentElement;
70
71 return qwebirc.util.createInput(type, parent, name, selected, this.option.id);
72 },
73 FE: function(element, parent) {
74 var n = new Element(element);
75 if(!$defined(parent))
76 parent = this.parentElement;
77
78 parent.appendChild(n);
79 return n;
80 },
81 focus: function() {
82 this.mainElement.focus();
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)();
99 },
100 cancel: function() {
101 }
102 });
103
104 qwebirc.config.TextInput = new Class({
105 Extends: qwebirc.config.Input,
106 render: function() {
107 var i = this.createInput("text");
108 this.mainElement = i;
109
110 i.value = this.value;
111 i.disabled = !this.enabled;
112
113 this.parent();
114 },
115 get: function() {
116 return this.parent(this.mainElement.value);
117 }
118 });
119
120 qwebirc.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");
126 this.parentElement.appendChild(i);
127
128 var k = new Element("div");
129 k.addClass("knob");
130 if(Browser.Engine.trident) {
131 k.setStyle("top", "0px");
132 k.setStyle("background-color", "black");
133 }
134
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
161 qwebirc.config.CheckInput = new Class({
162 Extends: qwebirc.config.Input,
163 render: function() {
164 var i = this.createInput("checkbox", null, null, null, this.id);
165 this.mainElement = i;
166
167 i.checked = this.value;
168 i.disabled = !this.enabled;
169
170 this.parent();
171 },
172 get: function() {
173 return this.parent(this.mainElement.checked);
174 }
175 });
176
177 qwebirc.config.RadioInput = new Class({
178 Extends: qwebirc.config.Input,
179 render: function() {
180 var value = this.option.options;
181
182 this.elements = [];
183
184 for(var i=0;i<value.length;i++) {
185 var d = this.FE("div", this.parentObject);
186 var e = this.createInput("radio", d, "options_radio" + this.position, i == this.option.position);
187 this.elements.push(e);
188 e.disabled = !this.enabled;
189
190 if(i == 0)
191 this.mainElement = e;
192
193 d.appendChild(document.createTextNode(value[i][0]));
194 };
195 this.parent();
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;
202 return this.parent(this.option.options[i][1]);
203 }
204 }
205 }
206 });
207
208 qwebirc.config.Option = new Class({
209 initialize: function(optionId, prefix, label, default_, extras) {
210 this.prefix = prefix;
211 this.label = label;
212 this.default_ = default_;
213 this.optionId = optionId;
214 this.extras = extras;
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;
224 }
225
226 if($defined(extras) && $defined(extras.settableByURL)) {
227 this.settableByURL = extras.settableByURL;
228 } else {
229 this.settableByURL = true;
230 }
231 },
232 setSavedValue: function(x) {
233 if(this.enabled)
234 this.value = x;
235 }
236 });
237
238 qwebirc.config.RadioOption = new Class({
239 Extends: qwebirc.config.Option,
240 Element: qwebirc.config.RadioInput,
241 initialize: function(optionId, prefix, label, default_, extras, options) {
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
249 this.parent(optionId, prefix, label, this.options[default_][1], extras);
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
265 qwebirc.config.TextOption = new Class({
266 Extends: qwebirc.config.Option,
267 Element: qwebirc.config.TextInput
268 });
269
270 qwebirc.config.CheckOption = new Class({
271 Extends: qwebirc.config.Option,
272 Element: qwebirc.config.CheckInput
273 });
274
275 qwebirc.config.HueOption = new Class({
276 Extends: qwebirc.config.Option,
277 Element: qwebirc.config.HueInput
278 });
279
280 qwebirc.ui.Options = new Class({
281 initialize: function(ui) {
282 if(!$defined(qwebirc.config.DefaultOptions))
283 this.__configureDefaults();
284
285 this.optionList = qwebirc.config.DefaultOptions.slice();
286 this.optionHash = {}
287 this.ui = ui;
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;
294 }.bind(this));
295 },
296 __configureDefaults: function() {
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];
302 var moreextras = x[4];
303 var extras = x[5];
304
305 var stype = typeof(default_);
306 if(stype == "number") {
307 return new qwebirc.config.RadioOption(optionId, prefix, label, default_, moreextras, extra);
308 } else {
309 var type;
310 if(stype == "boolean") {
311 type = qwebirc.config.CheckOption;
312 } else if(stype == "function") {
313 var options = default_();
314 type = options.class_;
315 default_ = options.default_;
316 } else {
317 type = qwebirc.config.TextOption;
318 }
319 return new type(optionId, prefix, label, default_, moreextras);
320 }
321 });
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() {
336 }
337 });
338
339 qwebirc.ui.OptionsPane = new Class({
340 Implements: [Events],
341 initialize: function(parentElement, optionObject) {
342 this.parentElement = parentElement;
343 this.optionObject = optionObject;
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 };
353
354 var t = FE("table", this.parentElement);
355 var tb = FE("tbody", t);
356
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
363 var row = FE("tr", tb);
364 var cella = FE("td", row);
365
366 x.id = qwebirc.util.generateID();
367 var label = new Element("label", {"for": x.id});
368 cella.appendChild(label);
369 label.set("text", x.label + ":");
370
371 var cellb = FE("td", row);
372 this.boxList.push([x, new x.Element(cellb, x, i, this)]);
373
374 }
375
376 var r = FE("tr", tb);
377 var cella = FE("td", r);
378 var cellb = FE("td", r);
379 var save = qwebirc.util.createInput("submit", cellb);
380 save.value = "Save";
381
382 save.addEvent("click", function() {
383 this.save();
384 this.fireEvent("close");
385 }.bind(this));
386
387 var cancel = qwebirc.util.createInput("submit", cellb);
388 cancel.value = "Cancel";
389 cancel.addEvent("click", function() {
390 this.cancel();
391 this.fireEvent("close");
392 }.bind(this));
393 },
394 save: function() {
395 this.boxList.forEach(function(x) {
396 var option = x[0];
397 var box = x[1];
398 this.optionObject.setValue(option, box.get());
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 = {};
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 p = qwebirc.util.parseURI("?" + decoded);
443 }
444
445 this.parsedOptions = p;
446 this.parent(ui);
447 },
448 _get: function(x) {
449 if(x.settableByURL !== true)
450 return this.parent(x);
451
452 var opt = this.parsedOptions[x.optionId];
453 if(!$defined(opt))
454 return this.parent(x);
455
456 return opt;
457 },
458 serialise: function() {
459 var result = [];
460 this.getOptionList().forEach(function(x) {
461 if(x.settableByURL && x.default_ != x.value)
462 result.push(x.optionId + "=" + x.value);
463 }.bind(this));
464
465 var raw = result.join("&");
466 var checksum = new qwebirc.util.crypto.MD5().digest(raw).slice(0, 2);
467 return (qwebirc.util.b64Encode(raw)).replaceAll("=", "") + checksum;
468 }
469 });
470
471 qwebirc.ui.DefaultOptionsClass = new Class({
472 Extends: qwebirc.ui.SuppliedArgOptions
473 });