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