| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474 |
- "use strict";
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- Object.defineProperty(exports, "camelize", {
- enumerable: true,
- get: function get() {
- return _util.camelize;
- }
- });
- exports.default = void 0;
- exports.filterEmpty = filterEmpty;
- exports.filterEmptyWithUndefined = filterEmptyWithUndefined;
- exports.getAttrs = exports.getAllProps = exports.getAllChildren = exports.flattenChildren = exports.findDOMNode = void 0;
- exports.getClass = getClass;
- exports.getComponentFromProp = exports.getComponent = void 0;
- exports.getComponentName = getComponentName;
- exports.getDataEvents = getDataEvents;
- exports.getEvent = getEvent;
- exports.getEvents = getEvents;
- exports.getKey = void 0;
- exports.getListeners = getListeners;
- exports.getPropsData = exports.getOptionProps = void 0;
- exports.getPropsSlot = getPropsSlot;
- exports.getSlots = exports.getSlotOptions = exports.getSlot = void 0;
- exports.getStyle = getStyle;
- exports.hasProp = exports.getValueByProp = exports.getTextFromElement = void 0;
- Object.defineProperty(exports, "initDefaultProps", {
- enumerable: true,
- get: function get() {
- return _initDefaultProps.default;
- }
- });
- exports.isEmptyContent = isEmptyContent;
- exports.isEmptyElement = isEmptyElement;
- exports.isEmptySlot = isEmptySlot;
- exports.isFragment = isFragment;
- exports.isStringElement = isStringElement;
- exports.isValidElement = isValidElement;
- exports.mergeProps = mergeProps;
- exports.splitAttrs = exports.slotHasProp = exports.parseStyleText = void 0;
- var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
- var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
- var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
- var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
- var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
- var _isPlainObject = _interopRequireDefault(require("lodash/isPlainObject"));
- var _classNames = _interopRequireDefault(require("../classNames"));
- var _vue = require("vue");
- var _util = require("../util");
- var _isValid = _interopRequireDefault(require("../isValid"));
- var _initDefaultProps = _interopRequireDefault(require("./initDefaultProps"));
- var _this = void 0;
- // function getType(fn) {
- // const match = fn && fn.toString().match(/^\s*function (\w+)/);
- // return match ? match[1] : '';
- // }
- var splitAttrs = function splitAttrs(attrs) {
- var allAttrs = Object.keys(attrs);
- var eventAttrs = {};
- var onEvents = {};
- var extraAttrs = {};
- for (var i = 0, l = allAttrs.length; i < l; i++) {
- var key = allAttrs[i];
- if ((0, _util.isOn)(key)) {
- eventAttrs[key[2].toLowerCase() + key.slice(3)] = attrs[key];
- onEvents[key] = attrs[key];
- } else {
- extraAttrs[key] = attrs[key];
- }
- }
- return {
- onEvents: onEvents,
- events: eventAttrs,
- extraAttrs: extraAttrs
- };
- };
- exports.splitAttrs = splitAttrs;
- var parseStyleText = function parseStyleText() {
- var cssText = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
- var camel = arguments.length > 1 ? arguments[1] : undefined;
- var res = {};
- var listDelimiter = /;(?![^(]*\))/g;
- var propertyDelimiter = /:(.+)/;
- if ((0, _typeof2.default)(cssText) === 'object') return cssText;
- cssText.split(listDelimiter).forEach(function (item) {
- if (item) {
- var tmp = item.split(propertyDelimiter);
- if (tmp.length > 1) {
- var k = camel ? (0, _util.camelize)(tmp[0].trim()) : tmp[0].trim();
- res[k] = tmp[1].trim();
- }
- }
- });
- return res;
- };
- exports.parseStyleText = parseStyleText;
- var hasProp = function hasProp(instance, prop) {
- return instance[prop] !== undefined;
- };
- // 重构后直接使用 hasProp 替换
- exports.hasProp = hasProp;
- var slotHasProp = function slotHasProp(slot, prop) {
- return hasProp(slot, prop);
- };
- exports.slotHasProp = slotHasProp;
- var getScopedSlots = function getScopedSlots(ele) {
- return ele.data && ele.data.scopedSlots || {};
- };
- var getSlots = function getSlots(ele) {
- var componentOptions = ele.componentOptions || {};
- if (ele.$vnode) {
- componentOptions = ele.$vnode.componentOptions || {};
- }
- var children = ele.children || componentOptions.children || [];
- var slots = {};
- children.forEach(function (child) {
- if (!isEmptyElement(child)) {
- var name = child.data && child.data.slot || 'default';
- slots[name] = slots[name] || [];
- slots[name].push(child);
- }
- });
- return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, slots), getScopedSlots(ele));
- };
- exports.getSlots = getSlots;
- var flattenChildren = function flattenChildren() {
- var children = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
- var filterEmpty = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
- var temp = Array.isArray(children) ? children : [children];
- var res = [];
- temp.forEach(function (child) {
- if (Array.isArray(child)) {
- res.push.apply(res, (0, _toConsumableArray2.default)(flattenChildren(child, filterEmpty)));
- } else if (child && child.type === _vue.Fragment) {
- res.push.apply(res, (0, _toConsumableArray2.default)(flattenChildren(child.children, filterEmpty)));
- } else if (child && (0, _vue.isVNode)(child)) {
- if (filterEmpty && !isEmptyElement(child)) {
- res.push(child);
- } else if (!filterEmpty) {
- res.push(child);
- }
- } else if ((0, _isValid.default)(child)) {
- res.push(child);
- }
- });
- return res;
- };
- exports.flattenChildren = flattenChildren;
- var getSlot = function getSlot(self) {
- var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';
- var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
- if ((0, _vue.isVNode)(self)) {
- if (self.type === _vue.Fragment) {
- return name === 'default' ? flattenChildren(self.children) : [];
- } else if (self.children && self.children[name]) {
- return flattenChildren(self.children[name](options));
- } else {
- return [];
- }
- } else {
- var res = self.$slots[name] && self.$slots[name](options);
- return flattenChildren(res);
- }
- };
- exports.getSlot = getSlot;
- var getAllChildren = function getAllChildren(ele) {
- var componentOptions = ele.componentOptions || {};
- if (ele.$vnode) {
- componentOptions = ele.$vnode.componentOptions || {};
- }
- return ele.children || componentOptions.children || [];
- };
- exports.getAllChildren = getAllChildren;
- var getSlotOptions = function getSlotOptions() {
- throw Error('使用 .type 直接取值');
- };
- exports.getSlotOptions = getSlotOptions;
- var findDOMNode = function findDOMNode(instance) {
- var _instance$vnode;
- 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);
- while (node && !node.tagName) {
- node = node.nextSibling;
- }
- return node;
- };
- exports.findDOMNode = findDOMNode;
- var getOptionProps = function getOptionProps(instance) {
- var res = {};
- if (instance.$ && instance.$.vnode) {
- var props = instance.$.vnode.props || {};
- Object.keys(instance.$props).forEach(function (k) {
- var v = instance.$props[k];
- var hyphenateKey = (0, _util.hyphenate)(k);
- if (v !== undefined || hyphenateKey in props) {
- res[k] = v; // 直接取 $props[k]
- }
- });
- } else if ((0, _vue.isVNode)(instance) && (0, _typeof2.default)(instance.type) === 'object') {
- var originProps = instance.props || {};
- var _props = {};
- Object.keys(originProps).forEach(function (key) {
- _props[(0, _util.camelize)(key)] = originProps[key];
- });
- var options = instance.type.props || {};
- Object.keys(options).forEach(function (k) {
- var v = (0, _util.resolvePropValue)(options, _props, k, _props[k]);
- if (v !== undefined || k in _props) {
- res[k] = v;
- }
- });
- }
- return res;
- };
- exports.getOptionProps = getOptionProps;
- var getComponent = function getComponent(instance) {
- var prop = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';
- var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : instance;
- var execute = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
- var com = undefined;
- if (instance.$) {
- var temp = instance[prop];
- if (temp !== undefined) {
- return typeof temp === 'function' && execute ? temp(options) : temp;
- } else {
- com = instance.$slots[prop];
- com = execute && com ? com(options) : com;
- }
- } else if ((0, _vue.isVNode)(instance)) {
- var _temp = instance.props && instance.props[prop];
- if (_temp !== undefined && instance.props !== null) {
- return typeof _temp === 'function' && execute ? _temp(options) : _temp;
- } else if (instance.type === _vue.Fragment) {
- com = instance.children;
- } else if (instance.children && instance.children[prop]) {
- com = instance.children[prop];
- com = execute && com ? com(options) : com;
- }
- }
- if (Array.isArray(com)) {
- com = flattenChildren(com);
- com = com.length === 1 ? com[0] : com;
- com = com.length === 0 ? undefined : com;
- }
- return com;
- };
- exports.getComponent = getComponent;
- var getComponentFromProp = function getComponentFromProp(instance, prop) {
- var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : instance;
- var execute = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
- if (instance.$createElement) {
- // const h = instance.$createElement;
- var temp = instance[prop];
- if (temp !== undefined) {
- return typeof temp === 'function' && execute ? temp(_vue.h, options) : temp;
- }
- return instance.$scopedSlots[prop] && execute && instance.$scopedSlots[prop](options) || instance.$scopedSlots[prop] || instance.$slots[prop] || undefined;
- } else {
- // const h = instance.context.$createElement;
- var _temp2 = getPropsData(instance)[prop];
- if (_temp2 !== undefined) {
- return typeof _temp2 === 'function' && execute ? _temp2(_vue.h, options) : _temp2;
- }
- var slotScope = getScopedSlots(instance)[prop];
- if (slotScope !== undefined) {
- return typeof slotScope === 'function' && execute ? slotScope(_vue.h, options) : slotScope;
- }
- var slotsProp = [];
- var componentOptions = instance.componentOptions || {};
- (componentOptions.children || []).forEach(function (child) {
- if (child.data && child.data.slot === prop) {
- if (child.data.attrs) {
- delete child.data.attrs.slot;
- }
- if (child.tag === 'template') {
- slotsProp.push(child.children);
- } else {
- slotsProp.push(child);
- }
- }
- });
- return slotsProp.length ? slotsProp : undefined;
- }
- };
- exports.getComponentFromProp = getComponentFromProp;
- var getAllProps = function getAllProps(ele) {
- var props = getOptionProps(ele);
- if (ele.$) {
- props = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), _this.$attrs);
- } else {
- props = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, ele.props), props);
- }
- return props;
- };
- exports.getAllProps = getAllProps;
- var getPropsData = function getPropsData(ins) {
- var vnode = ins.$ ? ins.$ : ins;
- var res = {};
- var originProps = vnode.props || {};
- var props = {};
- Object.keys(originProps).forEach(function (key) {
- props[(0, _util.camelize)(key)] = originProps[key];
- });
- var options = (0, _isPlainObject.default)(vnode.type) ? vnode.type.props : {};
- options && Object.keys(options).forEach(function (k) {
- var v = (0, _util.resolvePropValue)(options, props, k, props[k]);
- if (k in props) {
- // 仅包含 props,不包含默认值
- res[k] = v;
- }
- });
- return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), res); // 合并事件、未声明属性等
- };
- exports.getPropsData = getPropsData;
- var getValueByProp = function getValueByProp(ele, prop) {
- return getPropsData(ele)[prop];
- };
- exports.getValueByProp = getValueByProp;
- var getAttrs = function getAttrs(ele) {
- var data = ele.data;
- if (ele.$vnode) {
- data = ele.$vnode.data;
- }
- return data ? data.attrs || {} : {};
- };
- exports.getAttrs = getAttrs;
- var getKey = function getKey(ele) {
- var key = ele.key;
- return key;
- };
- exports.getKey = getKey;
- function getEvents() {
- var ele = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
- var on = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
- var props = {};
- if (ele.$) {
- props = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), ele.$attrs);
- } else {
- props = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), ele.props);
- }
- return splitAttrs(props)[on ? 'onEvents' : 'events'];
- }
- function getEvent(child, event) {
- return child.props && child.props[event];
- }
- // 获取 xxx.native 或者 原生标签 事件
- function getDataEvents(child) {
- var events = {};
- if (child.data && child.data.on) {
- events = child.data.on;
- }
- return (0, _objectSpread2.default)({}, events);
- }
- // use getListeners instead this.$listeners
- // https://github.com/vueComponent/ant-design-vue/issues/1705
- function getListeners(context) {
- return (context.$vnode ? context.$vnode.componentOptions.listeners : context.$listeners) || {};
- }
- function getClass(ele) {
- var props = ((0, _vue.isVNode)(ele) ? ele.props : ele.$attrs) || {};
- var tempCls = props.class || {};
- var cls = {};
- if (typeof tempCls === 'string') {
- tempCls.split(' ').forEach(function (c) {
- cls[c.trim()] = true;
- });
- } else if (Array.isArray(tempCls)) {
- (0, _classNames.default)(tempCls).split(' ').forEach(function (c) {
- cls[c.trim()] = true;
- });
- } else {
- cls = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, cls), tempCls);
- }
- return cls;
- }
- function getStyle(ele, camel) {
- var props = ((0, _vue.isVNode)(ele) ? ele.props : ele.$attrs) || {};
- var style = props.style || {};
- if (typeof style === 'string') {
- style = parseStyleText(style, camel);
- } else if (camel && style) {
- // 驼峰化
- var res = {};
- Object.keys(style).forEach(function (k) {
- return res[(0, _util.camelize)(k)] = style[k];
- });
- return res;
- }
- return style;
- }
- function getComponentName(opts) {
- return opts && (opts.Ctor.options.name || opts.tag);
- }
- function isFragment(c) {
- return c.length === 1 && c[0].type === _vue.Fragment;
- }
- function isEmptyContent(c) {
- return c === undefined || c === null || c === '' || Array.isArray(c) && c.length === 0;
- }
- function isEmptyElement(c) {
- return c && (c.type === _vue.Comment || c.type === _vue.Fragment && c.children.length === 0 || c.type === _vue.Text && c.children.trim() === '');
- }
- function isEmptySlot(c) {
- return !c || c().every(isEmptyElement);
- }
- function isStringElement(c) {
- return c && c.type === _vue.Text;
- }
- function filterEmpty() {
- var children = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
- var res = [];
- children.forEach(function (child) {
- if (Array.isArray(child)) {
- res.push.apply(res, (0, _toConsumableArray2.default)(child));
- } else if ((child === null || child === void 0 ? void 0 : child.type) === _vue.Fragment) {
- res.push.apply(res, (0, _toConsumableArray2.default)(filterEmpty(child.children)));
- } else {
- res.push(child);
- }
- });
- return res.filter(function (c) {
- return !isEmptyElement(c);
- });
- }
- function filterEmptyWithUndefined(children) {
- if (children) {
- var coms = filterEmpty(children);
- return coms.length ? coms : undefined;
- } else {
- return children;
- }
- }
- function mergeProps() {
- var args = [].slice.call(arguments, 0);
- var props = {};
- args.forEach(function () {
- var p = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
- for (var _i = 0, _Object$entries = Object.entries(p); _i < _Object$entries.length; _i++) {
- var _Object$entries$_i = (0, _slicedToArray2.default)(_Object$entries[_i], 2),
- k = _Object$entries$_i[0],
- v = _Object$entries$_i[1];
- props[k] = props[k] || {};
- if ((0, _isPlainObject.default)(v)) {
- (0, _extends2.default)(props[k], v);
- } else {
- props[k] = v;
- }
- }
- });
- return props;
- }
- function isValidElement(element) {
- if (Array.isArray(element) && element.length === 1) {
- element = element[0];
- }
- return element && element.__v_isVNode && (0, _typeof2.default)(element.type) !== 'symbol'; // remove text node
- }
- function getPropsSlot(slots, props) {
- var _props$prop, _slots$prop;
- var prop = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'default';
- 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);
- }
- var getTextFromElement = function getTextFromElement(ele) {
- if (isValidElement(ele) && isStringElement(ele[0])) {
- return ele[0].children;
- }
- return ele;
- };
- exports.getTextFromElement = getTextFromElement;
- var _default = hasProp;
- exports.default = _default;
|