UNPKG

dwui

Version:

a part of wrapper on iView UI

241 lines (220 loc) 5.77 kB
/** * 比较两个数组中 返回newArr相对oldArr发生变化的数据 * @param oldArr 变动之前的数组副本 * @param newArr 变动之后的数据 * @return {Array} 返回变动的数据,actionType标记发生的变化 */ export function compareArr (oldArr, newArr) { let changeItems = [] if (oldArr.length === 0) { if (newArr.length > 0) { for (let j = 0; j < newArr.length; j++) { if ((!checkIsEmptyObject(newArr[j], '$actionType'))) { changeItems.push(deepCopy(newArr[j])) } } return changeItems } else { return [] } } newArr.forEach((item) => { if (item.$actionType === 'update') { for (let i = 0; i < oldArr.length; i++) { if (compareObject(item, oldArr[i])) { break } if (i === oldArr.length - 1) { changeItems.push(deepCopy(item)) } } } else { if ((!checkIsEmptyObject(item, '$actionType'))) { changeItems.push(deepCopy(item)) } } }) return changeItems } /** * 根据对象删除数组中的数据的第一个值,返回新数组 * @param arr 数据数组 * @param delObject 需要删除的对象 * @return {Array} 返回 arr 中删除掉 delObject 后的新数组 */ export function delArrObject (arr, delObject) { let newArr = [] let hasDelete = false let deepCopyObj = deepCopy(delObject) if (arr.length === 0) { return newArr } else { arr.forEach((item) => { if (!compareObject(item, deepCopyObj) || hasDelete) { newArr.push(item) } else { hasDelete = true } }) return newArr } } /** * 返回typeOf 类类型 * @param obj * @return {*} */ function typeOf (obj) { const toString = Object.prototype.toString const map = { '[object Boolean]': 'boolean', '[object Number]': 'number', '[object String]': 'string', '[object Function]': 'function', '[object Array]': 'array', '[object Date]': 'date', '[object RegExp]': 'regExp', '[object Undefined]': 'undefined', '[object Null]': 'null', '[object Object]': 'object' } return map[toString.call(obj)] } /** * 深复制一个对象 * @param data * @return {*} */ function deepCopy (data) { const t = typeOf(data) let o if (t === 'array') { o = [] } else if (t === 'object') { o = {} } else { return data } if (t === 'array') { for (let i = 0; i < data.length; i++) { o.push(deepCopy(data[i])) } } else if (t === 'object') { for (let i in data) { o[i] = deepCopy(data[i]) } } return o } export { deepCopy } /** * 比较对象是否相等(无嵌套的属性) * @param x * @param y * @return {boolean} true:相等 false:不相等 * @note: 忽略$actionType,$rowKeyIndex 属性的比较 */ export function compareObject (x, y) { if (x === y) { return true } if (!(x instanceof Object) || !(y instanceof Object)) { return false } for (let p in x) { // Inherited properties were tested using x.constructor === y.constructor if (p === '$actionType' || p === '$rowKeyIndex') { continue } if (x.hasOwnProperty(p)) { // Allows comparing x[ p ] and y[ p ] when set to undefined if (!y.hasOwnProperty(p)) { return false } // If they have the same strict value or identity then they are equal if (x[ p ] === y[ p ]) { continue } // Numbers, Strings, Functions, Booleans must be strictly equal if (typeof (x[ p ]) !== 'object') { return false } if (Array.isArray(x[ p ])) { if (x[p] === y[p]) { continue } else if (x[p].length === y[p].length) { if (x[p].toString() === y[p].toString()) { continue } else { return false } } else { return false } } // Objects and Arrays must be tested recursively if (!Object.equals(x[ p ], y[ p ])) { return false } } } for (let p in y) { // allows x[ p ] to be set to undefined if (y.hasOwnProperty(p) && !x.hasOwnProperty(p)) { return false } } return true } /** * 判断某个对象是否为空 * @param o 需要比较的对象 * @param filterProperty 判断为空过程中需要忽略的属性 * @return {boolean} true:对象为空 false:对象不为空 */ function checkIsEmptyObject (o, filterProperty) { let properties = Object.getOwnPropertyNames(o) if (properties.length === 0) { return true } else { let flag = true properties.forEach((pro) => { if (pro !== filterProperty && pro !== '$rowKeyIndex' && pro !== '__ob__' && pro !== 'state') { if (o[pro]) { flag = false } } }) return flag } } // For Modal scrollBar hidden let cached export function getScrollBarSize () { if (cached === undefined) { const inner = document.createElement('div') inner.style.width = '100%' inner.style.height = '200px' const outer = document.createElement('div') const outerStyle = outer.style outerStyle.position = 'absolute' outerStyle.top = 0 outerStyle.left = 0 outerStyle.pointerEvents = 'none' outerStyle.visibility = 'hidden' outerStyle.width = '200px' outerStyle.height = '150px' outerStyle.overflow = 'hidden' outer.appendChild(inner) document.body.appendChild(outer) const widthContained = inner.offsetWidth outer.style.overflow = 'scroll' let widthScroll = inner.offsetWidth if (widthContained === widthScroll) { widthScroll = outer.clientWidth } document.body.removeChild(outer) cached = widthContained - widthScroll } return cached }