]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/frontends/qui.js
Merge default into quakenet.
[irc/quakenet/qwebirc.git] / js / ui / frontends / qui.js
1 qwebirc.ui.QUI = new Class({
2 Extends: qwebirc.ui.RootUI,
3 initialize: function(parentElement, theme, options) {
4 this.parent(parentElement, qwebirc.ui.QUI.Window, "qui", options);
5 this.theme = theme;
6 this.parentElement = parentElement;
7 this.setModifiableStylesheet("qui");
8 },
9 postInitialize: function() {
10 this.qjsui = new qwebirc.ui.QUI.JSUI("qwebirc-qui", this.parentElement);
11 this.qjsui.addEvent("reflow", function() {
12 var w = this.getActiveWindow();
13 if($defined(w))
14 w.onResize();
15 }.bind(this));
16 this.qjsui.top.addClass("outertabbar");
17 this.qjsui.left.addClass("outertabbar");
18
19 this.qjsui.top.addClass("outertabbar_top");
20 this.qjsui.left.addClass("outertabbar_left");
21
22 this.qjsui.bottom.addClass("input");
23 this.qjsui.right.addClass("nicklist");
24 this.qjsui.topic.addClass("topic");
25 this.qjsui.middle.addClass("lines");
26
27 this.outerTabs = new Element("div");
28 this.sideTabs = null;
29
30 this.tabs = new Element("div");
31 this.tabs.addClass("tabbar");
32
33 this.__createDropdownMenu();
34
35 this.outerTabs.appendChild(this.tabs);
36 this.origtopic = this.topic = this.qjsui.topic;
37 this.lines = this.qjsui.middle;
38 this.orignicklist = this.nicklist = this.qjsui.right;
39
40 this.input = this.qjsui.bottom;
41 this.reflow = this.qjsui.reflow.bind(this.qjsui);
42
43 var scrollHandler = function(x) {
44 var event = new Event(x);
45 var up, down;
46 if(this.sideTabs) {
47 var p = this.qjsui.left;
48
49 /* don't scroll if we're scrollable */
50 if(p.getScrollSize().y > p.clientHeight)
51 return;
52
53 up = event.wheel < 0;
54 down = event.wheel > 0;
55 } else {
56 up = event.wheel > 0;
57 down = event.wheel < 0;
58 }
59
60 if(up) {
61 this.nextWindow();
62 } else if(down) {
63 this.prevWindow();
64 }
65 event.stop();
66 }.bind(this);
67 this.qjsui.left.addEvent("mousewheel", scrollHandler);
68 this.qjsui.top.addEvent("mousewheel", scrollHandler);
69
70 this.createInput();
71 this.reflow();
72 for(var i=50;i<1000;i+=50)
73 this.reflow.delay(i, true);
74 for(var i=1000;i<2000;i+=100)
75 this.reflow.delay(i);
76 for(var i=2000;i<15000;i+=500)
77 this.reflow.delay(i);
78
79 this.setSideTabs(this.uiOptions.SIDE_TABS);
80
81 },
82 newWindow: function(client, type, name) {
83 var w = this.parent(client, type, name);
84 w.setSideTabs(this.sideTabs);
85 return w;
86 },
87 __createDropdownMenu: function() {
88 var dropdownMenu = new Element("span");
89 dropdownMenu.addClass("dropdownmenu");
90
91 dropdownMenu.hide = function() {
92 dropdownMenu.setStyle("display", "none");
93 dropdownMenu.visible = false;
94 document.removeEvent("mousedown", hideEvent);
95 }.bind(this);
96 var hideEvent = function() { dropdownMenu.hide(); };
97
98 dropdownMenu.hide();
99 this.parentElement.appendChild(dropdownMenu);
100
101 this.UICommands.forEach(function(x) {
102 var text = x[0];
103 var fn = this[x[1] + "Window"].bind(this);
104 var e = new Element("a");
105 e.addEvent("mousedown", function(e) { new Event(e).stop(); });
106 e.addEvent("click", function() {
107 dropdownMenu.hide();
108 fn();
109 });
110 e.set("text", text);
111 dropdownMenu.appendChild(e);
112 }.bind(this));
113
114 var dropdown = new Element("div");
115 dropdown.addClass("dropdown-tab");
116 dropdown.appendChild(new Element("img", {src: qwebirc.global.staticBaseURL + "images/icon.png", title: "menu", alt: "menu"}));
117 dropdown.setStyle("opacity", 1);
118
119 this.outerTabs.appendChild(dropdown);
120 dropdownMenu.show = function(x) {
121 new Event(x).stop();
122
123 if(dropdownMenu.visible) {
124 dropdownMenu.hide();
125 return;
126 }
127
128 dropdownMenu.setStyle("display", "inline-block");
129 dropdownMenu.visible = true;
130
131 document.addEvent("mousedown", hideEvent);
132 }.bind(this);
133 dropdown.addEvent("mousedown", function(e) { new Event(e).stop(); });
134 dropdown.addEvent("click", dropdownMenu.show);
135 },
136 createInput: function() {
137 var form = new Element("form");
138 this.input.appendChild(form);
139
140 form.addClass("input");
141
142 var inputbox = new Element("input");
143 this.addEvent("signedOn", function() {
144 inputbox.placeholder = "chat here! you can also use commands, like /JOIN or /HELP";
145 var d = function() { inputbox.addClass("input-flash"); }.delay(250);
146 var d = function() { inputbox.removeClass("input-flash"); }.delay(500);
147 var d = function() { inputbox.addClass("input-flash"); }.delay(750);
148 var d = function() { inputbox.removeClass("input-flash"); }.delay(1000);
149 var d = function() { inputbox.addClass("input-flash"); }.delay(1250);
150 var d = function() { inputbox.removeClass("input-flash"); }.delay(1750);
151 });
152 form.appendChild(inputbox);
153 this.inputbox = inputbox;
154 this.inputbox.maxLength = 470;
155
156 var sendInput = function() {
157 if(inputbox.value == "")
158 return;
159
160 this.resetTabComplete();
161 this.getActiveWindow().historyExec(inputbox.value);
162 inputbox.value = "";
163 inputbox.placeholder = "";
164 }.bind(this);
165
166 if(!qwebirc.util.deviceHasKeyboard()) {
167 inputbox.addClass("mobile-input");
168 var inputButton = new Element("input", {type: "button"});
169 inputButton.addClass("mobile-button");
170 inputButton.addEvent("click", function() {
171 sendInput();
172 inputbox.focus();
173 });
174 inputButton.value = ">";
175 this.input.appendChild(inputButton);
176 var reflowButton = function() {
177 var containerSize = this.input.getSize();
178 var buttonSize = inputButton.getSize();
179
180 var buttonLeft = containerSize.x - buttonSize.x - 5; /* lovely 5 */
181
182 inputButton.setStyle("left", buttonLeft);
183 inputbox.setStyle("width", buttonLeft - 5);
184 inputButton.setStyle("height", containerSize.y);
185 }.bind(this);
186 this.qjsui.addEvent("reflow", reflowButton);
187 } else {
188 inputbox.addClass("keyboard-input");
189 }
190
191 form.addEvent("submit", function(e) {
192 new Event(e).stop();
193 sendInput();
194 });
195
196 var reset = this.resetTabComplete.bind(this);
197 inputbox.addEvent("focus", reset);
198 inputbox.addEvent("mousedown", reset);
199 inputbox.addEvent("keypress", reset);
200
201 inputbox.addEvent("keydown", function(e) {
202 var resultfn;
203 var cvalue = inputbox.value;
204
205 if(e.alt || e.control || e.meta)
206 return;
207
208 if(e.key == "up" && !e.shift) {
209 resultfn = this.commandhistory.upLine;
210 } else if(e.key == "down" && !e.shift) {
211 resultfn = this.commandhistory.downLine;
212 } else if(e.key == "tab") {
213 this.tabComplete(inputbox, e.shift);
214
215 new Event(e).stop();
216 e.preventDefault();
217 return;
218 } else {
219 return;
220 }
221
222 this.resetTabComplete();
223 if((cvalue != "") && (this.lastcvalue != cvalue))
224 this.commandhistory.addLine(cvalue, true);
225
226 var result = resultfn.bind(this.commandhistory)();
227
228 new Event(e).stop();
229 e.preventDefault();
230
231 if(!result)
232 result = "";
233 this.lastcvalue = result;
234
235 inputbox.value = result;
236 qwebirc.util.setAtEnd(inputbox);
237 }.bind(this));
238 },
239 setLines: function(lines) {
240 this.lines.parentNode.replaceChild(lines, this.lines);
241 this.qjsui.middle = this.lines = lines;
242 },
243 setChannelItems: function(nicklist, topic) {
244 if(!$defined(nicklist)) {
245 nicklist = this.orignicklist;
246 topic = this.origtopic;
247 }
248 this.nicklist.parentNode.replaceChild(nicklist, this.nicklist);
249 this.qjsui.right = this.nicklist = nicklist;
250
251 this.topic.parentNode.replaceChild(topic, this.topic);
252 this.qjsui.topic = this.topic = topic;
253 },
254 setSideTabs: function(value) {
255 if(value === this.sideTabs)
256 return;
257
258 if(this.sideTabs === true) {
259 this.qjsui.left.removeChild(this.outerTabs);
260 } else if(this.sideTabs === false) {
261 this.qjsui.top.removeChild(this.outerTabs);
262 }
263 if(value) {
264 this.qjsui.left.appendChild(this.outerTabs);
265 this.qjsui.top.style.display = "none";
266 this.qjsui.left.style.display = "";
267 } else {
268 this.qjsui.top.appendChild(this.outerTabs);
269 this.qjsui.top.style.display = "";
270 this.qjsui.left.style.display = "none";
271 }
272 this.sideTabs = value;
273 this.windows.each(function(k, v) {
274 v.each(function(k, v2) {
275 v2.setSideTabs(value);
276 });
277 });
278 }
279 });
280
281 qwebirc.ui.QUI.JSUI = new Class({
282 Implements: [Events],
283 initialize: function(class_, parent, sizer) {
284 this.parent = parent;
285 this.sizer = $defined(sizer)?sizer:parent;
286
287 this.class_ = class_;
288 this.create();
289
290 this.reflowevent = null;
291
292 window.addEvent("resize", function() {
293 this.reflow(100);
294 }.bind(this));
295 },
296 applyClasses: function(pos, l) {
297 l.addClass("dynamicpanel");
298 l.addClass(this.class_);
299 l.addClass(pos + "boundpanel");
300 },
301 create: function() {
302 var XE = function(pos) {
303 var element = new Element("div");
304 this.applyClasses(pos, element);
305
306 this.parent.appendChild(element);
307 return element;
308 }.bind(this);
309
310 this.top = XE("top");
311 this.left = XE("left");
312 this.topic = XE("topic");
313 this.middle = XE("middle");
314 this.right = XE("right");
315 this.bottom = XE("bottom");
316 },
317 reflow: function(delay) {
318 if(!delay)
319 delay = 1;
320
321 if(this.reflowevent)
322 $clear(this.reflowevent);
323 this.__reflow();
324 this.reflowevent = this.__reflow.delay(delay, this);
325 },
326 __reflow: function() {
327 var bottom = this.bottom;
328 var middle = this.middle;
329 var right = this.right;
330 var topic = this.topic;
331 var top = this.top;
332 var left = this.left;
333
334 /* |----------------------------------------------|
335 * | top |
336 * |----------------------------------------------|
337 * | left | topic | right |
338 * | |-------------------------------| |
339 * | | middle | |
340 * | | | |
341 * | | | |
342 * | |---------------------------------------|
343 * | | bottom |
344 * |----------------------------------------------|
345 */
346
347 var topicsize = topic.getSize();
348 var topsize = top.getSize();
349 var rightsize = right.getSize();
350 var bottomsize = bottom.getSize();
351 var leftsize = left.getSize();
352 var docsize = this.sizer.getSize();
353
354 var mheight = (docsize.y - topsize.y - bottomsize.y - topicsize.y);
355 var mwidth = (docsize.x - rightsize.x - leftsize.x);
356
357 left.setStyle("top", topsize.y);
358 topic.setStyle("top", topsize.y);
359 topic.setStyle("left", leftsize.x);
360 topic.setStyle("width", docsize.x - leftsize.x);
361
362 middle.setStyle("top", (topsize.y + topicsize.y));
363 middle.setStyle("left", leftsize.x);
364 if(mheight > 0) {
365 middle.setStyle("height", mheight);
366 right.setStyle("height", mheight);
367 }
368
369 if(mwidth > 0)
370 middle.setStyle("width", mwidth);
371 right.setStyle("top", (topsize.y + topicsize.y));
372
373 bottom.setStyle("left", leftsize.x);
374 this.fireEvent("reflow");
375 },
376 showChannel: function(state, nicklistVisible) {
377 var display = "none";
378 if(state)
379 display = "block";
380
381 this.right.setStyle("display", nicklistVisible ? display : "none");
382 this.topic.setStyle("display", display);
383 },
384 showInput: function(state) {
385 this.bottom.isVisible = state;
386 this.bottom.setStyle("display", state?"block":"none");
387 }
388 });
389
390 qwebirc.ui.QUI.Window = new Class({
391 Extends: qwebirc.ui.Window,
392
393 initialize: function(parentObject, client, type, name, identifier) {
394 this.parent(parentObject, client, type, name, identifier);
395
396 this.tab = new Element("a");
397 this.tab.addClass("tab");
398 this.tab.addEvent("focus", function() { this.blur() }.bind(this.tab));;
399
400 this.spaceNode = document.createTextNode(" ");
401 parentObject.tabs.appendChild(this.tab);
402 parentObject.tabs.appendChild(this.spaceNode);
403
404 if(type != qwebirc.ui.WINDOW_STATUS && type != qwebirc.ui.WINDOW_CONNECT) {
405 var tabclose = new Element("span");
406 this.tabclose = tabclose;
407 tabclose.set("text", "X");
408 tabclose.addClass("tabclose");
409 var close = function(e) {
410 new Event(e).stop();
411
412 if(this.closed)
413 return;
414
415 if(type == qwebirc.ui.WINDOW_CHANNEL)
416 this.client.exec("/PART " + name);
417
418 this.close();
419
420 //parentObject.inputbox.focus();
421 }.bind(this);
422
423 tabclose.addEvent("click", close);
424 this.tab.addEvent("mouseup", function(e) {
425 var button = 1;
426
427 if(Browser.Engine.trident)
428 button = 4;
429
430 if(e.event.button == button)
431 close(e);
432 }.bind(this));
433
434 this.tab.appendChild(tabclose);
435 } else {
436 this.tabclose = null;
437 }
438
439 this.tab.appendText(name);
440 this.tab.addEvent("click", function(e) {
441 new Event(e).stop();
442
443 if(this.closed)
444 return;
445
446 parentObject.selectWindow(this);
447 }.bind(this));
448
449
450 this.lines = new Element("div");
451 this.parentObject.qjsui.applyClasses("middle", this.lines);
452 this.lines.addClass("lines");
453 if(type != qwebirc.ui.WINDOW_CUSTOM && type != qwebirc.ui.WINDOW_CONNECT)
454 this.lines.addClass("ircwindow");
455
456 this.lines.addEvent("scroll", function() {
457 this.scrolleddown = this.scrolledDown();
458 this.scrollpos = this.getScrollParent().getScroll();
459 }.bind(this));
460
461 if(type == qwebirc.ui.WINDOW_CHANNEL) {
462 this.topic = new Element("div");
463 this.parentObject.qjsui.applyClasses("topic", this.topic);
464 this.topic.addClass("topic");
465 this.topic.addClass("tab-invisible");
466 this.topic.set("html", "&nbsp;");
467 this.topic.addEvent("dblclick", this.editTopic.bind(this));
468 this.parentObject.qjsui.applyClasses("topic", this.topic);
469
470 this.prevNick = null;
471 this.nicklist = new Element("div");
472 this.nicklist.addClass("nicklist");
473 this.nicklist.addClass("tab-invisible");
474 this.nicklist.addEvent("click", this.removePrevMenu.bind(this));
475 this.parentObject.qjsui.applyClasses("right", this.nicklist);
476
477 this.updateTopic("");
478 }
479
480 this.nicksColoured = this.parentObject.uiOptions.NICK_COLOURS;
481 this.reflow();
482 },
483 rename: function(name) {
484 var newNode = document.createTextNode(name);
485 if(this.parentObject.sideTabs) {
486 this.tab.replaceChild(newNode, this.tab.childNodes[1]);
487 } else {
488 this.tab.replaceChild(newNode, this.tab.firstChild);
489 }
490
491 if(this.type == qwebirc.ui.WINDOW_QUERY)
492 this.updateTopic("");
493 },
494 editTopic: function() {
495 if(this.type != qwebirc.ui.WINDOW_CHANNEL)
496 return;
497
498 if(!this.client.nickOnChanHasPrefix(this.client.nickname, this.name, "@")) {
499 /* var cmodes = this.client.getChannelModes(channel);
500 if(cmodes.indexOf("t")) {*/
501 alert("Sorry, you need to be a channel operator to change the topic!");
502 return;
503 /*}*/
504 }
505 var newTopic = prompt("Change topic of " + this.name + " to:", this.topic.topicText);
506 if(newTopic === null)
507 return;
508
509 this.client.exec("/TOPIC " + newTopic);
510 },
511 reflow: function() {
512 this.parentObject.reflow();
513 },
514 onResize: function() {
515 if(this.scrolleddown) {
516 if(Browser.Engine.trident) {
517 this.scrollToBottom.delay(5, this);
518 } else {
519 this.scrollToBottom();
520 }
521 } else if($defined(this.scrollpos)) {
522 if(Browser.Engine.trident) {
523 this.getScrollParent().scrollTo(this.scrollpos.x, this.scrollpos.y);
524 } else {
525 this.getScrollParent().scrollTo.delay(5, this, [this.scrollpos.x, this.scrollpos.y]);
526 }
527 }
528 },
529 createMenu: function(nick, parent) {
530 var e = new Element("div");
531 parent.appendChild(e);
532 e.addClass("menu");
533
534 var nickArray = [nick];
535 qwebirc.ui.MENU_ITEMS.forEach(function(x) {
536 if(!x.predicate || x.predicate !== true && !x.predicate.apply(this, nickArray))
537 return;
538
539 var e2 = new Element("a");
540 e.appendChild(e2);
541
542 e2.set("text", "- " + x.text);
543
544 e2.addEvent("focus", function() { this.blur() }.bind(e2));
545 e2.addEvent("click", function(ev) { new Event(ev.stop()); this.menuClick(x.fn); }.bind(this));
546 }.bind(this));
547 return e;
548 },
549 menuClick: function(fn) {
550 /*
551 this.prevNick.removeChild(this.prevNick.menu);
552 this.prevNick.menu = null;
553 */
554 fn.bind(this)(this.prevNick.realNick);
555 this.removePrevMenu();
556 },
557 moveMenuClass: function() {
558 if(!this.prevNick)
559 return;
560 if(this.nicklist.firstChild == this.prevNick) {
561 this.prevNick.removeClass("selected-middle");
562 } else {
563 this.prevNick.addClass("selected-middle");
564 }
565 },
566 removePrevMenu: function() {
567 if(!this.prevNick)
568 return;
569
570 this.prevNick.removeClass("selected");
571 this.prevNick.removeClass("selected-middle");
572 if(this.prevNick.menu)
573 this.prevNick.removeChild(this.prevNick.menu);
574 this.prevNick = null;
575 },
576 nickListAdd: function(nick, position) {
577 var realNick = this.client.stripPrefix(nick);
578
579 var e = new Element("a");
580 qwebirc.ui.insertAt(position, this.nicklist, e);
581 var span = new Element("span");
582 if(this.parentObject.uiOptions.NICK_COLOURS) {
583 var colour = realNick.toHSBColour(this.client);
584 if($defined(colour))
585 span.setStyle("color", colour.rgbToHex());
586 }
587 span.set("text", nick);
588 e.appendChild(span);
589
590 e.realNick = realNick;
591
592 e.addEvent("click", function(x) {
593 if(this.prevNick == e) {
594 this.removePrevMenu();
595 return;
596 }
597
598 this.removePrevMenu();
599 this.prevNick = e;
600 e.addClass("selected");
601 this.moveMenuClass();
602 e.menu = this.createMenu(e.realNick, e);
603 new Event(x).stop();
604 }.bind(this));
605
606 e.addEvent("focus", function() { this.blur() }.bind(e));
607 this.moveMenuClass();
608 return e;
609 },
610 nickListRemove: function(nick, stored) {
611 this.nicklist.removeChild(stored);
612 this.moveMenuClass();
613 },
614 updateTopic: function(topic) {
615 var t = this.topic;
616
617 while(t.firstChild)
618 t.removeChild(t.firstChild);
619
620 var suffix;
621 if(this.type == qwebirc.ui.WINDOW_CHANNEL) {
622 suffix = ": ";
623 } else {
624 suffix = "";
625 }
626 qwebirc.ui.Colourise(this.name + suffix, t, null, null, this);
627
628 if(this.type == qwebirc.ui.WINDOW_CHANNEL) {
629 t.topicText = topic;
630 if (topic) {
631 this.parent(topic, t);
632 } else {
633 t.appendChild(document.createTextNode("(no topic set)"));
634 }
635 }
636
637 this.reflow();
638 },
639 select: function() {
640 var inputVisible = this.type != qwebirc.ui.WINDOW_CONNECT && this.type != qwebirc.ui.WINDOW_CUSTOM;
641
642 this.tab.removeClass("tab-unselected");
643 this.tab.addClass("tab-selected");
644
645 this.parentObject.setLines(this.lines);
646 this.parentObject.setChannelItems(this.nicklist, this.topic);
647 this.parentObject.qjsui.showInput(inputVisible);
648 this.parentObject.qjsui.showChannel($defined(this.nicklist), this.parentObject.uiOptions.SHOW_NICKLIST);
649
650 this.reflow();
651
652 this.parent();
653
654 if(inputVisible)
655 this.parentObject.inputbox.focus();
656
657 if(this.type == qwebirc.ui.WINDOW_CHANNEL && this.nicksColoured != this.parentObject.uiOptions.NICK_COLOURS) {
658 this.nicksColoured = this.parentObject.uiOptions.NICK_COLOURS;
659
660 var nodes = this.nicklist.childNodes;
661 if(this.parentObject.uiOptions.NICK_COLOURS) {
662 for(var i=0;i<nodes.length;i++) {
663 var e = nodes[i], span = e.firstChild;
664 var colour = e.realNick.toHSBColour(this.client);
665 if($defined(colour))
666 span.setStyle("color", colour.rgbToHex());
667 };
668 } else {
669 for(var i=0;i<nodes.length;i++) {
670 var span = nodes[i].firstChild;
671 span.setStyle("color", null);
672 };
673 }
674 }
675 },
676 deselect: function() {
677 this.parent();
678
679 this.tab.removeClass("tab-selected");
680 this.tab.addClass("tab-unselected");
681 },
682 close: function() {
683 this.parent();
684
685 this.parentObject.tabs.removeChild(this.tab);
686 this.parentObject.tabs.removeChild(this.spaceNode);
687 this.reflow();
688 },
689 addLine: function(type, line, colourClass) {
690 var e = new Element("div");
691
692 if(colourClass) {
693 e.addClass(colourClass);
694 } else if(this.lastcolour) {
695 e.addClass("linestyle1");
696 } else {
697 e.addClass("linestyle2");
698 }
699 this.lastcolour = !this.lastcolour;
700
701 this.parent(type, line, colourClass, e);
702 },
703 setHilighted: function(state) {
704 var laststate = this.hilighted;
705
706 this.parent(state);
707
708 if(state == laststate)
709 return;
710
711 this.tab.removeClass("tab-hilight-activity");
712 this.tab.removeClass("tab-hilight-us");
713 this.tab.removeClass("tab-hilight-speech");
714
715 switch(this.hilighted) {
716 case qwebirc.ui.HILIGHT_US:
717 this.tab.addClass("tab-hilight-us");
718 break;
719 case qwebirc.ui.HILIGHT_SPEECH:
720 this.tab.addClass("tab-hilight-speech");
721 break;
722 case qwebirc.ui.HILIGHT_ACTIVITY:
723 this.tab.addClass("tab-hilight-activity");
724 break;
725 }
726 },
727 setSideTabs: function(value) {
728 if(this.tabclose === null)
729 return;
730 this.tab.removeChild(this.tabclose);
731 if(value) {
732 this.tab.insertBefore(this.tabclose, this.tab.firstChild);
733 } else {
734 this.tab.appendChild(this.tabclose);
735 }
736 }
737 });