all files / src/ util.js

91.95% Statements 137/149
80.95% Branches 68/84
96.43% Functions 27/28
92.47% Lines 135/146
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309                                                    76× 76× 76× 76×         76×         76×                                                                           921× 1439×                     74× 74× 74× 195×   194×   193×   74×         72×     21× 12×     12×     12×         10× 10×   10×                                   10× 10× 10× 34× 34×   32×     10× 36× 28×     10×             71×   71× 71× 68× 79×       71×     378×     378× 377×                                                                              
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
 
var _uxcoreFormatter = require('uxcore-formatter');
 
var _uxcoreFormatter2 = _interopRequireDefault(_uxcoreFormatter);
 
var _cloneDeep = require('lodash/cloneDeep');
 
var _cloneDeep2 = _interopRequireDefault(_cloneDeep);
 
var _cssAnimation = require('css-animation');
 
var _cssAnimation2 = _interopRequireDefault(_cssAnimation);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
 
var scrollbarWidth = void 0;
 
// Measure scrollbar width for padding body during modal show/hide
var scrollbarMeasure = {
  position: 'absolute',
  top: '-9999px',
  width: '50px',
  height: '50px',
  overflow: 'scroll'
};
 
/**
 * Get IE version.
 * @return {number} the IE version, 0 if the browser is not IE.
 */
var getIEVer = function getIEVer() {
  Eif (window) {
    var ua = window.navigator.userAgent;
    var idx = ua.indexOf('MSIE');
    Iif (idx > 0) {
      // "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64;
      // Trident/6.0; SLCC2; .NET CLR 2.0.50727)"
      return parseInt(ua.substring(idx + 5, ua.indexOf('.', idx)), 10);
    }
    Iif (ua.match(/Trident\/7\./)) {
      // "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; SLCC2;
      // .NET CLR 2.0.50727; rv:11.0) like Gecko"
      return 11;
    }
    return 0;
  }
  return 0;
};
 
/**
 * Add or Remove an item from an array if the item does/does not exist.
 * @param {any} item the item need to add/remove.
 * @param {array} arr the array to change.
 * @return {array} the changed array.
 */
var toggleItemInArr = function toggleItemInArr(item, arr) {
  var idx = arr.indexOf(item);
  Iif (idx !== -1) {
    arr.splice(idx, 1);
  } else {
    arr.push(item);
  }
  return arr;
};
 
/**
 * format a string using uxcore-formatter.
 * @param {string} value the string to be formatted
 * @param {string} type the format type
 * @param {string} delimiter the format delimiter
 * @return {string} the formatted string
 */
 
var formatValue = function formatValue(value, type, delimiter) {
  var newDelimiter = delimiter || ' ';
  Iif (value === null || value === undefined) {
    return value;
  }
  var newValue = value.toString();
  if (type === 'money') {
    return _uxcoreFormatter2['default'].money(newValue, newDelimiter);
  } else if (type === 'card') {
    return _uxcoreFormatter2['default'].card(newValue, newDelimiter);
  } else Eif (type === 'cnmobile') {
    return _uxcoreFormatter2['default'].cnmobile(newValue, newDelimiter);
  }
  return newValue;
};
 
var arrayConcat = function arrayConcat(oldArr, newArr, reverse) {
  var resArr = reverse ? newArr.concat(oldArr) : oldArr.concat(newArr);
  return resArr;
};
 
var mergeData = function mergeData(data, obj, reverse) {
  var newData = (0, _cloneDeep2['default'])(data);
  // code compatible
  Iif (newData.datas) {
    newData.datas = arrayConcat(newData.datas, obj, reverse);
  } else Eif (newData.data) {
    newData.data = arrayConcat(newData.data, obj, reverse);
  }
  newData.totalCount += 1;
  return newData;
};
 
/* eslint-disable no-param-reassign */
var saveRef = function saveRef(refName, context) {
  return function (c) {
    context[refName] = c;
  };
};
/* eslint-enable no-param-reassign */
 
// For changeTreeSelected in Table.js
// will change the first param data, be cautious.
var changeValueR = function changeValueR(data, key, value) {
  if (data.data && data.data instanceof Array) {
    for (var i = 0; i < data.data.length; i++) {
      var item = data.data[i];
      item[key] = value;
      changeValueR(item, key, value);
    }
  }
};
 
var hasFixColumn = function hasFixColumn(props) {
  var hasLeft = false;
  var hasRight = false;
  var columns = props.jsxcolumns.filter(function (item) {
    if (item.fixed) {
      hasLeft = true;
      return true;
    }
    if (item.rightFixed) {
      hasRight = true;
      return true;
    }
    return false;
  });
  if (columns.length > 0) {
    return {
      hasLeft: hasLeft,
      hasRight: hasRight
    };
  }
  return false;
};
 
var isRowHalfChecked = function isRowHalfChecked(rowData, checkboxColumnKey) {
  if (rowData.data) {
    var isHalfChecked = rowData.data.some(function (item) {
      Iif (item[checkboxColumnKey]) {
        return true;
      }
      return isRowHalfChecked(item, checkboxColumnKey);
    });
    return isHalfChecked;
  }
  return false;
};
 
// depth-first recursion of multi branches tree
var getAllSelectedRows = function getAllSelectedRows(rowData, checkboxColumnKey) {
  var selectedRows = [];
  var stack = [];
  // put first level data into stack
  stack.push(rowData);
  while (stack.length) {
    var item = stack.shift();
    if (item[checkboxColumnKey]) {
      selectedRows.push(item);
    }
    if (item.data) {
      stack = item.data.concat(stack);
    }
  }
  return selectedRows;
};
 
// TODO cache row tree set
// const getRowTreeSet = (rowData) => {
//   let stack = [];
//   // put first level data into stack
//   stack.push(rowData);
//   while (stack.length) {
//     const item = stack.shift();
//     if (item.data) {
//       stack = stack.concat(item.data);
//     }
//   }
// };
 
var getSelectedKeys = function getSelectedKeys(columns) {
  var realColumns = [];
  var selectedKeys = [];
  columns.forEach(function (item) {
    var isGroup = {}.hasOwnProperty.call(item, 'columns') && _typeof(item.columns) === 'object';
    if (isGroup) {
      realColumns = realColumns.concat(item.columns);
    } else {
      realColumns.push(item);
    }
  });
  realColumns.forEach(function (item) {
    if (!item.hidden) {
      selectedKeys.push(item.dataKey);
    }
  });
  return selectedKeys;
};
 
var getConsts = function getConsts() {
  return {
    commonGroup: '__common__'
  };
};
 
var getDefaultExpandedKeys = function getDefaultExpandedKeys(data, levels) {
  var level = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
 
  var expandedKeys = [];
  if (Array.isArray(data)) {
    data.forEach(function (item) {
      if (level <= levels) {
        expandedKeys.push(item.jsxid);
        expandedKeys = expandedKeys.concat(getDefaultExpandedKeys(item.data, levels, level + 1));
      }
    });
  }
  return expandedKeys;
};
 
var measureScrollbar = function measureScrollbar() {
  Iif (typeof document === 'undefined' || typeof window === 'undefined') {
    return 0;
  }
  if (scrollbarWidth) {
    return scrollbarWidth;
  }
  var scrollDiv = document.createElement('div');
  Object.keys(scrollbarMeasure).forEach(function (scrollProp) {
    Eif (Object.hasOwnProperty.call(scrollbarMeasure, scrollProp)) {
      scrollDiv.style[scrollProp] = scrollbarMeasure[scrollProp];
    }
  });
  document.body.appendChild(scrollDiv);
  var width = scrollDiv.offsetWidth - scrollDiv.clientWidth;
  document.body.removeChild(scrollDiv);
  scrollbarWidth = width;
  return scrollbarWidth;
};
 
/* eslint-disable no-param-reassign */
 
var toggleHeightAnim = function toggleHeightAnim(node, show, done) {
  var height = void 0;
  (0, _cssAnimation2['default'])(node, '__css-animation__', {
    start: function start() {
      node.style.overflow = 'hidden';
      Iif (!show) {
        node.style.height = node.offsetHeight + 'px';
        node.style.opacity = 0;
      } else {
        height = node.offsetHeight;
        node.style.height = 0;
        node.style.opacity = 1;
      }
    },
    active: function active() {
      node.style.height = (show ? height : 0) + 'px';
    },
    end: function end() {
      node.style.height = '';
      node.style.overflow = '';
      done();
    }
  });
};
 
/* eslint-enable no-param-reassign */
 
var utils = {
  getIEVer: getIEVer,
  toggleItemInArr: toggleItemInArr,
  formatValue: formatValue,
  getSelectedKeys: getSelectedKeys,
  changeValueR: changeValueR,
  isRowHalfChecked: isRowHalfChecked,
  getAllSelectedRows: getAllSelectedRows,
  getConsts: getConsts,
  saveRef: saveRef,
  mergeData: mergeData,
  hasFixColumn: hasFixColumn,
  getDefaultExpandedKeys: getDefaultExpandedKeys,
  measureScrollbar: measureScrollbar,
  toggleHeightAnim: toggleHeightAnim
};
 
exports['default'] = utils;
module.exports = exports['default'];