Skip to content

Commit a15c28c

Browse files
committed
[academic_query] add auto update, timer and button functionality. Not bug_free, but I don_t see it getting better right now.
SQUASHED: AUTO-COMMIT-src-components-widgets-academic-query.js,AUTO-COMMIT-src-components-widgets-academic-subquery.js,
1 parent d2cee8f commit a15c28c

File tree

2 files changed

+40
-45
lines changed

2 files changed

+40
-45
lines changed

src/components/widgets/academic-query.js

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,6 @@
11
import Morph from 'src/components/widgets/lively-morph.js';
22
import AcademicSubquery from "src/components/widgets/academic-subquery.js";
33

4-
/*const observer = new MutationObserver(function(mutations) {
5-
mutations.forEach(mutation => {
6-
//lively.notify("observation", mutation.type)
7-
if (mutation.type == "characterData") {
8-
var element = mutation.target;
9-
lively.notify("ELEMENT",element)
10-
while (element.parentNode && (element.nodeName != "ACADEMIC-QUERY")) {
11-
lively.notify("CURRENT ELEMENT",element)
12-
element = element.parentNode;
13-
}
14-
if (element.nodeName == "ACADEMIC-QUERY") {
15-
element.textContent = element.viewToQuery();
16-
} else {
17-
lively.notify("Could not find academic-query");
18-
}
19-
}
20-
})
21-
})
22-
const config = {
23-
attributes: true,
24-
childList: true,
25-
subtree: true,
26-
attributeOldValue: true,
27-
characterDataOldValue: true,
28-
//attributeFilter: true // breaks for some reason
29-
}*/
30-
314
export default class AcademicQuery extends Morph {
325
constructor() {
336
super();
@@ -39,11 +12,17 @@ export default class AcademicQuery extends Morph {
3912
// alternativ immer das AcademicQuery object mitgeben
4013
// und dem Bescheid geben, wenn ein update ist
4114
// (alles kacke)
15+
// idealerweise bemerkt der hier schon, wenn sich
16+
// irgendwo unter ihm Text ändert
4217
var observer = new MutationObserver((mutations) => {
43-
mutations.forEach(mutation => {
18+
mutations.forEach(async mutation => {
4419
//lively.notify("SUPER observation", mutation.type)
4520
if (mutation.type == "childList") {
46-
if (this.subquery) this.textContent = this.subquery.viewToQuery();
21+
if (this.subquery) {
22+
this.textContent = await this.subquery.viewToQuery();
23+
var input = this.get('#queryInput');
24+
input.value = this.getQuery();
25+
}
4726
}
4827
})
4928
});
@@ -82,8 +61,8 @@ export default class AcademicQuery extends Morph {
8261
} else {
8362
lively.notify("Could not load query.");
8463
}
85-
var input = <input value={queryView.textContent} style="width: 300px"></input>;
86-
var updateButton = <button click={() => input.value = (this.getQuery())}>update</button>;
64+
var input = <input id="queryInput" value={this.textContent} style="width: 300px"></input>;
65+
var updateButton = <button click={() => this.setQuery(input.value)}>update</button>;
8766

8867
pane.innerHTML = ""
8968
pane.appendChild(<div>

src/components/widgets/academic-subquery.js

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ const config = {
149149
}*/
150150

151151
var observer;
152+
var timeout;
152153

153154
export default class AcademicSubquery extends Morph {
154155
constructor() {
@@ -160,16 +161,21 @@ export default class AcademicSubquery extends Morph {
160161

161162
observer = new MutationObserver((mutations) => {
162163
mutations.forEach(mutation => {
163-
//nach jeder Änderung erstmal nen Timer
164164
//lively.notify("observation", mutation.type)
165-
if (mutation.type == "characterData") {
166-
this.textContent = this.viewToQuery();
167-
}
168-
if (mutation.type == "childList") {
169-
var div = <div id="update"></div>;
170-
this.appendChild(div);
171-
this.removeChild(div);
172-
}
165+
166+
timeout = setTimeout(async () => {
167+
if (mutation.type == "characterData") {
168+
this.textContent = await this.viewToQuery();
169+
}
170+
if (mutation.type == "childList") {
171+
clearTimeout(timeout);
172+
//timeout = setTimeout(() => {
173+
var div = <div id="update"></div>;
174+
this.appendChild(div);
175+
this.removeChild(div);
176+
//}, 3000);
177+
}
178+
}, 1000);
173179
})
174180
});
175181

@@ -181,6 +187,8 @@ export default class AcademicSubquery extends Morph {
181187
characterDataOldValue: true,
182188
};
183189

190+
// TODO: Why are we switching focus after editing
191+
// once we start observing?
184192
observer.observe(this.get('#pane'), config);
185193
}
186194

@@ -202,7 +210,9 @@ export default class AcademicSubquery extends Morph {
202210
async setQueryObject(o) {
203211
this.ui = await this.queryToView(o);
204212

205-
this.updateView()
213+
this.updateView();
214+
215+
return this;
206216
}
207217

208218
async updateView() {
@@ -214,14 +224,14 @@ export default class AcademicSubquery extends Morph {
214224
}
215225
}
216226

217-
viewToQuery() {
227+
async viewToQuery() {
218228
var query = "... parsed from ui"
219229

220230
if (this.isComplex) {
221231
// TODO: Why is this neccessary?
222-
if (this.leftSubquery && this.rightSubquery) {
223-
var left = this.leftSubquery.viewToQuery()
224-
var right = this.rightSubquery.viewToQuery()
232+
if (await this.leftSubquery && await this.rightSubquery) {
233+
var left = await this.leftSubquery.viewToQuery()
234+
var right = await this.rightSubquery.viewToQuery()
225235
var conjunction = this.get('#conjunction').textContent
226236
query = conjunction + "(" + left + ", " + right + ")";
227237
}
@@ -232,6 +242,12 @@ export default class AcademicSubquery extends Morph {
232242
query = attr + comp + "'" + val + "'";
233243
}
234244

245+
if (query == "... parsed from ui") {
246+
lively.notify("LEFT", this.leftSubquery)
247+
lively.notify("RIGHT", this.rightSubquery)
248+
lively.notify("COMPLEX", this.isComplex)
249+
}
250+
235251
return query
236252
}
237253

0 commit comments

Comments
 (0)