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