You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
7.9 KiB

2 years ago
  1. jQuery.extend({
  2. handleError: function (s, xhr, status, e) {
  3. // If a local callback was specified, fire it
  4. if (s.error) {
  5. s.error.call(s.context || s, xhr, status, e);
  6. }
  7. // Fire the global callback
  8. if (s.global) {
  9. (s.context ? jQuery(s.context) : jQuery.event).trigger("ajaxError", [xhr, s, e]);
  10. }
  11. },
  12. createUploadIframe: function (id, uri) {
  13. //create frame
  14. var frameId = 'jUploadFrame' + id;
  15. if (window.ActiveXObject) {
  16. var io = document.createElement('<iframe id="' + frameId + '" name="' + frameId + '" />');
  17. if (typeof uri == 'boolean') {
  18. io.src = 'javascript:false';
  19. }
  20. else if (typeof uri == 'string') {
  21. io.src = uri;
  22. }
  23. }
  24. else {
  25. var io = document.createElement('iframe');
  26. io.id = frameId;
  27. io.name = frameId;
  28. }
  29. io.style.position = 'absolute';
  30. io.style.top = '-1000px';
  31. io.style.left = '-1000px';
  32. document.body.appendChild(io);
  33. return io
  34. },
  35. createUploadForm: function (id, fileElementId) {
  36. //create form
  37. var formId = 'jUploadForm' + id;
  38. var fileId = 'jUploadFile' + id;
  39. var form = $('<form action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>');
  40. var oldElement = $('#' + fileElementId);
  41. var newElement = $(oldElement).clone();
  42. $(oldElement).attr('id', fileId);
  43. $(oldElement).before(newElement);
  44. $(oldElement).appendTo(form);
  45. //set attributes
  46. $(form).css('position', 'absolute');
  47. $(form).css('top', '-1200px');
  48. $(form).css('left', '-1200px');
  49. $(form).appendTo('body');
  50. return form;
  51. },
  52. addOtherRequestsToForm: function (form, data) {
  53. // add extra parameter
  54. var originalElement = $('<input type="hidden" name="" value="">');
  55. for (var key in data) {
  56. name = key;
  57. value = data[key];
  58. var cloneElement = originalElement.clone();
  59. cloneElement.attr({ 'name': name, 'value': value });
  60. $(cloneElement).appendTo(form);
  61. }
  62. return form;
  63. },
  64. ajaxFileUpload: function (s) {
  65. // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
  66. s = jQuery.extend({}, jQuery.ajaxSettings, s);
  67. var id = new Date().getTime()
  68. var form = jQuery.createUploadForm(id, s.fileElementId);
  69. if (s.data) form = jQuery.addOtherRequestsToForm(form, s.data);
  70. var io = jQuery.createUploadIframe(id, s.secureuri);
  71. var frameId = 'jUploadFrame' + id;
  72. var formId = 'jUploadForm' + id;
  73. // Watch for a new set of requests
  74. if (s.global && !jQuery.active++) {
  75. jQuery.event.trigger("ajaxStart");
  76. }
  77. var requestDone = false;
  78. // Create the request object
  79. var xml = {}
  80. if (s.global)
  81. jQuery.event.trigger("ajaxSend", [xml, s]);
  82. // Wait for a response to come back
  83. var uploadCallback = function (isTimeout) {
  84. var io = document.getElementById(frameId);
  85. try {
  86. if (io.contentWindow) {
  87. xml.responseText = io.contentWindow.document.body ? io.contentWindow.document.body.innerHTML : null;
  88. xml.responseXML = io.contentWindow.document.XMLDocument ? io.contentWindow.document.XMLDocument : io.contentWindow.document;
  89. } else if (io.contentDocument) {
  90. xml.responseText = io.contentDocument.document.body ? io.contentDocument.document.body.innerHTML : null;
  91. xml.responseXML = io.contentDocument.document.XMLDocument ? io.contentDocument.document.XMLDocument : io.contentDocument.document;
  92. }
  93. } catch (e) {
  94. jQuery.handleError(s, xml, null, e);
  95. }
  96. if (xml || isTimeout == "timeout") {
  97. requestDone = true;
  98. var status;
  99. try {
  100. status = isTimeout != "timeout" ? "success" : "error";
  101. // Make sure that the request was successful or notmodified
  102. if (status != "error") {
  103. // process the data (runs the xml through httpData regardless of callback)
  104. var data = jQuery.uploadHttpData(xml, s.dataType);
  105. // If a local callback was specified, fire it and pass it the data
  106. if (s.success)
  107. s.success(data, status, xml);
  108. // Fire the global callback
  109. if (s.global)
  110. jQuery.event.trigger("ajaxSuccess", [xml, s]);
  111. } else
  112. jQuery.handleError(s, xml, status);
  113. } catch (e) {
  114. status = "error";
  115. jQuery.handleError(s, xml, status, e);
  116. }
  117. // The request was completed
  118. if (s.global)
  119. jQuery.event.trigger("ajaxComplete", [xml, s]);
  120. // Handle the global AJAX counter
  121. if (s.global && ! --jQuery.active)
  122. jQuery.event.trigger("ajaxStop");
  123. // Process result
  124. if (s.complete)
  125. s.complete(xml, status);
  126. jQuery(io).unbind()
  127. setTimeout(function () {
  128. try {
  129. $(io).remove();
  130. $(form).remove();
  131. } catch (e) {
  132. jQuery.handleError(s, xml, null, e);
  133. }
  134. }, 100)
  135. xml = null
  136. }
  137. }
  138. // Timeout checker
  139. if (s.timeout > 0) {
  140. setTimeout(function () {
  141. // Check to see if the request is still happening
  142. if (!requestDone) uploadCallback("timeout");
  143. }, s.timeout);
  144. }
  145. try {
  146. // var io = $('#' + frameId);
  147. var form = $('#' + formId);
  148. $(form).attr('action', s.url);
  149. $(form).attr('method', 'POST');
  150. $(form).attr('target', frameId);
  151. if (form.encoding) {
  152. form.encoding = 'multipart/form-data';
  153. }
  154. else {
  155. form.enctype = 'multipart/form-data';
  156. }
  157. $(form).submit();
  158. } catch (e) {
  159. jQuery.handleError(s, xml, null, e);
  160. }
  161. if (window.attachEvent) {
  162. document.getElementById(frameId).attachEvent('onload', uploadCallback);
  163. }
  164. else {
  165. document.getElementById(frameId).addEventListener('load', uploadCallback, false);
  166. }
  167. return { abort: function () { } };
  168. },
  169. uploadHttpData: function (r, type) {
  170. var data = !type;
  171. data = type == "xml" || data ? r.responseXML : r.responseText;
  172. // If the type is "script", eval it in global context
  173. if (type == "script")
  174. jQuery.globalEval(data);
  175. // Get the JavaScript object, if JSON is used.
  176. if (type == "json") {
  177. // If you add mimetype in your response,
  178. // you have to delete the '<pre></pre>' tag.
  179. // The pre tag in Chrome has attribute, so have to use regex to remove
  180. var data = r.responseText;
  181. var rx = new RegExp("<pre.*?>(.*?)</pre>", "i");
  182. var am = rx.exec(data);
  183. //this is the desired data extracted
  184. var data = (am) ? am[1] : ""; //the only submatch or empty
  185. eval("data = " + data);
  186. }
  187. // evaluate scripts within html
  188. if (type == "html")
  189. jQuery("<div>").html(data).evalScripts();
  190. //alert($('param', data).each(function(){alert($(this).attr('value'));}));
  191. return data;
  192. }
  193. })