Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/ve.ui.DataTransferHandlerFactory.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataTransferHandlerFactory class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Data transfer handler factory.
9 *
10 * @class
11 * @extends OO.Factory
12 * @constructor
13 */
14 ve.ui.DataTransferHandlerFactory = function VeUiDataTransferHandlerFactory() {
15 // Parent constructor
16 ve.ui.DataTransferHandlerFactory.super.apply( this, arguments );
17
18 // Handlers which match all kinds and a specific type
19 this.handlerNamesByType = {};
20 // Handlers which match a specific kind and type
21 this.handlerNamesByKindAndType = {};
22 // Handlers which match a specific file extension as a fallback
23 this.handlerNamesByExtension = {};
24 };
25
26 /* Inheritance */
27
28 OO.inheritClass( ve.ui.DataTransferHandlerFactory, OO.Factory );
29
30 /* Methods */
31
32 /**
33 * Register a constructor with the factory.
34 *
35 * function MyClass() {};
36 * OO.initClass( MyClass );
37 * MyClass.static.name = 'hello';
38 * // Register class with the factory, available via the symbolic name "hello"
39 * factory.register( MyClass );
40 *
41 * See https://doc.wikimedia.org/oojs/master/OO.Factory.html
42 *
43 * @param {Function} constructor Constructor to use when creating object
44 * @param {string} [name] Symbolic name to use for #create().
45 * This parameter may be omitted in favour of letting the constructor decide
46 * its own name, through `constructor.static.name`.
47 * @throws {Error} If a parameter is invalid
48 */
49 ve.ui.DataTransferHandlerFactory.prototype.register = function ( constructor ) {
50 // Parent method
51 ve.ui.DataTransferHandlerFactory.super.prototype.register.apply( this, arguments );
52
53 this.updateIndexes( constructor, true );
54 };
55
56 /**
57 * Unregister a constructor from the factory.
58 *
59 * See https://doc.wikimedia.org/oojs/master/OO.Factory.html
60 *
61 * @param {string|Function} constructor Constructor function or symbolic name to unregister
62 * @throws {Error} If a parameter is invalid
63 */
64 ve.ui.DataTransferHandlerFactory.prototype.unregister = function ( constructor ) {
65 // Parent method
66 ve.ui.DataTransferHandlerFactory.super.prototype.unregister.apply( this, arguments );
67
68 this.updateIndexes( constructor, false );
69 };
70
71 /**
72 * Update indexes used for handler loopup
73 *
74 * @param {Function} constructor Handler's constructor to insert/remove
75 * @param {boolean} insert Insert the handler into the indexes, remove otherwise
76 */
77 ve.ui.DataTransferHandlerFactory.prototype.updateIndexes = function ( constructor, insert ) {
78 function ensureArray( obj, prop ) {
79 if ( obj[ prop ] === undefined ) {
80 obj[ prop ] = [];
81 }
82 return obj[ prop ];
83 }
84
85 function ensureMap( obj, prop ) {
86 if ( obj[ prop ] === undefined ) {
87 obj[ prop ] = {};
88 }
89 return obj[ prop ];
90 }
91
92 function remove( arr, item ) {
93 let index;
94 if ( ( index = arr.indexOf( item ) ) !== -1 ) {
95 arr.splice( index, 1 );
96 }
97 }
98
99 const kinds = constructor.static.kinds,
100 types = constructor.static.types,
101 extensions = constructor.static.extensions;
102
103 if ( !kinds ) {
104 for ( let j = 0, jlen = types.length; j < jlen; j++ ) {
105 if ( insert ) {
106 ensureArray( this.handlerNamesByType, types[ j ] ).unshift( constructor.static.name );
107 } else {
108 remove( this.handlerNamesByType[ types[ j ] ], constructor.static.name );
109 }
110 }
111 } else {
112 for ( let i = 0, ilen = kinds.length; i < ilen; i++ ) {
113 for ( let j = 0, jlen = types.length; j < jlen; j++ ) {
114 if ( insert ) {
115 ensureArray(
116 ensureMap( this.handlerNamesByKindAndType, kinds[ i ] ),
117 types[ j ]
118 ).unshift( constructor.static.name );
119 } else {
120 remove( this.handlerNamesByKindAndType[ kinds[ i ] ][ types[ j ] ], constructor.static.name );
121 }
122 }
123 }
124 }
125 if ( constructor.prototype instanceof ve.ui.FileTransferHandler ) {
126 for ( let i = 0, ilen = extensions.length; i < ilen; i++ ) {
127 if ( insert ) {
128 ensureArray( this.handlerNamesByExtension, extensions[ i ] ).unshift( constructor.static.name );
129 } else {
130 remove( this.handlerNamesByExtension[ extensions[ i ] ], constructor.static.name );
131 }
132 }
133 }
134 };
135
136 /**
137 * Get a handler name for a specific data transfer item
138 *
139 * @param {ve.ui.DataTransferItem} item Data transfer item
140 * @param {boolean} isPaste Handler being used for paste
141 * @param {boolean} isPasteSpecial Handler being used for "paste special"
142 * @return {string|undefined} Handler name, or undefined if not found
143 */
144 ve.ui.DataTransferHandlerFactory.prototype.getHandlerNameForItem = function ( item, isPaste, isPasteSpecial ) {
145 // Fetch a given nested property, returning a zero-length array if
146 // any component of the path is not present.
147 // This is similar to ve.getProp, except with a `hasOwnProperty`
148 // test to ensure we aren't fooled by __proto__ and friends.
149 function fetch( obj, ...props ) {
150 props.every( ( prop ) => {
151 if (
152 typeof prop !== 'string' ||
153 !Object.prototype.hasOwnProperty.call( obj, prop )
154 ) {
155 obj = [];
156 return false;
157 }
158 obj = obj[ prop ];
159 return true;
160 } );
161 return obj;
162 }
163
164 const names = [
165 // 1. Match by kind + type (e.g. 'file' + 'text/html')
166 ...fetch( this.handlerNamesByKindAndType, item.kind, item.type ),
167 // 2. Match by just type (e.g. 'image/jpeg')
168 ...fetch( this.handlerNamesByType, item.type ),
169 // 3. Match by file extension (e.g. 'csv')
170 ...fetch( this.handlerNamesByExtension, item.getExtension() )
171 ];
172
173 for ( let i = 0; i < names.length; i++ ) {
174 const name = names[ i ];
175 const constructor = this.registry[ name ];
176
177 if ( isPasteSpecial && !constructor.static.handlesPasteSpecial ) {
178 continue;
179 }
180
181 if ( isPaste && !constructor.static.handlesPaste ) {
182 continue;
183 }
184
185 if ( constructor.static.matchFunction && !constructor.static.matchFunction( item ) ) {
186 continue;
187 }
188
189 return name;
190 }
191
192 // No matching handler
193 return;
194 };
195
196 /* Initialization */
197
198 ve.ui.dataTransferHandlerFactory = new ve.ui.DataTransferHandlerFactory();