cms/app-client/app/components/transcript-component.js
author ymh <ymh.work@gmail.com>
Fri, 02 Dec 2016 13:25:08 +0100
changeset 457 a7a333c0b1eb
parent 455 a8bed1c7df8e
child 459 6a296e92887f
permissions -rw-r--r--
Optimize transcript rendering : remove call to components
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
245
c9dd78a43b07 Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
     1
import Ember from 'ember';
455
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
     2
import IntervalTree from 'interval-tree';
245
c9dd78a43b07 Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
     3
c9dd78a43b07 Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
     4
export default Ember.Component.extend({
c9dd78a43b07 Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
     5
246
5b7ae96768be Bsic transcript-component design
Chloe Laisne <chloe.laisne@gmail.com>
parents: 245
diff changeset
     6
    classNames: ['transcript-component'],
5b7ae96768be Bsic transcript-component design
Chloe Laisne <chloe.laisne@gmail.com>
parents: 245
diff changeset
     7
5b7ae96768be Bsic transcript-component design
Chloe Laisne <chloe.laisne@gmail.com>
parents: 245
diff changeset
     8
    player: Ember.inject.service(),
5b7ae96768be Bsic transcript-component design
Chloe Laisne <chloe.laisne@gmail.com>
parents: 245
diff changeset
     9
345
4b66390442fd Cancel auto scroll on manual scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    10
    autoscrollElement : '.corpus-app-container',
4b66390442fd Cancel auto scroll on manual scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    11
    previousElement: 0,
246
5b7ae96768be Bsic transcript-component design
Chloe Laisne <chloe.laisne@gmail.com>
parents: 245
diff changeset
    12
    autoscroll: true,
247
7a5d729992b8 Design and animate transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 246
diff changeset
    13
    timeout: null,
246
5b7ae96768be Bsic transcript-component design
Chloe Laisne <chloe.laisne@gmail.com>
parents: 245
diff changeset
    14
457
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    15
    transcript: Ember.computed.readOnly('player.transcript'),
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    16
    hasSections: Ember.computed('transcript', function() {
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    17
      var sections = this.get('transcript.sections');
454
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    18
      return sections && sections.length > 0;
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    19
    }),
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    20
455
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    21
    didReceiveAttrs() {
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    22
      this._super(...arguments);
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    23
    },
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    24
246
5b7ae96768be Bsic transcript-component design
Chloe Laisne <chloe.laisne@gmail.com>
parents: 245
diff changeset
    25
    itemObserver: Ember.observer('player.item', function () {
454
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    26
      if ( !(this.get('isDestroyed') || this.get('isDestroying')) ) {
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    27
          this.set('autoscroll', true);
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    28
      }
246
5b7ae96768be Bsic transcript-component design
Chloe Laisne <chloe.laisne@gmail.com>
parents: 245
diff changeset
    29
    }),
245
c9dd78a43b07 Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
    30
455
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    31
    didInsertElement() {
345
4b66390442fd Cancel auto scroll on manual scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    32
        Ember.$('#' + this.elementId).parent().on('scroll', Ember.run.bind(this, this.onScroll));
457
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    33
        var intervals = new IntervalTree(this.get('player.model.duration_ms')/2);
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    34
        if(this.get('hasSections')) {
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    35
          this.get('transcript.sections').forEach((section, indexSection) => {
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    36
            section.turns.forEach((turn, indexTurn) => {
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    37
              let sentenceId = `#sentence-${indexSection}-${indexTurn}`;
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    38
              intervals.add(turn.begin ,turn.end , sentenceId/*, this.$(sentenceId)*/);
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    39
            });
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    40
          });
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    41
        } else {
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    42
          this.get('transcript.annotations').forEach((annotation, index) => {
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    43
            let sentenceId = `#sentence-${index}`;
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    44
            intervals.add(annotation.begin ,annotation.end , sentenceId/*, this.$(sentenceId)*/);
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    45
          });
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    46
        }
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    47
        this.set('intervals', intervals);
345
4b66390442fd Cancel auto scroll on manual scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    48
    },
4b66390442fd Cancel auto scroll on manual scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    49
455
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    50
    onScroll() {
454
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    51
        if ( !(this.get('isDestroyed') || this.get('isDestroying')) ) {
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    52
          this.set('autoscroll', false);
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    53
        }
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    54
        if(this.elementId) {
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    55
          Ember.$('#' + this.elementId).parent().off('scroll');
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    56
        }
345
4b66390442fd Cancel auto scroll on manual scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    57
    },
4b66390442fd Cancel auto scroll on manual scroll
Chloe Laisne <chloe.laisne@gmail.com>
parents: 257
diff changeset
    58
454
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    59
    scroll: Ember.observer('player.progress', function() {
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    60
      var self = this;
455
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    61
      var progress = this.get('player.progress');
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    62
      var previousElement = this.get('previousElement');
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    63
      if(previousElement && progress >= previousElement.start && progress < previousElement.end) {
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    64
        return;
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    65
      }
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    66
      var intervals = this.get('intervals');
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    67
      if(!intervals) {
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    68
        return;
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    69
      }
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    70
      var intervalList = intervals.search(progress);
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    71
      if(intervalList.length === 0) {
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    72
        return;
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    73
      }
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    74
      var target = intervalList[0];
457
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    75
      target.object = this.$(target.id);
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    76
      let previousDomElement = this.$('.sentence.active');
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    77
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    78
      if(previousDomElement) {
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    79
        previousDomElement.removeClass('active');
a7a333c0b1eb Optimize transcript rendering : remove call to components
ymh <ymh.work@gmail.com>
parents: 455
diff changeset
    80
        Ember.$(".fa-play",previousDomElement).show();
455
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    81
      }
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    82
      target.object.addClass('active');
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    83
      Ember.$(".fa-play",target.object).hide();
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    84
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    85
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    86
      if(this.get('autoscroll') && target.object.length && (!previousElement || target.id !== previousElement.id)) {
454
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    87
          Ember.$('#' + self.elementId).parent().off('scroll');
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    88
          Ember.$(this.get('autoscrollElement')).animate({
455
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    89
              scrollTop: target.object.offset().top + Ember.$(this.get('autoscrollElement')).scrollTop() - Ember.$(this.get('autoscrollElement')).offset().top - 154
454
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    90
          }, 150, 'swing', function() {
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    91
              setTimeout(function() {
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    92
                  Ember.$('#' + self.elementId).parent().on('scroll', Ember.run.bind(self, self.onScroll));
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    93
              }, 100);
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    94
          });
455
a8bed1c7df8e move the transcript highlight and autoscroll logic to the base component
ymh <ymh.work@gmail.com>
parents: 454
diff changeset
    95
          this.set('previousElement', target);
454
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    96
      }
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    97
    }),
710a2ae08a74 rework transcript component to correct various bugs
ymh <ymh.work@gmail.com>
parents: 347
diff changeset
    98
246
5b7ae96768be Bsic transcript-component design
Chloe Laisne <chloe.laisne@gmail.com>
parents: 245
diff changeset
    99
247
7a5d729992b8 Design and animate transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 246
diff changeset
   100
    actions: {
7a5d729992b8 Design and animate transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 246
diff changeset
   101
7a5d729992b8 Design and animate transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents: 246
diff changeset
   102
        play: function(progress) {
249
e4204b1a579a Quickfix micro-cuts
Chloe Laisne <chloe.laisne@gmail.com>
parents: 248
diff changeset
   103
            this.get('player').trigger('progressupdate', progress);
248
256272c33349 Transcript sentence lick and play
Chloe Laisne <chloe.laisne@gmail.com>
parents: 247
diff changeset
   104
            this.get('player').set('playing', true);
246
5b7ae96768be Bsic transcript-component design
Chloe Laisne <chloe.laisne@gmail.com>
parents: 245
diff changeset
   105
        }
5b7ae96768be Bsic transcript-component design
Chloe Laisne <chloe.laisne@gmail.com>
parents: 245
diff changeset
   106
    }
245
c9dd78a43b07 Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
   107
c9dd78a43b07 Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
   108
});