[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 /** 2 * @requires javelin-behavior javelin-stratcom javelin-dom 3 * @provides javelin-behavior-aphront-form-disable-on-submit 4 */ 5 6 JX.behavior('aphront-form-disable-on-submit', function() { 7 8 var restore = []; 9 var root = null; 10 var new_tab = false; 11 12 JX.Stratcom.listen('click', 'tag:button', function(e) { 13 var raw = e.getRawEvent(); 14 new_tab = (raw.altKey || raw.ctrlKey || raw.metaKey || raw.shiftKey); 15 }); 16 17 18 JX.Stratcom.listen('keydown', ['tag:form', 'tag:textarea'], function(e) { 19 var raw = e.getRawEvent(); 20 if (!(e.getSpecialKey() === 'return' && (raw.ctrlKey || raw.metaKey))) { 21 return; 22 } 23 24 e.kill(); 25 26 var form = e.getNode('tag:form'); 27 28 // This allows 'workflow' and similar actions to take effect. 29 var r = JX.DOM.invoke(form, 'didSyntheticSubmit'); 30 if (r.getPrevented()) { 31 return; 32 } 33 34 // Don't double-submit forms. 35 if (form._disabled) { 36 return; 37 } 38 39 will_submit(form); 40 41 // If nothing handled the synthetic submit, submit normally. 42 form.submit(); 43 }); 44 45 function will_submit(root) { 46 root._disabled = true; 47 var buttons = JX.DOM.scry(root, 'button'); 48 for (var ii = 0; ii < buttons.length; ii++) { 49 if (!buttons[ii].disabled) { 50 buttons[ii].disabled = 'disabled'; 51 JX.DOM.alterClass(buttons[ii], 'disabled', true); 52 restore.push(buttons[ii]); 53 } 54 } 55 } 56 57 JX.Stratcom.listen('submit', 'tag:form', function(e) { 58 if (e.getNode('workflow')) { 59 // Don't activate for forms with workflow, the workflow behavior will 60 // handle it. 61 return; 62 } 63 64 root = e.getNode('tag:form'); 65 66 // If the form is a "download" form, don't disable it on submit because 67 // we won't transition off the page. 68 if (JX.Stratcom.hasSigil(root, 'download')) { 69 return; 70 } 71 72 // Open the form to a new tab in Firefox explicitly (automatic in Chrome). 73 // We render some buttons as links so users may be confused that the links 74 // don't open to new tabs with Ctrl+click as normal links. 75 root.target = (new_tab ? '_blank' : ''); 76 if (new_tab) { 77 return; 78 } 79 80 if (root._disabled) { 81 e.kill(); 82 } 83 84 will_submit(root); 85 }); 86 87 JX.Stratcom.listen('unload', null, function() { 88 // Re-enable everything on page unload so we don't bfcache back to a page 89 // that has disabled forms. 90 for (var ii = 0; ii < restore.length; ii++) { 91 restore[ii].disabled = ''; 92 JX.DOM.alterClass(restore[ii], 'disabled', false); 93 root._disabled = false; 94 } 95 if (root) { 96 delete root._disabled; 97 } 98 restore = []; 99 root = null; 100 }); 101 102 });
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |