| 1 |
/** |
| 2 |
* Modal window functionality. |
| 3 |
* |
| 4 |
* @package WPZincDashboardWidget |
| 5 |
* @author WP Zinc |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Shows the overlay and modal. |
| 10 |
* |
| 11 |
* @since 1.0.0 |
| 12 |
* |
| 13 |
* @param string title Modal Title |
| 14 |
* @param string message Modal Message |
| 15 |
*/ |
| 16 |
function wpzinc_modal_open( title, message ) { |
| 17 |
|
| 18 |
// Show overlay and progress UI. |
| 19 |
jQuery( '.js-notices' ).text( '' ); |
| 20 |
jQuery( '.wpzinc-modal h2.title div.spinner' ).css( 'display', 'inline-block' ).css( 'visibility', 'visible' ); |
| 21 |
jQuery( '.wpzinc-modal h2.title div.tick' ).css( 'display', 'none' ).css( 'visibility', 'hidden' ); |
| 22 |
wpzinc_modal_update_title( title ); |
| 23 |
wpzinc_modal_update_message( message ); |
| 24 |
jQuery( '.wpzinc-modal div.notices' ).text( '' ); |
| 25 |
jQuery( '.wpzinc-modal button.close' ).hide(); |
| 26 |
|
| 27 |
// If there's no message, show the mini version of the modal. |
| 28 |
if ( typeof message === 'undefined' || ! message.length ) { |
| 29 |
jQuery( '.wpzinc-modal' ).addClass( 'wpzinc-modal-mini' ); |
| 30 |
} |
| 31 |
|
| 32 |
// Show. |
| 33 |
jQuery( '.wpzinc-modal-overlay, .wpzinc-modal' ).show(); |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Hides the overlay and modal. |
| 39 |
* |
| 40 |
* @since 1.8.3 |
| 41 |
*/ |
| 42 |
function wpzinc_modal_close() { |
| 43 |
|
| 44 |
jQuery( '.wpzinc-modal-overlay, .wpzinc-modal' ).fadeOut( |
| 45 |
'fast', |
| 46 |
function () { |
| 47 |
jQuery( '.wpzinc-modal h2.title div.spinner' ).css( 'display', 'none' ).css( 'visibility', 'hidden' ); |
| 48 |
jQuery( '.wpzinc-modal h2.title div.tick' ).css( 'display', 'none' ).css( 'visibility', 'hidden' ); |
| 49 |
wpzinc_modal_update_title( '' ); |
| 50 |
wpzinc_modal_update_message( '' ); |
| 51 |
jQuery( '.wpzinc-modal div.notices' ).text( '' ); |
| 52 |
jQuery( '.wpzinc-modal button.close' ).hide(); |
| 53 |
jQuery( '.wpzinc-modal' ).removeClass( 'wpzinc-modal-mini' ); |
| 54 |
jQuery( '.wpzinc-modal-overlay, .wpzinc-modal' ).hide(); |
| 55 |
} |
| 56 |
); |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Update the modal dialog's title |
| 62 |
* |
| 63 |
* @since 1.0.0 |
| 64 |
* |
| 65 |
* @param string message Modal Title. |
| 66 |
*/ |
| 67 |
function wpzinc_modal_update_title( title ) { |
| 68 |
|
| 69 |
jQuery( '.wpzinc-modal h2.title span.text' ).text( title ); |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Update the modal dialog's message |
| 75 |
* |
| 76 |
* @since 1.0.0 |
| 77 |
* |
| 78 |
* @param string message Modal Message. |
| 79 |
*/ |
| 80 |
function wpzinc_modal_update_message( message ) { |
| 81 |
|
| 82 |
jQuery( '.wpzinc-modal' ).removeClass( 'wpzinc-modal-mini' ); |
| 83 |
jQuery( '.wpzinc-modal p.message' ).text( message ); |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Displays a success message in the modal. |
| 89 |
* |
| 90 |
* @since 1.8.3 |
| 91 |
* |
| 92 |
* @param string message Message. |
| 93 |
*/ |
| 94 |
function wpzinc_modal_show_success_message( message ) { |
| 95 |
|
| 96 |
jQuery( '.wpzinc-modal' ).removeClass( 'wpzinc-modal-mini' ); |
| 97 |
jQuery( '.wpzinc-modal h2.title div.spinner' ).css( 'display', 'none' ).css( 'visibility', 'hidden' ); |
| 98 |
jQuery( '.wpzinc-modal h2.title div.tick' ).css( 'display', 'inline-block' ).css( 'visibility', 'visible' ); |
| 99 |
jQuery( '.wpzinc-modal p.message' ).text( '' ); |
| 100 |
jQuery( '.wpzinc-modal div.notices' ).html( '<div class="updated notice"><p>' + message + '</p></div>' ); |
| 101 |
jQuery( '.wpzinc-modal button.close' ).show(); |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Displays an error message in the modal. |
| 107 |
* |
| 108 |
* @since 1.8.3 |
| 109 |
* |
| 110 |
* @param string message Message. |
| 111 |
*/ |
| 112 |
function wpzinc_modal_show_error_message( message ) { |
| 113 |
|
| 114 |
jQuery( '.wpzinc-modal' ).removeClass( 'wpzinc-modal-mini' ); |
| 115 |
jQuery( '.wpzinc-modal h2.title div.spinner' ).css( 'display', 'none' ).css( 'visibility', 'hidden' ); |
| 116 |
jQuery( '.wpzinc-modal h2.title div.tick' ).css( 'display', 'none' ).css( 'visibility', 'hidden' ); |
| 117 |
jQuery( '.wpzinc-modal p.message' ).text( '' ); |
| 118 |
jQuery( '.wpzinc-modal div.notices' ).html( '<div class="error notice"><p>' + message + '</p></div>' ); |
| 119 |
jQuery( '.wpzinc-modal button.close' ).show(); |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Displays a success message in the main screen, hides the overlay and progress modal |
| 125 |
* and returns false, to prevent the calling script from making further AJAX calls. |
| 126 |
* |
| 127 |
* @since 1.0.0 |
| 128 |
* |
| 129 |
* @param string message Message. |
| 130 |
*/ |
| 131 |
function wpzinc_modal_show_success_message_and_exit( message ) { |
| 132 |
|
| 133 |
jQuery( '.wpzinc-modal' ).removeClass( 'wpzinc-modal-mini' ); |
| 134 |
|
| 135 |
// If no notice container exists, create one now. |
| 136 |
if ( ! jQuery( '.js-notices' ).length ) { |
| 137 |
if ( jQuery( 'hr.wp-header-end' ).length > 0 ) { |
| 138 |
jQuery( 'hr.wp-header-end' ).after( '<div class="js-notices"></div>' ); |
| 139 |
} else if ( jQuery( '.wrap > h1:first' ).length > 0 ) { |
| 140 |
jQuery( '.wrap > h1:first' ).after( '<div class="js-notices"></div>' ); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
// Define notice. |
| 145 |
jQuery( '.js-notices' ).html( '<div class="updated notice is-dismissible wpzinc-is-dismissible"><p>' + message + '</p><button type="button" class="notice-dismiss"></button></div>' ); |
| 146 |
|
| 147 |
// Scroll to notice. |
| 148 |
jQuery( 'html,body' ).animate( |
| 149 |
{ |
| 150 |
// wp admin bar = ~ 44px height. |
| 151 |
scrollTop: ( jQuery( '.js-notices' ).offset().top - 44 ) |
| 152 |
} |
| 153 |
); |
| 154 |
|
| 155 |
// Close modal. |
| 156 |
wpzinc_modal_close(); |
| 157 |
|
| 158 |
return false; |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Displays an error message in the main screen, hides the overlay and progress modal |
| 164 |
* and returns false, to prevent the calling script from making further AJAX calls. |
| 165 |
* |
| 166 |
* @since 1.8.3 |
| 167 |
* |
| 168 |
* @param string message Message. |
| 169 |
*/ |
| 170 |
function wpzinc_modal_show_error_message_and_exit( message ) { |
| 171 |
|
| 172 |
jQuery( '.wpzinc-modal' ).removeClass( 'wpzinc-modal-mini' ); |
| 173 |
|
| 174 |
// Define notice. |
| 175 |
jQuery( '.js-notices' ).html( '<div class="notice notice-error is-dismissible wpzinc-is-dismissible"><p>' + message + '</p></div>' ); |
| 176 |
|
| 177 |
// Scroll to notice. |
| 178 |
jQuery( 'html,body' ).animate( |
| 179 |
{ |
| 180 |
// wp admin bar = ~ 44px height. |
| 181 |
scrollTop: ( jQuery( '.js-notices' ).offset().top - 44 ) |
| 182 |
} |
| 183 |
); |
| 184 |
|
| 185 |
// Close modal. |
| 186 |
wpzinc_modal_close(); |
| 187 |
|
| 188 |
return false; |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Displays a tick on the modal for a second, and then hides the overlay and progress modal |
| 194 |
* |
| 195 |
* @since 1.0.0 |
| 196 |
* |
| 197 |
* @param string message Message. |
| 198 |
*/ |
| 199 |
function wpzinc_modal_show_success_and_exit( title, message ) { |
| 200 |
|
| 201 |
// Update message. |
| 202 |
wpzinc_modal_update_title( title ); |
| 203 |
|
| 204 |
// If there's no message, show the mini version of the modal. |
| 205 |
if ( typeof message === 'undefined' || ! message.length ) { |
| 206 |
jQuery( '.wpzinc-modal' ).addClass( 'wpzinc-modal-mini' ); |
| 207 |
} else { |
| 208 |
jQuery( '.wpzinc-modal' ).removeClass( 'wpzinc-modal-mini' ); |
| 209 |
wpzinc_modal_update_message( message ); |
| 210 |
} |
| 211 |
|
| 212 |
// Hide spinner, show tick. |
| 213 |
jQuery( '.wpzinc-modal h2.title div.spinner' ).css( 'display', 'none' ).css( 'visibility', 'hidden' ); |
| 214 |
jQuery( '.wpzinc-modal h2.title div.tick' ).css( 'display', 'inline-block' ).css( 'visibility', 'visible' ); |
| 215 |
|
| 216 |
setTimeout( |
| 217 |
function () { |
| 218 |
|
| 219 |
// Close modal. |
| 220 |
wpzinc_modal_close(); |
| 221 |
|
| 222 |
}, |
| 223 |
750 |
| 224 |
); |
| 225 |
|
| 226 |
return false; |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
jQuery( document ).ready( |
| 231 |
function ( $ ) { |
| 232 |
|
| 233 |
// Close the modal when its close button is clicked. |
| 234 |
$( 'body' ).on( |
| 235 |
'click', |
| 236 |
'.wpzinc-modal button.close', |
| 237 |
function ( e ) { |
| 238 |
|
| 239 |
wpzinc_modal_close(); |
| 240 |
|
| 241 |
} |
| 242 |
); |
| 243 |
|
| 244 |
// Allow notices added by this file using JS to be dismissed, as WordPress won't recognise |
| 245 |
// the button.notice-dismiss being clicked for notices added after the page load. |
| 246 |
$( 'body' ).on( |
| 247 |
'click', |
| 248 |
'.wpzinc-is-dismissible button.notice-dismiss', |
| 249 |
function ( e ) { |
| 250 |
|
| 251 |
e.preventDefault(); |
| 252 |
|
| 253 |
$( this ).closest( '.js-notices' ).remove(); |
| 254 |
|
| 255 |
} |
| 256 |
); |
| 257 |
|
| 258 |
} |
| 259 |
); |
| 260 |
|