Renamed AJAX variable, JS code style, added ESLint config

This commit is contained in:
Stefan Kalscheuer 2018-10-17 20:42:10 +02:00
parent c3cc5b1b84
commit 6f1cac5404
6 changed files with 64 additions and 39 deletions

3
.eslintrc.json Normal file
View File

@ -0,0 +1,3 @@
{
"extends": "./vendor/npm-asset/eslint-config-wordpress/index.js"
}

View File

@ -5,7 +5,7 @@
* Requires at least: 4.0
* Tested up to: 4.9
* Requires PHP: 5.2
* Stable tag: 1.0.0-alpha
* Stable tag: 1.0.0-beta
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html

View File

@ -1,6 +1,6 @@
{
"name": "stklcode/wp-liveticker2",
"version": "1.0.0-alpha",
"version": "1.0.0-beta",
"description": "A simple Liveticker for Wordpress.",
"keywords": [
"wordpress",
@ -32,7 +32,8 @@
"wp-coding-standards/wpcs": "~0.14",
"patchwork/jsqueeze": "^2.0.5",
"natxet/CssMin": "^3.0.5",
"matthiasmullie/minify": "^1.3"
"matthiasmullie/minify": "^1.3",
"npm-asset/eslint-config-wordpress": "^2.0"
},
"scripts": {
"post-install-cmd": [
@ -70,5 +71,11 @@
"minifycss styles/wp-liveticker2.css > styles/wp-liveticker2.min.css",
"minifyjs scripts/wp-liveticker2.js > scripts/wp-liveticker2.min.js"
]
},
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
}
]
}

View File

@ -274,7 +274,7 @@ class WPLiveticker2 {
// Add endpoint to script.
wp_localize_script(
'wplt2-js',
'ajax_object',
'wplt2Ajax',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'wplt2_update-ticks' ),

View File

@ -1,6 +1,6 @@
{
"name": "wp-liveticker2",
"version": "1.0.0-alpha",
"version": "1.0.0-beta",
"description": "A simple Liveticker for Wordpress.",
"author": "Stefan Kalscheuer",
"license": "GPL-2.0+"

View File

@ -1,13 +1,28 @@
/**
* Contructor of the WPLT object.
*
* @constructor
*/
function WPLT2() {
}
/**
* Initialize WP-Liveticker 2 JS component.
*
* @return void
* @return {void}
*/
WPLT2.init = function() {
// Opt out if AJAX pobject not present.
if ( 'undefined' === typeof wplt2Ajax ) {
return;
}
// Extract AJAX settings.
WPLT2.ajaxURL = wplt2Ajax.ajax_url;
WPLT2.nonce = wplt2Ajax.nonce;
WPLT2.pollInterval = wplt2Ajax.poll_interval;
// Get ticker elements.
WPLT2.ticker = [].map.call(
document.querySelectorAll( 'ul.wplt2-ticker-ajax' ),
@ -34,13 +49,8 @@ WPLT2.init = function () {
}
);
// Extract AJAX settings.
WPLT2.ajaxURL = ajax_object.ajax_url;
WPLT2.nonce = ajax_object.nonce;
WPLT2.pollInterval = ajax_object.poll_interval;
// Trigger update, if necessary.
if ( ( WPLT2.ticker.length > 0 || WPLT2.widgets.length ) && WPLT2.pollInterval > 0 ) {
if ( ( 0 < WPLT2.ticker.length || WPLT2.widgets.length ) && 0 < WPLT2.pollInterval ) {
setTimeout( WPLT2.update, WPLT2.pollInterval );
}
};
@ -48,13 +58,14 @@ WPLT2.init = function () {
/**
* Update liveticker on current page via AJAX call.
*
* @return void
* @return {void}
*/
WPLT2.update = function() {
// Extract ticker-slug, limit and timestamp of last poll.
var updateReq = 'action=wplt2_update-ticks&_ajax_nonce=' + WPLT2.nonce;
var i;
var j;
var i, j;
var xhr = new XMLHttpRequest();
for ( i = 0; i < WPLT2.ticker.length; i++ ) {
updateReq = updateReq +
@ -70,39 +81,38 @@ WPLT2.update = function () {
}
// Issue AJAX request.
var xhr = new XMLHttpRequest();
xhr.open( 'POST', WPLT2.ajaxURL, true );
xhr.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded;' );
xhr.onreadystatechange = function() {
var update;
if ( XMLHttpRequest.DONE === this.readyState && 200 === this.status ) {
try {
var update = JSON.parse( this.responseText );
update = JSON.parse( this.responseText );
if ( update ) {
update.forEach(
function( u ) {
WPLT2.ticker.forEach(
function( t ) {
if ( t.s === u.s ) {
// Update last poll timestamp.
t.t = u.t;
// Update HTML markup.
WPLT2.updateHTML( t, u );
t.t = u.t; // Update last poll timestamp.
WPLT2.updateHTML( t, u ); // Update HTML markup.
}
}
);
WPLT2.widgets.forEach( function ( t ) {
WPLT2.widgets.forEach(
function( t ) {
if ( t.w === u.w ) {
t.t = u.t;
WPLT2.updateHTML( t, u );
}
} );
}
);
}
// Re-trigger update.
setTimeout( WPLT2.update, WPLT2.pollInterval );
);
}
setTimeout( WPLT2.update, WPLT2.pollInterval ); // Re-trigger update.
} catch ( e ) {
console.warn( 'WP-Liveticker 2 AJAX update failed, stopping automatic updates.' )
console.warn( 'WP-Liveticker 2 AJAX update failed, stopping automatic updates.' );
}
}
};
@ -112,16 +122,22 @@ WPLT2.update = function () {
/**
* Do actual update of HTML code.
*
* @param t Ticker or Widget reference.
* @param u Update entity.
* @return void
* @param {Object} t Ticker or Widget reference.
* @param {number} t.l Limit of entries to display.
* @param {HTMLElement} t.e HTML element of the ticker/widget.
* @param {Object} u Update entity.
* @param {string} u.h HTML code to append.
* @param {number} u.t Timetsamp of last update.
* @return {void}
*/
WPLT2.updateHTML = function( t, u ) {
// Prepend HTML of new ticks.
t.e.innerHTML = u.h + t.e.innerHTML;
t.e.setAttribute( 'data-wplt2-last', u.t );
// Remove tail, if limit is set.
if ( t.l > 0 ) {
if ( 0 < t.l ) {
[].slice.call( t.e.getElementsByTagName( 'li' ), t.l ).forEach(
function( li ) {
li.remove();
@ -133,7 +149,6 @@ WPLT2.updateHTML = function( t, u ) {
document.addEventListener(
'DOMContentLoaded',
function() {
// Trigger periodic update of livetickers.
WPLT2.init();
WPLT2.init(); // Trigger periodic update of livetickers.
}
);