Skip to content

Commit 9dfeb7d

Browse files
committed
[academic_query] show descriptive query in read mode
SQUASHED: AUTO-COMMIT-src-components-widgets-academic-subquery.js,
1 parent 8290c56 commit 9dfeb7d

File tree

1 file changed

+81
-56
lines changed

1 file changed

+81
-56
lines changed

src/components/widgets/academic-subquery.js

Lines changed: 81 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,49 @@ export default class AcademicSubquery extends Morph {
141141

142142
/*MD ## Init MD*/
143143
async initialize() {
144+
this.updateView();
145+
146+
observer = new MutationObserver((mutations) => {
147+
clearTimeout(timeout);
148+
timeout = setTimeout(async () => {
149+
mutations.forEach(async mutation => {
150+
if (mutation.type == "characterData") {
151+
this.textContent = await this.viewToQuery();
152+
}
153+
if (mutation.type == "childList") {
154+
// TODO: better propagation to super elements
155+
var div = <div id="update"></div>;
156+
this.appendChild(div);
157+
this.removeChild(div);
158+
159+
}
160+
})
161+
}, 300);
162+
});
163+
const config = {
164+
attributes: true,
165+
childList: true,
166+
subtree: true,
167+
attributeOldValue: true,
168+
characterDataOldValue: true,
169+
};
170+
observer.observe(this.get('#pane'), config);
171+
172+
// TODO: falls ich das umbaue, sodass eine subquery einfach als
173+
// html Element in updateView erstellt wird, muss das hier auch da rein
174+
/* this.addEventListener('dragstart', (evt) => this.onDragStart(evt))
175+
this.addEventListener('dragend', (evt) => this.onDragEnd(evt))
176+
this.addEventListener('dragover', (evt) => this.onDragOver(evt))
177+
this.addEventListener('dragenter', (evt) => this.onDragEnter(evt))
178+
this.addEventListener('dragleave', (evt) => this.onDragLeave(evt))
179+
this.addEventListener('drop', (evt) => this.onDrop(evt))
180+
*/
181+
this.style.draggable = 'true';
182+
}
183+
184+
async getPreparedSchema() {
185+
if (this.schemaFiltered) { return this.schemaFiltered; }
186+
144187
// load the schema of a paper
145188
this.schema = await MicrosoftAcademicEntities.generateSchema("paper");
146189
// to use the descriptions in the UI, we need to shorten some
@@ -231,45 +274,8 @@ export default class AcademicSubquery extends Morph {
231274
default:
232275
return op;
233276
}};
234-
235-
this.updateView();
236-
237-
observer = new MutationObserver((mutations) => {
238-
clearTimeout(timeout);
239-
timeout = setTimeout(async () => {
240-
mutations.forEach(async mutation => {
241-
if (mutation.type == "characterData") {
242-
this.textContent = await this.viewToQuery();
243-
}
244-
if (mutation.type == "childList") {
245-
// TODO: better propagation to super elements
246-
var div = <div id="update"></div>;
247-
this.appendChild(div);
248-
this.removeChild(div);
249-
250-
}
251-
})
252-
}, 300);
253-
});
254-
const config = {
255-
attributes: true,
256-
childList: true,
257-
subtree: true,
258-
attributeOldValue: true,
259-
characterDataOldValue: true,
260-
};
261-
observer.observe(this.get('#pane'), config);
262277

263-
// TODO: falls ich das umbaue, sodass eine subquery einfach als
264-
// html Element in updateView erstellt wird, muss das hier auch da rein
265-
/* this.addEventListener('dragstart', (evt) => this.onDragStart(evt))
266-
this.addEventListener('dragend', (evt) => this.onDragEnd(evt))
267-
this.addEventListener('dragover', (evt) => this.onDragOver(evt))
268-
this.addEventListener('dragenter', (evt) => this.onDragEnter(evt))
269-
this.addEventListener('dragleave', (evt) => this.onDragLeave(evt))
270-
this.addEventListener('drop', (evt) => this.onDrop(evt))
271-
*/
272-
this.style.draggable = 'true';
278+
return this.schemaFiltered;
273279
}
274280

275281
onDragStart(event) {
@@ -407,8 +413,9 @@ export default class AcademicSubquery extends Morph {
407413
}
408414

409415
var currentAttribute;
410-
this.schemaFiltered.forEach(attr => {
411-
if (attr.name == attribute) {
416+
var schema = await this.getPreparedSchema();
417+
schema.forEach(attr => {
418+
if (attr.shortDesc == attribute || attr.name == attribute) {
412419
currentAttribute = attr;
413420
}})
414421

@@ -417,10 +424,10 @@ export default class AcademicSubquery extends Morph {
417424
}
418425

419426
if (currentAttribute.name.includes("."))
420-
query = "Composite(" + attribute + comp + val + ")";
427+
query = "Composite(" + currentAttribute.name + comp + val + ")";
421428
// TODO: Set type to Composite?
422429
else
423-
query = attribute + comp + val;
430+
query = currentAttribute.name + comp + val;
424431

425432

426433

@@ -438,14 +445,15 @@ export default class AcademicSubquery extends Morph {
438445
this.updateView();
439446
}
440447

441-
onChangeAttribute() {
448+
async onChangeAttribute() {
442449
var innerSpan = this.get('#inner');
443450
var compElement = innerSpan.querySelector('#comparator');
444451
var attrElement = innerSpan.querySelector('#attribute');
445452

446453
var selectedAttribute = attrElement.options[attrElement.selectedIndex].value;
447454
var currentAttribute;
448-
this.schemaFiltered.forEach(option => {
455+
var schema = await this.getPreparedSchema();
456+
schema.forEach(option => {
449457
if (option.name == selectedAttribute) { currentAttribute = option; }
450458
})
451459

@@ -473,12 +481,13 @@ export default class AcademicSubquery extends Morph {
473481
// attribute
474482
var attrElement = <select name='attribute' id='attribute'></select>;
475483
var currentAttribute;
476-
this.schemaFiltered.forEach(option => {
484+
var schema = await this.getPreparedSchema();
485+
schema.forEach(option => {
477486
var selected = (option.name == ast.attribute);
478487
if (selected) { currentAttribute = option; }
479488
attrElement.options.add(new Option(option.shortDesc, option.name, selected, selected))
480489
})
481-
attrElement.onchange = () => this.onChangeAttribute();
490+
attrElement.onchange = async () => await this.onChangeAttribute();
482491
query.appendChild(attrElement);
483492

484493
// comparator
@@ -540,7 +549,7 @@ export default class AcademicSubquery extends Morph {
540549
this.style.color = "black"
541550
}
542551

543-
buildSimpleQuery(ast) {
552+
async buildSimpleQuery(ast) {
544553
var inner =
545554
<span class="hover" contenteditable="false" id="inner">
546555
<span class="hovercontent">
@@ -558,14 +567,30 @@ export default class AcademicSubquery extends Morph {
558567
}}>OR</button>
559568
</span>
560569
</span>;
561-
var query = <span name="sub" draggable='true'></span>;
562-
[ast.attribute, ast.comparator, ast.value].forEach(value => {
563-
query.appendChild(<span class="queryPart" name="queryPart">{value}</span>)
564-
query.addEventListener('mouseover', (evt) => this.onMouseOver(evt));
565-
query.addEventListener('mouseout', (evt) => this.onMouseOut(evt));
566-
query.style.cursor = "grab"
567-
});
568-
inner.appendChild(query);
570+
var queryElement = <span name="sub" draggable='true'></span>;
571+
572+
// attribute
573+
var currentAttribute;
574+
var schema = await this.getPreparedSchema();
575+
schema.forEach(option => {
576+
if (option.name == ast.attribute) { currentAttribute = option; }
577+
})
578+
var attrElement = <span class="queryPart" name="queryPart">{currentAttribute.shortDesc}</span>;
579+
queryElement.appendChild(attrElement);
580+
581+
// comparator
582+
var compElement = <span class="queryPart" name="queryPart">{ast.comparator}</span>;
583+
queryElement.appendChild(compElement);
584+
585+
// value
586+
var valElement = <span class="queryPart" name="queryPart">{ast.value}</span>;
587+
queryElement.appendChild(valElement);
588+
589+
queryElement.addEventListener('mouseover', (evt) => this.onMouseOver(evt));
590+
queryElement.addEventListener('mouseout', (evt) => this.onMouseOut(evt));
591+
queryElement.style.cursor = "grab"
592+
593+
inner.appendChild(queryElement);
569594
var edit = <span id="edit" title="toggle edit mode" click={() => this.toggleEditing()}><i class="fa fa-pencil" aria-hidden="true"></i></span>;
570595
edit.style.cursor = "pointer";
571596
inner.appendChild(edit);
@@ -583,7 +608,7 @@ export default class AcademicSubquery extends Morph {
583608
if (this.editing) { // edit mode
584609
inner = await this.buildEditable(ast);
585610
} else { // read mode
586-
inner = this.buildSimpleQuery(ast);
611+
inner = await this.buildSimpleQuery(ast);
587612
}
588613
}
589614
inner.setAttribute("type", ast.type);

0 commit comments

Comments
 (0)