PageRenderTime 64ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/cms/mod_admin/imanager/upload/image.js

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