33ea94820692ec55f35aa15b08f07c8516b14301a3a9255108f3fc209558f5128a1ab709dfa690904f55a924753fc3ff10f679cb6c75b4b81e1ea6bddc64be 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import { getValueByPath } from 'element-ui/src/utils/util';
  2. export const getCell = function(event) {
  3. let cell = event.target;
  4. while (cell && cell.tagName.toUpperCase() !== 'HTML') {
  5. if (cell.tagName.toUpperCase() === 'TD') {
  6. return cell;
  7. }
  8. cell = cell.parentNode;
  9. }
  10. return null;
  11. };
  12. const isObject = function(obj) {
  13. return obj !== null && typeof obj === 'object';
  14. };
  15. export const orderBy = function(array, sortKey, reverse, sortMethod, sortBy) {
  16. if (!sortKey && !sortMethod && (!sortBy || Array.isArray(sortBy) && !sortBy.length)) {
  17. return array;
  18. }
  19. if (typeof reverse === 'string') {
  20. reverse = reverse === 'descending' ? -1 : 1;
  21. } else {
  22. reverse = (reverse && reverse < 0) ? -1 : 1;
  23. }
  24. const getKey = sortMethod ? null : function(value, index) {
  25. if (sortBy) {
  26. if (!Array.isArray(sortBy)) {
  27. sortBy = [sortBy];
  28. }
  29. return sortBy.map(function(by) {
  30. if (typeof by === 'string') {
  31. return getValueByPath(value, by);
  32. } else {
  33. return by(value, index, array);
  34. }
  35. });
  36. }
  37. if (sortKey !== '$key') {
  38. if (isObject(value) && '$value' in value) value = value.$value;
  39. }
  40. return [isObject(value) ? getValueByPath(value, sortKey) : value];
  41. };
  42. const compare = function(a, b) {
  43. if (sortMethod) {
  44. return sortMethod(a.value, b.value);
  45. }
  46. for (let i = 0, len = a.key.length; i < len; i++) {
  47. if (a.key[i] < b.key[i]) {
  48. return -1;
  49. }
  50. if (a.key[i] > b.key[i]) {
  51. return 1;
  52. }
  53. }
  54. return 0;
  55. };
  56. return array.map(function(value, index) {
  57. return {
  58. value: value,
  59. index: index,
  60. key: getKey ? getKey(value, index) : null
  61. };
  62. }).sort(function(a, b) {
  63. let order = compare(a, b);
  64. if (!order) {
  65. // make stable https://en.wikipedia.org/wiki/Sorting_algorithm#Stability
  66. order = a.index - b.index;
  67. }
  68. return order * reverse;
  69. }).map(item => item.value);
  70. };
  71. export const getColumnById = function(table, columnId) {
  72. let column = null;
  73. table.columns.forEach(function(item) {
  74. if (item.id === columnId) {
  75. column = item;
  76. }
  77. });
  78. return column;
  79. };
  80. export const getColumnByKey = function(table, columnKey) {
  81. let column = null;
  82. for (let i = 0; i < table.columns.length; i++) {
  83. const item = table.columns[i];
  84. if (item.columnKey === columnKey) {
  85. column = item;
  86. break;
  87. }
  88. }
  89. return column;
  90. };
  91. export const getColumnByCell = function(table, cell) {
  92. const matches = (cell.className || '').match(/el-table_[^\s]+/gm);
  93. if (matches) {
  94. return getColumnById(table, matches[0]);
  95. }
  96. return null;
  97. };
  98. export const getRowIdentity = (row, rowKey) => {
  99. if (!row) throw new Error('row is required when get row identity');
  100. if (typeof rowKey === 'string') {
  101. if (rowKey.indexOf('.') < 0) {
  102. return row[rowKey];
  103. }
  104. let key = rowKey.split('.');
  105. let current = row;
  106. for (let i = 0; i < key.length; i++) {
  107. current = current[key[i]];
  108. }
  109. return current;
  110. } else if (typeof rowKey === 'function') {
  111. return rowKey.call(null, row);
  112. }
  113. };
  114. export const getKeysMap = function(array, rowKey) {
  115. const arrayMap = {};
  116. (array || []).forEach((row, index) => {
  117. arrayMap[getRowIdentity(row, rowKey)] = { row, index };
  118. });
  119. return arrayMap;
  120. };
  121. function hasOwn(obj, key) {
  122. return Object.prototype.hasOwnProperty.call(obj, key);
  123. }
  124. export function mergeOptions(defaults, config) {
  125. const options = {};
  126. let key;
  127. for (key in defaults) {
  128. options[key] = defaults[key];
  129. }
  130. for (key in config) {
  131. if (hasOwn(config, key)) {
  132. const value = config[key];
  133. if (typeof value !== 'undefined') {
  134. options[key] = value;
  135. }
  136. }
  137. }
  138. return options;
  139. }
  140. export function parseWidth(width) {
  141. if (width !== undefined) {
  142. width = parseInt(width, 10);
  143. if (isNaN(width)) {
  144. width = null;
  145. }
  146. }
  147. return width;
  148. }
  149. export function parseMinWidth(minWidth) {
  150. if (typeof minWidth !== 'undefined') {
  151. minWidth = parseWidth(minWidth);
  152. if (isNaN(minWidth)) {
  153. minWidth = 80;
  154. }
  155. }
  156. return minWidth;
  157. };
  158. export function parseHeight(height) {
  159. if (typeof height === 'number') {
  160. return height;
  161. }
  162. if (typeof height === 'string') {
  163. if (/^\d+(?:px)?$/.test(height)) {
  164. return parseInt(height, 10);
  165. } else {
  166. return height;
  167. }
  168. }
  169. return null;
  170. }
  171. // https://github.com/reduxjs/redux/blob/master/src/compose.js
  172. export function compose(...funcs) {
  173. if (funcs.length === 0) {
  174. return arg => arg;
  175. }
  176. if (funcs.length === 1) {
  177. return funcs[0];
  178. }
  179. return funcs.reduce((a, b) => (...args) => a(b(...args)));
  180. }
  181. export function toggleRowStatus(statusArr, row, newVal) {
  182. let changed = false;
  183. const index = statusArr.indexOf(row);
  184. const included = index !== -1;
  185. const addRow = () => {
  186. statusArr.push(row);
  187. changed = true;
  188. };
  189. const removeRow = () => {
  190. statusArr.splice(index, 1);
  191. changed = true;
  192. };
  193. if (typeof newVal === 'boolean') {
  194. if (newVal && !included) {
  195. addRow();
  196. } else if (!newVal && included) {
  197. removeRow();
  198. }
  199. } else {
  200. if (included) {
  201. removeRow();
  202. } else {
  203. addRow();
  204. }
  205. }
  206. return changed;
  207. }
  208. export function walkTreeNode(root, cb, childrenKey = 'children', lazyKey = 'hasChildren') {
  209. const isNil = (array) => !(Array.isArray(array) && array.length);
  210. function _walker(parent, children, level) {
  211. cb(parent, children, level);
  212. children.forEach(item => {
  213. if (item[lazyKey]) {
  214. cb(item, null, level + 1);
  215. return;
  216. }
  217. const children = item[childrenKey];
  218. if (!isNil(children)) {
  219. _walker(item, children, level + 1);
  220. }
  221. });
  222. }
  223. root.forEach(item => {
  224. if (item[lazyKey]) {
  225. cb(item, null, 0);
  226. return;
  227. }
  228. const children = item[childrenKey];
  229. if (!isNil(children)) {
  230. _walker(item, children, 0);
  231. }
  232. });
  233. }
  234. export const objectEquals = function(objectA, objectB) {
  235. // 取对象a和b的属性名
  236. let aProps = Object.getOwnPropertyNames(objectA);
  237. let bProps = Object.getOwnPropertyNames(objectB);
  238. // 判断属性名的length是否一致
  239. if (aProps.length !== bProps.length) {
  240. return false;
  241. }
  242. // 循环取出属性名,再判断属性值是否一致
  243. for (let i = 0; i < aProps.length; i++) {
  244. let propName = aProps[i];
  245. if (objectA[propName] !== objectB[propName]) {
  246. return false;
  247. }
  248. }
  249. return true;
  250. };