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