| 1 |
/* global MilestoneConfig */ |
| 2 |
|
| 3 |
var Milestone = ( function () { |
| 4 |
// eslint-disable-next-line no-shadow |
| 5 |
var Milestone = function ( args ) { |
| 6 |
var widget_content = document.getElementById( args.content_id ), |
| 7 |
id = args.id, |
| 8 |
refresh = args.refresh * 1000; |
| 9 |
|
| 10 |
this.timer = function () { |
| 11 |
var instance = this; |
| 12 |
var xhr = new XMLHttpRequest(); |
| 13 |
|
| 14 |
xhr.onload = function () { |
| 15 |
var response = JSON.parse( xhr.responseText ), |
| 16 |
httpCheck = xhr.status >= 200 && xhr.status < 300, |
| 17 |
responseCheck = |
| 18 |
'undefined' !== typeof response.message && 'undefined' !== typeof response.refresh; |
| 19 |
|
| 20 |
if ( httpCheck && responseCheck ) { |
| 21 |
var countdownElement = widget_content.querySelector( '.milestone-countdown' ); |
| 22 |
|
| 23 |
countdownElement.outerHTML = response.message; |
| 24 |
refresh = response.refresh * 1000; |
| 25 |
|
| 26 |
if ( ! refresh ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
setTimeout( function () { |
| 31 |
instance.timer(); |
| 32 |
}, refresh ); |
| 33 |
} |
| 34 |
}; |
| 35 |
|
| 36 |
xhr.open( 'GET', MilestoneConfig.api_root + 'jetpack/v4/widgets/' + id ); |
| 37 |
xhr.send(); |
| 38 |
}; |
| 39 |
|
| 40 |
if ( refresh > 0 ) { |
| 41 |
this.timer(); |
| 42 |
} |
| 43 |
}; |
| 44 |
return function ( args ) { |
| 45 |
return new Milestone( args ); |
| 46 |
}; |
| 47 |
} )(); |
| 48 |
|
| 49 |
( function () { |
| 50 |
var i, |
| 51 |
MilestoneInstances = {}; |
| 52 |
|
| 53 |
if ( typeof MilestoneConfig === 'undefined' ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
for ( i = 0; i < MilestoneConfig.instances.length; i++ ) { |
| 58 |
MilestoneInstances[ i ] = new Milestone( MilestoneConfig.instances[ i ] ); |
| 59 |
} |
| 60 |
} )(); |
| 61 |
|