zentao.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. var ajaxFormUrl = '';
  2. $.extend(
  3. {
  4. ajaxForm: function(formID, callback)
  5. {
  6. form = $(formID);
  7. var options =
  8. {
  9. target : null,
  10. timeout : config.timeout,
  11. dataType:'json',
  12. beforeSubmit:function(arr, $form, options)
  13. {
  14. /* Check should use frame. */
  15. var feature = {};
  16. feature.fileapi = $("<input type='file'/>").get(0).files !== undefined;
  17. feature.formdata = window.FormData !== undefined;
  18. var fileInputs = $('input[type=file]:enabled', this).filter(function() { return $(this).val() !== ''; });
  19. var hasFileInputs = fileInputs.length > 0;
  20. var mp = 'multipart/form-data';
  21. var multipart = ($form.attr('enctype') == mp || $form.attr('encoding') == mp);
  22. var fileAPI = feature.fileapi && feature.formdata;
  23. var shouldUseFrame = (hasFileInputs || multipart) && !fileAPI;
  24. /* Append HTTP_X_REQUESTED_WITH on url when shouldUseFrame is true. */
  25. if(shouldUseFrame)
  26. {
  27. if(ajaxFormUrl == '')ajaxFormUrl = options.url;
  28. if(options.url != ajaxFormUrl) options.url = ajaxFormUrl;
  29. options.url = options.url.indexOf('&') >= 0 ? options.url + '&HTTP_X_REQUESTED_WITH=XMLHttpRequest' : options.url + '?HTTP_X_REQUESTED_WITH=XMLHttpRequest';
  30. }
  31. },
  32. success:function(response)
  33. {
  34. $.enableForm(formID);
  35. /* try parse to json when response is json's string. */
  36. try{if(typeof(response) == 'string') response = JSON.parse(response);}catch(e){}
  37. /* The response is not an object, some error occurs, alert it. */
  38. if(typeof(response) != 'object')
  39. {
  40. if(response) return alert(response);
  41. return alert('No response.');
  42. }
  43. /* The response.result is success. */
  44. if(response.result == 'success')
  45. {
  46. if(response.message && response.message.length)
  47. {
  48. submitButton = $(formID).find(':submit');
  49. var placement = response.placement ? response.placement : 'right';
  50. submitButton.popover({trigger:'manual', content:response.message, placement:placement}).popover('show');
  51. submitButton.next('.popover').addClass('popover-success');
  52. function distroy(){submitButton.popover('destroy')}
  53. setTimeout(distroy,2000);
  54. }
  55. if($.isFunction(callback)) return callback(response);
  56. if($('#responser').length && response.message && response.message.length)
  57. {
  58. $('#responser').html(response.message).addClass('red f-12px').show().delay(3000).fadeOut(100);
  59. }
  60. if(response.closeModal) setTimeout($.zui.closeModal, 1200);
  61. if(response.callback)
  62. {
  63. var rcall = window[response.callback];
  64. if($.isFunction(rcall))
  65. {
  66. if(rcall() === false) return;
  67. }
  68. }
  69. if(response.locate)
  70. {
  71. if(response.locate == 'loadInModal')
  72. {
  73. var modal = $('.modal');
  74. setTimeout(function()
  75. {
  76. modal.load(modal.attr('ref'), function(){$(this).find('.modal-dialog').css('width', $(this).data('width')); $.zui.ajustModalPosition()})
  77. }, 1000);
  78. }
  79. else
  80. {
  81. var reloadUrl = response.locate == 'reload' ? location.href : response.locate;
  82. setTimeout(function(){location.href = reloadUrl;}, 1200);
  83. }
  84. }
  85. if(response.ajaxReload)
  86. {
  87. var $target = $(response.ajaxReload);
  88. if($target.length === 1)
  89. {
  90. $target.load(document.location.href + ' ' + response.ajaxReload, function()
  91. {
  92. $target.dataTable();
  93. $target.find('[data-toggle="modal"]').modalTrigger();
  94. });
  95. }
  96. }
  97. return true;
  98. }
  99. /**
  100. * The response.result is fail.
  101. */
  102. /* The result.message is just a string. */
  103. if($.type(response.message) == 'string')
  104. {
  105. if($('#responser').length == 0) return alert(response.message);
  106. return $('#responser').html(response.message).addClass('text-error').show().delay(3000).fadeOut(100);
  107. }
  108. /* The result.message is just a object. */
  109. if($.type(response.message) == 'object')
  110. {
  111. var triggered = new Array();
  112. $.each(response.message, function(key, value)
  113. {
  114. /* Define the id of the error objecjt and it's label. */
  115. var errorOBJ = '#' + key;
  116. var errorLabel = key + 'Label';
  117. var i = triggered.push(false);
  118. /* Create the error message. */
  119. var errorMSG = '<div id="' + errorLabel + '" class="text-danger red">';
  120. errorMSG += $.type(value) == 'string' ? value : value.join(';');
  121. errorMSG += '</div>';
  122. /* Append error message, set style and set the focus events. */
  123. $('#' + errorLabel).remove();
  124. var isInputGroup = $(errorOBJ).closest('.input-group').size();
  125. var $showOBJ = isInputGroup ? $(errorOBJ).closest('.input-group').parent() : $(errorOBJ).parent();
  126. $showOBJ.append(errorMSG);
  127. $(errorOBJ).css('margin-bottom', 0);
  128. $(errorOBJ).css('border-color','#D2322D');
  129. if($(errorOBJ + '_chosen').size() > 0)
  130. {
  131. $(errorOBJ + '_chosen .chosen-single').css('border-color','#D2322D');
  132. $(errorOBJ + '_chosen .chosen-single').bind('keydown mousedown', function()
  133. {
  134. if(!triggered[i])
  135. {
  136. $(this).removeAttr('style')
  137. $('#' + errorLabel).remove();
  138. triggered[i] = true;
  139. }
  140. });
  141. }
  142. $(errorOBJ).bind('keydown mousedown', function()
  143. {
  144. if(!triggered[i])
  145. {
  146. $(this).removeAttr('style')
  147. $('#' + errorLabel).remove();
  148. triggered[i] = true;
  149. }
  150. });
  151. if(i == 1)$(errorOBJ).focus();
  152. setTimeout(function(){$('#' + errorLabel).remove();}, 5000);
  153. })
  154. }
  155. },
  156. /* When error occurs, alert the response text, status and error. */
  157. error:function(jqXHR, textStatus, errorThrown)
  158. {
  159. $.enableForm(formID);
  160. if(textStatus == 'timeout' || textStatus == 'error') return alert(lang.timeout);
  161. alert(jqXHR.responseText + textStatus + errorThrown);
  162. }
  163. };
  164. /* Call ajaxSubmit to sumit the form. */
  165. form.submit(function()
  166. {
  167. $(this).trigger('clearplaceholder');
  168. $(this).ajaxSubmit(options);
  169. return false; // Prevent the submitting event of the browser.
  170. });
  171. },
  172. /* Disable a form. */
  173. disableForm:function(formID)
  174. {
  175. $(formID).find(':submit').attr('disabled', true);
  176. },
  177. /* Enable a form. */
  178. enableForm:function(formID)
  179. {
  180. $(formID).find(':submit').attr('disabled', false);
  181. }
  182. });
  183. $(document).ready(function()
  184. {
  185. $.ajaxForm('.ajaxForm');
  186. $.ajaxForm("[data-type='ajax']");
  187. })