-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixin.js
162 lines (124 loc) · 4.74 KB
/
mixin.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
156
157
158
159
160
161
162
;(function (root, factory) {
if (typeof exports === 'object') {
// CommonJS
factory(require, exports, module);
} else if (typeof define === 'function' && define.amd) {
// AMD
define(['require', 'exports', 'module', './utils', 'cla55'], factory);
} else {
console && console.error('Unsupported module environment.'); // jshint ignore:line
}
}(this, function (require, exports, module) {
'use strict';
var _ = require('./utils'),
// Pseudo class implementation (all mixin properties needs to be own properties)
Cla55 = (function () {
var Cla55 = require('cla55').Cla55;
return Cla55.extend({}, {
// jscs:disable safeContextKeyword
extend: function () {
var Parent = this,
Child = Cla55.extend.apply(this, arguments),
key;
// Ensure static copy from parent
for (key in Parent.prototype) {
if (Parent.prototype.hasOwnProperty(key) && !Child.prototype.hasOwnProperty(key)) {
Child.prototype[key] = Parent.prototype[key];
}
}
// Make the static class accessable when creating instance
Child.prototype.__STATIC = Child;
return Child;
}
// jscs:enable safeContextKeyword
});
}()),
emptyTemplate = function () {
return null;
},
Mixin;
Mixin = Cla55.extend({
constructor: function constructor(VanDykeRuntime) {
var that = this;
if (that instanceof Mixin) {
// Initialize instance of Mixin
that = _.assign({}, that.__STATIC.prototype);
// Prefere runtime from arguments
if (!VanDykeRuntime) {
VanDykeRuntime = that.vandyke;
}
// Check whether a VanDyke Runtime is defined
if (!VanDykeRuntime) {
throw 'VanDyke Mixin needs a defined VanDyke Runtime';
}
// Prevent React to context binding for this class
that.vandyke = function (template) {
return new VanDykeRuntime(template);
};
// Remove the constructor to avoid conflicts in React
delete that.constructor;
// Remove the reference of the static class
delete that.__STATIC;
} else {
// Static initialize of Mixin
// Initial define of React
that.prototype.vandyke = VanDykeRuntime;
}
return that;
},
// VanDyke runtime
vandyke: null,
// Mixin defaults rewritable by extending the class
_vandykeHelpers: {},
_vandykeComponents: {},
_vandykeListeners: function () {
return this;
},
_vandykeData: function () {
return _.assign({}, this.props, this.state);
},
_vandykeTemplate: function () {
return this.template || emptyTemplate;
},
_vandykeRender: function () {
// Set the template to render
this.vandyke.template = _.result(this, 'vandykeTemplate', this._vandykeTemplate);
// Render
return this.vandyke.render(
_.result(this, 'vandykeData', this._vandykeData),
{
listeners: _.result(this, 'vandykeListeners', this._vandykeListeners),
helpers: _.result(this, 'vandykeHelpers', this._vandykeHelpers),
components: _.result(this, 'vandykeComponents', this._vandykeComponents)
}
);
},
// Overwrite defaults by defining your own for each component
/*
template: require('path-to/template.vandyke'),
vandykeHelpers: {},
vandykeComponents: {},
vandykeListeners: function () {
return this;
},
vandykeData: function () {
return {};
},
vandykeTemplate: function () {
return this.props.foo ? templateFoo : templateBar;
},
vandykeRender: function () {
return this._vandykeRender();
},
*/
// React life cycle
componentWillMount: function () {
// Create vandyke instance
this.vandyke = this.vandyke();
},
render: function () {
return _.result(this, 'vandykeRender', this._vandykeRender);
}
}, {});
module.exports = Mixin;
}));