| @@ -1,10 +1,93 @@ | ||
| 1 | 1 | "use strict"; |
| 2 | 2 | // ===================================================================================================================== |
| 3 | 3 | // == Full Screen - support functions == |
| 4 | 4 | // ===================================================================================================================== |
| 5 | 5 | |
| 6 | +/** | |
| 7 | + * Return every cookie path that can apply to the current WordPress admin URL. | |
| 8 | + * | |
| 9 | + * WordPress may run from a subdirectory. Updating the root, site, and admin | |
| 10 | + * paths prevents an older, more-specific cookie from overriding the new mode. | |
| 11 | + * | |
| 12 | + * @return {string[]} Unique absolute cookie paths. | |
| 13 | + */ | |
| 14 | +function wpbc_admin_ui__full_screen__get_cookie_paths() { | |
| 15 | + var cookie_paths = [ '/' ]; | |
| 16 | + var admin_marker = '/wp-admin/'; | |
| 17 | + var current_path = window.location && window.location.pathname ? window.location.pathname : ''; | |
| 18 | + var admin_index = current_path.indexOf( admin_marker ); | |
| 19 | + | |
| 20 | + if ( admin_index >= 0 ) { | |
| 21 | + cookie_paths.push( current_path.substring( 0, admin_index + 1 ) ); | |
| 22 | + cookie_paths.push( current_path.substring( 0, admin_index + admin_marker.length ) ); | |
| 23 | + } | |
| 24 | + | |
| 25 | + return cookie_paths.filter( function ( path, index ) { | |
| 26 | + return path && cookie_paths.indexOf( path ) === index; | |
| 27 | + } ); | |
| 28 | +} | |
| 29 | + | |
| 30 | +/** | |
| 31 | + * Save Full Screen preference in a short-lived browser cookie. | |
| 32 | + * | |
| 33 | + * This makes the next admin page load deterministic even if the asynchronous | |
| 34 | + * user-meta request is interrupted. The timestamp lets PHP distinguish this | |
| 35 | + * pending value from a stale legacy cookie. | |
| 36 | + * | |
| 37 | + * @param {string} value Fullscreen mode, either `On` or `Off`. | |
| 38 | + * | |
| 39 | + * @return {void} | |
| 40 | + */ | |
| 41 | +function wpbc_admin_ui__full_screen__set_cookie( value ) { | |
| 42 | + var max_age = 5 * 60; | |
| 43 | + var issued_at = Math.floor( Date.now() / 1000 ); | |
| 44 | + var cookie_value = encodeURIComponent( value + '|' + issued_at ); | |
| 45 | + | |
| 46 | + wpbc_admin_ui__full_screen__get_cookie_paths().forEach( function ( cookie_path ) { | |
| 47 | + document.cookie = 'wpbc_admin_full_screen=' + cookie_value + '; path=' + cookie_path + '; max-age=' + max_age + '; SameSite=Lax'; | |
| 48 | + } ); | |
| 49 | +} | |
| 50 | + | |
| 6 | 51 | /** |
| 52 | + * Apply Full Screen mode from a user click. | |
| 53 | + * | |
| 54 | + * @param HTMLElement el Clicked control. | |
| 55 | + * @param bool is_save_user_state Whether to save user preference. | |
| 56 | + */ | |
| 57 | +function wpbc_admin_ui__full_screen__do_on( el, is_save_user_state ) { | |
| 58 | + jQuery( 'body' ).addClass( 'wpbc_admin_full_screen' ); | |
| 59 | + wpbc_check_full_screen_mode(); | |
| 60 | + | |
| 61 | + if ( is_save_user_state ) { | |
| 62 | + wpbc_admin_ui__full_screen__set_cookie( 'On' ); | |
| 63 | + | |
| 64 | + if ( 'function' === typeof wpbc_save_custom_user_data_from_element ) { | |
| 65 | + wpbc_save_custom_user_data_from_element( el ); | |
| 66 | + } | |
| 67 | + } | |
| 68 | +} | |
| 69 | + | |
| 70 | +/** | |
| 71 | + * Exit Full Screen mode from a user click. | |
| 72 | + * | |
| 73 | + * @param HTMLElement el Clicked control. | |
| 74 | + * @param bool is_save_user_state Whether to save user preference. | |
| 75 | + */ | |
| 76 | +function wpbc_admin_ui__full_screen__do_off( el, is_save_user_state ) { | |
| 77 | + jQuery( 'body' ).removeClass( 'wpbc_admin_full_screen' ); | |
| 78 | + wpbc_check_full_screen_mode(); | |
| 79 | + | |
| 80 | + if ( is_save_user_state ) { | |
| 81 | + wpbc_admin_ui__full_screen__set_cookie( 'Off' ); | |
| 82 | + | |
| 83 | + if ( 'function' === typeof wpbc_save_custom_user_data_from_element ) { | |
| 84 | + wpbc_save_custom_user_data_from_element( el ); | |
| 85 | + } | |
| 86 | + } | |
| 87 | +} | |
| 88 | + | |
| 89 | +/** | |
| 7 | 90 | * Check Full screen mode, by removing top tab |
| 8 | 91 | */ |
| 9 | 92 | function wpbc_check_full_screen_mode(){ |
| 10 | 93 | if ( jQuery( 'body' ).hasClass( 'wpbc_admin_full_screen' ) ) { |
| @@ -26,5 +109,5 @@ | ||
| 26 | 109 | } |
| 27 | 110 | |
| 28 | 111 | jQuery( document ).ready( function () { |
| 29 | 112 | wpbc_check_full_screen_mode(); |
| 30 | -} ); | |
| 113 | +} ); | |