mCaptcha/templates/panel/sitekey/add/addLevelButton.ts

118 lines
3.5 KiB
TypeScript
Raw Normal View History

2021-05-01 12:11:22 +03:00
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2021-05-03 17:54:03 +03:00
import validateLevel from './levels/validateLevel';
import getNumLevels from './levels/getNumLevels';
import {LEVELS} from './levels/';
2021-05-03 17:54:03 +03:00
import * as UpdateLevel from './levels/updateLevel';
import {
getRemoveButtonHTML,
addRemoveLevelButtonEventListener,
} from './removeLevelButton';
import CONST from './const';
2021-05-01 12:11:22 +03:00
2021-05-03 17:54:03 +03:00
const ADD_LEVEL_BUTTON = 'sitekey-form__level-add-level-button';
2021-05-01 16:52:44 +03:00
2021-05-03 17:54:03 +03:00
/**
* Gets executed when 'Add' Button is clicked to add levels
* Used to validate levels per m_captcha::defense::Defense's
* specifications
*/
const addLevel = (e: Event) => {
const eventTarget = <HTMLElement>e.target;
const PARENT = <HTMLLabelElement>eventTarget.parentElement;
2021-05-03 17:54:03 +03:00
const FIELDSET = <HTMLElement>PARENT.parentElement;
const onScreenLevel = getNumLevels();
2021-05-01 16:52:44 +03:00
const isValid = validateLevel(onScreenLevel);
2021-05-01 16:52:44 +03:00
console.log(`[addLevelButton] isValid: ${isValid}`);
if (!isValid) {
2021-05-03 17:54:03 +03:00
return console.error('Aborting level addition');
2021-05-01 16:52:44 +03:00
}
eventTarget.remove();
PARENT.innerHTML = getRemoveButtonHTML(onScreenLevel);
PARENT.htmlFor = `${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${onScreenLevel}`;
//FIELDSET.innerHTML += getRemoveButtonHTML(numLevels);
addRemoveLevelButtonEventListener(onScreenLevel);
2021-05-01 16:52:44 +03:00
//PARENT.remove();
const newLevelHTML = getHtml(onScreenLevel + 1);
2021-05-03 17:54:03 +03:00
FIELDSET.insertAdjacentHTML('afterend', newLevelHTML);
UpdateLevel.register(onScreenLevel);
2021-05-01 12:11:22 +03:00
addLevelButtonAddEventListener();
};
2021-05-03 17:54:03 +03:00
/** adds onclick event listener */
const addLevelButtonAddEventListener = () => {
2021-05-01 18:57:02 +03:00
let addLevelButton = <HTMLElement>(
document.querySelector(`.${ADD_LEVEL_BUTTON}`)
);
2021-05-03 17:54:03 +03:00
addLevelButton.addEventListener('click', addLevel);
2021-05-01 12:11:22 +03:00
};
2021-05-03 17:54:03 +03:00
/**
* Generate HTML to be added when 'Add Level' button is clicked
* Check if './add-level.html` to see if this is up to date
*/
const getHtml = (level: number) => {
console.debug(`[generating HTML getHtml]level: ${level}`);
2021-05-03 17:54:03 +03:00
const HTML = `
<fieldset class="sitekey__level-container" id="level-group-${level}">
2021-05-03 17:54:03 +03:00
<legend class="sitekey__level-title">
Level ${level}
</legend>
<label class="sitekey-form__level-label" for="visitor${level}"
>Visitor
<input
class="sitekey-form__level-input"
type="number"
name=""
value=""
id="visitor${level}"
/>
</label>
<label class="sitekey-form__level-label" for="difficulty${level}">
Difficulty
<input
type="number"
name="difficulty"
class="sitekey-form__level-input"
value=""
id="difficulty${level}"
/>
</label>
<label class="sitekey-form__level-label--hidden" for="add">
Add level
<input
class="sitekey-form__level-add-level-button"
type="button"
name="add"
id="add"
value="Add"
/>
</label>
</fieldset>
`;
return HTML;
};
2021-05-01 12:11:22 +03:00
2021-05-03 17:54:03 +03:00
export default addLevelButtonAddEventListener;