0.12 - Auto-collect + Auto-collapse
This commit is contained in:
parent
ab65b0e8db
commit
f08d41bcfd
@ -1,31 +1,146 @@
|
|||||||
|
let notices = jQuery( 'div.updated, div.error, div.notice, div.update-nag' ).not( '.inline, .below-h2' );
|
||||||
|
|
||||||
(function($,document){
|
(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' ),
|
let button = $( '#meta-link-notices' ),
|
||||||
panel = $( '#meta-link-notices-wrap' ),
|
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(){
|
$(document).ready(function(){
|
||||||
|
|
||||||
console.log(noticeManager);
|
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 meta-links-notices if no notices on page
|
||||||
* Remove screen-meta-links wrapper if no panels on page
|
* Remove screen-meta-links wrapper if no panels on page
|
||||||
*/
|
*/
|
||||||
if ( ! notices.length ){
|
if ( ! notices.length ){
|
||||||
console.log('NO NOTICES');
|
console.log('NO NOTICES');
|
||||||
|
screenMeta.close(panel,button);
|
||||||
|
|
||||||
$('#meta-link-notices-link-wrap').detach();
|
$('#meta-link-notices-link-wrap').detach();
|
||||||
$('#meta-link-notices-wrap').detach();
|
$('#meta-link-notices-wrap').detach();
|
||||||
|
|
||||||
@ -34,73 +149,22 @@
|
|||||||
|
|
||||||
return;
|
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
|
// prevent jumpy scrollRestoration on reload page
|
||||||
button.on('click',function(){
|
// fixed below on 'beforeunload'
|
||||||
haveClosed = true;
|
// if (history.scrollRestoration) {
|
||||||
if ( $(this).hasClass('screen-meta-active') ){
|
// history.scrollRestoration = 'manual';
|
||||||
$(window).scrollTop(true);
|
//}
|
||||||
}else{
|
|
||||||
// wait (500).then(function(){ //still jumpy sometimes - but scrolls to correct position 400 ~ 600
|
|
||||||
// $(window).scrollTop(true);
|
/**
|
||||||
// });
|
* 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);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Plugin Name: abuyoyo / Notice Manager
|
* Plugin Name: Notice Manager
|
||||||
* Description: Manage notices on WordPress admin pages. Adds 'Notices' screen-meta-link.
|
* Description: Manage notices on WordPress admin pages. Adds 'Notices' screen-meta-link.
|
||||||
* Version: 0.11
|
* Version: 0.12
|
||||||
* Author: abuyoyo
|
* Author: abuyoyo
|
||||||
* Author URI: https://github.com/abuyoyo/
|
* Author URI: https://github.com/abuyoyo/
|
||||||
* Plugin URI: https://github.com/abuyoyo/notice-manager
|
* Plugin URI: https://github.com/abuyoyo/notice-manager
|
||||||
@ -10,52 +10,48 @@
|
|||||||
if ( ! defined('ABSPATH') ) wp_die( 'No soup for you!' );
|
if ( ! defined('ABSPATH') ) wp_die( 'No soup for you!' );
|
||||||
|
|
||||||
use WPHelper\PluginCore;
|
use WPHelper\PluginCore;
|
||||||
use WPHelper\AdminMenuPage;
|
|
||||||
|
|
||||||
new PluginCore(
|
new PluginCore(
|
||||||
__FILE__,
|
__FILE__,
|
||||||
[
|
[
|
||||||
'update_checker' => ['auth'=> $github_oauth],
|
'update_checker' => ['auth'=> $github_oauth],
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
new AdminMenuPage([
|
'admin_menu_page' => [
|
||||||
'slug' => 'notice-manager',
|
'parent' => 'options',
|
||||||
'title' => 'Notice Manager',
|
'render' => 'settings-page', // built-in settings page
|
||||||
'parent' => 'options',
|
'settings' => [
|
||||||
'render' => 'settings-page', // built-in settings page
|
'option_name' => 'notice_manager', // option_name used in wp_options table
|
||||||
'settings' => [
|
'sections' => [
|
||||||
'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' => [
|
|
||||||
[
|
[
|
||||||
'id' => 'screen_panel',
|
'id' => 'notice_manager',
|
||||||
'title' => 'Notices Panel',
|
// 'title' => 'N',
|
||||||
'type' => 'checkbox',
|
'description' => 'Setup How notice manager functions.',
|
||||||
'description' => 'Enable screen-meta-links \'Notices\' panel.',
|
'fields' => [
|
||||||
],
|
[
|
||||||
[
|
'id' => 'screen_panel',
|
||||||
'id' => 'auto_collect',
|
'title' => 'Notices Panel',
|
||||||
'title' => 'Auto-Collect Notices',
|
'type' => 'checkbox',
|
||||||
'type' => 'checkbox',
|
'description' => 'Enable screen-meta-links \'Notices\' panel.',
|
||||||
'description' => 'Automatic collection of notices into panel.',
|
],
|
||||||
],
|
[
|
||||||
[
|
'id' => 'auto_collect',
|
||||||
'id' => 'auto_collapse',
|
'title' => 'Auto-Collect Notices',
|
||||||
'title' => 'Auto-collapse Panel',
|
'type' => 'checkbox',
|
||||||
'type' => 'checkbox',
|
'description' => 'Automatic collection of notices into panel.',
|
||||||
'description' => 'Notices panel will stay open for a few seconds on page load, and then close automatically.',
|
],
|
||||||
],
|
[
|
||||||
|
'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';
|
require_once 'src/NoticeManager.php';
|
||||||
|
|
||||||
|
|||||||
@ -31,6 +31,8 @@ class NoticeManager{
|
|||||||
}else{
|
}else{
|
||||||
add_action( 'admin_init' , [ $this , 'register_notice_manager_panel' ] );
|
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
|
// 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_localize_script( 'notice_manager_panel', 'noticeManager', camelCaseKeys($this->options) );
|
||||||
wp_enqueue_style( 'admin_notices', NOTICE_MANAGER_URL . 'css/admin_notices.css' );
|
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(){
|
function register_notice_manager_panel(){
|
||||||
if ( ! function_exists( 'add_screen_meta_link' ) )
|
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
|
// button is a copy of is-dismissible button - for styling purposes only
|
||||||
// js functionality and listener - js/notice_manager_meta_panel.js
|
// js functionality and listener - js/notice_manager_meta_panel.js
|
||||||
echo '<div class="notice_container empty"></div>';
|
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>' ;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user