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