cms/app-client/app/components/player-sound-control.js
author ymh <ymh.work@gmail.com>
Mon, 19 Dec 2016 21:58:02 +0100
changeset 477 ce52f0fca330
child 480 814468b0fc69
permissions -rw-r--r--
add volume control to the player
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
477
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
import Ember from 'ember';
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
import * as d3s from 'd3-scale';
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
const speakerClassScale = d3s.scaleQuantize()
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
  .domain([0, 1])
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
  .range(['fa-volume-down', 'fa-volume-up']);
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
export default Ember.Component.extend({
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
  classNames: ['player-sound-control'],
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
  popcorn: null,
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
  volume: Ember.computed('popcorn', {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
    get: function() {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
      let popcorn = this.get('popcorn');
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
      if(!popcorn) {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
        return 0;
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
      }
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
      return popcorn.volume();
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    },
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    set: function(key, value) {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
      let popcorn = this.get('popcorn');
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
      if(!popcorn) {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
        return 0;
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
      }
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
      const newValue = Math.min(1.0, Math.max(0, value));
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
      popcorn.volume(newValue);
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
      return newValue;
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
    }
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
  }),
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
  muted: Ember.computed('popcorn', {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
    get: function() {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
      let popcorn = this.get('popcorn');
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
      if(!popcorn) {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
        return false;
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
      }
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
      return popcorn.muted();
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
    },
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
    set: function(key, value) {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
      let popcorn = this.get('popcorn');
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
      if(!popcorn) {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        return false;
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
      }
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
      if(value) {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
        popcorn.mute();
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
      } else {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
        popcorn.unmute();
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
      }
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
      return value;
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
    }
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
  }),
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
  popcornObserver: Ember.observer('popcorn', function() {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
    let popcorn = this.get('popcorn');
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
    if(!popcorn) {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
      return;
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
    }
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
    popcorn.on('volumechange', () => {  this.notifyPropertyChange('volume'); this.notifyPropertyChange('muted'); });
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
  }),
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
  speakerClass: Ember.computed('volume', 'muted', function() {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
    const volume = this.get('volume');
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
    const muted = this.get('muted');
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
    if(muted || volume === 0 ) {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
      return "fa-volume-off";
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
    } else {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
      return speakerClassScale(volume);
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
    }
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
  }),
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
  mouseEnter: function() {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
    let baseOffset = this.$("#sound-control-speaker").offset();
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
    if(this.get('muted')) {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
      return;
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
    }
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
    this.$("#sound-control-scale").show(400);
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
    this.$("#sound-control-scale").offset({ top: baseOffset.top + 55, left: baseOffset.left});
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
  },
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
  mouseLeave: function() {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
    this.$("#sound-control-scale").hide(400);
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
  },
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
  didInsertElement: function() {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
    this.set('volumeSliderHandler', this.$(".volume-slider").on("input change", (event) => { this.set('volume',Ember.$(event.target).val()); }));
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
  },
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
  willDestroyElement: function() {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
    let volumeSliderHandler = this.get('volumeSliderHandler');
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
    if(volumeSliderHandler) {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
      this.$(".volume-slider").off({"input change": volumeSliderHandler});
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
    }
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
  },
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
  actions: {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
    muteToggle() {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
      this.set('muted', !this.get('muted'));
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
    },
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
    clickMinus() {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
      if(this.get('muted')) {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
        return;
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
      }
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
      let volume = this.get('volume');
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
      this.set('volume', volume - 0.1);
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
    },
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
    clickPlus() {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
      if(this.get('muted')) {
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
        return;
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
      }
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
      let volume = this.get('volume');
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
      this.set('volume', volume + 0.1);
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
    }
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
  }
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
ce52f0fca330 add volume control to the player
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
});