nopaque/app/static/js/CorpusAnalysis/QueryBuilder.js

751 lines
31 KiB
JavaScript
Raw Normal View History

2022-07-13 12:06:22 +00:00
class ConcordanceQueryBuilder {
constructor() {
this.elements = {
counter: 0,
yourQueryContent: [],
queryContent:[],
2022-08-03 14:21:11 +00:00
closeQueryBuilder: document.querySelector("#close-query-builder"),
2022-07-13 12:06:22 +00:00
//#region QueryBuilder Elements
2022-08-01 08:00:12 +00:00
concordanceQueryBuilder: document.querySelector("#concordance-query-builder"),
concordanceQueryBuilderButton: document.querySelector("#concordance-query-builder-button"),
2022-08-03 14:21:11 +00:00
positionalAttrButton: document.querySelector('#positional-attr-button'),
positionalAttrArea: document.querySelector('#positional-attr'),
2022-08-01 08:00:12 +00:00
positionalAttr: document.querySelector("#token-attr"),
2022-08-03 14:21:11 +00:00
structuralAttrButton: document.querySelector('#structural-attr-button'),
structuralAttrArea: document.querySelector("#structural-attr"),
queryContainer: document.querySelector('#query-container'),
2022-08-01 08:00:12 +00:00
buttonPreparer: document.querySelector("#button-preparer"),
yourQuery: document.querySelector("#your-query"),
insertQueryButton: document.querySelector("#insert-query-button"),
2022-08-03 14:21:11 +00:00
queryPreview: document.querySelector('#query-preview'),
2022-08-01 08:00:12 +00:00
tokenQuery: document.querySelector("#token-query"),
tokenBuilderContent: document.querySelector("#token-builder-content"),
buildTokenButton: document.querySelector("#build-token-button"),
extFormQuery: document.querySelector('#concordance-extension-form-query'),
2022-07-13 12:06:22 +00:00
//#endregion QueryBuilder Elements
//#region Strucutral Attributes
2022-08-01 08:00:12 +00:00
sentence:document.querySelector("#sentence"),
entity: document.querySelector("#entity"),
textAnnotation: document.querySelector("#text-annotation"),
entityBuilder: document.querySelector("#entity-builder"),
englishEntType: document.querySelector("#english-ent-type"),
germanEntType: document.querySelector("#german-ent-type"),
emptyEntity: document.querySelector("#empty-entity"),
entityAnyType: false,
textAnnotationBuilder: document.querySelector("#text-annotation-builder"),
textAnnotationOptions: document.querySelector("#text-annotation-options"),
textAnnotationInput: document.querySelector("#text-annotation-input"),
textAnnotationSubmit: document.querySelector("#text-annotation-submit"),
2022-07-13 12:06:22 +00:00
//#endregion Structural Attributes
//#region Token Attributes
tokenCounter: 0,
2022-08-01 08:00:12 +00:00
lemma: document.querySelector("#lemma"),
emptyToken: document.querySelector("#empty-token"),
word: document.querySelector("#word"),
lemma: document.querySelector("#lemma"),
pos: document.querySelector("#pos"),
simplePosButton: document.querySelector("#simple-pos-button"),
incidenceModifiers: document.querySelector("[data-target='incidence-modifiers']"),
or: document.querySelector("#or"),
and: document.querySelector("#and"),
2022-07-13 12:06:22 +00:00
//#region Word and Lemma Elements
2022-08-01 08:00:12 +00:00
wordBuilder: document.querySelector("#word-builder"),
lemmaBuilder: document.querySelector("#lemma-builder"),
inputOptions: document.querySelector("#input-options"),
wordInput: document.querySelector("#word-input"),
wordSubmit: document.querySelector("#word-submit"),
lemmaInput: document.querySelector("#lemma-input"),
lemmaSubmit: document.querySelector("#lemma-submit"),
ignoreCaseCheckbox : document.querySelector("#ignore-case-checkbox"),
ignoreCase: document.querySelector("input[type='checkbox']"),
wildcardChar: document.querySelector("#wildcard-char"),
optionGroup: document.querySelector("#option-group"),
incidenceModifiersTB: document.querySelector("[data-target='incidence-modifiers-text-builder']"),
2022-07-13 12:06:22 +00:00
//#endregion Word and Lemma Elements
//#region posBuilder Elements
2022-08-01 08:00:12 +00:00
posBuilder: document.querySelector("#pos-builder"),
englishPos: document.querySelector("#english-pos"),
germanPos: document.querySelector("#german-pos"),
2022-07-13 12:06:22 +00:00
//#endregion posBuilder Elements
//#region simple_posBuilder Elements
2022-08-01 08:00:12 +00:00
simplePosBuilder: document.querySelector("#simplepos-builder"),
simplePos: document.querySelector("#simple-pos"),
2022-07-13 12:06:22 +00:00
//#endregion simple_posBuilder Elements
//#region incidence modifiers
2022-08-01 08:00:12 +00:00
oneOrMore: document.querySelector("#one-or-more"),
zeroOrMore: document.querySelector("#zero-or-more"),
zeroOrOne: document.querySelector("#zero-or-one"),
exactlyN: document.querySelector("#exactlyN"),
betweenNM: document.querySelector("#betweenNM"),
nInput: document.querySelector("#n-input"),
nSubmit: document.querySelector("#n-submit"),
nmInput: document.querySelector("#n-m-input"),
mInput: document.querySelector("#m-input"),
nmSubmit: document.querySelector("#n-m-submit"),
oneOrMoreTB: document.querySelector("#one-or-more-tb"),
zeroOrMoreTB: document.querySelector("#zero-or-more-tb"),
zeroOrOneTB: document.querySelector("#zero-or-one-tb"),
exactlyNTB: document.querySelector("#exactly-n-tb"),
betweenNMTB: document.querySelector("#between-n-m-tb")
2022-07-13 12:06:22 +00:00
//#endregion incidence modifiers
//#endregion Token Attributes
}
2022-08-03 14:21:11 +00:00
this.elements.closeQueryBuilder.addEventListener("click", () => {this.closeQueryBuilderModal();});
2022-08-01 08:00:12 +00:00
this.elements.concordanceQueryBuilderButton.addEventListener("click", () => {this.clearAll();});
this.elements.insertQueryButton.addEventListener("click", () => {this.insertQuery();});
2022-07-13 12:06:22 +00:00
2022-08-03 14:21:11 +00:00
this.elements.positionalAttrButton.addEventListener("click", () => {this.showPositionalAttrArea();});
this.elements.structuralAttrButton.addEventListener("click", () => {this.showStructuralAttrArea();});
2022-07-13 12:06:22 +00:00
//#region Structural Attribute Event Listeners
2022-08-01 08:00:12 +00:00
this.elements.sentence.addEventListener("click", () => {this.addSentence();});
this.elements.entity.addEventListener("click", () => {this.addEntity();});
this.elements.textAnnotation.addEventListener("click", () => {this.addTextAnnotation();});
2022-07-13 12:06:22 +00:00
2022-08-01 08:00:12 +00:00
this.elements.englishEntType.addEventListener("change", () => {this.englishEntTypeHandler();});
this.elements.germanEntType.addEventListener("change", () => {this.germanEntTypeHandler();});
this.elements.emptyEntity.addEventListener("click", () => {this.emptyEntityButton();});
2022-07-13 12:06:22 +00:00
2022-08-01 08:00:12 +00:00
this.elements.textAnnotationSubmit.addEventListener("click", () => {this.textAnnotationSubmitHandler();});
2022-07-13 12:06:22 +00:00
//#endregion
//#region Token Attribute Event Listeners
2022-08-01 08:00:12 +00:00
this.elements.buildTokenButton.addEventListener("click", () => {this.addToken();});
this.elements.emptyToken.addEventListener("click", () => {this.emptyTokenHandler();});
this.elements.word.addEventListener("click", () => {this.wordBuilder();});
this.elements.lemma.addEventListener("click", () => {this.lemmaBuilder();});
this.elements.pos.addEventListener("click", () => {this.posBuilder();});
this.elements.simplePosButton.addEventListener("click", () => {this.simplePosBuilder();});
this.elements.or.addEventListener("click", () => {this.orHandler();});
this.elements.and.addEventListener("click", () => {this.andHandler();});
this.elements.ignoreCase.addEventListener("change", () => {this.inputOptionHandler(this.elements.ignoreCase);});
this.elements.wildcardChar.addEventListener("click", () => {this.inputOptionHandler(this.elements.wildcardChar);});
this.elements.optionGroup.addEventListener("click", () => {this.inputOptionHandler(this.elements.optionGroup);});
this.elements.wordSubmit.addEventListener("click", () => {this.textSubmit();});
this.elements.lemmaSubmit.addEventListener("click", () => {this.textSubmit();});
this.elements.englishPos.addEventListener("change", () => {this.englishPosHandler();});
this.elements.germanPos.addEventListener("change", () => {this.germanPosHandler();});
this.elements.simplePos.addEventListener("change", () => {this.simplePosHandler();});
this.elements.oneOrMore.addEventListener("click", () => {this.incidenceModifiersHandler(this.elements.oneOrMore);});
this.elements.zeroOrMore.addEventListener("click", () => {this.incidenceModifiersHandler(this.elements.zeroOrMore);});
this.elements.zeroOrOne.addEventListener("click", () => {this.incidenceModifiersHandler(this.elements.zeroOrOne);});
this.elements.nSubmit.addEventListener("click", () => {this.nSubmitHandler();});
this.elements.nmSubmit.addEventListener("click", () => {this.nmSubmitHandler();});
this.elements.oneOrMoreTB.addEventListener("click", () => {this.incidenceModifiersHandlerTB(this.elements.oneOrMoreTB);});
this.elements.zeroOrMoreTB.addEventListener("click", () => {this.incidenceModifiersHandlerTB(this.elements.zeroOrMoreTB);});
this.elements.zeroOrOneTB.addEventListener("click", () => {this.incidenceModifiersHandlerTB(this.elements.zeroOrOneTB);});
this.elements.exactlyNTB.addEventListener("click", () => {this.incidenceModifiersHandlerTB(this.elements.exactlyNTB);});
this.elements.betweenNMTB.addEventListener("click", () => {this.incidenceModifiersHandlerTB(this.elements.betweenNMTB);});
2022-07-13 12:06:22 +00:00
//#endregion Token Attribute Event Listeners
}
//#region Structural Attribute Builder Functions
addSentence() {
this.hideEverything();
2022-08-01 08:00:12 +00:00
if(this.elements.sentence.text === 'End Sentence') {
this.buttonfactory('end-sentence', 'Sentence End', '</s>');
this.elements.sentence.innerHTML = 'Sentence';
this.elements.insertQueryButton.classList.remove("disabled");
2022-07-13 12:06:22 +00:00
this.elements.counter += 1;
2022-08-01 08:00:12 +00:00
} else {
this.buttonfactory('start-sentence', 'Sentence Start', '<s>');
this.elements.queryContent.push("sentence");
this.elements.sentence.innerHTML = 'End Sentence';
2022-07-13 12:06:22 +00:00
}
}
addEntity() {
2022-08-01 08:00:12 +00:00
if(this.elements.entity.text === 'End Entity'){
let queryText;
if (this.elements.entityAnyType === false){
queryText = '</ent_type>';
} else {
queryText = '</ent>';
}
this.buttonfactory('end-entity', 'Entity End', queryText);
this.elements.entity.innerHTML = 'Entity';
this.elements.insertQueryButton.classList.remove("disabled");
this.elements.counter += 1;
2022-07-13 12:06:22 +00:00
} else {
this.hideEverything();
2022-08-01 08:00:12 +00:00
this.elements.entityBuilder.classList.remove("hide");
2022-07-13 12:06:22 +00:00
}
}
englishEntTypeHandler() {
2022-08-01 08:00:12 +00:00
this.buttonfactory('start-entity', 'Entity Type=' + this.elements.englishEntType.value, '<ent_type="' + this.elements.englishEntType.value + '">');
this.elements.entity.innerHTML = 'End Entity';
this.hideEverything();
this.elements.entityAnyType = false;
}
germanEntTypeHandler() {
this.buttonfactory('start-entity', 'Entity Type=' + this.elements.germanEntType.value, '<ent_type="' + this.elements.germanEntType.value + '">');
this.elements.entity.innerHTML = 'End Entity';
2022-07-13 12:06:22 +00:00
this.hideEverything();
2022-08-01 08:00:12 +00:00
this.elements.entityAnyType = false;
2022-07-13 12:06:22 +00:00
}
emptyEntityButton() {
2022-08-01 08:00:12 +00:00
this.buttonfactory('start-empty-entity', 'Entity Start', '<ent>');
this.elements.entity.innerHTML = 'End Entity';
2022-07-13 12:06:22 +00:00
this.hideEverything();
2022-08-01 08:00:12 +00:00
this.elements.entityAnyType = true;
2022-07-13 12:06:22 +00:00
}
addTextAnnotation() {
this.hideEverything();
2022-08-01 08:00:12 +00:00
this.elements.textAnnotationBuilder.classList.remove("hide");
2022-07-13 12:06:22 +00:00
}
textAnnotationSubmitHandler() {
2022-08-01 08:00:12 +00:00
let queryText = ':: match.text_' + this.elements.textAnnotationOptions.value + '="' + this.elements.textAnnotationInput.value + '"';
this.buttonfactory('text-annotation', this.elements.textAnnotationOptions.value + '= ' + this.elements.textAnnotationInput.value, queryText);
this.elements.insertQueryButton.classList.remove("disabled");
2022-07-13 12:06:22 +00:00
this.elements.counter+=1;
this.hideEverything();
2022-08-01 08:00:12 +00:00
this.elements.textAnnotationInput.value = '';
2022-07-13 12:06:22 +00:00
}
//#endregion Structural Attribute Builder Functions
//#region Token Attribute Builder Functions
emptyTokenHandler() {
this.hideEverything();
2022-08-01 08:00:12 +00:00
this.elements.incidenceModifiers.classList.remove('hide');
this.tokenButtonfactory('emptyToken', 'empty token', '[]');
this.buttonDisabler('empty');
this.elements.buildTokenButton.classList.remove("disabled");
2022-07-13 12:06:22 +00:00
this.elements.tokenCounter+=1;
}
wordBuilder() {
2022-08-01 08:00:12 +00:00
this.elements.incidenceModifiers.classList.add('hide');
2022-07-13 12:06:22 +00:00
this.hideEverything();
2022-08-01 08:00:12 +00:00
this.elements.wordBuilder.classList.remove("hide");
this.elements.inputOptions.classList.remove("hide");
this.elements.ignoreCaseCheckbox.classList.remove("hide");
this.elements.incidenceModifiersTB.classList.remove('disabled');
2022-07-13 12:06:22 +00:00
}
lemmaBuilder() {
2022-08-01 08:00:12 +00:00
this.elements.incidenceModifiers.classList.add('hide');
2022-07-13 12:06:22 +00:00
this.hideEverything();
2022-08-01 08:00:12 +00:00
this.elements.lemmaBuilder.classList.remove("hide");
this.elements.inputOptions.classList.remove("hide");
this.elements.ignoreCaseCheckbox.classList.remove("hide");
this.elements.incidenceModifiersTB.classList.remove('disabled');
2022-07-13 12:06:22 +00:00
}
inputOptionHandler(elem) {
let input;
2022-08-01 08:00:12 +00:00
if (this.elements.wordBuilder.classList.contains('hide') === false){
2022-07-13 12:06:22 +00:00
input = this.elements.wordInput;
}else{
input = this.elements.lemmaInput;
}
if (elem === this.elements.optionGroup) {
2022-08-01 08:00:12 +00:00
input.value += '( option1 | option2 )';
let firstIndex = input.value.indexOf('option1');
let lastIndex = firstIndex + 'option1'.length;
2022-07-13 12:06:22 +00:00
input.focus();
input.setSelectionRange(firstIndex, lastIndex);
}else if (elem === this.elements.wildcardChar){
2022-08-01 08:00:12 +00:00
input.value += '.';
2022-07-13 12:06:22 +00:00
}
}
textSubmit() {
this.buttonDisabler();
2022-08-01 08:00:12 +00:00
this.elements.incidenceModifiers.classList.remove("disabled");
2022-07-13 12:06:22 +00:00
let c;
if (this.elements.ignoreCase.checked){
2022-08-01 08:00:12 +00:00
c = ' %c';
2022-07-13 12:06:22 +00:00
}else{
2022-08-01 08:00:12 +00:00
c = '';
2022-07-13 12:06:22 +00:00
}
2022-08-01 08:00:12 +00:00
if (this.elements.wordBuilder.classList.contains('hide') === false){
this.tokenButtonfactory('word', 'word=' + this.elements.wordInput.value + c, 'word="'+ this.elements.wordInput.value + '"' + c );
this.elements.buildTokenButton.classList.remove("disabled");
2022-07-13 12:06:22 +00:00
this.elements.tokenCounter+=1;
2022-08-01 08:00:12 +00:00
this.elements.wordInput.value = '';
}else if (this.elements.lemmaBuilder.classList.contains('hide') === false){
this.tokenButtonfactory('lemma', 'lemma=' + this.elements.lemmaInput.value + c, 'lemma="'+ this.elements.lemmaInput.value + '"' + c);
this.elements.buildTokenButton.classList.remove("disabled");
2022-07-13 12:06:22 +00:00
this.elements.tokenCounter+=1;
2022-08-01 08:00:12 +00:00
this.elements.lemmaInput.value = '';
2022-07-13 12:06:22 +00:00
}
}
posBuilder() {
this.hideEverything();
2022-08-01 08:00:12 +00:00
this.elements.incidenceModifiers.classList.remove('hide');
2022-07-13 12:06:22 +00:00
this.elements.positionalAttr.appendChild(this.elements.incidenceModifiers);
2022-08-01 08:00:12 +00:00
this.elements.posBuilder.classList.remove("hide");
2022-07-13 12:06:22 +00:00
}
englishPosHandler() {
this.buttonDisabler();
2022-08-01 08:00:12 +00:00
this.tokenButtonfactory('pos', 'pos=' + this.elements.englishPos.value, 'pos="' + this.elements.englishPos.value + '"');
this.elements.buildTokenButton.classList.remove("disabled");
2022-07-13 12:06:22 +00:00
this.elements.tokenCounter+=1;
}
germanPosHandler() {
this.buttonDisabler();
2022-08-01 08:00:12 +00:00
this.tokenButtonfactory('pos', 'pos=' + this.elements.germanPos.value, 'pos="' + this.elements.germanPos.value + '"');
this.elements.buildTokenButton.classList.remove("disabled");
2022-07-13 12:06:22 +00:00
this.elements.tokenCounter+=1;
}
simplePosBuilder() {
this.hideEverything();
2022-08-01 08:00:12 +00:00
this.elements.incidenceModifiers.classList.remove('hide');
2022-07-13 12:06:22 +00:00
this.elements.positionalAttr.appendChild(this.elements.incidenceModifiers);
2022-08-01 08:00:12 +00:00
this.elements.simplePosBuilder.classList.remove("hide");
2022-07-13 12:06:22 +00:00
}
simplePosHandler() {
this.buttonDisabler();
2022-08-01 08:00:12 +00:00
this.tokenButtonfactory('simplePos', 'simple_pos=' + this.elements.simplePos.value, 'simple_pos="' + this.elements.simplePos.value + '"');
this.elements.buildTokenButton.classList.remove("disabled");
2022-07-13 12:06:22 +00:00
this.elements.tokenCounter+=1;
}
incidenceModifiersHandler(input) {
2022-08-01 08:00:12 +00:00
this.tokenButtonfactory('incidenceModifier', input.text, input.dataset.token);
this.elements.incidenceModifiers.classList.add("disabled");
this.elements.tokenCounter+=1;
}
nSubmitHandler() {
this.tokenButtonfactory('exactlyN', 'Exactly ' + this.elements.nInput.value, '{' + this.elements.nInput.value + '}');
let instance = M.Modal.getInstance(this.elements.exactlyN);
instance.close();
this.elements.incidenceModifiers.classList.add('disabled');
}
nmSubmitHandler() {
this.tokenButtonfactory('betweenNM', 'Between ' + this.elements.nmInput.value + ' and ' + this.elements.mInput.value, '{' + this.elements.nmInput.value + ',' + this.elements.mInput.value + '}');
let instance = M.Modal.getInstance(this.elements.betweenNM);
instance.close();
this.elements.incidenceModifiers.classList.add('disabled');
}
2022-07-13 12:06:22 +00:00
2022-08-01 08:00:12 +00:00
// TB = Text Builder
2022-07-13 12:06:22 +00:00
incidenceModifiersHandlerTB(elem) {
let input;
2022-08-01 08:00:12 +00:00
if (this.elements.wordBuilder.classList.contains('hide') === false){
2022-07-13 12:06:22 +00:00
input = this.elements.wordInput;
}else{
input = this.elements.lemmaInput;
}
2022-08-01 08:00:12 +00:00
if (elem.id === 'exactly-n-tb'){
2022-07-13 12:06:22 +00:00
input.value += elem.dataset.token;
2022-08-01 08:00:12 +00:00
let index = input.value.lastIndexOf('{n}');
2022-07-13 12:06:22 +00:00
let instance = M.Dropdown.getInstance(this.elements.incidenceModifiersTB);
instance.close();
input.focus();
input.setSelectionRange(index+1, index+2);
2022-08-01 08:00:12 +00:00
}else if (elem.id === 'between-n-m-tb') {
input.value += elem.dataset.token;
let index = input.value.lastIndexOf('{n,m}');
2022-07-13 12:06:22 +00:00
let instance = M.Dropdown.getInstance(this.elements.incidenceModifiersTB);
instance.close();
input.focus();
input.setSelectionRange(index+1, index+2);
}else {
2022-08-01 08:00:12 +00:00
input.value += ' ' + elem.dataset.token;
2022-07-13 12:06:22 +00:00
}
2022-08-01 08:00:12 +00:00
this.elements.incidenceModifiersTB.classList.add('disabled');
2022-07-13 12:06:22 +00:00
}
orHandler() {
this.elements.positionalAttr.appendChild(this.elements.incidenceModifiers);
2022-08-01 08:00:12 +00:00
this.buttonDisabler('condition');
2022-07-13 12:06:22 +00:00
this.hideEverything();
2022-08-01 08:00:12 +00:00
this.tokenButtonfactory('or', 'or', '|');
this.elements.buildTokenButton.classList.remove("disabled");
2022-07-13 12:06:22 +00:00
this.elements.tokenCounter+=1;
}
andHandler() {
this.elements.positionalAttr.appendChild(this.elements.incidenceModifiers);
2022-08-01 08:00:12 +00:00
this.buttonDisabler('condition');
2022-07-13 12:06:22 +00:00
this.hideEverything();
2022-08-01 08:00:12 +00:00
this.tokenButtonfactory('and', 'and', '&');
this.elements.buildTokenButton.classList.remove("disabled");
2022-07-13 12:06:22 +00:00
this.elements.tokenCounter+=1;
}
hideEverything(){
2022-08-01 08:00:12 +00:00
this.elements.wordBuilder.classList.add("hide");
this.elements.lemmaBuilder.classList.add("hide");
this.elements.ignoreCaseCheckbox.classList.add("hide");
this.elements.inputOptions.classList.add("hide");
this.elements.posBuilder.classList.add("hide");
this.elements.simplePosBuilder.classList.add("hide");
this.elements.entityBuilder.classList.add("hide");
this.elements.textAnnotationBuilder.classList.add("hide");
}
buttonDisabler(attr = 'token') {
let tokenButtonList = this.elements.positionalAttr.querySelectorAll("a");
if (attr === 'start'){
2022-07-13 12:06:22 +00:00
tokenButtonList.forEach(element => {
2022-08-01 08:00:12 +00:00
if (element.id === 'or'){
element.classList.add("disabled");
} else if (element.id === 'and'){
element.classList.add("disabled");
}else if (element.dataset.target == 'incidence-modifiers') {
element.classList.add("disabled");
} else if (element.id === 'empty-token'){
element.classList.remove("disabled");
2022-07-13 12:06:22 +00:00
} else {
2022-08-01 08:00:12 +00:00
element.classList.remove("disabled");
2022-07-13 12:06:22 +00:00
}
});
2022-08-01 08:00:12 +00:00
}else if (attr === 'condition'){
2022-07-13 12:06:22 +00:00
tokenButtonList.forEach(element => {
2022-08-01 08:00:12 +00:00
if (element.id === 'or'){
element.classList.add("disabled");
} else if (element.id === 'and'){
element.classList.add("disabled");
}else if (element.dataset.target == 'incidence-modifiers') {
element.classList.add("disabled");
} else if (element.id === 'empty-token'){
element.classList.add("disabled");
2022-07-13 12:06:22 +00:00
} else {
2022-08-01 08:00:12 +00:00
element.classList.remove("disabled");
2022-07-13 12:06:22 +00:00
}
});
2022-08-01 08:00:12 +00:00
}else if (attr === 'empty') {
2022-07-13 12:06:22 +00:00
tokenButtonList.forEach(element => {
2022-08-01 08:00:12 +00:00
if (element.id == 'or'){
element.classList.add("disabled");
} else if (element.id == 'and'){
element.classList.add("disabled");
}else if (element.dataset.target == 'incidence-modifiers') {
element.classList.remove("disabled");
2022-07-13 12:06:22 +00:00
} else {
2022-08-01 08:00:12 +00:00
element.classList.add("disabled");
2022-07-13 12:06:22 +00:00
}
});
}else{
tokenButtonList.forEach(element => {
2022-08-01 08:00:12 +00:00
if (element.id == 'or'){
element.classList.remove("disabled");
} else if (element.id == 'and'){
element.classList.remove("disabled");
}else if (element.dataset.target == 'incidence-modifiers') {
element.classList.remove("disabled");
2022-07-13 12:06:22 +00:00
} else {
2022-08-01 08:00:12 +00:00
element.classList.add("disabled");
2022-07-13 12:06:22 +00:00
}
});
}
}
2022-08-01 08:00:12 +00:00
tokenButtonfactory(dataType, prettyText, tokenText) {
2022-08-03 14:21:11 +00:00
let chipColor = 'style="background-color:#';
if (dataType === 'pos' || dataType === 'word' || dataType === 'lemma' || dataType === 'simplePos'){
chipColor += 'EF60B4';
}else if (dataType === "emptyToken"){
chipColor += '43C6FC';
}else if (dataType === "and" || dataType === "or"){
chipColor += 'FFCC00';
}else if (dataType === "incidenceModifier" || dataType === "betweenNM" || dataType === "exactlyN"){
chipColor += '2FBBAB';
}else {
chipColor = '';
}
2022-08-01 08:00:12 +00:00
tokenText = encodeURI(tokenText);
2022-08-03 14:21:11 +00:00
this.elements.buttonPreparer.innerHTML += '<div class="chip"' + chipColor +'" data-type="' + dataType + '" data-tokentext="' + tokenText + '">' + prettyText + '<i class="material-icons close">close</i></div>';
2022-08-01 08:00:12 +00:00
let tokenDummyButton = this.elements.buttonPreparer.querySelector(':first-child');
tokenDummyButton.addEventListener("click", () => {this.deleteTokenAttr(tokenDummyButton);});
2022-07-13 12:06:22 +00:00
this.elements.tokenQuery.appendChild(tokenDummyButton);
}
deleteTokenAttr(attr){
let nodesList = attr.parentElement.childNodes;
let indexOfAttr;
for (let i = 0; i < nodesList.length; i++) {
if(nodesList[i] === attr){
indexOfAttr = i;
}
}
if(indexOfAttr>0){
2022-08-01 08:00:12 +00:00
if (attr.dataset.type === 'or' || attr.dataset.type === 'and') {
2022-07-13 12:06:22 +00:00
this.elements.tokenQuery.removeChild(nodesList[indexOfAttr+1]);
this.elements.tokenCounter-=1;
}else {
2022-08-01 08:00:12 +00:00
if (nodesList[indexOfAttr-1].dataset.type === 'or' || nodesList[indexOfAttr-1].dataset.type === 'and'){
2022-07-13 12:06:22 +00:00
this.elements.tokenQuery.removeChild(nodesList[indexOfAttr-1])
this.elements.tokenCounter-=1;
}
}
}
this.elements.tokenQuery.removeChild(attr);
this.elements.tokenCounter-=1;
if(this.elements.tokenCounter === 0){
2022-08-01 08:00:12 +00:00
this.elements.buildTokenButton.classList.add("disabled");
this.buttonDisabler('start');
2022-07-13 12:06:22 +00:00
this.hideEverything();
}
}
addToken() {
2022-08-01 08:00:12 +00:00
let tokenQueryContent = '';
let tokenQueryText = '';
let emptyTokenCheck = false;
for (let element of this.elements.tokenQuery.childNodes) {
2022-08-03 14:21:11 +00:00
tokenQueryContent += ' ' + element.firstChild.data + ' ';
2022-08-01 08:00:12 +00:00
tokenQueryText += ' ' + decodeURI(element.dataset.tokentext);
if (element.dataset.type === "emptyToken"){
emptyTokenCheck = true;
}
}
if (emptyTokenCheck === false){
tokenQueryText = '[' + tokenQueryText + ']';
2022-08-03 14:21:11 +00:00
}
2022-08-01 08:00:12 +00:00
this.buttonfactory('token', tokenQueryContent, tokenQueryText);
tokenQueryContent = '';
this.elements.tokenQuery.innerHTML = '';
2022-07-13 12:06:22 +00:00
this.elements.tokenCounter = 0;
2022-08-01 08:00:12 +00:00
this.elements.buildTokenButton.classList.add("disabled");
2022-07-13 12:06:22 +00:00
this.hideEverything();
2022-08-01 08:00:12 +00:00
this.buttonDisabler('start');
this.elements.insertQueryButton.classList.remove("disabled");
this.elements.counter += 1;
2022-07-13 12:06:22 +00:00
}
//#endregion Token Attribute Builder Functions
//#region General Functions
2022-08-03 14:21:11 +00:00
closeQueryBuilderModal(){
let instance = M.Modal.getInstance(this.elements.concordanceQueryBuilder);
instance.close();
}
showPositionalAttrArea(){
this.elements.positionalAttrArea.classList.remove('hide');
this.elements.structuralAttrArea.classList.add('hide');
}
showStructuralAttrArea(){
this.elements.positionalAttrArea.classList.add('hide');
this.elements.structuralAttrArea.classList.remove('hide');
}
2022-08-01 08:00:12 +00:00
buttonfactory(dataType, prettyText, queryText) {
2022-08-03 14:21:11 +00:00
let chipColor = 'style="background-color:#';
if (dataType === 'start-sentence' || dataType === 'end-sentence'){
chipColor += 'FD9720';
}else if (dataType === "start-empty-entity" || dataType === "start-entity" || dataType === "end-entity"){
chipColor += 'A6E22D';
}else if (dataType === "text-annotation"){
chipColor += '2FBBAB';
}else if (dataType === "token"){
chipColor += '28B3D1';
}else {
chipColor = '';
}
2022-08-01 08:00:12 +00:00
queryText = encodeURI(queryText);
2022-08-03 14:21:11 +00:00
this.elements.buttonPreparer.innerHTML += '<div class="chip"' + chipColor +'" data-type="' + dataType + '" data-query="' + queryText + '">' + prettyText + '<i class="material-icons close">close</i></div>';
2022-08-01 08:00:12 +00:00
let dummyButton = this.elements.buttonPreparer.querySelector(':first-child');
dummyButton.addEventListener("click", () => {this.deleteAttr(dummyButton);});
2022-07-13 12:06:22 +00:00
this.elements.yourQuery.appendChild(dummyButton);
2022-08-03 14:21:11 +00:00
this.elements.queryContainer.classList.remove("hide");
this.queryPreviewBuilder();
}
queryPreviewBuilder(){
this.elements.yourQueryContent = [];
for (let element of this.elements.yourQuery.childNodes) {
let queryElement = decodeURI(element.dataset.query);
if (queryElement.includes("<")){
queryElement = queryElement.replace("<", "&#60;");
}
if (queryElement.includes(">")){
queryElement = queryElement.replace(">", "&#62;");
}
this.elements.yourQueryContent.push(queryElement);
}
let queryString = this.elements.yourQueryContent.join(' ');
queryString += ";";
this.elements.queryPreview.innerHTML = queryString;
2022-07-13 12:06:22 +00:00
}
deleteAttr(attr) {
let siblingList = [];
let nodesList = attr.parentElement.childNodes;
let indexOfAttr;
let connectedElement;
2022-08-01 08:00:12 +00:00
// Why nodesList.indexOf(attr) doesn"t work?!
2022-07-13 12:06:22 +00:00
for (let i = 0; i < nodesList.length; i++) {
if(nodesList[i] === attr){
indexOfAttr = i;
}
}
2022-08-01 08:00:12 +00:00
2022-07-13 12:06:22 +00:00
switch (attr.dataset.type) {
2022-08-01 08:00:12 +00:00
case 'start-sentence':
2022-07-13 12:06:22 +00:00
for (let i = indexOfAttr; i < nodesList.length; i++) {
2022-08-01 08:00:12 +00:00
if (nodesList[i].dataset.type === 'end-sentence'){
2022-07-13 12:06:22 +00:00
siblingList.push(nodesList[i]);
}
}
connectedElement = siblingList[0];
2022-08-01 08:00:12 +00:00
if (connectedElement === undefined){
this.elements.sentence.innerHTML = 'Sentence';
}
2022-07-13 12:06:22 +00:00
break;
2022-08-01 08:00:12 +00:00
case 'end-sentence':
2022-07-13 12:06:22 +00:00
for (let i = 0; i < indexOfAttr; i++) {
2022-08-01 08:00:12 +00:00
if (nodesList[i].dataset.type === 'start-sentence'){
2022-07-13 12:06:22 +00:00
siblingList.push(nodesList[i]);
}
}
connectedElement = siblingList[siblingList.length -1];
break;
2022-08-01 08:00:12 +00:00
case 'start-entity':
2022-07-13 12:06:22 +00:00
for (let i = indexOfAttr; i < nodesList.length; i++) {
2022-08-01 08:00:12 +00:00
if (nodesList[i].dataset.type === 'end-entity'){
2022-07-13 12:06:22 +00:00
siblingList.push(nodesList[i]);
}
}
connectedElement = siblingList[0];
2022-08-01 08:00:12 +00:00
if (connectedElement === undefined){
this.elements.entity.innerHTML = 'Entity';
}
2022-07-13 12:06:22 +00:00
break;
2022-08-01 08:00:12 +00:00
case 'end-entity':
2022-07-13 12:06:22 +00:00
for (let i = 0; i < indexOfAttr; i++) {
2022-08-01 08:00:12 +00:00
if (nodesList[i].dataset.type === 'start-entity'){
2022-07-13 12:06:22 +00:00
siblingList.push(nodesList[i]);
}
}
connectedElement = siblingList[siblingList.length -1];
break;
default:
2022-08-01 08:00:12 +00:00
connectedElement = undefined;
2022-07-13 12:06:22 +00:00
break;
}
2022-08-01 08:00:12 +00:00
if (connectedElement !== undefined ){
2022-07-13 12:06:22 +00:00
this.elements.yourQuery.removeChild(connectedElement);
}
2022-08-01 08:00:12 +00:00
2022-07-13 12:06:22 +00:00
this.elements.yourQuery.removeChild(attr);
this.elements.counter -= 1;
if(this.elements.counter === 0){
2022-08-01 08:00:12 +00:00
this.elements.insertQueryButton.classList.add("disabled");
2022-08-03 14:21:11 +00:00
this.elements.queryContainer.classList.add("hide");
2022-07-13 12:06:22 +00:00
}
2022-08-03 14:21:11 +00:00
this.queryPreviewBuilder();
2022-07-13 12:06:22 +00:00
}
insertQuery() {
2022-08-01 08:00:12 +00:00
this.elements.yourQueryContent = [];
for (let element of this.elements.yourQuery.childNodes) {
let queryElement = decodeURI(element.dataset.query);
this.elements.yourQueryContent.push(queryElement);
}
let queryString = this.elements.yourQueryContent.join(' ');
queryString += ";";
2022-07-13 12:06:22 +00:00
2022-08-01 08:00:12 +00:00
this.elements.concordanceQueryBuilder.classList.add('modal-close');
this.elements.extFormQuery.value = queryString;
2022-07-13 12:06:22 +00:00
}
clearAll() {
2022-08-01 08:00:12 +00:00
this.elements.tokenQuery.innerHTML = '';
2022-07-13 12:06:22 +00:00
this.elements.tokenCounter = 0;
this.elements.counter = 0;
2022-08-01 08:00:12 +00:00
this.elements.buildTokenButton.classList.add("disabled");
this.elements.insertQueryButton.classList.add("disabled");
this.elements.concordanceQueryBuilder.classList.remove('modal-close');
2022-08-03 14:21:11 +00:00
this.elements.positionalAttrArea.classList.add('hide');
this.elements.structuralAttrArea.classList.add('hide');
2022-07-13 12:06:22 +00:00
this.hideEverything();
2022-08-01 08:00:12 +00:00
this.buttonDisabler('start');
this.elements.yourQuery.innerHTML = '';
2022-08-03 14:21:11 +00:00
this.elements.queryContainer.classList.add("hide");
2022-07-13 12:06:22 +00:00
}
//#endregion General Functions
}