Skip to content

Commit d04ceb5

Browse files
authored
Merge pull request #67 from daveseah/dev-bl/template-select
Console Script to Select Database + Template
2 parents 5e576c5 + 2ec8e34 commit d04ceb5

File tree

13 files changed

+341
-58
lines changed

13 files changed

+341
-58
lines changed

build/app/assets/index.ejs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
min-height: 100%
2424
}
2525
</style>
26+
<script src="netcreate-config.js">
27+
// auto generated netcreate config script
28+
</script>
2629
<script>
30+
// NC_UNISYS are network parameters
2731
if (window.NC_UNISYS) {
2832
let err = `**FATAL ERROR** NC_UNISYS already initialized.\n\n`;
2933
err += `Take a screen shot with your phone and report the error!`
@@ -45,6 +49,7 @@
4549
uport : '<%=uport%>'
4650
}
4751
};
52+
//
4853
window.NC_DBG = null; // {} or null
4954
if (window.NC_DBG) {
5055
Object.assign(window.NC_DBG, {

build/app/assets/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
min-height: 100%
2222
}
2323
</style>
24+
<script src="netcreate-config.js">
25+
// auto generated netcreate config script
26+
</script>
2427
<script>
2528
// index.html is loaded only by standalone mode without a server
2629
// server-based operation uses index.ejs

build/app/assets/netcreate-config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// this file generated by NC command
2+
const NC_CONFIG = {
3+
dataset: "netcreate"
4+
};
5+
if (typeof process === "object") module.exports = NC_CONFIG;
6+
if (typeof window === "object") window.NC_CONFIG = NC_CONFIG;
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
{
2+
3+
"_comments": [ "/// NetCreate Template File ///",
4+
"/// IMPORTANT: As of 1/31/2019, the following functionality is available: ///",
5+
"/// * `label` parameter for each field may be customized ///",
6+
"/// * `hidden` parameter may be used to hide fields ///",
7+
"/// * changing the order of the `options` WILL change their order on selections ///",
8+
"/// * changing the order of the prompts themselves will NOT change their order ///",
9+
"/// * `duplicateWarning` is used in NodeSelector ///",
10+
"/// * `sourceNodeIsLockedMessage` is used in NodeSelector ///",
11+
"/// * `edgeIsLockedMessage` is used in EdgeEditor ///",
12+
"/// The rest of the definitions are there as placeholders for future functionality ///"
13+
],
14+
15+
"name": "Tacitus",
16+
17+
"description": "Tacitus with all genders included in a single Person node type",
18+
19+
20+
"nodePrompts": {
21+
"label": {
22+
"label": "Label",
23+
"help": "A short title for the node",
24+
"duplicateWarning": "You’re entering a duplicate node. Do you want to View the Existing node, or Continue creating?",
25+
"sourceNodeIsLockedMessage": "This node is currently being edited by someone else, please try again later.",
26+
"_cmt4": "/// `Label` is always required and cannot be hidden"
27+
},
28+
"type": {
29+
"label": "Type",
30+
"help": "Multiple people are a 'Group'",
31+
"hidden": false,
32+
"options": [
33+
{
34+
"_comment": "Default color when nothing is selected",
35+
"id": "",
36+
"label": "",
37+
"color": "#EEEEEE"
38+
},
39+
{
40+
"id": "person",
41+
"label": "Person",
42+
"color": "#aabaf2"
43+
},
44+
{
45+
"id": "group",
46+
"label": "Group",
47+
"color": "#3399FF"
48+
},
49+
{
50+
"id": "place",
51+
"label": "Place",
52+
"color": "#00FF00"
53+
},
54+
{
55+
"id": "thing",
56+
"label": "Thing",
57+
"color": "#FF0000"
58+
},
59+
{
60+
"id": "event",
61+
"label": "Event",
62+
"color": "#009999"
63+
}
64+
]
65+
},
66+
"degrees": {
67+
"label": "Degrees",
68+
"help": "Number of edges.",
69+
"hidden": false
70+
},
71+
"notes": {
72+
"label": "Significance",
73+
"help": "Add some details.",
74+
"hidden": false
75+
},
76+
"info": {
77+
"label": "Geocode or Date",
78+
"help": "Use latitude/longitude or a date mm/dd/yyy",
79+
"hidden": true
80+
},
81+
"delete": {
82+
"hidden": false
83+
}
84+
},
85+
86+
87+
88+
"edgePrompts": {
89+
"edgeIsLockedMessage": "This edge is currently being edited by someone else, please try again later.",
90+
"source": {
91+
"label": "Source",
92+
"help": "",
93+
"hidden": false
94+
},
95+
"type": {
96+
"label": "Type",
97+
"help": "",
98+
"hidden": false,
99+
"options": [
100+
{
101+
"id": "notSelected",
102+
"label": ""
103+
},
104+
{
105+
"id": "peaceful",
106+
"label": "has peaceful, familial or conversational interaction with"
107+
},
108+
{
109+
"id": "martial",
110+
"label": "has martial or adversarial interaction with"
111+
},
112+
{
113+
"id": "writes",
114+
"label": "sends written communication to"
115+
},
116+
{
117+
"id": "member",
118+
"label": "is a group member of"
119+
},
120+
{
121+
"id": "participate",
122+
"label": "participates in"
123+
},
124+
{
125+
"id": "visit",
126+
"label": "makes visit to"
127+
}
128+
]
129+
},
130+
"target": {
131+
"label": "Target",
132+
"help": "",
133+
"hidden": false
134+
},
135+
"notes": {
136+
"label": "Signficance",
137+
"help": "",
138+
"hidden": false
139+
},
140+
"info": {
141+
"label": "Approximate Date of Interaction",
142+
"help": "",
143+
"hidden": true
144+
},
145+
"citation": {
146+
"label": "Citation",
147+
"help": "Enter Chapter number.",
148+
"hidden": false
149+
}
150+
}
151+
152+
}

build/app/system/datastore.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ DSTOR.PromiseJSONFile = function(jsonFile) {
146146
D3DATA = Object.assign(D3DATA, JSON.parse(data));
147147
resolve(D3DATA);
148148
});
149-
xobj.open("GET", `data/${jsonFile}`, true);
149+
xobj.open("GET", `${jsonFile}`, true);
150150
xobj.send();
151151
});
152152
return promise;

build/app/unisys/server-database.js

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ const SESSION = require("../unisys/common-session");
1818
const LOGGER = require("../unisys/server-logger");
1919
const PROMPTS = require("../system/util/prompts");
2020
const PR = PROMPTS.Pad("ServerDB");
21-
const DB_FILE = "./runtime/netcreate.loki";
21+
const RUNTIMEPATH = './runtime/';
22+
const TEMPLATEPATH = './app/assets/templates/';
2223
const DB_CLONEMASTER = "blank.loki";
24+
const NC_CONFIG = require("../assets/netcreate-config");
2325

2426
/// MODULE-WIDE VARS //////////////////////////////////////////////////////////
2527
/// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
@@ -32,28 +34,34 @@ let NODES; // loki "nodes" collection
3234
let EDGES; // loki "edges" collection
3335
let m_locked_nodes;
3436
let m_locked_edges;
37+
let TEMPLATE;
3538

3639
/// API METHODS ///////////////////////////////////////////////////////////////
3740
/// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
3841
let DB = {};
3942
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
4043
/*/ API: Initialize the database
4144
/*/
42-
DB.InitializeDatabase = function(options = {}) {
43-
FS.ensureDirSync(PATH.dirname(DB_FILE));
44-
if (!FS.existsSync(DB_FILE)) {
45-
console.log(PR, `NO EXISTING DATABASE ${DB_FILE}, so creating BLANK DATABASE...`);
45+
DB.InitializeDatabase = function (options = {}) {
46+
47+
let dataset = NC_CONFIG.dataset;
48+
let db_file = m_GetValidDBFilePath(dataset);
49+
FS.ensureDirSync(PATH.dirname(db_file));
50+
if (!FS.existsSync(db_file)) {
51+
console.log(PR, `NO EXISTING DATABASE ${db_file}, so creating BLANK DATABASE...`);
4652
}
53+
console.log(PR, `LOADING DATABASE ${db_file}`);
4754
let ropt = {
4855
autoload: true,
4956
autoloadCallback: f_DatabaseInitialize,
5057
autosave: true,
5158
autosaveCallback: f_AutosaveStatus,
52-
autosaveInterval: 4000 // save every four seconds
59+
autosaveInterval: 4000, // save every four seconds
5360
};
5461
ropt = Object.assign(ropt, options);
55-
m_db = new Loki(DB_FILE, ropt);
62+
m_db = new Loki(db_file, ropt);
5663
m_options = ropt;
64+
m_options.db_file = db_file; // store for use by DB.WriteJSON
5765

5866
// callback on load
5967
function f_DatabaseInitialize() {
@@ -118,7 +126,20 @@ DB.InitializeDatabase = function(options = {}) {
118126
console.log(PR,`DATABASE LOADED! m_max_nodeID '${m_max_nodeID}', m_max_edgeID '${m_max_edgeID}'`);
119127
m_db.saveDatabase();
120128

121-
if (typeof m_options.onLoadComplete==='function') {
129+
// LOAD TEMPLATE - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
130+
let templatePath = RUNTIMEPATH + NC_CONFIG.dataset + ".template";
131+
FS.ensureDirSync(PATH.dirname(templatePath));
132+
// Does the template exist?
133+
if (!FS.existsSync(templatePath)) {
134+
console.log(PR, `NO EXISTING TEMPLATE ${templatePath}, so cloning default template...`);
135+
FS.copySync(TEMPLATEPATH+'_default.template', templatePath);
136+
}
137+
console.log(PR, `LOADING TEMPLATE ${templatePath}`);
138+
// Now load it
139+
TEMPLATE = FS.readJsonSync(templatePath);
140+
141+
// Call complete callback
142+
if (typeof m_options.onLoadComplete === 'function') {
122143
m_options.onLoadComplete();
123144
}
124145
} // end f_DatabaseInitialize
@@ -140,7 +161,7 @@ DB.PKT_GetDatabase = function(pkt) {
140161
let edges = EDGES.chain().data({ removeMeta: true });
141162
if (DBG) console.log(PR,`PKT_GetDatabase ${pkt.Info()} (loaded ${nodes.length} nodes, ${edges.length} edges)`);
142163
LOGGER.Write(pkt.Info(), `getdatabase`);
143-
return { nodes, edges };
164+
return { d3data: { nodes, edges }, template: TEMPLATE };
144165
};
145166
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
146167
/*/ API: reset database from scratch
@@ -430,8 +451,13 @@ DB.FilterEdgeLog = function(edge) {
430451
/*/ called by brunch to generate an up-to-date JSON file to path.
431452
creates the path if it doesn't exist
432453
/*/
433-
DB.WriteJSON = function( filePath ) {
434-
let db = new Loki(DB_FILE,{
454+
DB.WriteDbJSON = function (filePath) {
455+
let dataset = NC_CONFIG.dataset;
456+
457+
// Ideally we should use m_otions value, but in standlone mode,
458+
// m_options might not be defined.
459+
let db_file = m_options ? m_options.db_file : m_GetValidDBFilePath(dataset);
460+
let db = new Loki(db_file,{
435461
autoload: true,
436462
autoloadCallback: () => {
437463
if (typeof filePath==='string') {
@@ -446,14 +472,29 @@ DB.WriteJSON = function( filePath ) {
446472
FS.ensureDirSync(PATH.dirname( filePath ));
447473
if (DBG) console.log(PR,`writing file ${filePath}`);
448474
FS.writeFileSync( filePath, json );
449-
console.log(PR,`*** WROTE JSON DATABASE`);
475+
console.log(PR, `*** WROTE JSON DATABASE ${filePath}`);
450476
} else {
451477
console.log(PR,`ERR path ${filePath} must be a pathname`);
452478
}
453479
}
454480
}
455481
);
456482
};
483+
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
484+
/*/ called by brunch to generate an up-to-date Template file to path.
485+
creates the path if it doesn't exist
486+
/*/
487+
DB.WriteTemplateJSON = function (filePath) {
488+
let templatePath = RUNTIMEPATH + NC_CONFIG.dataset + ".template";
489+
FS.ensureDirSync(PATH.dirname(templatePath));
490+
// Does the template exist?
491+
if (!FS.existsSync(templatePath)) {
492+
console.error(PR, `ERR could not find template ${templatePath}`);
493+
} else {
494+
FS.copySync(templatePath, filePath);
495+
console.log(PR, `*** COPIED TEMPLATE ${templatePath} to ${filePath}`);
496+
}
497+
};
457498

458499
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
459500
/// utility function for cleaning nodes with numeric id property
@@ -486,6 +527,17 @@ function m_CleanID(prompt, id) {
486527
}
487528
return id;
488529
}
530+
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
531+
/// utility function for getting a valid file path
532+
function m_GetValidDBFilePath(dataset) {
533+
// validate dataset name
534+
let regex = /^([A-z0-9-_+./])*$/; // Allow _ - + . /, so nested pathways are allowed
535+
if (!regex.test(dataset)) {
536+
console.error(PR, `Trying to initialize database with bad dataset name: ${dataset}`);
537+
}
538+
539+
return RUNTIMEPATH + dataset + ".loki";
540+
}
489541

490542
/// EXPORT MODULE DEFINITION //////////////////////////////////////////////////
491543
/// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

build/app/view/netcreate/components/EdgeEditor.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ class EdgeEditor extends UNISYS.Component {
774774
<FormText><b>EDGE {formData.id}</b></FormText>
775775
<FormGroup row>
776776
<Col sm={3}>
777-
<Label for="source" className="small text-muted">Source</Label>
777+
<Label for="source" className="small text-muted">{edgePrompts.source.label}</Label>
778778
</Col>
779779
<Col sm={9}>
780780
<AutoComplete
@@ -794,7 +794,7 @@ class EdgeEditor extends UNISYS.Component {
794794
</FormGroup>
795795
<FormGroup row hidden={edgePrompts.type.hidden}>
796796
<Col sm={3}>
797-
<Label for="relationship" className="small text-muted">Type</Label>
797+
<Label for="relationship" className="small text-muted">{edgePrompts.type.label}</Label>
798798
</Col>
799799
<Col sm={9}>
800800
<Input type="select" name="relationship" id="relationship"
@@ -810,7 +810,7 @@ class EdgeEditor extends UNISYS.Component {
810810
</FormGroup>
811811
<FormGroup row>
812812
<Col sm={3}>
813-
<Label for="nodeLabel" className="small text-muted">Target</Label>
813+
<Label for="nodeLabel" className="small text-muted">{edgePrompts.target.label}</Label>
814814
</Col>
815815
<Col sm={9}>
816816
<AutoComplete

0 commit comments

Comments
 (0)