Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ce/ve.ce.KeyDownHandlerFactory.js

raw symbols SCIP
1 /*!
2 * VisualEditor KeyDownHandlerFactory class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Key down handler factory.
9 *
10 * @class
11 * @extends OO.Factory
12 * @constructor
13 */
14 ve.ce.KeyDownHandlerFactory = function VeCeKeyDownHandlerFactory() {
15 // Parent constructor
16 ve.ce.KeyDownHandlerFactory.super.apply( this, arguments );
17
18 // Handlers which match all kinds and a specific type
19 this.handlerNamesByKeys = {};
20 };
21
22 /* Inheritance */
23
24 OO.inheritClass( ve.ce.KeyDownHandlerFactory, OO.Factory );
25
26 /* Methods */
27
28 /**
29 * Register a constructor with the factory.
30 *
31 * function MyClass() {};
32 * OO.initClass( MyClass );
33 * MyClass.static.name = 'hello';
34 * // Register class with the factory, available via the symbolic name "hello"
35 * factory.register( MyClass );
36 *
37 * See https://doc.wikimedia.org/oojs/master/OO.Factory.html
38 *
39 * @param {Function} constructor Constructor to use when creating object
40 * @param {string} [name] Symbolic name to use for #create().
41 * This parameter may be omitted in favour of letting the constructor decide
42 * its own name, through `constructor.static.name`.
43 * @throws {Error} If a parameter is invalid
44 */
45 ve.ce.KeyDownHandlerFactory.prototype.register = function ( constructor ) {
46 // Parent method
47 ve.ce.KeyDownHandlerFactory.super.prototype.register.call( this, constructor );
48
49 const keys = constructor.static.keys;
50 const name = constructor.static.name;
51
52 // TODO: Clean up handlerNamesByKeys in unregister
53 for ( let i = 0, ilen = keys.length; i < ilen; i++ ) {
54 this.handlerNamesByKeys[ keys[ i ] ] = this.handlerNamesByKeys[ keys[ i ] ] || [];
55 if ( this.handlerNamesByKeys[ keys[ i ] ].indexOf( name ) === -1 ) {
56 this.handlerNamesByKeys[ keys[ i ] ].push( name );
57 }
58 }
59 };
60
61 /**
62 * Get the handler for a specific key
63 *
64 * @param {number} key Key code
65 * @param {string} selectionName Selection type name
66 * @return {Function[]} Matched handlers
67 */
68 ve.ce.KeyDownHandlerFactory.prototype.lookupHandlersForKey = function ( key, selectionName ) {
69 const constructors = [],
70 names = this.handlerNamesByKeys[ key ] || [];
71
72 // Length is likely to be 1 or 0 so don't cache
73 for ( let i = 0; i < names.length; i++ ) {
74 const constructor = this.registry[ names[ i ] ];
75 const supportedSelections = constructor.static.supportedSelections;
76 if ( !supportedSelections || supportedSelections.indexOf( selectionName ) !== -1 ) {
77 constructors.push( constructor );
78 }
79 }
80
81 return constructors;
82 };
83
84 /**
85 * Execute the handlers for a specific key
86 *
87 * @param {number} key Key code
88 * @param {string} selectionName Selection type name
89 * @param {ve.ce.Surface} surface
90 * @param {jQuery.Event} e Key down event
91 * @return {boolean} Some handlers acted
92 */
93 ve.ce.KeyDownHandlerFactory.prototype.executeHandlersForKey = function ( key, selectionName, surface, e ) {
94 const handlers = this.lookupHandlersForKey( key, selectionName );
95
96 let acted = false;
97 // Length is likely to be 1 or 0 so don't cache
98 for ( let i = 0; i < handlers.length; i++ ) {
99 if ( handlers[ i ].static.execute( surface, e ) ) {
100 acted = true;
101 }
102 }
103
104 return acted;
105 };
106
107 /* Initialization */
108
109 ve.ce.keyDownHandlerFactory = new ve.ce.KeyDownHandlerFactory();