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