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