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