]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/frontends/qui.js
remove dubious # hrefs
[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 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
269 qwebirc.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 topic.setStyle("width", docsize.x - leftsize.x);
349
350 middle.setStyle("top", (topsize.y + topicsize.y));
351 middle.setStyle("left", leftsize.x);
352 if(mheight > 0) {
353 middle.setStyle("height", mheight);
354 right.setStyle("height", mheight);
355 }
356
357 if(mwidth > 0)
358 middle.setStyle("width", mwidth);
359 right.setStyle("top", (topsize.y + topicsize.y));
360
361 bottom.setStyle("left", leftsize.x);
362 this.fireEvent("reflow");
363 },
364 showChannel: function(state, nicklistVisible) {
365 var display = "none";
366 if(state)
367 display = "block";
368
369 this.right.setStyle("display", nicklistVisible ? display : "none");
370 this.topic.setStyle("display", display);
371 },
372 showInput: function(state) {
373 this.bottom.isVisible = state;
374 this.bottom.setStyle("display", state?"block":"none");
375 }
376 });
377
378 qwebirc.ui.QUI.Window = new Class({
379 Extends: qwebirc.ui.Window,
380
381 initialize: function(parentObject, client, type, name, identifier) {
382 this.parent(parentObject, client, type, name, identifier);
383
384 this.tab = new Element("a");
385 this.tab.addClass("tab");
386 this.tab.addEvent("focus", function() { this.blur() }.bind(this.tab));;
387
388 this.spaceNode = document.createTextNode(" ");
389 parentObject.tabs.appendChild(this.tab);
390 parentObject.tabs.appendChild(this.spaceNode);
391
392 if(type != qwebirc.ui.WINDOW_STATUS && type != qwebirc.ui.WINDOW_CONNECT) {
393 var tabclose = new Element("span");
394 this.tabclose = tabclose;
395 tabclose.set("text", "X");
396 tabclose.addClass("tabclose");
397 var close = function(e) {
398 new Event(e).stop();
399
400 if(this.closed)
401 return;
402
403 if(type == qwebirc.ui.WINDOW_CHANNEL)
404 this.client.exec("/PART " + name);
405
406 this.close();
407
408 //parentObject.inputbox.focus();
409 }.bind(this);
410
411 tabclose.addEvent("click", close);
412 this.tab.addEvent("mouseup", function(e) {
413 var button = 1;
414
415 if(Browser.Engine.trident)
416 button = 4;
417
418 if(e.event.button == button)
419 close(e);
420 }.bind(this));
421
422 this.tab.appendChild(tabclose);
423 } else {
424 this.tabclose = null;
425 }
426
427 this.tab.appendText(name);
428 this.tab.addEvent("click", function(e) {
429 new Event(e).stop();
430
431 if(this.closed)
432 return;
433
434 parentObject.selectWindow(this);
435 }.bind(this));
436
437
438 this.lines = new Element("div");
439 this.parentObject.qjsui.applyClasses("middle", this.lines);
440 this.lines.addClass("lines");
441 if(type != qwebirc.ui.WINDOW_CUSTOM && type != qwebirc.ui.WINDOW_CONNECT)
442 this.lines.addClass("ircwindow");
443
444 this.lines.addEvent("scroll", function() {
445 this.scrolleddown = this.scrolledDown();
446 this.scrollpos = this.getScrollParent().getScroll();
447 }.bind(this));
448
449 if(type == qwebirc.ui.WINDOW_CHANNEL) {
450 this.topic = new Element("div");
451 this.parentObject.qjsui.applyClasses("topic", this.topic);
452 this.topic.addClass("topic");
453 this.topic.addClass("tab-invisible");
454 this.topic.set("html", "&nbsp;");
455 this.topic.addEvent("dblclick", this.editTopic.bind(this));
456 this.parentObject.qjsui.applyClasses("topic", this.topic);
457
458 this.prevNick = null;
459 this.nicklist = new Element("div");
460 this.nicklist.addClass("nicklist");
461 this.nicklist.addClass("tab-invisible");
462 this.nicklist.addEvent("click", this.removePrevMenu.bind(this));
463 this.parentObject.qjsui.applyClasses("right", this.nicklist);
464 }
465
466 if(type == qwebirc.ui.WINDOW_CHANNEL)
467 this.updateTopic("");
468
469 this.nicksColoured = this.parentObject.uiOptions.NICK_COLOURS;
470 this.reflow();
471 },
472 rename: function(name) {
473 this.tab.replaceChild(document.createTextNode(name), this.tab.firstChild);
474 },
475 editTopic: function() {
476 if(!this.client.nickOnChanHasPrefix(this.client.nickname, this.name, "@")) {
477 /* var cmodes = this.client.getChannelModes(channel);
478 if(cmodes.indexOf("t")) {*/
479 alert("Sorry, you need to be a channel operator to change the topic!");
480 return;
481 /*}*/
482 }
483 var newTopic = prompt("Change topic of " + this.name + " to:", this.topic.topicText);
484 if(newTopic === null)
485 return;
486
487 this.client.exec("/TOPIC " + newTopic);
488 },
489 reflow: function() {
490 this.parentObject.reflow();
491 },
492 onResize: function() {
493 if(this.scrolleddown) {
494 if(Browser.Engine.trident) {
495 this.scrollToBottom.delay(5, this);
496 } else {
497 this.scrollToBottom();
498 }
499 } else if($defined(this.scrollpos)) {
500 if(Browser.Engine.trident) {
501 this.getScrollParent().scrollTo(this.scrollpos.x, this.scrollpos.y);
502 } else {
503 this.getScrollParent().scrollTo.delay(5, this, [this.scrollpos.x, this.scrollpos.y]);
504 }
505 }
506 },
507 createMenu: function(nick, parent) {
508 var e = new Element("div");
509 parent.appendChild(e);
510 e.addClass("menu");
511
512 var nickArray = [nick];
513 qwebirc.ui.MENU_ITEMS.forEach(function(x) {
514 if(!x.predicate || x.predicate !== true && !x.predicate.apply(this, nickArray))
515 return;
516
517 var e2 = new Element("a");
518 e.appendChild(e2);
519
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 var span = new Element("span");
560 if(this.parentObject.uiOptions.NICK_COLOURS) {
561 var colour = realNick.toHSBColour(this.client);
562 if($defined(colour))
563 span.setStyle("color", colour.rgbToHex());
564 }
565 span.set("text", nick);
566 e.appendChild(span);
567
568 e.realNick = realNick;
569
570 e.addEvent("click", function(x) {
571 if(this.prevNick == e) {
572 this.removePrevMenu();
573 return;
574 }
575
576 this.removePrevMenu();
577 this.prevNick = e;
578 e.addClass("selected");
579 this.moveMenuClass();
580 e.menu = this.createMenu(e.realNick, e);
581 new Event(x).stop();
582 }.bind(this));
583
584 e.addEvent("focus", function() { this.blur() }.bind(e));
585 this.moveMenuClass();
586 return e;
587 },
588 nickListRemove: function(nick, stored) {
589 this.nicklist.removeChild(stored);
590 this.moveMenuClass();
591 },
592 updateTopic: function(topic) {
593 var t = this.topic;
594
595 while(t.firstChild)
596 t.removeChild(t.firstChild);
597
598 if(topic) {
599 t.topicText = topic;
600 this.parent(topic, t);
601 } else {
602 t.topicText = topic;
603 var e = new Element("div");
604 e.set("text", "(no topic set)");
605 e.addClass("emptytopic");
606 t.appendChild(e);
607 }
608 this.reflow();
609 },
610 select: function() {
611 var inputVisible = this.type != qwebirc.ui.WINDOW_CONNECT && this.type != qwebirc.ui.WINDOW_CUSTOM;
612
613 this.tab.removeClass("tab-unselected");
614 this.tab.addClass("tab-selected");
615
616 this.parentObject.setLines(this.lines);
617 this.parentObject.setChannelItems(this.nicklist, this.topic);
618 this.parentObject.qjsui.showInput(inputVisible);
619 this.parentObject.qjsui.showChannel($defined(this.nicklist), this.parentObject.uiOptions.SHOW_NICKLIST);
620
621 this.reflow();
622
623 this.parent();
624
625 if(inputVisible)
626 this.parentObject.inputbox.focus();
627
628 if(this.type == qwebirc.ui.WINDOW_CHANNEL && this.nicksColoured != this.parentObject.uiOptions.NICK_COLOURS) {
629 this.nicksColoured = this.parentObject.uiOptions.NICK_COLOURS;
630
631 var nodes = this.nicklist.childNodes;
632 if(this.parentObject.uiOptions.NICK_COLOURS) {
633 for(var i=0;i<nodes.length;i++) {
634 var e = nodes[i], span = e.firstChild;
635 var colour = e.realNick.toHSBColour(this.client);
636 if($defined(colour))
637 span.setStyle("color", colour.rgbToHex());
638 };
639 } else {
640 for(var i=0;i<nodes.length;i++) {
641 var span = nodes[i].firstChild;
642 span.setStyle("color", null);
643 };
644 }
645 }
646 },
647 deselect: function() {
648 this.parent();
649
650 this.tab.removeClass("tab-selected");
651 this.tab.addClass("tab-unselected");
652 },
653 close: function() {
654 this.parent();
655
656 this.parentObject.tabs.removeChild(this.tab);
657 this.parentObject.tabs.removeChild(this.spaceNode);
658 this.reflow();
659 },
660 addLine: function(type, line, colourClass) {
661 var e = new Element("div");
662
663 if(colourClass) {
664 e.addClass(colourClass);
665 } else if(this.lastcolour) {
666 e.addClass("linestyle1");
667 } else {
668 e.addClass("linestyle2");
669 }
670 this.lastcolour = !this.lastcolour;
671
672 this.parent(type, line, colourClass, e);
673 },
674 setHilighted: function(state) {
675 var laststate = this.hilighted;
676
677 this.parent(state);
678
679 if(state == laststate)
680 return;
681
682 this.tab.removeClass("tab-hilight-activity");
683 this.tab.removeClass("tab-hilight-us");
684 this.tab.removeClass("tab-hilight-speech");
685
686 switch(this.hilighted) {
687 case qwebirc.ui.HILIGHT_US:
688 this.tab.addClass("tab-hilight-us");
689 break;
690 case qwebirc.ui.HILIGHT_SPEECH:
691 this.tab.addClass("tab-hilight-speech");
692 break;
693 case qwebirc.ui.HILIGHT_ACTIVITY:
694 this.tab.addClass("tab-hilight-activity");
695 break;
696 }
697 },
698 setSideTabs: function(value) {
699 if(this.tabclose === null)
700 return;
701 this.tab.removeChild(this.tabclose);
702 if(value) {
703 this.tab.insertBefore(this.tabclose, this.tab.firstChild);
704 } else {
705 this.tab.appendChild(this.tabclose);
706 }
707 }
708 });