1+ const FrameHelper = require ( './frameHelper' ) ;
12/**
23 * A frame pattern can have the following variables
34 * renderer : object, property, data, * - matches a specific part of the frame
45 * label : matches the label of a property
5- * frame_type: object, data, document, oneOf
6+ * frame_type: object, data, document, id, oneOf
67 * subject: id of the subject
78 * subjectClass: class of the subject
89 * range: type of a property (applies to property and data)
@@ -209,7 +210,7 @@ FramePattern.prototype.checkLabel = function (rtype, frame) {
209210} ;
210211
211212FramePattern . prototype . IDsMatch = function ( ida , idb ) {
212- return TerminusClient . FrameHelper . compareIDs ( ida , idb ) ;
213+ return FrameHelper . compareIDs ( ida , idb ) ;
213214} ;
214215
215216FramePattern . prototype . classIDsMatch = function ( ida , idb ) {
@@ -314,6 +315,41 @@ FrameRule.prototype.unpack = function(arr, nonstring){
314315 return str ;
315316}
316317
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+
317353FrameRule . prototype . prettyPrint = function ( type ) {
318354 //starts with obj. ...
319355 if ( this . scope ( ) == "*" ) {
@@ -355,7 +391,13 @@ FrameRule.prototype.prettyPrint = function(type){
355391 str += ".parent(" + this . parent . prettyPrint ( ) + ")" ;
356392 }
357393 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+ }
359401 }
360402 if ( typeof this . index ( ) != "undefined" ) {
361403 str += ".index(" + this . unpack ( this . index ( ) , true ) + ")" ;
@@ -367,7 +409,7 @@ FrameRule.prototype.prettyPrint = function(type){
367409 str += ".renderer('" + this . renderer ( ) + "')" ;
368410 }
369411 if ( typeof this . render ( ) != "undefined" ) {
370- str += ".render(" + this . render ( ) + ")" ;
412+ str += ".render(" + this . render + ")" ;
371413 }
372414 if ( typeof this . compare ( ) != "undefined" ) {
373415 str += ".compare(" + this . compare ( ) + ")" ;
@@ -390,28 +432,26 @@ FrameRule.prototype.prettyPrint = function(type){
390432 if ( typeof this . header ( ) != "undefined" ) {
391433 str += ".header(" + this . header ( ) + ")" ;
392434 }
393- if ( typeof this . features ( ) != "undefined" ) {
394- str += ".features(" + this . unpack ( this . features ( ) ) + ")" ;
435+ if ( typeof this . style ( ) != "undefined" ) {
436+ str += ".style(\"" + this . style ( ) + "\ ")";
395437 }
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 ( ) ) + ")" ;
398443 }
399444 if ( typeof this . showEmpty ( ) != "undefined" ) {
400445 str += ".showEmpty(" + this . show_empty ( ) + ")" ;
401446 }
402447 if ( typeof this . dataviewer ( ) != "undefined" ) {
403- str += ".dataviewer('" + this . dataviewer ( ) + "' )" ;
448+ str += ".dataviewer(\"" + this . dataviewer ( ) + "\" )" ;
404449 }
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 ( ) ) + ")" ;
407452 }
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 ( ) ) + ")" ;
415455 }
416456 return str ;
417457}
@@ -424,7 +464,7 @@ FrameRule.prototype.scope = function(scope){
424464
425465FrameRule . prototype . frameType = function ( ...frametype ) {
426466 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 ;
428468 return this ;
429469} ;
430470
@@ -509,57 +549,172 @@ FrameRule.prototype.status = function(...status){
509549 return this ;
510550} ;
511551
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+ */
517555
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+ */
518559FrameRule . 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 {
520572 this . rule . render = func ;
521- return this ;
522573 }
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 ;
524614}
525615
616+ /**
617+ * Specifies arguments to a renderer
618+ */
526619FrameRule . prototype . args = function ( json ) {
527620 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 ;
529675 return this ;
530676}
531677
678+ /**
679+ * Function which specifies the ordering of values in a frame
680+ */
532681FrameRule . prototype . compare = function ( func ) {
533682 if ( ! func ) return this . rule . compare ;
534683 this . rule . compare = func ;
535684 return this ;
536685}
537686
687+ /**
688+ * Edit / view mode
689+ */
538690FrameRule . prototype . mode = function ( m ) {
539691 if ( ! m ) return this . rule . mode ;
540692 this . rule . mode = m ;
541693 return this ;
542694}
543695
696+ /**
697+ * Specifies whether a frame should be presented in collapsed form or not
698+ */
544699FrameRule . prototype . collapse = function ( m ) {
545700 if ( ! m ) return this . rule . collapse ;
546701 this . rule . collapse = m ;
547702 return this ;
548703}
549704
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+ */
556708
557709FrameRule . prototype . view = function ( m ) {
558710 if ( ! m ) return this . rule . view ;
559711 this . rule . view = m ;
560712 return this ;
561713}
562714
715+ /**
716+ * Should actions which are disabled in the given context be displayed?
717+ */
563718FrameRule . prototype . showDisabledButtons = function ( m ) {
564719 if ( ! m ) return this . rule . show_disabled_buttons ;
565720 this . rule . show_disabled_buttons = m ;
@@ -584,12 +739,6 @@ FrameRule.prototype.header = function(m){
584739 return this ;
585740}
586741
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-
593742FrameRule . prototype . showEmpty = function ( m ) {
594743 if ( ! m ) return this . rule . show_empty ;
595744 this . rule . show_empty = m ;
0 commit comments