Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/ve.ui.FileTransferHandler.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 * @extends ve.ui.DataTransferHandler
12 * @abstract
13 *
14 * @constructor
15 * @param {ve.ui.Surface} surface
16 * @param {ve.ui.DataTransferItem} item
17 */
18 ve.ui.FileTransferHandler = function VeUiFileTransferHandler() {
19 // Parent constructor
20 ve.ui.FileTransferHandler.super.apply( this, arguments );
21
22 // Properties
23 this.file = this.item.getAsFile();
24
25 this.reader = new FileReader();
26
27 // Events
28 this.reader.addEventListener( 'progress', this.onFileProgress.bind( this ) );
29 this.reader.addEventListener( 'load', this.onFileLoad.bind( this ) );
30 this.reader.addEventListener( 'error', this.onFileError.bind( this ) );
31 };
32
33 /* Inheritance */
34
35 OO.inheritClass( ve.ui.FileTransferHandler, ve.ui.DataTransferHandler );
36
37 /* Static properties */
38
39 ve.ui.FileTransferHandler.static.kinds = [ 'file' ];
40
41 /**
42 * List of file extensions supported by this handler
43 *
44 * This is used as a fallback if no types were matched.
45 *
46 * @static
47 * @property {string[]}
48 * @inheritable
49 */
50 ve.ui.FileTransferHandler.static.extensions = [];
51
52 /* Methods */
53
54 /**
55 * @inheritdoc
56 */
57 ve.ui.FileTransferHandler.prototype.process = function () {
58 this.createProgress( this.insertableDataDeferred.promise() );
59 this.reader.readAsText( this.file );
60 };
61
62 /**
63 * Handle progress events from the file reader
64 *
65 * @param {Event} e Progress event
66 */
67 ve.ui.FileTransferHandler.prototype.onFileProgress = function ( e ) {
68 if ( e.lengthComputable ) {
69 this.setProgress( 100 * e.loaded / e.total );
70 } else {
71 this.setProgress( false );
72 }
73 };
74
75 /**
76 * Handle load events from the file reader
77 *
78 * @param {Event} e Load event
79 */
80 ve.ui.FileTransferHandler.prototype.onFileLoad = function () {
81 this.setProgress( 100 );
82 };
83
84 /**
85 * Handle error events from the file reader
86 *
87 * @param {Event} e Error event
88 */
89 ve.ui.FileTransferHandler.prototype.onFileError = function () {
90 this.abort();
91 };
92
93 /**
94 * @inheritdoc
95 */
96 ve.ui.FileTransferHandler.prototype.abort = function () {
97 // Parent method
98 ve.ui.FileTransferHandler.super.prototype.abort.apply( this, arguments );
99
100 this.reader.abort();
101 };
102
103 /**
104 * Overrides the parent to make the default label the filename
105 *
106 * @param {jQuery.Promise} progressCompletePromise
107 * @param {jQuery|string|Function} [label] Progress bar label, defaults to file name
108 */
109 ve.ui.FileTransferHandler.prototype.createProgress = function ( progressCompletePromise, label ) {
110 // Make the default label the filename
111 label = label || this.file.name;
112
113 // Parent method
114 ve.ui.FileTransferHandler.super.prototype.createProgress.call( this, progressCompletePromise, label );
115 };