43c87be6ffbc0c557d4deb3db4e1d1ec7d39ab6e
[redakcja.git] / redakcja / static / filebrowser / uploadify / jquery.uploadify.v2.1.0.js
1 /*
2 Uploadify v2.1.0
3 Release Date: August 24, 2009
4
5 Copyright (c) 2009 Ronnie Garcia, Travis Nickels
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24 */
25
26 if(jQuery)(
27         function(jQuery){
28                 jQuery.extend(jQuery.fn,{
29                         uploadify:function(options) {
30                                 jQuery(this).each(function(){
31                                         settings = jQuery.extend({
32                                         id             : jQuery(this).attr('id'), // The ID of the object being Uploadified
33                                         uploader       : 'uploadify.swf', // The path to the uploadify swf file
34                                         script         : 'uploadify.php', // The path to the uploadify backend upload script
35                                         expressInstall : null, // The path to the express install swf file
36                                         folder         : '', // The path to the upload folder
37                                         height         : 30, // The height of the flash button
38                                         width          : 110, // The width of the flash button
39                                         cancelImg      : 'cancel.png', // The path to the cancel image for the default file queue item container
40                                         wmode          : 'opaque', // The wmode of the flash file
41                                         scriptAccess   : 'sameDomain', // Set to "always" to allow script access across domains
42                                         fileDataName   : 'Filedata', // The name of the file collection object in the backend upload script
43                                         method         : 'POST', // The method for sending variables to the backend upload script
44                                         queueSizeLimit : 999, // The maximum size of the file queue
45                                         simUploadLimit : 1, // The number of simultaneous uploads allowed
46                                         queueID        : false, // The optional ID of the queue container
47                                         displayData    : 'percentage', // Set to "speed" to show the upload speed in the default queue item
48                                         onInit         : function() {}, // Function to run when uploadify is initialized
49                                         onSelect       : function() {}, // Function to run when a file is selected
50                                         onQueueFull    : function() {}, // Function to run when the queue reaches capacity
51                                         onCheck        : function() {}, // Function to run when script checks for duplicate files on the server
52                                         onCancel       : function() {}, // Function to run when an item is cleared from the queue
53                                         onError        : function() {}, // Function to run when an upload item returns an error
54                                         onProgress     : function() {}, // Function to run each time the upload progress is updated
55                                         onComplete     : function() {}, // Function to run when an upload is completed
56                                         onAllComplete  : function() {}  // Functino to run when all uploads are completed
57                                 }, options);
58                                 var pagePath = location.pathname;
59                                 pagePath = pagePath.split('/');
60                                 pagePath.pop();
61                                 pagePath = pagePath.join('/') + '/';
62                                 var data = {};
63                                 data.uploadifyID = settings.id;
64                                 data.pagepath = pagePath;
65                                 if (settings.buttonImg) data.buttonImg = escape(settings.buttonImg);
66                                 if (settings.buttonText) data.buttonText = escape(settings.buttonText);
67                                 if (settings.rollover) data.rollover = true;
68                                 data.script = settings.script;
69                                 data.folder = escape(settings.folder);
70                                 if (settings.scriptData) {
71                                         var scriptDataString = '';
72                                         for (var name in settings.scriptData) {
73                                                 scriptDataString += '&' + name + '=' + settings.scriptData[name];
74                                         }
75                                         data.scriptData = escape(scriptDataString.substr(1));
76                                 }
77                                 data.width          = settings.width;
78                                 data.height         = settings.height;
79                                 data.wmode          = settings.wmode;
80                                 data.method         = settings.method;
81                                 data.queueSizeLimit = settings.queueSizeLimit;
82                                 data.simUploadLimit = settings.simUploadLimit;
83                                 if (settings.hideButton)   data.hideButton   = true;
84                                 if (settings.fileDesc)     data.fileDesc     = settings.fileDesc;
85                                 if (settings.fileExt)      data.fileExt      = settings.fileExt;
86                                 if (settings.multi)        data.multi        = true;
87                                 if (settings.auto)         data.auto         = true;
88                                 if (settings.sizeLimit)    data.sizeLimit    = settings.sizeLimit;
89                                 if (settings.checkScript)  data.checkScript  = settings.checkScript;
90                                 if (settings.fileDataName) data.fileDataName = settings.fileDataName;
91                                 if (settings.queueID)      data.queueID      = settings.queueID;
92                                 if (settings.onInit() !== false) {
93                                         jQuery(this).css('display','none');
94                                         jQuery(this).after('<div id="' + jQuery(this).attr('id') + 'Uploader"></div>');
95                                         swfobject.embedSWF(settings.uploader, settings.id + 'Uploader', settings.width, settings.height, '9.0.24', settings.expressInstall, data, {'quality':'high','wmode':settings.wmode,'allowScriptAccess':settings.scriptAccess});
96                                         if (settings.queueID == false) {
97                                                 jQuery("#" + jQuery(this).attr('id') + "Uploader").after('<div id="' + jQuery(this).attr('id') + 'Queue" class="uploadifyQueue"></div>');
98                                         }
99                                 }
100                                 if (typeof(settings.onOpen) == 'function') {
101                                         jQuery(this).bind("uploadifyOpen", settings.onOpen);
102                                 }
103                                 jQuery(this).bind("uploadifySelect", {'action': settings.onSelect, 'queueID': settings.queueID}, function(event, ID, fileObj) {
104                                         if (event.data.action(event, ID, fileObj) !== false) {
105                                                 var byteSize = Math.round(fileObj.size / 1024 * 100) * .01;
106                                                 var suffix = 'KB';
107                                                 if (byteSize > 1000) {
108                                                         byteSize = Math.round(byteSize *.001 * 100) * .01;
109                                                         suffix = 'MB';
110                                                 }
111                                                 var sizeParts = byteSize.toString().split('.');
112                                                 if (sizeParts.length > 1) {
113                                                         byteSize = sizeParts[0] + '.' + sizeParts[1].substr(0,2);
114                                                 } else {
115                                                         byteSize = sizeParts[0];
116                                                 }
117                                                 if (fileObj.name.length > 20) {
118                                                         fileName = fileObj.name.substr(0,20) + '...';
119                                                 } else {
120                                                         fileName = fileObj.name;
121                                                 }
122                                                 queue = '#' + jQuery(this).attr('id') + 'Queue';
123                                                 if (event.data.queueID) {
124                                                         queue = '#' + event.data.queueID;
125                                                 }
126                                                 jQuery(queue).append('<div id="' + jQuery(this).attr('id') + ID + '" class="uploadifyQueueItem">\
127                                                                 <div class="cancel">\
128                                                                         <a href="javascript:jQuery(\'#' + jQuery(this).attr('id') + '\').uploadifyCancel(\'' + ID + '\')"><img src="' + settings.cancelImg + '" border="0" /></a>\
129                                                                 </div>\
130                                                                 <span class="fileName">' + fileName + ' (' + byteSize + suffix + ')</span><span class="percentage"></span>\
131                                                                 <div class="uploadifyProgress">\
132                                                                         <div id="' + jQuery(this).attr('id') + ID + 'ProgressBar" class="uploadifyProgressBar"><!--Progress Bar--></div>\
133                                                                 </div>\
134                                                         </div>');
135                                         }
136                                 });
137                                 if (typeof(settings.onSelectOnce) == 'function') {
138                                         jQuery(this).bind("uploadifySelectOnce", settings.onSelectOnce);
139                                 }
140                                 jQuery(this).bind("uploadifyQueueFull", {'action': settings.onQueueFull}, function(event, queueSizeLimit) {
141                                         if (event.data.action(event, queueSizeLimit) !== false) {
142                                                 alert('The queue is full.  The max size is ' + queueSizeLimit + '.');
143                                         }
144                                 });
145                                 jQuery(this).bind("uploadifyCheckExist", {'action': settings.onCheck}, function(event, checkScript, fileQueueObj, folder, single) {
146                                         var postData = new Object();
147                                         postData = fileQueueObj;
148                                         postData.folder = pagePath + folder;
149                                         if (single) {
150                                                 for (var ID in fileQueueObj) {
151                                                         var singleFileID = ID;
152                                                 }
153                                         }
154                                         jQuery.post(checkScript, postData, function(data) {
155                                                 for(var key in data) {
156                                                         if (event.data.action(event, checkScript, fileQueueObj, folder, single) !== false) {
157                                                                 var replaceFile = confirm("Do you want to replace the file " + data[key] + "?");
158                                                                 if (!replaceFile) {
159                                                                         document.getElementById(jQuery(event.target).attr('id') + 'Uploader').cancelFileUpload(key, true,true);
160                                                                 }
161                                                         }
162                                                 }
163                                                 if (single) {
164                                                         document.getElementById(jQuery(event.target).attr('id') + 'Uploader').startFileUpload(singleFileID, true);
165                                                 } else {
166                                                         document.getElementById(jQuery(event.target).attr('id') + 'Uploader').startFileUpload(null, true);
167                                                 }
168                                         }, "json");
169                                 });
170                                 jQuery(this).bind("uploadifyCancel", {'action': settings.onCancel}, function(event, ID, fileObj, data, clearFast) {
171                                         if (event.data.action(event, ID, fileObj, data, clearFast) !== false) {
172                                                 var fadeSpeed = (clearFast == true) ? 0 : 250;
173                                                 jQuery("#" + jQuery(this).attr('id') + ID).fadeOut(fadeSpeed, function() { jQuery(this).remove() });
174                                         }
175                                 });
176                                 if (typeof(settings.onClearQueue) == 'function') {
177                                         jQuery(this).bind("uploadifyClearQueue", settings.onClearQueue);
178                                 }
179                                 var errorArray = [];
180                                 jQuery(this).bind("uploadifyError", {'action': settings.onError}, function(event, ID, fileObj, errorObj) {
181                                         if (event.data.action(event, ID, fileObj, errorObj) !== false) {
182                                                 var fileArray = new Array(ID, fileObj, errorObj);
183                                                 errorArray.push(fileArray);
184                                                 jQuery("#" + jQuery(this).attr('id') + ID + " .percentage").text(" - " + errorObj.type + " Error");
185                                                 jQuery("#" + jQuery(this).attr('id') + ID).addClass('uploadifyError');
186                                         }
187                                 });
188                                 jQuery(this).bind("uploadifyProgress", {'action': settings.onProgress, 'toDisplay': settings.displayData}, function(event, ID, fileObj, data) {
189                                         if (event.data.action(event, ID, fileObj, data) !== false) {
190                                                 jQuery("#" + jQuery(this).attr('id') + ID + "ProgressBar").css('width', data.percentage + '%');
191                                                 if (event.data.toDisplay == 'percentage') displayData = ' - ' + data.percentage + '%';
192                                                 if (event.data.toDisplay == 'speed') displayData = ' - ' + data.speed + 'KB/s';
193                                                 if (event.data.toDisplay == null) displayData = ' ';
194                                                 jQuery("#" + jQuery(this).attr('id') + ID + " .percentage").text(displayData);
195                                         }
196                                 });
197                                 jQuery(this).bind("uploadifyComplete", {'action': settings.onComplete}, function(event, ID, fileObj, response, data) {
198                                         if (event.data.action(event, ID, fileObj, unescape(response), data) !== false) {
199                                                 jQuery("#" + jQuery(this).attr('id') + ID + " .percentage").text(' - Completed');
200                                                 jQuery("#" + jQuery(this).attr('id') + ID).fadeOut(250, function() { jQuery(this).remove()});
201                                         }
202                                 });
203                                 if (typeof(settings.onAllComplete) == 'function') {
204                                         jQuery(this).bind("uploadifyAllComplete", {'action': settings.onAllComplete}, function(event, uploadObj) {
205                                                 if (event.data.action(event, uploadObj) !== false) {
206                                                         errorArray = [];
207                                                 }
208                                         });
209                                 }
210                         });
211                 },
212                 uploadifySettings:function(settingName, settingValue, resetObject) {
213                         var returnValue = false;
214                         jQuery(this).each(function() {
215                                 if (settingName == 'scriptData' && settingValue != null) {
216                                         if (resetObject) {
217                                                 var scriptData = settingValue;
218                                         } else {
219                                                 var scriptData = jQuery.extend(settings.scriptData, settingValue);
220                                         }
221                                         var scriptDataString = '';
222                                         for (var name in scriptData) {
223                                                 scriptDataString += '&' + name + '=' + escape(scriptData[name]);
224                                         }
225                                         settingValue = scriptDataString.substr(1);
226                                 }
227                                 returnValue = document.getElementById(jQuery(this).attr('id') + 'Uploader').updateSettings(settingName, settingValue);
228                         });
229                         if (settingValue == null) {
230                                 if (settingName == 'scriptData') {
231                                         var returnSplit = unescape(returnValue).split('&');
232                                         var returnObj   = new Object();
233                                         for (var i = 0; i < returnSplit.length; i++) {
234                                                 var iSplit = returnSplit[i].split('=');
235                                                 returnObj[iSplit[0]] = iSplit[1];
236                                         }
237                                         returnValue = returnObj;
238                                 }
239                                 return returnValue;
240                         }
241                 },
242                 uploadifyUpload:function(ID) {
243                         jQuery(this).each(function() {
244                                 document.getElementById(jQuery(this).attr('id') + 'Uploader').startFileUpload(ID, false);
245                         });
246                 },
247                 uploadifyCancel:function(ID) {
248                         jQuery(this).each(function() {
249                                 document.getElementById(jQuery(this).attr('id') + 'Uploader').cancelFileUpload(ID, true, false);
250                         });
251                 },
252                 uploadifyClearQueue:function() {
253                         jQuery(this).each(function() {
254                                 document.getElementById(jQuery(this).attr('id') + 'Uploader').clearFileUploadQueue(false);
255                         });
256                 }
257         })
258 })(jQuery);