Make WordPress Core

Ticket #21170: 21170.patch

File 21170.patch, 6.2 KB (added by azaozz, 13 years ago)
  • wp-admin/js/utils.dev.js

     
    11// utility functions
     2var wp = wp || {};
     3wp._private = wp._private || {};
    24
    35var wpCookies = {
    46// The following functions are from Cookie.js class in TinyMCE, Moxiecode, used under LGPL.
     
    169171
    170172        return wpCookies.getHash('wp-settings-' + userSettings.uid) || {};
    171173}
     174
     175/**
     176 * Based on:
     177 * Amplify Core 1.1.0
     178 *
     179 * Copyright 2011 appendTo LLC. (http://appendto.com/team)
     180 * Dual licensed under the MIT or GPL licenses.
     181 * http://appendto.com/open-source-licenses
     182 *
     183 * http://amplifyjs.com
     184 */
     185wp._private.WP_Actions = function() {
     186        var slice = [].slice, subscriptions = {};
     187       
     188        /**
     189         * Triggers an action (custom event) previously added with addAction()
     190         * 
     191         * Usage: doAction( action, ... )
     192         * Any additional parameters will be passed to the callback(s).
     193         *
     194         * @action string Name of the action.
     195         */ 
     196        this.doAction = function( action ) {
     197                var args = slice.call( arguments, 1 ), eventSubscriptions, subscription, length, i = 0;
     198
     199                if ( !subscriptions[ action ] )
     200                        return false;
     201
     202                eventSubscriptions = subscriptions[ action ].slice();
     203               
     204                for ( length = eventSubscriptions.length; i < length; i++ ) {
     205                        subscription = eventSubscriptions[ i ];
     206                        subscription.callback.apply( subscription.context, args );
     207                }
     208               
     209                subscriptions[ action ].done++;
     210
     211                return true;
     212        }
     213
     214        /**
     215         * Adds an action (custom event) that is triggered later
     216         *
     217         * Usage:
     218         * addAction( 'action.namespace', callback );
     219         * addAction( 'action.namespace', callback, priority );
     220         * addAction( 'action.namespace', callback, priority, unique );
     221         * addAction( 'action.namespace', callback, priority, unique, scope );
     222         * addAction( 'action.namespace otheraction.namespace', callback );
     223         *
     224         * @action_names string Action name + namespace. The namespace is required. It is similar to jQuery('selector').bind('click.myname', ...)
     225         *                      and has to be unique for this action and for the script adding the action
     226         *                      similarly to how PHP functions are unique (prefixed) in each WordPress plugin.
     227         *                      Supports adding the same callback to multiple actions (separated by spaces).
     228         * @callback function Callback function for the action. Can be anonymous: addAction( action.namespace, function(){...} );
     229         * @priority int optional Action priority. Lower values have higher priority. Default is 10 (same as add_action() in PHP).
     230         * @unique bool optional Whether to reject attempts to add the same action.namespace multiple times. Defaults to true.
     231         * @scope object optional The JS scope when the callback is invoked.
     232         */
     233        this.addAction = function( action_names, callback, priority, unique, context ) {
     234                var eventIndex, events = action_names.split(' '), eventLength = events.length,
     235                        added, newaction, registered, i, subscriptionInfo, namespace;
     236               
     237                priority = priority || 10;
     238
     239                if ( typeof(unique) == 'undefined' )
     240                        unique = true;
     241
     242                for ( eventIndex = 0; eventIndex < eventLength; eventIndex++ ) {
     243                        newaction = events[ eventIndex ];
     244                        namespace = newaction.split('.');
     245                        added = false;
     246
     247                        if ( !namespace[1] )
     248                                return false;
     249
     250                        newaction = namespace[0];
     251                        namespace = namespace[1];
     252
     253                        if ( !subscriptions[ newaction ] ) {
     254                                subscriptions[ newaction ] = [];
     255                                subscriptions[ newaction ]['done'] = 0;
     256                        } else if ( unique ) {
     257                                for ( registered in subscriptions[ newaction ] ) {
     258                                        if ( subscriptions[ newaction ][ registered ].namespace === namespace )
     259                                                return false;
     260                                }
     261                        }
     262
     263                        i = subscriptions[ newaction ].length - 1;
     264                        subscriptionInfo = {
     265                                namespace: namespace,
     266                                callback: callback,
     267                                priority: priority,
     268                                context: context
     269                        };
     270
     271                        for ( ; i >= 0; i-- ) {
     272                                if ( subscriptions[ newaction ][ i ].priority <= priority ) {
     273                                        subscriptions[ newaction ].splice( i + 1, 0, subscriptionInfo );
     274                                        added = true;
     275                                        break;
     276                                }
     277                        }
     278
     279                        if ( !added ) {
     280                                subscriptions[ newaction ].unshift( subscriptionInfo );
     281                        }
     282                }
     283
     284                return true;
     285        }
     286
     287        /**
     288         * Removes an action
     289         *
     290         * Usage: removeAction( action.namespace )
     291         *
     292         * Different actions on the same hook are identified by their namespace.
     293         *
     294         * @action string Name of the action and the namespace that was used on registering it.
     295         */ 
     296        this.removeAction = function( action_name ) {
     297                var act = action_name.split('.'), namespace, action, i, ret = false, actions, reindexed = [];
     298
     299                if ( !act[1] )
     300                        return false;
     301
     302                action = act[0];
     303                namespace = act[1];
     304
     305                if ( !subscriptions[ action ] )
     306                        return false;
     307
     308                actions = subscriptions[ action ];
     309
     310                for ( i in actions ) {
     311                        if ( !actions[ i ] ) {
     312                                continue;
     313                        } else if ( actions[ i ].namespace === namespace ) {
     314                                ret = true;
     315                                continue;
     316                        } else {
     317                                reindexed.push( actions[i] );
     318                        }
     319                }
     320
     321                if ( reindexed.length )
     322                        subscriptions[ action ] = reindexed;
     323                else
     324                        delete( subscriptions[ action ] );
     325
     326                return ret;
     327        }
     328
     329        /**
     330         * Removes all actions from a hook.
     331         *
     332         * @action_name string Name of the action.
     333         */
     334        this.removeAllActions = function( action_name ) {
     335                var act = action_name.split('.'), action = act[0];
     336               
     337                if ( !subscriptions[ action ] )
     338                        return false;
     339
     340                delete( subscriptions[ action ] );
     341                return true;
     342        }
     343
     344        // Returns the number of times an action was ran or false if the action is not registered.
     345        this.didAction = function( action_name ) {
     346                var act = action_name.split('.'), action = act[0];
     347
     348                if ( subscriptions[ action ] )
     349                        return subscriptions[ action ].done;
     350
     351                return false;
     352        }
     353
     354        // Returns an array of all currently registered actions.
     355        // TODO needed?
     356        this.registeredActions = function() {
     357                var ret = [], i;
     358
     359                for ( i in subscriptions ) {
     360                        ret.push(i)
     361                }
     362
     363                return ret;
     364        }
     365
     366};
     367
     368// instantiate
     369wp._private.actions = new wp._private.WP_Actions;
     370wp.doAction = wp._private.actions.doAction;
     371wp.addAction = wp._private.actions.addAction;
     372wp.removeAction = wp._private.actions.removeAction;
     373wp.removeAllActions = wp._private.actions.removeAllActions;
     374wp.didAction = wp._private.actions.didAction;
     375