app/assets/javascripts/activeadmin-selleo-cms/forms.storage.js in activeadmin-selleo-cms-0.0.45 vs app/assets/javascripts/activeadmin-selleo-cms/forms.storage.js in activeadmin-selleo-cms-0.0.46

- old
+ new

@@ -1,26 +1,56 @@ +function generateUUID(){ + var d = new Date().getTime(); + var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { + var r = (d + Math.random()*16)%16 | 0; + d = Math.floor(d/16); + return (c=='x' ? r : (r&0x7|0x8)).toString(16); + }); + return uuid; +}; + +function submitAnswer(form_id, input, value){ + $.post('/form_answers', { + form_answer: { + form_uuid: localStorage[form_id], + dom_id: $(input).attr('id'), + form_id: $(input).closest('form').data('form-id'), + form_question_id: $(input).data('form-question-id'), + value: value + } + }) +} + function setupCmsForm(form_id) { $('#'+form_id+' .required fieldset [type="radio"]:first,[type="checkbox"]:first').attr('required','true'); $('#'+form_id).validate(); - $.each($('#' + form_id + ' input, textarea'), function(){ - if ( localStorage[$(this).attr('id')] != undefined ) { - if ( ($(this).attr('type') == 'checkbox') || ($(this).attr('type') == 'radio') ) { - $(this).attr('checked', (localStorage[$(this).attr('id')] == "true")); - } else if ($(this).attr('type') == 'file') { -// ?? - } else { - $(this).val(localStorage[$(this).attr('id')]); + if (localStorage[form_id] == undefined) { + localStorage[form_id] = generateUUID(); + } + + $.get('/form_answers.json', { + form_uuid: localStorage[form_id] + }).success(function(resp){ + $.each(resp, function(){ + if (this["value"] != null) { + var form_elem = $('#'+this["dom_id"]); + if ( ($(form_elem).attr('type') == 'checkbox') || ($(form_elem).attr('type') == 'radio') ) { + $(form_elem).attr('checked', true); + } else if ($(form_elem).attr('type') == 'file') { +// ?? + } else { + $(form_elem).val(this["value"]); + } } - } + }) }); $('#' + form_id + ' input, textarea').change(function(){ - if ( ($(this).attr('type') == 'checkbox') || ($(this).attr('type') == 'radio') ) { - localStorage[$(this).attr('id')] = $(this).is(':checked'); - } else if ($(this).attr('type') == 'file') { + if ($(this).attr('type') == 'file') { // ?? - } else { - localStorage[$(this).attr('id')] = $(this).val(); - } - }); + } else if ( ($(this).attr('type') == 'checkbox') || ($(this).attr('type') == 'radio') ) { + submitAnswer(form_id, this, $(this).is(':checked')) + } else { + submitAnswer(form_id, this, $(this).val()) + }}); } \ No newline at end of file