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