aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMelonai <einebeere@gmail.com>2020-05-10 01:46:29 +0200
committerMelonai <einebeere@gmail.com>2020-05-10 01:46:29 +0200
commita00a8a867cae381982c7b8b77f07836ab4a504ed (patch)
treef36ded8eedce38fccda418e689b11fbe0b3a45dd
parent9e48090f0b25e7104e1aee86bbd6a04fcdcbb250 (diff)
downloadshorest-a00a8a867cae381982c7b8b77f07836ab4a504ed.tar.zst
shorest-a00a8a867cae381982c7b8b77f07836ab4a504ed.zip
js restructure
-rw-r--r--Cargo.toml2
-rw-r--r--client/static/main.js113
2 files changed, 54 insertions, 61 deletions
diff --git a/Cargo.toml b/Cargo.toml
index db5fa3f..6e44c76 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "shorest"
-version = "0.1.0"
+version = "0.1.1"
authors = ["Melonai <einebeere@gmail.com>"]
edition = "2018"
diff --git a/client/static/main.js b/client/static/main.js
index cd7c922..5c8f452 100644
--- a/client/static/main.js
+++ b/client/static/main.js
@@ -1,76 +1,69 @@
$(document).ready(function() {
- const form = $('#form');
- form.on('submit', onFormSubmit);
- form.attr("novalidate",true);
- $('#url').on({'input': inputUpdate, 'paste': pasteTrim});
- inputUpdate();
-});
+ const FORM = $('#form');
+ const URL_FIELD = $('#url');
-function onFormSubmit() {
- const urlField = $('#url');
- if (validateURL(urlField.val())) {
- const data = JSON.stringify({'url': 'https://' + urlField.val()});
- $.ajax('/', {method: 'POST', data: data, contentType: 'application/json'}).then(onSuccess);
- }
- return false;
-}
+ FORM.on('submit', onFormSubmit);
+ FORM.attr("novalidate",true);
+ URL_FIELD.on({'input': inputUpdate, 'paste': pasteTrim});
-function onSuccess(response) {
- const responseDiv = $('#response-template')[0].content.querySelector('div');
- const node = document.importNode(responseDiv, true);
- const urlField = $('#url');
- let text;
- if (urlField.val().length < 20 ) {
- text = 'The short link for <strong>' + urlField.val() + '</strong> is<br><strong>sho.rest/' + response.hash + '</strong>';
- } else {
- text = 'The short link for your URL is<br><strong>sho.rest/' + response.hash + '</strong>';
+ function onFormSubmit() {
+ if (validateURL(URL_FIELD.val())) {
+ const data = JSON.stringify({'url': 'https://' + URL_FIELD.val()});
+ $.ajax('/', {method: 'POST', data: data, contentType: 'application/json'}).then(onSuccess);
+ }
+ return false;
}
- node.querySelector('.response-text').innerHTML = text;
- $(node).find('.copy-text').on('click', copyClick);
- $('#responses')[0].prepend(node);
-}
-function inputUpdate() {
- const userInput = $('#url').val();
- setButtonVisible(validateURL(userInput));
-}
+ function onSuccess(response) {
+ const responseDiv = $('#response-template')[0].content.querySelector('div');
+ const node = document.importNode(responseDiv, true);
+ let text;
+ if (URL_FIELD.val().length < 20 ) {
+ text = 'The short link for <strong>' + URL_FIELD.val() + '</strong> is<br><strong>sho.rest/' + response.hash + '</strong>';
+ } else {
+ text = 'The short link for your URL is<br><strong>sho.rest/' + response.hash + '</strong>';
+ }
+ node.querySelector('.response-text').innerHTML = text;
+ $(node).find('.copy-text').on('click', copyClick);
+ $('#responses')[0].prepend(node);
+ }
-function setButtonVisible(visible) {
- const form = $('#form');
+ function inputUpdate() {
+ const visible = validateURL(URL_FIELD.val())
+ if (!FORM[0].hasAttribute('disabled') === visible) return;
- if (!form[0].hasAttribute('disabled') === visible) return;
+ const valuesDisabled = {borderColor: '#FFBCBC', borderRight: 'none', buttonValue: '', buttonValueColor: '#FFFFFF'};
+ const valuesEnabled = {borderColor: '#E0E0E0', borderRight: '', buttonValue: '→', buttonValueColor: '#727272'};
- const valuesDisabled = {borderColor: '#FFBCBC', borderRight: 'none', buttonValue: '', buttonValueColor: '#FFFFFF'};
- const valuesEnabled = {borderColor: '#E0E0E0', borderRight: '', buttonValue: '→', buttonValueColor: '#727272'};
+ const btn = $('#btn');
+ const left = $('#left');
+ const formGroup = $('#form-group');
- const btn = $('#btn');
- const left = $('#left');
- const formGroup = $('#form-group');
+ let values;
+ if (visible) {
+ values = valuesEnabled;
+ FORM.removeAttr('disabled');
+ } else {
+ values = valuesDisabled;
+ FORM[0].setAttribute('disabled', '');
+ }
- let values;
- if (visible) {
- values = valuesEnabled;
- form.removeAttr('disabled');
- } else {
- values = valuesDisabled;
- form[0].setAttribute('disabled', '');
+ formGroup.css('border-color', values.borderColor);
+ left.css('border-right', values.borderRight);
+ btn.css('color', values.buttonValueColor);
+ btn.val(values.buttonValue);
}
- formGroup.css('border-color', values.borderColor);
- left.css('border-right', values.borderRight);
- btn.css('color', values.buttonValueColor);
- btn.val(values.buttonValue);
-
-}
+ function pasteTrim() {
+ const pattern = /^https?:\/\//;
+ setTimeout(() => {
+ URL_FIELD.val(URL_FIELD.val().replace(pattern, ''));
+ inputUpdate();
+ }, 0);
+ }
-function pasteTrim() {
- const pattern = /^https?:\/\//;
- setTimeout(() => {
- const element = $('#url');
- element.val(element.val().replace(pattern, ''));
- inputUpdate();
- }, 0);
-}
+ inputUpdate();
+});
function copyClick(event) {
const target = $(event.target);