util.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.UNSELECTABLE_ATTRIBUTE = exports.UNSELECTABLE_STYLE = undefined;
  6. var _typeof2 = require('babel-runtime/helpers/typeof');
  7. var _typeof3 = _interopRequireDefault(_typeof2);
  8. exports.toTitle = toTitle;
  9. exports.getValuePropValue = getValuePropValue;
  10. exports.getPropValue = getPropValue;
  11. exports.isMultiple = isMultiple;
  12. exports.isCombobox = isCombobox;
  13. exports.isMultipleOrTags = isMultipleOrTags;
  14. exports.isMultipleOrTagsOrCombobox = isMultipleOrTagsOrCombobox;
  15. exports.isSingleMode = isSingleMode;
  16. exports.toArray = toArray;
  17. exports.getMapKey = getMapKey;
  18. exports.preventDefaultEvent = preventDefaultEvent;
  19. exports.findIndexInValueBySingleValue = findIndexInValueBySingleValue;
  20. exports.getLabelFromPropsValue = getLabelFromPropsValue;
  21. exports.getSelectKeys = getSelectKeys;
  22. exports.findFirstMenuItem = findFirstMenuItem;
  23. exports.includesSeparators = includesSeparators;
  24. exports.splitBySeparators = splitBySeparators;
  25. exports.defaultFilterFn = defaultFilterFn;
  26. exports.validateOptionValue = validateOptionValue;
  27. exports.saveRef = saveRef;
  28. exports.generateUUID = generateUUID;
  29. var _propsUtil = require('../_util/props-util');
  30. var _vnode = require('../_util/vnode');
  31. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  32. function toTitle(title) {
  33. if (typeof title === 'string') {
  34. return title.trim();
  35. }
  36. return '';
  37. }
  38. function getValuePropValue(child) {
  39. if (!child) {
  40. return null;
  41. }
  42. var props = (0, _propsUtil.getPropsData)(child);
  43. if ('value' in props) {
  44. return props.value;
  45. }
  46. if ((0, _propsUtil.getKey)(child) !== undefined) {
  47. return (0, _propsUtil.getKey)(child);
  48. }
  49. if ((0, _propsUtil.getSlotOptions)(child).isSelectOptGroup) {
  50. var label = (0, _propsUtil.getComponentFromProp)(child, 'label');
  51. if (label) {
  52. return label;
  53. }
  54. }
  55. throw new Error('Need at least a key or a value or a label (only for OptGroup) for ' + child);
  56. }
  57. function getPropValue(child, prop) {
  58. if (prop === 'value') {
  59. return getValuePropValue(child);
  60. }
  61. if (prop === 'children') {
  62. var newChild = child.$slots ? (0, _vnode.cloneVNodes)(child.$slots['default'], true) : (0, _vnode.cloneVNodes)(child.componentOptions.children, true);
  63. if (newChild.length === 1 && !newChild[0].tag) {
  64. return newChild[0].text;
  65. }
  66. return newChild;
  67. }
  68. var data = (0, _propsUtil.getPropsData)(child);
  69. if (prop in data) {
  70. return data[prop];
  71. } else {
  72. return (0, _propsUtil.getAttrs)(child)[prop];
  73. }
  74. }
  75. function isMultiple(props) {
  76. return props.multiple;
  77. }
  78. function isCombobox(props) {
  79. return props.combobox;
  80. }
  81. function isMultipleOrTags(props) {
  82. return props.multiple || props.tags;
  83. }
  84. function isMultipleOrTagsOrCombobox(props) {
  85. return isMultipleOrTags(props) || isCombobox(props);
  86. }
  87. function isSingleMode(props) {
  88. return !isMultipleOrTagsOrCombobox(props);
  89. }
  90. function toArray(value) {
  91. var ret = value;
  92. if (value === undefined) {
  93. ret = [];
  94. } else if (!Array.isArray(value)) {
  95. ret = [value];
  96. }
  97. return ret;
  98. }
  99. function getMapKey(value) {
  100. return (typeof value === 'undefined' ? 'undefined' : (0, _typeof3['default'])(value)) + '-' + value;
  101. }
  102. function preventDefaultEvent(e) {
  103. e.preventDefault();
  104. }
  105. function findIndexInValueBySingleValue(value, singleValue) {
  106. var index = -1;
  107. if (value) {
  108. for (var i = 0; i < value.length; i++) {
  109. if (value[i] === singleValue) {
  110. index = i;
  111. break;
  112. }
  113. }
  114. }
  115. return index;
  116. }
  117. function getLabelFromPropsValue(value, key) {
  118. var label = void 0;
  119. value = toArray(value);
  120. if (value) {
  121. for (var i = 0; i < value.length; i++) {
  122. if (value[i].key === key) {
  123. label = value[i].label;
  124. break;
  125. }
  126. }
  127. }
  128. return label;
  129. }
  130. function getSelectKeys(menuItems, value) {
  131. if (value === null || value === undefined) {
  132. return [];
  133. }
  134. var selectedKeys = [];
  135. menuItems.forEach(function (item) {
  136. if ((0, _propsUtil.getSlotOptions)(item).isMenuItemGroup) {
  137. selectedKeys = selectedKeys.concat(getSelectKeys(item.componentOptions.children, value));
  138. } else {
  139. var itemValue = getValuePropValue(item);
  140. var itemKey = item.key;
  141. if (findIndexInValueBySingleValue(value, itemValue) !== -1 && itemKey !== undefined) {
  142. selectedKeys.push(itemKey);
  143. }
  144. }
  145. });
  146. return selectedKeys;
  147. }
  148. var UNSELECTABLE_STYLE = exports.UNSELECTABLE_STYLE = {
  149. userSelect: 'none',
  150. WebkitUserSelect: 'none'
  151. };
  152. var UNSELECTABLE_ATTRIBUTE = exports.UNSELECTABLE_ATTRIBUTE = {
  153. unselectable: 'on'
  154. };
  155. function findFirstMenuItem(children) {
  156. for (var i = 0; i < children.length; i++) {
  157. var child = children[i];
  158. var props = (0, _propsUtil.getPropsData)(child);
  159. if ((0, _propsUtil.getSlotOptions)(child).isMenuItemGroup) {
  160. var found = findFirstMenuItem(child.componentOptions.children);
  161. if (found) {
  162. return found;
  163. }
  164. } else if (!(props.disabled || props.disabled === '')) {
  165. return child;
  166. }
  167. }
  168. return null;
  169. }
  170. function includesSeparators(str, separators) {
  171. for (var i = 0; i < separators.length; ++i) {
  172. if (str.lastIndexOf(separators[i]) > 0) {
  173. return true;
  174. }
  175. }
  176. return false;
  177. }
  178. function splitBySeparators(str, separators) {
  179. var reg = new RegExp('[' + separators.join() + ']');
  180. return str.split(reg).filter(function (token) {
  181. return token;
  182. });
  183. }
  184. function defaultFilterFn(input, child) {
  185. var props = (0, _propsUtil.getPropsData)(child);
  186. if (props.disabled) {
  187. return false;
  188. }
  189. var value = getPropValue(child, this.optionFilterProp);
  190. if (value.length && value[0].text) {
  191. value = value[0].text;
  192. } else {
  193. value = String(value);
  194. }
  195. return value.toLowerCase().indexOf(input.toLowerCase()) > -1;
  196. }
  197. function validateOptionValue(value, props) {
  198. if (isSingleMode(props) || isMultiple(props)) {
  199. return;
  200. }
  201. if (typeof value !== 'string') {
  202. throw new Error('Invalid `value` of type `' + (typeof value === 'undefined' ? 'undefined' : (0, _typeof3['default'])(value)) + '` supplied to Option, ' + 'expected `string` when `tags/combobox` is `true`.');
  203. }
  204. }
  205. function saveRef(instance, name) {
  206. return function (node) {
  207. instance[name] = node;
  208. };
  209. }
  210. function generateUUID() {
  211. if (process.env.NODE_ENV === 'test') {
  212. return 'test-uuid';
  213. }
  214. var d = new Date().getTime();
  215. var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  216. var r = (d + Math.random() * 16) % 16 | 0;
  217. d = Math.floor(d / 16);
  218. return (c === 'x' ? r : r & 0x7 | 0x8).toString(16);
  219. });
  220. return uuid;
  221. }