This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
blessedoo.js
155 lines (134 loc) · 4.46 KB
/
blessedoo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var fs = require('fs'),
xml2js = require('xml2js'),
blessed = require('blessed');
var blessedoo = function(config) {
var views = {};
var elementsWithId = {};
config = typeof config === 'object' ? config : {};
var screen = blessed.screen(config);
screen.key('q', function() {
process.exit(0);
});
function getEventAttributes(list) {
return list.filter(function(str) {
return str.indexOf('on') === 0; //onclick, onpress etc
});
}
function getNonBlessedNodes(list) {
return list.filter(function(str) {
return str.indexOf('blessed:') !== 0 && str !== 'attributes';
});
}
function getBlessedNodes(list) {
return list.filter(function(str) {
return str.indexOf('blessed:') === 0;
});
}
function getBlessedName(str) {
return str.indexOf('blessed:') === 0 && str.substring(8);
}
function flattenAttributeNode(options, key, node) {
// options = {}, key = 'style', node = dict
var o = {};
function flatten(obj) {
var next = obj[Object.keys(obj)[0]];
if(typeof(next) == 'object')
return flatten(next);
return obj;
}
Object.keys(node).forEach(function(subkey) {
var value = node[subkey];
if(subkey == 'attributes') {
// handle attributes
var tmpKey = Object.keys(value)[0];
o[tmpKey] = value[tmpKey];
} else {
// handle object
o[subkey] = flatten(value);
}
});
options[key] = o;
}
function cleanAttributes(attributes) {
Object.keys(attributes).forEach(function(k) {
if(attributes[k] === 'true')
attributes[k] = true;
if(~~attributes[k] === parseInt(attributes[k], 10))
attributes[k] = ~~attributes[k];
});
return attributes;
}
function parseView(data, context, callback) {
var rootType = Object.getOwnPropertyNames(data);
if(rootType.length !== 1 || !rootType[0] || !getBlessedName(rootType[0]))
return callback(new Error('view must contain one single root blessed node'));
// take the zeroth node (since there should be one root)...
rootType = rootType[0];
// if it's null, then the prop doesn't exist in blessed and cannot be used... maybe check if function
if(!blessed[getBlessedName(rootType)])
return callback(new Error('invalid blessed type used for root node'));
var rootElement = null;
function traverseXMLNode(xmlKey, xmlNode, parent) {
//console.log('key -> ', xmlKey, ' :: ', xmlNode);
var keys = Object.keys(xmlNode);
//var options = xmlNode.attributes;
var options = cleanAttributes(xmlNode.attributes) || {};
var events = getEventAttributes(Object.keys(options));
var blessedNodes = getBlessedNodes(keys);
var nonBlessedNodes = getNonBlessedNodes(keys);
options.parent = parent;
nonBlessedNodes.forEach(function(key) {
flattenAttributeNode(options, key, xmlNode[key][0]);
//options[key] = flattenAttributeNode(xmlNode[key][0]); // this wont work for bg
});
var element = blessed[getBlessedName(xmlKey)](options);
if(options && options.id)
elementsWithId[options.id] = element;
events.forEach(function(e) {
element.on(e.substring(2), context[options[e]]);
});
if(parent === null)
rootElement = element;
blessedNodes.forEach(function(key) {
for(var i = 0; i < xmlNode[key].length; i++) {
traverseXMLNode(key, xmlNode[key][i], element);
}
});
}
traverseXMLNode(rootType, data[rootType], null);
callback(null, rootElement);
return rootElement;
}
return {
getElementById: function(id) {
return elementsWithId[id] || null;
},
getScreen: function() { return screen; },
render: function() {
screen.render();
},
setView: function(element) {
screen.append(element);
screen.render();
},
setViewFromFilename: function(filename) {
screen.append(views[filename]);
screen.render();
},
loadViews: function(files) {
files.forEach(loadView);
},
loadView: function(filename, context, callback) {
var parser = new xml2js.Parser({attrkey: 'attributes'});
parser.addListener('end', function(data) {
views[filename] = parseView(data, context, callback);
});
fs.readFile(filename, function(err, data) {
if(err)
return callback(err);
parser.parseString(data);
});
}
};
};
module.exports = blessedoo;