| 1 |
jQuery( document ).ready( function ( $ ) { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
// Variables |
| 5 |
var message = $( '#bbp-converter-message' ), |
| 6 |
stop = $( '#bbp-converter-stop' ), |
| 7 |
start = $( '#bbp-converter-start' ), |
| 8 |
restart = $( '#_bbp_converter_restart' ), |
| 9 |
status = $( '#bbp-converter-status' ), |
| 10 |
spinner = $( '#bbp-converter-spinner' ), |
| 11 |
settings = $( '#bbp-converter-settings' ), |
| 12 |
password = $( '#_bbp_converter_db_pass' ), |
| 13 |
toggle = $( '.bbp-db-pass-toggle' ), |
| 14 |
step_p = $( '#bbp-converter-step-percentage' ), |
| 15 |
total_p = $( '#bbp-converter-total-percentage' ), |
| 16 |
fields = settings.find( 'table:first-of-type input, table:first-of-type select' ); |
| 17 |
|
| 18 |
/** |
| 19 |
* Show/hide db password button toggle |
| 20 |
* |
| 21 |
* @since 2.6.0 bbPress (r6676) |
| 22 |
* |
| 23 |
* @param {element} e |
| 24 |
*/ |
| 25 |
toggle.on( 'click', function( e ) { |
| 26 |
var type = ( password.attr( 'type' ) === 'password' ) ? 'text' : 'password'; |
| 27 |
|
| 28 |
password.attr( 'type', type ); |
| 29 |
|
| 30 |
toggle |
| 31 |
.toggleClass( 'password' ) |
| 32 |
.toggleClass( 'text' ); |
| 33 |
|
| 34 |
e.preventDefault(); |
| 35 |
}); |
| 36 |
|
| 37 |
/** |
| 38 |
* Start button click |
| 39 |
* |
| 40 |
* @since 2.6.0 bbPress (r6470) |
| 41 |
* |
| 42 |
* @param {element} e |
| 43 |
*/ |
| 44 |
start.on( 'click', function( e ) { |
| 45 |
bbp_converter_user_start(); |
| 46 |
e.preventDefault(); |
| 47 |
} ); |
| 48 |
|
| 49 |
/** |
| 50 |
* Stop button click |
| 51 |
* |
| 52 |
* @since 2.6.0 bbPress (r6470) |
| 53 |
* |
| 54 |
* @param {element} e |
| 55 |
*/ |
| 56 |
$( stop ).on( 'click', function( e ) { |
| 57 |
bbp_converter_user_stop(); |
| 58 |
e.preventDefault(); |
| 59 |
} ); |
| 60 |
|
| 61 |
/** |
| 62 |
* Start the converter |
| 63 |
* |
| 64 |
* @since 2.6.0 bbPress (r6470) |
| 65 |
* |
| 66 |
* @returns {void} |
| 67 |
*/ |
| 68 |
function bbp_converter_user_start() { |
| 69 |
bbp_converter_start(); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Stop the converter |
| 74 |
* |
| 75 |
* @since 2.6.0 bbPress (r6470) |
| 76 |
* |
| 77 |
* @returns {void} |
| 78 |
*/ |
| 79 |
function bbp_converter_user_stop() { |
| 80 |
bbp_converter_stop( |
| 81 |
BBP_Converter.strings.button_continue, |
| 82 |
BBP_Converter.strings.import_stopped_user |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Return values of converter settings |
| 88 |
* |
| 89 |
* @since 2.6.0 bbPress (r6470) |
| 90 |
* |
| 91 |
* @returns {converterL#2.bbp_converter_settings.values} |
| 92 |
*/ |
| 93 |
function bbp_converter_settings() { |
| 94 |
var values = {}; |
| 95 |
|
| 96 |
$.each( settings.serializeArray(), function( i, field ) { |
| 97 |
values[ field.name ] = field.value; |
| 98 |
} ); |
| 99 |
|
| 100 |
if ( values['_bbp_converter_restart'] ) { |
| 101 |
restart.removeAttr( 'checked' ); |
| 102 |
} |
| 103 |
|
| 104 |
if ( values['_bbp_converter_delay_time'] ) { |
| 105 |
BBP_Converter.state.delay = parseInt( values['_bbp_converter_delay_time'], 10 ) * 1000; |
| 106 |
} |
| 107 |
|
| 108 |
values['action'] = 'bbp_converter_process'; |
| 109 |
values['_ajax_nonce'] = BBP_Converter.ajax_nonce; |
| 110 |
|
| 111 |
return values; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Run the converter step |
| 116 |
* |
| 117 |
* @since 2.6.0 bbPress (r6470) |
| 118 |
* |
| 119 |
* @returns {void} |
| 120 |
*/ |
| 121 |
function bbp_converter_post() { |
| 122 |
$.post( ajaxurl, bbp_converter_settings(), function( response ) { |
| 123 |
|
| 124 |
// Parse the json response |
| 125 |
try { |
| 126 |
var data = response.data; |
| 127 |
|
| 128 |
// Success |
| 129 |
if ( true === response.success ) { |
| 130 |
bbp_converter_step( data ); |
| 131 |
|
| 132 |
// Failure |
| 133 |
} else { |
| 134 |
bbp_converter_stop(); |
| 135 |
} |
| 136 |
|
| 137 |
} catch( e ) { |
| 138 |
bbp_converter_stop(); |
| 139 |
} |
| 140 |
}, 'json' ); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Process the next step |
| 145 |
* |
| 146 |
* @since 2.6.0 bbPress (r6600) |
| 147 |
* |
| 148 |
* @param {object} data |
| 149 |
* @returns {void} |
| 150 |
*/ |
| 151 |
function bbp_converter_step( data ) { |
| 152 |
|
| 153 |
// Bail if not running |
| 154 |
if ( ! BBP_Converter.state.running ) { |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
// Do the step |
| 159 |
bbp_converter_log( data.progress ); |
| 160 |
bbp_converter_percentages( data.step_percent, data.total_percent ); |
| 161 |
bbp_converter_status( data ); |
| 162 |
bbp_converter_wait(); |
| 163 |
|
| 164 |
// Done |
| 165 |
if ( data.current_step === data.final_step ) { |
| 166 |
bbp_converter_stop( |
| 167 |
BBP_Converter.strings.button_start, |
| 168 |
BBP_Converter.strings.import_complete |
| 169 |
); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Wait to do the next AJAX request |
| 175 |
* |
| 176 |
* @since 2.6.0 bbPress (r6600) |
| 177 |
* |
| 178 |
* @returns {void} |
| 179 |
*/ |
| 180 |
function bbp_converter_wait() { |
| 181 |
clearTimeout( BBP_Converter.state.running ); |
| 182 |
|
| 183 |
// Bail if not running |
| 184 |
if ( ! BBP_Converter.state.running ) { |
| 185 |
return; |
| 186 |
} |
| 187 |
|
| 188 |
// Wait, then POST |
| 189 |
BBP_Converter.state.running = setTimeout( function() { |
| 190 |
bbp_converter_post(); |
| 191 |
}, parseInt( BBP_Converter.state.delay, 10 ) ); |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Start the converter and set the various flags |
| 196 |
* |
| 197 |
* @since 2.6.0 bbPress (r6600) |
| 198 |
* |
| 199 |
* @returns {void} |
| 200 |
*/ |
| 201 |
function bbp_converter_start() { |
| 202 |
clearTimeout( BBP_Converter.state.running ); |
| 203 |
clearInterval( BBP_Converter.state.status ); |
| 204 |
|
| 205 |
BBP_Converter.state.running = true; |
| 206 |
|
| 207 |
var log = BBP_Converter.strings.start_continue; |
| 208 |
if ( false === BBP_Converter.state.started ) { |
| 209 |
log = BBP_Converter.strings.start_start; |
| 210 |
BBP_Converter.state.started = true; |
| 211 |
} |
| 212 |
|
| 213 |
bbp_converter_update( |
| 214 |
BBP_Converter.strings.button_continue, |
| 215 |
log, |
| 216 |
BBP_Converter.strings.status_starting |
| 217 |
); |
| 218 |
|
| 219 |
message.addClass( 'started' ); |
| 220 |
|
| 221 |
start.hide(); |
| 222 |
stop.show(); |
| 223 |
|
| 224 |
spinner.css( 'visibility', 'visible' ); |
| 225 |
fields.prop( 'readonly', true ); |
| 226 |
|
| 227 |
bbp_converter_post(); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Stop the converter, and update the UI |
| 232 |
* |
| 233 |
* @since 2.6.0 bbPress (r6470) |
| 234 |
* |
| 235 |
* @param {string} button New text for button |
| 236 |
* @param {string} log New text to add to import monitor |
| 237 |
* |
| 238 |
* @returns {void} |
| 239 |
*/ |
| 240 |
function bbp_converter_stop( button, log ) { |
| 241 |
clearTimeout( BBP_Converter.state.running ); |
| 242 |
clearInterval( BBP_Converter.state.status ); |
| 243 |
|
| 244 |
BBP_Converter.state.running = false; |
| 245 |
BBP_Converter.state.status = false; |
| 246 |
|
| 247 |
if ( ! button ) { |
| 248 |
button = BBP_Converter.strings.button_continue; |
| 249 |
} |
| 250 |
|
| 251 |
if ( ! log ) { |
| 252 |
log = BBP_Converter.strings.status_stopped; |
| 253 |
} |
| 254 |
|
| 255 |
bbp_converter_update( |
| 256 |
button, |
| 257 |
log, |
| 258 |
BBP_Converter.strings.status_stopped |
| 259 |
); |
| 260 |
|
| 261 |
start.show(); |
| 262 |
stop.hide(); |
| 263 |
|
| 264 |
spinner.css( 'visibility', 'hidden' ); |
| 265 |
fields.prop( 'readonly', false ); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Update the various screen texts |
| 270 |
* |
| 271 |
* @since 2.6.0 bbPress (r6600) |
| 272 |
* |
| 273 |
* @param {string} b_text |
| 274 |
* @param {string} p_text |
| 275 |
* @param {string} s_text |
| 276 |
* |
| 277 |
* @returns {void} |
| 278 |
*/ |
| 279 |
function bbp_converter_update( b_text, p_text, s_text ) { |
| 280 |
start.val( b_text ); |
| 281 |
bbp_converter_log( p_text ); |
| 282 |
status.text( s_text ); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Update the status |
| 287 |
* |
| 288 |
* @since 2.6.0 bbPress (r6513) |
| 289 |
* |
| 290 |
* @param {object} data |
| 291 |
* |
| 292 |
* @returns {void} |
| 293 |
*/ |
| 294 |
function bbp_converter_status( data ) { |
| 295 |
var remaining = parseInt( BBP_Converter.state.delay, 10 ) / 1000; |
| 296 |
|
| 297 |
status.text( BBP_Converter.strings.status_counting.replace( '%s', remaining ) ); |
| 298 |
clearInterval( BBP_Converter.state.status ); |
| 299 |
|
| 300 |
BBP_Converter.state.status = setInterval( function() { |
| 301 |
remaining--; |
| 302 |
status.text( BBP_Converter.strings.status_counting.replace( '%s', remaining ) ); |
| 303 |
|
| 304 |
if ( remaining <= 0 ) { |
| 305 |
clearInterval( BBP_Converter.state.status ); |
| 306 |
|
| 307 |
if ( parseInt( data.current_step, 10 ) < parseInt( data.final_step, 10 ) ) { |
| 308 |
status.text( BBP_Converter.strings.status_up_next.replace( '%s', data.current_step ) ); |
| 309 |
} else { |
| 310 |
status.text( BBP_Converter.strings.status_complete ); |
| 311 |
} |
| 312 |
} |
| 313 |
}, 1000 ); |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Prepend some text to the import monitor |
| 318 |
* |
| 319 |
* @since 2.6.0 bbPress (r6470) |
| 320 |
* |
| 321 |
* @param {string} text Text to prepend to the import monitor |
| 322 |
* |
| 323 |
* @returns {void} |
| 324 |
*/ |
| 325 |
function bbp_converter_log( text ) { |
| 326 |
text = '<p>' + text + '</p>'; |
| 327 |
|
| 328 |
message.prepend( text ); |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Prepend some text to the import monitor |
| 333 |
* |
| 334 |
* @since 2.6.0 bbPress (r6470) |
| 335 |
* |
| 336 |
* @returns {void} |
| 337 |
*/ |
| 338 |
function bbp_converter_percentages( step_percent, total_percent ) { |
| 339 |
step_p.width( step_percent + '%' ); |
| 340 |
total_p.width( total_percent + '%' ); |
| 341 |
} |
| 342 |
} ); |
| 343 |
|