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