1 | 'use strict';
|
2 |
|
3 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
4 |
|
5 | var _Object$keys = _interopDefault(require('babel-runtime/core-js/object/keys'));
|
6 | var _Object$assign = _interopDefault(require('babel-runtime/core-js/object/assign'));
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | var VueCompile = function VueCompile(option) {
|
14 | return this.makeVueInstance(option);
|
15 | };
|
16 |
|
17 |
|
18 | VueCompile.prototype.makeVueInstance = function makeVueInstance (option) {
|
19 | var render = this.makeVueRender(option.component);
|
20 | var newMethod = this.makeGlobalFunction(option.method);
|
21 | var oldData = option.data || {};
|
22 | var newData = _Object$assign({}, oldData);
|
23 | newData.data = oldData;
|
24 |
|
25 | return _Object$assign(this.makeVueLifeCycle(option.lifeCycle), {
|
26 | methods: newMethod,
|
27 | data: function (_) { return newData; },
|
28 | render: render
|
29 | });
|
30 | };
|
31 |
|
32 |
|
33 | VueCompile.prototype.makeGlobalFunction = function makeGlobalFunction (methods) {
|
34 | if (!methods) { methods = {}; }
|
35 | methods.setData = function (obj) {
|
36 | var vueInstanceSelf = this;
|
37 | _Object$keys(obj).forEach(function (key) {
|
38 | if (vueInstanceSelf[key] !== undefined) {
|
39 | vueInstanceSelf[key] = obj[key];
|
40 | vueInstanceSelf['data'][key] = obj[key];
|
41 | }
|
42 | });
|
43 | };
|
44 | methods.fetch = weex.requireModule('stream').fetch;
|
45 | methods.getPlatform = function (_) { return weex.config.platform; };
|
46 | return methods;
|
47 | };
|
48 |
|
49 |
|
50 | VueCompile.prototype.makeVueLifeCycle = function makeVueLifeCycle (lifeCycle) {
|
51 | return lifeCycle ? {
|
52 | created: lifeCycle.init,
|
53 | mounted: lifeCycle.ready,
|
54 | destroyed: lifeCycle.destory
|
55 | } : {};
|
56 | };
|
57 |
|
58 |
|
59 | VueCompile.prototype.makeVueRender = function makeVueRender (components) {
|
60 | var classSelf = this;
|
61 | return function (createElement) {
|
62 | var vueInstanceSelf = this;
|
63 | classSelf.makeVueRouter.call(vueInstanceSelf);
|
64 |
|
65 | return createElement('div', classSelf.getContainerStyle(), components.map(function (ref) {
|
66 | var components = ref.components;
|
67 | var option = ref.option;
|
68 |
|
69 | if (option) {
|
70 |
|
71 | classSelf.translateToVueProps.call(vueInstanceSelf, option);
|
72 | classSelf.translateToVueMethods.call(vueInstanceSelf, option);
|
73 | return createElement(components, option);
|
74 | } else {
|
75 | return createElement(components);
|
76 | }
|
77 | }));
|
78 | };
|
79 | };
|
80 |
|
81 |
|
82 | VueCompile.prototype.getContainerStyle = function getContainerStyle () {
|
83 | return {
|
84 | attrs: {
|
85 | id: 'main-container',
|
86 | class: 'main-container'
|
87 | },
|
88 | style: {
|
89 | 'margin-top': 0,
|
90 | 'margin-bottom': 0,
|
91 | 'margin-left': 0,
|
92 | 'margin-right': 0,
|
93 | 'background-color': '#f0eff5'
|
94 | }
|
95 | };
|
96 | };
|
97 |
|
98 |
|
99 | VueCompile.prototype.makeVueRouter = function makeVueRouter () {
|
100 | var TYPE = process.env.COMPILE_ENV;
|
101 | if (TYPE === 'vue') {
|
102 | if (!this.$route || !this.$router) { throw new Error('vue路由api依赖 vue-router'); }
|
103 | this.router = _Object$assign(this.$router, this.$route);
|
104 | this.router.pop = function (_) { return history.back(); };
|
105 | } else if (TYPE === 'weex') {
|
106 | this.router = {};
|
107 | this.router.push = function (path) {
|
108 | var navigator = weex.requireModule('navigator');
|
109 | if (!navigator) {
|
110 | throw new Error('weex路由api依赖 weex navigator');
|
111 | }
|
112 | var url = weex.config.bundleUrl;
|
113 | url = url.split('/').slice(0, -1).join('/') + path + '.js';
|
114 | navigator.push({
|
115 | url: url,
|
116 | animated: "true"
|
117 | });
|
118 | };
|
119 | this.router.pop = function (_) {
|
120 | navigator.pop({
|
121 | animated: "true"
|
122 | });
|
123 | };
|
124 | }
|
125 | };
|
126 |
|
127 |
|
128 | VueCompile.prototype.translateToVueProps = function translateToVueProps (option) {
|
129 | var vueInstanceSelf = this;
|
130 | if (!option.prop) { return; }
|
131 | var PROPS = _Object$assign({}, option.prop);
|
132 | option.props = option.props || {};
|
133 | _Object$keys(PROPS).forEach(function (key) {
|
134 | if (option.props) { option.props[key] = vueInstanceSelf[PROPS[key]]; }
|
135 | });
|
136 | };
|
137 |
|
138 |
|
139 | VueCompile.prototype.translateToVueMethods = function translateToVueMethods (option) {
|
140 | var vueInstanceSelf = this;
|
141 | if (option.on || !option.method) { return; }
|
142 | var METHODS = _Object$assign({}, option.method);
|
143 | option.on = {};
|
144 | _Object$keys(METHODS).forEach(function (key) {
|
145 | if (option.on) { option.on[key] = vueInstanceSelf[METHODS[key]]; }
|
146 | });
|
147 | };
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | function OComp$2(options) {
|
162 | var this$1 = this;
|
163 |
|
164 | if (!options.data) {
|
165 | throw new Error('no data');
|
166 | }
|
167 |
|
168 | this.data = options.data;
|
169 |
|
170 | for (var key in this$1.data) {
|
171 | this$1[key] = this$1.data[key];
|
172 | }
|
173 |
|
174 | if (!options.method) {
|
175 | throw new Error('no method');
|
176 | }
|
177 |
|
178 | this.method = options.method;
|
179 |
|
180 | for (var key$1 in this$1.method) {
|
181 | this$1[key$1] = this$1.method[key$1];
|
182 | }
|
183 |
|
184 | if (options.component) {
|
185 | this.component = options.component;
|
186 | } else {
|
187 | this.component = [];
|
188 | }
|
189 | }
|
190 |
|
191 | OComp$2.prototype.render = function () {
|
192 | var components = this.component;
|
193 | var html = '';
|
194 | var css = '';
|
195 | var data = {};
|
196 | var method = {};
|
197 |
|
198 | for (var i = 0; i < components.length; i++) {
|
199 | var component = components[i];
|
200 | var comp = component.components;
|
201 |
|
202 | html = html + "\n" + (comp.html) + "\n";
|
203 |
|
204 | if (component.hasOwnProperty('option') && component.option && component.option.hasOwnProperty('prop')) {
|
205 |
|
206 | var props = component.option.prop;
|
207 |
|
208 | for (var key in props) {
|
209 | var prop = props[key];
|
210 |
|
211 | html = html.replace(new RegExp(("{{" + key + "}}"), 'g'), ("{{ " + prop + " }}"));
|
212 | html = html.replace(new RegExp(("{{ " + key + " }}"), 'g'), ("{{ " + prop + " }}"));
|
213 | }
|
214 | }
|
215 |
|
216 |
|
217 | if (comp.hasOwnProperty('data')) {
|
218 | var _data = comp.data;
|
219 |
|
220 | for (var key$1 in _data) {
|
221 | var name = "OCOMP_" + i + "_DATA_" + key$1;
|
222 |
|
223 | html = html.replace(new RegExp(("{{" + key$1 + "}}"), 'g'), ("{{ " + name + " }}"));
|
224 | html = html.replace(new RegExp(("{{ " + key$1 + " }}"), 'g'), ("{{ " + name + " }}"));
|
225 |
|
226 | data[name] = _data[key$1];
|
227 | }
|
228 | }
|
229 |
|
230 |
|
231 | if (comp.hasOwnProperty('method')) {
|
232 | var _methods = comp.method;
|
233 | var idx = 0;
|
234 |
|
235 | for (var key$2 in _methods) {
|
236 | var name$1 = "OCOMP_" + i + "_METHOD_" + idx;
|
237 |
|
238 | html = html.replace(new RegExp(("{{" + key$2 + "}}"), 'g'), ("{{ " + name$1 + " }}"));
|
239 | html = html.replace(new RegExp(("{{ " + key$2 + " }}"), 'g'), ("{{ " + name$1 + " }}"));
|
240 |
|
241 | var method_str = _methods[key$2].toString();
|
242 |
|
243 | if (comp.hasOwnProperty('data')) {
|
244 | var _data$1 = comp.data;
|
245 |
|
246 | for (var key$3 in _data$1) {
|
247 | var name$2 = "OCOMP_" + i + "_DATA_" + key$3;
|
248 | method_str = method_str.replace(new RegExp(key$3, 'g'), name$2);
|
249 | }
|
250 | }
|
251 |
|
252 | if (component.hasOwnProperty('option') && component.option && component.option.hasOwnProperty('method')) {
|
253 |
|
254 | var outer_methods = component.option.method;
|
255 |
|
256 | for (var key$4 in outer_methods) {
|
257 | var outer_method = outer_methods[key$4];
|
258 | method_str = method_str.replace(new RegExp('\\$' + key$4, 'g'), outer_method);
|
259 | }
|
260 | }
|
261 |
|
262 | html = html.replace(new RegExp(key$2, 'g'), name$1);
|
263 | method[name$1] = method_str;
|
264 |
|
265 | idx++;
|
266 | }
|
267 | }
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 | css = css + "\n" + (comp.css) + "\n";
|
284 | }
|
285 |
|
286 | return {
|
287 | html: html,
|
288 | css: css,
|
289 | data: data,
|
290 | method: method
|
291 | };
|
292 | };
|
293 |
|
294 | OComp$2.prototype.getData = function () {
|
295 | return this.data;
|
296 | };
|
297 |
|
298 | OComp$2.prototype.getMethod = function () {
|
299 | return this.method;
|
300 | };
|
301 |
|
302 | var OComp = function OComp(option) {
|
303 | this.VUE_COMPONENTS = 'vue';
|
304 | this.WX_COMPONENTS = 'wx';
|
305 | this.WEEX_COMPONENTS = 'weex';
|
306 | var type = [this.VUE_COMPONENTS, this.WX_COMPONENTS, this.WEEX_COMPONENTS];
|
307 |
|
308 | if (!option) { throw new Error('构造参数不存在'); }
|
309 | var componentsArr = option.component;
|
310 | if (!componentsArr || !Array.isArray(componentsArr)) { throw new Error('组件列表必须为数组'); }
|
311 |
|
312 | var TYPE = process.env.COMPILE_ENV;
|
313 | if (!(type.indexOf(TYPE) > -1)) { throw new Error('组件类型错误'); }
|
314 |
|
315 | if (TYPE === this.VUE_COMPONENTS || TYPE === this.WEEX_COMPONENTS) { return new VueCompile(option); }else if (TYPE === this.WX_COMPONENTS) { return new OComp$2(option); }
|
316 | };
|
317 |
|
318 | module.exports = OComp;
|