Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- Videos - odtwarzanie video w bloczku 'Wideo' na stronie głównej
- Założenia:
- 1. Player startuje gdy w vieporcie (z opóźnieniem po załadowaniu strony)
- 2. Player pauzuje gdy poza vieportem i użytkownik nie włączył głosu (gdy wejdzie w vieport to znowu odtwarzamy).
- 3. Player jest wyciszony na starcie
- 4. Materiał video jest początkowo w niższej jakości
- 5. Po włączeniu fullscreena zmieniamy jakość na HD
- 6. Gdy ktoś spauzuje film i wyjedzie z vieportu a następnie wróci to już mu nie startujemy video
- * @author Konrad Beśka
- * @since 13.01.2017
- */
- (function (w, doc) {
- w.Inpl = w.Inpl || {};
- w.console = w.console || {log: function () {}};
- if(w.Inpl.hasOwnProperty('videos')) {
- console.log('Istnieje już [window.Inpl.videos]');
- return;
- }
- // konfiguracja videoplayera pochodzi z helpera php i renderowana jest w źródle strony do poniższej zmiennej
- if (! w.playerVideosConfig){
- console.log('Brak konfiguracji dla videonewsa, czyli brak [window.playerVideosConfig]');
- return;
- }
- var config = w.playerVideosConfig;
- var playlistData = w.playerVideosPlaylist;
- var workId = 0;
- if (config.tracks && config.tracks[0] && config.tracks[0].data && config.tracks[0].data.id ){
- workId = config.tracks[0].data.id;
- } else {
- console.log('Brak id materiału, czyli brak [window.playerVideosConfig.tracks[0].data.id]');
- return;
- }
- // <FUNKCJE POMOCNICZE>
- // Manipulacja klasami css
- /**
- * @param {Node} el - element DOM
- * @param {String} name - nazwa klasy css
- * @returns {boolean}
- */
- function hasClass(el, name) {
- return (' ' + el.className + ' ').indexOf(' ' + name + ' ') > -1;
- }
- /**
- * @param {Node} el - element DOM
- * @param {String} name - nazwa klasy css
- * @returns {Node}
- */
- function addClass(el, name) {
- if (!hasClass(el, name)) {
- el.className += (el.className ? ' ' : '') + name;
- }
- return el;
- }
- /**
- * @param {Node} el - element DOM
- * @param {String} name - nazwa klasy css
- * @returns {Node}
- */
- function removeClass(el, name) {
- var classList = ' ' + el.className + ' ';
- while (classList.indexOf(' ' + name + ' ') > -1) {
- classList = classList.replace(' ' + name + ' ', ' ');
- }
- el.className = typeof classList.trim === 'function' ? classList.trim() : classList.replace(/^\s+|\s+$/g, '');
- return el;
- }
- /**
- * Sprawdza czy podana zmienna jest tablicą
- *
- * @param variable
- */
- function isArray(variable) {
- if (Array.isArray) {
- return Array.isArray(variable);
- } else if( Object.prototype.toString.call(variable) === '[object Array]' ){
- return true;
- }
- return false;
- }
- /**
- * Tworzenie elementu DOM
- *
- * @param {String} tag - tag html, np. div
- * @param {String} cssClass - klasa css - bez kropki, np. list-item-element
- * @param {String} html - html do wstrzyknięcia do danego elementu
- * @param {Object} attributes - obiekt z atrybutami html elementu np. {href: '/url'}
- * @returns {Node}
- */
- function domEle(tag, cssClass, html, attributes) {
- var el = document.createElement(tag);
- if (typeof cssClass !== 'undefined' && cssClass !== null) {
- el.className = cssClass;
- }
- if (typeof html !== 'undefined' && html !== null) {
- el.innerHTML = html;
- }
- if (typeof attributes === 'object') {
- Object.keys(attributes).forEach(function (attr, index) {
- if (attributes.hasOwnProperty(attr)){
- el.setAttribute(attr, attributes[attr]);
- }
- });
- }
- return el;
- }
- /**
- * Dodajemy elementy Node DOM do innego Noda
- * Wywodujemy w postaci addpend(rodzic, dziecko1, dziecko2..);
- *
- * @param container
- * @param {Node|Array<Node>} childs
- */
- function append(container, childs) {
- if (arguments.length > 2){
- Array.prototype.forEach.call(arguments, function (ele, index) {
- if (index < 1 || ele === null){
- return;
- }
- if(isArray(ele)){
- ele.forEach(function (el) {
- if (el === null){
- return;
- }
- container.appendChild(el);
- })
- } else {
- container.appendChild(ele);
- }
- });
- } else {
- if (childs === null){
- return container;
- }
- if(isArray(childs)){
- childs.forEach(function (el) {
- if (el === null){
- return;
- }
- container.appendChild(el);
- })
- } else {
- container.appendChild(childs);
- }
- }
- return container;
- }
- /**
- * Dodajemy listener na event dom
- *
- * @param {Node} node
- * @param {string }eventName - nazwa zdarzenia
- * @param {Function} callback
- */
- function event(node, eventName, callback) {
- if (node.addEventListener) {
- node.addEventListener(eventName, callback);
- } else if (node.attachEvent) {
- node.attachEvent('on' + eventName, callback);
- }
- }
- /**
- * Limitowanie ilości wywołań
- *
- * @param {Function} fn - funkcja, którą chcemy opóźnić
- * @param {Number} threshhold - czas odstępu wywołań w milisekundach
- * @param {Object} scope - kontekst
- * @returns {Function}
- */
- function throttle(fn, threshhold, scope) {
- threshhold = threshhold && threshhold % 1 === 0 ? threshhold : 500;
- var last,
- timer;
- return function () {
- var context = scope || this;
- var now = +new Date();
- var args = arguments;
- if (last && now < last + threshhold) {
- clearTimeout(timer);
- timer = setTimeout(function () {
- last = now;
- fn.apply(context, args);
- }, threshhold);
- } else {
- last = now;
- fn.apply(context, args);
- }
- };
- }
- function toArray(obj) {
- return Object.keys(obj).map( function (key) { return obj[key]; });
- }
- // </FUNKCJE POMOCNICZE>
- w.Inpl.videos = {
- pausedByUser: false, // Gdy ręcznie spauzuje to juz nie odtwarzamy go automatycznie (np. gdy ponownie wjechał do viewportu), aby wiedzieć czy ręcznie spauzowal - sprawdzamy event pause playera i czy jest w vieporcie - jezeli tak to znaczy ze to user spauzował
- interactByUser: false,
- videoHeight: 229,
- place: null,
- infoBlock: null, // warstwa pokazywana podczas pauzy i zakończenia video
- header: null, // tytuł oraz logo
- video: null,
- linksArray: null,
- /**
- * @returns {string}
- */
- getCookieName: function() {
- return 'videos-id-' + this.getWorkId();
- },
- getWorkId: function () {
- return workId;
- },
- isAutoplay: function () {
- return true; //config.videonewsAutoplay || false;
- },
- wasWatched: function () {
- // w celu możliwości testowania, bez konieczności czyszczenia localstorage
- if (config.videonewsTestVideonewsWorkId && config.videonewsTestVideonewsWorkId > 0 ) {
- return false;
- }
- // sprawdzamy czy istnieje ciacho czyli czy już odtwarzał wcześniej - jeżeli tak to pomijamy
- return Inpl.storage(this.getCookieName()) ? true : false;
- },
- getVideoUrl: function () {
- return null;
- },
- getPosterUrl: function () {
- return config.tracks[0].poster;
- },
- getTitle: function () {
- return config.tracks[0].data.title;
- },
- getNthDataSrc: function (i) {
- var images = doc.querySelectorAll('#videos li a.interia-tv img');
- var dataSrcArray = []
- images.forEach( function( item, index ) {
- dataSrcArray[index] = item.getAttribute('data-src');
- });
- return function () {
- return dataSrcArray[i]
- };
- },
- setWatched: function () {
- var expire = new Date();
- expire.setTime(expire.getTime() + 86400000);
- Inpl.storage(this.getCookieName(), 1, {expires: expire});
- //doc.cookie = this.getCookieName() + '=' + 1 + '; expires=' + expire.toGMTString();
- },
- getPlace: function () {
- if (this.place !== null){
- return this.place;
- }
- return this.place = doc.getElementsByClassName('player-videos-wrapper')[0];
- },
- /**
- * Wyświetlamy planszę nad playerem
- *
- * @param {('pause'|'end')} type - typ planszy
- */
- showInfoBlock: function (type) {
- var that = this;
- var player = doc.getElementsByClassName('player-videos-wrapper')[0];
- if (this.infoBlock !== null){
- addClass(this.infoBlock, 'player-videonews__infoblock--visible');
- removeClass(this.infoBlock, 'player-videonews__infoblock--pause');
- removeClass(this.infoBlock, 'player-videonews__infoblock--end');
- addClass(this.infoBlock, 'player-videonews__infoblock--' + type);
- return;
- }
- //pierwszy raz - rendering
- var thumb;
- append(
- this.getPlace(),
- this.infoBlock = append(
- domEle('div', 'player-videonews__infoblock player-videonews__infoblock--visible player-videonews__infoblock--' + type),
- append(
- thumb = domEle('div', 'player-videonews__infoblock-thumb'),
- domEle('div', 'player-videonews__infoblock-thumb-ico'),
- domEle('img', 'player-videonews__infoblock-thumb-img', null, {src: that.getPosterUrl()})
- ),
- append(
- domEle('div', 'player-videonews__infoblock-info'),
- domEle('h2', 'player-videonews__infoblock-title', that.getTitle()),
- domEle('a', 'player-videonews__infoblock-share', 'Udostępnij', {href: 'http://www.facebook.com/sharer/sharer.php?u=' + encodeURI(that.getVideoUrl()), target: '_blank'})
- )
- )
- );
- // play i replay
- event(thumb, 'click', function () {
- that.video.play();
- });
- },
- /**
- * Ukrywamy planszę
- */
- hideInfoBlock: function () {
- if (this.infoBlock !== null){
- removeClass(this.infoBlock, 'player-videonews__infoblock--visible');
- }
- },
- /**
- * Renderujemy tytuł filmu i logo
- * @param {String} title
- */
- renderHeader: function (title) {
- append(
- this.getPlace(),
- this.header = append(
- domEle('div', 'player-videonews__header player-videonews__header--visible'),
- domEle('div', 'player-videonews__title', title),
- domEle('img', 'player-videonews__logo', null, {src: '/i/videonews/logo.svg'})
- )
- );
- },
- getIwaConfig: function () {
- return {
- attachment_id: this.video._attachmentId,
- title: this.video._attachmentTitle,
- origin: this.video._attachmentOrigin? this.video._attachmentOrigin : ''
- }
- },
- /**
- *
- * @param data
- * @return {{}}
- */
- prepareTrackData: function (data) {
- var preparedSrc = { lo: [], hi: [] };
- if ( data.src.hi[0].src ) { preparedSrc.hi.push( { type: 'video/mp4', src: data.src.hi[0].src } ); }
- if ( data.src.lo[0].src ) { preparedSrc.lo.push( { type: 'video/mp4', src: data.src.lo[0].src } ); }
- return {
- title: data.data.title,
- src: preparedSrc,
- width: data.data.width,
- height: data.data.height,
- poster: data.data.poster,
- data: {
- title: data.data.title,
- id: data.data.id,
- desc: data.data.desc,
- url: data.data.url
- }
- };
- },
- addTracksToTracklist: function (trackList, tracksData) {
- var trackData;
- var i;
- for (i = 1; i < 3; i += 1) {
- trackData = this.prepareTrackData(tracksData[i]);
- trackList.append(trackList.createTrackItem( trackData ));
- }
- },
- shiftLinks: function (linksArr) {
- linksArr.unshift(linksArr.pop());
- return linksArr;
- },
- updateLinkElements: function (trackList, linksArray) {
- var adAmount = 0;
- trackList.getAll().forEach( function( item, index ) {
- if (item._isAd) {
- adAmount += 1;
- return;
- }
- index = adAmount ? index - adAmount : index;
- linksArray[index].innerHTML = '';
- !hasClass(linksArray[index], 'player-videos-link') ? append(linksArray[index], domEle('img', 'lazy videos-img', item._data.title, {src: item._poster}), domEle('span', null, item._data.title)) : linksArray[index].innerHTML = item._data.title;;
- linksArray[index].href = item._data.url;
- } );
- },
- rotateTracklist: function (trackList, linkElements) {
- if (trackList.getCurrentTrack()._hasAd || !trackList || !linkElements) {
- return;
- }
- var that = this;
- if (!that.linksArray) {
- that.linksArray = toArray(linkElements);
- }
- that.linksArray = that.shiftLinks(that.linksArray);
- that.updateLinkElements(trackList, that.linksArray);
- },
- getLinkElements: function () {
- return doc.querySelectorAll('a.interia-tv');
- },
- /**
- * Akcja inicjująca
- */
- init: function () {
- var that = this;
- // inicjalizacja playera.
- /** @type {global.Inpl.Video.Player} */
- var video = this.video = Inpl.Video.createInstance(config);
- that.renderHeader(config.videonewsTitle);
- var trackList = video.getTrackList();
- that.addTracksToTracklist(trackList, playlistData);
- // Logika odtwarzania video
- video.on('ended', function () {
- that.showInfoBlock('end');
- // jesli to ostatni element wideo ...
- if ( !trackList.isLastTrack() ) {
- video.nextAndPlay();
- that.rotateTracklist(trackList, that.getLinkElements());
- }
- });
- video.on('pause', function () {
- if (checkVieport()){
- that.pausedByUser = true;
- }
- that.showInfoBlock('pause');
- });
- video.on('play', function () {
- that.hideInfoBlock('pause');
- that.hideInfoBlock('end');
- });
- video.one('play', function () {
- //that.setWatched();
- });
- video.on('ready', function () {
- /** @type {global.Inpl.TrackItem} */
- var track = video.getCurrentTrack();
- track.curQuality('lo');
- this.trigger('changequality', track.curQuality());
- this._player.src( this.getTrackList().getCurrentTrack().getSrc() );
- video.muted(true);
- // dopiero teraz nasłuchujemy na zmiany usera (bo wcześniejsze (powyższe) wyciszenie by nam się tutaj złapało)
- setTimeout(function () {
- var prevVolume = video.muted() ? 0 : video.volume();
- function logIwa(eventName) {
- iwa('stream', 'content', eventName, that.getIwaConfig());
- }
- // trigerujemy event mousemove, aby tooltip był widoczny (automatycznie znika co sekundę) - inaczej tego nie idzie zrobić w łatwy sposób..
- var tooltipTimer = setInterval(function () {
- window.vjs.trigger(video._player.controlBar.buttonsPanel.volumeChange.el_, 'mousemove');
- }, 1000);
- video.on('volumechange', function () {
- that.interactByUser = true;
- if (!iwa){
- return;
- }
- clearInterval(tooltipTimer);
- var currentVolume = video.volume();
- if (prevVolume == 0 && currentVolume > 0 ){
- logIwa('muteoff');
- } else if (prevVolume > 0 && currentVolume == 0 ){
- logIwa('muteon');
- }
- prevVolume = currentVolume;
- });
- }, 100);
- if (that.isAutoplay()){
- monitorViewport();
- event(window, 'scroll', throttle(monitorViewport, 300));
- }
- });
- video.on('fullscreenchange', function () {
- /** @type {global.Inpl.TrackItem} */
- var track = video.getCurrentTrack();
- // zmieniamy jakość materiału na wysoką
- var currentQuality = track.curQuality();
- if (currentQuality !== 'hi'){
- track.curQuality('hi');
- this.trigger('changequality', track.curQuality());
- this._player.src( this.getTrackList().getCurrentTrack().getSrc() );
- }
- //włączamy dźwięk
- video.muted(false);
- video.play();
- if (this.isFullScreen() && iwa) {
- iwa('stream', 'content', 'fullscreen', that.getIwaConfig());
- }
- });
- /**
- * Sprawdza viewport i odpal player tylko gdy widoczny
- * @returns {boolean} - czy w viewporcie
- */
- var monitorViewport = function () {
- if (checkVieport()){
- // player widoczny dla uzytkownika więc gdy wcześniej ręcznie nie pauzował to go włączamy
- if (that.pausedByUser === false){
- video.play();
- }
- } else {
- if (that.pausedByUser === false && that.interactByUser === false){
- video.pause();
- }
- }
- };
- var checkVieport = function () {
- var pos = videojs.findPosition(video.getContainer());
- var srollTop = typeof window.pageYOffset != 'undefined' ? window.pageYOffset: document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop? document.body.scrollTop:0;
- if (srollTop + window.innerHeight - that.videoHeight/2 > pos.top && pos.top + that.videoHeight/2 > srollTop ){
- return true;
- } else {
- return false;
- }
- };
- }
- };
- event(window, 'load', function () {
- setTimeout(function () {
- Inpl.videos.init();
- }, 500);
- });
- })(window, document);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement