]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/panes/options.js
65043132777bab442dce54bb3f577634835d4c16
[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 ];
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 if(this.enabled)
167 this.value = x;
168 }
169 });
170
171 qwebirc.config.RadioOption = new Class({
172 Extends: qwebirc.config.Option,
173 Element: qwebirc.config.RadioInput,
174 initialize: function(optionId, prefix, label, default_, extras, options) {
175 this.options = options.map(function(x) {
176 if(typeof(x) == "string")
177 return [x, x];
178 return x;
179 });
180 this.defaultposition = default_;
181
182 this.parent(optionId, prefix, label, this.options[default_][1], extras);
183 },
184 setSavedValue: function(x) {
185 for(var i=0;i<this.options.length;i++) {
186 var y = this.options[i][1];
187 if(x == y) {
188 this.position = i;
189 this.value = x;
190 return;
191 }
192 }
193 this.position = this.defaultposition;
194 this.value = this.default_;
195 }
196 });
197
198 qwebirc.config.TextOption = new Class({
199 Extends: qwebirc.config.Option,
200 Element: qwebirc.config.TextInput
201 });
202
203 qwebirc.config.CheckOption = new Class({
204 Extends: qwebirc.config.Option,
205 Element: qwebirc.config.CheckInput
206 });
207
208 qwebirc.ui.Options = new Class({
209 initialize: function(ui) {
210 if(!$defined(qwebirc.config.DefaultOptions))
211 this.__configureDefaults();
212
213 this.optionList = qwebirc.config.DefaultOptions.slice();
214 this.optionHash = {}
215 this.ui = ui;
216
217 this._setup();
218 this.optionList.forEach(function(x) {
219 x.setSavedValue(this._get(x));
220 this.optionHash[x.prefix] = x;
221 this[x.prefix] = x.value;
222 }.bind(this));
223 },
224 __configureDefaults: function() {
225 qwebirc.config.DefaultOptions = qwebirc.config.DEFAULT_OPTIONS.map(function(x) {
226 var optionId = x[0];
227 var prefix = x[1];
228 var label = x[2];
229 var default_ = x[3];
230 var moreextras = x[4];
231 var extras = x[5];
232
233 var stype = typeof(default_);
234 if(stype == "number") {
235 return new qwebirc.config.RadioOption(optionId, prefix, label, default_, moreextras, extra);
236 } else {
237 var type;
238 if(stype == "boolean") {
239 type = qwebirc.config.CheckOption;
240 } else {
241 type = qwebirc.config.TextOption;
242 }
243 return new type(optionId, prefix, label, default_, moreextras);
244 }
245 });
246 },
247 setValue: function(option, value) {
248 this.optionHash[option.prefix].value = value;
249 this[option.prefix] = value;
250 },
251 getOptionList: function() {
252 return this.optionList;
253 },
254 _get: function(x) {
255 return x.default_;
256 },
257 _setup: function() {
258 },
259 flush: function() {
260 }
261 });
262
263 qwebirc.ui.OptionsPane = new Class({
264 Implements: [Events],
265 initialize: function(parentElement, optionObject) {
266 this.parentElement = parentElement;
267 this.optionObject = optionObject;
268
269 this.createElements();
270 },
271 createElements: function() {
272 var FE = function(element, parent) {
273 var n = new Element(element);
274 parent.appendChild(n);
275 return n;
276 };
277
278 var t = FE("table", this.parentElement);
279 var tb = FE("tbody", t);
280
281 this.boxList = [];
282
283 var optList = this.optionObject.getOptionList();
284 for(var i=0;i<optList.length;i++) {
285 var x = optList[i];
286
287 var row = FE("tr", tb);
288 var cella = FE("td", row);
289 cella.set("text", x.label + ":");
290
291 var cellb = FE("td", row);
292 this.boxList.push([x, new x.Element(cellb, x, i, this)]);
293
294 }
295
296 var r = FE("tr", tb);
297 var cella = FE("td", r);
298 var cellb = FE("td", r);
299 var save = qwebirc.util.createInput("submit", cellb);
300 save.value = "Save";
301
302 save.addEvent("click", function() {
303 this.save();
304 this.fireEvent("close");
305 }.bind(this));
306
307 var cancel = qwebirc.util.createInput("submit", cellb);
308 cancel.value = "Cancel";
309 cancel.addEvent("click", function() {
310 this.fireEvent("close");
311 }.bind(this));
312 },
313 save: function() {
314 this.boxList.forEach(function(x) {
315 var option = x[0];
316 var box = x[1];
317 this.optionObject.setValue(option, box.get());
318 }.bind(this));
319 this.optionObject.flush();
320 }
321 });
322
323 qwebirc.ui.CookieOptions = new Class({
324 Extends: qwebirc.ui.Options,
325 _setup: function() {
326 this.__cookie = new Hash.Cookie("opt1", {duration: 3650, autoSave: false});
327 },
328 _get: function(x) {
329 var v = this.__cookie.get(x.optionId);
330 if(!$defined(v))
331 return x.default_;
332
333 return v;
334 },
335 flush: function() {
336 this.__cookie.erase();
337 this._setup();
338
339 this.getOptionList().forEach(function(x) {
340 this.__cookie.set(x.optionId, x.value);
341 }.bind(this));
342 this.__cookie.save();
343 }
344 });
345
346 qwebirc.ui.DefaultOptionsClass = new Class({
347 Extends: qwebirc.ui.CookieOptions
348 });