| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
ve.ui.DataTransferHandlerFactory = function VeUiDataTransferHandlerFactory() { |
| 15 |
|
| 16 |
ve.ui.DataTransferHandlerFactory.super.apply( this, arguments ); |
| 17 |
|
| 18 |
|
| 19 |
this.handlerNamesByType = {}; |
| 20 |
|
| 21 |
this.handlerNamesByKindAndType = {}; |
| 22 |
|
| 23 |
this.handlerNamesByExtension = {}; |
| 24 |
}; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
OO.inheritClass( ve.ui.DataTransferHandlerFactory, OO.Factory ); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
ve.ui.DataTransferHandlerFactory.prototype.register = function ( constructor ) { |
| 50 |
|
| 51 |
ve.ui.DataTransferHandlerFactory.super.prototype.register.apply( this, arguments ); |
| 52 |
|
| 53 |
this.updateIndexes( constructor, true ); |
| 54 |
}; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
ve.ui.DataTransferHandlerFactory.prototype.unregister = function ( constructor ) { |
| 65 |
|
| 66 |
ve.ui.DataTransferHandlerFactory.super.prototype.unregister.apply( this, arguments ); |
| 67 |
|
| 68 |
this.updateIndexes( constructor, false ); |
| 69 |
}; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 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 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
ve.ui.DataTransferHandlerFactory.prototype.getHandlerNameForItem = function ( item, isPaste, isPasteSpecial ) { |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 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 |
|
| 166 |
...fetch( this.handlerNamesByKindAndType, item.kind, item.type ), |
| 167 |
|
| 168 |
...fetch( this.handlerNamesByType, item.type ), |
| 169 |
|
| 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 |
|
| 193 |
return; |
| 194 |
}; |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
ve.ui.dataTransferHandlerFactory = new ve.ui.DataTransferHandlerFactory(); |