diff options
| author | jpl | 2017-02-16 10:51:38 +0100 |
|---|---|---|
| committer | jpl | 2017-02-16 10:51:38 +0100 |
| commit | 3e99839515fa7f7ae653d6b0439e083e6ee2fa31 (patch) | |
| tree | 8cda04b25632d4e230abc8d8378c7a3ca630063e /app/assets/javascripts | |
| parent | 4d41a1cc6adbf64d16a9293f883be15d3ee6c03f (diff) | |
| download | chouette-core-3e99839515fa7f7ae653d6b0439e083e6ee2fa31.tar.bz2 | |
fix for moved form-submit buttons (html5 with poly)
Diffstat (limited to 'app/assets/javascripts')
| -rw-r--r-- | app/assets/javascripts/plugins/html5-form-attr.js | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/app/assets/javascripts/plugins/html5-form-attr.js b/app/assets/javascripts/plugins/html5-form-attr.js new file mode 100644 index 000000000..8ec2d7161 --- /dev/null +++ b/app/assets/javascripts/plugins/html5-form-attr.js @@ -0,0 +1,69 @@ +(function($) { + + /** + * polyfill for html5 form attr + */ + var SAMPLE_FORM_NAME, sampleElementFound, sampleForm, sampleFormAndHiddenInput; + SAMPLE_FORM_NAME = 'html-5-polyfill-test'; + sampleForm = $('<form id=\'' + SAMPLE_FORM_NAME + '\'/>'); + sampleFormAndHiddenInput = sampleForm.add($('<input type=\'hidden\' form=\'' + SAMPLE_FORM_NAME + '\'/>')); + sampleFormAndHiddenInput.prependTo('body'); + sampleElementFound = sampleForm[0].elements[0]; + sampleFormAndHiddenInput.remove(); + if (sampleElementFound) { + return; + } + + /** + * Append a field to a form + # + */ + $.fn.appendField = function(data) { + var $form; + if (!this.is('form')) { + return; + } + if (!$.isArray(data) && data.name && data.value) { + data = [data]; + } + $form = this; + $.each(data, function(i, item) { + $('<input/>').attr('type', 'hidden').attr('name', item.name).val(item.value).appendTo($form); + }); + return $form; + }; + + /** + * Find all input fields with form attribute point to jQuery object + * + */ + $('form[id]').submit(function(e) { + var data; + data = $('[form=' + this.id + ']').serializeArray(); + $(this).appendField(data); + }).each(function() { + var $fields, form; + form = this; + $fields = $('[form=' + this.id + ']'); + $fields.filter('button, input').filter('[type=reset],[type=submit]').click(function() { + var type; + type = this.type.toLowerCase(); + if (type === 'reset') { + form.reset(); + $fields.each(function() { + this.value = this.defaultValue; + this.checked = this.defaultChecked; + }).filter('select').each(function() { + $(this).find('option').each(function() { + this.selected = this.defaultSelected; + }); + }); + } else if (type.match(/^submit|image$/i)) { + $(form).appendField({ + name: this.name, + value: this.value + }).submit(); + } + }); + }); +})(jQuery); |
