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