diff --git a/app/assets/javascripts/live-coder.js b/app/assets/javascripts/live-coder.js index aed1f8b..26aee9d 100644 --- a/app/assets/javascripts/live-coder.js +++ b/app/assets/javascripts/live-coder.js @@ -1,17 +1,8 @@ -function updateResults(includeJavascript) { - // var includeJavascript = includeJavascript; - // - // if (!(typeof(includeJavascript) != 'undefined' && includeJavascript == true)){ - // includeJavascript = false; - // } - - var resultsContainer = document.querySelectorAll('[data-id="results"]')[0]; - var codeHtml = document.querySelectorAll('[data-id="code-html"]')[0].value.trim(); - var codeCss = document.querySelectorAll('[data-id="code-css"]')[0].value.trim(); - - // if(includeJavascript == true){ - var codeJs = document.querySelectorAll('[data-id="code-js"]')[0].value.trim(); - // } +function updateResults(elem) { + 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(); + var codeJs = $(elem).find('.code-js')[0].value.trim(); resultsContainer.innerHTML = ""; var iDoc = document.createElement('html'); @@ -33,13 +24,11 @@ function updateResults(includeJavascript) { iBody.innerHTML = codeHtml; iDoc.appendChild(iBody); - // if(includeJavascript == true){ - var codeScript = document.createElement("script"); - codeScript.setAttribute("type", "text/javascript"); - var scriptNode = document.createTextNode(codeJs); - codeScript.appendChild(scriptNode); - iDoc.appendChild(codeScript); - // } + var codeScript = document.createElement("script"); + codeScript.setAttribute("type", "text/javascript"); + var scriptNode = document.createTextNode(codeJs); + codeScript.appendChild(scriptNode); + iDoc.appendChild(codeScript); codeFrame.contentWindow.document.open(); codeFrame.contentWindow.document.appendChild(iDoc); @@ -98,39 +87,36 @@ function indentSelection(e){ } } +function loadLiveCoders(){ + $.each($('.answer-sec.live_code-type'), function(index, elem){ + var qid = $(elem).data('qid'); + $(elem).find("[data-id='live-coder-answer']").load("/live-coder-entry/" + qid, function(){ + $(elem).find('.js-error').addClass('hidden'); + $(elem).find(".code-input textarea").linedtextarea(); + updateResults(this); + }); + }); +} + timer = 0; $(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'); if (timer) { clearTimeout(timer); } - timer = setTimeout(updateResults, 500); + timer = setTimeout(updateResults(elem), 500); }); $("html").on('keydown', "textarea[data-id^=code-]", function(e){ indentSelection(e); }); - // If JavaScript is enabled, display the livecoder section dynamically - var qid = $('form#answer-form').data('qid'); - $("[data-id='live-coder-answer']").load("/live-coder-entry/" + qid, function(){ - // if it loads in, and hide "finish later" checkbox - $("[data-id='live-coder-finish-later']").addClass("hidden"); - $('.js-error').addClass('hidden'); - updateResults(); - $(".code-input textarea").linedtextarea(); + $.when(loadLiveCoders()).done(function(){ + //simple live coder for summary page + $("[data-id=live-coder-no-js], [data-id=live-coder-finish-later]").addClass('hidden'); + $("[data-id=live-coder]").removeClass('hidden'); }); - $("[data-id=live-coder]").each(function(){ - updateResults(); - }); - - - //simple live coder for summary page - $("[data-id=live-coder-no-js]").addClass('hidden'); - $("[data-id=live-coder]").removeClass('hidden'); - - $(".code-input textarea").linedtextarea(); - }); diff --git a/app/assets/javascripts/summary-edit.js b/app/assets/javascripts/summary-edit.js index 183ea4f..5f5dc12 100644 --- a/app/assets/javascripts/summary-edit.js +++ b/app/assets/javascripts/summary-edit.js @@ -98,9 +98,11 @@ var saveClickHandler = function(e) { var cssAnswer = $(thisEd.find('textarea.code-css')[0]).val(); var jsAnswer = $(thisEd.find('textarea.code-js')[0]).val(); data = { - 'html': htmlAnswer, - 'css': cssAnswer, - 'js': jsAnswer + 'live_code': { + 'html': htmlAnswer, + 'css': cssAnswer, + 'js': jsAnswer + } } } else if(thisEd.hasClass('radio-type')) { $(thisEd.find('input')).each(function() { @@ -136,7 +138,6 @@ var saveClickHandler = function(e) { }), success: function(data){ executeQuery = true; - //console.log(data); }, error: function(data){ executeQuery = false; diff --git a/app/controllers/candidate_controller.rb b/app/controllers/candidate_controller.rb index 2a271f8..bd99277 100644 --- a/app/controllers/candidate_controller.rb +++ b/app/controllers/candidate_controller.rb @@ -14,11 +14,9 @@ class CandidateController < ApplicationController def question qid = prep_status.current_question_id - redirect_to :summary and return if qid.nil? - prep_question qid - @answer = @question.answer.nil? ? Answer.new : Answer.find(@question.answer_id) + prep_instance_answer @question end def update_answer @@ -28,13 +26,14 @@ class CandidateController < ApplicationController end def live_coder - question + prep_question params[:question_id] + prep_instance_answer @question + prep_answer params[:question_id] render layout: false end def summary @quiz = current_candidate.my_quiz - redirect_to :question and return unless prep_status.current_question_id.nil? end @@ -67,6 +66,10 @@ class CandidateController < ApplicationController @status ||= QuizStatus.new(current_candidate) end + def prep_instance_answer question + @answer = question.answer.nil? ? Answer.new : Answer.find(question.answer_id) + end + def answer_params params.require(:answer).permit( :question_id, :answer_id, diff --git a/app/views/candidate/question.html.erb b/app/views/candidate/question.html.erb index d479d02..0610e88 100644 --- a/app/views/candidate/question.html.erb +++ b/app/views/candidate/question.html.erb @@ -7,30 +7,32 @@ <%= form_for(@answer, url: post_answer_path(@answer.id), html:{method: :post, id: 'answer-form', data: {qid: @question.question_id}}) do |form| %>
-

<%= @question.question %>

+
+

<%= @question.question %>

-
- <%= hidden_field_tag 'answer[question_id]', @question.question_id %> - <%= hidden_field_tag 'answer[answer_id]', @question.answer_id %> - <%= render partial: @question.input_type, locals: {question: @question, form: form} %> -
- - <% if @status.on_summary %> - -
- +
+ <%= hidden_field_tag 'answer[question_id]', @question.question_id %> + <%= hidden_field_tag 'answer[answer_id]', @question.answer_id %> + <%= render partial: @question.input_type, locals: {question: @question, form: form} %>
- <% else %> + <% if @status.on_summary %> -
- -
-
- -
+
+ +
- <% end %> + <% else %> + +
+ +
+
+ +
+ + <% end %> +
<% end %>