script.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. document.getElementById('pl-fs').addEventListener('click', (function() {
  182. var elem = this.elPlayer;
  183. if (elem.requestFullscreen) {
  184. elem.requestFullscreen();
  185. } else if (elem.msRequestFullscreen) {
  186. elem.msRequestFullscreen();
  187. } else if (elem.mozRequestFullScreen) {
  188. elem.mozRequestFullScreen();
  189. } else if (elem.webkitRequestFullscreen) {
  190. elem.webkitRequestFullscreen();
  191. }
  192. document.getElementById('pl-fs-wrap').classList.add('fs');
  193. }).bind(this));
  194. document.getElementById('pl-fs-ex').addEventListener('click', (function(){
  195. if (document.exitFullscreen) {
  196. document.exitFullscreen();
  197. } else if (document.msExitFullscreen) {
  198. document.msExitFullscreen();
  199. } else if (document.mozCancelFullScreen) {
  200. document.mozCancelFullScreen();
  201. } else if (document.webkitExitFullscreen) {
  202. document.webkitExitFullscreen();
  203. }
  204. document.getElementById('pl-fs-wrap').classList.remove('fs');
  205. }).bind(this));
  206. }
  207. lb.player.prototype.onPlayerReady = function () {
  208. this.video.playVideo();
  209. this.showTime();
  210. this.showLoaded();
  211. this.showProgressPosition();
  212. }
  213. lb.player.prototype.onPlayerStateChange = function (e) {
  214. switch(e.data) {
  215. case -1: // Not initialized
  216. break;
  217. case 0: // Ended
  218. this.onPause.call(this);
  219. this.onEnded.call(this);
  220. break;
  221. case 1: // Playing
  222. this.onPlay.call(this);
  223. break;
  224. case 2: // Paused
  225. this.onPause.call(this);
  226. break;
  227. case 3: // Buffering
  228. break;
  229. case 5: // Cued
  230. break;
  231. }
  232. }
  233. lb.player.prototype.onPlay = function () {
  234. this.elPlayer.classList.remove('loading');
  235. this.elControlsPlayback.classList.add('pause');
  236. this.elControlsPlayback.classList.remove('play');
  237. this.elVolumeBar.style.height = this.video.getVolume() + '%';
  238. this.showControls();
  239. }
  240. lb.player.prototype.onPause = function () {
  241. this.elControlsPlayback.classList.add('play');
  242. this.elControlsPlayback.classList.remove('pause');
  243. }
  244. lb.player.prototype.onEnded = function () {
  245. if(this.current < lb.videos.length - 1) {
  246. this.elUpNext.style.display = 'block';
  247. var time = 15;
  248. this.upNextTimer = setInterval((function() {
  249. if(time > 0) {
  250. time--;
  251. this.elUpNextCounter.innerHTML = time + ' segundos';
  252. } else {
  253. this.onNextClick(this);
  254. this.elUpNextCounter.innerHTML = '15 segundos';
  255. }
  256. }).bind(this), 333);
  257. } else {
  258. this.finalScreen.style.display = 'block';
  259. }
  260. }
  261. lb.player.prototype.onPlayClick = function () {
  262. this.video.playVideo();
  263. }
  264. lb.player.prototype.onPauseClick = function () {
  265. this.video.pauseVideo();
  266. }
  267. lb.player.prototype.onVolumeClick = function () {
  268. this.video.mute();
  269. this.elControlsAudio.classList.add('mute');
  270. this.elControlsAudio.classList.remove('volume');
  271. }
  272. lb.player.prototype.onMuteClick = function () {
  273. this.video.unMute();
  274. this.elControlsAudio.classList.add('volume');
  275. this.elControlsAudio.classList.remove('mute');
  276. }
  277. lb.player.prototype.onNextClick = function () {
  278. if(this.current < lb.videos.length - 1) {
  279. this.setCurrent(this.current + 1);
  280. } else {
  281. this.video.pauseVideo();
  282. this.finalScreen.style.display = 'block';
  283. }
  284. }
  285. lb.player.prototype.onWatchAgainClick = function () {
  286. this.finalScreen.style.display = 'none';
  287. if(this.current === lb.videos.length - 1) {
  288. this.setCurrent(0);
  289. } else {
  290. this.setCurrent(this.current);
  291. }
  292. }
  293. lb.player.prototype.hideVolumeWrap = function () {
  294. this.elVolumeWrap.style.display = 'none';
  295. this.elVolumeWrap.removeEventListener('transitionend', this.hideVolumeWrapBind)
  296. }
  297. lb.player.prototype.onVolumeControlsHover = function () {
  298. this.hideSeekBar();
  299. this.elVolumeWrap.removeEventListener('transitionend', this.hideVolumeWrapBind);
  300. this.elVolumeWrap.style.display = 'block';
  301. setTimeout((function(){
  302. this.elVolumeWrap.style.opacity = 1;
  303. }).bind(this), 10);
  304. }
  305. lb.player.prototype.onVolumeControlsLeave = function () {
  306. this.showSeekBar();
  307. this.hideVolumeWrapBind = (function() {
  308. this.elVolumeWrap.style.display = 'none';
  309. this.elVolumeWrap.removeEventListener('transitionend', this.hideVolumeWrapBind);
  310. }).bind(this);
  311. this.elVolumeWrap.addEventListener('transitionend', this.hideVolumeWrapBind);
  312. var timer = setInterval((function() {
  313. if(this.isChangingVolume) {
  314. return;
  315. }
  316. this.elVolumeWrap.style.opacity = 0;
  317. clearInterval(timer);
  318. }).bind(this),10);
  319. }
  320. lb.player.prototype.hideNext = function () {
  321. //this.elPopup.style.display = 'none';
  322. //this.elPopup.removeEventListener('transitionend', this.hideNextBind)
  323. }
  324. lb.player.prototype.onNextHover = function () {
  325. this.hideSeekBar();
  326. //this.elPopup.removeEventListener('transitionend', this.hideNextBind);
  327. //this.elPopup.style.display = 'block';
  328. setTimeout((function(){
  329. //this.elPopup.style.opacity = 1;
  330. }).bind(this),10);
  331. }
  332. lb.player.prototype.onNextLeave = function () {
  333. this.showSeekBar();
  334. this.hideNextBind = (function() {
  335. //this.elPopup.style.display = 'none';
  336. //this.elPopup.removeEventListener('transitionend', this.hideNextBind);
  337. }).bind(this);
  338. //this.elPopup.addEventListener('transitionend', this.hideNextBind);
  339. //this.elPopup.style.opacity = 0;
  340. }
  341. lb.player.prototype.startSeek = function (e) {
  342. this.isSeeking = true;
  343. this.bindSeek = this.seek.bind(this);
  344. this.bindSeek(e);
  345. this.elSeek.addEventListener('mousemove', this.bindSeek);
  346. }
  347. lb.player.prototype.stopSeek = function (e) {
  348. if(!this.isSeeking) return;
  349. var layerX = e.clientX - this.elSeek.getBoundingClientRect().left,
  350. seconds = (layerX / this.elSeek.clientWidth) * this.video.getDuration();
  351. seconds = (seconds < 0 ? 0 : seconds > this.video.getDuration()-1 ? this.video.getDuration()-1 : seconds);
  352. this.video.seekTo(seconds, true);
  353. this.isSeeking = false;
  354. this.elSeek.removeEventListener('mousemove', this.bindSeek);
  355. }
  356. lb.player.prototype.seek = function(e) {
  357. var layerX = e.clientX - this.elSeek.getBoundingClientRect().left,
  358. seconds = (layerX / this.elSeek.clientWidth) * this.video.getDuration(),
  359. progress = (seconds / this.video.getDuration()) * 100;
  360. progress = (progress < 0 ? 0 : progress > 100 ? 100 : progress);
  361. seconds = (seconds < 0 ? 0 : seconds > this.video.getDuration() ? this.video.getDuration() : seconds);
  362. this.video.seekTo(seconds, false);
  363. this.elProgressPosition.style.width = progress + '%';
  364. }
  365. lb.player.prototype.startVolumeChange = function(e) {
  366. this.isChangingVolume = true;
  367. this.bindVolumeChange = this.volumeChange.bind(this);
  368. this.bindVolumeChange(e);
  369. this.elVolume.addEventListener('mousemove', this.bindVolumeChange);
  370. }
  371. lb.player.prototype.stopVolumeChange = function(e) {
  372. if(!this.isChangingVolume) return;
  373. var layerY = e.clientY - this.elVolume.getBoundingClientRect().top,
  374. volume = 100 - (layerY / this.elVolume.clientHeight) * 100;
  375. this.isChangingVolume = false;
  376. this.elVolume.removeEventListener('mousemove', this.bindVolumeChange);
  377. }
  378. lb.player.prototype.volumeChange = function(e) {
  379. if(window.YT === undefined) return;
  380. var layerY = e.clientY - this.elVolume.getBoundingClientRect().top,
  381. volume = 100 - (layerY / this.elVolume.clientHeight) * 100;
  382. volume = (volume < 0 ? 0 : volume > 100 ? 100 : volume);
  383. this.video.setVolume(volume);
  384. this.elVolumeBar.style.height = volume + '%';
  385. };
  386. lb.player.prototype.hideSeekBar = function () {
  387. this.elProgressBarWrap.classList.add('hide');
  388. }
  389. lb.player.prototype.showSeekBar = function () {
  390. this.elProgressBarWrap.classList.remove('hide');
  391. }
  392. lb.player.prototype.hideControls = function () {
  393. this.controlsHideBind = (function(){
  394. document.getElementById('exit-button').style.display = 'none';
  395. document.getElementById('exit-button').addEventListener('transitionend', this.controlsHideBind);
  396. this.elControls.style.display = 'none';
  397. this.elControls.addEventListener('transitionend', this.controlsHideBind);
  398. }).bind(this);
  399. document.getElementById('exit-button').addEventListener('transitionend', this.controlsHideBind);
  400. document.getElementById('exit-button').classList.add('hide');
  401. this.elControls.addEventListener('transitionend', this.controlsHideBind);
  402. this.elControls.classList.add('hide');
  403. }
  404. lb.player.prototype.showControls = function () {
  405. clearTimeout(this.controlsTimer);
  406. document.getElementById('exit-button').style.display = 'block';
  407. document.getElementById('exit-button').removeEventListener('transitionend', this.controlsHideBind);
  408. document.getElementById('exit-button').classList.remove('hide');
  409. this.elControls.style.display = 'block';
  410. this.elControls.removeEventListener('transitionend', this.controlsHideBind);
  411. this.elControls.classList.remove('hide');
  412. this.controlsTimer = setTimeout(this.hideControls.bind(this), 3000);
  413. }