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