mirror of
https://github.com/hrfee/jfa-go.git
synced 2025-01-05 07:50:10 +00:00
move all shared typescript to common.ts
This commit is contained in:
parent
9abb177427
commit
2d6b1717db
@ -465,7 +465,7 @@
|
|||||||
<p>{{ .contactMessage }}</p>
|
<p>{{ .contactMessage }}</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<script src="serialize.js"></script>
|
<script src="common.js"></script>
|
||||||
<script>
|
<script>
|
||||||
var availableProfiles = [];
|
var availableProfiles = [];
|
||||||
{{ if .notifications }}
|
{{ if .notifications }}
|
||||||
|
@ -114,7 +114,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<script src="serialize.js"></script>
|
<script src="common.js"></script>
|
||||||
<script>
|
<script>
|
||||||
var usernameEnabled = {{ .username }}
|
var usernameEnabled = {{ .username }}
|
||||||
var validationStrings = {
|
var validationStrings = {
|
||||||
|
39
ts/admin.ts
39
ts/admin.ts
@ -1,45 +1,6 @@
|
|||||||
interface Window {
|
|
||||||
token: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set in admin.html
|
// Set in admin.html
|
||||||
var cssFile: string;
|
var cssFile: string;
|
||||||
|
|
||||||
const _get = (url: string, data: Object, onreadystatechange: () => void): void => {
|
|
||||||
let req = new XMLHttpRequest();
|
|
||||||
req.open("GET", url, true);
|
|
||||||
req.responseType = 'json';
|
|
||||||
req.setRequestHeader("Authorization", "Basic " + btoa(window.token + ":"));
|
|
||||||
req.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
|
||||||
req.onreadystatechange = onreadystatechange;
|
|
||||||
req.send(JSON.stringify(data));
|
|
||||||
};
|
|
||||||
|
|
||||||
const _post = (url: string, data: Object, onreadystatechange: () => void): void => {
|
|
||||||
let req = new XMLHttpRequest();
|
|
||||||
req.open("POST", url, true);
|
|
||||||
req.setRequestHeader("Authorization", "Basic " + btoa(window.token + ":"));
|
|
||||||
req.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
|
||||||
req.onreadystatechange = onreadystatechange;
|
|
||||||
req.send(JSON.stringify(data));
|
|
||||||
};
|
|
||||||
|
|
||||||
function _delete(url: string, data: Object, onreadystatechange: () => void): void {
|
|
||||||
let req = new XMLHttpRequest();
|
|
||||||
req.open("DELETE", url, true);
|
|
||||||
req.setRequestHeader("Authorization", "Basic " + btoa(window.token + ":"));
|
|
||||||
req.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
|
||||||
req.onreadystatechange = onreadystatechange;
|
|
||||||
req.send(JSON.stringify(data));
|
|
||||||
}
|
|
||||||
|
|
||||||
const rmAttr = (el: HTMLElement, attr: string): void => {
|
|
||||||
if (el.classList.contains(attr)) {
|
|
||||||
el.classList.remove(attr);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
const addAttr = (el: HTMLElement, attr: string): void => el.classList.add(attr);
|
|
||||||
|
|
||||||
const Focus = (el: HTMLElement): void => rmAttr(el, 'unfocused');
|
const Focus = (el: HTMLElement): void => rmAttr(el, 'unfocused');
|
||||||
const Unfocus = (el: HTMLElement): void => addAttr(el, 'unfocused');
|
const Unfocus = (el: HTMLElement): void => addAttr(el, 'unfocused');
|
||||||
|
|
||||||
|
79
ts/common.ts
Normal file
79
ts/common.ts
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
interface Window {
|
||||||
|
token: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
function serializeForm(id: string): Object {
|
||||||
|
const form = document.getElementById(id) as HTMLFormElement;
|
||||||
|
let formData = {};
|
||||||
|
for (let i = 0; i < form.elements.length; i++) {
|
||||||
|
const el = form.elements[i];
|
||||||
|
if ((el as HTMLInputElement).type == "submit") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let name = (el as HTMLInputElement).name;
|
||||||
|
if (!name) {
|
||||||
|
name = el.id;
|
||||||
|
}
|
||||||
|
switch ((el as HTMLInputElement).type) {
|
||||||
|
case "checkbox":
|
||||||
|
formData[name] = (el as HTMLInputElement).checked;
|
||||||
|
break;
|
||||||
|
case "text":
|
||||||
|
case "password":
|
||||||
|
case "email":
|
||||||
|
case "number":
|
||||||
|
formData[name] = (el as HTMLInputElement).value;
|
||||||
|
break;
|
||||||
|
case "select-one":
|
||||||
|
case "select":
|
||||||
|
let val: string = (el as HTMLSelectElement).value.toString();
|
||||||
|
if (!isNaN(val as any)) {
|
||||||
|
formData[name] = +val;
|
||||||
|
} else {
|
||||||
|
formData[name] = val;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return formData;
|
||||||
|
}
|
||||||
|
|
||||||
|
const rmAttr = (el: HTMLElement, attr: string): void => {
|
||||||
|
if (el.classList.contains(attr)) {
|
||||||
|
el.classList.remove(attr);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const addAttr = (el: HTMLElement, attr: string): void => el.classList.add(attr);
|
||||||
|
|
||||||
|
const _get = (url: string, data: Object, onreadystatechange: () => void): void => {
|
||||||
|
let req = new XMLHttpRequest();
|
||||||
|
req.open("GET", url, true);
|
||||||
|
req.responseType = 'json';
|
||||||
|
req.setRequestHeader("Authorization", "Basic " + btoa(window.token + ":"));
|
||||||
|
req.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
||||||
|
req.onreadystatechange = onreadystatechange;
|
||||||
|
req.send(JSON.stringify(data));
|
||||||
|
};
|
||||||
|
|
||||||
|
const _post = (url: string, data: Object, onreadystatechange: () => void, response?: boolean): void => {
|
||||||
|
let req = new XMLHttpRequest();
|
||||||
|
req.open("POST", url, true);
|
||||||
|
if (response) {
|
||||||
|
req.responseType = 'json';
|
||||||
|
}
|
||||||
|
req.setRequestHeader("Authorization", "Basic " + btoa(window.token + ":"));
|
||||||
|
req.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
||||||
|
req.onreadystatechange = onreadystatechange;
|
||||||
|
req.send(JSON.stringify(data));
|
||||||
|
};
|
||||||
|
|
||||||
|
function _delete(url: string, data: Object, onreadystatechange: () => void): void {
|
||||||
|
let req = new XMLHttpRequest();
|
||||||
|
req.open("DELETE", url, true);
|
||||||
|
req.setRequestHeader("Authorization", "Basic " + btoa(window.token + ":"));
|
||||||
|
req.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
||||||
|
req.onreadystatechange = onreadystatechange;
|
||||||
|
req.send(JSON.stringify(data));
|
||||||
|
}
|
||||||
|
|
57
ts/form.ts
57
ts/form.ts
@ -7,35 +7,6 @@ interface pwValStrings {
|
|||||||
length, uppercase, lowercase, number, special: pwValString;
|
length, uppercase, lowercase, number, special: pwValString;
|
||||||
}
|
}
|
||||||
|
|
||||||
const _post = (url: string, data: Object, onreadystatechange: () => void): void => {
|
|
||||||
let req = new XMLHttpRequest();
|
|
||||||
req.open("POST", url, true);
|
|
||||||
req.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
|
||||||
req.responseType = 'json';
|
|
||||||
req.onreadystatechange = onreadystatechange;
|
|
||||||
req.send(JSON.stringify(data));
|
|
||||||
};
|
|
||||||
|
|
||||||
const toggleSpinner = (): void => {
|
|
||||||
const submitButton = document.getElementById('submitButton') as HTMLButtonElement;
|
|
||||||
if (document.getElementById('createAccountSpinner')) {
|
|
||||||
submitButton.innerHTML = `<span>Create Account</span>`;
|
|
||||||
submitButton.disabled = false;
|
|
||||||
} else {
|
|
||||||
submitButton.innerHTML = `
|
|
||||||
<span id="createAccountSpinner" class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>Creating...
|
|
||||||
`;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const rmAttr = (el: HTMLElement, attr: string): void => {
|
|
||||||
if (el.classList.contains(attr)) {
|
|
||||||
el.classList.remove(attr);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const addAttr = (el: HTMLElement, attr: string): void => el.classList.add(attr);
|
|
||||||
|
|
||||||
var validationStrings: pwValStrings;
|
var validationStrings: pwValStrings;
|
||||||
var bsVersion: number;
|
var bsVersion: number;
|
||||||
|
|
||||||
@ -62,8 +33,19 @@ var defaultPwValStrings: pwValStrings = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const toggleSpinner = (): void => {
|
||||||
|
const submitButton = document.getElementById('submitButton') as HTMLButtonElement;
|
||||||
|
if (document.getElementById('createAccountSpinner')) {
|
||||||
|
submitButton.innerHTML = `<span>Create Account</span>`;
|
||||||
|
submitButton.disabled = false;
|
||||||
|
} else {
|
||||||
|
submitButton.innerHTML = `
|
||||||
|
<span id="createAccountSpinner" class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>Creating...
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
for (let key in validationStrings) {
|
for (let key in validationStrings) {
|
||||||
console.log(key);
|
|
||||||
if (validationStrings[key].singular == "" || !(validationStrings[key].plural.includes("{n}"))) {
|
if (validationStrings[key].singular == "" || !(validationStrings[key].plural.includes("{n}"))) {
|
||||||
validationStrings[key].singular = defaultPwValStrings[key].singular;
|
validationStrings[key].singular = defaultPwValStrings[key].singular;
|
||||||
}
|
}
|
||||||
@ -154,19 +136,6 @@ var usernameEnabled: boolean;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
}, true);
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,35 +0,0 @@
|
|||||||
function serializeForm(id: string): Object {
|
|
||||||
const form = document.getElementById(id) as HTMLFormElement;
|
|
||||||
let formData = {};
|
|
||||||
for (let i = 0; i < form.elements.length; i++) {
|
|
||||||
const el = form.elements[i];
|
|
||||||
if ((el as HTMLInputElement).type == "submit") {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let name = (el as HTMLInputElement).name;
|
|
||||||
if (!name) {
|
|
||||||
name = el.id;
|
|
||||||
}
|
|
||||||
switch ((el as HTMLInputElement).type) {
|
|
||||||
case "checkbox":
|
|
||||||
formData[name] = (el as HTMLInputElement).checked;
|
|
||||||
break;
|
|
||||||
case "text":
|
|
||||||
case "password":
|
|
||||||
case "email":
|
|
||||||
case "number":
|
|
||||||
formData[name] = (el as HTMLInputElement).value;
|
|
||||||
break;
|
|
||||||
case "select-one":
|
|
||||||
case "select":
|
|
||||||
let val: string = (el as HTMLSelectElement).value.toString();
|
|
||||||
if (!isNaN(val as any)) {
|
|
||||||
formData[name] = +val;
|
|
||||||
} else {
|
|
||||||
formData[name] = val;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return formData;
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user