[improvement] sort out utils (#2672)

This commit is contained in:
neverland
2019-02-02 10:41:01 +08:00
committed by GitHub
parent 0eb06b10a8
commit f0056297c6
9 changed files with 93 additions and 90 deletions
+9 -21
View File
@@ -1,19 +1,19 @@
import Vue from 'vue';
import use from './use';
import { useSlots } from './slots';
const isServer = Vue.prototype.$isServer;
export { use, useSlots } from './use';
function isDef(value) {
export const isServer = Vue.prototype.$isServer;
export function isDef(value) {
return value !== undefined && value !== null;
}
function isObj(x) {
export function isObj(x) {
const type = typeof x;
return x !== null && (type === 'object' || type === 'function');
}
function get(object, path) {
export function get(object, path) {
const keys = path.split('.');
let result = object;
@@ -25,27 +25,15 @@ function get(object, path) {
}
const camelizeRE = /-(\w)/g;
function camelize(str) {
export function camelize(str) {
return str.replace(camelizeRE, (_, c) => c.toUpperCase());
}
function isAndroid() {
export function isAndroid() {
/* istanbul ignore next */
return isServer ? false : /android/.test(navigator.userAgent.toLowerCase());
}
function range(num, min, max) {
export function range(num, min, max) {
return Math.min(Math.max(num, min), max);
}
export {
use,
get,
range,
isObj,
isDef,
isServer,
useSlots,
camelize,
isAndroid
};
+34 -29
View File
@@ -1,36 +1,41 @@
import { isServer } from '.';
export default {
// get nearest scroll element
getScrollEventTarget(element, rootParent = window) {
let node = element;
// bugfix, see http://w3help.org/zh-cn/causes/SD9013 and http://stackoverflow.com/questions/17016740/onscroll-function-is-not-working-for-chrome
while (node && node.tagName !== 'HTML' && node.tagName !== 'BODY' && node.nodeType === 1 && node !== rootParent) {
const { overflowY } = this.getComputedStyle(node);
if (overflowY === 'scroll' || overflowY === 'auto') {
return node;
}
node = node.parentNode;
export const getComputedStyle = !isServer && document.defaultView.getComputedStyle.bind(document.defaultView);
// get nearest scroll element
// http://w3help.org/zh-cn/causes/SD9013
// http://stackoverflow.com/questions/17016740/onscroll-function-is-not-working-for-chrome
export function getScrollEventTarget(element, rootParent = window) {
let node = element;
while (
node &&
node.tagName !== 'HTML' &&
node.tagName !== 'BODY' &&
node.nodeType === 1 &&
node !== rootParent
) {
const { overflowY } = getComputedStyle(node);
if (overflowY === 'scroll' || overflowY === 'auto') {
return node;
}
return rootParent;
},
node = node.parentNode;
}
return rootParent;
}
getScrollTop(element) {
return 'scrollTop' in element ? element.scrollTop : element.pageYOffset;
},
export function getScrollTop(element) {
return 'scrollTop' in element ? element.scrollTop : element.pageYOffset;
}
setScrollTop(element, value) {
'scrollTop' in element ? element.scrollTop = value : element.scrollTo(element.scrollX, value);
},
export function setScrollTop(element, value) {
'scrollTop' in element ? (element.scrollTop = value) : element.scrollTo(element.scrollX, value);
}
// get distance from element top to page top
getElementTop(element) {
return (element === window ? 0 : element.getBoundingClientRect().top) + this.getScrollTop(window);
},
// get distance from element top to page top
export function getElementTop(element) {
return (element === window ? 0 : element.getBoundingClientRect().top) + getScrollTop(window);
}
getVisibleHeight(element) {
return element === window ? element.innerHeight : element.getBoundingClientRect().height;
},
getComputedStyle: !isServer && document.defaultView.getComputedStyle.bind(document.defaultView)
};
export function getVisibleHeight(element) {
return element === window ? element.innerHeight : element.getBoundingClientRect().height;
}
-8
View File
@@ -1,8 +0,0 @@
export function useSlots({ $slots, $scopedSlots }) {
return (name, props) => {
if ($scopedSlots[name]) {
return $scopedSlots[name](props);
}
return $slots[name];
};
}
+10 -1
View File
@@ -2,7 +2,16 @@ import useBem from './bem';
import useSfc from './sfc';
import useI18n from './i18n';
export default function (name) {
export function use(name) {
name = 'van-' + name;
return [useSfc(name), useBem(name), useI18n(name)];
}
export function useSlots({ $slots, $scopedSlots }) {
return (name, props) => {
if ($scopedSlots[name]) {
return $scopedSlots[name](props);
}
return $slots[name];
};
}