cirandas.net

ref: master

plugins/html5_video/public/video-channel.js


  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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
**  Noosfero's Video Channel specific client script
**  Released under the same Noosfero's license
*/

(function (exports, $) {
"use strict";

var vEl = document.createElement('video');
var canPlay = {
  webm: !!vEl.canPlayType('video/webm').replace(/no/,''),
  ogg:  !!vEl.canPlayType('video/ogg').replace(/no/,''),
  mp4:  !!vEl.canPlayType('video/mp4').replace(/no/,'')
};

exports.VideoChannel = function VideoChannel(baseEl) {
  this.baseEl = baseEl;
  if ($('.video-player', this.baseEl)[0]){
    this.player = new NoosferoVideoPlayer(this.baseEl, this);
    this.init();
  }
};

VideoChannel.prototype.init = function() {
  var me = this;
  $('.video-list-item', this.baseEl).each(
    function(num, item) {
      me.initItem(item);
    }
  );
  if ( $('.video-list li', this.baseEl)[0] ) {
    this.updatePlayer( $('li', this.baseEl).first() );
  } else {
    log.info('there is no playable video yet.');
    $('.video-player', this.baseEl).hide();
  }
};

VideoChannel.prototype.initItem = function(item) {
  var me = this;
  $(item).click(function(){ me.updatePlayer(item, true); });
  var link = $('a', item)[0];
  link.onclick = function(){ return false };
  link.nextFrame = VideoChannel.nextFrame;
  if ( !link.frameFade )
    link.frameFade = $('<div class="frame-fade"></div>').prependTo(link)[0];
  link.frameFade.style.backgroundImage = link.style.backgroundImage;
  link.addEventListener("animationend", function(){ link.nextFrame() }, false);
  link.addEventListener("webkitAnimationEnd", function(){ link.nextFrame() }, false);
  link.nextFrame();
};

VideoChannel.nextFrame = function(fade) {
  if ( !fade ) {
    this.frameFade.style.opacity = 0.0;
    this.frameFade.style.animationName = "";
    this.frameFade.style.MozAnimationName = "";
    this.frameFade.style.webkitAnimationName = "";
    if ( !this.bgYPos ) this.bgYPos = 0;
    this.style.backgroundPosition = "50% "+ ( this.bgYPos++ * -120 ) +"px";
    if ( this.bgYPos > 5 ) this.bgYPos = 0;
    this.frameFade.style.backgroundPosition = "50% "+ ( this.bgYPos * -120 ) +"px";
    var link = this;
    setTimeout( function(){ link.nextFrame(true) }, 10 );
  } else {
    this.frameFade.style.animationDuration = "1s";
    this.frameFade.style.animationName = "fadein";
    this.frameFade.style.MozAnimationDuration = "1s";
    this.frameFade.style.MozAnimationName = "fadein";
    this.frameFade.style.webkitAnimationDuration = "1s";
    this.frameFade.style.webkitAnimationName = "'fadein'";
  }
};

VideoChannel.prototype.updatePlayer = function(item, autoplay) {
  var json = $('a', item)[0].getAttribute("data-webversions");
  this.player.videoList = JSON.parse(json);
  this.player.selectWebVersion();
  this.player.update( this.getItemData(item), autoplay );
};

VideoChannel.prototype.getItemData = function(item) {
  var link = $('a', item)[0];
  var spans = $('span', item);
  var data = {};
  data.pageURL = link.href;
  data.videoURL = link.getAttribute('data-download');
  data.posterURL = link.getAttribute('data-poster');
  data.title = $(link).text();
  data.abstract = $('.abstract', item)[0].innerHTML;
  data.tags = $('.vli-data-tags > div', item)[0].innerHTML;
  return data;
};

///////////// Video Player /////////////////////////////////////////////////////

exports.NoosferoVideoPlayer = function NoosferoVideoPlayer(place, channel) {
  this.channel = channel;
  this.divBase = $('.video-player', place)[0];
  if(!this.divBase) return;
  this.info = {
    title      : $('h2',            this.divBase)[0],
    quality    : $('.quality ul',   this.divBase)[0],
    tags       : $('.tags div',     this.divBase)[0],
    abstract   : $('.abstract div', this.divBase)[0],
    videoCtrl  : $('.video-ctrl',   this.divBase)[0],
    downloadBt : $('.download-bt',  this.divBase)
      .button({ icons: { primary: "ui-icon-circle-arrow-s" } })[0]
  };
  this.videoBox = $('.video-box', this.divBase)[0];
  var me = this;
  this.zoomBt = $('<button class="zoom">Zoom</button>')
      .button({ icons: { primary: "ui-icon-zoomin" }, text: false })
      .click(function(){ me.toggleZoom() })
      .appendTo(this.info.videoCtrl);
  this.zoomBt[0].title = "Zoom in";
  this.videoEl = $('video', this.divBase)[0];
};

NoosferoVideoPlayer.prototype.update = function(data, autoplay) {
  this.info.title.innerHTML = data.title;
  this.videoEl.src = data.videoURL;
  this.videoEl.autoplay = autoplay;
  this.poster = data.posterURL;
  this.videoEl.load();
  var tags = data.tags || '<span class="empty">None</span>'
  $(this.info.tags).empty().append(tags);
  var desc = data.abstract || '<span class="empty">None</span>'
  $(this.info.abstract).empty().append(desc);
  this.info.downloadBt.href = data.videoURL;
};

NoosferoVideoPlayer.prototype.updateQualityOpts = function(type) {
  var me = this;
  $(this.info.quality).empty();
  for ( var size in this.videoList[type] ) {
    var videoData = this.videoList[type][size];
    log.info( 'Quality option:', videoData );
    if ( videoData.status == "done" ) {
      var txt = videoData.size_name;
      if ( !this.channel ) {
        txt = videoData.size.w +'x'+ videoData.size.h +
            ' <small>'+ videoData.vbrate +' KB/s</small>';
      }
      var bt = $(
        '<li data-path="'+videoData.path+'">'+ txt +'</li>')
        .button({ icons: { primary:"ui-icon-video" } })
        .click(function(){ me.load(this.video, true) })
        .appendTo(this.info.quality)[0];
      bt.video = videoData;
      videoData.qualityBt = bt;
    }
  }
};

NoosferoVideoPlayer.prototype.load = function (video, userSelection) {
  if ( this.currentVideo ) $(this.currentVideo.qualityBt).button().button("enable");
  $(video.qualityBt).button("disable");
  this.currentVideo = video;
  this.videoEl.src = video.path;
  this.videoEl.preload = "metadata";
  if ( userSelection )
    $.cookie( "video_quality", video.size_name, {path:'/'} );
  if ( $.cookie("video_zoom") == "true" ) this.zoomIn();
  else this.zoomOut();
};

NoosferoVideoPlayer.prototype.toggleZoom = function () {
  if ( $(this.divBase).hasClass("zoom-in") ) this.zoomOut();
  else this.zoomIn();
};

NoosferoVideoPlayer.prototype.zoomIn = function () {
  $.cookie( "video_zoom", "true", {path:'/'} );
  $(this.divBase).removeClass("zoom-out").addClass("zoom-in");
};

NoosferoVideoPlayer.prototype.zoomOut = function () {
  $.cookie( "video_zoom", "false", {path:'/'} );
  $(this.divBase).removeClass("zoom-in").addClass("zoom-out");
};

NoosferoVideoPlayer.prototype.selectWebVersion = function () {
  var video = null;
  var me = this;
  var q1 = $.cookie("video_quality") || "tiny";
  var q2 = ( q1 == "tiny" ) ? "nice" : "tiny";
  var type = canPlay.webm ? "WEBM" : canPlay.ogg ? "OGV" : "MP4";
  if (  (video = this.getVideoFromList(type, q1))
     || (video = this.getVideoFromList(type, q2))
     ) {
    this.updateQualityOpts(video.type);
    setTimeout( function(){ me.load(video) }, 10 );
  }
};

NoosferoVideoPlayer.prototype.getVideoFromList = function (type, quality) {
  log.info( 'Trying to getVideoFromList', type, quality );
  if (!this.videoList && !this.videoList) {
    log.info( 'The video list is empty' );
    return null;
  }
  if ( quality.toLowerCase() != "nice" ) quality = "tiny";
  var selected = this.videoList[type][quality];
  log.info( 'getVideoFromList find:', selected );
  if ( selected && selected.status == "done" ) {
    log.info( 'getVideoFromList success' );
    return selected;
  } else {
    log.info( 'getVideoFromList fail' );
    return null;
  }
};

}(window, jQuery));