UNPKG

11.1 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
7var _Object$assign = _interopDefault(require('babel-runtime/core-js/object/assign'));
8var _Reflect$set = _interopDefault(require('babel-runtime/core-js/reflect/set'));
9var _Object$keys = _interopDefault(require('babel-runtime/core-js/object/keys'));
10var _Reflect$defineProperty = _interopDefault(require('babel-runtime/core-js/reflect/define-property'));
11var _Reflect$get = _interopDefault(require('babel-runtime/core-js/reflect/get'));
12var _Reflect$has = _interopDefault(require('babel-runtime/core-js/reflect/has'));
13var _Map = _interopDefault(require('babel-runtime/core-js/map'));
14
15var getLifeCycle = function (lifeCycle) {
16 return lifeCycle ? {
17 created: lifeCycle.init,
18 mounted: lifeCycle.ready,
19 destoryed: lifeCycle.destory
20 } : {};
21};
22
23// 将props指向vue父组件实例
24var getProps = function (vueInstance, props) {
25 if (!props) { return; }
26 var newProps = {};
27 _Object$keys(props).forEach(function (key) {
28 var value = props[key];
29 var prop = typeof value === 'string' && value.indexOf('$') === 0 ? vueInstance[value.substring(1, value.length)] : value;
30 // 若是以$开头则为变量
31 _Reflect$set(newProps, key, prop);
32 });
33 return newProps;
34};
35
36// 将methods指向vue父组件实例
37var getMethods = function (vueInstance, methods) {
38 if (!methods) { return; }
39 var newMethod = {};
40 _Object$keys(methods).forEach(function (key) {
41 var value = methods[key];
42 if (!value.indexOf('$') === 0) { throw new Error('定义组件的method 必须使用$开头的字符串表示变量'); }
43 _Reflect$set(newMethod, key, vueInstance[value.substring(1, value.length)]);
44 });
45 return newMethod;
46};
47
48var baseStyle = {
49 'margin-top': 0,
50 'margin-bottom': 0,
51 'margin-left': 0,
52 'margin-right': 0
53};
54
55
56var getContainerStyle = function (style) {
57 return {
58 attrs: {
59 id: 'main-container',
60 class: 'main-container'
61 },
62 style: _Object$assign(baseStyle, style)
63 };
64};
65
66// 生成vue render方法
67var getRender = function (option) {
68 var components = option.component;
69
70 function render(vueInstance, h, components, option) {
71 if (option) {
72 var prop = option.prop;
73 var method = option.method;
74 return h(components, {
75 props: getProps(vueInstance, prop),
76 on: getMethods(vueInstance, method)
77 }); // 若存在组件配置
78 } else { return h(components); } // 不存在组件配置
79 }
80
81 return function (h) {
82 var vueInstance = this; // this指向vue实例
83 return Array.isArray(components) ? h('div', getContainerStyle(option.style), components.map(function (ref) {
84 var components = ref.components;
85 var option = ref.option;
86
87 return render(vueInstance, h, components, option);
88 })) : render(vueInstance, h, components.components, components.option);
89 };
90};
91
92// 配置vue router 实例
93var getRouter = function (vueInstance) {
94 var TYPE = process.env.COMPILE_ENV;
95 var router = {};
96 var pushHandler = {};
97 var popHandler = {};
98 if (TYPE === 'vue') {
99 if (!vueInstance.$route || !vueInstance.$router) { throw new Error('vue路由api依赖 vue-router'); }
100 popHandler = {
101 get: function (_) { return function (_) { return history.back(); }; }
102 };
103 router = _Object$assign(vueInstance.$router, vueInstance.$route);
104 } else if (TYPE === 'weex') {
105 var navigator = weex.requireModule('navigator');
106 if (!navigator) { throw new Error('weex路由api依赖 weex navigator'); }
107 pushHandler = {
108 get: function (_) { return function (path) {
109 var navigator = weex.requireModule('navigator');
110 if (!navigator) { throw new Error('weex路由api依赖 weex navigator'); }
111 var url = weex.config.bundleUrl.split('/').slice(0, -1).join('/') + '/' + path + '.js'; // 将a.js的绝对地址转为b.js的绝对地址
112 navigator.push({ url: url, animated: "true" });
113 }; }
114 };
115 popHandler = {
116 get: function (_) { return function (_) { return navigator.pop({ animated: 'true' }); }; }
117 };
118 // 添加push对象
119 _Reflect$defineProperty(router, 'push', pushHandler);
120 }
121 // 添加pop对象
122 _Reflect$defineProperty(router, 'pop', popHandler);
123 return router;
124};
125
126// vue实例上挂载自定义对象
127
128var defineInstanceProperty = function (vueInstance) {
129 var properties = new _Map();
130 // 定义setData
131 properties.set('setData', {
132 // 调用setData时遍历挂载在vue实例下的data 并赋值
133 get: function (_) { return function (obj) { return _Object$keys(obj).map(function (key) { return _Reflect$has(vueInstance, key) && _Reflect$set(vueInstance, key, obj[key]); }); }; }
134 });
135 // 定义data proxy 对象
136 properties.set('data', {
137 get: function (_) { return vueInstance; }
138 });
139 // 定义fetch 对象
140 properties.set('fetch', {
141 get: function (_) { return weex.requireModule('stream').fetch; }
142 });
143 // 定义config 对象
144 properties.set('config', {
145 get: function (_) { return weex.config; }
146 });
147 // 定义router 对象
148 properties.set('router', {
149 get: function (_) { return getRouter(vueInstance); }
150 });
151 // 遍历并定义
152 properties.forEach(function (handler, name) {
153 _Reflect$get(vueInstance, name) === undefined && _Reflect$defineProperty(vueInstance, name, handler);
154 });
155};
156
157var mixins = {
158 beforeCreate: function beforeCreate() {
159 defineInstanceProperty(this);
160 }
161};
162
163var VueCompile = function VueCompile(option) {
164 return this.getInstance(option);
165};
166
167// 生成vue实例
168VueCompile.prototype.getInstance = function getInstance (option) {
169 var render = getRender(option);
170 var data = option.data || {};
171 return _Object$assign(getLifeCycle(option.lifeCycle), {
172 mixins: [mixins],
173 methods: option.method,
174 data: function (_) { return data; },
175 render: render
176 });
177};
178
179/**
180 * Created by zhangyi on 2017/4/12.
181 * Desc: ocmp
182 */
183
184function OComp$1(options) {
185 var this$1 = this;
186
187 if (!options.data) {
188 throw new Error('no data');
189 }
190
191 this.data = options.data;
192
193 for (var key in this$1.data) {
194 this$1[key] = this$1.data[key];
195 }
196
197 if (!options.method) {
198 throw new Error('no method');
199 }
200
201 this.method = options.method;
202
203 for (var key$1 in this$1.method) {
204 this$1[key$1] = this$1.method[key$1];
205 }
206
207 if (options.component) {
208 this.component = options.component;
209 } else {
210 this.component = [];
211 }
212}
213
214OComp$1.prototype.render = function () {
215 var components = this.component;
216 var html = '';
217 var css = '';
218 var data = {};
219 var method = {};
220
221 for (var i = 0; i < components.length; i++) {
222 var component = components[i];
223 var comp = component.components;
224
225 html = html + "\n" + (comp.html) + "\n";
226 //外部属性替换
227 if (component.hasOwnProperty('option') && component.option && component.option.hasOwnProperty('prop')) {
228
229 var props = component.option.prop;
230
231 for (var key in props) {
232 var prop = props[key];
233
234 html = html.replace(new RegExp(("{{" + key + "}}"), 'g'), ("{{ " + prop + " }}"));
235 html = html.replace(new RegExp(("{{ " + key + " }}"), 'g'), ("{{ " + prop + " }}"));
236 }
237 }
238
239 //内部属性
240 if (comp.hasOwnProperty('data')) {
241 var _data = comp.data;
242
243 for (var key$1 in _data) {
244 var name = "OCOMP_" + i + "_DATA_" + key$1;
245
246 html = html.replace(new RegExp(("{{" + key$1 + "}}"), 'g'), ("{{ " + name + " }}"));
247 html = html.replace(new RegExp(("{{ " + key$1 + " }}"), 'g'), ("{{ " + name + " }}"));
248
249 data[name] = _data[key$1];
250 }
251 }
252
253 //内部函数
254 if (comp.hasOwnProperty('method')) {
255 var _methods = comp.method;
256 var idx = 0;
257
258 for (var key$2 in _methods) {
259 var name$1 = "OCOMP_" + i + "_METHOD_" + idx;
260
261 html = html.replace(new RegExp(("{{" + key$2 + "}}"), 'g'), ("{{ " + name$1 + " }}"));
262 html = html.replace(new RegExp(("{{ " + key$2 + " }}"), 'g'), ("{{ " + name$1 + " }}"));
263
264 var method_str = _methods[key$2].toString();
265
266 if (comp.hasOwnProperty('data')) {
267 var _data$1 = comp.data;
268
269 for (var key$3 in _data$1) {
270 var name$2 = "OCOMP_" + i + "_DATA_" + key$3;
271 method_str = method_str.replace(new RegExp(key$3, 'g'), name$2);
272 }
273 }
274
275 if (component.hasOwnProperty('option') && component.option && component.option.hasOwnProperty('method')) {
276
277 var outer_methods = component.option.method;
278
279 for (var key$4 in outer_methods) {
280 var outer_method = outer_methods[key$4];
281 method_str = method_str.replace(new RegExp('\\$' + key$4, 'g'), outer_method);
282 }
283 }
284
285 html = html.replace(new RegExp(key$2, 'g'), name$1);
286 method[name$1] = method_str;
287
288 idx++;
289 }
290 }
291
292 // 外部函数替换
293 // if(component.hasOwnProperty('option') && component.option &&
294 // component.option.hasOwnProperty('method')) {
295 //
296 // let methods = component.option.method
297 //
298 // for(let key in methods) {
299 // let method = methods[key]
300 //
301 // html = html.replace(new RegExp(key, 'g'), method)
302 // html = html.replace(new RegExp(key, 'g'), method)
303 // }
304 // }
305
306 css = css + "\n" + (comp.css) + "\n";
307 }
308
309 return {
310 html: html,
311 css: css,
312 data: data,
313 method: method
314 };
315};
316
317OComp$1.prototype.getData = function () {
318 return this.data;
319};
320
321OComp$1.prototype.getMethod = function () {
322 return this.method;
323};
324
325var OComp = function OComp(option) {
326 this.VUE_COMPONENTS = 'vue';
327 this.WX_COMPONENTS = 'wx';
328 this.WEEX_COMPONENTS = 'weex';
329 var type = [this.VUE_COMPONENTS, this.WX_COMPONENTS, this.WEEX_COMPONENTS];
330
331 if (!option) { throw new Error('构造参数不存在'); }
332 var componentsArr = option.component;
333 if (!componentsArr) { throw new Error('组件不存在'); }
334
335 var TYPE = process.env.COMPILE_ENV;
336 if (!(type.indexOf(TYPE) > -1)) { throw new Error('组件类型错误'); }
337
338 if (TYPE === this.VUE_COMPONENTS || TYPE === this.WEEX_COMPONENTS) { return new VueCompile(option); }else if (TYPE === this.WX_COMPONENTS) { return new OComp$1(option); }
339};
340
341var Component = function Component(ref, option) {
342 var weexComponent = ref.weexComponent;
343
344 this.components = weexComponent;
345 this.option = option;
346};
347
348exports.OComp = OComp;
349exports.Component = Component;