bootstrap-source/bootstrap-3.0.3/docs-assets/js/filesaver.js
author stetrabby <info@trabucchi.de>
Sun, 29 Dec 2013 12:41:43 +0100
changeset 60 152e56840f49
parent 54 0ded9d7748b7
permissions -rwxr-xr-x
Markup changed
     1 /* Blob.js
     2  * A Blob implementation.
     3  * 2013-06-20
     4  *
     5  * By Eli Grey, http://eligrey.com
     6  * By Devin Samarin, https://github.com/eboyjr
     7  * License: X11/MIT
     8  *   See LICENSE.md
     9  */
    10 
    11 /*global self, unescape */
    12 /*jslint bitwise: true, regexp: true, confusion: true, es5: true, vars: true, white: true,
    13   plusplus: true */
    14 
    15 /*! @source http://purl.eligrey.com/github/Blob.js/blob/master/Blob.js */
    16 
    17 if (!(typeof Blob === "function" || typeof Blob === "object") || typeof URL === "undefined")
    18 if ((typeof Blob === "function" || typeof Blob === "object") && typeof webkitURL !== "undefined") self.URL = webkitURL;
    19 else var Blob = (function (view) {
    20 	"use strict";
    21 
    22 	var BlobBuilder = view.BlobBuilder || view.WebKitBlobBuilder || view.MozBlobBuilder || view.MSBlobBuilder || (function(view) {
    23 		var
    24 			  get_class = function(object) {
    25 				return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1];
    26 			}
    27 			, FakeBlobBuilder = function BlobBuilder() {
    28 				this.data = [];
    29 			}
    30 			, FakeBlob = function Blob(data, type, encoding) {
    31 				this.data = data;
    32 				this.size = data.length;
    33 				this.type = type;
    34 				this.encoding = encoding;
    35 			}
    36 			, FBB_proto = FakeBlobBuilder.prototype
    37 			, FB_proto = FakeBlob.prototype
    38 			, FileReaderSync = view.FileReaderSync
    39 			, FileException = function(type) {
    40 				this.code = this[this.name = type];
    41 			}
    42 			, file_ex_codes = (
    43 				  "NOT_FOUND_ERR SECURITY_ERR ABORT_ERR NOT_READABLE_ERR ENCODING_ERR "
    44 				+ "NO_MODIFICATION_ALLOWED_ERR INVALID_STATE_ERR SYNTAX_ERR"
    45 			).split(" ")
    46 			, file_ex_code = file_ex_codes.length
    47 			, real_URL = view.URL || view.webkitURL || view
    48 			, real_create_object_URL = real_URL.createObjectURL
    49 			, real_revoke_object_URL = real_URL.revokeObjectURL
    50 			, URL = real_URL
    51 			, btoa = view.btoa
    52 			, atob = view.atob
    53 
    54 			, ArrayBuffer = view.ArrayBuffer
    55 			, Uint8Array = view.Uint8Array
    56 		;
    57 		FakeBlob.fake = FB_proto.fake = true;
    58 		while (file_ex_code--) {
    59 			FileException.prototype[file_ex_codes[file_ex_code]] = file_ex_code + 1;
    60 		}
    61 		if (!real_URL.createObjectURL) {
    62 			URL = view.URL = {};
    63 		}
    64 		URL.createObjectURL = function(blob) {
    65 			var
    66 				  type = blob.type
    67 				, data_URI_header
    68 			;
    69 			if (type === null) {
    70 				type = "application/octet-stream";
    71 			}
    72 			if (blob instanceof FakeBlob) {
    73 				data_URI_header = "data:" + type;
    74 				if (blob.encoding === "base64") {
    75 					return data_URI_header + ";base64," + blob.data;
    76 				} else if (blob.encoding === "URI") {
    77 					return data_URI_header + "," + decodeURIComponent(blob.data);
    78 				} if (btoa) {
    79 					return data_URI_header + ";base64," + btoa(blob.data);
    80 				} else {
    81 					return data_URI_header + "," + encodeURIComponent(blob.data);
    82 				}
    83 			} else if (real_create_object_URL) {
    84 				return real_create_object_URL.call(real_URL, blob);
    85 			}
    86 		};
    87 		URL.revokeObjectURL = function(object_URL) {
    88 			if (object_URL.substring(0, 5) !== "data:" && real_revoke_object_URL) {
    89 				real_revoke_object_URL.call(real_URL, object_URL);
    90 			}
    91 		};
    92 		FBB_proto.append = function(data/*, endings*/) {
    93 			var bb = this.data;
    94 			// decode data to a binary string
    95 			if (Uint8Array && (data instanceof ArrayBuffer || data instanceof Uint8Array)) {
    96 				var
    97 					  str = ""
    98 					, buf = new Uint8Array(data)
    99 					, i = 0
   100 					, buf_len = buf.length
   101 				;
   102 				for (; i < buf_len; i++) {
   103 					str += String.fromCharCode(buf[i]);
   104 				}
   105 				bb.push(str);
   106 			} else if (get_class(data) === "Blob" || get_class(data) === "File") {
   107 				if (FileReaderSync) {
   108 					var fr = new FileReaderSync;
   109 					bb.push(fr.readAsBinaryString(data));
   110 				} else {
   111 					// async FileReader won't work as BlobBuilder is sync
   112 					throw new FileException("NOT_READABLE_ERR");
   113 				}
   114 			} else if (data instanceof FakeBlob) {
   115 				if (data.encoding === "base64" && atob) {
   116 					bb.push(atob(data.data));
   117 				} else if (data.encoding === "URI") {
   118 					bb.push(decodeURIComponent(data.data));
   119 				} else if (data.encoding === "raw") {
   120 					bb.push(data.data);
   121 				}
   122 			} else {
   123 				if (typeof data !== "string") {
   124 					data += ""; // convert unsupported types to strings
   125 				}
   126 				// decode UTF-16 to binary string
   127 				bb.push(unescape(encodeURIComponent(data)));
   128 			}
   129 		};
   130 		FBB_proto.getBlob = function(type) {
   131 			if (!arguments.length) {
   132 				type = null;
   133 			}
   134 			return new FakeBlob(this.data.join(""), type, "raw");
   135 		};
   136 		FBB_proto.toString = function() {
   137 			return "[object BlobBuilder]";
   138 		};
   139 		FB_proto.slice = function(start, end, type) {
   140 			var args = arguments.length;
   141 			if (args < 3) {
   142 				type = null;
   143 			}
   144 			return new FakeBlob(
   145 				  this.data.slice(start, args > 1 ? end : this.data.length)
   146 				, type
   147 				, this.encoding
   148 			);
   149 		};
   150 		FB_proto.toString = function() {
   151 			return "[object Blob]";
   152 		};
   153 		return FakeBlobBuilder;
   154 	}(view));
   155 
   156 	return function Blob(blobParts, options) {
   157 		var type = options ? (options.type || "") : "";
   158 		var builder = new BlobBuilder();
   159 		if (blobParts) {
   160 			for (var i = 0, len = blobParts.length; i < len; i++) {
   161 				builder.append(blobParts[i]);
   162 			}
   163 		}
   164 		return builder.getBlob(type);
   165 	};
   166 }(self));
   167 
   168 /* FileSaver.js
   169  * A saveAs() FileSaver implementation.
   170  * 2013-10-21
   171  *
   172  * By Eli Grey, http://eligrey.com
   173  * License: X11/MIT
   174  *   See LICENSE.md
   175  */
   176 
   177 /*global self */
   178 /*jslint bitwise: true, regexp: true, confusion: true, es5: true, vars: true, white: true,
   179   plusplus: true */
   180 
   181 /*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */
   182 
   183 var saveAs = saveAs
   184   || (typeof navigator !== 'undefined' && navigator.msSaveOrOpenBlob && navigator.msSaveOrOpenBlob.bind(navigator))
   185   || (function(view) {
   186 	"use strict";
   187 	var
   188 		  doc = view.document
   189 		  // only get URL when necessary in case BlobBuilder.js hasn't overridden it yet
   190 		, get_URL = function() {
   191 			return view.URL || view.webkitURL || view;
   192 		}
   193 		, URL = view.URL || view.webkitURL || view
   194 		, save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a")
   195 		, can_use_save_link =  !view.externalHost && "download" in save_link
   196 		, click = function(node) {
   197 			var event = doc.createEvent("MouseEvents");
   198 			event.initMouseEvent(
   199 				"click", true, false, view, 0, 0, 0, 0, 0
   200 				, false, false, false, false, 0, null
   201 			);
   202 			node.dispatchEvent(event);
   203 		}
   204 		, webkit_req_fs = view.webkitRequestFileSystem
   205 		, req_fs = view.requestFileSystem || webkit_req_fs || view.mozRequestFileSystem
   206 		, throw_outside = function (ex) {
   207 			(view.setImmediate || view.setTimeout)(function() {
   208 				throw ex;
   209 			}, 0);
   210 		}
   211 		, force_saveable_type = "application/octet-stream"
   212 		, fs_min_size = 0
   213 		, deletion_queue = []
   214 		, process_deletion_queue = function() {
   215 			var i = deletion_queue.length;
   216 			while (i--) {
   217 				var file = deletion_queue[i];
   218 				if (typeof file === "string") { // file is an object URL
   219 					URL.revokeObjectURL(file);
   220 				} else { // file is a File
   221 					file.remove();
   222 				}
   223 			}
   224 			deletion_queue.length = 0; // clear queue
   225 		}
   226 		, dispatch = function(filesaver, event_types, event) {
   227 			event_types = [].concat(event_types);
   228 			var i = event_types.length;
   229 			while (i--) {
   230 				var listener = filesaver["on" + event_types[i]];
   231 				if (typeof listener === "function") {
   232 					try {
   233 						listener.call(filesaver, event || filesaver);
   234 					} catch (ex) {
   235 						throw_outside(ex);
   236 					}
   237 				}
   238 			}
   239 		}
   240 		, FileSaver = function(blob, name) {
   241 			// First try a.download, then web filesystem, then object URLs
   242 			var
   243 				  filesaver = this
   244 				, type = blob.type
   245 				, blob_changed = false
   246 				, object_url
   247 				, target_view
   248 				, get_object_url = function() {
   249 					var object_url = get_URL().createObjectURL(blob);
   250 					deletion_queue.push(object_url);
   251 					return object_url;
   252 				}
   253 				, dispatch_all = function() {
   254 					dispatch(filesaver, "writestart progress write writeend".split(" "));
   255 				}
   256 				// on any filesys errors revert to saving with object URLs
   257 				, fs_error = function() {
   258 					// don't create more object URLs than needed
   259 					if (blob_changed || !object_url) {
   260 						object_url = get_object_url(blob);
   261 					}
   262 					if (target_view) {
   263 						target_view.location.href = object_url;
   264 					} else {
   265                         window.open(object_url, "_blank");
   266                     }
   267 					filesaver.readyState = filesaver.DONE;
   268 					dispatch_all();
   269 				}
   270 				, abortable = function(func) {
   271 					return function() {
   272 						if (filesaver.readyState !== filesaver.DONE) {
   273 							return func.apply(this, arguments);
   274 						}
   275 					};
   276 				}
   277 				, create_if_not_found = {create: true, exclusive: false}
   278 				, slice
   279 			;
   280 			filesaver.readyState = filesaver.INIT;
   281 			if (!name) {
   282 				name = "download";
   283 			}
   284 			if (can_use_save_link) {
   285 				object_url = get_object_url(blob);
   286 				// FF for Android has a nasty garbage collection mechanism
   287 				// that turns all objects that are not pure javascript into 'deadObject'
   288 				// this means `doc` and `save_link` are unusable and need to be recreated
   289 				// `view` is usable though:
   290 				doc = view.document;
   291 				save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a");
   292 				save_link.href = object_url;
   293 				save_link.download = name;
   294 				var event = doc.createEvent("MouseEvents");
   295 				event.initMouseEvent(
   296 					"click", true, false, view, 0, 0, 0, 0, 0
   297 					, false, false, false, false, 0, null
   298 				);
   299 				save_link.dispatchEvent(event);
   300 				filesaver.readyState = filesaver.DONE;
   301 				dispatch_all();
   302 				return;
   303 			}
   304 			// Object and web filesystem URLs have a problem saving in Google Chrome when
   305 			// viewed in a tab, so I force save with application/octet-stream
   306 			// http://code.google.com/p/chromium/issues/detail?id=91158
   307 			if (view.chrome && type && type !== force_saveable_type) {
   308 				slice = blob.slice || blob.webkitSlice;
   309 				blob = slice.call(blob, 0, blob.size, force_saveable_type);
   310 				blob_changed = true;
   311 			}
   312 			// Since I can't be sure that the guessed media type will trigger a download
   313 			// in WebKit, I append .download to the filename.
   314 			// https://bugs.webkit.org/show_bug.cgi?id=65440
   315 			if (webkit_req_fs && name !== "download") {
   316 				name += ".download";
   317 			}
   318 			if (type === force_saveable_type || webkit_req_fs) {
   319 				target_view = view;
   320 			}
   321 			if (!req_fs) {
   322 				fs_error();
   323 				return;
   324 			}
   325 			fs_min_size += blob.size;
   326 			req_fs(view.TEMPORARY, fs_min_size, abortable(function(fs) {
   327 				fs.root.getDirectory("saved", create_if_not_found, abortable(function(dir) {
   328 					var save = function() {
   329 						dir.getFile(name, create_if_not_found, abortable(function(file) {
   330 							file.createWriter(abortable(function(writer) {
   331 								writer.onwriteend = function(event) {
   332 									target_view.location.href = file.toURL();
   333 									deletion_queue.push(file);
   334 									filesaver.readyState = filesaver.DONE;
   335 									dispatch(filesaver, "writeend", event);
   336 								};
   337 								writer.onerror = function() {
   338 									var error = writer.error;
   339 									if (error.code !== error.ABORT_ERR) {
   340 										fs_error();
   341 									}
   342 								};
   343 								"writestart progress write abort".split(" ").forEach(function(event) {
   344 									writer["on" + event] = filesaver["on" + event];
   345 								});
   346 								writer.write(blob);
   347 								filesaver.abort = function() {
   348 									writer.abort();
   349 									filesaver.readyState = filesaver.DONE;
   350 								};
   351 								filesaver.readyState = filesaver.WRITING;
   352 							}), fs_error);
   353 						}), fs_error);
   354 					};
   355 					dir.getFile(name, {create: false}, abortable(function(file) {
   356 						// delete file if it already exists
   357 						file.remove();
   358 						save();
   359 					}), abortable(function(ex) {
   360 						if (ex.code === ex.NOT_FOUND_ERR) {
   361 							save();
   362 						} else {
   363 							fs_error();
   364 						}
   365 					}));
   366 				}), fs_error);
   367 			}), fs_error);
   368 		}
   369 		, FS_proto = FileSaver.prototype
   370 		, saveAs = function(blob, name) {
   371 			return new FileSaver(blob, name);
   372 		}
   373 	;
   374 	FS_proto.abort = function() {
   375 		var filesaver = this;
   376 		filesaver.readyState = filesaver.DONE;
   377 		dispatch(filesaver, "abort");
   378 	};
   379 	FS_proto.readyState = FS_proto.INIT = 0;
   380 	FS_proto.WRITING = 1;
   381 	FS_proto.DONE = 2;
   382 
   383 	FS_proto.error =
   384 	FS_proto.onwritestart =
   385 	FS_proto.onprogress =
   386 	FS_proto.onwrite =
   387 	FS_proto.onabort =
   388 	FS_proto.onerror =
   389 	FS_proto.onwriteend =
   390 		null;
   391 
   392 	view.addEventListener("unload", process_deletion_queue, false);
   393 	return saveAs;
   394 }(this.self || this.window || this.content));
   395 // `self` is undefined in Firefox for Android content script context
   396 // while `this` is nsIContentFrameMessageManager
   397 // with an attribute `content` that corresponds to the window
   398 
   399 if (typeof module !== 'undefined') module.exports = saveAs;
Impressum Datenschutzerklärung