1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.createRenderFnc = exports.createRenderFn = undefined;
|
7 |
|
8 | var _courier = require('./courier');
|
9 |
|
10 | var _courier2 = _interopRequireDefault(_courier);
|
11 |
|
12 | var _normalizeSlots = require('./normalizeSlots');
|
13 |
|
14 | var _normalizeSlots2 = _interopRequireDefault(_normalizeSlots);
|
15 |
|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
17 |
|
18 | var isObject = function isObject(test) {
|
19 | return Object.prototype.toString.call(test) === '[object Object]';
|
20 | };
|
21 | var isFn = function isFn(test) {
|
22 | return Object.prototype.toString.call(test) === '[object Function]';
|
23 | };
|
24 |
|
25 | var justBindOptions = ['listeners', 'nativeOn', 'scopedSlots'];
|
26 |
|
27 | var justBindFn = function justBindFn(key) {
|
28 | return justBindOptions.indexOf(key) > -1;
|
29 | };
|
30 |
|
31 | var getOptionsKeys = function getOptionsKeys(options) {
|
32 | return Object.keys(options).concat(['listeners', 'props', 'attrs']).filter(function (k, i, a) {
|
33 | return a.indexOf(k) === i;
|
34 | });
|
35 | };
|
36 |
|
37 | var createOptionHandlers = function createOptionHandlers(originalOptions, keys) {
|
38 | var options = {};
|
39 |
|
40 | keys.forEach(function (key) {
|
41 | var option = originalOptions[key];
|
42 |
|
43 | if (!option) {
|
44 | options[key] = function (owner) {
|
45 | return owner;
|
46 | };
|
47 | return;
|
48 | }
|
49 |
|
50 | if (isFn(option)) {
|
51 |
|
52 | options[key] = option;
|
53 | return;
|
54 | }
|
55 |
|
56 | if (isObject(option)) {
|
57 | var optionKeys = Object.keys(option);
|
58 |
|
59 | if (!optionKeys.some(function (k) {
|
60 | return isFn(option[k]);
|
61 | })) {
|
62 | options[key] = function (owner) {
|
63 | return Object.assign({}, owner, option);
|
64 | };
|
65 | return;
|
66 | }
|
67 |
|
68 | options[key] = function (owner) {
|
69 | var _this = this;
|
70 |
|
71 | var result = Object.assign({}, owner);
|
72 | var justBind = justBindFn(key);
|
73 |
|
74 | optionKeys.forEach(function (k) {
|
75 | var value = option && option[k];
|
76 |
|
77 | if (isFn(value)) {
|
78 | if (justBind) {
|
79 | value = value.bind(_this);
|
80 | } else {
|
81 | value = value.call(_this, owner);
|
82 | }
|
83 | }
|
84 | result[k] = value;
|
85 | });
|
86 | return result;
|
87 | };
|
88 | return;
|
89 | }
|
90 |
|
91 | options[key] = function () {
|
92 | return option;
|
93 | };
|
94 | });
|
95 |
|
96 | return options;
|
97 | };
|
98 |
|
99 | var preprocessOptions = function preprocessOptions(originalOptions) {
|
100 | var keys = getOptionsKeys(originalOptions);
|
101 | var options = createOptionHandlers(originalOptions, keys);
|
102 |
|
103 | return function (context, isFunctional) {
|
104 | var result = {
|
105 | on: {},
|
106 | props: {},
|
107 | attrs: {}
|
108 | };
|
109 |
|
110 | keys.forEach(function (key) {
|
111 | var owner = isFunctional ? context[key] || context.data[key] : context['$' + key];
|
112 |
|
113 | var value = options[key].call(context, owner);
|
114 |
|
115 | if (key === 'listeners') {
|
116 | key = 'on';
|
117 | }
|
118 |
|
119 | result[key] = value;
|
120 | });
|
121 |
|
122 | return result;
|
123 | };
|
124 | };
|
125 |
|
126 | var createRenderFn = exports.createRenderFn = function createRenderFn(Component, options) {
|
127 | var getData = preprocessOptions(options || {});
|
128 |
|
129 | return function renderHoc(h, context) {
|
130 |
|
131 | var data = getData(context || this, !!context);
|
132 | var scopedSlots = context && context.data && context.data.scopedSlots || this && this.$scopedSlots;
|
133 | var slots = context && context.children || this && this.$slots && (0, _normalizeSlots2.default)(this.$slots) || [];
|
134 |
|
135 | data.scopedSlots = data.scopedSlots || scopedSlots;
|
136 |
|
137 | return h(Component, data, slots);
|
138 | };
|
139 | };
|
140 |
|
141 | var createRenderFnc = exports.createRenderFnc = (0, _courier2.default)(2, function (options, Component) {
|
142 | return createRenderFn(Component, options);
|
143 | });
|
144 |
|
\ | No newline at end of file |