]> jfr.im git - irc/quakenet/qwebirc.git/blame - js/ui/optionspane.js
Add sound support, not currently enabled in the UI.
[irc/quakenet/qwebirc.git] / js / ui / optionspane.js
CommitLineData
ebb21d2e
CP
1qwebirc.config.CHECK_BOX = 1;
2qwebirc.config.TEXT_BOX = 2;
3qwebirc.config.RADIO_BUTTONS = 3;
4
5qwebirc.config.DEFAULT_OPTIONS = [
fb71087a
CP
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) {
127631e0
CP
14 if(ui.setBeepOnMention)
15 ui.setBeepOnMention(value);
fb71087a
CP
16 }
17 }],
c38a0240 18 [2, "OPEN_QUERIES", "Open new queries automatically", false]
ebb21d2e
CP
19];
20
21qwebirc.config.DefaultOptions = null;
22
c38a0240 23qwebirc.config.Input = new Class({
fb71087a 24 initialize: function(parent, option, position, parentObject) {
c38a0240
CP
25 this.option = option;
26 this.value = option.value;
27 this.position = position;
fb71087a
CP
28 this.parentElement = parent;
29 this.parentObject = parentObject;
c38a0240
CP
30
31 this.render();
32 },
33 FE: function(element, parent) {
34 var n = new Element(element);
35 if(!$defined(parent))
fb71087a 36 parent = this.parentElement;
c38a0240
CP
37
38 parent.appendChild(n);
39 return n;
40 },
41 focus: function() {
42 this.mainElement.focus();
fb71087a
CP
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)();
c38a0240
CP
59 }
60});
61
62qwebirc.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;
fb71087a
CP
70
71 this.parent();
c38a0240
CP
72 },
73 get: function() {
fb71087a 74 return this.parent(this.mainElement.value);
c38a0240
CP
75 }
76});
77
78qwebirc.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;
fb71087a
CP
86
87 this.parent();
c38a0240
CP
88 },
89 get: function() {
fb71087a 90 return this.parent(this.mainElement.checked);
c38a0240
CP
91 }
92});
93
94qwebirc.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 };
fb71087a 116 this.parent();
c38a0240
CP
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;
fb71087a 123 return this.parent(this.option.options[i][1]);
c38a0240
CP
124 }
125 }
126 }
127});
128
ebb21d2e 129qwebirc.config.Option = new Class({
fb71087a 130 initialize: function(optionId, prefix, label, default_, extras) {
c38a0240 131 this.prefix = prefix;
ebb21d2e 132 this.label = label;
c38a0240
CP
133 this.default_ = default_;
134 this.optionId = optionId;
fb71087a 135 this.extras = extras;
c38a0240
CP
136 },
137 setSavedValue: function(x) {
138 this.value = x;
ebb21d2e
CP
139 }
140});
141
c38a0240
CP
142qwebirc.config.RadioOption = new Class({
143 Extends: qwebirc.config.Option,
144 Element: qwebirc.config.RadioInput,
fb71087a 145 initialize: function(optionId, prefix, label, default_, extras, options) {
c38a0240
CP
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
fb71087a 153 this.parent(optionId, prefix, label, this.options[default_][1], extras);
c38a0240
CP
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
169qwebirc.config.TextOption = new Class({
170 Extends: qwebirc.config.Option,
171 Element: qwebirc.config.TextInput
172});
173
174qwebirc.config.CheckOption = new Class({
175 Extends: qwebirc.config.Option,
176 Element: qwebirc.config.CheckInput
177});
178
ebb21d2e 179qwebirc.ui.Options = new Class({
fb71087a 180 initialize: function(ui) {
ebb21d2e
CP
181 if(!$defined(qwebirc.config.DefaultOptions))
182 this.__configureDefaults();
183
c38a0240
CP
184 this.optionList = qwebirc.config.DefaultOptions.slice();
185 this.optionHash = {}
fb71087a 186 this.ui = ui;
c38a0240
CP
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;
ebb21d2e
CP
193 }.bind(this));
194 },
195 __configureDefaults: function() {
c38a0240
CP
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];
fb71087a
CP
201 var moreextras = x[4];
202 var extras = x[5];
ebb21d2e 203
c38a0240
CP
204 var stype = typeof(default_);
205 if(stype == "number") {
fb71087a 206 return new qwebirc.config.RadioOption(optionId, prefix, label, default_, moreextras, extra);
ebb21d2e 207 } else {
c38a0240
CP
208 var type;
209 if(stype == "boolean") {
210 type = qwebirc.config.CheckOption;
211 } else {
212 type = qwebirc.config.TextOption;
213 }
fb71087a 214 return new type(optionId, prefix, label, default_, moreextras);
ebb21d2e 215 }
ebb21d2e 216 });
c38a0240
CP
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() {
ebb21d2e
CP
231 }
232});
233
234qwebirc.ui.OptionsPane = new Class({
c38a0240
CP
235 Implements: [Events],
236 initialize: function(parentElement, optionObject) {
ebb21d2e 237 this.parentElement = parentElement;
c38a0240 238 this.optionObject = optionObject;
ebb21d2e
CP
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 };
ebb21d2e
CP
248
249 var t = FE("table", this.parentElement);
250 var tb = FE("tbody", t);
251
c38a0240
CP
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
ebb21d2e
CP
258 var row = FE("tr", tb);
259 var cella = FE("td", row);
c38a0240
CP
260 cella.set("text", x.label + ":");
261
ebb21d2e 262 var cellb = FE("td", row);
fb71087a 263 this.boxList.push([x, new x.Element(cellb, x, i, this)]);
c38a0240 264
c38a0240
CP
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());
ebb21d2e 291 }.bind(this));
c38a0240 292 this.optionObject.flush();
ebb21d2e
CP
293 }
294});
c38a0240
CP
295
296qwebirc.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
319qwebirc.ui.DefaultOptionsClass = new Class({
320 Extends: qwebirc.ui.CookieOptions
321});
322