script.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. lb.player = function lbPlayer() {
  2. var self = this;
  3. this.upNextTimer;
  4. this.current;
  5. this.title;
  6. this.subtitle;
  7. this.video;
  8. this.videoID;
  9. this.placeholder;
  10. this.elPlayer = document.getElementById('pl-player');
  11. this.elVideo = document.getElementById('pl-video');
  12. this.elTitle = document.getElementById('pl-player-title').getElementsByTagName('h1')[0];
  13. this.elSubtitle = document.getElementById('pl-player-title').getElementsByTagName('h3')[0];
  14. this.elPlaceholder = document.getElementById('pl-placeholder');
  15. this.elSeek = document.getElementById('pl-playback-progress');
  16. this.elTime = document.getElementById('pl-playback-time');
  17. this.elRuntime = document.getElementById('pl-runtime');
  18. this.elLoaded = document.getElementById('pl-playback-loaded');
  19. this.elProgressBarWrap = document.getElementById('pl-playback-progress-wrap');
  20. this.elProgressPosition = document.getElementById('pl-playback-position');
  21. this.elInfoTitle = document.getElementById('pl-info-series');
  22. this.elInfoSubtitle = document.getElementById('pl-info-episode');
  23. //this.elPopup = document.getElementById('pl-next-popup');
  24. // this.elPopupImage = document.getElementById('pl-next-popup-image');
  25. // this.elPopupSubtitle = document.getElementById('pl-next-popup-subtitle');
  26. this.elUpNext = document.getElementById('pl-up-next');
  27. this.elUpNextWatchAgain = document.getElementById('pl-watch-again');
  28. this.elUpNextBg = document.getElementById('pl-up-next-bg');
  29. this.elUpNextImage = document.getElementById('pl-up-next-image');
  30. this.elUpNextTitle = document.getElementById('pl-up-next-title').getElementsByTagName('h1')[0];
  31. this.elUpNextSubtitle = document.getElementById('pl-up-next-title').getElementsByTagName('h3')[0];
  32. this.elUpNextCounter = document.getElementById('pl-up-next-timer');
  33. this.elUpNextGo = document.getElementById('pl-up-next-go');
  34. this.elControls = document.getElementById('pl-player-ctrl-wrap');
  35. this.elControlsPlayback = document.getElementById('pl-playback-ctrls-wrap');
  36. this.elControlsAudio = document.getElementById('pl-audio-ctrls-wrap');
  37. this.elControlPlay = document.getElementById('pl-playback-play');
  38. this.elControlPause = document.getElementById('pl-playback-pause');
  39. this.elControlVolume = document.getElementById('pl-audio-volume');
  40. this.elControlMute = document.getElementById('pl-audio-mute');
  41. this.elControlsNav = document.getElementById('pl-navigation-ctrls-wrap');
  42. this.elControlNext = document.getElementById('pl-next');
  43. this.finalScreen = document.getElementById('pl-final-screen');
  44. this.finalWatchAgain = document.getElementById('pl-final-watch-again');
  45. this.elVolumeWrap = document.getElementById('pl-volume-wrap');
  46. this.elVolume = document.getElementById('pl-volume');
  47. this.elVolumeBar = document.getElementById('pl-volume-bar');
  48. this.hookListeners();
  49. this.setCurrent(0);
  50. };
  51. lb.player.prototype.setTitle = function (value) {
  52. this.title = value;
  53. this.elTitle.innerHTML = this.elInfoTitle.innerHTML = value;
  54. }
  55. lb.player.prototype.setSubtitle = function (value) {
  56. this.subtitle = value;
  57. this.elSubtitle.innerHTML = this.elInfoSubtitle.innerHTML = value;
  58. }
  59. lb.player.prototype.setPlaceholder = function (value) {
  60. this.placeholder = value;
  61. this.elPlaceholder.style.backgroundImage = 'url("' + value + '")';
  62. }
  63. lb.player.prototype.setVideo = function (value) {
  64. var self = this;
  65. this.videoID = value;
  66. if(this.video) {
  67. this.video.loadVideoById(value);
  68. } else {
  69. this.video = new YT.Player(this.elVideo, {
  70. height: '768',
  71. width: '1366',
  72. videoId: value,
  73. playerVars: {
  74. controls: 0,
  75. disablekb: 1,
  76. enablejsapi: 1,
  77. loop: 0,
  78. modestbranding: 1,
  79. rel: 0,
  80. start: 0,
  81. showinfo: 0,
  82. iv_load_policy: 3,
  83. cc_load_policy: 1
  84. },
  85. events: {
  86. 'onReady': self.onPlayerReady.bind(this),
  87. 'onStateChange': self.onPlayerStateChange.bind(this)
  88. }
  89. });
  90. }
  91. }
  92. lb.player.prototype.changeInfo = function (title, subtitle, placeholder, video) {
  93. this.setTitle(title);
  94. this.setSubtitle(subtitle);
  95. this.setPlaceholder(placeholder);
  96. this.setVideo(video);
  97. }
  98. lb.player.prototype.showTime = function () {
  99. setInterval((function() {
  100. var remaining = this.video.getDuration() - this.video.getCurrentTime(),
  101. s = Math.floor(remaining) % 60,
  102. m = Math.floor(remaining / 60) % 60,
  103. h = Math.floor((remaining / 60) / 60) % 60;
  104. this.elTime.innerHTML = (h < 10 ? '' + h : h) + ':' + (m < 10 ? '0' + m : m) + ':' + (s < 10 ? '0' + s : s);
  105. }).bind(this), 100);
  106. }
  107. lb.player.prototype.showLoaded = function () {
  108. setInterval((function() {
  109. this.elLoaded.style.width = this.video.getVideoLoadedFraction() * 100 + '%';
  110. }).bind(this), 100);
  111. }
  112. lb.player.prototype.showProgressPosition = function () {
  113. setInterval((function() {
  114. if(this.isSeeking) return;
  115. this.elProgressPosition.style.width = (this.video.getCurrentTime() / this.video.getDuration()) * 100 + '%';
  116. }).bind(this), 100);
  117. }
  118. lb.player.prototype.setCurrent = function(current) {
  119. clearInterval(this.upNextTimer);
  120. this.elPlayer.classList.add('loading');
  121. this.current = current;
  122. this.changeInfo(
  123. lb.videos[current].title,
  124. lb.videos[current].subtitle,
  125. lb.videos[current].placeholder,
  126. lb.videos[current].video);
  127. this.elUpNext.style.display = 'none';
  128. this.finalWatchAgain.style.backgroundImage =
  129. this.elUpNextWatchAgain.style.backgroundImage = 'url("' + lb.videos[current].thumb480 + '")';
  130. this.setNext();
  131. if(this.current === lb.videos.length - 1) {
  132. this.finalWatchAgain.style.backgroundImage = 'url("' + lb.videos[0].thumb480 + '")';
  133. }
  134. }
  135. // lb.player.prototype.setNext = function () {
  136. // if(this.current === lb.videos.length - 1){
  137. // this.elPopupImage.style.backgroundImage = 'url("/img/obsabsorver.jpg")';
  138. // this.elPopupSubtitle.innerHTML = "Outros títulos";
  139. // return;
  140. // }
  141. //
  142. // this.elUpNextBg.style.backgroundImage = 'url("' + lb.videos[this.current + 1].placeholder + '")';
  143. // this.elUpNextImage.style.backgroundImage = 'url("' + lb.videos[this.current + 1].thumb480 + '")';
  144. // this.elPopupImage.style.backgroundImage = 'url("' + lb.videos[this.current + 1].thumb130 + '")';
  145. //
  146. // this.elPopupSubtitle.innerHTML = this.elUpNextSubtitle.innerHTML = lb.videos[this.current + 1].subtitle;
  147. // this.elUpNextTitle.innerHTML = lb.videos[this.current + 1].title;
  148. // this.elRuntime.innerHTML = lb.videos[this.current + 1].runtime;
  149. // }
  150. lb.player.prototype.hookListeners = function () {
  151. this.elPlayer.addEventListener('contextmenu', function(e){ e.preventDefault() });
  152. this.elControlPlay.addEventListener('click', this.onPlayClick.bind(this));
  153. this.elControlPause.addEventListener('click', this.onPauseClick.bind(this));
  154. this.elControlVolume.addEventListener('click', this.onVolumeClick.bind(this));
  155. this.elControlMute.addEventListener('click', this.onMuteClick.bind(this));
  156. // this.elPopup.getElementsByClassName('inner')[0].addEventListener('click', this.onNextClick.bind(this));
  157. //this.elControlNext.addEventListener('click', this.onNextClick.bind(this));
  158. this.elUpNextGo.addEventListener('click', this.onNextClick.bind(this));
  159. this.elUpNextWatchAgain.addEventListener('click', this.onWatchAgainClick.bind(this));
  160. this.finalWatchAgain.addEventListener('click', this.onWatchAgainClick.bind(this));
  161. this.elControlsAudio.addEventListener('mouseenter', this.onVolumeControlsHover.bind(this));
  162. this.elControlsAudio.addEventListener('mouseleave', this.onVolumeControlsLeave.bind(this));
  163. this.elControlsNav.addEventListener('mouseenter', this.onNextHover.bind(this));
  164. this.elControlsNav.addEventListener('mouseleave', this.onNextLeave.bind(this));
  165. this.elSeek.addEventListener('mousedown', this.startSeek.bind(this));
  166. document.addEventListener('mouseup', this.stopSeek.bind(this));
  167. this.elVolume.addEventListener('mousedown', this.startVolumeChange.bind(this));
  168. document.addEventListener('mouseup', this.stopVolumeChange.bind(this));
  169. this.elPlayer.addEventListener('mousemove', this.showControls.bind(this));
  170. window.addEventListener('keypress', (function(e) {
  171. var k = e.keyCode || e.which;
  172. if(k === 0 || k === 32) {
  173. if(this.video.getPlayerState() <= 0) return;
  174. if(this.video.getPlayerState() === 1) {
  175. this.onPauseClick();
  176. } else {
  177. this.onPlayClick();
  178. }
  179. }
  180. }).bind(this));
  181. //Bind keyboard events to make the playback controllable by the keyboard.
  182. window.addEventListener('keydown', (function(e) {
  183. var k = e.keyCode || e.which;
  184. //Volume Controls.
  185. if (k == '38') //Up Arrow
  186. this.changeVolumeDelta(+10);
  187. else if (k == '40') //Down Arrow
  188. this.changeVolumeDelta(-10);
  189. //Playback controls.
  190. else if (k == '37') //Left Arrow.
  191. this.seekTo(-10);
  192. else if (k == '39') //Right Arrow.
  193. this.seekTo(+10);
  194. }).bind(this));
  195. document.getElementById('pl-fs').addEventListener('click', (function() {
  196. var elem = this.elPlayer;
  197. if (elem.requestFullscreen) {
  198. elem.requestFullscreen();
  199. } else if (elem.msRequestFullscreen) {
  200. elem.msRequestFullscreen();
  201. } else if (elem.mozRequestFullScreen) {
  202. elem.mozRequestFullScreen();
  203. } else if (elem.webkitRequestFullscreen) {
  204. elem.webkitRequestFullscreen();
  205. }
  206. document.getElementById('pl-fs-wrap').classList.add('fs');
  207. }).bind(this));
  208. document.getElementById('pl-fs-ex').addEventListener('click', (function(){
  209. if (document.exitFullscreen) {
  210. document.exitFullscreen();
  211. } else if (document.msExitFullscreen) {
  212. document.msExitFullscreen();
  213. } else if (document.mozCancelFullScreen) {
  214. document.mozCancelFullScreen();
  215. } else if (document.webkitExitFullscreen) {
  216. document.webkitExitFullscreen();
  217. }
  218. document.getElementById('pl-fs-wrap').classList.remove('fs');
  219. }).bind(this));
  220. }
  221. lb.player.prototype.onPlayerReady = function () {
  222. this.video.playVideo();
  223. this.showTime();
  224. this.showLoaded();
  225. this.showProgressPosition();
  226. }
  227. lb.player.prototype.onPlayerStateChange = function (e) {
  228. switch(e.data) {
  229. case -1: // Not initialized
  230. break;
  231. case 0: // Ended
  232. this.onPause.call(this);
  233. this.onEnded.call(this);
  234. break;
  235. case 1: // Playing
  236. this.onPlay.call(this);
  237. break;
  238. case 2: // Paused
  239. this.onPause.call(this);
  240. break;
  241. case 3: // Buffering
  242. break;
  243. case 5: // Cued
  244. break;
  245. }
  246. }
  247. lb.player.prototype.onPlay = function () {
  248. this.elPlayer.classList.remove('loading');
  249. this.elControlsPlayback.classList.add('pause');
  250. this.elControlsPlayback.classList.remove('play');
  251. this.elVolumeBar.style.height = this.video.getVolume() + '%';
  252. this.showControls();
  253. }
  254. lb.player.prototype.onPause = function () {
  255. this.elControlsPlayback.classList.add('play');
  256. this.elControlsPlayback.classList.remove('pause');
  257. }
  258. lb.player.prototype.onEnded = function () {
  259. if(this.current < lb.videos.length - 1) {
  260. this.elUpNext.style.display = 'block';
  261. var time = 15;
  262. this.upNextTimer = setInterval((function() {
  263. if(time > 0) {
  264. time--;
  265. this.elUpNextCounter.innerHTML = time + ' segundos';
  266. } else {
  267. this.onNextClick(this);
  268. this.elUpNextCounter.innerHTML = '15 segundos';
  269. }
  270. }).bind(this), 333);
  271. } else {
  272. this.finalScreen.style.display = 'block';
  273. }
  274. }
  275. lb.player.prototype.onPlayClick = function () {
  276. this.video.playVideo();
  277. }
  278. lb.player.prototype.onPauseClick = function () {
  279. this.video.pauseVideo();
  280. }
  281. lb.player.prototype.onVolumeClick = function () {
  282. this.video.mute();
  283. this.elControlsAudio.classList.add('mute');
  284. this.elControlsAudio.classList.remove('volume');
  285. }
  286. lb.player.prototype.onMuteClick = function () {
  287. this.video.unMute();
  288. this.elControlsAudio.classList.add('volume');
  289. this.elControlsAudio.classList.remove('mute');
  290. }
  291. lb.player.prototype.onNextClick = function () {
  292. if(this.current < lb.videos.length - 1) {
  293. this.setCurrent(this.current + 1);
  294. } else {
  295. this.video.pauseVideo();
  296. this.finalScreen.style.display = 'block';
  297. }
  298. }
  299. lb.player.prototype.onWatchAgainClick = function () {
  300. this.finalScreen.style.display = 'none';
  301. if(this.current === lb.videos.length - 1) {
  302. this.setCurrent(0);
  303. } else {
  304. this.setCurrent(this.current);
  305. }
  306. }
  307. lb.player.prototype.hideVolumeWrap = function () {
  308. this.elVolumeWrap.style.display = 'none';
  309. this.elVolumeWrap.removeEventListener('transitionend', this.hideVolumeWrapBind)
  310. }
  311. lb.player.prototype.onVolumeControlsHover = function () {
  312. this.hideSeekBar();
  313. this.elVolumeWrap.removeEventListener('transitionend', this.hideVolumeWrapBind);
  314. this.elVolumeWrap.style.display = 'block';
  315. setTimeout((function(){
  316. this.elVolumeWrap.style.opacity = 1;
  317. }).bind(this), 10);
  318. }
  319. lb.player.prototype.onVolumeControlsLeave = function () {
  320. this.showSeekBar();
  321. this.hideVolumeWrapBind = (function() {
  322. this.elVolumeWrap.style.display = 'none';
  323. this.elVolumeWrap.removeEventListener('transitionend', this.hideVolumeWrapBind);
  324. }).bind(this);
  325. this.elVolumeWrap.addEventListener('transitionend', this.hideVolumeWrapBind);
  326. var timer = setInterval((function() {
  327. if(this.isChangingVolume) {
  328. return;
  329. }
  330. this.elVolumeWrap.style.opacity = 0;
  331. clearInterval(timer);
  332. }).bind(this),10);
  333. }
  334. lb.player.prototype.hideNext = function () {
  335. //this.elPopup.style.display = 'none';
  336. //this.elPopup.removeEventListener('transitionend', this.hideNextBind)
  337. }
  338. lb.player.prototype.onNextHover = function () {
  339. this.hideSeekBar();
  340. //this.elPopup.removeEventListener('transitionend', this.hideNextBind);
  341. //this.elPopup.style.display = 'block';
  342. setTimeout((function(){
  343. //this.elPopup.style.opacity = 1;
  344. }).bind(this),10);
  345. }
  346. lb.player.prototype.onNextLeave = function () {
  347. this.showSeekBar();
  348. this.hideNextBind = (function() {
  349. //this.elPopup.style.display = 'none';
  350. //this.elPopup.removeEventListener('transitionend', this.hideNextBind);
  351. }).bind(this);
  352. //this.elPopup.addEventListener('transitionend', this.hideNextBind);
  353. //this.elPopup.style.opacity = 0;
  354. }
  355. lb.player.prototype.startSeek = function (e) {
  356. this.isSeeking = true;
  357. this.bindSeek = this.seek.bind(this);
  358. this.bindSeek(e);
  359. this.elSeek.addEventListener('mousemove', this.bindSeek);
  360. }
  361. lb.player.prototype.stopSeek = function (e) {
  362. if(!this.isSeeking) return;
  363. var layerX = e.clientX - this.elSeek.getBoundingClientRect().left,
  364. seconds = (layerX / this.elSeek.clientWidth) * this.video.getDuration();
  365. seconds = (seconds < 0 ? 0 : seconds > this.video.getDuration()-1 ? this.video.getDuration()-1 : seconds);
  366. this.video.seekTo(seconds, true);
  367. this.isSeeking = false;
  368. this.elSeek.removeEventListener('mousemove', this.bindSeek);
  369. }
  370. lb.player.prototype.seek = function(e) {
  371. var layerX = e.clientX - this.elSeek.getBoundingClientRect().left,
  372. seconds = (layerX / this.elSeek.clientWidth) * this.video.getDuration(),
  373. progress = (seconds / this.video.getDuration()) * 100;
  374. progress = (progress < 0 ? 0 : progress > 100 ? 100 : progress);
  375. seconds = (seconds < 0 ? 0 : seconds > this.video.getDuration() ? this.video.getDuration() : seconds);
  376. this.video.seekTo(seconds, false);
  377. this.elProgressPosition.style.width = progress + '%';
  378. }
  379. lb.player.prototype.seekTo = function(deltaSeconds) {
  380. var currSeconds = this.video.getCurrentTime();
  381. this.video.seekTo(currSeconds + deltaSeconds, true);
  382. }
  383. lb.player.prototype.startVolumeChange = function(e) {
  384. this.isChangingVolume = true;
  385. this.bindVolumeChange = this.volumeChange.bind(this);
  386. this.bindVolumeChange(e);
  387. this.elVolume.addEventListener('mousemove', this.bindVolumeChange);
  388. }
  389. lb.player.prototype.stopVolumeChange = function(e) {
  390. if(!this.isChangingVolume) return;
  391. var layerY = e.clientY - this.elVolume.getBoundingClientRect().top,
  392. volume = 100 - (layerY / this.elVolume.clientHeight) * 100;
  393. this.isChangingVolume = false;
  394. this.elVolume.removeEventListener('mousemove', this.bindVolumeChange);
  395. }
  396. lb.player.prototype.volumeChange = function(e) {
  397. if(window.YT === undefined) return;
  398. var layerY = e.clientY - this.elVolume.getBoundingClientRect().top,
  399. volume = 100 - (layerY / this.elVolume.clientHeight) * 100,
  400. delta = volume - this.video.getVolume();
  401. this.changeVolumeDelta(delta);
  402. };
  403. lb.player.prototype.changeVolumeDelta = function(deltaVolume) {
  404. var currVolume = this.video.getVolume();
  405. var finalVolume = currVolume + deltaVolume;
  406. finalVolume = (finalVolume < 0 ? 0 : finalVolume > 100 ? 100 : finalVolume);
  407. this.video.setVolume(finalVolume);
  408. this.elVolumeBar.style.height = finalVolume + '%';
  409. }
  410. lb.player.prototype.hideSeekBar = function () {
  411. this.elProgressBarWrap.classList.add('hide');
  412. }
  413. lb.player.prototype.showSeekBar = function () {
  414. this.elProgressBarWrap.classList.remove('hide');
  415. }
  416. lb.player.prototype.hideControls = function () {
  417. this.controlsHideBind = (function(){
  418. document.getElementById('exit-button').style.display = 'none';
  419. document.getElementById('exit-button').addEventListener('transitionend', this.controlsHideBind);
  420. this.elControls.style.display = 'none';
  421. this.elControls.addEventListener('transitionend', this.controlsHideBind);
  422. }).bind(this);
  423. document.getElementById('exit-button').addEventListener('transitionend', this.controlsHideBind);
  424. document.getElementById('exit-button').classList.add('hide');
  425. this.elControls.addEventListener('transitionend', this.controlsHideBind);
  426. this.elControls.classList.add('hide');
  427. }
  428. lb.player.prototype.showControls = function () {
  429. clearTimeout(this.controlsTimer);
  430. document.getElementById('exit-button').style.display = 'block';
  431. document.getElementById('exit-button').removeEventListener('transitionend', this.controlsHideBind);
  432. document.getElementById('exit-button').classList.remove('hide');
  433. this.elControls.style.display = 'block';
  434. this.elControls.removeEventListener('transitionend', this.controlsHideBind);
  435. this.elControls.classList.remove('hide');
  436. this.controlsTimer = setTimeout(this.hideControls.bind(this), 3000);
  437. }