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