diff --git a/js/notice_manager_panel.js b/js/notice_manager_panel.js index 3f72499..1bb9cf0 100644 --- a/js/notice_manager_panel.js +++ b/js/notice_manager_panel.js @@ -1,31 +1,146 @@ +let notices = jQuery( 'div.updated, div.error, div.notice, div.update-nag' ).not( '.inline, .below-h2' ); + (function($,document){ + + // wait function used with autoCollapse + wait = window.wait || function(ms){ + var dfd = $.Deferred(); + setTimeout(dfd.resolve, ms); //callback, timeout till callback + return dfd.promise(); + }; + + let button = $( '#meta-link-notices' ), panel = $( '#meta-link-notices-wrap' ), - haveClosed = false; + haveClosed = false, - let notices = $( 'div.updated, div.error, div.notice, div.update-nag' ).not( '.inline, .below-h2' ); + // notices = $( 'div.updated, div.error, div.notice, div.update-nag' ).not( '.inline, .below-h2' ), + dismissNoticesButton = $( '#meta-link-notices-wrap button.notice-dismiss' ); + + dismissNoticesButton.on('click',function(){ + screenMeta.close(panel,button); + collectNotices(); + }); + + //original wp focus on click function + button.on( 'focus.scroll-into-view', function(e){ + if ( e.target.scrollIntoView ) + e.target.scrollIntoView(false); + }); + //scroll page to top when closing notice panel + button.on('click',function(){ + haveClosed = true; + if ( $(this).hasClass('screen-meta-active') ){ + // $(window).scrollTop(true); + }else{ + // wait (500).then(function(){ //still jumpy sometimes - but scrolls to correct position 400 ~ 600 + // $(window).scrollTop(true); + // }); + } + + }); + - - // $( '#meta-link-notices-wrap button' ); - let dismissNoticesButton = $( '#meta-link-notices-wrap button.notice-dismiss' ); + $(document).ready(function(){ console.log(noticeManager); + notices = $( 'div.updated, div.error, div.notice, div.update-nag' ).not( '.inline, .below-h2' ); + + + maybeRemoveNoticesPanel(); + + + if (noticeManager.autoCollect){ + collectNotices(); + }else{ + /** + * Move ALL notices above page title. + * Default no-panel action - override WordPress moving notices BELOW title. + */ + notices.insertBefore('.wrap:first'); + } + + /** - * Move ALL notices above page title. ALWAYS! + * auto-open notices panel */ - notices.insertBefore('.wrap:first'); + if (button.length){ + panel.toggle(); + button.addClass( 'screen-meta-active' ); + screenMeta.open(panel,button); + } + + + if (noticeManager.autoCollapse){ + + // auto-close notices panel after short delay + // only auto-close if we have not interacted (opened/closed) with panel previously + wait(10000).then(function(){ + if ( ! haveClosed ){ + screenMeta.close(panel,button); + } + }); + } + + + + + }); + + /** + * Collect notices into panel. + * Remove dismiss-notices button. + */ + function collectNotices(){ + + notices.appendTo('.notice_container').eq(0); + $('.notice_container').removeClass('empty'); + + if (dismissNoticesButton.length) + dismissNoticesButton.detach(); + + $(document).on('DOMNodeRemoved', '.notice.is-dismissible' , function (e) { + console.log('DOMNodeRemoved'); + console.log(e.target); + console.log(e); + + // $(e.target).on("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(e){ + notices = panel.find( 'div.updated, div.error, div.notice, div.update-nag' ).filter(':visible'); + console.log('DomNodeRemoved:notices.length'); + console.log(notices.length); + maybeRemoveNoticesPanel(); + // }); + + // notices = $( 'div.updated, div.error, div.notice, div.update-nag' ).not( '.inline, .below-h2' ); + + // notices = panel.find( 'div.updated, div.error, div.notice, div.update-nag' );//.not( '.inline, .below-h2' ); + + + + }); + + } + + function maybeRemoveNoticesPanel(){ + + + + console.log('notices.length'); + console.log(notices.length); /** * Remove meta-links-notices if no notices on page * Remove screen-meta-links wrapper if no panels on page */ if ( ! notices.length ){ console.log('NO NOTICES'); + screenMeta.close(panel,button); + $('#meta-link-notices-link-wrap').detach(); $('#meta-link-notices-wrap').detach(); @@ -34,73 +149,22 @@ return; } - - - /** - * auto-open notices panel - */ - if (button.length){ - panel.toggle(); - button.addClass( 'screen-meta-active' ); - - screenMeta.open(panel,button); - } - - if (noticeManager.autoCollect){ - collectNotices(); - } - - if (noticeManager.autoCollapse){ - wait = window.wait || function(ms){ - var dfd = $.Deferred(); - setTimeout(dfd.resolve, ms); //callback, timeout till callback - return dfd.promise(); - }; - - // auto-close notices panel after short delay - // only auto-close if we have not interacted (opened/closed) with panel previously - wait(20000).then(function(){ - if ( ! haveClosed ) - screenMeta.close(panel,button); - }); - } - }); - - - - dismissNoticesButton.on('click',function(){ - screenMeta.close(panel,button); - collectNotices(); - }); - - - - - function collectNotices(){ - $( 'div.updated, div.error, div.notice, div.update-nag' ).not( '.inline, .below-h2' ) - .appendTo('.notice_container').eq(0); - $('.notice_container').removeClass('empty'); - - dismissNoticesButton.detach(); } + - //original wp focus on click function - /* button.on( 'focus.scroll-into-view', function(e){ - if ( e.target.scrollIntoView ) - e.target.scrollIntoView(false); - }); */ - //scroll page to top when closing notice panel - button.on('click',function(){ - haveClosed = true; - if ( $(this).hasClass('screen-meta-active') ){ - $(window).scrollTop(true); - }else{ - // wait (500).then(function(){ //still jumpy sometimes - but scrolls to correct position 400 ~ 600 - // $(window).scrollTop(true); - // }); - } - + // prevent jumpy scrollRestoration on reload page + // fixed below on 'beforeunload' + // if (history.scrollRestoration) { + // history.scrollRestoration = 'manual'; + //} + + + /** + * Set history.scrollTop to prevent jump on page refresh when scrollRestoration = auto + */ + $(window).on('beforeunload', function() { + history.pushState({scrollTop:document.body.scrollTop},document.title,document.location.pathname); }); diff --git a/notice-manager.php b/notice-manager.php index c8157ad..4bf00e2 100644 --- a/notice-manager.php +++ b/notice-manager.php @@ -1,8 +1,8 @@ ['auth'=> $github_oauth], - ] -); -new AdminMenuPage([ - 'slug' => 'notice-manager', - 'title' => 'Notice Manager', - 'parent' => 'options', - 'render' => 'settings-page', // built-in settings page - 'settings' => [ - 'option_name' => 'notice_manager', // option_name used in wp_options table - // 'option_group' => 'wp_head_cleaner_settings' . '_settings_group', // Optional - Settings group used in register_setting() and settings_fields() - 'sections' => [ - [ - 'id' => 'notice_manager', - // 'title' => 'N', - 'description' => 'Setup How notice manager functions.', - 'fields' => [ + 'admin_menu_page' => [ + 'parent' => 'options', + 'render' => 'settings-page', // built-in settings page + 'settings' => [ + 'option_name' => 'notice_manager', // option_name used in wp_options table + 'sections' => [ [ - 'id' => 'screen_panel', - 'title' => 'Notices Panel', - 'type' => 'checkbox', - 'description' => 'Enable screen-meta-links \'Notices\' panel.', - ], - [ - 'id' => 'auto_collect', - 'title' => 'Auto-Collect Notices', - 'type' => 'checkbox', - 'description' => 'Automatic collection of notices into panel.', - ], - [ - 'id' => 'auto_collapse', - 'title' => 'Auto-collapse Panel', - 'type' => 'checkbox', - 'description' => 'Notices panel will stay open for a few seconds on page load, and then close automatically.', - ], + 'id' => 'notice_manager', + // 'title' => 'N', + 'description' => 'Setup How notice manager functions.', + 'fields' => [ + [ + 'id' => 'screen_panel', + 'title' => 'Notices Panel', + 'type' => 'checkbox', + 'description' => 'Enable screen-meta-links \'Notices\' panel.', + ], + [ + 'id' => 'auto_collect', + 'title' => 'Auto-Collect Notices', + 'type' => 'checkbox', + 'description' => 'Automatic collection of notices into panel.', + ], + [ + 'id' => 'auto_collapse', + 'title' => 'Auto-collapse Panel', + 'type' => 'checkbox', + 'description' => 'Notices panel will stay open for a few seconds on page load, and then close automatically.', + ], + ] + ] ] ] ] ] -]); +); require_once 'src/NoticeManager.php'; diff --git a/src/NoticeManager.php b/src/NoticeManager.php index b5ae74a..bcbd7fd 100644 --- a/src/NoticeManager.php +++ b/src/NoticeManager.php @@ -31,6 +31,8 @@ class NoticeManager{ }else{ add_action( 'admin_init' , [ $this , 'register_notice_manager_panel' ] ); } + + add_action('admin_print_footer_scripts', [$this,'admin_print_footer_scripts']); // we don't want to update wp-plugin registered with same name @@ -42,6 +44,12 @@ class NoticeManager{ wp_localize_script( 'notice_manager_panel', 'noticeManager', camelCaseKeys($this->options) ); wp_enqueue_style( 'admin_notices', NOTICE_MANAGER_URL . 'css/admin_notices.css' ); } + + function admin_print_footer_scripts(){ + + // echo ''; + // echo ''; + } function register_notice_manager_panel(){ if ( ! function_exists( 'add_screen_meta_link' ) ) @@ -66,7 +74,10 @@ class NoticeManager{ // button is a copy of is-dismissible button - for styling purposes only // js functionality and listener - js/notice_manager_meta_panel.js echo '
'; - echo '' ; + + // if auto_collect is ON - we don't need the button. + if (!$this->options['auto_collect']) + echo '' ; } /**