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