util.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. export const NODE_KEY = '$treeNodeId';
  2. export const markNodeData = function(node, data) {
  3. if (!data || data[NODE_KEY]) return;
  4. Object.defineProperty(data, NODE_KEY, {
  5. value: node.id,
  6. enumerable: false,
  7. configurable: false,
  8. writable: false
  9. });
  10. };
  11. export const getNodeKey = function(key, data) {
  12. if (!key) return data[NODE_KEY];
  13. return data[key];
  14. };
  15. export const objectAssign = function(target) {
  16. for (let i = 1, j = arguments.length; i < j; i++) {
  17. let source = arguments[i] || {};
  18. for (let prop in source) {
  19. if (source.hasOwnProperty(prop)) {
  20. let value = source[prop];
  21. if (value !== undefined) {
  22. target[prop] = value;
  23. }
  24. }
  25. }
  26. }
  27. return target;
  28. };
  29. // TODO: use native Array.find, Array.findIndex when IE support is dropped
  30. export const arrayFindIndex = function(arr, pred) {
  31. for (let i = 0; i !== arr.length; ++i) {
  32. if (pred(arr[i])) {
  33. return i;
  34. }
  35. }
  36. return -1;
  37. };
  38. export const getChildState = function(node) {
  39. let all = true;
  40. let none = true;
  41. let allWithoutDisable = true;
  42. for (let i = 0, j = node.length; i < j; i++) {
  43. const n = node[i];
  44. if (n.checked !== true || n.indeterminate) {
  45. all = false;
  46. if (!n.disabled) {
  47. allWithoutDisable = false;
  48. }
  49. }
  50. if (n.checked !== false || n.indeterminate) {
  51. none = false;
  52. }
  53. }
  54. return {
  55. all,
  56. none,
  57. allWithoutDisable,
  58. half: !all && !none
  59. };
  60. };
  61. export const reInitChecked = function(node) {
  62. if (!node || node.childNodesId.length === 0) return;
  63. let childNodes = node.getChildNodes(node.childNodesId);
  64. const {
  65. all,
  66. none,
  67. half
  68. } = getChildState(childNodes);
  69. if (all) {
  70. node.checked = true;
  71. node.indeterminate = false;
  72. } else if (half) {
  73. node.checked = false;
  74. node.indeterminate = true;
  75. } else if (none) {
  76. node.checked = false;
  77. node.indeterminate = false;
  78. }
  79. let parent = node.getParent(node.parentId);
  80. if (!parent || parent.level === 0) return;
  81. if (!node.store().checkStrictly) {
  82. reInitChecked(parent);
  83. }
  84. };
  85. export const getPropertyFromData = function(node, prop) {
  86. const props = node.store().props;
  87. const data = node.data || {};
  88. const config = props[prop];
  89. if (typeof config === 'function') {
  90. return config(data, node);
  91. } else if (typeof config === 'string') {
  92. return data[config];
  93. } else if (typeof config === 'undefined') {
  94. const dataProp = data[prop];
  95. return dataProp === undefined ? '' : dataProp;
  96. }
  97. };
  98. export const isNull = function(v) {
  99. return v === undefined || v === null || v === '';
  100. }