From e079320083c5fc4398b6cb37e05d5b2edd6179ec Mon Sep 17 00:00:00 2001 From: Marcos Mariano Date: Wed, 14 Jan 2015 20:24:21 -0200 Subject: [PATCH] Feature: Form reset Implementing a reset functionality to .ui.form --- src/definitions/behaviors/form.js | 51 ++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/definitions/behaviors/form.js b/src/definitions/behaviors/form.js index 0075d4573..44f7be1e1 100644 --- a/src/definitions/behaviors/form.js +++ b/src/definitions/behaviors/form.js @@ -46,6 +46,7 @@ $.fn.form = function(fields, parameters) { $message = $(this).find(selector.message), $prompt = $(this).find(selector.prompt), $submit = $(this).find(selector.submit), + $reset = $(this).find(selector.reset), formErrors = [], @@ -59,6 +60,7 @@ $.fn.form = function(fields, parameters) { initialize: function() { module.verbose('Initializing form validation', $module, validation, settings); module.bindEvents(); + module.setDefaults(); module.instantiate(); }, @@ -115,6 +117,7 @@ $.fn.form = function(fields, parameters) { ; // attach submit events module.attachEvents($submit, 'submit'); + module.attachEvents($reset, 'reset'); $field .each(function() { @@ -129,6 +132,51 @@ $.fn.form = function(fields, parameters) { ; }, + setDefaults: function() { + $field + .each(function () { + var + $field = $(this), + type = $field.prop('type') + ; + + switch (type) { + case 'checkbox': + $field.data('defaultValue', $field.is(':checked')); + break; + default: + $field.data('defaultValue', $field.val()); + } + }) + ; + }, + + reset: function() { + $field + .each(function () { + var + $field = $(this), + $parent = $field.parent(), + type = $field.prop('type'), + defaultValue = $field.data('defaultValue') + ; + + switch (type) { + case 'hidden': + $parent.is('.ui.dropdown') && $parent.dropdown('restore defaults'); + break; + case 'checkbox': + $parent.is('.ui.checkbox') && $parent.checkbox(defaultValue ? 'check' : 'uncheck'); + break; + default: + $field.val(defaultValue); + } + + $field.parents('.field').removeClass(settings.className.error); + }) + ; + }, + removeEvents: function() { $module .off(eventNamespace) @@ -670,7 +718,8 @@ $.fn.form.settings = { checkbox: 'input[type="checkbox"], input[type="radio"]', input : 'input', prompt : '.prompt.label', - submit : '.submit' + submit : '.submit', + reset : '.reset' }, className : {