| 1 |
( function( $, _ ){ |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
const fp = {}; |
| 5 |
|
| 6 |
fp.selectors = { |
| 7 |
moduleGenerator: '.fp-module-generator', |
| 8 |
}; |
| 9 |
|
| 10 |
fp.log = function ( $element, html, data, attrClass ){ |
| 11 |
if ( 'undefined' === typeof attrClass ){ |
| 12 |
attrClass = 'notice is-dismissible'; |
| 13 |
} else { |
| 14 |
attrClass = 'notice is-dismissible ' + attrClass; |
| 15 |
} |
| 16 |
|
| 17 |
var noticeContent = _.template( html )( data ), |
| 18 |
templateVars = { |
| 19 |
'attrClass': attrClass, |
| 20 |
'html': noticeContent |
| 21 |
}, |
| 22 |
noticeHtml = _.template( '<div class="<%= attrClass %>"><p><%= html %></p><button type="button" class="notice-dismiss"></button></div>' )( templateVars ), |
| 23 |
$notice = $( noticeHtml ); |
| 24 |
|
| 25 |
$notice.on( 'click.wp-dismiss-notice', '.notice-dismiss', function( event ) { |
| 26 |
event.preventDefault(); |
| 27 |
$notice.fadeTo( 100 , 0, function() { |
| 28 |
$(this).slideUp( 100, function() { |
| 29 |
$(this).remove(); |
| 30 |
}); |
| 31 |
}); |
| 32 |
}); |
| 33 |
|
| 34 |
return $element.append( $notice ); |
| 35 |
}; |
| 36 |
|
| 37 |
fp.moduleGenerate = ( $form, _POST ) => { |
| 38 |
if ( 'undefined' === typeof _POST ){ |
| 39 |
_POST = window.fakerpress.qs.parse( $form.serialize() ); |
| 40 |
} |
| 41 |
|
| 42 |
// Always Hard set the Action |
| 43 |
_POST.action = 'fakerpress.module_generate'; |
| 44 |
|
| 45 |
var $submit_container = $form.find( '.fp-submit' ), |
| 46 |
$spinner = $submit_container.find( '.spinner' ), |
| 47 |
$button = $submit_container.find( '.button-primary' ), |
| 48 |
$response = $submit_container.find( '.fp-response' ); |
| 49 |
|
| 50 |
if ( $spinner.hasClass( 'is-active' ) ){ |
| 51 |
return; |
| 52 |
} |
| 53 |
$spinner.addClass( 'is-active' ); |
| 54 |
$button.prop( 'disabled', true ); |
| 55 |
|
| 56 |
$.ajax({ |
| 57 |
dataType: 'json', |
| 58 |
type: 'POST', |
| 59 |
url: window.ajaxurl, |
| 60 |
data: _POST, |
| 61 |
complete: function( jqXHR, status ){ |
| 62 |
if ( 'success' !== status ){ |
| 63 |
$spinner.removeClass( 'is-active' ); |
| 64 |
$button.prop( 'disabled', false ); |
| 65 |
|
| 66 |
fp.log( $response, '<%= message %>', { message: jqXHR.responseText }, 'notice-error' ); |
| 67 |
} |
| 68 |
}, |
| 69 |
success: function( data, textStatus, jqXHR ) { |
| 70 |
$spinner.removeClass( 'is-active' ); |
| 71 |
if ( null === data ) { |
| 72 |
$button.prop( 'disabled', false ); |
| 73 |
fp.log( $response, '<%= message %>', { message: jqXHR.responseText }, 'notice-error' ); |
| 74 |
} else if ( data.status ){ |
| 75 |
if ( data.is_capped && data.offset < data.total ){ |
| 76 |
_POST.offset = data.offset; |
| 77 |
_POST.total = data.total; |
| 78 |
|
| 79 |
fp.moduleGenerate( $form, _POST ); |
| 80 |
} else { |
| 81 |
$button.prop( 'disabled', false ); |
| 82 |
} |
| 83 |
|
| 84 |
fp.log( $response, 'Faked <%= total %>: <%= message %>', { message: data.message, total: data.results.length }, 'notice-success' ); |
| 85 |
} else { |
| 86 |
$button.prop( 'disabled', false ); |
| 87 |
fp.log( $response, '<%= message %>', data, 'notice-error' ); |
| 88 |
} |
| 89 |
} |
| 90 |
}); |
| 91 |
}; |
| 92 |
|
| 93 |
// Document Ready Actions |
| 94 |
$( () => { |
| 95 |
const $forms = $( fp.selectors.moduleGenerator ).each( function() { |
| 96 |
const $form = $( this ); |
| 97 |
|
| 98 |
$form.on( 'submit', function ( event ) { |
| 99 |
fp.moduleGenerate( $form ); |
| 100 |
|
| 101 |
console.log( 'Form', $form ); |
| 102 |
event.preventDefault(); |
| 103 |
return; |
| 104 |
} ); |
| 105 |
} ); |
| 106 |
} ); |
| 107 |
}( jQuery, _ ) ); |
| 108 |
|