1
+ const FrameHelper = require ( './frameHelper' ) ;
1
2
/**
2
3
* A frame pattern can have the following variables
3
4
* renderer : object, property, data, * - matches a specific part of the frame
4
5
* label : matches the label of a property
5
- * frame_type: object, data, document, oneOf
6
+ * frame_type: object, data, document, id, oneOf
6
7
* subject: id of the subject
7
8
* subjectClass: class of the subject
8
9
* range: type of a property (applies to property and data)
@@ -209,7 +210,7 @@ FramePattern.prototype.checkLabel = function (rtype, frame) {
209
210
} ;
210
211
211
212
FramePattern . prototype . IDsMatch = function ( ida , idb ) {
212
- return TerminusClient . FrameHelper . compareIDs ( ida , idb ) ;
213
+ return FrameHelper . compareIDs ( ida , idb ) ;
213
214
} ;
214
215
215
216
FramePattern . prototype . classIDsMatch = function ( ida , idb ) {
@@ -314,6 +315,41 @@ FrameRule.prototype.unpack = function(arr, nonstring){
314
315
return str ;
315
316
}
316
317
318
+ FrameRule . prototype . unpackFeatures = function ( feats ) {
319
+ let extensions = { } ;
320
+ var fstr = "" ;
321
+ for ( var i = 0 ; i < feats . length ; i ++ ) {
322
+ if ( typeof feats [ i ] == "string" ) {
323
+ fstr += '"' + feats [ i ] + '"' ;
324
+ }
325
+ else if ( typeof feats [ i ] == "object" ) {
326
+ var fid = Object . keys ( feats [ i ] ) [ 0 ] ;
327
+ fstr += '"' + fid + '"' ;
328
+ for ( var prop in feats [ i ] [ fid ] ) {
329
+ extensions [ prop ] = feats [ i ] [ fid ] [ prop ] ;
330
+ }
331
+ }
332
+ if ( i < feats . length - 1 ) {
333
+ fstr += ", "
334
+ }
335
+ }
336
+ for ( var k = 0 ; k < Object . keys ( extensions ) . length ; k ++ ) {
337
+ var ext = Object . keys ( extensions ) [ k ] ;
338
+ var val = extensions [ ext ] ;
339
+ fstr += ")." + ext + "(" ;
340
+ if ( typeof val == "function" ) {
341
+ fstr += val ;
342
+ }
343
+ else if ( typeof val == "string" ) {
344
+ fstr += '"' + val + '"' ;
345
+ }
346
+ else if ( typeof val == "object" ) {
347
+ fstr += JSON . stringify ( val ) ;
348
+ }
349
+ }
350
+ return fstr ;
351
+ }
352
+
317
353
FrameRule . prototype . prettyPrint = function ( type ) {
318
354
//starts with obj. ...
319
355
if ( this . scope ( ) == "*" ) {
@@ -355,7 +391,13 @@ FrameRule.prototype.prettyPrint = function(type){
355
391
str += ".parent(" + this . parent . prettyPrint ( ) + ")" ;
356
392
}
357
393
if ( typeof this . depth ( ) != "undefined" ) {
358
- str += ".depth('" + this . depth ( ) + "')" ;
394
+ var d = this . depth ( ) ;
395
+ if ( typeof d == "string" ) {
396
+ str += ".depth('" + this . depth ( ) + "')" ;
397
+ }
398
+ else {
399
+ str += ".depth(" + this . depth ( ) + ")" ;
400
+ }
359
401
}
360
402
if ( typeof this . index ( ) != "undefined" ) {
361
403
str += ".index(" + this . unpack ( this . index ( ) , true ) + ")" ;
@@ -367,7 +409,7 @@ FrameRule.prototype.prettyPrint = function(type){
367
409
str += ".renderer('" + this . renderer ( ) + "')" ;
368
410
}
369
411
if ( typeof this . render ( ) != "undefined" ) {
370
- str += ".render(" + this . render ( ) + ")" ;
412
+ str += ".render(" + this . render + ")" ;
371
413
}
372
414
if ( typeof this . compare ( ) != "undefined" ) {
373
415
str += ".compare(" + this . compare ( ) + ")" ;
@@ -390,28 +432,26 @@ FrameRule.prototype.prettyPrint = function(type){
390
432
if ( typeof this . header ( ) != "undefined" ) {
391
433
str += ".header(" + this . header ( ) + ")" ;
392
434
}
393
- if ( typeof this . features ( ) != "undefined" ) {
394
- str += ".features(" + this . unpack ( this . features ( ) ) + ")" ;
435
+ if ( typeof this . style ( ) != "undefined" ) {
436
+ str += ".style(\"" + this . style ( ) + "\ ")";
395
437
}
396
- if ( typeof this . headerFeatures ( ) != "undefined" ) {
397
- str += ".headerFeatures(" + this . unpack ( this . headerFeatures ( ) ) + ")" ;
438
+ if ( typeof this . headerStyle ( ) != "undefined" ) {
439
+ str += ".headerStyle(\"" + this . headerStyle ( ) + "\")" ;
440
+ }
441
+ if ( typeof this . args ( ) != "undefined" ) {
442
+ str += ".args(" + JSON . stringify ( this . args ( ) ) + ")" ;
398
443
}
399
444
if ( typeof this . showEmpty ( ) != "undefined" ) {
400
445
str += ".showEmpty(" + this . show_empty ( ) + ")" ;
401
446
}
402
447
if ( typeof this . dataviewer ( ) != "undefined" ) {
403
- str += ".dataviewer('" + this . dataviewer ( ) + "' )" ;
448
+ str += ".dataviewer(\"" + this . dataviewer ( ) + "\" )" ;
404
449
}
405
- if ( typeof this . args ( ) != "undefined" ) {
406
- str += ".args (" + JSON . stringify ( this . args ( ) ) + ")" ;
450
+ if ( typeof this . features ( ) != "undefined" ) {
451
+ str += ".features (" + this . unpackFeatures ( this . features ( ) ) + ")" ;
407
452
}
408
- if ( typeof this . featureRenderers ( ) != "undefined" ) {
409
- str += ".featureRenderers({" ;
410
- for ( var f in this . featureRenderers ( ) ) {
411
- str += JSON . stringify ( f ) + ":" + this . rule . feature_renderers [ f ] + "," ;
412
- }
413
- str = str . substring ( 0 , str . length - 1 ) ;
414
- str += "})" ;
453
+ if ( typeof this . headerFeatures ( ) != "undefined" ) {
454
+ str += ".headerFeatures(" + this . unpackFeatures ( this . headerFeatures ( ) ) + ")" ;
415
455
}
416
456
return str ;
417
457
}
@@ -424,7 +464,7 @@ FrameRule.prototype.scope = function(scope){
424
464
425
465
FrameRule . prototype . frameType = function ( ...frametype ) {
426
466
if ( typeof frametype == "undefined" || frametype . length == 0 ) return this . rule . pattern . frametype ;
427
- this . rule . pattern . frametype = frame_type ;
467
+ this . rule . pattern . frametype = frametype ;
428
468
return this ;
429
469
} ;
430
470
@@ -509,57 +549,172 @@ FrameRule.prototype.status = function(...status){
509
549
return this ;
510
550
} ;
511
551
512
- FrameRule . prototype . renderer = function ( rend ) {
513
- if ( ! rend ) return this . rule . renderer ;
514
- this . rule . renderer = rend ;
515
- return this ;
516
- }
552
+ /**
553
+ * The below correspond to rule actions - what happens when a pattern matches
554
+ */
517
555
556
+ /**
557
+ * These onces can apply to features - if the rule includes an earlier features or header_features call, subsequent render, style, hidden and args apply to it
558
+ */
518
559
FrameRule . prototype . render = function ( func ) {
519
- if ( func ) {
560
+ if ( ! func ) return this . rule . render ;
561
+ var hf = this . headerFeatures ( ) ;
562
+ var f = this . features ( ) ;
563
+ if ( hf && hf . length ) {
564
+ var feats = this . applyFeatureProperty ( hf , "render" , func ) ;
565
+ this . headerFeatures ( ...feats ) ;
566
+ }
567
+ else if ( f && f . length ) {
568
+ var feats = this . applyFeatureProperty ( f , "render" , func ) ;
569
+ this . features ( ...feats ) ;
570
+ }
571
+ else {
520
572
this . rule . render = func ;
521
- return this ;
522
573
}
523
- return func ;
574
+ return this ;
575
+ }
576
+
577
+ FrameRule . prototype . style = function ( style ) {
578
+ if ( typeof style == "undefined" ) return this . rule . style ;
579
+ var hf = this . headerFeatures ( ) ;
580
+ var f = this . features ( ) ;
581
+ if ( hf && hf . length ) {
582
+ var feats = this . applyFeatureProperty ( hf , "style" , style ) ;
583
+ this . headerFeatures ( ...feats ) ;
584
+ }
585
+ else if ( f && f . length ) {
586
+ var feats = this . applyFeatureProperty ( f , "style" , style ) ;
587
+ this . features ( ...feats ) ;
588
+ }
589
+ else {
590
+ this . rule . style = style ;
591
+ }
592
+ return this ;
593
+ } ;
594
+
595
+ /**
596
+ * The frame or feature will be hidden or unhidden (boolean)
597
+ */
598
+ FrameRule . prototype . hidden = function ( m ) {
599
+ if ( typeof m == "undefined" ) return this . rule . hidden ;
600
+ var hf = this . headerFeatures ( ) ;
601
+ var f = this . features ( ) ;
602
+ if ( hf && hf . length ) {
603
+ var feats = this . applyFeatureProperty ( hf , "hidden" , m ) ;
604
+ this . headerFeatures ( ...feats ) ;
605
+ }
606
+ else if ( f && f . length ) {
607
+ var feats = this . applyFeatureProperty ( f , "hidden" , m ) ;
608
+ this . features ( ...feats ) ;
609
+ }
610
+ else {
611
+ this . rule . hidden = m ;
612
+ }
613
+ return this ;
524
614
}
525
615
616
+ /**
617
+ * Specifies arguments to a renderer
618
+ */
526
619
FrameRule . prototype . args = function ( json ) {
527
620
if ( ! json ) return this . rule . args ;
528
- this . rule . args = json ;
621
+ var hf = this . headerFeatures ( ) ;
622
+ var f = this . features ( ) ;
623
+ if ( hf && hf . length ) {
624
+ var feats = this . applyFeatureProperty ( hf , "args" , json ) ;
625
+ this . headerFeatures ( ...feats ) ;
626
+ }
627
+ else if ( f && f . length ) {
628
+ var feats = this . applyFeatureProperty ( f , "args" , json ) ;
629
+ this . features ( ...feats ) ;
630
+ }
631
+ else {
632
+ this . rule . args = json ;
633
+ }
634
+ return this ;
635
+ }
636
+
637
+ /**
638
+ * Adds a property to a feature array
639
+ */
640
+ FrameRule . prototype . applyFeatureProperty = function ( feats , prop , val ) {
641
+ var nfeats = [ ] ;
642
+ for ( var i = 0 ; i < feats . length ; i ++ ) {
643
+ if ( typeof feats [ i ] == "string" ) {
644
+ var nfeat = { } ;
645
+ nfeat [ feats [ i ] ] = { } ;
646
+ nfeat [ feats [ i ] ] [ prop ] = val ;
647
+ nfeats . push ( nfeat ) ;
648
+ }
649
+ else if ( typeof feats [ i ] == "object" ) {
650
+ let fkey = Object . keys ( feats [ i ] ) [ 0 ] ;
651
+ if ( fkey ) {
652
+ var nfeat = feats [ i ] ;
653
+ nfeat [ fkey ] [ prop ] = val ;
654
+ nfeats . push ( nfeat ) ;
655
+ }
656
+ }
657
+ }
658
+ return nfeats ;
659
+ }
660
+
661
+
662
+
663
+ /**
664
+ * These apply to regular frames
665
+ */
666
+ FrameRule . prototype . renderer = function ( rend ) {
667
+ if ( ! rend ) return this . rule . renderer ;
668
+ this . rule . renderer = rend ;
669
+ return this ;
670
+ }
671
+
672
+ FrameRule . prototype . headerStyle = function ( hstyle ) {
673
+ if ( ! hstyle ) return this . rule . header_style ;
674
+ this . rule . header_style = hstyle ;
529
675
return this ;
530
676
}
531
677
678
+ /**
679
+ * Function which specifies the ordering of values in a frame
680
+ */
532
681
FrameRule . prototype . compare = function ( func ) {
533
682
if ( ! func ) return this . rule . compare ;
534
683
this . rule . compare = func ;
535
684
return this ;
536
685
}
537
686
687
+ /**
688
+ * Edit / view mode
689
+ */
538
690
FrameRule . prototype . mode = function ( m ) {
539
691
if ( ! m ) return this . rule . mode ;
540
692
this . rule . mode = m ;
541
693
return this ;
542
694
}
543
695
696
+ /**
697
+ * Specifies whether a frame should be presented in collapsed form or not
698
+ */
544
699
FrameRule . prototype . collapse = function ( m ) {
545
700
if ( ! m ) return this . rule . collapse ;
546
701
this . rule . collapse = m ;
547
702
return this ;
548
703
}
549
704
550
-
551
- FrameRule . prototype . hidden = function ( m ) {
552
- if ( ! m ) return this . rule . hidden ;
553
- this . rule . hidden = m ;
554
- return this ;
555
- }
705
+ /**
706
+ * Specifies whether a frame should include a view selector
707
+ */
556
708
557
709
FrameRule . prototype . view = function ( m ) {
558
710
if ( ! m ) return this . rule . view ;
559
711
this . rule . view = m ;
560
712
return this ;
561
713
}
562
714
715
+ /**
716
+ * Should actions which are disabled in the given context be displayed?
717
+ */
563
718
FrameRule . prototype . showDisabledButtons = function ( m ) {
564
719
if ( ! m ) return this . rule . show_disabled_buttons ;
565
720
this . rule . show_disabled_buttons = m ;
@@ -584,12 +739,6 @@ FrameRule.prototype.header = function(m){
584
739
return this ;
585
740
}
586
741
587
- FrameRule . prototype . featureRenderers = function ( m ) {
588
- if ( ! m ) return this . rule . feature_renderers ;
589
- this . rule . feature_renderers = m ;
590
- return this ;
591
- }
592
-
593
742
FrameRule . prototype . showEmpty = function ( m ) {
594
743
if ( ! m ) return this . rule . show_empty ;
595
744
this . rule . show_empty = m ;
0 commit comments