/** * @package: WordPress * @subpackage: Admin Bar Button Plugin * @description: JS for use in the admin area (on the Admin Bar Button settings page) */ $ = jQuery.noConflict(); (function($){ $(document).ready(function(){ abbPage.init(); }); var abbPage = { /** * Pseudo constructor */ init : function(){ this.tabs = $('.nav-tab', '#admin-bar-button-page'); this.tabContents = $('.nav-tab-content', '#admin-bar-button-page'); this._invokeColourPicker(); // Invoke the WordPress colour picker widget on the specified selectors this._tabActivate(); // Activate the tab stored in Session Storage (or the first tab if none) this._createEvents(); // Create object events }, // init /** * Invoke the WordPress colour picker widget on the specified selectors */ _invokeColourPicker : function(){ $(function(){ $('.colour-picker', '#admin-bar-button-page').wpColorPicker(); }); }, // _invokeColourPicker /** * Return the currently active tab from session storage * * @return string|boolean Returns 'false' if not tab is set */ _tabGet : function(){ var tabID = false; if(typeof(Storage) !== "undefined"){ tabID = sessionStorage.ABBTab; } return (typeof(tabID) === 'string') ? tabID : false; }, // _tabGet /** * Set the currently active tab in session storage * * @param required string tab The tab to set as active */ _tabSet : function(tab){ if(typeof(tab) !== 'string' || $('#'+tab).length == 0) sessionStorage.removeItem('ABBTab'); if(typeof(Storage) !== "undefined"){ sessionStorage.ABBTab = tab; } }, // _tabSet /** * Activate the given tab, or the tab stored in session storage if one is not passed * Activates the first tab if no valid tab is specified * * @param mixed tab The tab to activate */ _tabActivate : function(tab){ var tabID; // The ID of the tab being activated /** * Set the tab object that is to be set as active, and the ID of that tab */ if(typeof(tab) === 'object'){ // A tab object has been passed... tabID = tab.attr('id'); // ...Grab the ID of the tab that has been passed } else if(typeof(tab) === 'string'){ // A string value has been passed... tabID = tab; // ...Set the tab ID to that of the string value that has been passed tab = $('#'+tabID); // ...Grab the tab object from the tab ID } else { // Nothing (or an invalid value) has been passed... tabID = this._tabGet(); // ...Grab the ID tab currently stored in session storage tab = $('#'+tabID); // ...Grab the tab object from the tab ID } if(tab.length === 0){ // Ensure that a valid object has been set... tab = $(this.tabs[0]); // ...It hasn't - grab the first tab object tabID = tab.attr('id'); // ...It hasn't - Grab the ID of the first tab object } this._tabSet(tabID); // Set the current tab this.tabs.removeClass('nav-tab-active'); // Remove the 'nav-tab-active' class from all tab selectors tab.addClass('nav-tab-active'); // Add the 'nav-tab-active' class to the now active tab this.tabContents.removeClass('nav-tab-content-active'); // Remove the 'nav-tab-content-active' class from all tab content selectors $('#content-' + tabID).addClass('nav-tab-content-active'); // Add the 'nav-tab-content-active' class to the now active tab content }, // _tabActivate /** * Dismiss any admin notices that are currently visible */ _dismissNotices : function(){ $('.notice-dismiss', '#admin-bar-button-page').trigger('click'); }, // _dismissNotices /** * Create object events */ _createEvents : function(){ var t = this; // This object /** * Handle clicks to the navigation tabs */ $('.nav-tab', '#admin-bar-button-page').on('click', function(){ t._dismissNotices() // Dismiss any admin notices that are currently visible t._tabActivate($(this)); // Activate the tab the has been clicked }); /** * Handle clicks to the 'Restore Defaults' button */ $('input[name="delete"]', '#admin-bar-button-page').on('click', function(){ /** Ensure that the user really does want to reset the settings */ var result = confirm('Are you sure you want to restore the default settings?'); if(result !== true){ return false; } }); /** * Handle changrs to the 'Admin bar show time' settings */ $('#bar_auto_hide').on('change', function(){ var showTime = $('#bar_show_time'); // Grab the 'Admin bar show time' control if($(this).is(':checked')){ // The user has checked the 'Auto-hide the admin bar' control showTime.removeProp('readonly'); // ...Disable the 'Admin bar show time' control } else { // The user has unchecked the 'Auto-hide the admin bar' control showTime.prop('readonly', true); // ...Disable the 'Admin bar show time' control } }); /** * Handle changrs to the 'Animate actions' settings */ $('#animate').on('change', function(){ var duration = $('#animate_duration'); // Grab the 'Animation duration' control var direction = $('#animate_direction'); // Grab the 'Animation direction' control if($(this).is(':checked')){ // The user has checked the 'Animate actions' setting... duration.removeProp('readonly'); // ...Enable the 'Animation duration' control direction.removeProp('disabled'); // ...Enable the 'Animation direction' control } else { // The user has unchecked the 'Animate actions' setting... duration.prop('readonly', true); // ...Disable the 'Animation duration' control direction.prop('disabled', true); // ...Disable the 'Animation direction' control } }); /** * Handle window unload events */ window.onbeforeunload = function(e){ var target = $(document.activeElement), // Grab the element that was last clicked validClick = (target.parents('#admin-bar-button-page').length == 0); // If a click occured, check whether or not it occured outside of the Admin Bar Button settings if(validClick) t._tabSet(false); // Unset the active tab }; } // createEvents } })(jQuery);