index.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 get() {
  9. return _util.camelize;
  10. }
  11. });
  12. exports.default = void 0;
  13. exports.filterEmpty = filterEmpty;
  14. exports.filterEmptyWithUndefined = filterEmptyWithUndefined;
  15. exports.getAttrs = exports.getAllProps = exports.getAllChildren = exports.flattenChildren = exports.findDOMNode = void 0;
  16. exports.getClass = getClass;
  17. exports.getComponentFromProp = exports.getComponent = void 0;
  18. exports.getComponentName = getComponentName;
  19. exports.getDataEvents = getDataEvents;
  20. exports.getEvent = getEvent;
  21. exports.getEvents = getEvents;
  22. exports.getKey = void 0;
  23. exports.getListeners = getListeners;
  24. exports.getPropsData = exports.getOptionProps = void 0;
  25. exports.getPropsSlot = getPropsSlot;
  26. exports.getSlots = exports.getSlotOptions = exports.getSlot = void 0;
  27. exports.getStyle = getStyle;
  28. exports.hasProp = exports.getValueByProp = exports.getTextFromElement = void 0;
  29. Object.defineProperty(exports, "initDefaultProps", {
  30. enumerable: true,
  31. get: function get() {
  32. return _initDefaultProps.default;
  33. }
  34. });
  35. exports.isEmptyContent = isEmptyContent;
  36. exports.isEmptyElement = isEmptyElement;
  37. exports.isEmptySlot = isEmptySlot;
  38. exports.isFragment = isFragment;
  39. exports.isStringElement = isStringElement;
  40. exports.isValidElement = isValidElement;
  41. exports.mergeProps = mergeProps;
  42. exports.splitAttrs = exports.slotHasProp = exports.parseStyleText = void 0;
  43. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  44. var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
  45. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  46. var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
  47. var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  48. var _isPlainObject = _interopRequireDefault(require("lodash/isPlainObject"));
  49. var _classNames = _interopRequireDefault(require("../classNames"));
  50. var _vue = require("vue");
  51. var _util = require("../util");
  52. var _isValid = _interopRequireDefault(require("../isValid"));
  53. var _initDefaultProps = _interopRequireDefault(require("./initDefaultProps"));
  54. var _this = void 0;
  55. // function getType(fn) {
  56. // const match = fn && fn.toString().match(/^\s*function (\w+)/);
  57. // return match ? match[1] : '';
  58. // }
  59. var splitAttrs = function splitAttrs(attrs) {
  60. var allAttrs = Object.keys(attrs);
  61. var eventAttrs = {};
  62. var onEvents = {};
  63. var extraAttrs = {};
  64. for (var i = 0, l = allAttrs.length; i < l; i++) {
  65. var key = allAttrs[i];
  66. if ((0, _util.isOn)(key)) {
  67. eventAttrs[key[2].toLowerCase() + key.slice(3)] = attrs[key];
  68. onEvents[key] = attrs[key];
  69. } else {
  70. extraAttrs[key] = attrs[key];
  71. }
  72. }
  73. return {
  74. onEvents: onEvents,
  75. events: eventAttrs,
  76. extraAttrs: extraAttrs
  77. };
  78. };
  79. exports.splitAttrs = splitAttrs;
  80. var parseStyleText = function parseStyleText() {
  81. var cssText = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  82. var camel = arguments.length > 1 ? arguments[1] : undefined;
  83. var res = {};
  84. var listDelimiter = /;(?![^(]*\))/g;
  85. var propertyDelimiter = /:(.+)/;
  86. if ((0, _typeof2.default)(cssText) === 'object') return cssText;
  87. cssText.split(listDelimiter).forEach(function (item) {
  88. if (item) {
  89. var tmp = item.split(propertyDelimiter);
  90. if (tmp.length > 1) {
  91. var k = camel ? (0, _util.camelize)(tmp[0].trim()) : tmp[0].trim();
  92. res[k] = tmp[1].trim();
  93. }
  94. }
  95. });
  96. return res;
  97. };
  98. exports.parseStyleText = parseStyleText;
  99. var hasProp = function hasProp(instance, prop) {
  100. return instance[prop] !== undefined;
  101. };
  102. // 重构后直接使用 hasProp 替换
  103. exports.hasProp = hasProp;
  104. var slotHasProp = function slotHasProp(slot, prop) {
  105. return hasProp(slot, prop);
  106. };
  107. exports.slotHasProp = slotHasProp;
  108. var getScopedSlots = function getScopedSlots(ele) {
  109. return ele.data && ele.data.scopedSlots || {};
  110. };
  111. var getSlots = function getSlots(ele) {
  112. var componentOptions = ele.componentOptions || {};
  113. if (ele.$vnode) {
  114. componentOptions = ele.$vnode.componentOptions || {};
  115. }
  116. var children = ele.children || componentOptions.children || [];
  117. var slots = {};
  118. children.forEach(function (child) {
  119. if (!isEmptyElement(child)) {
  120. var name = child.data && child.data.slot || 'default';
  121. slots[name] = slots[name] || [];
  122. slots[name].push(child);
  123. }
  124. });
  125. return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, slots), getScopedSlots(ele));
  126. };
  127. exports.getSlots = getSlots;
  128. var flattenChildren = function flattenChildren() {
  129. var children = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  130. var filterEmpty = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  131. var temp = Array.isArray(children) ? children : [children];
  132. var res = [];
  133. temp.forEach(function (child) {
  134. if (Array.isArray(child)) {
  135. res.push.apply(res, (0, _toConsumableArray2.default)(flattenChildren(child, filterEmpty)));
  136. } else if (child && child.type === _vue.Fragment) {
  137. res.push.apply(res, (0, _toConsumableArray2.default)(flattenChildren(child.children, filterEmpty)));
  138. } else if (child && (0, _vue.isVNode)(child)) {
  139. if (filterEmpty && !isEmptyElement(child)) {
  140. res.push(child);
  141. } else if (!filterEmpty) {
  142. res.push(child);
  143. }
  144. } else if ((0, _isValid.default)(child)) {
  145. res.push(child);
  146. }
  147. });
  148. return res;
  149. };
  150. exports.flattenChildren = flattenChildren;
  151. var getSlot = function getSlot(self) {
  152. var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';
  153. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  154. if ((0, _vue.isVNode)(self)) {
  155. if (self.type === _vue.Fragment) {
  156. return name === 'default' ? flattenChildren(self.children) : [];
  157. } else if (self.children && self.children[name]) {
  158. return flattenChildren(self.children[name](options));
  159. } else {
  160. return [];
  161. }
  162. } else {
  163. var res = self.$slots[name] && self.$slots[name](options);
  164. return flattenChildren(res);
  165. }
  166. };
  167. exports.getSlot = getSlot;
  168. var getAllChildren = function getAllChildren(ele) {
  169. var componentOptions = ele.componentOptions || {};
  170. if (ele.$vnode) {
  171. componentOptions = ele.$vnode.componentOptions || {};
  172. }
  173. return ele.children || componentOptions.children || [];
  174. };
  175. exports.getAllChildren = getAllChildren;
  176. var getSlotOptions = function getSlotOptions() {
  177. throw Error('使用 .type 直接取值');
  178. };
  179. exports.getSlotOptions = getSlotOptions;
  180. var findDOMNode = function findDOMNode(instance) {
  181. var _instance$vnode;
  182. var node = (instance === null || instance === void 0 ? void 0 : (_instance$vnode = instance.vnode) === null || _instance$vnode === void 0 ? void 0 : _instance$vnode.el) || instance && (instance.$el || instance);
  183. while (node && !node.tagName) {
  184. node = node.nextSibling;
  185. }
  186. return node;
  187. };
  188. exports.findDOMNode = findDOMNode;
  189. var getOptionProps = function getOptionProps(instance) {
  190. var res = {};
  191. if (instance.$ && instance.$.vnode) {
  192. var props = instance.$.vnode.props || {};
  193. Object.keys(instance.$props).forEach(function (k) {
  194. var v = instance.$props[k];
  195. var hyphenateKey = (0, _util.hyphenate)(k);
  196. if (v !== undefined || hyphenateKey in props) {
  197. res[k] = v; // 直接取 $props[k]
  198. }
  199. });
  200. } else if ((0, _vue.isVNode)(instance) && (0, _typeof2.default)(instance.type) === 'object') {
  201. var originProps = instance.props || {};
  202. var _props = {};
  203. Object.keys(originProps).forEach(function (key) {
  204. _props[(0, _util.camelize)(key)] = originProps[key];
  205. });
  206. var options = instance.type.props || {};
  207. Object.keys(options).forEach(function (k) {
  208. var v = (0, _util.resolvePropValue)(options, _props, k, _props[k]);
  209. if (v !== undefined || k in _props) {
  210. res[k] = v;
  211. }
  212. });
  213. }
  214. return res;
  215. };
  216. exports.getOptionProps = getOptionProps;
  217. var getComponent = function getComponent(instance) {
  218. var prop = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';
  219. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : instance;
  220. var execute = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
  221. var com = undefined;
  222. if (instance.$) {
  223. var temp = instance[prop];
  224. if (temp !== undefined) {
  225. return typeof temp === 'function' && execute ? temp(options) : temp;
  226. } else {
  227. com = instance.$slots[prop];
  228. com = execute && com ? com(options) : com;
  229. }
  230. } else if ((0, _vue.isVNode)(instance)) {
  231. var _temp = instance.props && instance.props[prop];
  232. if (_temp !== undefined && instance.props !== null) {
  233. return typeof _temp === 'function' && execute ? _temp(options) : _temp;
  234. } else if (instance.type === _vue.Fragment) {
  235. com = instance.children;
  236. } else if (instance.children && instance.children[prop]) {
  237. com = instance.children[prop];
  238. com = execute && com ? com(options) : com;
  239. }
  240. }
  241. if (Array.isArray(com)) {
  242. com = flattenChildren(com);
  243. com = com.length === 1 ? com[0] : com;
  244. com = com.length === 0 ? undefined : com;
  245. }
  246. return com;
  247. };
  248. exports.getComponent = getComponent;
  249. var getComponentFromProp = function getComponentFromProp(instance, prop) {
  250. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : instance;
  251. var execute = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
  252. if (instance.$createElement) {
  253. // const h = instance.$createElement;
  254. var temp = instance[prop];
  255. if (temp !== undefined) {
  256. return typeof temp === 'function' && execute ? temp(_vue.h, options) : temp;
  257. }
  258. return instance.$scopedSlots[prop] && execute && instance.$scopedSlots[prop](options) || instance.$scopedSlots[prop] || instance.$slots[prop] || undefined;
  259. } else {
  260. // const h = instance.context.$createElement;
  261. var _temp2 = getPropsData(instance)[prop];
  262. if (_temp2 !== undefined) {
  263. return typeof _temp2 === 'function' && execute ? _temp2(_vue.h, options) : _temp2;
  264. }
  265. var slotScope = getScopedSlots(instance)[prop];
  266. if (slotScope !== undefined) {
  267. return typeof slotScope === 'function' && execute ? slotScope(_vue.h, options) : slotScope;
  268. }
  269. var slotsProp = [];
  270. var componentOptions = instance.componentOptions || {};
  271. (componentOptions.children || []).forEach(function (child) {
  272. if (child.data && child.data.slot === prop) {
  273. if (child.data.attrs) {
  274. delete child.data.attrs.slot;
  275. }
  276. if (child.tag === 'template') {
  277. slotsProp.push(child.children);
  278. } else {
  279. slotsProp.push(child);
  280. }
  281. }
  282. });
  283. return slotsProp.length ? slotsProp : undefined;
  284. }
  285. };
  286. exports.getComponentFromProp = getComponentFromProp;
  287. var getAllProps = function getAllProps(ele) {
  288. var props = getOptionProps(ele);
  289. if (ele.$) {
  290. props = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), _this.$attrs);
  291. } else {
  292. props = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, ele.props), props);
  293. }
  294. return props;
  295. };
  296. exports.getAllProps = getAllProps;
  297. var getPropsData = function getPropsData(ins) {
  298. var vnode = ins.$ ? ins.$ : ins;
  299. var res = {};
  300. var originProps = vnode.props || {};
  301. var props = {};
  302. Object.keys(originProps).forEach(function (key) {
  303. props[(0, _util.camelize)(key)] = originProps[key];
  304. });
  305. var options = (0, _isPlainObject.default)(vnode.type) ? vnode.type.props : {};
  306. options && Object.keys(options).forEach(function (k) {
  307. var v = (0, _util.resolvePropValue)(options, props, k, props[k]);
  308. if (k in props) {
  309. // 仅包含 props,不包含默认值
  310. res[k] = v;
  311. }
  312. });
  313. return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), res); // 合并事件、未声明属性等
  314. };
  315. exports.getPropsData = getPropsData;
  316. var getValueByProp = function getValueByProp(ele, prop) {
  317. return getPropsData(ele)[prop];
  318. };
  319. exports.getValueByProp = getValueByProp;
  320. var getAttrs = function getAttrs(ele) {
  321. var data = ele.data;
  322. if (ele.$vnode) {
  323. data = ele.$vnode.data;
  324. }
  325. return data ? data.attrs || {} : {};
  326. };
  327. exports.getAttrs = getAttrs;
  328. var getKey = function getKey(ele) {
  329. var key = ele.key;
  330. return key;
  331. };
  332. exports.getKey = getKey;
  333. function getEvents() {
  334. var ele = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  335. var on = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  336. var props = {};
  337. if (ele.$) {
  338. props = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), ele.$attrs);
  339. } else {
  340. props = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), ele.props);
  341. }
  342. return splitAttrs(props)[on ? 'onEvents' : 'events'];
  343. }
  344. function getEvent(child, event) {
  345. return child.props && child.props[event];
  346. }
  347. // 获取 xxx.native 或者 原生标签 事件
  348. function getDataEvents(child) {
  349. var events = {};
  350. if (child.data && child.data.on) {
  351. events = child.data.on;
  352. }
  353. return (0, _objectSpread2.default)({}, events);
  354. }
  355. // use getListeners instead this.$listeners
  356. // https://github.com/vueComponent/ant-design-vue/issues/1705
  357. function getListeners(context) {
  358. return (context.$vnode ? context.$vnode.componentOptions.listeners : context.$listeners) || {};
  359. }
  360. function getClass(ele) {
  361. var props = ((0, _vue.isVNode)(ele) ? ele.props : ele.$attrs) || {};
  362. var tempCls = props.class || {};
  363. var cls = {};
  364. if (typeof tempCls === 'string') {
  365. tempCls.split(' ').forEach(function (c) {
  366. cls[c.trim()] = true;
  367. });
  368. } else if (Array.isArray(tempCls)) {
  369. (0, _classNames.default)(tempCls).split(' ').forEach(function (c) {
  370. cls[c.trim()] = true;
  371. });
  372. } else {
  373. cls = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, cls), tempCls);
  374. }
  375. return cls;
  376. }
  377. function getStyle(ele, camel) {
  378. var props = ((0, _vue.isVNode)(ele) ? ele.props : ele.$attrs) || {};
  379. var style = props.style || {};
  380. if (typeof style === 'string') {
  381. style = parseStyleText(style, camel);
  382. } else if (camel && style) {
  383. // 驼峰化
  384. var res = {};
  385. Object.keys(style).forEach(function (k) {
  386. return res[(0, _util.camelize)(k)] = style[k];
  387. });
  388. return res;
  389. }
  390. return style;
  391. }
  392. function getComponentName(opts) {
  393. return opts && (opts.Ctor.options.name || opts.tag);
  394. }
  395. function isFragment(c) {
  396. return c.length === 1 && c[0].type === _vue.Fragment;
  397. }
  398. function isEmptyContent(c) {
  399. return c === undefined || c === null || c === '' || Array.isArray(c) && c.length === 0;
  400. }
  401. function isEmptyElement(c) {
  402. return c && (c.type === _vue.Comment || c.type === _vue.Fragment && c.children.length === 0 || c.type === _vue.Text && c.children.trim() === '');
  403. }
  404. function isEmptySlot(c) {
  405. return !c || c().every(isEmptyElement);
  406. }
  407. function isStringElement(c) {
  408. return c && c.type === _vue.Text;
  409. }
  410. function filterEmpty() {
  411. var children = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  412. var res = [];
  413. children.forEach(function (child) {
  414. if (Array.isArray(child)) {
  415. res.push.apply(res, (0, _toConsumableArray2.default)(child));
  416. } else if ((child === null || child === void 0 ? void 0 : child.type) === _vue.Fragment) {
  417. res.push.apply(res, (0, _toConsumableArray2.default)(filterEmpty(child.children)));
  418. } else {
  419. res.push(child);
  420. }
  421. });
  422. return res.filter(function (c) {
  423. return !isEmptyElement(c);
  424. });
  425. }
  426. function filterEmptyWithUndefined(children) {
  427. if (children) {
  428. var coms = filterEmpty(children);
  429. return coms.length ? coms : undefined;
  430. } else {
  431. return children;
  432. }
  433. }
  434. function mergeProps() {
  435. var args = [].slice.call(arguments, 0);
  436. var props = {};
  437. args.forEach(function () {
  438. var p = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  439. for (var _i = 0, _Object$entries = Object.entries(p); _i < _Object$entries.length; _i++) {
  440. var _Object$entries$_i = (0, _slicedToArray2.default)(_Object$entries[_i], 2),
  441. k = _Object$entries$_i[0],
  442. v = _Object$entries$_i[1];
  443. props[k] = props[k] || {};
  444. if ((0, _isPlainObject.default)(v)) {
  445. (0, _extends2.default)(props[k], v);
  446. } else {
  447. props[k] = v;
  448. }
  449. }
  450. });
  451. return props;
  452. }
  453. function isValidElement(element) {
  454. if (Array.isArray(element) && element.length === 1) {
  455. element = element[0];
  456. }
  457. return element && element.__v_isVNode && (0, _typeof2.default)(element.type) !== 'symbol'; // remove text node
  458. }
  459. function getPropsSlot(slots, props) {
  460. var _props$prop, _slots$prop;
  461. var prop = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'default';
  462. return (_props$prop = props[prop]) !== null && _props$prop !== void 0 ? _props$prop : (_slots$prop = slots[prop]) === null || _slots$prop === void 0 ? void 0 : _slots$prop.call(slots);
  463. }
  464. var getTextFromElement = function getTextFromElement(ele) {
  465. if (isValidElement(ele) && isStringElement(ele[0])) {
  466. return ele[0].children;
  467. }
  468. return ele;
  469. };
  470. exports.getTextFromElement = getTextFromElement;
  471. var _default = hasProp;
  472. exports.default = _default;