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 * Requires at least: 4.0
* Tested up to: 4.9 * Tested up to: 4.9
* Requires PHP: 5.2 * Requires PHP: 5.2
* Stable tag: 1.0.0-alpha * Stable tag: 1.0.0-beta
* License: GPLv2 or later * License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html * License URI: http://www.gnu.org/licenses/gpl-2.0.html

View File

@ -1,6 +1,6 @@
{ {
"name": "stklcode/wp-liveticker2", "name": "stklcode/wp-liveticker2",
"version": "1.0.0-alpha", "version": "1.0.0-beta",
"description": "A simple Liveticker for Wordpress.", "description": "A simple Liveticker for Wordpress.",
"keywords": [ "keywords": [
"wordpress", "wordpress",
@ -32,7 +32,8 @@
"wp-coding-standards/wpcs": "~0.14", "wp-coding-standards/wpcs": "~0.14",
"patchwork/jsqueeze": "^2.0.5", "patchwork/jsqueeze": "^2.0.5",
"natxet/CssMin": "^3.0.5", "natxet/CssMin": "^3.0.5",
"matthiasmullie/minify": "^1.3" "matthiasmullie/minify": "^1.3",
"npm-asset/eslint-config-wordpress": "^2.0"
}, },
"scripts": { "scripts": {
"post-install-cmd": [ "post-install-cmd": [
@ -70,5 +71,11 @@
"minifycss styles/wp-liveticker2.css > styles/wp-liveticker2.min.css", "minifycss styles/wp-liveticker2.css > styles/wp-liveticker2.min.css",
"minifyjs scripts/wp-liveticker2.js > scripts/wp-liveticker2.min.js" "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. // Add endpoint to script.
wp_localize_script( wp_localize_script(
'wplt2-js', 'wplt2-js',
'ajax_object', 'wplt2Ajax',
array( array(
'ajax_url' => admin_url( 'admin-ajax.php' ), 'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'wplt2_update-ticks' ), 'nonce' => wp_create_nonce( 'wplt2_update-ticks' ),

View File

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

View File

@ -1,13 +1,28 @@
/**
* Contructor of the WPLT object.
*
* @constructor
*/
function WPLT2() { function WPLT2() {
} }
/** /**
* Initialize WP-Liveticker 2 JS component. * Initialize WP-Liveticker 2 JS component.
* *
* @return void * @return {void}
*/ */
WPLT2.init = function () { 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. // Get ticker elements.
WPLT2.ticker = [].map.call( WPLT2.ticker = [].map.call(
document.querySelectorAll( 'ul.wplt2-ticker-ajax' ), 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. // 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 ); setTimeout( WPLT2.update, WPLT2.pollInterval );
} }
}; };
@ -48,13 +58,14 @@ WPLT2.init = function () {
/** /**
* Update liveticker on current page via AJAX call. * Update liveticker on current page via AJAX call.
* *
* @return void * @return {void}
*/ */
WPLT2.update = function () { WPLT2.update = function() {
// Extract ticker-slug, limit and timestamp of last poll. // Extract ticker-slug, limit and timestamp of last poll.
var updateReq = 'action=wplt2_update-ticks&_ajax_nonce=' + WPLT2.nonce; var updateReq = 'action=wplt2_update-ticks&_ajax_nonce=' + WPLT2.nonce;
var i; var i, j;
var j; var xhr = new XMLHttpRequest();
for ( i = 0; i < WPLT2.ticker.length; i++ ) { for ( i = 0; i < WPLT2.ticker.length; i++ ) {
updateReq = updateReq + updateReq = updateReq +
@ -70,39 +81,38 @@ WPLT2.update = function () {
} }
// Issue AJAX request. // Issue AJAX request.
var xhr = new XMLHttpRequest();
xhr.open( 'POST', WPLT2.ajaxURL, true ); xhr.open( 'POST', WPLT2.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() {
if( XMLHttpRequest.DONE === this.readyState && 200 === this.status ) { var update;
if ( XMLHttpRequest.DONE === this.readyState && 200 === this.status ) {
try { try {
var update = JSON.parse( this.responseText ); update = JSON.parse( this.responseText );
if ( update ) { if ( update ) {
update.forEach( update.forEach(
function( u ) { function( u ) {
WPLT2.ticker.forEach( WPLT2.ticker.forEach(
function( t ) { function( t ) {
if ( t.s === u.s ) { if ( t.s === u.s ) {
// Update last poll timestamp. t.t = u.t; // Update last poll timestamp.
WPLT2.updateHTML( t, u ); // Update HTML markup.
}
}
);
WPLT2.widgets.forEach(
function( t ) {
if ( t.w === u.w ) {
t.t = u.t; t.t = u.t;
// Update HTML markup.
WPLT2.updateHTML( t, u ); WPLT2.updateHTML( t, u );
} }
} }
); );
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 ); // Re-trigger update.
setTimeout( WPLT2.update, WPLT2.pollInterval );
} catch ( e ) { } 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. * Do actual update of HTML code.
* *
* @param t Ticker or Widget reference. * @param {Object} t Ticker or Widget reference.
* @param u Update entity. * @param {number} t.l Limit of entries to display.
* @return void * @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 ) { WPLT2.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.setAttribute( 'data-wplt2-last', u.t ); t.e.setAttribute( 'data-wplt2-last', u.t );
// Remove tail, if limit is set. // Remove tail, if limit is set.
if ( t.l > 0 ) { if ( 0 < t.l ) {
[].slice.call( t.e.getElementsByTagName( 'li' ), t.l ).forEach( [].slice.call( t.e.getElementsByTagName( 'li' ), t.l ).forEach(
function( li ) { function( li ) {
li.remove(); li.remove();
@ -133,7 +149,6 @@ WPLT2.updateHTML = function( t, u ) {
document.addEventListener( document.addEventListener(
'DOMContentLoaded', 'DOMContentLoaded',
function() { function() {
// Trigger periodic update of livetickers. WPLT2.init(); // Trigger periodic update of livetickers.
WPLT2.init();
} }
); );