]> jfr.im git - irc/quakenet/qwebirc.git/blame - 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
CommitLineData
ebb21d2e
CP
1qwebirc.config.CHECK_BOX = 1;
2qwebirc.config.TEXT_BOX = 2;
3qwebirc.config.RADIO_BUTTONS = 3;
4
85449eee
CP
5qwebirc.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
ebb21d2e 16qwebirc.config.DEFAULT_OPTIONS = [
aeb8c784 17 [1, "BEEP_ON_MENTION", "Beep when nick mentioned or on query activity (requires Flash)", true, {
2fb3b3b0
CP
18 enabled: function() {
19 if(!$defined(Browser.Plugins.Flash) || Browser.Plugins.Flash.version < 8)
20 return [false, false]; /* [disabled, default_value] */
21 return [true];
fb71087a
CP
22 },
23 get: function(value, ui) {
127631e0
CP
24 if(ui.setBeepOnMention)
25 ui.setBeepOnMention(value);
fb71087a
CP
26 }
27 }],
2fb3b3b0 28 [7, "FLASH_ON_MENTION", "Flash titlebar when nick mentioned or on query activity", true, {
85449eee 29 enabled: qwebirc.ui.supportsFocus
2fb3b3b0 30 }],
7af2d0c1
CP
31 [2, "DEDICATED_MSG_WINDOW", "Send privmsgs to dedicated messages window", false],
32 [4, "DEDICATED_NOTICE_WINDOW", "Send notices to dedicated message window", false],
326478c2 33 [3, "NICK_OV_STATUS", "Show status (@/+) before nicknames in nicklist", true],
de4250ad 34 [5, "ACCEPT_SERVICE_INVITES", "Automatically join channels when invited by Q", true],
5c0b5a6e 35 [6, "USE_HIDDENHOST", "Hide your hostmask when authed to Q (+x)", true],
85449eee
CP
36 [8, "LASTPOS_LINE", "Show a last position indicator for each window", true, {
37 enabled: qwebirc.ui.supportsFocus
38 }],
0a5b0904
CP
39 [9, "NICK_COLOURS", "Automatically colour nicknames", false],
40 [10, "HIDE_JOINPARTS", "Hide JOINS/PARTS/QUITS", false]
ebb21d2e
CP
41];
42
43qwebirc.config.DefaultOptions = null;
44
c38a0240 45qwebirc.config.Input = new Class({
fb71087a 46 initialize: function(parent, option, position, parentObject) {
c38a0240
CP
47 this.option = option;
48 this.value = option.value;
2fb3b3b0 49 this.enabled = this.option.enabled;
c38a0240 50 this.position = position;
fb71087a
CP
51 this.parentElement = parent;
52 this.parentObject = parentObject;
c38a0240
CP
53
54 this.render();
55 },
a1e826c7
CP
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 },
c38a0240
CP
62 FE: function(element, parent) {
63 var n = new Element(element);
64 if(!$defined(parent))
fb71087a 65 parent = this.parentElement;
c38a0240
CP
66
67 parent.appendChild(n);
68 return n;
69 },
70 focus: function() {
71 this.mainElement.focus();
fb71087a
CP
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)();
c38a0240
CP
88 }
89});
90
91qwebirc.config.TextInput = new Class({
92 Extends: qwebirc.config.Input,
93 render: function() {
a1e826c7 94 var i = this.createInput("text");
c38a0240
CP
95 this.mainElement = i;
96
c38a0240 97 i.value = this.value;
2fb3b3b0
CP
98 i.disabled = !this.enabled;
99
fb71087a 100 this.parent();
c38a0240
CP
101 },
102 get: function() {
fb71087a 103 return this.parent(this.mainElement.value);
c38a0240
CP
104 }
105});
106
107qwebirc.config.CheckInput = new Class({
108 Extends: qwebirc.config.Input,
109 render: function() {
a1e826c7 110 var i = this.createInput("checkbox");
c38a0240
CP
111 this.mainElement = i;
112
c38a0240 113 i.checked = this.value;
2fb3b3b0
CP
114 i.disabled = !this.enabled;
115
fb71087a 116 this.parent();
c38a0240
CP
117 },
118 get: function() {
fb71087a 119 return this.parent(this.mainElement.checked);
c38a0240
CP
120 }
121});
122
123qwebirc.config.RadioInput = new Class({
124 Extends: qwebirc.config.Input,
125 render: function() {
126 var value = this.option.options;
127
128 this.elements = [];
2fb3b3b0 129
c38a0240
CP
130 for(var i=0;i<value.length;i++) {
131 var d = this.FE("div", this.parentObject);
a1e826c7 132 var e = this.createInput("radio", d, "options_radio" + this.position, i == this.option.position);
c38a0240 133 this.elements.push(e);
2fb3b3b0
CP
134 e.disabled = !this.enabled;
135
c38a0240
CP
136 if(i == 0)
137 this.mainElement = e;
138
139 d.appendChild(document.createTextNode(value[i][0]));
140 };
fb71087a 141 this.parent();
c38a0240
CP
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;
fb71087a 148 return this.parent(this.option.options[i][1]);
c38a0240
CP
149 }
150 }
151 }
152});
153
ebb21d2e 154qwebirc.config.Option = new Class({
fb71087a 155 initialize: function(optionId, prefix, label, default_, extras) {
c38a0240 156 this.prefix = prefix;
ebb21d2e 157 this.label = label;
c38a0240
CP
158 this.default_ = default_;
159 this.optionId = optionId;
fb71087a 160 this.extras = extras;
2fb3b3b0
CP
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 }
c38a0240
CP
171 },
172 setSavedValue: function(x) {
01343497
CP
173 if(this.enabled)
174 this.value = x;
ebb21d2e
CP
175 }
176});
177
c38a0240
CP
178qwebirc.config.RadioOption = new Class({
179 Extends: qwebirc.config.Option,
180 Element: qwebirc.config.RadioInput,
fb71087a 181 initialize: function(optionId, prefix, label, default_, extras, options) {
c38a0240
CP
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
fb71087a 189 this.parent(optionId, prefix, label, this.options[default_][1], extras);
c38a0240
CP
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
205qwebirc.config.TextOption = new Class({
206 Extends: qwebirc.config.Option,
207 Element: qwebirc.config.TextInput
208});
209
210qwebirc.config.CheckOption = new Class({
211 Extends: qwebirc.config.Option,
212 Element: qwebirc.config.CheckInput
213});
214
ebb21d2e 215qwebirc.ui.Options = new Class({
fb71087a 216 initialize: function(ui) {
ebb21d2e
CP
217 if(!$defined(qwebirc.config.DefaultOptions))
218 this.__configureDefaults();
219
c38a0240
CP
220 this.optionList = qwebirc.config.DefaultOptions.slice();
221 this.optionHash = {}
fb71087a 222 this.ui = ui;
c38a0240
CP
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;
ebb21d2e
CP
229 }.bind(this));
230 },
231 __configureDefaults: function() {
c38a0240
CP
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];
fb71087a
CP
237 var moreextras = x[4];
238 var extras = x[5];
ebb21d2e 239
c38a0240
CP
240 var stype = typeof(default_);
241 if(stype == "number") {
fb71087a 242 return new qwebirc.config.RadioOption(optionId, prefix, label, default_, moreextras, extra);
ebb21d2e 243 } else {
c38a0240
CP
244 var type;
245 if(stype == "boolean") {
246 type = qwebirc.config.CheckOption;
247 } else {
248 type = qwebirc.config.TextOption;
249 }
fb71087a 250 return new type(optionId, prefix, label, default_, moreextras);
ebb21d2e 251 }
ebb21d2e 252 });
c38a0240
CP
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() {
ebb21d2e
CP
267 }
268});
269
270qwebirc.ui.OptionsPane = new Class({
c38a0240
CP
271 Implements: [Events],
272 initialize: function(parentElement, optionObject) {
ebb21d2e 273 this.parentElement = parentElement;
c38a0240 274 this.optionObject = optionObject;
ebb21d2e
CP
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 };
ebb21d2e
CP
284
285 var t = FE("table", this.parentElement);
286 var tb = FE("tbody", t);
287
c38a0240
CP
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
ebb21d2e
CP
294 var row = FE("tr", tb);
295 var cella = FE("td", row);
c38a0240
CP
296 cella.set("text", x.label + ":");
297
ebb21d2e 298 var cellb = FE("td", row);
fb71087a 299 this.boxList.push([x, new x.Element(cellb, x, i, this)]);
c38a0240 300
c38a0240
CP
301 }
302
303 var r = FE("tr", tb);
304 var cella = FE("td", r);
305 var cellb = FE("td", r);
a1e826c7 306 var save = qwebirc.util.createInput("submit", cellb);
c38a0240
CP
307 save.value = "Save";
308
309 save.addEvent("click", function() {
310 this.save();
311 this.fireEvent("close");
312 }.bind(this));
313
a1e826c7 314 var cancel = qwebirc.util.createInput("submit", cellb);
c38a0240
CP
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());
ebb21d2e 325 }.bind(this));
c38a0240 326 this.optionObject.flush();
ebb21d2e
CP
327 }
328});
c38a0240
CP
329
330qwebirc.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
353qwebirc.ui.DefaultOptionsClass = new Class({
354 Extends: qwebirc.ui.CookieOptions
355});