Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface data transfer handler class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Data transfer handler.
9 *
10 * @class
11 * @abstract
12 *
13 * @constructor
14 * @param {ve.ui.Surface} surface
15 * @param {ve.ui.DataTransferItem} item Data transfer item to handle
16 */
17 ve.ui.DataTransferHandler = function VeUiDataTransferHandler( surface, item ) {
18 // Properties
19 this.surface = surface;
20 this.item = item;
21 this.progress = false;
22 this.progressBar = null;
23
24 this.insertableDataDeferred = ve.createDeferred();
25 };
26
27 /* Inheritance */
28
29 OO.initClass( ve.ui.DataTransferHandler );
30
31 /* Static properties */
32
33 /**
34 * Symbolic name for this handler. Must be unique.
35 *
36 * @static
37 * @property {string}
38 * @inheritable
39 */
40 ve.ui.DataTransferHandler.static.name = null;
41
42 /**
43 * List of transfer kinds supported by this handler
44 *
45 * Null means all kinds are supported.
46 *
47 * @static
48 * @property {string[]|null}
49 * @inheritable
50 */
51 ve.ui.DataTransferHandler.static.kinds = null;
52
53 /**
54 * List of mime types supported by this handler
55 *
56 * @static
57 * @property {string[]}
58 * @inheritable
59 */
60 ve.ui.DataTransferHandler.static.types = [];
61
62 /**
63 * Use handler when data transfer source is a paste
64 *
65 * @static
66 * @type {boolean}
67 * @inheritable
68 */
69 ve.ui.DataTransferHandler.static.handlesPaste = true;
70
71 /**
72 * Use handler when data transfer source is a "paste special"
73 *
74 * @static
75 * @type {boolean}
76 * @inheritable
77 */
78 ve.ui.DataTransferHandler.static.handlesPasteSpecial = false;
79
80 /**
81 * Custom match function which is given the data transfer item as its only argument
82 * and returns a boolean indicating if the handler matches
83 *
84 * Null means the handler always matches
85 *
86 * @static
87 * @type {Function}
88 * @inheritable
89 */
90 ve.ui.DataTransferHandler.static.matchFunction = null;
91
92 /* Methods */
93
94 /**
95 * Process the file
96 *
97 * Implementations should aim to resolve this.insertableDataDeferred.
98 *
99 * @abstract
100 * @method
101 */
102 ve.ui.DataTransferHandler.prototype.process = null;
103
104 /**
105 * Insert the file at a specified fragment
106 *
107 * @return {jQuery.Promise} Promise which resolves with data to insert
108 */
109 ve.ui.DataTransferHandler.prototype.getInsertableData = function () {
110 this.process();
111
112 return this.insertableDataDeferred.promise();
113 };
114
115 /**
116 * Resolve the data transfer handler with some data
117 *
118 * @param {ve.dm.Document|string|Array} dataOrDoc Insertable data or document
119 */
120 ve.ui.DataTransferHandler.prototype.resolve = function ( dataOrDoc ) {
121 this.insertableDataDeferred.resolve( dataOrDoc );
122 };
123
124 /**
125 * Abort the data transfer handler
126 */
127 ve.ui.DataTransferHandler.prototype.abort = function () {
128 this.insertableDataDeferred.reject();
129 };
130
131 /**
132 * Create a progress bar with a specified label
133 *
134 * @param {jQuery.Promise} progressCompletePromise Promise which resolves when the progress action is complete
135 * @param {jQuery|string|Function} label Progress bar label
136 */
137 ve.ui.DataTransferHandler.prototype.createProgress = function ( progressCompletePromise, label ) {
138 this.surface.createProgress( progressCompletePromise, label ).done( ( progressBar, cancelPromise ) => {
139 // Set any progress that was achieved before this resolved
140 progressBar.setProgress( this.progress );
141 this.progressBar = progressBar;
142 cancelPromise.fail( this.abort.bind( this ) );
143 } );
144 };
145
146 /**
147 * Set progress bar progress
148 *
149 * Progress is stored in a property in case the progress bar doesn't exist yet.
150 *
151 * @param {number} progress Progress percent
152 */
153 ve.ui.DataTransferHandler.prototype.setProgress = function ( progress ) {
154 this.progress = progress;
155 if ( this.progressBar ) {
156 this.progressBar.setProgress( this.progress );
157 }
158 };