summaryrefslogtreecommitdiff
path: root/chimere/static/jme/plugins/poster.js
blob: a4c038cc8bd9d3e64175a749565940f6fd79578e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
 * Simple Poster Plugin for jme
 * @author Matt Dertinger
 * @version 1.0.0
 *
 * http://protofunc.com/jme
 * http://github.com/aFarkas/jMediaelement
 *
 * @description	
 *
 * HTML:
 * 
 * <video class="player" preload="none" poster="../media/big-buck-bunny-trailer.png" controls="controls">
 *   ...
 * </video>
 *
 * OR
 *
 * <div class="fallback">
 *   <img class="photo" src="../media/big-buck-bunny-trailer.png" alt="" data-enabled="enabled" />
 *   ...
 * </div>
 *
 * API:
 *
 * $('video, audio').setPosterAttribute()
 *
 * $('video, audio').enablePoster(index|object)
 *
 * $('video, audio').disablePoster(index|object)
 * 
 * Config:
 * 
 * Documentation:
 * 
 */
(function($){
  //enable posters
  $(document).bind('jmeEmbed', function(e, data){
    data = data.data;
    var mm = $(e.target);
    mm.setPosterAttribute();
    if ($.attr(e.target, 'poster')) {
      data.posterDisplay = $('<img />', {
        src : $.attr(e.target, 'poster'),
        "class": "poster-display inactive-poster-display",
        alt : ""
      }).insertAfter(e.target);
      if( data.posterDisplay ){
        mm.enablePoster(data.posterDisplay, data);
      }
      //add fullwindow support
      if(data.posterDisplay.videoOverlay && mm.is('video')){
        data.posterDisplay
          .videoOverlay({
            fullscreenClass: 'poster-in-fullscreen',
            video: mm,
            startCSS: {
              width: 'auto',
              zIndex: 99999
            },
            position: {
              bottom: 0,
              left: 0,
              right: 0
            }
          })
        ;
      }
      mm
        .bind('play', function(){
          mm.disablePoster(data.posterDisplay, data);
        })
        .bind('ended', function(){
          mm.enablePoster(data.posterDisplay, data);
		  //worarkound:
		  mm.pause();
        })
      ;
    }
  });
  
  /* 
   * extend jme api
   */
  $.multimediaSupport.fn._extend({
    positionPoster: function(object, _data){
      object = (isFinite(object)) ? posters.filter(':eq('+ object +')') : $(object);
      if( !_data ){
        _data = $.data(this.element, 'mediaElemSupport');
      }
      // Only if the poster is visible
      if (!_data.posterDisplay || _data.posterDisplay.is(":hidden")) { return; }
      _data.posterDisplay.height($(this.element).height() + "px"); // Need incase controlsBelow
      _data.posterDisplay.width($(this.element).width() + "px"); // Could probably do 100% of box
    },
    disablePoster: function(object, _data){
      object = (isFinite(object)) ? posters.filter(':eq('+ object +')') : $(object);
      if( !_data ){
        _data = $.data(this.element, 'mediaElemSupport');
      }
      object.removeAttr('data-enabled');
      _data.posterDisplay.addClass('inactive-poster-display').fadeOut('slow');
    },
    enablePoster: function(object, _data){
      var posters = $('img.poster-display', this.element),
          that = this,
          mm = $(this.element),
          posterData,
          found
      ;
      
      if( !_data ){
        _data = mm.data('mediaElemSupport');
      }
      object = (isFinite(object)) ? posters.filter(':eq('+ object +')') : $(object);
      posters
        .filter('[data-enabled]')
        .each(function(){
          if(this !== object[0]){
            that.disablePoster(this, _data);
          }
        })
      ;
      if (!object[0]) { return; }
      
      posterData = $.data(object[0], 'jmePoster') || $.data(object[0], 'jmePoster', {load: false});
      posterData.posterDisplay = _data.posterDisplay;
      posterData.posterDisplay.removeClass('inactive-poster-display').fadeIn("slow");
      // We may not need this. But if we do, we should make it respect config.
      /* if (this.options.fit) {
        that.positionPoster(this, _data);
      } */
      object.attr('data-enabled', 'enabled');
      
    },
    setPosterAttribute: function() {
      if (!$.attr(this.element, 'poster')) {
        var image = $('img.photo[data-enabled]', this.element).first();
        if (image) { $.attr(this.element, 'poster', image.attr('src')); }
      }
    }
  }, true);
  
})(jQuery);