]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/panes/options.js
251b01d60ef0a4c8eb395111755289717f9cfc3a
[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 qwebirc.config.DEFAULT_OPTIONS = [
13 [1, "BEEP_ON_MENTION", "Beep when nick mentioned or on query activity (requires Flash)", true, {
14 enabled: function() {
15 if(!$defined(Browser.Plugins.Flash) || Browser.Plugins.Flash.version < 8)
16 return [false, false]; /* [disabled, default_value] */
17 return [true];
18 },
19 get: function(value, ui) {
20 if(ui.setBeepOnMention)
21 ui.setBeepOnMention(value);
22 }
23 }],
24 [7, "FLASH_ON_MENTION", "Flash titlebar when nick mentioned or on query activity", true, {
25 enabled: qwebirc.ui.supportsFocus
26 }],
27 [2, "DEDICATED_MSG_WINDOW", "Send privmsgs to dedicated messages window", false],
28 [4, "DEDICATED_NOTICE_WINDOW", "Send notices to dedicated message window", false],
29 [3, "NICK_OV_STATUS", "Show status (@/+) before nicknames in channel lines", true],
30 [5, "ACCEPT_SERVICE_INVITES", "Automatically join channels when invited by Q", true],
31 [6, "USE_HIDDENHOST", "Hide your hostmask when authed to Q (+x)", true],
32 [8, "LASTPOS_LINE", "Show a last position indicator for each window", true, {
33 enabled: qwebirc.ui.supportsFocus
34 }],
35 [9, "NICK_COLOURS", "Automatically colour nicknames", false],
36 [10, "HIDE_JOINPARTS", "Hide JOINS/PARTS/QUITS", false],
37 [11, "STYLE_HUE", "Adjust user interface hue", function() {
38 return {class_: qwebirc.config.HueOption, default_: 210};
39 }, {
40 get: function(value, ui) {
41 ui.setModifiableStylesheetValues(value, 0, 0);
42 }
43 }]
44 ];
45
46 qwebirc.config.DefaultOptions = null;
47
48 qwebirc.config.Input = new Class({
49 initialize: function(parent, option, position, parentObject) {
50 this.option = option;
51 this.value = option.value;
52 this.enabled = this.option.enabled;
53 this.position = position;
54 this.parentElement = parent;
55 this.parentObject = parentObject;
56
57 this.render();
58 },
59 createInput: function(type, parent, name, selected) {
60 if(!$defined(parent))
61 parent = this.parentElement;
62
63 return qwebirc.util.createInput(type, parent, name, selected);
64 },
65 FE: function(element, parent) {
66 var n = new Element(element);
67 if(!$defined(parent))
68 parent = this.parentElement;
69
70 parent.appendChild(n);
71 return n;
72 },
73 focus: function() {
74 this.mainElement.focus();
75 },
76 render: function() {
77 this.event("render", this.mainElement);
78 },
79 get: function(value) {
80 this.event("get", [value, this.parentObject.optionObject.ui]);
81 return value;
82 },
83 event: function(name, x) {
84 if(!$defined(this.option.extras))
85 return;
86 var t = this.option.extras[name];
87 if(!$defined(t))
88 return;
89
90 t.pass(x, this)();
91 },
92 cancel: function() {
93 }
94 });
95
96 qwebirc.config.TextInput = new Class({
97 Extends: qwebirc.config.Input,
98 render: function() {
99 var i = this.createInput("text");
100 this.mainElement = i;
101
102 i.value = this.value;
103 i.disabled = !this.enabled;
104
105 this.parent();
106 },
107 get: function() {
108 return this.parent(this.mainElement.value);
109 }
110 });
111
112 qwebirc.config.HueInput = new Class({
113 Extends: qwebirc.config.Input,
114 render: function() {
115 var i = new Element("div");
116 i.addClass("qwebirc-optionspane");
117 i.addClass("hue-slider");
118 this.parentElement.appendChild(i);
119
120 var k = new Element("div");
121 k.addClass("knob");
122 if(Browser.Engine.trident) {
123 k.setStyle("top", "0px");
124 k.setStyle("background-color", "black");
125 }
126
127 i.appendChild(k);
128
129 var slider = new Slider(i, k, {steps: 36, range: [0, 369], wheel: true});
130 slider.set(this.value);
131 this.startValue = this.value;
132
133 slider.addEvent("change", function(step) {
134 this.value = step;
135 this.get();
136 }.bind(this));
137 this.mainElement = i;
138
139 if(!this.enabled)
140 slider.detach();
141
142 this.parent();
143 },
144 get: function() {
145 return this.parent(this.value);
146 },
147 cancel: function() {
148 this.value = this.startValue;
149 this.get();
150 }
151 });
152
153 qwebirc.config.CheckInput = new Class({
154 Extends: qwebirc.config.Input,
155 render: function() {
156 var i = this.createInput("checkbox");
157 this.mainElement = i;
158
159 i.checked = this.value;
160 i.disabled = !this.enabled;
161
162 this.parent();
163 },
164 get: function() {
165 return this.parent(this.mainElement.checked);
166 }
167 });
168
169 qwebirc.config.RadioInput = new Class({
170 Extends: qwebirc.config.Input,
171 render: function() {
172 var value = this.option.options;
173
174 this.elements = [];
175
176 for(var i=0;i<value.length;i++) {
177 var d = this.FE("div", this.parentObject);
178 var e = this.createInput("radio", d, "options_radio" + this.position, i == this.option.position);
179 this.elements.push(e);
180 e.disabled = !this.enabled;
181
182 if(i == 0)
183 this.mainElement = e;
184
185 d.appendChild(document.createTextNode(value[i][0]));
186 };
187 this.parent();
188 },
189 get: function() {
190 for(var i=0;i<this.elements.length;i++) {
191 var x = this.elements[i];
192 if(x.checked) {
193 this.option.position = i;
194 return this.parent(this.option.options[i][1]);
195 }
196 }
197 }
198 });
199
200 qwebirc.config.Option = new Class({
201 initialize: function(optionId, prefix, label, default_, extras) {
202 this.prefix = prefix;
203 this.label = label;
204 this.default_ = default_;
205 this.optionId = optionId;
206 this.extras = extras;
207
208 if($defined(extras) && $defined(extras.enabled)) {
209 var enabledResult = extras.enabled();
210 this.enabled = enabledResult[0];
211
212 if(!enabledResult[0] && enabledResult.length > 1)
213 this.default_ = enabledResult[1];
214 } else {
215 this.enabled = true;
216 }
217 },
218 setSavedValue: function(x) {
219 if(this.enabled)
220 this.value = x;
221 }
222 });
223
224 qwebirc.config.RadioOption = new Class({
225 Extends: qwebirc.config.Option,
226 Element: qwebirc.config.RadioInput,
227 initialize: function(optionId, prefix, label, default_, extras, options) {
228 this.options = options.map(function(x) {
229 if(typeof(x) == "string")
230 return [x, x];
231 return x;
232 });
233 this.defaultposition = default_;
234
235 this.parent(optionId, prefix, label, this.options[default_][1], extras);
236 },
237 setSavedValue: function(x) {
238 for(var i=0;i<this.options.length;i++) {
239 var y = this.options[i][1];
240 if(x == y) {
241 this.position = i;
242 this.value = x;
243 return;
244 }
245 }
246 this.position = this.defaultposition;
247 this.value = this.default_;
248 }
249 });
250
251 qwebirc.config.TextOption = new Class({
252 Extends: qwebirc.config.Option,
253 Element: qwebirc.config.TextInput
254 });
255
256 qwebirc.config.CheckOption = new Class({
257 Extends: qwebirc.config.Option,
258 Element: qwebirc.config.CheckInput
259 });
260
261 qwebirc.config.HueOption = new Class({
262 Extends: qwebirc.config.Option,
263 Element: qwebirc.config.HueInput
264 });
265
266 qwebirc.ui.Options = new Class({
267 initialize: function(ui) {
268 if(!$defined(qwebirc.config.DefaultOptions))
269 this.__configureDefaults();
270
271 this.optionList = qwebirc.config.DefaultOptions.slice();
272 this.optionHash = {}
273 this.ui = ui;
274
275 this._setup();
276 this.optionList.forEach(function(x) {
277 x.setSavedValue(this._get(x));
278 this.optionHash[x.prefix] = x;
279 this[x.prefix] = x.value;
280 }.bind(this));
281 },
282 __configureDefaults: function() {
283 qwebirc.config.DefaultOptions = qwebirc.config.DEFAULT_OPTIONS.map(function(x) {
284 var optionId = x[0];
285 var prefix = x[1];
286 var label = x[2];
287 var default_ = x[3];
288 var moreextras = x[4];
289 var extras = x[5];
290
291 var stype = typeof(default_);
292 if(stype == "number") {
293 return new qwebirc.config.RadioOption(optionId, prefix, label, default_, moreextras, extra);
294 } else {
295 var type;
296 if(stype == "boolean") {
297 type = qwebirc.config.CheckOption;
298 } else if(stype == "function") {
299 var options = default_();
300 type = options.class_;
301 default_ = options.default_;
302 } else {
303 type = qwebirc.config.TextOption;
304 }
305 return new type(optionId, prefix, label, default_, moreextras);
306 }
307 });
308 },
309 setValue: function(option, value) {
310 this.optionHash[option.prefix].value = value;
311 this[option.prefix] = value;
312 },
313 getOptionList: function() {
314 return this.optionList;
315 },
316 _get: function(x) {
317 return x.default_;
318 },
319 _setup: function() {
320 },
321 flush: function() {
322 }
323 });
324
325 qwebirc.ui.OptionsPane = new Class({
326 Implements: [Events],
327 initialize: function(parentElement, optionObject) {
328 this.parentElement = parentElement;
329 this.optionObject = optionObject;
330
331 this.createElements();
332 },
333 createElements: function() {
334 var FE = function(element, parent) {
335 var n = new Element(element);
336 parent.appendChild(n);
337 return n;
338 };
339
340 var t = FE("table", this.parentElement);
341 var tb = FE("tbody", t);
342
343 this.boxList = [];
344
345 var optList = this.optionObject.getOptionList();
346 for(var i=0;i<optList.length;i++) {
347 var x = optList[i];
348
349 var row = FE("tr", tb);
350 var cella = FE("td", row);
351 cella.set("text", x.label + ":");
352
353 var cellb = FE("td", row);
354 this.boxList.push([x, new x.Element(cellb, x, i, this)]);
355
356 }
357
358 var r = FE("tr", tb);
359 var cella = FE("td", r);
360 var cellb = FE("td", r);
361 var save = qwebirc.util.createInput("submit", cellb);
362 save.value = "Save";
363
364 save.addEvent("click", function() {
365 this.save();
366 this.fireEvent("close");
367 }.bind(this));
368
369 var cancel = qwebirc.util.createInput("submit", cellb);
370 cancel.value = "Cancel";
371 cancel.addEvent("click", function() {
372 this.cancel();
373 this.fireEvent("close");
374 }.bind(this));
375 },
376 save: function() {
377 this.boxList.forEach(function(x) {
378 var option = x[0];
379 var box = x[1];
380 this.optionObject.setValue(option, box.get());
381 }.bind(this));
382 this.optionObject.flush();
383 },
384 cancel: function() {
385 this.boxList.forEach(function(x) {
386 x[1].cancel();
387 }.bind(this));
388 }
389 });
390
391 qwebirc.ui.CookieOptions = new Class({
392 Extends: qwebirc.ui.Options,
393 _setup: function() {
394 this.__cookie = new Hash.Cookie("opt1", {duration: 3650, autoSave: false});
395 },
396 _get: function(x) {
397 var v = this.__cookie.get(x.optionId);
398 if(!$defined(v))
399 return x.default_;
400
401 return v;
402 },
403 flush: function() {
404 this.__cookie.erase();
405 this._setup();
406
407 this.getOptionList().forEach(function(x) {
408 this.__cookie.set(x.optionId, x.value);
409 }.bind(this));
410 this.__cookie.save();
411 }
412 });
413
414 qwebirc.ui.DefaultOptionsClass = new Class({
415 Extends: qwebirc.ui.CookieOptions
416 });