| 1 |
( function( $ ) { |
| 2 |
const Sync_Base = { |
| 3 |
id: 'sync-base', |
| 4 |
syncing: false, |
| 5 |
items: false, |
| 6 |
completed: false, |
| 7 |
callback: null, |
| 8 |
methodGetItems: '', |
| 9 |
itemsKey: '', |
| 10 |
chunkSize: 50, |
| 11 |
sync( callback ) { |
| 12 |
if ( this.syncing ) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
this.callback = callback; |
| 17 |
|
| 18 |
if ( this.items === false ) { |
| 19 |
this.get_items(); |
| 20 |
} else if ( ! this.dispatch() ) { |
| 21 |
this.completed = true; |
| 22 |
this.callToCallback(); |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
this.syncing = true; |
| 27 |
}, |
| 28 |
init() { |
| 29 |
this.syncing = false; |
| 30 |
this.items = false; |
| 31 |
this.completed = false; |
| 32 |
}, |
| 33 |
is_completed() { |
| 34 |
return this.completed; |
| 35 |
}, |
| 36 |
dispatch() { |
| 37 |
const that = this, |
| 38 |
items = this.items ? this.items.splice( 0, this.chunkSize ) : false; |
| 39 |
if ( ! items || items.length === 0 ) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
$.ajax( { |
| 43 |
url: '', |
| 44 |
data: { |
| 45 |
'lp-ajax': this.id, |
| 46 |
sync: items, |
| 47 |
}, |
| 48 |
method: 'post', |
| 49 |
success( response ) { |
| 50 |
response = LP.parseJSON( response ); |
| 51 |
that.syncing = false; |
| 52 |
if ( response.result !== 'success' ) { |
| 53 |
that.completed = true; |
| 54 |
} |
| 55 |
that.callToCallback(); |
| 56 |
if ( that.is_completed() ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
that.sync( that.callback ); |
| 61 |
}, |
| 62 |
} ); |
| 63 |
|
| 64 |
return true; |
| 65 |
}, |
| 66 |
callToCallback() { |
| 67 |
this.callback && this.callback.call( this ); |
| 68 |
}, |
| 69 |
get_items() { |
| 70 |
const that = this; |
| 71 |
$.ajax( { |
| 72 |
url: '', |
| 73 |
data: { |
| 74 |
'lp-ajax': this.id, |
| 75 |
sync: this.methodGetItems, |
| 76 |
}, |
| 77 |
success( response ) { |
| 78 |
that.syncing = false; |
| 79 |
response = LP.parseJSON( response ); |
| 80 |
if ( response[ that.itemsKey ] ) { |
| 81 |
that.items = response[ that.itemsKey ]; |
| 82 |
that.sync( that.callback ); |
| 83 |
} else { |
| 84 |
that.completed = true; |
| 85 |
that.items = []; |
| 86 |
that.callToCallback(); |
| 87 |
} |
| 88 |
}, |
| 89 |
} ); |
| 90 |
}, |
| 91 |
}; |
| 92 |
|
| 93 |
const Sync_Course_Orders = $.extend( {}, Sync_Base, { |
| 94 |
id: 'sync-course-orders', |
| 95 |
methodGetItems: 'get-courses', |
| 96 |
itemsKey: 'courses', |
| 97 |
} ); |
| 98 |
|
| 99 |
const Sync_User_Courses = $.extend( {}, Sync_Base, { |
| 100 |
id: 'sync-user-courses', |
| 101 |
methodGetItems: 'get-users', |
| 102 |
itemsKey: 'users', |
| 103 |
chunkSize: 500, |
| 104 |
} ); |
| 105 |
|
| 106 |
const Sync_User_Orders = $.extend( {}, Sync_Base, { |
| 107 |
id: 'sync-user-orders', |
| 108 |
methodGetItems: 'get-users', |
| 109 |
itemsKey: 'users', |
| 110 |
chunkSize: 500, |
| 111 |
} ); |
| 112 |
|
| 113 |
const Sync_Course_Final_Quiz = $.extend( {}, Sync_Base, { |
| 114 |
id: 'sync-course-final-quiz', |
| 115 |
methodGetItems: 'get-courses', |
| 116 |
itemsKey: 'courses', |
| 117 |
chunkSize: 500, |
| 118 |
} ); |
| 119 |
|
| 120 |
const Sync_Remove_Older_Data = $.extend( {}, Sync_Base, { |
| 121 |
id: 'sync-remove-older-data', |
| 122 |
methodGetItems: 'remove-older-data', |
| 123 |
itemsKey: '_nothing_here', |
| 124 |
chunkSize: 500, |
| 125 |
} ); |
| 126 |
|
| 127 |
const Sync_Calculate_Course_Results = $.extend( {}, Sync_Base, { |
| 128 |
id: 'sync-calculate-course-results', |
| 129 |
methodGetItems: 'get-users', |
| 130 |
itemsKey: 'users', |
| 131 |
chunkSize: 1, |
| 132 |
} ); |
| 133 |
|
| 134 |
window.LP_Sync_Data = { |
| 135 |
syncs: [], |
| 136 |
syncing: 0, |
| 137 |
options: {}, |
| 138 |
start( options ) { |
| 139 |
this.syncs = []; |
| 140 |
this.options = $.extend( { |
| 141 |
onInit() { |
| 142 |
}, |
| 143 |
onStart() { |
| 144 |
|
| 145 |
}, |
| 146 |
onCompleted() { |
| 147 |
|
| 148 |
}, |
| 149 |
onCompletedAll() { |
| 150 |
|
| 151 |
}, |
| 152 |
}, options || {} ); |
| 153 |
|
| 154 |
if ( ! this.get_syncs() ) { |
| 155 |
return; |
| 156 |
} |
| 157 |
this.reset(); |
| 158 |
this.options.onInit.call( this ); |
| 159 |
var that = this, |
| 160 |
syncing = 0, |
| 161 |
totalSyncs = this.syncs.length, |
| 162 |
syncCallback = function( $sync ) { |
| 163 |
if ( $sync.is_completed() ) { |
| 164 |
syncing++; |
| 165 |
that.options.onCompleted.call( that, $sync ); |
| 166 |
if ( syncing >= totalSyncs ) { |
| 167 |
that.options.onCompletedAll.call( that ); |
| 168 |
return; |
| 169 |
} |
| 170 |
that.sync( syncing, syncCallback ); |
| 171 |
} |
| 172 |
}; |
| 173 |
this.sync( syncing, syncCallback ); |
| 174 |
}, |
| 175 |
reset() { |
| 176 |
for ( const sync in this.syncs ) { |
| 177 |
try { |
| 178 |
this[ this.syncs[ sync ] ].init(); |
| 179 |
} catch ( e ) { |
| 180 |
} |
| 181 |
} |
| 182 |
}, |
| 183 |
sync( sync, callback ) { |
| 184 |
const that = this, |
| 185 |
$sync = this[ this.syncs[ sync ] ]; |
| 186 |
that.options.onStart.call( that, $sync ); |
| 187 |
$sync.sync( function() { |
| 188 |
callback.call( that, $sync ); |
| 189 |
} ); |
| 190 |
}, |
| 191 |
get_syncs() { |
| 192 |
const syncs = $( 'input[name^="lp-repair"]:checked' ).serializeJSON()[ 'lp-repair' ]; |
| 193 |
if ( ! syncs ) { |
| 194 |
return false; |
| 195 |
} |
| 196 |
|
| 197 |
for ( let sync in syncs ) { |
| 198 |
if ( syncs[ sync ] !== 'yes' ) { |
| 199 |
continue; |
| 200 |
} |
| 201 |
|
| 202 |
sync = sync.replace( /[-]+/g, '_' ); |
| 203 |
|
| 204 |
if ( ! this[ sync ] ) { |
| 205 |
continue; |
| 206 |
} |
| 207 |
|
| 208 |
this.syncs.push( sync ); |
| 209 |
} |
| 210 |
|
| 211 |
return this.syncs; |
| 212 |
}, |
| 213 |
get_sync( id ) { |
| 214 |
id = id.replace( /[-]+/g, '_' ); |
| 215 |
return this[ id ]; |
| 216 |
}, |
| 217 |
sync_course_orders: Sync_Course_Orders, |
| 218 |
sync_user_orders: Sync_User_Orders, |
| 219 |
sync_user_courses: Sync_User_Courses, |
| 220 |
sync_course_final_quiz: Sync_Course_Final_Quiz, |
| 221 |
sync_remove_older_data: Sync_Remove_Older_Data, |
| 222 |
sync_calculate_course_results: Sync_Calculate_Course_Results, |
| 223 |
}; |
| 224 |
|
| 225 |
$( document ).ready( function() { |
| 226 |
function initSyncs() { |
| 227 |
const $chkAll = $( '#learn-press-check-all-syncs' ), |
| 228 |
$chks = $( '#learn-press-syncs' ).find( '[name^="lp-repair"]' ); |
| 229 |
|
| 230 |
$chkAll.on( 'click', function() { |
| 231 |
$chks.prop( 'checked', this.checked ); |
| 232 |
} ); |
| 233 |
|
| 234 |
$chks.on( 'click', function() { |
| 235 |
$chkAll.prop( 'checked', $chks.filter( ':checked' ).length === $chks.length ); |
| 236 |
} ); |
| 237 |
} |
| 238 |
|
| 239 |
initSyncs(); |
| 240 |
} ).on( 'click', '.lp-button-repair', function() { |
| 241 |
function getInput( sync ) { |
| 242 |
return $( 'ul#learn-press-syncs' ).find( 'input[name*="' + sync + '"]' ); |
| 243 |
} |
| 244 |
|
| 245 |
LP_Sync_Data.start( { |
| 246 |
onInit() { |
| 247 |
$( 'ul#learn-press-syncs' ).children().removeClass( 'syncing synced' ); |
| 248 |
$( '.lp-button-repair' ).prop( 'disabled', true ); |
| 249 |
}, |
| 250 |
onStart( $sync ) { |
| 251 |
getInput( $sync.id ).closest( 'li' ).addClass( 'syncing' ); |
| 252 |
}, |
| 253 |
onCompleted( $sync ) { |
| 254 |
getInput( $sync.id ).closest( 'li' ).removeClass( 'syncing' ).addClass( 'synced' ); |
| 255 |
}, |
| 256 |
onCompletedAll() { |
| 257 |
$( 'ul#learn-press-syncs' ).children().removeClass( 'syncing synced' ); |
| 258 |
$( '.lp-button-repair' ).prop( 'disabled', false ); |
| 259 |
}, |
| 260 |
} ); |
| 261 |
} ); |
| 262 |
}( jQuery ) ); |
| 263 |
|