| | @@ -7,24 +7,27 @@ |
| 7 | 7 | const E1 = function(selector){ |
| 8 | 8 | const e = document.querySelector(selector); |
| 9 | 9 | if(!e) throw new Error("missing required DOM element: "+selector); |
| 10 | 10 | return e; |
| 11 | 11 | }; |
| 12 | + //document.body.classList.add('chat-only-mode'); |
| 12 | 13 | const Chat = (function(){ |
| 13 | 14 | const cs = { |
| 14 | 15 | e:{/*map of certain DOM elements.*/ |
| 15 | 16 | messageInjectPoint: E1('#message-inject-point'), |
| 16 | 17 | pageTitle: E1('head title'), |
| 17 | 18 | loadToolbar: undefined /* the load-posts toolbar (dynamically created) */, |
| 18 | 19 | inputWrapper: E1("#chat-input-area"), |
| 20 | + fileSelectWrapper: E1('#chat-input-file-area'), |
| 19 | 21 | messagesWrapper: E1('#chat-messages-wrapper'), |
| 20 | 22 | inputForm: E1('#chat-form'), |
| 21 | 23 | btnSubmit: E1('#chat-message-submit'), |
| 22 | 24 | inputSingle: E1('#chat-input-single'), |
| 23 | 25 | inputMulti: E1('#chat-input-multi'), |
| 24 | 26 | inputCurrent: undefined/*one of inputSingle or inputMulti*/, |
| 25 | | - inputFile: E1('#chat-input-file') |
| 27 | + inputFile: E1('#chat-input-file'), |
| 28 | + contentDiv: E1('div.content') |
| 26 | 29 | }, |
| 27 | 30 | me: F.user.name, |
| 28 | 31 | mxMsg: F.config.chat.initSize ? -F.config.chat.initSize : -50, |
| 29 | 32 | mnMsg: undefined/*lowest message ID we've seen so far (for history loading)*/, |
| 30 | 33 | pageIsActive: 'visible'===document.visibilityState, |
| | @@ -63,10 +66,11 @@ |
| 63 | 66 | this.e.inputCurrent = this.e.inputSingle; |
| 64 | 67 | } |
| 65 | 68 | D.addClass(old, 'hidden'); |
| 66 | 69 | D.removeClass(this.e.inputCurrent, 'hidden'); |
| 67 | 70 | this.e.inputCurrent.value = old.value; |
| 71 | + old.value = ''; |
| 68 | 72 | return this; |
| 69 | 73 | }, |
| 70 | 74 | /** Enables (if yes is truthy) or disables all elements in |
| 71 | 75 | * this.disableDuringAjax. */ |
| 72 | 76 | enableAjaxComponents: function(yes){ |
| | @@ -112,43 +116,141 @@ |
| 112 | 116 | injectMessageElem: function f(e, atEnd){ |
| 113 | 117 | const mip = atEnd ? this.e.loadToolbar : this.e.messageInjectPoint; |
| 114 | 118 | if(atEnd){ |
| 115 | 119 | mip.parentNode.insertBefore(e, mip); |
| 116 | 120 | }else{ |
| 117 | | - if(mip.nextSibling){ |
| 118 | | - mip.parentNode.insertBefore(e, mip.nextSibling); |
| 119 | | - }else{ |
| 120 | | - mip.parentNode.appendChild(e); |
| 121 | + const self = this; |
| 122 | + if(false && this.isUiFlipped()){ |
| 123 | + /* When UI is flipped, new messages start out under the |
| 124 | + text input area because of its position:sticky |
| 125 | + style. We have to scroll them up. When the page footer |
| 126 | + is not hidden but is not on-screen, this causes a |
| 127 | + slight amount of UI jarring as the footer is *also* |
| 128 | + scrolled into view (for whatever reason). |
| 129 | + |
| 130 | + The remaining problem here is messages with IMG tags. |
| 131 | + At this point in the process their IMG.src has not yet |
| 132 | + been loaded - that's async. We scroll the message into |
| 133 | + view, but then the downstream loading of IMG.src pushes |
| 134 | + the message content back down, sliding the message |
| 135 | + behind the input field. This can be verified by delaying the |
| 136 | + message scroll by a second or so to give the image time |
| 137 | + to load (from a local server instance). |
| 138 | + */ |
| 139 | + D.addClass(self.e.inputWrapper,'unsticky'); |
| 140 | + } |
| 141 | + if(mip.nextSibling) mip.parentNode.insertBefore(e, mip.nextSibling); |
| 142 | + else mip.parentNode.appendChild(e); |
| 143 | + if(false && this.isUiFlipped()){ |
| 144 | + //e.scrollIntoView(); |
| 145 | + setTimeout(function(){ |
| 146 | + //self.e.inputWrapper.scrollIntoView(); |
| 147 | + //self.e.fileSelectWrapper.scrollIntoView(); |
| 148 | + //e.scrollIntoView(); |
| 149 | + //D.removeClass(self.e.inputWrapper,'unsticky'); |
| 150 | + self.e.inputWrapper.scrollIntoView(); |
| 151 | + },0); |
| 121 | 152 | } |
| 122 | 153 | } |
| 123 | 154 | }, |
| 155 | + /** Returns true if chat-only mode is enabled. */ |
| 156 | + isChatOnlyMode: ()=>document.body.classList.contains('chat-only-mode'), |
| 157 | + /** Returns true if the UI seems to be in "bottom-up" mode. */ |
| 158 | + isUiFlipped: function(){ |
| 159 | + const style = window.getComputedStyle(this.e.contentDiv); |
| 160 | + return style.flexDirection.indexOf("-reverse")>0; |
| 161 | + }, |
| 162 | + /** |
| 163 | + Enters (if passed a truthy value or no arguments) or leaves |
| 164 | + "chat-only" mode. That mode hides the page's header and |
| 165 | + footer, leaving only the chat application visible to the |
| 166 | + user. |
| 167 | + */ |
| 168 | + chatOnlyMode: function f(yes){ |
| 169 | + if(undefined === f.elemsToToggle){ |
| 170 | + f.elemsToToggle = []; |
| 171 | + document.body.childNodes.forEach(function(e){ |
| 172 | + if(!e.classList) return/*TEXT nodes and such*/; |
| 173 | + else if(!e.classList.contains('content') |
| 174 | + && !e.classList.contains('fossil-PopupWidget') |
| 175 | + /*kludge^^^ for settingsPopup click handling!*/){ |
| 176 | + f.elemsToToggle.push(e); |
| 177 | + } |
| 178 | + }); |
| 179 | + } |
| 180 | + if(!arguments.length) yes = true; |
| 181 | + if(yes === this.isChatOnlyMode()) return this; |
| 182 | + if(yes){ |
| 183 | + D.addClass(f.elemsToToggle, 'hidden'); |
| 184 | + D.addClass(document.body, 'chat-only-mode'); |
| 185 | + document.body.scroll(0,document.body.height); |
| 186 | + }else{ |
| 187 | + D.removeClass(f.elemsToToggle, 'hidden'); |
| 188 | + D.removeClass(document.body, 'chat-only-mode'); |
| 189 | + setTimeout(()=>document.body.scrollIntoView( |
| 190 | + /*moves to (0,0), whereas scrollTo(0,0) does not! |
| 191 | + setTimeout() is unfortunately necessary to get the scroll |
| 192 | + placement correct.*/ |
| 193 | + ), 0); |
| 194 | + } |
| 195 | + const msg = document.querySelector('.message-widget'); |
| 196 | + if(msg) setTimeout(()=>msg.scrollIntoView(),0); |
| 197 | + return this; |
| 198 | + }, |
| 199 | + toggleChatOnlyMode: function(){ |
| 200 | + return this.chatOnlyMode(!this.isChatOnlyMode()); |
| 201 | + }, |
| 124 | 202 | settings:{ |
| 125 | 203 | get: (k,dflt)=>F.storage.get(k,dflt), |
| 126 | 204 | getBool: (k,dflt)=>F.storage.getBool(k,dflt), |
| 127 | 205 | set: (k,v)=>F.storage.set(k,v), |
| 128 | 206 | defaults:{ |
| 129 | 207 | "images-inline": !!F.config.chat.imagesInline, |
| 130 | | - "monospace-messages": false |
| 208 | + "monospace-messages": false, |
| 209 | + "bottom-up": true |
| 131 | 210 | } |
| 132 | 211 | } |
| 133 | 212 | }; |
| 134 | | - Object.keys(cs.settings.defaults).forEach(function f(k){ |
| 135 | | - const v = cs.settings.get(k,f); |
| 136 | | - if(f===v) cs.settings.set(k,cs.settings.defaults[k]); |
| 213 | + /* Install default settings... */ |
| 214 | + Object.keys(cs.settings.defaults).forEach(function(k){ |
| 215 | + const v = cs.settings.get(k,cs); |
| 216 | + if(cs===v) cs.settings.set(k,cs.settings.defaults[k]); |
| 137 | 217 | }); |
| 138 | 218 | if(window.innerWidth<window.innerHeight){ |
| 139 | 219 | /* Alignment of 'my' messages: right alignment is conventional |
| 140 | 220 | for mobile chat apps but can be difficult to read in wide |
| 141 | | - windows (desktop/tablet landscape mode). Can be toggled via |
| 142 | | - settings popup. */ |
| 221 | + windows (desktop/tablet landscape mode), so we default to a |
| 222 | + layout based on the apparently "orientation" of the window: |
| 223 | + tall vs wide. Can be toggled via settings popup. */ |
| 143 | 224 | document.body.classList.add('my-messages-right'); |
| 144 | 225 | } |
| 226 | + if(cs.settings.getBool("bottom-up")){ |
| 227 | + document.body.classList.add('chat-bottom-up'); |
| 228 | + } |
| 145 | 229 | if(cs.settings.getBool('monospace-messages',false)){ |
| 146 | 230 | document.body.classList.add('monospace-messages'); |
| 147 | 231 | } |
| 148 | 232 | cs.e.inputCurrent = cs.e.inputSingle; |
| 149 | 233 | cs.pageTitleOrig = cs.e.pageTitle.innerText; |
| 234 | + |
| 235 | + if(true){ |
| 236 | + /* In order to make the input area opaque, such that the message |
| 237 | + list scrolls under it without being visible, we have to |
| 238 | + ensure that the input area has a non-transparent background |
| 239 | + color. Ideally we'd select the color of div.content, but that |
| 240 | + is not necessarily set, so we fall back to using the body's |
| 241 | + background color and hope it's been explicitly set |
| 242 | + somewhere. If we rely on the input area having its own color |
| 243 | + specified in CSS then all skins would have to define an |
| 244 | + appropriate color. Thus our selection of the body color, |
| 245 | + while slightly unfortunate, is in the interest of keeping |
| 246 | + skins from being forced to define an opaque bg color. |
| 247 | + */ |
| 248 | + const bodyStyle = window.getComputedStyle(document.body); |
| 249 | + cs.e.inputWrapper.style.backgroundColor = bodyStyle.backgroundColor; |
| 250 | + } |
| 251 | + |
| 150 | 252 | const qs = (e)=>document.querySelector(e); |
| 151 | 253 | const argsToArray = function(args){ |
| 152 | 254 | return Array.prototype.slice.call(args,0); |
| 153 | 255 | }; |
| 154 | 256 | cs.reportError = function(/*msg args*/){ |
| | @@ -265,12 +367,15 @@ |
| 265 | 367 | this.e.content.style.backgroundColor = m.uclr; |
| 266 | 368 | this.e.tab.style.backgroundColor = m.uclr; |
| 267 | 369 | |
| 268 | 370 | const d = new Date(m.mtime); |
| 269 | 371 | D.append( |
| 270 | | - D.clearElement(this.e.tab), D.text( |
| 271 | | - m.xfrom+' @ '+d.getHours()+":"+(d.getMinutes()+100).toString().slice(1,3)) |
| 372 | + D.clearElement(this.e.tab), |
| 373 | + D.text( |
| 374 | + m.xfrom," #",m.msgid,' @ ',d.getHours(),":", |
| 375 | + (d.getMinutes()+100).toString().slice(1,3) |
| 376 | + ) |
| 272 | 377 | ); |
| 273 | 378 | var contentTarget = this.e.content; |
| 274 | 379 | if( m.fsize>0 ){ |
| 275 | 380 | if( m.fmime |
| 276 | 381 | && m.fmime.startsWith("image/") |
| | @@ -285,14 +390,17 @@ |
| 285 | 390 | "(" + m.fname + " " + m.fsize + " bytes)" |
| 286 | 391 | ) |
| 287 | 392 | D.attr(a,'target','_blank'); |
| 288 | 393 | contentTarget.appendChild(a); |
| 289 | 394 | } |
| 290 | | - contentTarget = D.div(); |
| 395 | + ; |
| 291 | 396 | } |
| 292 | 397 | if(m.xmsg){ |
| 293 | | - if(contentTarget !== this.e.content){ |
| 398 | + if(m.fsize>0){ |
| 399 | + /* We have file/image content, so need another element for |
| 400 | + the message text. */ |
| 401 | + contentTarget = D.div(); |
| 294 | 402 | D.append(this.e.content, contentTarget); |
| 295 | 403 | } |
| 296 | 404 | // The m.xmsg text comes from the same server as this script and |
| 297 | 405 | // is guaranteed by that server to be "safe" HTML - safe in the |
| 298 | 406 | // sense that it is not possible for a malefactor to inject HTML |
| | @@ -361,11 +469,11 @@ |
| 361 | 469 | } |
| 362 | 470 | }, false); |
| 363 | 471 | /* Add help button for drag/drop/paste zone */ |
| 364 | 472 | Chat.e.inputFile.parentNode.insertBefore( |
| 365 | 473 | F.helpButtonlets.create( |
| 366 | | - document.querySelector('#chat-input-file-area .help-buttonlet') |
| 474 | + Chat.e.fileSelectWrapper.querySelector('.help-buttonlet') |
| 367 | 475 | ), Chat.e.inputFile |
| 368 | 476 | ); |
| 369 | 477 | //////////////////////////////////////////////////////////// |
| 370 | 478 | // File drag/drop visual notification. |
| 371 | 479 | const dropHighlight = Chat.e.inputFile /* target zone */; |
| | @@ -490,11 +598,11 @@ |
| 490 | 598 | Chat.deleteMessage(eMsg); |
| 491 | 599 | }); |
| 492 | 600 | } |
| 493 | 601 | }/*refresh()*/ |
| 494 | 602 | }); |
| 495 | | - f.popup.installClickToHide(); |
| 603 | + f.popup.installHideHandlers(); |
| 496 | 604 | f.popup.hide = function(){ |
| 497 | 605 | delete this._eMsg; |
| 498 | 606 | D.clearElement(this.e); |
| 499 | 607 | return this.show(false); |
| 500 | 608 | }; |
| | @@ -517,15 +625,11 @@ |
| 517 | 625 | |
| 518 | 626 | (function(){/*Set up #chat-settings-button */ |
| 519 | 627 | const settingsButton = document.querySelector('#chat-settings-button'); |
| 520 | 628 | var popupSize = undefined/*placement workaround*/; |
| 521 | 629 | const settingsPopup = new F.PopupWidget({ |
| 522 | | - cssClass: ['fossil-tooltip', 'chat-settings-popup'], |
| 523 | | - adjustY: function(y){ |
| 524 | | - const rect = settingsButton.getBoundingClientRect(); |
| 525 | | - return rect.top + rect.height + 2; |
| 526 | | - } |
| 630 | + cssClass: ['fossil-tooltip', 'chat-settings-popup'] |
| 527 | 631 | }); |
| 528 | 632 | /* Settings menu entries... */ |
| 529 | 633 | const settingsOps = [{ |
| 530 | 634 | label: "Multi-line input", |
| 531 | 635 | boolValue: ()=>Chat.inputElement()===Chat.e.inputMulti, |
| | @@ -540,56 +644,28 @@ |
| 540 | 644 | Chat.settings.set('monospace-messages', |
| 541 | 645 | document.body.classList.contains('monospace-messages')); |
| 542 | 646 | } |
| 543 | 647 | },{ |
| 544 | 648 | label: "Chat-only mode", |
| 545 | | - boolValue: ()=>!!document.body.classList.contains('chat-only-mode'), |
| 546 | | - callback: function f(){ |
| 547 | | - if(undefined === f.isHidden){ |
| 548 | | - f.isHidden = false; |
| 549 | | - f.elemsToToggle = []; |
| 550 | | - document.body.childNodes.forEach(function(e){ |
| 551 | | - if(!e.classList) return/*TEXT nodes and such*/; |
| 552 | | - else if(!e.classList.contains('content') |
| 553 | | - && !e.classList.contains('fossil-PopupWidget') |
| 554 | | - /*kludge^^^ for settingsPopup click handling!*/){ |
| 555 | | - f.elemsToToggle.push(e); |
| 556 | | - } |
| 557 | | - }); |
| 558 | | - /* In order to make the input area opaque, such that the |
| 559 | | - message list scrolls under it without being visible, we |
| 560 | | - have to ensure that the input area has a non-inherited |
| 561 | | - background color. Ideally we'd select the color of |
| 562 | | - div.content, but that is not necessarily set, so we fall |
| 563 | | - back to using the body's background color. If we rely on |
| 564 | | - the input area having its own color specified in CSS then |
| 565 | | - all skins would have to define an appropriate color. |
| 566 | | - Thus our selection of the body color, while slightly unfortunate, |
| 567 | | - is in the interest of keeping skins from being forced to |
| 568 | | - define an opaque bg color. |
| 569 | | - */ |
| 570 | | - f.initialBg = Chat.e.messagesWrapper.style.backgroundColor; |
| 571 | | - const cs = window.getComputedStyle(document.body); |
| 572 | | - f.inheritedBg = cs.backgroundColor; |
| 573 | | - } |
| 574 | | - const iws = Chat.e.inputWrapper.style; |
| 575 | | - if((f.isHidden = !f.isHidden)){ |
| 576 | | - D.addClass(f.elemsToToggle, 'hidden'); |
| 577 | | - D.addClass(document.body, 'chat-only-mode'); |
| 578 | | - iws.backgroundColor = f.inheritedBg; |
| 579 | | - }else{ |
| 580 | | - D.removeClass(f.elemsToToggle, 'hidden'); |
| 581 | | - D.removeClass(document.body, 'chat-only-mode'); |
| 582 | | - iws.backgroundColor = f.initialBg; |
| 583 | | - } |
| 649 | + boolValue: ()=>Chat.isChatOnlyMode(), |
| 650 | + callback: function(){ |
| 651 | + Chat.toggleChatOnlyMode(); |
| 584 | 652 | } |
| 585 | 653 | },{ |
| 586 | 654 | label: "Left-align my posts", |
| 587 | 655 | boolValue: ()=>!document.body.classList.contains('my-messages-right'), |
| 588 | 656 | callback: function f(){ |
| 589 | 657 | document.body.classList.toggle('my-messages-right'); |
| 590 | 658 | } |
| 659 | + },{ |
| 660 | + label: "Bottom-up chat", |
| 661 | + boolValue: ()=>document.body.classList.contains('chat-bottom-up'), |
| 662 | + callback: function(){ |
| 663 | + document.body.classList.toggle('chat-bottom-up'); |
| 664 | + Chat.settings.set('bottom-up', |
| 665 | + document.body.classList.contains('chat-bottom-up')); |
| 666 | + } |
| 591 | 667 | },{ |
| 592 | 668 | label: "Images inline", |
| 593 | 669 | boolValue: ()=>Chat.settings.getBool('images-inline'), |
| 594 | 670 | callback: function(){ |
| 595 | 671 | const v = Chat.settings.getBool('images-inline',true); |
| | @@ -620,33 +696,43 @@ |
| 620 | 696 | } |
| 621 | 697 | D.append(settingsPopup.e, line); |
| 622 | 698 | btn.addEventListener('click', callback); |
| 623 | 699 | }); |
| 624 | 700 | }; |
| 625 | | - /** |
| 626 | | - Reminder: |
| 627 | | - settingsPopup.installClickToHide(); |
| 628 | | - Don't do this for this popup! It interferes with the embedded |
| 629 | | - "?" buttons in the popup, which are also PopupWidget users. |
| 630 | | - */ |
| 701 | + settingsPopup.installHideHandlers(false, true, true) |
| 702 | + /** Reminder: click-to-hide interferes with "?" embedded within |
| 703 | + the popup, so cannot be used together with those. Enabling |
| 704 | + this means, however, that tapping the menu button to toggle |
| 705 | + the menu cannot work because tapping the menu button while the |
| 706 | + menu is opened will, because of the click-to-hide handler, |
| 707 | + hide the menu before the button gets an event saying to toggle |
| 708 | + it.*/; |
| 631 | 709 | D.attr(settingsButton, 'role', 'button'); |
| 632 | 710 | settingsButton.addEventListener('click',function(ev){ |
| 633 | 711 | //ev.preventDefault(); |
| 634 | 712 | if(settingsPopup.isShown()) settingsPopup.hide(); |
| 635 | 713 | else settingsPopup.show(settingsButton); |
| 636 | 714 | /* Reminder: we cannot toggle the visibility from her |
| 637 | 715 | */ |
| 638 | 716 | }, false); |
| 639 | 717 | |
| 640 | | - /* Find an ideal X position for the popup, directly under the settings |
| 718 | + /* Find an ideal X/Y position for the popup, directly above the settings |
| 641 | 719 | button, based on the size of the popup... */ |
| 642 | 720 | settingsPopup.show(document.body); |
| 643 | 721 | popupSize = settingsPopup.e.getBoundingClientRect(); |
| 644 | 722 | settingsPopup.hide(); |
| 645 | 723 | settingsPopup.options.adjustX = function(x){ |
| 646 | 724 | const rect = settingsButton.getBoundingClientRect(); |
| 647 | 725 | return rect.right - popupSize.width; |
| 726 | + }; |
| 727 | + settingsPopup.options.adjustY = function(y){ |
| 728 | + const rect = settingsButton.getBoundingClientRect(); |
| 729 | + if(Chat.isUiFlipped()){ |
| 730 | + return rect.top - popupSize.height -2; |
| 731 | + }else{ |
| 732 | + return rect.bottom + 2; |
| 733 | + } |
| 648 | 734 | }; |
| 649 | 735 | })()/*#chat-settings-button setup*/; |
| 650 | 736 | |
| 651 | 737 | |
| 652 | 738 | /** Callback for poll() to inject new content into the page. jx == |
| | @@ -756,14 +842,17 @@ |
| 756 | 842 | .catch(e=>console.error(e)) |
| 757 | 843 | /* ^^^ we don't use Chat.reportError(e) here b/c the polling |
| 758 | 844 | fails exepectedly when it times out, but is then immediately |
| 759 | 845 | resumed, and reportError() produces a loud error message. */ |
| 760 | 846 | .finally(function(x){ |
| 761 | | - if(isFirstCall) Chat.ajaxEnd(); |
| 847 | + if(isFirstCall){ |
| 848 | + Chat.ajaxEnd(); |
| 849 | + Chat.e.inputWrapper.scrollIntoView(); |
| 850 | + } |
| 762 | 851 | poll.running=false; |
| 763 | 852 | }); |
| 764 | 853 | } |
| 765 | 854 | poll.running = false; |
| 766 | 855 | poll(true); |
| 767 | 856 | setInterval(poll, 1000); |
| 768 | 857 | F.page.chat = Chat/* enables testing the APIs via the dev tools */; |
| 769 | 858 | })(); |
| 770 | 859 | |