wrap frontend JS into IIFE to
Exposing the liveticker functionality to a public namespace is not necessary, so it is now wrapped into an anonymous function.
This commit is contained in:
parent
b88e1c2903
commit
acf3b010f1
@ -3,15 +3,19 @@
|
|||||||
*
|
*
|
||||||
* @class
|
* @class
|
||||||
*/
|
*/
|
||||||
function scLiveticker() {
|
( function() {
|
||||||
}
|
var ajaxURL = sclivetickerAjax.ajax_url;
|
||||||
|
var nonce = sclivetickerAjax.nonce;
|
||||||
|
var pollInterval = sclivetickerAjax.poll_interval;
|
||||||
|
var ticker;
|
||||||
|
var widgets;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize iveticker JS component.
|
* Initialize iveticker JS component.
|
||||||
*
|
*
|
||||||
* @return {void}
|
* @return {void}
|
||||||
*/
|
*/
|
||||||
scLiveticker.init = function() {
|
var init = function() {
|
||||||
var updateNow = false;
|
var updateNow = false;
|
||||||
|
|
||||||
// Opt out if AJAX pobject not present.
|
// Opt out if AJAX pobject not present.
|
||||||
@ -20,12 +24,12 @@ scLiveticker.init = function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Extract AJAX settings.
|
// Extract AJAX settings.
|
||||||
scLiveticker.ajaxURL = sclivetickerAjax.ajax_url;
|
ajaxURL = sclivetickerAjax.ajax_url;
|
||||||
scLiveticker.nonce = sclivetickerAjax.nonce;
|
nonce = sclivetickerAjax.nonce;
|
||||||
scLiveticker.pollInterval = sclivetickerAjax.poll_interval;
|
pollInterval = sclivetickerAjax.poll_interval;
|
||||||
|
|
||||||
// Get ticker elements.
|
// Get ticker elements.
|
||||||
scLiveticker.ticker = [].map.call(
|
ticker = [].map.call(
|
||||||
document.querySelectorAll( 'div.wp-block-scliveticker-ticker.sclt-ajax' ),
|
document.querySelectorAll( 'div.wp-block-scliveticker-ticker.sclt-ajax' ),
|
||||||
function( elem ) {
|
function( elem ) {
|
||||||
var list = elem.querySelector( 'ul' );
|
var list = elem.querySelector( 'ul' );
|
||||||
@ -50,7 +54,7 @@ scLiveticker.init = function() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Get widget elements.
|
// Get widget elements.
|
||||||
scLiveticker.widgets = [].map.call(
|
widgets = [].map.call(
|
||||||
document.querySelectorAll( 'div.wp-widget-scliveticker-ticker.sclt-ajax' ),
|
document.querySelectorAll( 'div.wp-widget-scliveticker-ticker.sclt-ajax' ),
|
||||||
function( elem ) {
|
function( elem ) {
|
||||||
var list = elem.querySelector( 'ul' );
|
var list = elem.querySelector( 'ul' );
|
||||||
@ -75,70 +79,70 @@ scLiveticker.init = function() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Trigger update, if necessary.
|
// Trigger update, if necessary.
|
||||||
if ( ( 0 < scLiveticker.ticker.length || scLiveticker.widgets.length ) && 0 < scLiveticker.pollInterval ) {
|
if ( ( 0 < ticker.length || widgets.length ) && 0 < pollInterval ) {
|
||||||
if ( updateNow ) {
|
if ( updateNow ) {
|
||||||
scLiveticker.update();
|
update();
|
||||||
} else {
|
} else {
|
||||||
setTimeout( scLiveticker.update, scLiveticker.pollInterval );
|
setTimeout( update, pollInterval );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update liveticker on current page via AJAX call.
|
* Update liveticker on current page via AJAX call.
|
||||||
*
|
*
|
||||||
* @return {void}
|
* @return {void}
|
||||||
*/
|
*/
|
||||||
scLiveticker.update = function() {
|
var update = function() {
|
||||||
// Extract ticker-slug, limit and timestamp of last poll.
|
// Extract ticker-slug, limit and timestamp of last poll.
|
||||||
var updateReq = 'action=sclt_update-ticks&_ajax_nonce=' + scLiveticker.nonce;
|
var updateReq = 'action=sclt_update-ticks&_ajax_nonce=' + nonce;
|
||||||
var i, j;
|
var i, j;
|
||||||
var xhr = new XMLHttpRequest();
|
var xhr = new XMLHttpRequest();
|
||||||
|
|
||||||
for ( i = 0; i < scLiveticker.ticker.length; i++ ) {
|
for ( i = 0; i < ticker.length; i++ ) {
|
||||||
updateReq = updateReq +
|
updateReq = updateReq +
|
||||||
'&update[' + i + '][s]=' + scLiveticker.ticker[ i ].s +
|
'&update[' + i + '][s]=' + ticker[ i ].s +
|
||||||
'&update[' + i + '][l]=' + scLiveticker.ticker[ i ].l +
|
'&update[' + i + '][l]=' + ticker[ i ].l +
|
||||||
'&update[' + i + '][t]=' + scLiveticker.ticker[ i ].t;
|
'&update[' + i + '][t]=' + ticker[ i ].t;
|
||||||
}
|
}
|
||||||
for ( j = 0; j < scLiveticker.widgets.length; j++ ) {
|
for ( j = 0; j < widgets.length; j++ ) {
|
||||||
updateReq = updateReq +
|
updateReq = updateReq +
|
||||||
'&update[' + ( i + j ) + '][w]=' + scLiveticker.widgets[ j ].w +
|
'&update[' + ( i + j ) + '][w]=' + widgets[ j ].w +
|
||||||
'&update[' + ( i + j ) + '][l]=' + scLiveticker.widgets[ j ].l +
|
'&update[' + ( i + j ) + '][l]=' + widgets[ j ].l +
|
||||||
'&update[' + ( i + j ) + '][t]=' + scLiveticker.widgets[ j ].t;
|
'&update[' + ( i + j ) + '][t]=' + widgets[ j ].t;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Issue AJAX request.
|
// Issue AJAX request.
|
||||||
xhr.open( 'POST', scLiveticker.ajaxURL, true );
|
xhr.open( 'POST', ajaxURL, true );
|
||||||
xhr.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded;' );
|
xhr.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded;' );
|
||||||
xhr.onreadystatechange = function() {
|
xhr.onreadystatechange = function() {
|
||||||
var update;
|
var updateResp;
|
||||||
if ( XMLHttpRequest.DONE === this.readyState && 200 === this.status ) {
|
if ( XMLHttpRequest.DONE === this.readyState && 200 === this.status ) {
|
||||||
try {
|
try {
|
||||||
update = JSON.parse( this.responseText );
|
updateResp = JSON.parse( this.responseText );
|
||||||
if ( update ) {
|
if ( updateResp ) {
|
||||||
update.forEach(
|
updateResp.forEach(
|
||||||
function( u ) {
|
function( u ) {
|
||||||
scLiveticker.ticker.forEach(
|
ticker.forEach(
|
||||||
function( t ) {
|
function( t ) {
|
||||||
if ( t.s === u.s ) {
|
if ( t.s === u.s ) {
|
||||||
t.t = u.t; // Update last poll timestamp.
|
t.t = u.t; // Update last poll timestamp.
|
||||||
scLiveticker.updateHTML( t, u ); // Update HTML markup.
|
updateHTML( t, u ); // Update HTML markup.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
scLiveticker.widgets.forEach(
|
widgets.forEach(
|
||||||
function( t ) {
|
function( t ) {
|
||||||
if ( t.w === u.w ) {
|
if ( t.w === u.w ) {
|
||||||
t.t = u.t;
|
t.t = u.t;
|
||||||
scLiveticker.updateHTML( t, u );
|
updateHTML( t, u );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
setTimeout( scLiveticker.update, scLiveticker.pollInterval ); // Re-trigger update.
|
setTimeout( update, pollInterval ); // Re-trigger update.
|
||||||
} catch ( e ) {
|
} catch ( e ) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.warn( 'Liveticker AJAX update failed, stopping automatic updates.' );
|
console.warn( 'Liveticker AJAX update failed, stopping automatic updates.' );
|
||||||
@ -146,9 +150,9 @@ scLiveticker.update = function() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
xhr.send( updateReq );
|
xhr.send( updateReq );
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Do actual update of HTML code.
|
* Do actual update of HTML code.
|
||||||
*
|
*
|
||||||
* @param {Object} t Ticker or Widget reference.
|
* @param {Object} t Ticker or Widget reference.
|
||||||
@ -159,7 +163,7 @@ scLiveticker.update = function() {
|
|||||||
* @param {number} u.t Timetsamp of last update.
|
* @param {number} u.t Timetsamp of last update.
|
||||||
* @return {void}
|
* @return {void}
|
||||||
*/
|
*/
|
||||||
scLiveticker.updateHTML = function( t, u ) {
|
var updateHTML = function( t, u ) {
|
||||||
// Prepend HTML of new ticks.
|
// Prepend HTML of new ticks.
|
||||||
t.e.innerHTML = u.h + t.e.innerHTML;
|
t.e.innerHTML = u.h + t.e.innerHTML;
|
||||||
t.e.parentNode.setAttribute( 'data-sclt-last', u.t );
|
t.e.parentNode.setAttribute( 'data-sclt-last', u.t );
|
||||||
@ -172,11 +176,12 @@ scLiveticker.updateHTML = function( t, u ) {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
document.addEventListener(
|
document.addEventListener(
|
||||||
'DOMContentLoaded',
|
'DOMContentLoaded',
|
||||||
function() {
|
function() {
|
||||||
scLiveticker.init(); // Trigger periodic update of livetickers.
|
init(); // Trigger periodic update of livetickers.
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
}() );
|
||||||
|
Loading…
x
Reference in New Issue
Block a user