index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. Object.defineProperty(exports, "camelize", {
  7. enumerable: true,
  8. get: function () {
  9. return _util.camelize;
  10. }
  11. });
  12. exports.default = void 0;
  13. exports.filterEmpty = filterEmpty;
  14. exports.filterEmptyWithUndefined = filterEmptyWithUndefined;
  15. exports.flattenChildren = exports.findDOMNode = void 0;
  16. exports.getClass = getClass;
  17. exports.getComponent = void 0;
  18. exports.getComponentName = getComponentName;
  19. exports.getEvents = getEvents;
  20. exports.getOptionProps = exports.getKey = void 0;
  21. exports.getPropsSlot = getPropsSlot;
  22. exports.getSlot = void 0;
  23. exports.getStyle = getStyle;
  24. exports.hasProp = exports.getTextFromElement = void 0;
  25. Object.defineProperty(exports, "initDefaultProps", {
  26. enumerable: true,
  27. get: function () {
  28. return _initDefaultProps.default;
  29. }
  30. });
  31. exports.isEmptyContent = isEmptyContent;
  32. exports.isEmptyElement = isEmptyElement;
  33. exports.isEmptySlot = isEmptySlot;
  34. exports.isFragment = isFragment;
  35. exports.isStringElement = isStringElement;
  36. exports.isValidElement = isValidElement;
  37. exports.splitAttrs = exports.skipFlattenKey = exports.parseStyleText = void 0;
  38. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  39. var _classNames = _interopRequireDefault(require("../classNames"));
  40. var _vue = require("vue");
  41. var _util = require("../util");
  42. var _isValid = _interopRequireDefault(require("../isValid"));
  43. var _initDefaultProps = _interopRequireDefault(require("./initDefaultProps"));
  44. // function getType(fn) {
  45. // const match = fn && fn.toString().match(/^\s*function (\w+)/);
  46. // return match ? match[1] : '';
  47. // }
  48. const splitAttrs = attrs => {
  49. const allAttrs = Object.keys(attrs);
  50. const eventAttrs = {};
  51. const onEvents = {};
  52. const extraAttrs = {};
  53. for (let i = 0, l = allAttrs.length; i < l; i++) {
  54. const key = allAttrs[i];
  55. if ((0, _util.isOn)(key)) {
  56. eventAttrs[key[2].toLowerCase() + key.slice(3)] = attrs[key];
  57. onEvents[key] = attrs[key];
  58. } else {
  59. extraAttrs[key] = attrs[key];
  60. }
  61. }
  62. return {
  63. onEvents,
  64. events: eventAttrs,
  65. extraAttrs
  66. };
  67. };
  68. exports.splitAttrs = splitAttrs;
  69. const parseStyleText = function () {
  70. let cssText = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  71. let camel = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  72. const res = {};
  73. const listDelimiter = /;(?![^(]*\))/g;
  74. const propertyDelimiter = /:(.+)/;
  75. if (typeof cssText === 'object') return cssText;
  76. cssText.split(listDelimiter).forEach(function (item) {
  77. if (item) {
  78. const tmp = item.split(propertyDelimiter);
  79. if (tmp.length > 1) {
  80. const k = camel ? (0, _util.camelize)(tmp[0].trim()) : tmp[0].trim();
  81. res[k] = tmp[1].trim();
  82. }
  83. }
  84. });
  85. return res;
  86. };
  87. exports.parseStyleText = parseStyleText;
  88. const hasProp = (instance, prop) => {
  89. return instance[prop] !== undefined;
  90. };
  91. exports.hasProp = hasProp;
  92. const skipFlattenKey = exports.skipFlattenKey = Symbol('skipFlatten');
  93. const flattenChildren = function () {
  94. let children = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  95. let filterEmpty = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  96. const temp = Array.isArray(children) ? children : [children];
  97. const res = [];
  98. temp.forEach(child => {
  99. if (Array.isArray(child)) {
  100. res.push(...flattenChildren(child, filterEmpty));
  101. } else if (child && child.type === _vue.Fragment) {
  102. if (child.key === skipFlattenKey) {
  103. res.push(child);
  104. } else {
  105. res.push(...flattenChildren(child.children, filterEmpty));
  106. }
  107. } else if (child && (0, _vue.isVNode)(child)) {
  108. if (filterEmpty && !isEmptyElement(child)) {
  109. res.push(child);
  110. } else if (!filterEmpty) {
  111. res.push(child);
  112. }
  113. } else if ((0, _isValid.default)(child)) {
  114. res.push(child);
  115. }
  116. });
  117. return res;
  118. };
  119. exports.flattenChildren = flattenChildren;
  120. const getSlot = function (self) {
  121. let name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';
  122. let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  123. if ((0, _vue.isVNode)(self)) {
  124. if (self.type === _vue.Fragment) {
  125. return name === 'default' ? flattenChildren(self.children) : [];
  126. } else if (self.children && self.children[name]) {
  127. return flattenChildren(self.children[name](options));
  128. } else {
  129. return [];
  130. }
  131. } else {
  132. const res = self.$slots[name] && self.$slots[name](options);
  133. return flattenChildren(res);
  134. }
  135. };
  136. exports.getSlot = getSlot;
  137. const findDOMNode = instance => {
  138. var _a;
  139. let node = ((_a = instance === null || instance === void 0 ? void 0 : instance.vnode) === null || _a === void 0 ? void 0 : _a.el) || instance && (instance.$el || instance);
  140. while (node && !node.tagName) {
  141. node = node.nextSibling;
  142. }
  143. return node;
  144. };
  145. exports.findDOMNode = findDOMNode;
  146. const getOptionProps = instance => {
  147. const res = {};
  148. if (instance.$ && instance.$.vnode) {
  149. const props = instance.$.vnode.props || {};
  150. Object.keys(instance.$props).forEach(k => {
  151. const v = instance.$props[k];
  152. const hyphenateKey = (0, _util.hyphenate)(k);
  153. if (v !== undefined || hyphenateKey in props) {
  154. res[k] = v; // 直接取 $props[k]
  155. }
  156. });
  157. } else if ((0, _vue.isVNode)(instance) && typeof instance.type === 'object') {
  158. const originProps = instance.props || {};
  159. const props = {};
  160. Object.keys(originProps).forEach(key => {
  161. props[(0, _util.camelize)(key)] = originProps[key];
  162. });
  163. const options = instance.type.props || {};
  164. Object.keys(options).forEach(k => {
  165. const v = (0, _util.resolvePropValue)(options, props, k, props[k]);
  166. if (v !== undefined || k in props) {
  167. res[k] = v;
  168. }
  169. });
  170. }
  171. return res;
  172. };
  173. exports.getOptionProps = getOptionProps;
  174. const getComponent = function (instance) {
  175. let prop = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';
  176. let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : instance;
  177. let execute = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
  178. let com = undefined;
  179. if (instance.$) {
  180. const temp = instance[prop];
  181. if (temp !== undefined) {
  182. return typeof temp === 'function' && execute ? temp(options) : temp;
  183. } else {
  184. com = instance.$slots[prop];
  185. com = execute && com ? com(options) : com;
  186. }
  187. } else if ((0, _vue.isVNode)(instance)) {
  188. const temp = instance.props && instance.props[prop];
  189. if (temp !== undefined && instance.props !== null) {
  190. return typeof temp === 'function' && execute ? temp(options) : temp;
  191. } else if (instance.type === _vue.Fragment) {
  192. com = instance.children;
  193. } else if (instance.children && instance.children[prop]) {
  194. com = instance.children[prop];
  195. com = execute && com ? com(options) : com;
  196. }
  197. }
  198. if (Array.isArray(com)) {
  199. com = flattenChildren(com);
  200. com = com.length === 1 ? com[0] : com;
  201. com = com.length === 0 ? undefined : com;
  202. }
  203. return com;
  204. };
  205. exports.getComponent = getComponent;
  206. const getKey = ele => {
  207. const key = ele.key;
  208. return key;
  209. };
  210. exports.getKey = getKey;
  211. function getEvents() {
  212. let ele = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  213. let on = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  214. let props = {};
  215. if (ele.$) {
  216. props = (0, _extends2.default)((0, _extends2.default)({}, props), ele.$attrs);
  217. } else {
  218. props = (0, _extends2.default)((0, _extends2.default)({}, props), ele.props);
  219. }
  220. return splitAttrs(props)[on ? 'onEvents' : 'events'];
  221. }
  222. function getClass(ele) {
  223. const props = ((0, _vue.isVNode)(ele) ? ele.props : ele.$attrs) || {};
  224. const tempCls = props.class || {};
  225. let cls = {};
  226. if (typeof tempCls === 'string') {
  227. tempCls.split(' ').forEach(c => {
  228. cls[c.trim()] = true;
  229. });
  230. } else if (Array.isArray(tempCls)) {
  231. (0, _classNames.default)(tempCls).split(' ').forEach(c => {
  232. cls[c.trim()] = true;
  233. });
  234. } else {
  235. cls = (0, _extends2.default)((0, _extends2.default)({}, cls), tempCls);
  236. }
  237. return cls;
  238. }
  239. function getStyle(ele, camel) {
  240. const props = ((0, _vue.isVNode)(ele) ? ele.props : ele.$attrs) || {};
  241. let style = props.style || {};
  242. if (typeof style === 'string') {
  243. style = parseStyleText(style, camel);
  244. } else if (camel && style) {
  245. // 驼峰化
  246. const res = {};
  247. Object.keys(style).forEach(k => res[(0, _util.camelize)(k)] = style[k]);
  248. return res;
  249. }
  250. return style;
  251. }
  252. function getComponentName(opts) {
  253. return opts && (opts.Ctor.options.name || opts.tag);
  254. }
  255. function isFragment(c) {
  256. return c.length === 1 && c[0].type === _vue.Fragment;
  257. }
  258. function isEmptyContent(c) {
  259. return c === undefined || c === null || c === '' || Array.isArray(c) && c.length === 0;
  260. }
  261. function isEmptyElement(c) {
  262. return c && (c.type === _vue.Comment || c.type === _vue.Fragment && c.children.length === 0 || c.type === _vue.Text && c.children.trim() === '');
  263. }
  264. function isEmptySlot(c) {
  265. return !c || c().every(isEmptyElement);
  266. }
  267. function isStringElement(c) {
  268. return c && c.type === _vue.Text;
  269. }
  270. function filterEmpty() {
  271. let children = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  272. const res = [];
  273. children.forEach(child => {
  274. if (Array.isArray(child)) {
  275. res.push(...child);
  276. } else if ((child === null || child === void 0 ? void 0 : child.type) === _vue.Fragment) {
  277. res.push(...filterEmpty(child.children));
  278. } else {
  279. res.push(child);
  280. }
  281. });
  282. return res.filter(c => !isEmptyElement(c));
  283. }
  284. function filterEmptyWithUndefined(children) {
  285. if (children) {
  286. const coms = filterEmpty(children);
  287. return coms.length ? coms : undefined;
  288. } else {
  289. return children;
  290. }
  291. }
  292. function isValidElement(element) {
  293. if (Array.isArray(element) && element.length === 1) {
  294. element = element[0];
  295. }
  296. return element && element.__v_isVNode && typeof element.type !== 'symbol'; // remove text node
  297. }
  298. function getPropsSlot(slots, props) {
  299. let prop = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'default';
  300. var _a, _b;
  301. return (_a = props[prop]) !== null && _a !== void 0 ? _a : (_b = slots[prop]) === null || _b === void 0 ? void 0 : _b.call(slots);
  302. }
  303. const getTextFromElement = ele => {
  304. if (isValidElement(ele) && isStringElement(ele[0])) {
  305. return ele[0].children;
  306. }
  307. return ele;
  308. };
  309. exports.getTextFromElement = getTextFromElement;
  310. var _default = exports.default = hasProp;