2019-09-10 00:19:49 +00:00
|
|
|
/*
|
2020-04-10 18:06:55 +00:00
|
|
|
* Copyright (C) 2019-2020 Yomichan Authors
|
2019-09-10 00:19:49 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2020-01-01 17:00:31 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-09-10 00:19:49 +00:00
|
|
|
*/
|
|
|
|
|
2020-03-11 02:30:36 +00:00
|
|
|
/* global
|
2020-05-03 01:39:24 +00:00
|
|
|
* DOM
|
2020-03-11 02:30:36 +00:00
|
|
|
* conditionsNormalizeOptionValue
|
|
|
|
*/
|
2019-09-10 00:19:49 +00:00
|
|
|
|
|
|
|
class ConditionsUI {
|
|
|
|
static instantiateTemplate(templateSelector) {
|
|
|
|
const template = document.querySelector(templateSelector);
|
|
|
|
const content = document.importNode(template.content, true);
|
|
|
|
return $(content.firstChild);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ConditionsUI.Container = class Container {
|
|
|
|
constructor(conditionDescriptors, conditionNameDefault, conditionGroups, container, addButton) {
|
|
|
|
this.children = [];
|
|
|
|
this.conditionDescriptors = conditionDescriptors;
|
|
|
|
this.conditionNameDefault = conditionNameDefault;
|
|
|
|
this.conditionGroups = conditionGroups;
|
|
|
|
this.container = container;
|
|
|
|
this.addButton = addButton;
|
|
|
|
|
|
|
|
this.container.empty();
|
|
|
|
|
2019-09-29 19:26:46 +00:00
|
|
|
for (const conditionGroup of toIterable(conditionGroups)) {
|
2019-09-10 00:19:49 +00:00
|
|
|
this.children.push(new ConditionsUI.ConditionGroup(this, conditionGroup));
|
|
|
|
}
|
|
|
|
|
2020-02-27 02:01:40 +00:00
|
|
|
this.addButton.on('click', this.onAddConditionGroup.bind(this));
|
2019-09-10 00:19:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup() {
|
|
|
|
for (const child of this.children) {
|
|
|
|
child.cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.addButton.off('click');
|
|
|
|
this.container.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
|
|
|
// Override
|
|
|
|
}
|
|
|
|
|
2019-09-18 01:23:21 +00:00
|
|
|
isolate(object) {
|
|
|
|
// Override
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2019-09-10 00:19:49 +00:00
|
|
|
remove(child) {
|
|
|
|
const index = this.children.indexOf(child);
|
|
|
|
if (index < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
child.cleanup();
|
|
|
|
this.children.splice(index, 1);
|
|
|
|
this.conditionGroups.splice(index, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
onAddConditionGroup() {
|
2019-09-18 01:23:21 +00:00
|
|
|
const conditionGroup = this.isolate({
|
2019-09-10 00:19:49 +00:00
|
|
|
conditions: [this.createDefaultCondition(this.conditionNameDefault)]
|
2019-09-18 01:23:21 +00:00
|
|
|
});
|
2019-09-10 00:19:49 +00:00
|
|
|
this.conditionGroups.push(conditionGroup);
|
|
|
|
this.save();
|
|
|
|
this.children.push(new ConditionsUI.ConditionGroup(this, conditionGroup));
|
|
|
|
}
|
|
|
|
|
|
|
|
createDefaultCondition(type) {
|
|
|
|
let operator = '';
|
|
|
|
let value = '';
|
2019-11-25 19:19:18 +00:00
|
|
|
if (hasOwn(this.conditionDescriptors, type)) {
|
2019-09-10 00:19:49 +00:00
|
|
|
const conditionDescriptor = this.conditionDescriptors[type];
|
|
|
|
operator = conditionDescriptor.defaultOperator;
|
|
|
|
({value} = this.getOperatorDefaultValue(type, operator));
|
|
|
|
if (typeof value === 'undefined') {
|
|
|
|
value = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {type, operator, value};
|
|
|
|
}
|
|
|
|
|
|
|
|
getOperatorDefaultValue(type, operator) {
|
2019-11-25 19:19:18 +00:00
|
|
|
if (hasOwn(this.conditionDescriptors, type)) {
|
2019-09-10 00:19:49 +00:00
|
|
|
const conditionDescriptor = this.conditionDescriptors[type];
|
2019-11-25 19:19:18 +00:00
|
|
|
if (hasOwn(conditionDescriptor.operators, operator)) {
|
2019-09-10 00:19:49 +00:00
|
|
|
const operatorDescriptor = conditionDescriptor.operators[operator];
|
2019-11-25 19:19:18 +00:00
|
|
|
if (hasOwn(operatorDescriptor, 'defaultValue')) {
|
2020-05-06 16:44:56 +00:00
|
|
|
return {value: this.isolate(operatorDescriptor.defaultValue), fromOperator: true};
|
2019-09-10 00:19:49 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-25 19:19:18 +00:00
|
|
|
if (hasOwn(conditionDescriptor, 'defaultValue')) {
|
2020-05-06 16:44:56 +00:00
|
|
|
return {value: this.isolate(conditionDescriptor.defaultValue), fromOperator: false};
|
2019-09-10 00:19:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return {fromOperator: false};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ConditionsUI.ConditionGroup = class ConditionGroup {
|
|
|
|
constructor(parent, conditionGroup) {
|
|
|
|
this.parent = parent;
|
|
|
|
this.children = [];
|
|
|
|
this.conditionGroup = conditionGroup;
|
|
|
|
this.container = $('<div>').addClass('condition-group').appendTo(parent.container);
|
|
|
|
this.options = ConditionsUI.instantiateTemplate('#condition-group-options-template').appendTo(parent.container);
|
|
|
|
this.separator = ConditionsUI.instantiateTemplate('#condition-group-separator-template').appendTo(parent.container);
|
|
|
|
this.addButton = this.options.find('.condition-add');
|
|
|
|
|
2019-09-29 19:26:46 +00:00
|
|
|
for (const condition of toIterable(conditionGroup.conditions)) {
|
2019-09-10 00:19:49 +00:00
|
|
|
this.children.push(new ConditionsUI.Condition(this, condition));
|
|
|
|
}
|
|
|
|
|
2020-02-27 02:01:40 +00:00
|
|
|
this.addButton.on('click', this.onAddCondition.bind(this));
|
2019-09-10 00:19:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup() {
|
|
|
|
for (const child of this.children) {
|
|
|
|
child.cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.addButton.off('click');
|
|
|
|
this.container.remove();
|
|
|
|
this.options.remove();
|
|
|
|
this.separator.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
|
|
|
this.parent.save();
|
|
|
|
}
|
|
|
|
|
2019-09-18 01:23:21 +00:00
|
|
|
isolate(object) {
|
|
|
|
return this.parent.isolate(object);
|
|
|
|
}
|
|
|
|
|
2019-09-10 00:19:49 +00:00
|
|
|
remove(child) {
|
|
|
|
const index = this.children.indexOf(child);
|
|
|
|
if (index < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
child.cleanup();
|
|
|
|
this.children.splice(index, 1);
|
|
|
|
this.conditionGroup.conditions.splice(index, 1);
|
|
|
|
|
|
|
|
if (this.children.length === 0) {
|
|
|
|
this.parent.remove(this, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onAddCondition() {
|
2019-09-18 01:23:21 +00:00
|
|
|
const condition = this.isolate(this.parent.createDefaultCondition(this.parent.conditionNameDefault));
|
2019-09-10 00:19:49 +00:00
|
|
|
this.conditionGroup.conditions.push(condition);
|
|
|
|
this.children.push(new ConditionsUI.Condition(this, condition));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ConditionsUI.Condition = class Condition {
|
|
|
|
constructor(parent, condition) {
|
|
|
|
this.parent = parent;
|
|
|
|
this.condition = condition;
|
|
|
|
this.container = ConditionsUI.instantiateTemplate('#condition-template').appendTo(parent.container);
|
2020-05-03 01:39:24 +00:00
|
|
|
this.input = this.container.find('.condition-input');
|
|
|
|
this.inputInner = null;
|
2019-09-10 00:19:49 +00:00
|
|
|
this.typeSelect = this.container.find('.condition-type');
|
|
|
|
this.operatorSelect = this.container.find('.condition-operator');
|
|
|
|
this.removeButton = this.container.find('.condition-remove');
|
|
|
|
|
|
|
|
this.updateTypes();
|
|
|
|
this.updateOperators();
|
|
|
|
this.updateInput();
|
|
|
|
|
2020-02-27 02:01:40 +00:00
|
|
|
this.typeSelect.on('change', this.onConditionTypeChanged.bind(this));
|
|
|
|
this.operatorSelect.on('change', this.onConditionOperatorChanged.bind(this));
|
|
|
|
this.removeButton.on('click', this.onRemoveClicked.bind(this));
|
2019-09-10 00:19:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup() {
|
2020-05-03 01:39:24 +00:00
|
|
|
this.inputInner.off('change');
|
2019-09-10 00:19:49 +00:00
|
|
|
this.typeSelect.off('change');
|
|
|
|
this.operatorSelect.off('change');
|
|
|
|
this.removeButton.off('click');
|
|
|
|
this.container.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
|
|
|
this.parent.save();
|
|
|
|
}
|
|
|
|
|
2020-05-06 16:44:56 +00:00
|
|
|
isolate(object) {
|
|
|
|
return this.parent.isolate(object);
|
|
|
|
}
|
|
|
|
|
2019-09-10 00:19:49 +00:00
|
|
|
updateTypes() {
|
|
|
|
const conditionDescriptors = this.parent.parent.conditionDescriptors;
|
|
|
|
const optionGroup = this.typeSelect.find('optgroup');
|
|
|
|
optionGroup.empty();
|
|
|
|
for (const type of Object.keys(conditionDescriptors)) {
|
|
|
|
const conditionDescriptor = conditionDescriptors[type];
|
|
|
|
$('<option>').val(type).text(conditionDescriptor.name).appendTo(optionGroup);
|
|
|
|
}
|
|
|
|
this.typeSelect.val(this.condition.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateOperators() {
|
|
|
|
const conditionDescriptors = this.parent.parent.conditionDescriptors;
|
|
|
|
const optionGroup = this.operatorSelect.find('optgroup');
|
|
|
|
optionGroup.empty();
|
|
|
|
|
|
|
|
const type = this.condition.type;
|
2019-11-25 19:19:18 +00:00
|
|
|
if (hasOwn(conditionDescriptors, type)) {
|
2019-09-10 00:19:49 +00:00
|
|
|
const conditionDescriptor = conditionDescriptors[type];
|
|
|
|
const operators = conditionDescriptor.operators;
|
|
|
|
for (const operatorName of Object.keys(operators)) {
|
|
|
|
const operatorDescriptor = operators[operatorName];
|
|
|
|
$('<option>').val(operatorName).text(operatorDescriptor.name).appendTo(optionGroup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.operatorSelect.val(this.condition.operator);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateInput() {
|
|
|
|
const conditionDescriptors = this.parent.parent.conditionDescriptors;
|
|
|
|
const {type, operator} = this.condition;
|
|
|
|
|
|
|
|
const objects = [];
|
2020-05-03 01:39:24 +00:00
|
|
|
let inputType = null;
|
2019-11-25 19:19:18 +00:00
|
|
|
if (hasOwn(conditionDescriptors, type)) {
|
2019-09-10 00:19:49 +00:00
|
|
|
const conditionDescriptor = conditionDescriptors[type];
|
|
|
|
objects.push(conditionDescriptor);
|
2020-05-03 01:39:24 +00:00
|
|
|
if (hasOwn(conditionDescriptor, 'type')) {
|
|
|
|
inputType = conditionDescriptor.type;
|
|
|
|
}
|
2019-11-25 19:19:18 +00:00
|
|
|
if (hasOwn(conditionDescriptor.operators, operator)) {
|
2019-09-10 00:19:49 +00:00
|
|
|
const operatorDescriptor = conditionDescriptor.operators[operator];
|
|
|
|
objects.push(operatorDescriptor);
|
2020-05-03 01:39:24 +00:00
|
|
|
if (hasOwn(operatorDescriptor, 'type')) {
|
|
|
|
inputType = operatorDescriptor.type;
|
|
|
|
}
|
2019-09-10 00:19:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 01:39:24 +00:00
|
|
|
this.input.empty();
|
|
|
|
if (inputType === 'select') {
|
|
|
|
this.inputInner = this.createSelectElement(objects);
|
|
|
|
} else if (inputType === 'keyMulti') {
|
|
|
|
this.inputInner = this.createInputKeyMultiElement(objects);
|
|
|
|
} else {
|
|
|
|
this.inputInner = this.createInputElement(objects);
|
|
|
|
}
|
|
|
|
this.inputInner.appendTo(this.input);
|
|
|
|
this.inputInner.on('change', this.onInputChanged.bind(this));
|
|
|
|
|
2020-05-06 16:44:56 +00:00
|
|
|
const {valid, value} = this.validateValue(this.condition.value);
|
2020-05-03 01:39:24 +00:00
|
|
|
this.inputInner.toggleClass('is-invalid', !valid);
|
2020-05-06 16:44:56 +00:00
|
|
|
this.inputInner.val(value);
|
2020-05-03 01:39:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
createInputElement(objects) {
|
|
|
|
const inputInner = ConditionsUI.instantiateTemplate('#condition-input-text-template');
|
|
|
|
|
|
|
|
const props = new Map([
|
|
|
|
['placeholder', ''],
|
|
|
|
['type', 'text']
|
|
|
|
]);
|
|
|
|
|
2019-09-10 00:19:49 +00:00
|
|
|
for (const object of objects) {
|
2019-11-25 19:19:18 +00:00
|
|
|
if (hasOwn(object, 'placeholder')) {
|
2020-02-26 02:23:11 +00:00
|
|
|
props.set('placeholder', object.placeholder);
|
2019-09-10 00:19:49 +00:00
|
|
|
}
|
|
|
|
if (object.type === 'number') {
|
2020-02-26 02:23:11 +00:00
|
|
|
props.set('type', 'number');
|
2019-09-10 00:19:49 +00:00
|
|
|
for (const prop of ['step', 'min', 'max']) {
|
2019-11-25 19:19:18 +00:00
|
|
|
if (hasOwn(object, prop)) {
|
2020-02-26 02:23:11 +00:00
|
|
|
props.set(prop, object[prop]);
|
2019-09-10 00:19:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 02:23:11 +00:00
|
|
|
for (const [prop, value] of props.entries()) {
|
2020-05-03 01:39:24 +00:00
|
|
|
inputInner.prop(prop, value);
|
2019-09-10 00:19:49 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 01:39:24 +00:00
|
|
|
return inputInner;
|
|
|
|
}
|
|
|
|
|
|
|
|
createInputKeyMultiElement(objects) {
|
|
|
|
const inputInner = this.createInputElement(objects);
|
|
|
|
|
|
|
|
inputInner.prop('readonly', true);
|
|
|
|
|
|
|
|
let values = [];
|
|
|
|
for (const object of objects) {
|
|
|
|
if (hasOwn(object, 'values')) {
|
|
|
|
values = object.values;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const pressedKeyIndices = new Set();
|
|
|
|
|
|
|
|
const onKeyDown = ({originalEvent}) => {
|
|
|
|
const pressedKeyEventName = DOM.getKeyFromEvent(originalEvent);
|
|
|
|
if (pressedKeyEventName === 'Escape' || pressedKeyEventName === 'Backspace') {
|
|
|
|
pressedKeyIndices.clear();
|
|
|
|
inputInner.val('');
|
|
|
|
inputInner.change();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const pressedModifiers = DOM.getActiveModifiers(originalEvent);
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/metaKey
|
|
|
|
// https://askubuntu.com/questions/567731/why-is-shift-alt-being-mapped-to-meta
|
|
|
|
// It works with mouse events on some platforms, so try to determine if metaKey is pressed
|
|
|
|
// hack; only works when Shift and Alt are not pressed
|
|
|
|
const isMetaKeyChrome = (
|
|
|
|
pressedKeyEventName === 'Meta' &&
|
|
|
|
getSetDifference(new Set(['shift', 'alt']), pressedModifiers).size !== 0
|
|
|
|
);
|
|
|
|
if (isMetaKeyChrome) {
|
|
|
|
pressedModifiers.add('meta');
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const modifier of pressedModifiers) {
|
|
|
|
const foundIndex = values.findIndex(({optionValue}) => optionValue === modifier);
|
|
|
|
if (foundIndex !== -1) {
|
|
|
|
pressedKeyIndices.add(foundIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const inputValue = [...pressedKeyIndices].map((i) => values[i].name).join(' + ');
|
|
|
|
inputInner.val(inputValue);
|
|
|
|
inputInner.change();
|
|
|
|
};
|
|
|
|
|
|
|
|
inputInner.on('keydown', onKeyDown);
|
|
|
|
|
|
|
|
return inputInner;
|
|
|
|
}
|
|
|
|
|
|
|
|
createSelectElement(objects) {
|
|
|
|
const inputInner = ConditionsUI.instantiateTemplate('#condition-input-select-template');
|
|
|
|
|
|
|
|
const data = new Map([
|
|
|
|
['values', []],
|
|
|
|
['defaultValue', null]
|
|
|
|
]);
|
|
|
|
|
|
|
|
for (const object of objects) {
|
|
|
|
if (hasOwn(object, 'values')) {
|
|
|
|
data.set('values', object.values);
|
|
|
|
}
|
|
|
|
if (hasOwn(object, 'defaultValue')) {
|
2020-05-06 16:44:56 +00:00
|
|
|
data.set('defaultValue', this.isolate(object.defaultValue));
|
2020-05-03 01:39:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const {optionValue, name} of data.get('values')) {
|
|
|
|
const option = ConditionsUI.instantiateTemplate('#condition-input-option-template');
|
|
|
|
option.attr('value', optionValue);
|
|
|
|
option.text(name);
|
|
|
|
option.appendTo(inputInner);
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultValue = data.get('defaultValue');
|
|
|
|
if (defaultValue !== null) {
|
2020-05-06 16:44:56 +00:00
|
|
|
inputInner.val(this.isolate(defaultValue));
|
2020-05-03 01:39:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return inputInner;
|
2019-09-10 00:19:49 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 16:44:56 +00:00
|
|
|
validateValue(value, isInput=false) {
|
2019-09-10 00:19:49 +00:00
|
|
|
const conditionDescriptors = this.parent.parent.conditionDescriptors;
|
|
|
|
let valid = true;
|
2020-05-06 16:44:56 +00:00
|
|
|
let inputTransformedValue = null;
|
2019-09-10 00:19:49 +00:00
|
|
|
try {
|
2020-05-06 16:44:56 +00:00
|
|
|
[value, inputTransformedValue] = conditionsNormalizeOptionValue(
|
2019-09-10 00:19:49 +00:00
|
|
|
conditionDescriptors,
|
|
|
|
this.condition.type,
|
|
|
|
this.condition.operator,
|
2020-05-06 16:44:56 +00:00
|
|
|
value,
|
|
|
|
isInput
|
2019-09-10 00:19:49 +00:00
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
valid = false;
|
|
|
|
}
|
2020-05-06 16:44:56 +00:00
|
|
|
return {valid, value, inputTransformedValue};
|
2019-09-10 00:19:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onInputChanged() {
|
2020-05-06 16:44:56 +00:00
|
|
|
const {valid, value, inputTransformedValue} = this.validateValue(this.inputInner.val(), true);
|
2020-05-03 01:39:24 +00:00
|
|
|
this.inputInner.toggleClass('is-invalid', !valid);
|
|
|
|
this.inputInner.val(value);
|
2020-05-06 16:44:56 +00:00
|
|
|
this.condition.value = inputTransformedValue !== null ? inputTransformedValue : value;
|
2019-09-10 00:19:49 +00:00
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
onConditionTypeChanged() {
|
|
|
|
const type = this.typeSelect.val();
|
|
|
|
const {operator, value} = this.parent.parent.createDefaultCondition(type);
|
|
|
|
this.condition.type = type;
|
|
|
|
this.condition.operator = operator;
|
|
|
|
this.condition.value = value;
|
|
|
|
this.save();
|
|
|
|
this.updateOperators();
|
|
|
|
this.updateInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
onConditionOperatorChanged() {
|
|
|
|
const type = this.condition.type;
|
|
|
|
const operator = this.operatorSelect.val();
|
|
|
|
const {value, fromOperator} = this.parent.parent.getOperatorDefaultValue(type, operator);
|
|
|
|
this.condition.operator = operator;
|
|
|
|
if (fromOperator) {
|
|
|
|
this.condition.value = value;
|
|
|
|
}
|
|
|
|
this.save();
|
|
|
|
this.updateInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
onRemoveClicked() {
|
|
|
|
this.parent.remove(this);
|
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
};
|