From 30bc21c96aa79ac4e98a4b7f40a795b23a118b6e Mon Sep 17 00:00:00 2001 From: Henne Vogelsang Date: Tue, 5 Mar 2024 15:32:14 +0100 Subject: [PATCH] Move word counting javascript to the page It really does not need to run on every page load... --- .haml-lint_todo.yml | 3 ++- app/assets/javascripts/osem.js | 23 ----------------------- app/views/proposals/_form.html.haml | 24 ++++++++++++++++++++++++ 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/.haml-lint_todo.yml b/.haml-lint_todo.yml index 8d6474cd..76ec92e7 100644 --- a/.haml-lint_todo.yml +++ b/.haml-lint_todo.yml @@ -45,6 +45,7 @@ linters: - "app/views/layouts/_admin_sidebar.html.haml" - "app/views/proposals/index.html.haml" - "app/views/proposals/show.html.haml" + - "app/views/proposals/_form.html.haml" # Offense count: 28 IdNames: @@ -145,4 +146,4 @@ linters: # Offense count: 1 FinalNewline: exclude: - - "app/views/layouts/_admin.html.haml" \ No newline at end of file + - "app/views/layouts/_admin.html.haml" diff --git a/app/assets/javascripts/osem.js b/app/assets/javascripts/osem.js index 55c528b2..240a48c9 100644 --- a/app/assets/javascripts/osem.js +++ b/app/assets/javascripts/osem.js @@ -142,29 +142,6 @@ function word_count(text, divId, maxcount) { }); }; -/* Wait for the DOM to be ready before attaching events to the elements */ -$( document ).ready(function() { - /* Set the minimum and maximum proposal abstract word length */ - function updateEventTypeRequirements() { - var $selected = $("#event_event_type_id option:selected") - var max = $selected.data("max-words"); - var min = $selected.data("min-words"); - - $("#abstract-maximum-word-count").text(max); - $("#abstract-minimum-word-count").text(min); - word_count($('#event_abstract').get(0), 'abstract-count', max); - } - $("#event_event_type_id").change(updateEventTypeRequirements); - updateEventTypeRequirements(); - - /* Count the proposal abstract length */ - $("#event_abstract").on('input', function() { - var $selected = $("#event_event_type_id option:selected") - var max = $selected.data("max-words"); - word_count(this, 'abstract-count', max); - } ); -}); - /* Commodity function for modal windows */ window.build_dialog = function(selector, content) { diff --git a/app/views/proposals/_form.html.haml b/app/views/proposals/_form.html.haml index e1c40e8a..928d4a9c 100644 --- a/app/views/proposals/_form.html.haml +++ b/app/views/proposals/_form.html.haml @@ -90,3 +90,27 @@ - submit_copy = action_is_edit ? 'Update Proposal' : 'Create Proposal' = f.submit submit_copy, class: 'btn btn-success' +- content_for :script_head do + :javascript + /* Wait for the DOM to be ready before attaching events to the elements */ + $( document ).ready(function() { + /* Set the minimum and maximum proposal abstract word length */ + function updateEventTypeRequirements() { + var $selected = $("#event_event_type_id option:selected") + var max = $selected.data("max-words"); + var min = $selected.data("min-words"); + + $("#abstract-maximum-word-count").text(max); + $("#abstract-minimum-word-count").text(min); + word_count($('#event_abstract').get(0), 'abstract-count', max); + } + $("#event_event_type_id").change(updateEventTypeRequirements); + updateEventTypeRequirements(); + + /* Count the proposal abstract length */ + $("#event_abstract").on('input', function() { + var $selected = $("#event_event_type_id option:selected") + var max = $selected.data("max-words"); + word_count(this, 'abstract-count', max); + } ); + });