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