diff --git a/frontends/etiquette_flask/static/js/albums.js b/frontends/etiquette_flask/static/js/albums.js index 78fbc4b..542f90f 100644 --- a/frontends/etiquette_flask/static/js/albums.js +++ b/frontends/etiquette_flask/static/js/albums.js @@ -9,7 +9,7 @@ function on_pageload() create_child_title_entry = document.getElementById("create_child_title_entry"); create_child_submit_button = document.getElementById("create_child_submit_button"); create_child_cancel_button = document.getElementById("create_child_cancel_button"); - bind_box_to_button(create_child_title_entry, create_child_submit_button); + common.bind_box_to_button(create_child_title_entry, create_child_submit_button); } document.addEventListener("DOMContentLoaded", on_pageload); @@ -55,7 +55,7 @@ function create_album_and_follow(title, parent) console.log(response); } } - post(url, data, receive_callback); + common.post(url, data, receive_callback); } function submit_create_child(event) diff --git a/frontends/etiquette_flask/static/js/common.js b/frontends/etiquette_flask/static/js/common.js index 467d0fc..f53f436 100644 --- a/frontends/etiquette_flask/static/js/common.js +++ b/frontends/etiquette_flask/static/js/common.js @@ -1,5 +1,8 @@ -var INPUT_TYPES = new Set(["INPUT", "TEXTAREA"]); +var common = {}; +common.INPUT_TYPES = new Set(["INPUT", "TEXTAREA"]); + +common.create_message_bubble = function create_message_bubble(message_area, message_positivity, message_text, lifespan) { if (lifespan === undefined) @@ -15,6 +18,7 @@ function create_message_bubble(message_area, message_positivity, message_text, l setTimeout(function(){message_area.removeChild(message);}, lifespan); } +common._request = function _request(method, url, callback) { var request = new XMLHttpRequest(); @@ -39,17 +43,22 @@ function _request(method, url, callback) request.open(method, url, asynchronous); return request; } + +common.get = function get(url, callback) { - request = _request("GET", url, callback); + request = common._request("GET", url, callback); request.send(); } + +common.post = function post(url, data, callback) { - request = _request("POST", url, callback); + request = common._request("POST", url, callback); request.send(data); } +common.delete_all_children = function delete_all_children(element) { while (element.firstChild) @@ -58,6 +67,7 @@ function delete_all_children(element) } } +common.bind_box_to_button = function bind_box_to_button(box, button, ctrl_enter) { // Thanks Yaroslav Yakovlev @@ -77,6 +87,7 @@ function bind_box_to_button(box, button, ctrl_enter) box.addEventListener("keyup", bound_box_hook); } +common.entry_with_history_hook = function entry_with_history_hook(event) { //console.log(event); @@ -115,6 +126,7 @@ function entry_with_history_hook(event) } } +common.html_to_element = function html_to_element(html) { var template = document.createElement("template"); @@ -122,6 +134,7 @@ function html_to_element(html) return template.content.firstChild; } +common.normalize_tagname = function normalize_tagname(tagname) { tagname = tagname.trim(); diff --git a/frontends/etiquette_flask/static/js/editor.js b/frontends/etiquette_flask/static/js/editor.js index 8b51a68..e4dda56 100644 --- a/frontends/etiquette_flask/static/js/editor.js +++ b/frontends/etiquette_flask/static/js/editor.js @@ -225,11 +225,11 @@ function Editor(elements, on_open, on_save, on_cancel) var edit_element = this.edit_elements[index]; if (edit_element.tagName == "TEXTAREA") { - bind_box_to_button(edit_element, this.save_button, true); + common.bind_box_to_button(edit_element, this.save_button, true); } else { - bind_box_to_button(edit_element, this.save_button, false); + common.bind_box_to_button(edit_element, this.save_button, false); } } } diff --git a/frontends/etiquette_flask/static/js/hotkeys.js b/frontends/etiquette_flask/static/js/hotkeys.js index 003fab4..758c19e 100644 --- a/frontends/etiquette_flask/static/js/hotkeys.js +++ b/frontends/etiquette_flask/static/js/hotkeys.js @@ -33,7 +33,7 @@ function should_prevent_hotkey(event) } else { - return INPUT_TYPES.has(event.target.tagName); + return common.INPUT_TYPES.has(event.target.tagName); } } diff --git a/frontends/etiquette_flask/static/js/photoclipboard.js b/frontends/etiquette_flask/static/js/photoclipboard.js index 042e538..7f670b3 100644 --- a/frontends/etiquette_flask/static/js/photoclipboard.js +++ b/frontends/etiquette_flask/static/js/photoclipboard.js @@ -226,7 +226,7 @@ function update_clipboard_tray() var tray_lines = document.getElementById("clipboard_tray_lines"); if (!clipboard_tray.classList.contains("hidden")) { - delete_all_children(tray_lines); + common.delete_all_children(tray_lines); var photo_ids = Array.from(photo_clipboard); photo_ids.sort(); for (var i = 0; i < photo_ids.length; i += 1) diff --git a/frontends/etiquette_flask/static/js/tag_autocomplete.js b/frontends/etiquette_flask/static/js/tag_autocomplete.js index adc8026..5df88cd 100644 --- a/frontends/etiquette_flask/static/js/tag_autocomplete.js +++ b/frontends/etiquette_flask/static/js/tag_autocomplete.js @@ -15,7 +15,7 @@ function init_datalist() document.body.appendChild(datalist); } - delete_all_children(datalist); + common.delete_all_children(datalist); for (var index = 0; index < tag_autocomplete.tagset["tags"].length; index += 1) { var option = document.createElement("option"); @@ -33,7 +33,7 @@ function init_datalist() tag_autocomplete.resolve = function resolve(tagname) { - tagname = normalize_tagname(tagname); + tagname = common.normalize_tagname(tagname); if (tag_autocomplete.tagset["tags"].indexOf(tagname) != -1) { return tagname; @@ -69,7 +69,7 @@ function update_tagset() { console.log("Updating known tagset."); var url = "/all_tags.json"; - get(url, tag_autocomplete.update_tagset_callback); + common.get(url, tag_autocomplete.update_tagset_callback); } function on_pageload() diff --git a/frontends/etiquette_flask/templates/album.html b/frontends/etiquette_flask/templates/album.html index 8c80a2b..72cffd9 100644 --- a/frontends/etiquette_flask/templates/album.html +++ b/frontends/etiquette_flask/templates/album.html @@ -145,7 +145,7 @@ function _paste_unpaste_photo_clipboard(add_or_remove) save_photo_clipboard(); location.reload(); }; - post(url, data, callback); + common.post(url, data, callback); } function paste_photo_clipboard() { @@ -201,7 +201,7 @@ function on_save(editor, edit_element_map, display_element_map) data.append("description", description_editor.value); editor.show_spinner(); - post(url, data, callback); + common.post(url, data, callback); } function on_cancel(editor, edit_element_map, display_element_map) diff --git a/frontends/etiquette_flask/templates/bookmarks.html b/frontends/etiquette_flask/templates/bookmarks.html index f2d1f21..9f01162 100644 --- a/frontends/etiquette_flask/templates/bookmarks.html +++ b/frontends/etiquette_flask/templates/bookmarks.html @@ -100,7 +100,7 @@ function create_bookmark(url, title) data.append("title", title); } var callback = function(){location.reload();}; - post(api_url, data, callback); + common.post(api_url, data, callback); } @@ -131,7 +131,7 @@ function on_save(editor, edit_element_map) var data = new FormData(); data.append("title", title_editor.value); data.append("url", url_editor.value); - post(url, data, callback); + common.post(url, data, callback); } on_cancel = undefined; diff --git a/frontends/etiquette_flask/templates/clipboard.html b/frontends/etiquette_flask/templates/clipboard.html index 7cb7c92..f226402 100644 --- a/frontends/etiquette_flask/templates/clipboard.html +++ b/frontends/etiquette_flask/templates/clipboard.html @@ -124,13 +124,13 @@ var holder = document.getElementById("photo_card_holder"); var add_box = document.getElementById("add_tag_textbox"); var add_button = document.getElementById("add_tag_button"); -add_box.addEventListener("keyup", entry_with_history_hook); -bind_box_to_button(add_box, add_button); +add_box.addEventListener("keyup", common.entry_with_history_hook); +common.bind_box_to_button(add_box, add_button); var remove_box = document.getElementById("remove_tag_textbox"); var remove_button = document.getElementById("remove_tag_button"); -remove_box.addEventListener("keyup", entry_with_history_hook); -bind_box_to_button(remove_box, remove_button); +remove_box.addEventListener("keyup", common.entry_with_history_hook); +common.bind_box_to_button(remove_box, remove_button); function recalculate_needed() { @@ -182,14 +182,14 @@ function request_more_divs() var holder = document.getElementById("photo_card_holder"); for (photo_id in response) { - photo_div = html_to_element(response[photo_id]); + photo_div = common.html_to_element(response[photo_id]); divs[photo_id] = photo_div; needed.delete(photo_id) holder.appendChild(photo_div); } apply_check_all(); } - post(url, data, callback); + common.post(url, data, callback); } function myhook() @@ -232,7 +232,7 @@ function submit_add_remove_tag(action, tagname, callback) var data = new FormData(); data.append("photo_ids", photo_ids); data.append("tagname", tagname); - post(url, data, callback); + common.post(url, data, callback); } function add_remove_callback(response) { @@ -257,7 +257,7 @@ function add_remove_callback(response) else if (action == "remove") {message_text = "Removed tag " + tagname;} } - create_message_bubble(message_area, message_positivity, message_text, 8000); + common.create_message_bubble(message_area, message_positivity, message_text, 8000); } var refresh_in_progress = false; @@ -274,7 +274,7 @@ function submit_refresh_metadata(callback) var data = new FormData(); data.append("photo_ids", photo_ids); refresh_in_progress = true; - post(url, data, callback); + common.post(url, data, callback); } function refresh_metadata_callback(response) { @@ -285,7 +285,7 @@ function refresh_metadata_callback(response) var message_area = document.getElementById("message_area"); var message_positivity = "message_negative"; var message_text = response["error_message"]; - create_message_bubble(message_area, message_positivity, message_text, 8000); + common.create_message_bubble(message_area, message_positivity, message_text, 8000); } else { @@ -309,7 +309,7 @@ function searchhidden_callback(response) message_positivity = "message_positive"; message_text = "Success." } - create_message_bubble(message_area, message_positivity, message_text, 8000); + common.create_message_bubble(message_area, message_positivity, message_text, 8000); } function submit_set_searchhidden(callback) { @@ -321,7 +321,7 @@ function submit_set_searchhidden(callback) var photo_ids = Array.from(photo_clipboard).join(","); data.append("photo_ids", photo_ids); - post(url, data, callback); + common.post(url, data, callback); } function submit_unset_searchhidden(callback) { @@ -333,7 +333,7 @@ function submit_unset_searchhidden(callback) var photo_ids = Array.from(photo_clipboard).join(","); data.append("photo_ids", photo_ids); - post(url, data, callback); + common.post(url, data, callback); } diff --git a/frontends/etiquette_flask/templates/login.html b/frontends/etiquette_flask/templates/login.html index f63f245..d7b3908 100644 --- a/frontends/etiquette_flask/templates/login.html +++ b/frontends/etiquette_flask/templates/login.html @@ -101,16 +101,16 @@ button var login_input_username = document.getElementById("login_input_username"); var login_input_password = document.getElementById("login_input_password"); var login_submit_button = document.getElementById("login_submit_button"); -bind_box_to_button(login_input_username, login_submit_button); -bind_box_to_button(login_input_password, login_submit_button); +common.bind_box_to_button(login_input_username, login_submit_button); +common.bind_box_to_button(login_input_password, login_submit_button); var register_input_username = document.getElementById("register_input_username"); var register_input_password_1 = document.getElementById("register_input_password_1"); var register_input_password_2 = document.getElementById("register_input_password_2"); var register_input_button = document.getElementById("register_input_button"); -bind_box_to_button(register_input_username, register_input_button); -bind_box_to_button(register_input_password_1, register_input_button); -bind_box_to_button(register_input_password_2, register_input_button); +common.bind_box_to_button(register_input_username, register_input_button); +common.bind_box_to_button(register_input_password_1, register_input_button); +common.bind_box_to_button(register_input_password_2, register_input_button); var message_area = document.getElementById("message_area"); @@ -120,14 +120,14 @@ function submit_login() var password = document.getElementById("login_input_password").value; if (username == "" || password == "") { - create_message_bubble(message_area, "message_negative", "Fill out the form, yo.", 8000); + common.create_message_bubble(message_area, "message_negative", "Fill out the form, yo.", 8000); return; } var url = "/login"; data = new FormData(); data.append("username", username); data.append("password", password); - return post(url, data, receive_callback); + return common.post(url, data, receive_callback); } function submit_register() { @@ -136,7 +136,7 @@ function submit_register() var password_2 = document.getElementById("register_input_password_2").value; if (username == "" || password_1 == "" || password_2 == "") { - create_message_bubble(message_area, "message_negative", "Fill out the form, yo.", 8000); + common.create_message_bubble(message_area, "message_negative", "Fill out the form, yo.", 8000); return; } var url = "/register"; @@ -144,14 +144,14 @@ function submit_register() data.append("username", username); data.append("password_1", password_1); data.append("password_2", password_2); - return post(url, data, receive_callback); + return common.post(url, data, receive_callback); } function receive_callback(response) { response = response["data"]; if ("error_type" in response) { - create_message_bubble(message_area, "message_negative", response["error_message"], 8000); + common.create_message_bubble(message_area, "message_negative", response["error_message"], 8000); } else { diff --git a/frontends/etiquette_flask/templates/photo.html b/frontends/etiquette_flask/templates/photo.html index b960dee..5c97359 100644 --- a/frontends/etiquette_flask/templates/photo.html +++ b/frontends/etiquette_flask/templates/photo.html @@ -260,8 +260,8 @@ var content_body = document.getElementById('content_body'); var add_tag_box = document.getElementById('add_tag_textbox'); var add_tag_button = document.getElementById('add_tag_button'); var message_area = document.getElementById('message_area'); -add_tag_box.addEventListener("keyup", entry_with_history_hook); -bind_box_to_button(add_tag_box, add_tag_button, false); +add_tag_box.addEventListener("keyup", common.entry_with_history_hook); +common.bind_box_to_button(add_tag_box, add_tag_button, false); photo_img_holder = document.getElementById("photo_img_holder"); photo_img = document.getElementById("photo_img"); @@ -272,7 +272,7 @@ function add_photo_tag(photoid, tagname, callback) var url = "/photo/" + photoid + "/add_tag"; var data = new FormData(); data.append("tagname", tagname); - return post(url, data, callback); + return common.post(url, data, callback); } function remove_photo_tag(photoid, tagname, callback) { @@ -280,7 +280,7 @@ function remove_photo_tag(photoid, tagname, callback) var url = "/photo/" + photoid + "/remove_tag"; var data = new FormData(); data.append("tagname", tagname); - return post(url, data, callback); + return common.post(url, data, callback); } function submit_tag(callback) { @@ -314,7 +314,7 @@ function receive_callback(response) return; } } - create_message_bubble(message_area, message_positivity, message_text, 8000); + common.create_message_bubble(message_area, message_positivity, message_text, 8000); } function refresh_metadata(photoid) @@ -322,7 +322,7 @@ function refresh_metadata(photoid) var url= "/photo/" + photoid + "/refresh_metadata"; var data = new FormData(); var callback = function(){location.reload();}; - post(url, data, callback); + common.post(url, data, callback); } var ZOOM_BG_URL = "url('{{photo|file_link}}')"; diff --git a/frontends/etiquette_flask/templates/search.html b/frontends/etiquette_flask/templates/search.html index 6e60293..22c53d3 100644 --- a/frontends/etiquette_flask/templates/search.html +++ b/frontends/etiquette_flask/templates/search.html @@ -598,6 +598,6 @@ for (var index = 0; index < found_on_page.length; index += 1) input_musts.addEventListener("keyup", function(){tag_input_hook(this, inputted_musts, "search_builder_musts_inputted")}); input_mays.addEventListener("keyup", function(){tag_input_hook(this, inputted_mays, "search_builder_mays_inputted")}); input_forbids.addEventListener("keyup", function(){tag_input_hook(this, inputted_forbids, "search_builder_forbids_inputted")}); -bind_box_to_button(input_expression, document.getElementById("search_go_button")); +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 b3a86b2..23f1211 100644 --- a/frontends/etiquette_flask/templates/tags.html +++ b/frontends/etiquette_flask/templates/tags.html @@ -162,8 +162,8 @@ body var box = document.getElementById('add_tag_textbox'); var button = document.getElementById('add_tag_button'); var message_area = document.getElementById('message_area'); -box.addEventListener("keyup", entry_with_history_hook); -bind_box_to_button(box, button, false); +box.addEventListener("keyup", common.entry_with_history_hook); +common.bind_box_to_button(box, button, false); function submit_tag(callback) { @@ -176,7 +176,7 @@ function edit_tags(action, tagname, callback) var url = "/tags/" + action; data = new FormData(); data.append("tagname", tagname); - return post(url, data, callback); + return common.post(url, data, callback); } function delete_tag_synonym(tagname, callback) { @@ -236,7 +236,7 @@ function receive_callback(response) {message_text = "Unlinked tags " + tagname;} } - create_message_bubble(message_area, message_positivity, message_text, 8000); + common.create_message_bubble(message_area, message_positivity, message_text, 8000); } } @@ -279,7 +279,7 @@ function on_save(editor, edit_element_map, display_element_map) var data = new FormData(); data.append("name", name_editor.value); data.append("description", description_editor.value); - post(url, data, callback); + common.post(url, data, callback); } function on_cancel(editor, edit_element_map, display_element_map)