11import Morph from 'src/components/widgets/lively-morph.js' ;
22import ohm from "https://unpkg.com/[email protected] /dist/ohm.js" ; 3- import MicrosoftAcademicEntities from "src/client/literature.js" ;
3+ import { Author , Paper , MicrosoftAcademicEntities } from "src/client/literature.js" ;
44
55var g = ohm . grammar (
66 `Academic {
@@ -256,8 +256,8 @@ export default class AcademicSubquery extends Morph {
256256 this . textContent = q ;
257257
258258 var match = g . match ( q ) ;
259- var queryObject = s ( match ) . interpret ( ) ;
260- this . ui = await this . queryToView ( queryObject ) ;
259+ this . ast = s ( match ) . interpret ( ) ;
260+ this . ui = await this . queryToView ( this . ast ) ;
261261
262262 this . updateView ( )
263263 }
@@ -268,6 +268,7 @@ export default class AcademicSubquery extends Morph {
268268 }
269269
270270 async setQueryObject ( o ) {
271+ this . ast = o
271272 this . ui = await this . queryToView ( o ) ;
272273
273274 this . updateView ( ) ;
@@ -316,35 +317,72 @@ export default class AcademicSubquery extends Morph {
316317 return query
317318 }
318319
319- toggleEditing ( ) {
320+ async toggleEditing ( ) {
320321 var queries = this . get ( "#inner" ) . querySelectorAll ( "[name='sub']" ) ;
321322 var edit = this . get ( '#edit' ) ;
322- edit . innerHTML = "" ;
323+ // edit.innerHTML = "";
323324
324325 // TODO: Hier this.editing = !this.editing und dann updateView()
325- if ( ! this . editing ) {
326- this . editing = true ;
327- edit . appendChild ( < i class = "fa fa-hand-paper-o" aria-hidden = "true" > </ i > ) ;
328- queries . forEach ( q => {
329- q . setAttribute ( "contenteditable" , true ) ;
330- q . style . cursor = "text" ;
331- } ) ;
332- } else {
333- this . editing = false ;
334- edit . appendChild ( < i class = "fa fa-pencil" aria-hidden = "true" > </ i > ) ;
335- queries . forEach ( q => {
336- q . setAttribute ( "contenteditable" , false ) ;
337- q . style . cursor = "grab" ;
338- } ) ;
339- }
340- }
341-
342- onMouseOver ( event ) {
343- this . style . color = "orange"
326+ this . editing = ! this . editing ;
327+ this . ui = await this . queryToView ( this . ast ) ; // TODO: sollte das hier in updateView passieren?
328+ this . updateView ( ) ;
329+ // TODO: Edit Zeichen in buildEditable ändern
330+
331+ /*
332+ edit.appendChild(<i class="fa fa-hand-paper-o" aria-hidden="true"></i>);
333+ edit.appendChild(<i class="fa fa-pencil" aria-hidden="true"></i>);
334+ */
344335 }
345336
346- onMouseOut ( event ) {
347- this . style . color = "black"
337+ // builds the UI in edit mode
338+ async buildEditable ( object ) {
339+ var inner = < span id = "inner" > </ span > ;
340+ var query = < span name = "sub" draggable = 'false' > </ span > ;
341+ var schema = await MicrosoftAcademicEntities . generateSchema ( "paper" ) ; // TODO: in der Klasse speichern?
342+
343+ // attribute
344+ var attribute = < select name = 'attribute' id = 'attribute' > </ select > ;
345+ var selectedAttribute ;
346+ schema . filter ( attr => attr . operations != "None" ) . forEach ( option => {
347+ var selected = ( option . name == object . attribute ) ;
348+ if ( selected ) { selectedAttribute = option ; }
349+ attribute . options . add ( new Option ( option . name , option . name , selected , selected ) )
350+ } )
351+ query . appendChild ( attribute ) ;
352+
353+ // comparator
354+ var mapOperationToSymbol = op => { switch ( op ) {
355+ case "Equals" :
356+ return [ "==" , "=" ] ;
357+ case "StartsWith" :
358+ return [ "=" ] ;
359+ case "IsBetween" :
360+ return [ ">" , ">=" , "<" , "<=" , "=" ] ;
361+ default :
362+ return op ;
363+ } // TODO: in der Klasse speichern?
364+ } ;
365+ var comparator = < select name = 'comparator' id = 'comparator' > </ select > ;
366+ selectedAttribute . operations . split ( ", " )
367+ . map ( operation => mapOperationToSymbol ( operation ) ) // map words to arrays of symbols
368+ . flat ( ) // flatten array of arrays
369+ . filter ( ( item , pos , self ) => self . indexOf ( item ) == pos ) // deduplicate
370+ . forEach ( option => {
371+ var selected = ( option == object . comparator ) ;
372+ comparator . options . add ( new Option ( option , option , selected , selected ) )
373+ } ) ;
374+ query . appendChild ( comparator ) ;
375+
376+ // value
377+ var value = < input id = "value" name = "value" value = { object . value } > </ input > ;
378+ // TODO: fit input type to attribute type
379+ query . appendChild ( value ) ;
380+
381+ inner . appendChild ( query ) ;
382+ var edit = < span id = "edit" title = "toggle edit mode" click = { ( ) => this . toggleEditing ( ) } > < i class = "fa fa-pencil" aria-hidden = "true" > </ i > </ span > ;
383+ edit . style . cursor = "pointer" ;
384+ inner . appendChild ( edit ) ;
385+ return inner ;
348386 }
349387
350388 async buildConjunctionQuery ( object ) {
@@ -372,6 +410,14 @@ export default class AcademicSubquery extends Morph {
372410 return inner ;
373411 }
374412
413+ onMouseOver ( event ) {
414+ this . style . color = "orange"
415+ }
416+
417+ onMouseOut ( event ) {
418+ this . style . color = "black"
419+ }
420+
375421 buildSimpleQuery ( object ) {
376422 var inner =
377423 < span class = "hover" contenteditable = "false" id = "inner" >
@@ -390,7 +436,6 @@ export default class AcademicSubquery extends Morph {
390436 } } > OR</ button >
391437 </ span >
392438 </ span > ;
393-
394439 var query = < span name = "sub" draggable = 'true' > </ span > ;
395440 [ object . attribute , object . comparator , object . value ] . forEach ( value => {
396441 query . appendChild ( < span class = "queryPart" name = "queryPart" > { value } </ span > )
@@ -407,14 +452,16 @@ export default class AcademicSubquery extends Morph {
407452
408453 async queryToView ( object ) {
409454 var inner = < span id = "inner" > </ span > ;
410-
411455 if ( object . type == "conjunction" ) {
412456 this . isComplex = true ;
413457 inner = await this . buildConjunctionQuery ( object ) ;
414- } else {
415- // "composite" or "simple"
458+ } else { // "composite" or "simple"
416459 this . isComplex = false ;
417- inner = this . buildSimpleQuery ( object ) ;
460+ if ( this . editing ) { // edit mode
461+ inner = await this . buildEditable ( object ) ;
462+ } else { // drag&drop / read mode
463+ inner = this . buildSimpleQuery ( object ) ;
464+ }
418465 }
419466 inner . setAttribute ( "type" , object . type ) ;
420467
0 commit comments