filesaver.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. * @fileoverview A wrapper for the HTML5 FileSaver object.
  16. *
  17. */
  18. goog.provide('goog.fs.FileSaver');
  19. goog.provide('goog.fs.FileSaver.EventType');
  20. goog.provide('goog.fs.FileSaver.ReadyState');
  21. goog.require('goog.events.EventTarget');
  22. goog.require('goog.fs.Error');
  23. goog.require('goog.fs.ProgressEvent');
  24. /**
  25. * An object for monitoring the saving of files. This emits ProgressEvents of
  26. * the types listed in {@link goog.fs.FileSaver.EventType}.
  27. *
  28. * This should not be instantiated directly. Instead, its subclass
  29. * {@link goog.fs.FileWriter} should be accessed via
  30. * {@link goog.fs.FileEntry#createWriter}.
  31. *
  32. * @param {!FileSaver} fileSaver The underlying FileSaver object.
  33. * @constructor
  34. * @extends {goog.events.EventTarget}
  35. */
  36. goog.fs.FileSaver = function(fileSaver) {
  37. goog.fs.FileSaver.base(this, 'constructor');
  38. /**
  39. * The underlying FileSaver object.
  40. *
  41. * @type {!FileSaver}
  42. * @private
  43. */
  44. this.saver_ = fileSaver;
  45. this.saver_.onwritestart = goog.bind(this.dispatchProgressEvent_, this);
  46. this.saver_.onprogress = goog.bind(this.dispatchProgressEvent_, this);
  47. this.saver_.onwrite = goog.bind(this.dispatchProgressEvent_, this);
  48. this.saver_.onabort = goog.bind(this.dispatchProgressEvent_, this);
  49. this.saver_.onerror = goog.bind(this.dispatchProgressEvent_, this);
  50. this.saver_.onwriteend = goog.bind(this.dispatchProgressEvent_, this);
  51. };
  52. goog.inherits(goog.fs.FileSaver, goog.events.EventTarget);
  53. /**
  54. * Possible states for a FileSaver.
  55. *
  56. * @enum {number}
  57. */
  58. goog.fs.FileSaver.ReadyState = {
  59. /**
  60. * The object has been constructed, but there is no pending write.
  61. */
  62. INIT: 0,
  63. /**
  64. * Data is being written.
  65. */
  66. WRITING: 1,
  67. /**
  68. * The data has been written to the file, the write was aborted, or an error
  69. * occurred.
  70. */
  71. DONE: 2
  72. };
  73. /**
  74. * Events emitted by a FileSaver.
  75. *
  76. * @enum {string}
  77. */
  78. goog.fs.FileSaver.EventType = {
  79. /**
  80. * Emitted when the writing begins. readyState will be WRITING.
  81. */
  82. WRITE_START: 'writestart',
  83. /**
  84. * Emitted when progress has been made in saving the file. readyState will be
  85. * WRITING.
  86. */
  87. PROGRESS: 'progress',
  88. /**
  89. * Emitted when the data has been successfully written. readyState will be
  90. * WRITING.
  91. */
  92. WRITE: 'write',
  93. /**
  94. * Emitted when the writing has been aborted. readyState will be WRITING.
  95. */
  96. ABORT: 'abort',
  97. /**
  98. * Emitted when an error is encountered or the writing has been aborted.
  99. * readyState will be WRITING.
  100. */
  101. ERROR: 'error',
  102. /**
  103. * Emitted when the writing is finished, whether successfully or not.
  104. * readyState will be DONE.
  105. */
  106. WRITE_END: 'writeend'
  107. };
  108. /**
  109. * Abort the writing of the file.
  110. */
  111. goog.fs.FileSaver.prototype.abort = function() {
  112. try {
  113. this.saver_.abort();
  114. } catch (e) {
  115. throw new goog.fs.Error(e, 'aborting save');
  116. }
  117. };
  118. /**
  119. * @return {goog.fs.FileSaver.ReadyState} The current state of the FileSaver.
  120. */
  121. goog.fs.FileSaver.prototype.getReadyState = function() {
  122. return /** @type {goog.fs.FileSaver.ReadyState} */ (this.saver_.readyState);
  123. };
  124. /**
  125. * @return {goog.fs.Error} The error encountered while writing, if any.
  126. */
  127. goog.fs.FileSaver.prototype.getError = function() {
  128. return this.saver_.error &&
  129. new goog.fs.Error(this.saver_.error, 'saving file');
  130. };
  131. /**
  132. * Wrap a progress event emitted by the underlying file saver and re-emit it.
  133. *
  134. * @param {!ProgressEvent} event The underlying event.
  135. * @private
  136. */
  137. goog.fs.FileSaver.prototype.dispatchProgressEvent_ = function(event) {
  138. this.dispatchEvent(new goog.fs.ProgressEvent(event, this));
  139. };
  140. /** @override */
  141. goog.fs.FileSaver.prototype.disposeInternal = function() {
  142. delete this.saver_;
  143. goog.fs.FileSaver.base(this, 'disposeInternal');
  144. };