-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlashFileDownloader.js
221 lines (173 loc) · 6.88 KB
/
FlashFileDownloader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* FileDownloader
*/
var FileDownloader = {};
FileDownloader.misc = {};
FileDownloader.flashPath = 'flash-file-downloader/FlashFileDownloader.swf';
FileDownloader.flashContainer = 'flashContainer';
FileDownloader.lastResult = undefined;
FileDownloader.networkReady = true;
FileDownloader.JSReady = false;
FileDownloader.ready = false;
FileDownloader.download = function(url, callback) {
if (! FileDownloader.ready || ! FileDownloader.misc.getMovie("FlashFileDownloader").download) {
return setTimeout(function() { FileDownloader.download(url, callback); }, 500);
}
FileDownloader.ready = false;
if (! FileDownloader.misc.isFlashAvailable()) {
FileDownloader.misc.log("Sorry, we need Flash to proceed");
}
FileDownloader._url = url;
FileDownloader._download(FileDownloader._url, callback);
}
/******************************************************************************/
// Misc. utility functions
FileDownloader_isJavaScriptReady = function() {
return FileDownloader.JSReady;
}
FileDownloader.misc.quietEmbedSWF = function(path, containerId) {
var swfName = FileDownloader.misc.fileBasename(path);
FileDownloader.misc.addInvisibleElement(swfName, containerId);
var containerEl = document.getElementById(containerId);
var width = parseInt(getComputedStyle(containerEl).width);
var height = parseInt(getComputedStyle(containerEl).height);
var params = { allowScriptAccess : "always", name : swfName, wmode : "transparent" };
var flashvars = false;
var attributes = { name : swfName};
swfobject.embedSWF(path, swfName, "" + width, "" + height, "10.0.0",
"expressInstall.swf", flashvars, params, attributes);
}
FileDownloader.misc.addInvisibleElement = function(id, containerEl) {
var container = document.getElementById(containerEl) || containerEl || document.body;
if (!container) {
return setTimeout(function() { FileDownloader.misc.addInvisibleElement(id, containerEl); }, 500);
}
if (document.getElementById(id)) {
return false;
}
var elementType = 'div';
var innerContainer = document.getElementById('file_downloader_container');
if (!innerContainer) {
innerContainer = document.createElement('div');
innerContainer.id = 'file_downloader_container';
innerContainer.style.display = 'none';
container.appendChild(innerContainer);
}
// var innerContainer = document.getElementById('file_downloader_container');
var el = document.createElement(elementType);
el.id = id;
el.style.display = 'none';
container.appendChild(el);
return true;
}
FileDownloader.misc.fileBasename = function(path) {
var s = path.replace(/.*\//, '').replace(/.swf/i, '');
return s;
}
FileDownloader.misc.getMovie = function(movieName) {
if (navigator.appName.indexOf("Microsoft") != -1) {
return window[movieName];
} else {
return document[movieName];
}
}
FileDownloader.misc.handleResult = function(resultObj) {
FileDownloader.lastResult = resultObj;
// Clear queue of other methods if
if (!resultObj.error()) {
FileDownloader.ready = true;
// Execute user function on object here
if (FileDownloader._handler) {
FileDownloader._handler(resultObj);
}
} else {
// nothing worked, return last result
FileDownloader.ready = true;
// Execute user function on object here
if (FileDownloader._handler) {
FileDownloader._handler(resultObj);
}
}
}
// This function gets called from ActionScript after the download is finished
FileDownloader.misc.sendToJavaScript = function(params) {
FileDownloader.networkReady = true;
result = new FileDownloader.result(params);
FileDownloader.misc.handleResult(result);
}
FileDownloader.misc.sendTimestampToFlash = function () {
var timestamp = new Date().getTime() + "";
FileDownloader.misc.getMovie("FlashFileDownloader").uploadTimestamp(timestamp);
}
FileDownloader.misc.getTime = function() {
return (new Date()).getTime();
}
FileDownloader.misc.sendToActionScript = function(command) {
if (!FileDownloader.networkReady) {
return setTimeout(function() { FileDownloader.misc.sendToActionScript(command)}, 500);
}
FileDownloader.networkReady = false;
FileDownloader.misc.getMovie("FlashFileDownloader").sendToActionScript(command);
}
FileDownloader.misc.isFlashAvailable = function() {
if (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"])
return true;
else if (FileDownloader.isFlashReady())
return true;
else if (window.ActiveXObject) {
for (x = 7; x <= 11; x++) {
try {
oFlash = eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash." + x + "');");
if (oFlash) {
return true;
}
}
catch(e) { }
}
}
else
return false;
}
FileDownloader.misc.addOnloadEvent = function(newEvent) {
var originalEvent = window.onload;
if (typeof window.onload != 'function') {
window.onload = newEvent;
} else {
window.onload = function() {
if (originalEvent) { originalEvent(); }
newEvent();
}
}
}
FileDownloader.misc.onloadActions = function () {
FileDownloader.networkReady = true;
FileDownloader.JSReady = true;
FileDownloader.ready = true;
FileDownloader.misc.quietEmbedSWF(FileDownloader.flashPath, FileDownloader.flashContainer);
}
FileDownloader.misc.log = function() {
if (typeof(console) != "undefined" && console.log)
console.log(arguments);
}
FileDownloader.isFlashReady = function() {
var m = FileDownloader.misc.getMovie("FlashFileDownloader");
return m && m.sendToActionScript;
}
FileDownloader._download = function (url, callback) {
// Callbacks implemented: success, failure, progress
if (!callback) {
callback = {}
}
var defaultHandler = FileDownloader.misc.log;
FileDownloader.progressHandler = callback.progress || defaultHandler;
FileDownloader.successHandler = callback.success || defaultHandler;
FileDownloader.failureHandler = callback.failureHandler || defaultHandler;
console.log(FileDownloader.misc.getMovie("FlashFileDownloader").download);
FileDownloader.misc.getMovie("FlashFileDownloader").download(url);
FileDownloader.misc.getMovie("FlashFileDownloader").addCallback('progress', 'FileDownloader.progressHandler');
FileDownloader.misc.getMovie("FlashFileDownloader").addCallback('success', 'FileDownloader.successHandler');
FileDownloader.misc.getMovie("FlashFileDownloader").addCallback('failure', 'FileDownloader.failureHandler') ;
}
/* Create our container element */
document.write('<div id="file_downloader_container"></div>');
FileDownloader.misc.addOnloadEvent(FileDownloader.misc.onloadActions);