0.12 - Auto-collect + Auto-collapse

This commit is contained in:
abuyoyo 2020-04-10 19:30:48 +03:00
parent ab65b0e8db
commit f08d41bcfd
3 changed files with 180 additions and 109 deletions

View File

@ -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);
});

View File

@ -1,8 +1,8 @@
<?php
/**
* Plugin Name: abuyoyo / Notice Manager
* Plugin Name: Notice Manager
* Description: Manage notices on WordPress admin pages. Adds 'Notices' screen-meta-link.
* Version: 0.11
* Version: 0.12
* Author: abuyoyo
* Author URI: https://github.com/abuyoyo/
* Plugin URI: https://github.com/abuyoyo/notice-manager
@ -10,52 +10,48 @@
if ( ! defined('ABSPATH') ) wp_die( 'No soup for you!' );
use WPHelper\PluginCore;
use WPHelper\AdminMenuPage;
new PluginCore(
__FILE__,
[
'update_checker' => ['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';

View File

@ -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 '<script defer>var noticeManager=' .json_encode(camelCaseKeys($this->options)) . ';</script>';
// echo '<script defer src="'. NOTICE_MANAGER_URL . 'js/notice_manager_panel.js' .'"></script>';
}
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 '<div class="notice_container empty"></div>';
echo '<button type="button" class="notice-dismiss"><span class="screen-reader-text">' . __( 'Dismiss' ) . '</span><strong> Dismiss Notices</strong></button><div></div>' ;
// if auto_collect is ON - we don't need the button.
if (!$this->options['auto_collect'])
echo '<button type="button" class="notice-dismiss"><span class="screen-reader-text">' . __( 'Dismiss' ) . '</span><strong> Dismiss Notices</strong></button><div></div>' ;
}
/**