1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
6 |
|
7 | var _Object$assign = _interopDefault(require('babel-runtime/core-js/object/assign'));
|
8 | var _Reflect$set = _interopDefault(require('babel-runtime/core-js/reflect/set'));
|
9 | var _Object$keys = _interopDefault(require('babel-runtime/core-js/object/keys'));
|
10 | var _Reflect$defineProperty = _interopDefault(require('babel-runtime/core-js/reflect/define-property'));
|
11 | var _Reflect$get = _interopDefault(require('babel-runtime/core-js/reflect/get'));
|
12 | var _Reflect$has = _interopDefault(require('babel-runtime/core-js/reflect/has'));
|
13 | var _Map = _interopDefault(require('babel-runtime/core-js/map'));
|
14 |
|
15 | var getLifeCycle = function (lifeCycle) {
|
16 | return lifeCycle ? {
|
17 | created: lifeCycle.init,
|
18 | mounted: lifeCycle.ready,
|
19 | destoryed: lifeCycle.destory
|
20 | } : {};
|
21 | };
|
22 |
|
23 |
|
24 | var 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父组件实例
|
37 | var 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 |
|
48 | var baseStyle = {
|
49 | 'margin-top': 0,
|
50 | 'margin-bottom': 0,
|
51 | 'margin-left': 0,
|
52 | 'margin-right': 0
|
53 | };
|
54 |
|
55 |
|
56 | var 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 |
|
67 | var 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;
|
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 |
|
93 | var 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';
|
112 | navigator.push({ url: url, animated: "true" });
|
113 | }; }
|
114 | };
|
115 | popHandler = {
|
116 | get: function (_) { return function (_) { return navigator.pop({ animated: 'true' }); }; }
|
117 | };
|
118 |
|
119 | _Reflect$defineProperty(router, 'push', pushHandler);
|
120 | }
|
121 |
|
122 | _Reflect$defineProperty(router, 'pop', popHandler);
|
123 | return router;
|
124 | };
|
125 |
|
126 |
|
127 |
|
128 | var defineInstanceProperty = function (vueInstance) {
|
129 | var properties = new _Map();
|
130 |
|
131 | properties.set('setData', {
|
132 |
|
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 |
|
140 | properties.set('fetch', {
|
141 | get: function (_) { return weex.requireModule('stream').fetch; }
|
142 | });
|
143 |
|
144 | properties.set('config', {
|
145 | get: function (_) { return weex.config; }
|
146 | });
|
147 |
|
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 |
|
157 | var mixins = {
|
158 | beforeCreate: function beforeCreate() {
|
159 | defineInstanceProperty(this);
|
160 | }
|
161 | };
|
162 |
|
163 | var VueCompile = function VueCompile(option) {
|
164 | return this.getInstance(option);
|
165 | };
|
166 |
|
167 |
|
168 | VueCompile.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 |
|
181 |
|
182 |
|
183 |
|
184 | function 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 |
|
214 | OComp$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 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
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 |
|
317 | OComp$1.prototype.getData = function () {
|
318 | return this.data;
|
319 | };
|
320 |
|
321 | OComp$1.prototype.getMethod = function () {
|
322 | return this.method;
|
323 | };
|
324 |
|
325 | var 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 |
|
341 | var Component = function Component(ref, option) {
|
342 | var weexComponent = ref.weexComponent;
|
343 |
|
344 | this.components = weexComponent;
|
345 | this.option = option;
|
346 | };
|
347 |
|
348 | exports.OComp = OComp;
|
349 | exports.Component = Component;
|