gitea源码

jquery.are-you-sure.ts 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // @ts-nocheck
  2. // Fork of the upstream module. The only changes are:
  3. // * use export to make it work with ES6 modules.
  4. // * the addition of `const` to make it strict mode compatible.
  5. // * ignore forms with "ignore-dirty" class, ignore hidden forms (closest('.tw-hidden'))
  6. /*!
  7. * jQuery Plugin: Are-You-Sure (Dirty Form Detection)
  8. * https://github.com/codedance/jquery.AreYouSure/
  9. *
  10. * Copyright (c) 2012-2014, Chris Dance and PaperCut Software http://www.papercut.com/
  11. * Dual licensed under the MIT or GPL Version 2 licenses.
  12. * http://jquery.org/license
  13. *
  14. * Author: chris.dance@papercut.com
  15. * Version: 1.9.0
  16. * Date: 13th August 2014
  17. */
  18. export function initAreYouSure($) {
  19. $.fn.areYouSure = function(options) {
  20. var settings = $.extend(
  21. {
  22. 'message' : 'You have unsaved changes!',
  23. 'dirtyClass' : 'dirty',
  24. 'change' : null,
  25. 'silent' : false,
  26. 'addRemoveFieldsMarksDirty' : false,
  27. 'fieldEvents' : 'change keyup propertychange input',
  28. 'fieldSelector': ":input:not(input[type=submit]):not(input[type=button])"
  29. }, options);
  30. var getValue = function($field) {
  31. if ($field.hasClass('ays-ignore')
  32. || $field.hasClass('aysIgnore')
  33. || $field.attr('data-ays-ignore')
  34. || $field.attr('name') === undefined) {
  35. return null;
  36. }
  37. if ($field.is(':disabled')) {
  38. return 'ays-disabled';
  39. }
  40. var val;
  41. var type = $field.attr('type');
  42. if ($field.is('select')) {
  43. type = 'select';
  44. }
  45. switch (type) {
  46. case 'checkbox':
  47. case 'radio':
  48. val = $field.is(':checked');
  49. break;
  50. case 'select':
  51. val = '';
  52. $field.find('option').each(function(o) {
  53. var $option = $(this);
  54. if ($option.is(':selected')) {
  55. val += $option.val();
  56. }
  57. });
  58. break;
  59. default:
  60. val = $field.val();
  61. }
  62. return val;
  63. };
  64. var storeOrigValue = function($field) {
  65. $field.data('ays-orig', getValue($field));
  66. };
  67. var checkForm = function(evt) {
  68. var isFieldDirty = function($field) {
  69. var origValue = $field.data('ays-orig');
  70. if (undefined === origValue) {
  71. return false;
  72. }
  73. return (getValue($field) != origValue);
  74. };
  75. var $form = ($(this).is('form'))
  76. ? $(this)
  77. : $(this).parents('form');
  78. // Test on the target first as it's the most likely to be dirty
  79. if (isFieldDirty($(evt.target))) {
  80. setDirtyStatus($form, true);
  81. return;
  82. }
  83. const $fields = $form.find(settings.fieldSelector);
  84. if (settings.addRemoveFieldsMarksDirty) {
  85. // Check if field count has changed
  86. var origCount = $form.data("ays-orig-field-count");
  87. if (origCount != $fields.length) {
  88. setDirtyStatus($form, true);
  89. return;
  90. }
  91. }
  92. // Brute force - check each field
  93. var isDirty = false;
  94. $fields.each(function() {
  95. var $field = $(this);
  96. if (isFieldDirty($field)) {
  97. isDirty = true;
  98. return false; // break
  99. }
  100. });
  101. setDirtyStatus($form, isDirty);
  102. };
  103. var initForm = function($form) {
  104. var fields = $form.find(settings.fieldSelector);
  105. $(fields).each(function() { storeOrigValue($(this)); });
  106. $(fields).unbind(settings.fieldEvents, checkForm);
  107. $(fields).bind(settings.fieldEvents, checkForm);
  108. $form.data("ays-orig-field-count", $(fields).length);
  109. setDirtyStatus($form, false);
  110. };
  111. var setDirtyStatus = function($form, isDirty) {
  112. var changed = isDirty != $form.hasClass(settings.dirtyClass);
  113. $form.toggleClass(settings.dirtyClass, isDirty);
  114. // Fire change event if required
  115. if (changed) {
  116. if (settings.change) settings.change.call($form, $form);
  117. if (isDirty) $form.trigger('dirty.areYouSure', [$form]);
  118. if (!isDirty) $form.trigger('clean.areYouSure', [$form]);
  119. $form.trigger('change.areYouSure', [$form]);
  120. }
  121. };
  122. var rescan = function() {
  123. var $form = $(this);
  124. var fields = $form.find(settings.fieldSelector);
  125. $(fields).each(function() {
  126. var $field = $(this);
  127. if (!$field.data('ays-orig')) {
  128. storeOrigValue($field);
  129. $field.bind(settings.fieldEvents, checkForm);
  130. }
  131. });
  132. // Check for changes while we're here
  133. $form.trigger('checkform.areYouSure');
  134. };
  135. var reinitialize = function() {
  136. initForm($(this));
  137. }
  138. if (!settings.silent && !window.aysUnloadSet) {
  139. window.aysUnloadSet = true;
  140. $(window).bind('beforeunload', function() {
  141. const $forms = $("form:not(.ignore-dirty)").filter('.' + settings.dirtyClass);
  142. const dirtyFormCount = Array.from($forms).reduce((res, form) => form.closest('.tw-hidden') ? res : res + 1, 0);
  143. if (dirtyFormCount === 0) return;
  144. // Prevent multiple prompts - seen on Chrome and IE
  145. if (navigator.userAgent.toLowerCase().match(/msie|chrome/)) {
  146. if (window.aysHasPrompted) {
  147. return;
  148. }
  149. window.aysHasPrompted = true;
  150. window.setTimeout(function() {window.aysHasPrompted = false;}, 900);
  151. }
  152. return settings.message;
  153. });
  154. }
  155. return this.each(function(elem) {
  156. if (!$(this).is('form')) {
  157. return;
  158. }
  159. var $form = $(this);
  160. $form.submit(function() {
  161. $form.removeClass(settings.dirtyClass);
  162. });
  163. $form.bind('reset', function() { setDirtyStatus($form, false); });
  164. // Add a custom events
  165. $form.bind('rescan.areYouSure', rescan);
  166. $form.bind('reinitialize.areYouSure', reinitialize);
  167. $form.bind('checkform.areYouSure', checkForm);
  168. initForm($form);
  169. });
  170. };
  171. }
  172. export function applyAreYouSure(selectorOrEl: string|Element|$, opts = {}) {
  173. $(selectorOrEl).areYouSure(opts);
  174. }
  175. export function reinitializeAreYouSure(selectorOrEl: string|Element|$) {
  176. $(selectorOrEl).trigger('reinitialize.areYouSure');
  177. }
  178. export function ignoreAreYouSure(selectorOrEl: string|Element|$) {
  179. // here we should only add "ignore-dirty" but not remove "dirty".
  180. // because when using "enter" to submit a form, the "dirty" class will appear again before reloading.
  181. $(selectorOrEl).addClass('ignore-dirty');
  182. }