-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
129 lines (101 loc) · 3.44 KB
/
app.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
var _ = require('./utils'),
RenderApp = require('./render'),
createConText = require('@triskel/con-text')
var addDirectiveIf = require('./directives/if.js'),
addDirectiveRepeat= require('./directives/repeat.js'),
addDirectiveOn= require('./directives/on.js'),
addDirectiveBind= require('./directives/bind.js'),
addDirectiveClass = require('./directives/class.js')
function createApp(options) {
options = options || {}
var add_directives = _.extend({
if: true,
repeat: true,
bind: true,
on: true,
'class': true,
}, options.add_directives || {}),
directive_ns = options.directive_ns || 'data',
render_options = {}
var app = new RenderApp(render_options)
// Data envelope for RenderApp
var APP = Object.create( app ),
TEXT = createConText(APP)
// APP.directive = function (directive, initNode, with_node) {
//
// app.directive(directive, function () {
// // this.watchData = watchData;
// initNode.apply(this, arguments);
// }, with_node);
//
// };
// preset directives
var special_chars = {
nbsp: '\u00a0', hellip: '…', quot: '"',
}
APP.withNode(function (node) {
var text_node = typeof node === 'string' ? node : node.text
if( text_node ) return {
replace_text: '',
initNode: function (el) {
// console.log('node.text', arguments)
var renderText = TEXT.interpolate(text_node),
parent_el = el.parentElement || el.parentNode
if( parent_el && /{{.*}}/.test(text_node) ) parent_el.insertBefore( document.createComment(' text: ' + text_node + ' '), el )
this.watchData(function (data) {
var text = renderText(data).replace(/&([a-z]+);/g, function (matched, special_char) {
return special_chars[special_char] || matched
})
if( text !== el.textContent ) el.textContent = text
})
}
}
})
if( add_directives.if ) addDirectiveIf(APP, TEXT, directive_ns)
if( add_directives.repeat ) addDirectiveRepeat(APP, TEXT, directive_ns)
if( add_directives.bind ) addDirectiveBind(APP, TEXT, directive_ns)
if( add_directives.on ) addDirectiveOn(APP, TEXT, directive_ns)
if( add_directives['class'] ) addDirectiveClass(APP, TEXT, directive_ns)
function _renderApp (_parent, _nodes, render_options) {
render_options = Object.create(render_options || {})
var APP_ = Object.create(this),
// parent_app = render_options.parent_app || {},
data = render_options.data || {},
data_listeners = [],
watchData = function (onData) {
data_listeners.push(onData)
onData(data)
},
updateData = function (_data) {
if( _data ) data = _data
data_listeners.forEach(function (listener) {
listener(data)
})
}
APP_.render_app = APP_.render_app || APP_
APP_.watchData = watchData
APP_.updateData = updateData
APP_.render = _renderApp.bind(APP_)
Object.defineProperty(APP_, 'data', {
get: function () {
return data
},
})
var inserted_nodes = app.render.apply(APP_, arguments)
return {
get data () {
return data
},
set data (_data) {
updateData(_data)
},
updateData: updateData,
inserted_nodes: inserted_nodes,
}
}
APP.render = _renderApp.bind(APP)
return APP
}
var APP = createApp()
APP.createApp = createApp
module.exports = APP