kollapsminoriteten/wp-content/plugins/jetpack/modules/widgets/milestone/milestone.js

60 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-11-15 23:26:29 +01:00
/* global MilestoneConfig */
2020-09-15 14:30:05 +02:00
var Milestone = ( function () {
var Milestone = function ( args ) {
2020-05-06 17:20:49 +02:00
var widget_content = document.getElementById( args.content_id ),
2019-11-15 23:26:29 +01:00
id = args.id,
refresh = args.refresh * 1000;
2020-09-15 14:30:05 +02:00
this.timer = function () {
2019-11-15 23:26:29 +01:00
var instance = this;
2020-05-06 17:20:49 +02:00
var xhr = new XMLHttpRequest();
2019-11-15 23:26:29 +01:00
2020-09-15 14:30:05 +02:00
xhr.onload = function () {
2020-05-06 17:20:49 +02:00
var response = JSON.parse( xhr.responseText ),
httpCheck = xhr.status >= 200 && xhr.status < 300,
responseCheck =
'undefined' !== typeof response.message && 'undefined' !== typeof response.refresh;
if ( httpCheck && responseCheck ) {
var countdownElement = widget_content.querySelector( '.milestone-countdown' );
countdownElement.outerHTML = response.message;
refresh = response.refresh * 1000;
2019-11-15 23:26:29 +01:00
if ( ! refresh ) {
return;
}
2020-09-15 14:30:05 +02:00
setTimeout( function () {
2019-11-15 23:26:29 +01:00
instance.timer();
}, refresh );
2020-05-06 17:20:49 +02:00
}
};
xhr.open( 'GET', MilestoneConfig.api_root + 'jetpack/v4/widgets/' + id );
xhr.send();
2019-11-15 23:26:29 +01:00
};
if ( refresh > 0 ) {
this.timer();
}
};
2020-09-15 14:30:05 +02:00
return function ( args ) {
2019-11-15 23:26:29 +01:00
return new Milestone( args );
};
2020-05-06 17:20:49 +02:00
} )();
2019-11-15 23:26:29 +01:00
2020-09-15 14:30:05 +02:00
( function () {
2019-11-15 23:26:29 +01:00
var i,
MilestoneInstances = {};
if ( typeof MilestoneConfig === 'undefined' ) {
return;
}
for ( i = 0; i < MilestoneConfig.instances.length; i++ ) {
MilestoneInstances[ i ] = new Milestone( MilestoneConfig.instances[ i ] );
}
} )();