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