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