| 1 |
// Copyright 2011 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview A wrapper for the HTML5 FileSaver object. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
goog.provide('goog.fs.FileSaver'); |
| 21 |
goog.provide('goog.fs.FileSaver.EventType'); |
| 22 |
goog.provide('goog.fs.FileSaver.ReadyState'); |
| 23 |
|
| 24 |
goog.require('goog.events.EventTarget'); |
| 25 |
goog.require('goog.fs.Error'); |
| 26 |
goog.require('goog.fs.ProgressEvent'); |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
/** |
| 31 |
* An object for monitoring the saving of files. This emits ProgressEvents of |
| 32 |
* the types listed in {@link goog.fs.FileSaver.EventType}. |
| 33 |
* |
| 34 |
* This should not be instantiated directly. Instead, its subclass |
| 35 |
* {@link goog.fs.FileWriter} should be accessed via |
| 36 |
* {@link goog.fs.FileEntry#createWriter}. |
| 37 |
* |
| 38 |
* @param {!FileSaver} fileSaver The underlying FileSaver object. |
| 39 |
* @constructor |
| 40 |
* @extends {goog.events.EventTarget} |
| 41 |
*/ |
| 42 |
goog.fs.FileSaver = function(fileSaver) { |
| 43 |
goog.fs.FileSaver.base(this, 'constructor'); |
| 44 |
|
| 45 |
/** |
| 46 |
* The underlying FileSaver object. |
| 47 |
* |
| 48 |
* @type {!FileSaver} |
| 49 |
* @private |
| 50 |
*/ |
| 51 |
this.saver_ = fileSaver; |
| 52 |
|
| 53 |
this.saver_.onwritestart = goog.bind(this.dispatchProgressEvent_, this); |
| 54 |
this.saver_.onprogress = goog.bind(this.dispatchProgressEvent_, this); |
| 55 |
this.saver_.onwrite = goog.bind(this.dispatchProgressEvent_, this); |
| 56 |
this.saver_.onabort = goog.bind(this.dispatchProgressEvent_, this); |
| 57 |
this.saver_.onerror = goog.bind(this.dispatchProgressEvent_, this); |
| 58 |
this.saver_.onwriteend = goog.bind(this.dispatchProgressEvent_, this); |
| 59 |
}; |
| 60 |
goog.inherits(goog.fs.FileSaver, goog.events.EventTarget); |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* Possible states for a FileSaver. |
| 65 |
* |
| 66 |
* @enum {number} |
| 67 |
*/ |
| 68 |
goog.fs.FileSaver.ReadyState = { |
| 69 |
/** |
| 70 |
* The object has been constructed, but there is no pending write. |
| 71 |
*/ |
| 72 |
INIT: 0, |
| 73 |
/** |
| 74 |
* Data is being written. |
| 75 |
*/ |
| 76 |
WRITING: 1, |
| 77 |
/** |
| 78 |
* The data has been written to the file, the write was aborted, or an error |
| 79 |
* occurred. |
| 80 |
*/ |
| 81 |
DONE: 2 |
| 82 |
}; |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* Events emitted by a FileSaver. |
| 87 |
* |
| 88 |
* @enum {string} |
| 89 |
*/ |
| 90 |
goog.fs.FileSaver.EventType = { |
| 91 |
/** |
| 92 |
* Emitted when the writing begins. readyState will be WRITING. |
| 93 |
*/ |
| 94 |
WRITE_START: 'writestart', |
| 95 |
/** |
| 96 |
* Emitted when progress has been made in saving the file. readyState will be |
| 97 |
* WRITING. |
| 98 |
*/ |
| 99 |
PROGRESS: 'progress', |
| 100 |
/** |
| 101 |
* Emitted when the data has been successfully written. readyState will be |
| 102 |
* WRITING. |
| 103 |
*/ |
| 104 |
WRITE: 'write', |
| 105 |
/** |
| 106 |
* Emitted when the writing has been aborted. readyState will be WRITING. |
| 107 |
*/ |
| 108 |
ABORT: 'abort', |
| 109 |
/** |
| 110 |
* Emitted when an error is encountered or the writing has been aborted. |
| 111 |
* readyState will be WRITING. |
| 112 |
*/ |
| 113 |
ERROR: 'error', |
| 114 |
/** |
| 115 |
* Emitted when the writing is finished, whether successfully or not. |
| 116 |
* readyState will be DONE. |
| 117 |
*/ |
| 118 |
WRITE_END: 'writeend' |
| 119 |
}; |
| 120 |
|
| 121 |
|
| 122 |
/** |
| 123 |
* Abort the writing of the file. |
| 124 |
*/ |
| 125 |
goog.fs.FileSaver.prototype.abort = function() { |
| 126 |
try { |
| 127 |
this.saver_.abort(); |
| 128 |
} catch (e) { |
| 129 |
throw new goog.fs.Error(e, 'aborting save'); |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
/** |
| 135 |
* @return {goog.fs.FileSaver.ReadyState} The current state of the FileSaver. |
| 136 |
*/ |
| 137 |
goog.fs.FileSaver.prototype.getReadyState = function() { |
| 138 |
return /** @type {goog.fs.FileSaver.ReadyState} */ (this.saver_.readyState); |
| 139 |
}; |
| 140 |
|
| 141 |
|
| 142 |
/** |
| 143 |
* @return {goog.fs.Error} The error encountered while writing, if any. |
| 144 |
*/ |
| 145 |
goog.fs.FileSaver.prototype.getError = function() { |
| 146 |
return this.saver_.error && |
| 147 |
new goog.fs.Error(this.saver_.error, 'saving file'); |
| 148 |
}; |
| 149 |
|
| 150 |
|
| 151 |
/** |
| 152 |
* Wrap a progress event emitted by the underlying file saver and re-emit it. |
| 153 |
* |
| 154 |
* @param {!ProgressEvent} event The underlying event. |
| 155 |
* @private |
| 156 |
*/ |
| 157 |
goog.fs.FileSaver.prototype.dispatchProgressEvent_ = function(event) { |
| 158 |
this.dispatchEvent(new goog.fs.ProgressEvent(event, this)); |
| 159 |
}; |
| 160 |
|
| 161 |
|
| 162 |
/** @override */ |
| 163 |
goog.fs.FileSaver.prototype.disposeInternal = function() { |
| 164 |
delete this.saver_; |
| 165 |
goog.fs.FileSaver.base(this, 'disposeInternal'); |
| 166 |
}; |