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