Path: blob/trunk/third_party/closure/goog/fs/filesaver.js
2868 views
// Copyright 2011 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview A wrapper for the HTML5 FileSaver object.16*17*/1819goog.provide('goog.fs.FileSaver');20goog.provide('goog.fs.FileSaver.EventType');21goog.provide('goog.fs.FileSaver.ReadyState');2223goog.require('goog.events.EventTarget');24goog.require('goog.fs.Error');25goog.require('goog.fs.ProgressEvent');26272829/**30* An object for monitoring the saving of files. This emits ProgressEvents of31* the types listed in {@link goog.fs.FileSaver.EventType}.32*33* This should not be instantiated directly. Instead, its subclass34* {@link goog.fs.FileWriter} should be accessed via35* {@link goog.fs.FileEntry#createWriter}.36*37* @param {!FileSaver} fileSaver The underlying FileSaver object.38* @constructor39* @extends {goog.events.EventTarget}40*/41goog.fs.FileSaver = function(fileSaver) {42goog.fs.FileSaver.base(this, 'constructor');4344/**45* The underlying FileSaver object.46*47* @type {!FileSaver}48* @private49*/50this.saver_ = fileSaver;5152this.saver_.onwritestart = goog.bind(this.dispatchProgressEvent_, this);53this.saver_.onprogress = goog.bind(this.dispatchProgressEvent_, this);54this.saver_.onwrite = goog.bind(this.dispatchProgressEvent_, this);55this.saver_.onabort = goog.bind(this.dispatchProgressEvent_, this);56this.saver_.onerror = goog.bind(this.dispatchProgressEvent_, this);57this.saver_.onwriteend = goog.bind(this.dispatchProgressEvent_, this);58};59goog.inherits(goog.fs.FileSaver, goog.events.EventTarget);606162/**63* Possible states for a FileSaver.64*65* @enum {number}66*/67goog.fs.FileSaver.ReadyState = {68/**69* The object has been constructed, but there is no pending write.70*/71INIT: 0,72/**73* Data is being written.74*/75WRITING: 1,76/**77* The data has been written to the file, the write was aborted, or an error78* occurred.79*/80DONE: 281};828384/**85* Events emitted by a FileSaver.86*87* @enum {string}88*/89goog.fs.FileSaver.EventType = {90/**91* Emitted when the writing begins. readyState will be WRITING.92*/93WRITE_START: 'writestart',94/**95* Emitted when progress has been made in saving the file. readyState will be96* WRITING.97*/98PROGRESS: 'progress',99/**100* Emitted when the data has been successfully written. readyState will be101* WRITING.102*/103WRITE: 'write',104/**105* Emitted when the writing has been aborted. readyState will be WRITING.106*/107ABORT: 'abort',108/**109* Emitted when an error is encountered or the writing has been aborted.110* readyState will be WRITING.111*/112ERROR: 'error',113/**114* Emitted when the writing is finished, whether successfully or not.115* readyState will be DONE.116*/117WRITE_END: 'writeend'118};119120121/**122* Abort the writing of the file.123*/124goog.fs.FileSaver.prototype.abort = function() {125try {126this.saver_.abort();127} catch (e) {128throw new goog.fs.Error(e, 'aborting save');129}130};131132133/**134* @return {goog.fs.FileSaver.ReadyState} The current state of the FileSaver.135*/136goog.fs.FileSaver.prototype.getReadyState = function() {137return /** @type {goog.fs.FileSaver.ReadyState} */ (this.saver_.readyState);138};139140141/**142* @return {goog.fs.Error} The error encountered while writing, if any.143*/144goog.fs.FileSaver.prototype.getError = function() {145return this.saver_.error &&146new goog.fs.Error(this.saver_.error, 'saving file');147};148149150/**151* Wrap a progress event emitted by the underlying file saver and re-emit it.152*153* @param {!ProgressEvent} event The underlying event.154* @private155*/156goog.fs.FileSaver.prototype.dispatchProgressEvent_ = function(event) {157this.dispatchEvent(new goog.fs.ProgressEvent(event, this));158};159160161/** @override */162goog.fs.FileSaver.prototype.disposeInternal = function() {163delete this.saver_;164goog.fs.FileSaver.base(this, 'disposeInternal');165};166167168