@@ -39,6 +39,21 @@ class CategoriesTree {
3939 return this . _tree ;
4040 }
4141
42+ /**
43+ * @abstract The Custom Equations setter.
44+ *
45+ * The whole data set, JSON compatible, with categories and equations.
46+ * This variant adds also some Sample data.
47+ *
48+ * @async the async variant with Promise contract
49+ */
50+ async setCustomEquations ( value ) {
51+ var converted = this . convert ( value ) ;
52+ converted = await this . addSamples ( converted ) ;
53+ this . data = this . getCustomEquationsProxy ( converted ) ;
54+ this . initialise ( ) ; // it is essential to invoke it here before currentEquations
55+ }
56+
4257 /**
4358 * @abstract The Custom Equations setter.
4459 *
@@ -62,7 +77,6 @@ class CategoriesTree {
6277 /** @abstract Use this construct to make copies
6378 */
6479 getCustomEquationsProxy ( data ) {
65- var inst = this ;
6680 var rows = null ;
6781 rows = [ data ] ;
6882 return _proxy ( rows ) ;
@@ -71,7 +85,7 @@ class CategoriesTree {
7185 * @abstract Copy a single node with relevant data.
7286 */
7387 function _copy ( from ) {
74- var keys = [ 'text' , 'state' , 'attributes' , 'selected' ] ;
88+ var keys = [ 'text' , 'state' , 'attributes' , 'selected' , 'haveSamples' ] ;
7589 var to = { } ;
7690 for ( var key of Object . keys ( from ) ) {
7791 if ( keys . includes ( key ) ) {
@@ -513,6 +527,59 @@ class CategoriesTree {
513527 } ]
514528 } ;
515529 }
530+
531+ /**
532+ * @abstract Adds a Samples branch to the Categories tree.
533+ *
534+ * This routine secures against repeated insertions.
535+ *
536+ * @param from - the Categories (Custom Equations) tree as from JSON
537+ * @result the changed tree with added Samples
538+ */
539+ async addSamples ( from ) {
540+
541+ /**
542+ * Searches from a given start node down the hierarchy until node with text is found.
543+ */
544+ function getSamplesNode ( node , text ) {
545+
546+ return _traverse ( node . children ) ;
547+
548+ function _traverse ( nodes ) {
549+ for ( var node of nodes ) {
550+ if ( node . text === text ) {
551+ return node ;
552+ }
553+ var children = node . children ;
554+ if ( children && children . length ) {
555+ var found = _traverse ( children ) ;
556+ if ( found ) {
557+ return found ;
558+ }
559+ }
560+ }
561+ return undefined ;
562+ }
563+ }
564+
565+ /**
566+ * @abstract Loads a JSON file and returns the object.
567+ */
568+ async function loadSamples ( ) {
569+
570+ var response = await fetch ( 'formulas/sampleEquations.json' ) ;
571+ return await response . json ( ) ;
572+ }
573+
574+ if ( ! from . haveSamples && ! getSamplesNode ( from , "Samples" ) ) {
575+
576+ var samples = await loadSamples ( ) ;
577+ samples = getSamplesNode ( samples , "Samples" ) ;
578+ from . children . push ( samples )
579+ from . haveSamples = true ;
580+ }
581+ return from ;
582+ }
516583
517584 /**
518585 * @abstract Sort routine. Sorts the whole category tree.
@@ -618,7 +685,6 @@ class CategoriesTree {
618685 function _traverse ( nodes , ...args ) {
619686 for ( var node of nodes ) {
620687 inst . findNode ( node ) ;
621- // console.debug(`Traversing : row : ${node.text}, state : ${node.state}, isLeaf : ${inst.tree.tree('isLeaf', node.target)} `);
622688 func ( node , ...args ) ;
623689 var children = node . children ;
624690 if ( children && children . length ) {
0 commit comments