Diff
Not logged in

Differences From Artifact [df7bc9ff86]:

To Artifact [e524dedd4d]:


136
137
138
139
140
141
142
143


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166





















167
168
169
170
171
172
173
136
137
138
139
140
141
142

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162





163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190







-
+
+


















-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







        contentDiv: E1('div.content'),
        viewConfig: E1('#chat-config'),
        viewPreview: E1('#chat-preview'),
        previewContent: E1('#chat-preview-content'),
        btnPreview: E1('#chat-preview-button'),
        views: document.querySelectorAll('.chat-view'),
        activeUserListWrapper: E1('#chat-user-list-wrapper'),
        activeUserList: E1('#chat-user-list')
        activeUserList: E1('#chat-user-list'),
        btnClearFilter: E1('#chat-clear-filter')
      },
      me: F.user.name,
      mxMsg: F.config.chat.initSize ? -F.config.chat.initSize : -50,
      mnMsg: undefined/*lowest message ID we've seen so far (for history loading)*/,
      pageIsActive: 'visible'===document.visibilityState,
      changesSincePageHidden: 0,
      notificationBubbleColor: 'white',
      totalMessageCount: 0, // total # of inbound messages
      //! Number of messages to load for the history buttons
      loadMessageCount: Math.abs(F.config.chat.initSize || 20),
      ajaxInflight: 0,
      usersLastSeen:{
        /* Map of user names to their most recent message time
           (JS Date object). Only messages received by the chat client
           are considered. */
        /* Reminder: to convert a Julian time J to JS:
           new Date((J - 2440587.5) * 86400000) */
      },
      filterState:{
        activeUser: undefined,
        match: function(uname){
          return this.activeUser===uname || !this.activeUser;
        }
      filter: {
        user:{
          activeTag: undefined,
          match: function(uname){
            return !this.activeTag || this.activeTag===uname;
          },
          matchElem: function(e){
            return !this.activeTag || this.activeTag===e.dataset.xfrom;
          }
        },
        hashtag:{
          activeTag: undefined,
          match: function(tag){
            return !this.activeTag || tag===this.activeTag;
          },
          matchElem: function(e){
            return !this.activeTag
              || !!e.querySelector('[data-hashtag='+this.activeTag+']');
          }
        },
        current: undefined/*gets set to current active filter*/
      },
      /** Gets (no args) or sets (1 arg) the current input text field value,
          taking into account single- vs multi-line input. The getter returns
          a string and the setter returns this object. */
      inputValue: function(){
        const e = this.inputElement();
        if(arguments.length){
278
279
280
281
282
283
284
285


286
287
288
289
290
291
292
295
296
297
298
299
300
301

302
303
304
305
306
307
308
309
310







-
+
+







      /* Injects DOM element e as a new row in the chat, at the oldest
         end of the list if atEnd is truthy, else at the newest end of
         the list. */
      injectMessageElem: function f(e, atEnd){
        const mip = atEnd ? this.e.loadOlderToolbar : this.e.messageInjectPoint,
              holder = this.e.viewMessages,
              prevMessage = this.e.newestMessage;
        if(!this.filterState.match(e.dataset.xfrom)){
        if(this.filter.current
           && !this.filter.current.matchElem(e)){
          e.classList.add('hidden');
        }
        if(atEnd){
          const fe = mip.nextElementSibling;
          if(fe) mip.parentNode.insertBefore(e, fe);
          else D.append(mip.parentNode, e);
        }else{
476
477
478
479
480
481
482
483

484
485
486
487
488
489
490
494
495
496
497
498
499
500

501
502
503
504
505
506
507
508







-
+







            else if(l) return -1;
            else if(r) return 1;
            else return 0;
          };
          callee.addUserElem = function(u){
            const uSpan = D.addClass(D.span(), 'chat-user');
            const uDate = self.usersLastSeen[u];
            if(self.filterState.activeUser===u){
            if(self.filter.user.activeTag===u){
              uSpan.classList.add('selected');
            }
            uSpan.dataset.uname = u;
            D.append(uSpan, u, "\n", 
                     D.append(
                       D.addClass(D.span(),'timestamp'),
                       localTimeString(uDate)//.substr(5/*chop off year*/)
498
499
500
501
502
503
504
505




































































506
507
508
509
510
511







512
513
514
515
516
517
518
519
520




521
522
523
524
525












526
527
528

529
530
531


532
533
534
535
536
537
538
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595


596
597
598
599
600
601
602
603








604
605
606
607





608
609
610
611
612
613
614
615
616
617
618
619
620


621



622
623
624
625
626
627
628
629
630








+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+




-
-
+
+
+
+
+
+
+

-
-
-
-
-
-
-
-
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+

-
-
+
-
-
-
+
+







        //D.clearElement(this.e.activeUserList);
        D.remove(this.e.activeUserList.querySelectorAll('.chat-user'));
        Object.keys(this.usersLastSeen).sort(
          callee.sortUsersSeen
        ).forEach(callee.addUserElem);
        return this;
      },
      /**
         For each Chat.MessageWidget element (X.message-widget) for
         which predicate(elem) returns true, the 'hidden' class is
         removed from that message. For all others, 'hidden' is
         added. If predicate is falsy, 'hidden' is removed from all
         elements. After filtering, it will try to scroll the last
         not-filtered-out message into view, but exactly where it
         scrolls into view (top, middle, button) is
         unpredictable. Returns this object.

         The argument may optionally be an object from this.filter,
         in which case its matchElem() method becomes the predicate.

         Note that this does not encapsulate certain filter-specific
         logic which applies changes to elements other than the
         main message list or this.e.btnClearFilter.
      */
      applyMessageFilter: function(predicate){
        const self = this;
        let eLast;
        console.debug("applyMessageFilter(",predicate,")");
        if(!predicate){
          D.removeClass(this.e.viewMessages.querySelectorAll('.message-widget.hidden'),
                        'hidden');
          D.addClass(this.e.btnClearFilter, 'hidden');
        }else if('function'!==typeof predicate
                && predicate.matchElem){
          /* assume Chat.filter object */
          const p = predicate;
          predicate = (e)=>p.matchElem(e);
        }
        if(predicate){
          this.e.viewMessages.querySelectorAll('.message-widget').forEach(function(e){
            if(predicate(e)){
              e.classList.remove('hidden');
              eLast = e;
            }else{
              e.classList.add('hidden');
            }
          });
          D.removeClass(this.e.btnClearFilter, 'hidden');
        }
        if(eLast) eLast.scrollIntoView(false);
        else this.scrollMessagesTo(1);
        return this;
      },
      /**
         Clears the current message filter, if any, and clears the
         activeTag property of all members of this.filter. Returns
         this object. This also unfortunately performs some
         filter-type-specific logic which we have not yet managed to
         encapsulate more cleanly.
       */
      clearFilters: function(){
        if(!this.filter.current) return this;
        this.filter.current = undefined;
        this.applyMessageFilter(false);
        const self = this;
        Object.keys(this.filter).forEach(function(k){
          const f = self.filter[k];
          if(f) f.activeTag = undefined;
        });
        this.e.activeUserList.querySelectorAll('.chat-user').forEach(
          /*Unfortante filter-specific logic*/
          (e)=>e.classList.remove('selected')
        );
        return this;
      },
      /**
         Applies user name filter to all current messages, or clears
         the filter if uname is falsy.
      */
      setUserFilter: function(uname){
        this.filterState.activeUser = uname;
        const mw = this.e.viewMessages.querySelectorAll('.message-widget');
        if(!uname || (this.filter.current
                      && this.filter.current!==this.filter.user)){
          this.clearFilters();
        }
        this.filter.user.activeTag = uname;
        if(uname) this.applyMessageFilter(this.filter.user);
        this.filter.current = uname ? this.filter.user : undefined;
        const self = this;
        let eLast;
        if(!uname){
          D.removeClass(Chat.e.viewMessages.querySelectorAll('.message-widget.hidden'),
                        'hidden');
        }else{
          mw.forEach(function(w){
            if(self.filterState.match(w.dataset.xfrom)){
              w.classList.remove('hidden');
        this.e.activeUserList.querySelectorAll('.chat-user').forEach(function(e){
          e.classList[
            self.filter.user.activeTag===e.dataset.uname
              ? 'add' : 'remove'
              eLast = w;
            }else{
              w.classList.add('hidden');
            }
          });
          ]('selected');
        });
        return this;
      },
      /**
         Applies a hashtag filter to all current messages, or clears
         the filter if tag is falsy.
      */
      setHashtagFilter: function(tag){
        if(!tag || (this.filter.current
                    && this.filter.current!==this.filter.hashtag)){
          this.clearFilters();
        }
        if(eLast) eLast.scrollIntoView(false);
        else this.scrollMessagesTo(1);
        this.filter.hashtag.activeTag = tag;
        cs.e.activeUserList.querySelectorAll('.chat-user').forEach(function(e){
          e.classList[uname===e.dataset.uname ? 'add' : 'remove']('selected');
        });
        if(tag) this.applyMessageFilter(this.filter.hashtag);
        this.filter.current = tag ? this.filter.hashtag : undefined;
        return this;
      },

      /**
         If animations are enabled, passes its arguments
         to D.addClassBriefly(), else this is a no-op.
         If cb is a function, it is called after the
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795





796
797






















798
799
800
801
802
803
804
872
873
874
875
876
877
878

879


880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920







-

-
-





+
+
+
+
+


+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







      const uname = eUser.dataset.uname;
      let eLast;
      cs.setCurrentView(cs.e.viewMessages);
      if(eUser.classList.contains('selected')){
        /* If curently selected, toggle filter off */
        eUser.classList.remove('selected');
        cs.setUserFilter(false);
        delete f.$eSelected;
      }else{
        if(f.$eSelected) f.$eSelected.classList.remove('selected');
        f.$eSelected = eUser;
        eUser.classList.add('selected');
        cs.setUserFilter(uname);
      }
      return false;
    }, false);

    cs.e.btnClearFilter.addEventListener('click',function(){
      D.addClass(this,'hidden');
      cs.clearFilters();
    }, false);
    return cs;
  })()/*Chat initialization*/;

  /** To be passed each MessageWidget's top-level DOM element
      after initial processing of the message, to set up
      hashtag references. */
  const setupHashtags = function f(elem){
    if(!f.$click){
      f.$click = function(ev){
        const tag = ev.target.dataset.hashtag;
        if(tag){
          console.debug("hashtag = ",tag);
          Chat.setHashtagFilter(
            tag===Chat.filter.hashtag.activeTag
              ? false : tag
          );
        }
      };
    }
    elem.querySelectorAll('[data-hashtag]').forEach(function(e){
      e.dataset.hashtag = e.dataset.hashtag.toLowerCase();
      e.addEventListener('click', f.$click, false);
    })
  };

  /**
     Custom widget type for rendering messages (one message per
     instance). These are modelled after FIELDSET elements but we
     don't use FIELDSET because of cross-browser inconsistencies in
     features of the FIELDSET/LEGEND combination, e.g. inability to
     align legends via CSS in Firefox and clicking-related
913
914
915
916
917
918
919

920
921
922
923
924
925
926
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043







+







          // perfectly safe to use in this context.
          if(m.xmsg instanceof Array){
            // Used by Chat.reportErrorAsMessage()
            D.append(contentTarget, m.xmsg);
          }else{
            contentTarget.innerHTML = m.xmsg;
            contentTarget.querySelectorAll('a').forEach(addAnchorTargetBlank);
            setupHashtags(contentTarget);
            if(F.pikchr){
              F.pikchr.addSrcView(contentTarget.querySelectorAll('svg.pikchr'));
            }
          }
        }
        this.e.tab.firstElementChild.addEventListener('click', this._handleLegendClicked, false);
        /*if(eXFrom){
997
998
999
1000
1001
1002
1003
1004

1005
1006

1007
1008
1009
1010
1011
1012
1013
1014
1015
1016

1017
1018
1019

1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1114
1115
1116
1117
1118
1119
1120

1121


1122
1123
1124
1125
1126
1127
1128
1129
1130
1131

1132
1133


1134





1135
1136
1137
1138
1139
1140
1141







-
+
-
-
+









-
+

-
-
+
-
-
-
-
-







                  D.a(F.repoUrl('timeline',{
                    u: eMsg.dataset.xfrom,
                    y: 'a'
                  }), "User's Timeline"),
                  'target', '_blank'
                );
                D.append(toolbar2, timelineLink);
                if(Chat.filterState.activeUser &&
                if(Chat.filter.current){
                   Chat.filterState.match(eMsg.dataset.xfrom)){
                  /* Add a button to clear user filter and jump to
                  /* Add a button to clear filter and jump to
                     this message in its original context. */
                  D.append(
                    this.e,
                    D.append(
                      D.addClass(D.div(), 'toolbar'),
                      D.button(
                        "Message in context",
                        function(){
                          self.hide();
                          Chat.setUserFilter(false);
                          Chat.clearFilters();
                          eMsg.scrollIntoView(false);
                          Chat.animate(
                            eMsg.firstElementChild, 'anim-flip-h'
                          Chat.animate(eMsg.firstElementChild, 'anim-flip-h');
                            //eMsg.firstElementChild, 'anim-flip-v'
                            //eMsg.childNodes, 'anim-rotate-360'
                            //eMsg.childNodes, 'anim-flip-v'
                            //eMsg, 'anim-flip-v'
                          );
                        })
                    )
                  );
                }/*jump-to button*/
              }
              const tab = eMsg.querySelector('.message-widget-tab');
              D.append(tab, this.e);
1239
1240
1241
1242
1243
1244
1245
1246
1247




1248
1249
1250
1251
1252
1253
1254
1349
1350
1351
1352
1353
1354
1355


1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366







-
-
+
+
+
+







        label: "Show active users list",
        boolValue: ()=>!Chat.e.activeUserListWrapper.classList.contains('hidden'),
        persistentSetting: 'active-user-list',
        callback: function(){
          D.toggleClass(Chat.e.activeUserListWrapper,'hidden');
          D.removeClass(Chat.e.activeUserListWrapper, 'collapsed');
          if(Chat.e.activeUserListWrapper.classList.contains('hidden')){
            /* When hiding this element, undo all filtering */
            Chat.setUserFilter(false);
            /* When hiding this element, undo user filtering */
            if(Chat.filter.current === Chat.filter.user){
              Chat.setUserFilter(false);
            }
            /*Ideally we'd scroll the final message into view
              now, but because viewMessages is currently hidden behind
              viewConfig, scrolling is a no-op. */
            Chat.scrollMessagesTo(1);
          }else{
            Chat.updateActiveUserList();
            Chat.animate(Chat.e.activeUserListWrapper, 'anim-flip-v');