From e1033d01387970aac725019c4b17f32a68d875d1 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Wed, 17 Jun 2020 12:42:08 -0700 Subject: [PATCH] Move tagname related functions from common.js to tag_autocomplete.js. --- frontends/etiquette_flask/static/js/common.js | 33 ---------------- .../static/js/tag_autocomplete.js | 39 ++++++++++++++++++- .../etiquette_flask/templates/photo.html | 2 +- .../etiquette_flask/templates/search.html | 8 ++-- frontends/etiquette_flask/templates/tags.html | 5 ++- 5 files changed, 45 insertions(+), 42 deletions(-) diff --git a/frontends/etiquette_flask/static/js/common.js b/frontends/etiquette_flask/static/js/common.js index 9818d2b..066c5ed 100644 --- a/frontends/etiquette_flask/static/js/common.js +++ b/frontends/etiquette_flask/static/js/common.js @@ -125,19 +125,6 @@ function entry_with_history_hook(event) } } -common.entry_with_tagname_replacements = -function entry_with_tagname_replacements(event) -{ - var cursor_position = event.target.selectionStart; - var new_value = common.tagname_replacements(event.target.value); - if (new_value != event.target.value) - { - event.target.value = new_value; - event.target.selectionStart = cursor_position; - event.target.selectionEnd = cursor_position; - } -} - common.html_to_element = function html_to_element(html) { @@ -262,26 +249,6 @@ function init_button_with_confirm() }); } -common.normalize_tagname = -function normalize_tagname(tagname) -{ - tagname = tagname.trim(); - tagname = tagname.toLocaleLowerCase(); - tagname = tagname.split("."); - tagname = tagname[tagname.length-1]; - tagname = tagname.split("+")[0]; - tagname = common.tagname_replacements(tagname); - return tagname; -} - -common.tagname_replacements = -function tagname_replacements(tagname) -{ - tagname = tagname.replace(new RegExp(" ", 'g'), "_"); - tagname = tagname.replace(new RegExp("-", 'g'), "_"); - return tagname; -} - common.refresh = function refresh() { diff --git a/frontends/etiquette_flask/static/js/tag_autocomplete.js b/frontends/etiquette_flask/static/js/tag_autocomplete.js index 5df88cd..763fa64 100644 --- a/frontends/etiquette_flask/static/js/tag_autocomplete.js +++ b/frontends/etiquette_flask/static/js/tag_autocomplete.js @@ -2,7 +2,8 @@ var tag_autocomplete = {}; tag_autocomplete.tagset = {"tags": [], "synonyms": {}}; -tag_autocomplete.DATALIST_ID = "tag_autocomplete_datalist" +tag_autocomplete.DATALIST_ID = "tag_autocomplete_datalist"; + tag_autocomplete.init_datalist = function init_datalist() { @@ -30,10 +31,43 @@ function init_datalist() } } +tag_automplete.normalize_tagname = +function normalize_tagname(tagname) +{ + tagname = tagname.trim(); + tagname = tagname.toLocaleLowerCase(); + tagname = tagname.split("."); + tagname = tagname[tagname.length-1]; + tagname = tagname.split("+")[0]; + tagname = tag_automplete.tagname_replacements(tagname); + return tagname; +} + +tag_automplete.tagname_replacements = +function tagname_replacements(tagname) +{ + tagname = tagname.replace(new RegExp(" ", 'g'), "_"); + tagname = tagname.replace(new RegExp("-", 'g'), "_"); + return tagname; +} + +tag_automplete.entry_with_tagname_replacements_hook = +function entry_with_tagname_replacements_hook(event) +{ + var cursor_position = event.target.selectionStart; + var new_value = tag_automplete.tagname_replacements(event.target.value); + if (new_value != event.target.value) + { + event.target.value = new_value; + event.target.selectionStart = cursor_position; + event.target.selectionEnd = cursor_position; + } +} + tag_autocomplete.resolve = function resolve(tagname) { - tagname = common.normalize_tagname(tagname); + tagname = tag_autocomplete.normalize_tagname(tagname); if (tag_autocomplete.tagset["tags"].indexOf(tagname) != -1) { return tagname; @@ -59,6 +93,7 @@ function update_tagset_callback(response) { tag_autocomplete.init_datalist(); } + console.log(`Updated tagset contains ${tag_autocomplete.tagset.tags.length}.`); return tag_autocomplete.tagset; } console.error(response); diff --git a/frontends/etiquette_flask/templates/photo.html b/frontends/etiquette_flask/templates/photo.html index 22048f4..c0ddb92 100644 --- a/frontends/etiquette_flask/templates/photo.html +++ b/frontends/etiquette_flask/templates/photo.html @@ -277,7 +277,7 @@ var PHOTO_ID = "{{photo.id}}"; var add_tag_box = document.getElementById('add_tag_textbox'); var add_tag_button = document.getElementById('add_tag_button'); add_tag_box.addEventListener("keyup", common.entry_with_history_hook); -add_tag_box.addEventListener("keyup", common.entry_with_tagname_replacements); +add_tag_box.addEventListener("keyup", tag_autocomplete.entry_with_tagname_replacements_hook); common.bind_box_to_button(add_tag_box, add_tag_button, false); var message_area = document.getElementById('message_area'); diff --git a/frontends/etiquette_flask/templates/search.html b/frontends/etiquette_flask/templates/search.html index df87a38..9e50f45 100644 --- a/frontends/etiquette_flask/templates/search.html +++ b/frontends/etiquette_flask/templates/search.html @@ -673,11 +673,11 @@ var inputted_forbids = []; {% endfor %} input_musts.addEventListener("keyup", tag_input_hook_musts); -input_musts.addEventListener("keyup", common.entry_with_tagname_replacements); -input_mays.addEventListener("keyup",tag_input_hook_mays); -input_mays.addEventListener("keyup", common.entry_with_tagname_replacements); +input_musts.addEventListener("keyup", tag_autocomplete.entry_with_tagname_replacements_hook); +input_mays.addEventListener("keyup", tag_input_hook_mays); +input_mays.addEventListener("keyup", tag_autocomplete.entry_with_tagname_replacements_hook); input_forbids.addEventListener("keyup", tag_input_hook_forbids); -input_forbids.addEventListener("keyup", common.entry_with_tagname_replacements); +input_forbids.addEventListener("keyup", tag_autocomplete.entry_with_tagname_replacements_hook); common.bind_box_to_button(input_expression, document.getElementById("search_go_button")); diff --git a/frontends/etiquette_flask/templates/tags.html b/frontends/etiquette_flask/templates/tags.html index 0cec320..e6aadc3 100644 --- a/frontends/etiquette_flask/templates/tags.html +++ b/frontends/etiquette_flask/templates/tags.html @@ -14,8 +14,9 @@ {% if theme %}{% endif %} - + +