diff --git a/.eslintrc.yml b/.eslintrc.yml index dec7a9c..9c90984 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -4,6 +4,8 @@ env: browser: true jquery: true extends: 'eslint:recommended' +plugins: + - ignore-erb rules: indent: - error diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 3cfbc1b..9faf3dd 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -14,13 +14,7 @@ //= require jquery_ujs //= require turbolinks //= require modernizr-lite/modernizr -//= require jquery-linedtextarea-moser -//= require button-group -//= require form-animation -//= require summary-edit -//= require textarea-limit -//= require live-coder - - -//= require admin +//= require forms/button-group +//= require forms/animations +//= require forms/textarea-limit diff --git a/app/assets/javascripts/form-animation.js b/app/assets/javascripts/forms/animations.js similarity index 99% rename from app/assets/javascripts/form-animation.js rename to app/assets/javascripts/forms/animations.js index ad71014..a920c94 100644 --- a/app/assets/javascripts/form-animation.js +++ b/app/assets/javascripts/forms/animations.js @@ -7,6 +7,7 @@ $textInput.each(function() { $(this).prev('label').addClass('animate'); } }); + $textInput.on('focus', function() { $(this).prev('label').addClass('animate'); }).on('focusout', function() { diff --git a/app/assets/javascripts/button-group.js b/app/assets/javascripts/forms/button-group.js similarity index 100% rename from app/assets/javascripts/button-group.js rename to app/assets/javascripts/forms/button-group.js diff --git a/app/assets/javascripts/textarea-limit.js b/app/assets/javascripts/forms/textarea-limit.js similarity index 100% rename from app/assets/javascripts/textarea-limit.js rename to app/assets/javascripts/forms/textarea-limit.js diff --git a/app/assets/javascripts/live-coder.js b/app/assets/javascripts/live-coder.js new file mode 100644 index 0000000..d641253 --- /dev/null +++ b/app/assets/javascripts/live-coder.js @@ -0,0 +1,2 @@ +//= require live-coder/linedtextarea +//= require live-coder/editor diff --git a/app/assets/javascripts/live-coder.js.erb b/app/assets/javascripts/live-coder/editor.js.erb similarity index 94% rename from app/assets/javascripts/live-coder.js.erb rename to app/assets/javascripts/live-coder/editor.js.erb index c645a5b..3f9652d 100644 --- a/app/assets/javascripts/live-coder.js.erb +++ b/app/assets/javascripts/live-coder/editor.js.erb @@ -18,9 +18,7 @@ function updateResults(elem) { var jqueryNode = document.createElement("script"); jqueryNode.setAttribute("type", "text/javascript"); - // TODO: fix eslint runner to handle erb snippets - // jqueryNode.setAttribute("src", "<%= "//#{ENV['full_app_url']}#{javascript_path "jquery"}" %>"); - jqueryNode.setAttribute("src", "https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"); + jqueryNode.setAttribute("src", "<%= "//#{ENV['full_app_url']}#{javascript_path "jquery"}" %>"); iHead.appendChild(jqueryNode); var codeStyle = document.createElement("style"); diff --git a/app/assets/javascripts/jquery-linedtextarea-moser.js b/app/assets/javascripts/live-coder/linedtextarea.js similarity index 100% rename from app/assets/javascripts/jquery-linedtextarea-moser.js rename to app/assets/javascripts/live-coder/linedtextarea.js diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index a638fed..cf00ea8 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -28,4 +28,14 @@ module ApplicationHelper %w(Coder live_code) ], selected: (val.blank? ? '' : val)) end + + # include javascript only once + # Allows the safe loading of js dependencies in partials multiple times. + def content_for_javascript_once code_label, &block + @js_blocks ||= [] + return if @js_blocks.include? code_label + + @js_blocks << code_label + content_for :custom_javascipt, &block + end end diff --git a/app/views/admin/question/_live_code.html.erb b/app/views/admin/question/_live_code.html.erb index 99793f0..4efc955 100644 --- a/app/views/admin/question/_live_code.html.erb +++ b/app/views/admin/question/_live_code.html.erb @@ -1,3 +1,9 @@ +<% + content_for_javascript_once 'live-coder' do + javascript_include_tag "live-coder" + end +%> +