Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / app / policyApp / controller / FileSaver.js
1 var saveAs = saveAs || (function(view) {
2         "use strict";
3         // IE <10 is explicitly unsupported
4         if (typeof view === "undefined" || typeof navigator !== "undefined" && /MSIE [1-9]\./.test(navigator.userAgent)) {
5             return;
6         }
7         var
8             doc = view.document
9         // only get URL when necessary in case Blob.js hasn't overridden it yet
10             , get_URL = function() {
11                 return view.URL || view.webkitURL || view;
12             }
13             , save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a")
14             , can_use_save_link = "download" in save_link
15             , click = function(node) {
16                 var event = new MouseEvent("click");
17                 node.dispatchEvent(event);
18             }
19             , is_safari = /constructor/i.test(view.HTMLElement)
20             , throw_outside = function(ex) {
21                 (view.setImmediate || view.setTimeout)(function() {
22                     throw ex;
23                 }, 0);
24             }
25             , force_saveable_type = "application/octet-stream"
26         // the Blob API is fundamentally broken as there is no "downloadfinished" event to subscribe to
27             , arbitrary_revoke_timeout = 1000 * 40 // in ms
28             , revoke = function(file) {
29                 var revoker = function() {
30                     if (typeof file === "string") { // file is an object URL
31                         get_URL().revokeObjectURL(file);
32                     } else { // file is a File
33                         file.remove();
34                     }
35                 };
36                 setTimeout(revoker, arbitrary_revoke_timeout);
37             }
38             , dispatch = function(filesaver, event_types, event) {
39                 event_types = [].concat(event_types);
40                 var i = event_types.length;
41                 while (i--) {
42                     var listener = filesaver["on" + event_types[i]];
43                     if (typeof listener === "function") {
44                         try {
45                             listener.call(filesaver, event || filesaver);
46                         } catch (ex) {
47                             throw_outside(ex);
48                         }
49                     }
50                 }
51             }
52             , auto_bom = function(blob) {
53                 // prepend BOM for UTF-8 XML and text/* types (including HTML)
54                 // note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
55                 if (/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
56                     return new Blob([String.fromCharCode(0xFEFF), blob], {type: blob.type});
57                 }
58                 return blob;
59             }
60             , FileSaver = function(blob, name, no_auto_bom) {
61                 if (!no_auto_bom) {
62                     blob = auto_bom(blob);
63                 }
64                 // First try a.download, then web filesystem, then object URLs
65                 var
66                     filesaver = this
67                     , type = blob.type
68                     , force = type === force_saveable_type
69                     , object_url
70                     , dispatch_all = function() {
71                         dispatch(filesaver, "writestart progress write writeend".split(" "));
72                     }
73                 // on any filesys errors revert to saving with object URLs
74                     , fs_error = function() {
75                         if (force && is_safari && view.FileReader) {
76                             // Safari doesn't allow downloading of blob urls
77                             var reader = new FileReader();
78                             reader.onloadend = function() {
79                                 var base64Data = reader.result;
80                                 view.location.href = "data:attachment/file" + base64Data.slice(base64Data.search(/[,;]/));
81                                 filesaver.readyState = filesaver.DONE;
82                                 dispatch_all();
83                             };
84                             reader.readAsDataURL(blob);
85                             filesaver.readyState = filesaver.INIT;
86                             return;
87                         }
88                         // don't create more object URLs than needed
89                         if (!object_url) {
90                             object_url = get_URL().createObjectURL(blob);
91                         }
92                         if (force) {
93                             view.location.href = object_url;
94                         } else {
95                             var opened = view.open(object_url, "_blank");
96                             if (!opened) {
97                                 // Apple does not allow window.open, see https://developer.apple.com/library/safari/documentation/Tools/Conceptual/SafariExtensionGuide/WorkingwithWindowsandTabs/WorkingwithWindowsandTabs.html
98                                 view.location.href = object_url;
99                             }
100                         }
101                         filesaver.readyState = filesaver.DONE;
102                         dispatch_all();
103                         revoke(object_url);
104                     }
105                     ;
106                 filesaver.readyState = filesaver.INIT;
107
108                 if (can_use_save_link) {
109                     object_url = get_URL().createObjectURL(blob);
110                     setTimeout(function() {
111                         save_link.href = object_url;
112                         save_link.download = name;
113                         click(save_link);
114                         dispatch_all();
115                         revoke(object_url);
116                         filesaver.readyState = filesaver.DONE;
117                     });
118                     return;
119                 }
120
121                 fs_error();
122             }
123             , FS_proto = FileSaver.prototype
124             , saveAs = function(blob, name, no_auto_bom) {
125                 return new FileSaver(blob, name || blob.name || "download", no_auto_bom);
126             }
127             ;
128         // IE 10+ (native saveAs)
129         if (typeof navigator !== "undefined" && navigator.msSaveOrOpenBlob) {
130             return function(blob, name, no_auto_bom) {
131                 name = name || blob.name || "download";
132
133                 if (!no_auto_bom) {
134                     blob = auto_bom(blob);
135                 }
136                 return navigator.msSaveOrOpenBlob(blob, name);
137             };
138         }
139
140         FS_proto.abort = function(){};
141         FS_proto.readyState = FS_proto.INIT = 0;
142         FS_proto.WRITING = 1;
143         FS_proto.DONE = 2;
144
145         FS_proto.error =
146             FS_proto.onwritestart =
147                 FS_proto.onprogress =
148                     FS_proto.onwrite =
149                         FS_proto.onabort =
150                             FS_proto.onerror =
151                                 FS_proto.onwriteend =
152                                     null;
153
154         return saveAs;
155     }(
156         typeof self !== "undefined" && self
157         || typeof window !== "undefined" && window
158         || this.content
159     ));
160 // `self` is undefined in Firefox for Android content script context
161 // while `this` is nsIContentFrameMessageManager
162 // with an attribute `content` that corresponds to the window
163
164 if (typeof module !== "undefined" && module.exports) {
165     module.exports.saveAs = saveAs;
166 } else if ((typeof define !== "undefined" && define !== null) && (define.amd !== null)) {
167     define([], function() {
168         return saveAs;
169     });
170 }