PageSourceSearch

https://www.diffuz.com/js/jalios/core/jalios-single-submit.js

js diffuz.com collected 2026-10-03 22:03:28 UTC 7,137 bytes, 260 lines download raw bytes

1!function ($) {
2  
3  // Namespace
4  if (!$.jalios)      { $.jalios      = {}; }
5  if (!$.jalios.form) { $.jalios.form = {}; }
6 
7  // ------------------------------------------
8  //  PRIVATE
9  // ------------------------------------------
10  
11  var markAlreadyDisabledForExclusion = function(event) {
12    $('.disabled, :disabled').each(function(index) {
13      var $this = $(this);
14      $this.addClass('excludeSingleSubmit');
15    });
16  }
17  
18  var refresh = function(event){
19    var refresh = event.refresh;
20    if (!refresh){ return; }
21
22    if (refresh.type == 'before'){
23      
24      // Only ajax-refresh of forms
25      if (!refresh.options || !refresh.options.isform){ return; }
26
27      var $form = $(refresh.options.isform);
28      if (!$form.exists()){ return; } // Check element
29    
30      // Prevent submit if wysiwyg init still in progress
31      var hasPendingWysiwigInit = $form.hasClass('wysiwyg-init-in-progress');
32      if (hasPendingWysiwigInit){
33        event.preventDefault();
34        event.stopPropagation();
35        event.stopImmediatePropagation();
36        return;
37      }
38    
39      // Check class="noSingleSubmitButton"
40      // FIXME: Use data-* attribute instead ?
41
42      $.jalios.form.SingleSubmit.ajaxRefreshing = true;
43      if ($form.hasClass('noSingleSubmitButton')){ return; }
44      
45      // Prevent submit if we are processing something
46      if ($.jalios.form.SingleSubmit.processing){
47        event.preventDefault();
48        event.stopPropagation();
49        event.stopImmediatePropagation();
50        return;
51      }
52      
53      // Disable buttons
54      $.jalios.form.SingleSubmit.disable();
55      
56    } else if (refresh.type == 'success' || refresh.type == 'fail'){ // after, fail, error, ... 
57      $.jalios.form.SingleSubmit.enable();
58      $.jalios.form.SingleSubmit.ajaxRefreshing = false;
59    }
60  }
61  
62  var submit = function(event){
63
64    var $form =  $(event.currentTarget);
65    
66    // Check element
67    if (!$form.exists()){ return; }
68    
69    // An ajax-refresh is running
70    if ($.jalios.form.SingleSubmit.ajaxRefreshing){
71      event.preventDefault();
72      event.stopPropagation();
73      event.stopImmediatePropagation();
74      return;
75    }
76    
77    // Prevent submit if wysiwyg init still in progress
78    var hasPendingWysiwigInit = $form.hasClass('wysiwyg-init-in-progress');
79    if (hasPendingWysiwigInit){
80      event.preventDefault();
81      event.stopPropagation();
82      event.stopImmediatePropagation();
83      return;
84    }
85    
86    // Check class="noSingleSubmitButton"
87    // FIXME: Use data-* attribute instead ?
88    if ($form.hasClass('noSingleSubmitButton')){ return; }
89    
90    // Prevent submit if we are processing something
91    if ($.jalios.form.SingleSubmit.processing){
92      event.preventDefault();
93      event.stopPropagation();
94      event.stopImmediatePropagation();
95      return;
96    }
97    
98    // Disable buttons
99    $.jalios.form.SingleSubmit.disable();
100    
101    // Remove unload message
102    window.onbeforeunload = null;
103  }
104 
105  // Register a logger
106  var register =  function(){
107    $(document).ready(markAlreadyDisabledForExclusion);
108    $(document).on("jalios:refresh", refresh);
109    $(document).on('submit','FORM', submit);
110  }
111  
112  // ----------
113  //  UNLOAD
114  // ----------
115  
116  var onKeyPress = function(event){
117    // Skip F5 key
118    if (event.keyCode == 116) {
119      return;
120    }
121
122    $(event.target).attr('data-jalios-dirty-input', 'true');    
123  }
124  
125  var isDirty = function() {
126    // 1. Check basic input that were observed using keypress
127    var hasDirtyInput = $(document).find('[data-jalios-dirty-input="true"]').length;
128    if (hasDirtyInput) {
129      return true;
130    }
131    
132    // 2. use custom API for TinyMCE wysiwyg
133    if (typeof(tinyMCE) !== 'undefined') {
134      var isAnyWysiwygDirty = false;
135      jQuery.each(tinyMCE.editors, function(idx, editor) { 
136        isAnyWysiwygDirty |= editor.isDirty();
137      });
138      if (isAnyWysiwygDirty) {
139        return true;
140      }
141    }
142    
143    return false;
144  }
145  
146  var initUnload = function(){
147    // Register unload message
148    window.onbeforeunload = onFormUnload;
149    
150    // Register dirty check for basic input
151    $(document).on('keypress', ':input', onKeyPress);
152    
153  }
154  
155  var onFormUnload = function(){
156    // If this flag is enabled, the message will not be displayed 
157    if (window.disableOnBeforeUnloadMessage) {
158      return;
159    }
160
161    if (!isDirty()) {
162      return;
163    }
164    
165    // Otherwise display the message to the user
166    return I18N.glp('warn.edit.contentlost');
167  }
168
169  // ------------------------------------------
170  //  PUBLIC
171  // ------------------------------------------
172 
173 /**
174   * SingleSubmit plugin will prevent forms and XHR to be submited more than once<br/>
175   * <br/> 
176   * 
177   * <h5>Features</h5>
178   * <ul>
179   *   <li>Listen to Ajax-Request submits</li> 
180   *   <li>Add global boolean to prevent multiple submit</li>
181   *   <li>Activate feature with disable() / enable()</li>
182   *   <li>FORM.noSingleSubmitButton bypass the process</li>
183   * </ul>
184   * 
185   * <h5>Convention</h5>
186   * <ul>
187   *   <li>
188   *     disable: new class convention to disable a given element. (do not use attribute because of side effects)
189   *     <br/>=> Handled by Ajax-Refresh
190   *   </li>
191   * </ul>
192   *            
193   * 
194   * @namespace $.jalios.form.SingleSubmit
195   * @copyright Jalios SA
196   * @example
197   * 
198   * $.jalios.form.SingleSubmit.disable();
199   * 
200   * @unittest js/tests/jalios/core/jalios-single-submit.html
201   */
202 
203  // Plugin definition
204  $.jalios.form.SingleSubmit = {
205    
206    // Expose variable for hacks
207    processing: false,
208    
209    /**
210     * Enable all formButton of Forms
211     */
212    enable:  function(){ $.jalios.form.SingleSubmit.toggle(true);  },
213    
214    /**
215     * Disable all formButton of Forms without class 'noSingleSubmitButton'
216     */
217    disable: function(){ $.jalios.form.SingleSubmit.toggle(false); },
218    
219    /**
220     * Toggle availability of all formButton of Forms without class 'noSingleSubmitButton'
221     *  @param {boolean} enable to force state enbale/disable
222     */
223    toggle:  function(enable){
224      
225      // Rise a flag for the given page
226      // FIXME: add a class on BODY ? to handle live()
227      $.jalios.form.SingleSubmit.processing = !enable;
228      
229      // Update all formButton
230      $('INPUT.formButton, BUTTON.formButton, A.formButton, INPUT.btn, BUTTON.btn, A.btn').each(function(index) {
231         var $this = $(this);
232         var $form = $($this.prop('form') || $this.closest('FORM'));
233         if ($form.hasClass('noSingleSubmitButton')){ return; }
234         if ($this.hasClass('excludeSingleSubmit')){ return; }
235         
236         $this.toggleClass('disabled', !enable);
237      });
238    },
239    
240    /**
241     * Init an unload message before leaving form
242     */
243    'initUnload': initUnload
244  };
245  
246  // Plugin initialization on DOM ready
247  $(document).ready(function($) {
248    register();
249  });
250  
251}(window.jQuery);
252
253
254// ------------------------------------------
255//  DEPRECATED
256// ------------------------------------------
257
258function initUnloadMessage(){
259  jQuery.jalios.form.SingleSubmit.initUnload();
260}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.