| @@ -1,79 +1,79 @@ | ||
| 1 | -const Hook = { | |
| 2 | - hooks: { action: {}, filter: {} }, | |
| 3 | - addAction( action, callable, priority, tag ) { | |
| 4 | - this.addHook( 'action', action, callable, priority, tag ); | |
| 5 | - return this; | |
| 6 | - }, | |
| 7 | - addFilter( action, callable, priority, tag ) { | |
| 8 | - this.addHook( 'filter', action, callable, priority, tag ); | |
| 9 | - return this; | |
| 10 | - }, | |
| 11 | - doAction( action ) { | |
| 12 | - return this.doHook( 'action', action, arguments ); | |
| 13 | - }, | |
| 14 | - applyFilters( action ) { | |
| 15 | - return this.doHook( 'filter', action, arguments ); | |
| 16 | - }, | |
| 17 | - removeAction( action, tag ) { | |
| 18 | - this.removeHook( 'action', action, tag ); | |
| 19 | - return this; | |
| 20 | - }, | |
| 21 | - removeFilter( action, priority, tag ) { | |
| 22 | - this.removeHook( 'filter', action, priority, tag ); | |
| 23 | - return this; | |
| 24 | - }, | |
| 25 | - addHook( hookType, action, callable, priority, tag ) { | |
| 26 | - if ( undefined === this.hooks[ hookType ][ action ] ) { | |
| 27 | - this.hooks[ hookType ][ action ] = []; | |
| 28 | - } | |
| 29 | - const hooks = this.hooks[ hookType ][ action ]; | |
| 30 | - if ( undefined === tag ) { | |
| 31 | - tag = action + '_' + hooks.length; | |
| 32 | - } | |
| 33 | - this.hooks[ hookType ][ action ].push( { tag, callable, priority } ); | |
| 34 | - return this; | |
| 35 | - }, | |
| 36 | - doHook( hookType, action, args ) { | |
| 37 | - args = Array.prototype.slice.call( args, 1 ); | |
| 38 | - | |
| 39 | - if ( undefined !== this.hooks[ hookType ][ action ] ) { | |
| 40 | - let hooks = this.hooks[ hookType ][ action ], | |
| 41 | - hook; | |
| 42 | - | |
| 43 | - hooks.sort( function( a, b ) { | |
| 44 | - return a.priority - b.priority; | |
| 45 | - } ); | |
| 46 | - | |
| 47 | - for ( let i = 0; i < hooks.length; i++ ) { | |
| 48 | - hook = hooks[ i ].callable; | |
| 49 | - if ( typeof hook !== 'function' ) { | |
| 50 | - hook = window[ hook ]; | |
| 51 | - } | |
| 52 | - | |
| 53 | - if ( 'action' === hookType ) { | |
| 54 | - args[ i ] = hook.apply( null, args ); | |
| 55 | - } else { | |
| 56 | - args[ 0 ] = hook.apply( null, args ); | |
| 57 | - } | |
| 58 | - } | |
| 59 | - } | |
| 60 | - | |
| 61 | - if ( 'filter' === hookType ) { | |
| 62 | - return args[ 0 ]; | |
| 63 | - } | |
| 64 | - return args; | |
| 65 | - }, | |
| 66 | - removeHook( hookType, action, priority, tag ) { | |
| 67 | - if ( undefined !== this.hooks[ hookType ][ action ] ) { | |
| 68 | - const hooks = this.hooks[ hookType ][ action ]; | |
| 69 | - for ( let i = hooks.length - 1; i >= 0; i-- ) { | |
| 70 | - if ( ( undefined === tag || tag === hooks[ i ].tag ) && ( undefined === priority || priority === hooks[ i ].priority ) ) { | |
| 71 | - hooks.splice( i, 1 ); | |
| 72 | - } | |
| 73 | - } | |
| 74 | - } | |
| 75 | - return this; | |
| 76 | - }, | |
| 77 | -}; | |
| 78 | - | |
| 79 | -export default Hook; | |
| 1 | +const Hook = { | |
| 2 | + hooks: { action: {}, filter: {} }, | |
| 3 | + addAction( action, callable, priority, tag ) { | |
| 4 | + this.addHook( 'action', action, callable, priority, tag ); | |
| 5 | + return this; | |
| 6 | + }, | |
| 7 | + addFilter( action, callable, priority, tag ) { | |
| 8 | + this.addHook( 'filter', action, callable, priority, tag ); | |
| 9 | + return this; | |
| 10 | + }, | |
| 11 | + doAction( action ) { | |
| 12 | + return this.doHook( 'action', action, arguments ); | |
| 13 | + }, | |
| 14 | + applyFilters( action ) { | |
| 15 | + return this.doHook( 'filter', action, arguments ); | |
| 16 | + }, | |
| 17 | + removeAction( action, tag ) { | |
| 18 | + this.removeHook( 'action', action, tag ); | |
| 19 | + return this; | |
| 20 | + }, | |
| 21 | + removeFilter( action, priority, tag ) { | |
| 22 | + this.removeHook( 'filter', action, priority, tag ); | |
| 23 | + return this; | |
| 24 | + }, | |
| 25 | + addHook( hookType, action, callable, priority, tag ) { | |
| 26 | + if ( undefined === this.hooks[ hookType ][ action ] ) { | |
| 27 | + this.hooks[ hookType ][ action ] = []; | |
| 28 | + } | |
| 29 | + const hooks = this.hooks[ hookType ][ action ]; | |
| 30 | + if ( undefined === tag ) { | |
| 31 | + tag = action + '_' + hooks.length; | |
| 32 | + } | |
| 33 | + this.hooks[ hookType ][ action ].push( { tag, callable, priority } ); | |
| 34 | + return this; | |
| 35 | + }, | |
| 36 | + doHook( hookType, action, args ) { | |
| 37 | + args = Array.prototype.slice.call( args, 1 ); | |
| 38 | + | |
| 39 | + if ( undefined !== this.hooks[ hookType ][ action ] ) { | |
| 40 | + let hooks = this.hooks[ hookType ][ action ], | |
| 41 | + hook; | |
| 42 | + | |
| 43 | + hooks.sort( function( a, b ) { | |
| 44 | + return a.priority - b.priority; | |
| 45 | + } ); | |
| 46 | + | |
| 47 | + for ( let i = 0; i < hooks.length; i++ ) { | |
| 48 | + hook = hooks[ i ].callable; | |
| 49 | + if ( typeof hook !== 'function' ) { | |
| 50 | + hook = window[ hook ]; | |
| 51 | + } | |
| 52 | + | |
| 53 | + if ( 'action' === hookType ) { | |
| 54 | + args[ i ] = hook.apply( null, args ); | |
| 55 | + } else { | |
| 56 | + args[ 0 ] = hook.apply( null, args ); | |
| 57 | + } | |
| 58 | + } | |
| 59 | + } | |
| 60 | + | |
| 61 | + if ( 'filter' === hookType ) { | |
| 62 | + return args[ 0 ]; | |
| 63 | + } | |
| 64 | + return args; | |
| 65 | + }, | |
| 66 | + removeHook( hookType, action, priority, tag ) { | |
| 67 | + if ( undefined !== this.hooks[ hookType ][ action ] ) { | |
| 68 | + const hooks = this.hooks[ hookType ][ action ]; | |
| 69 | + for ( let i = hooks.length - 1; i >= 0; i-- ) { | |
| 70 | + if ( ( undefined === tag || tag === hooks[ i ].tag ) && ( undefined === priority || priority === hooks[ i ].priority ) ) { | |
| 71 | + hooks.splice( i, 1 ); | |
| 72 | + } | |
| 73 | + } | |
| 74 | + } | |
| 75 | + return this; | |
| 76 | + }, | |
| 77 | +}; | |
| 78 | + | |
| 79 | +export default Hook; | |