diff --git a/app/assets/javascripts/jquery-linedtextarea-moser.js b/app/assets/javascripts/jquery-linedtextarea-moser.js index bea9ef9..e1059ae 100644 --- a/app/assets/javascripts/jquery-linedtextarea-moser.js +++ b/app/assets/javascripts/jquery-linedtextarea-moser.js @@ -87,17 +87,6 @@ } - /* Set the width */ - // var sidebarWidth = linesDiv.outerWidth(); - // var paddingHorizontal = parseInt( linedWrapDiv.css("border-left-width") ) + parseInt( linedWrapDiv.css("border-right-width") ) + parseInt( linedWrapDiv.css("padding-left") ) + parseInt( linedWrapDiv.css("padding-right") ); - // var linedWrapDivNewWidth = originalTextAreaWidth - paddingHorizontal; - // var textareaNewWidth = originalTextAreaWidth - sidebarWidth - paddingHorizontal - 20; - // - // textarea.width( textareaNewWidth ); - // linedWrapDiv.width( linedWrapDivNewWidth ); - - - /* React to the scroll event */ textarea.scroll( function(){ var domTextArea = $(this)[0]; diff --git a/app/assets/javascripts/live-coder.js.erb b/app/assets/javascripts/live-coder.js.erb index f559a09..d88b9e2 100644 --- a/app/assets/javascripts/live-coder.js.erb +++ b/app/assets/javascripts/live-coder.js.erb @@ -1,4 +1,6 @@ function updateResults(elem) { + if ($(elem).length ===0){return false;}; + var resultsContainer = $(elem).find('[data-id="results"]')[0]; var codeHtml = $(elem).find('.code-html')[0].value.trim(); var codeCss = $(elem).find('.code-css')[0].value.trim(); @@ -100,7 +102,7 @@ $(function(){ // wait a half second before updating results // restart the timer if they resume typing $('html').on('keyup', '.code-input textarea', function(){ - var elem = $(this).closest('.answer-sec.live_code-type, .answer-sec.live_code_text-type'); + var elem = $(this).closest("[data-id=live-coder-answer]"); if (timer) { clearTimeout(timer); } timer = setTimeout(updateResults(elem), 500); }); diff --git a/app/assets/javascripts/summary-edit.js b/app/assets/javascripts/summary-edit.js index e3ebf7f..5b2dc34 100644 --- a/app/assets/javascripts/summary-edit.js +++ b/app/assets/javascripts/summary-edit.js @@ -1,181 +1,83 @@ +/* global updateResults */ +/* TODO: remove global ^ once live-coder is properly name spaced */ /** * Summary Page Answer Editor */ +function disableForm($form){ + $form.find('fieldset').prop('disabled', true); + $form.find('textarea').prop('disabled', true); + $form.find('.button-save, .button-cancel').hide(); + $form.find('.button-edit').show(); + $form.find('.editable').removeClass('editable'); + $('.button-edit, .submit-button').removeClass('disabled-button'); +} -(function($){ - $.fn.setTextAreaHeight = function(input) { - return this.each(function(){ - var lineHeight = parseInt($(this).css('line-height')); - var rows = Math.ceil(input / lineHeight); - rows = rows === 0 ? 1 : rows; - - $(this).attr('rows', rows); - }); - }; - - $('input[type="radio"]').on('change', function() { - var inputName = $(this).attr('name'); - //$('input[name="'+inputName+'"]').attr('checked', false); - var value = $(this).attr('value'); - $('input[name="'+inputName+'"][value="'+value+'"]').attr("checked",true); - $('input[name="'+inputName+'"]').each(function() { - if($(this).val() != value) { - $(this).attr('checked', false); - } - }); +function restoreValues($form){ + $form.find('[type=radio][data-last], [type=checkbox][data-last]').each(function(){ + $(this).prop('checked', $(this).attr('data-last')); }); - // $('.run-js').hide().delay(); -}(jQuery)); + $form.find('textarea[data-last]').each(function(){ + $(this).val($(this).attr('data-last')); + }); +} -var existingValue = []; +function updateLocalValues($form){ + $form.find('[type=radio][data-last], [type=checkbox][data-last]').each(function(){ + $(this).attr('data-last', $(this).prop('checked') ? 'checked' : ''); + }); -var editClickHandler = function(e) { + $form.find('textarea[data-last]').each(function(){ + $(this).attr('data-last', $(this).val()); + }); +} + +function prepareAjax($form) { + $form.on("ajax:success", function(e, data){ + $form.prepend('
' + $.trim(thisEd.data('answer')) + '
'); - thisEd.find('.answer-block, .code-answer').prop('disabled', true); - thisEd.find('.button-edit').show(); - thisEd.find('.button-save, .button-cancel').hide(); - // thisEd.find('button.run-js').hide(); - existingValue = []; -}; + var $form = $(e.delegateTarget).closest('form'); + $form.find('.error, .success').remove(); + disableForm($form); + restoreValues($form); + updateResults($form.find("[data-id=live-coder-answer]")); +} -var saveClickHandler = function(e) { +function saveClickHandler(e) { e.preventDefault(); - var thisEd = $(e.delegateTarget); - var data =[]; - var executeQuery; - var questionId = thisEd.find('.button-edit').attr('data-questionId'); - var answerId = thisEd.find('.button-edit').attr('data-answerId'); + var $form = $(e.delegateTarget).closest('form'); + $form.find('.error, .success').remove(); + $form.submit(); +} - if (thisEd.hasClass('live_code-type')) { - var htmlAnswer = $(thisEd.find('textarea.code-html')[0]).val(); - var cssAnswer = $(thisEd.find('textarea.code-css')[0]).val(); - var jsAnswer = $(thisEd.find('textarea.code-js')[0]).val(); - data = { - 'live_code': { - 'html': htmlAnswer, - 'css': cssAnswer, - 'js': jsAnswer - } - }; - } else if(thisEd.hasClass('radio-type')) { - $(thisEd.find('input')).each(function() { - if($(this).prop('checked') === true) { - data = ({ - 'radio': $(this).val() - }); - } - }); - } else if(thisEd.hasClass('checkbox-type')) { - data = {'checkbox': []}; - - $(thisEd.find('input')).each(function() { - if($(this).prop('checked') === true) { - data.checkbox.push($(this).val()); - } - }); - } else { - data = {'text': thisEd.find('textarea').val()}; - } - - if(data === '') { - $(thisEd).before('' + $.trim(thisEd.find('textarea').val()) + '
'); - var postUrl = thisEd.closest('form').attr('action'); - - $.ajax({ - type: "POST", - url: postUrl, - data: ({ - 'answer': $.extend(data, {'question_id': questionId, 'answer_id': answerId}), - 'submit': true - }), - success: function(){ //unused data - executeQuery = true; - }, - error: function(){ //unused data - executeQuery = false; - } - }).done(function() { - if(executeQuery === true) { - $('.success, .error').remove(); - $(thisEd).before('