Browse Source

Pull request #3325: KERNEL-14035 refactor: core/utils

Merge in VISUAL/fineui from ~ZHENFEI.LI/fineui:es6 to es6

* commit '3d958f3c9e642af2e94fdb860286c40f2bfdeb55':
  KERNEL-14035 refactor: core/utils
es6
Zhenfei.Li-李振飞 2 years ago
parent
commit
61799cbba4
  1. 3
      src/core/index.js
  2. 50
      src/core/utils/chinesePY.js
  3. 163
      src/core/utils/color.js
  4. 25
      src/core/utils/events/eventlistener.js
  5. 3
      src/core/utils/events/index.js
  6. 172
      src/core/utils/events/mousemovetracker.js
  7. 228
      src/core/utils/events/wheelhandler.js
  8. 83
      src/core/utils/i18n.js
  9. 4
      src/core/utils/index.js

3
src/core/index.js

@ -15,6 +15,7 @@ import { useInWorker } from "./worker";
import * as constant from "./constant"; import * as constant from "./constant";
import * as logic from "./logic"; import * as logic from "./logic";
import { Element } from "./element"; import { Element } from "./element";
import * as utils from "./utils";
export * from "./decorator"; export * from "./decorator";
export * from "./2.base"; export * from "./2.base";
@ -29,6 +30,7 @@ export * from "./structure";
export * from "./h"; export * from "./h";
export * from "./constant"; export * from "./constant";
export * from "./logic"; export * from "./logic";
export * from "./utils";
export { export {
StyleLoaderManager, StyleLoaderManager,
@ -57,4 +59,5 @@ Object.assign(BI, {
...structure, ...structure,
useInWorker, useInWorker,
...h, ...h,
...utils,
}); });

50
src/core/utils/chinesePY.js

@ -376,41 +376,42 @@ const oMultiDiff = {
40765: "YQ", 40765: "YQ",
40784: "QJ", 40784: "QJ",
40840: "YK", 40840: "YK",
40863: "QJG" 40863: "QJG",
}; };
const _checkPYCh = function (ch) { function _checkPYCh(ch) {
var uni = ch.charCodeAt(0); const uni = ch.charCodeAt(0);
// 如果不在汉字处理范围之内,返回原字符,也可以调用自己的处理函数 // 如果不在汉字处理范围之内,返回原字符,也可以调用自己的处理函数
if (uni > 40869 || uni < 19968) { if (uni > 40869 || uni < 19968) {
return ch; return ch;
} // dealWithOthers(ch); } // dealWithOthers(ch);
return (oMultiDiff[uni] ? oMultiDiff[uni] : (_ChineseFirstPY.charAt(uni - 19968))); return (oMultiDiff[uni] ? oMultiDiff[uni] : (_ChineseFirstPY.charAt(uni - 19968)));
}; }
const _mkPYRslt = function (arr, options) { function _mkPYRslt(arr, options) {
var ignoreMulti = options.ignoreMulti; const ignoreMulti = options.ignoreMulti;
var splitChar = options.splitChar; const splitChar = options.splitChar;
var arrRslt = [""], k, multiLen = 0; let arrRslt = [""], k, multiLen = 0;
for (var i = 0, len = arr.length; i < len; i++) { for (let i = 0, len = arr.length; i < len; i++) {
var str = arr[i]; const str = arr[i];
var strlen = str.length; const strlen = str.length;
// 多音字过多的情况下,指数增长会造成浏览器卡死,超过20完全卡死,18勉强能用,考虑到不同性能最好是16或者14 // 多音字过多的情况下,指数增长会造成浏览器卡死,超过20完全卡死,18勉强能用,考虑到不同性能最好是16或者14
// 超过14个多音字之后,后面的都用第一个拼音 // 超过14个多音字之后,后面的都用第一个拼音
if (strlen == 1 || multiLen > 14 || ignoreMulti) { if (strlen === 1 || multiLen > 14 || ignoreMulti) {
var tmpStr = str.substring(0, 1); const tmpStr = str.substring(0, 1);
for (k = 0; k < arrRslt.length; k++) { for (k = 0; k < arrRslt.length; k++) {
arrRslt[k] += tmpStr; arrRslt[k] += tmpStr;
} }
} else { } else {
var tmpArr = arrRslt.slice(0); const tmpArr = arrRslt.slice(0);
arrRslt = []; arrRslt = [];
multiLen++; multiLen++;
for (k = 0; k < strlen; k++) { for (k = 0; k < strlen; k++) {
// 复制一个相同的arrRslt // 复制一个相同的arrRslt
var tmp = tmpArr.slice(0); const tmp = tmpArr.slice(0);
// 把当前字符str[k]添加到每个元素末尾 // 把当前字符str[k]添加到每个元素末尾
for (var j = 0; j < tmp.length; j++) { for (let j = 0; j < tmp.length; j++) {
tmp[j] += str.charAt(k); tmp[j] += str.charAt(k);
} }
// 把复制并修改后的数组连接到arrRslt上 // 把复制并修改后的数组连接到arrRslt上
@ -418,28 +419,25 @@ const _mkPYRslt = function (arr, options) {
} }
} }
} }
// BI-56386 这边直接将所有多音字组合拼接是有风险的,因为丢失了每一组的起始索引信息, 外部使用indexOf等方法会造成错位 // BI-56386 这边直接将所有多音字组合拼接是有风险的,因为丢失了每一组的起始索引信息, 外部使用indexOf等方法会造成错位
// 一旦错位就可能认为不符合条件, 但实际上还是有可能符合条件的,故此处以一个无法搜索的不可见字符作为连接 // 一旦错位就可能认为不符合条件, 但实际上还是有可能符合条件的,故此处以一个无法搜索的不可见字符作为连接
return arrRslt.join(splitChar || "").toLowerCase(); return arrRslt.join(splitChar || "").toLowerCase();
}; }
export function makeFirstPY(str, options) { export function makeFirstPY(str, options) {
options = options || {}; options = options || {};
if (typeof (str) !== "string") { if (typeof (str) !== "string") {
return "" + str; return `${str}`;
} }
var arrResult = []; // 保存中间结果的数组 const arrResult = []; // 保存中间结果的数组
for (var i = 0, len = str.length; i < len; i++) { for (let i = 0, len = str.length; i < len; i++) {
// 获得unicode码 // 获得unicode码
var ch = str.charAt(i); const ch = str.charAt(i);
// 检查该unicode码是否在处理范围之内,在则返回该码对映汉字的拼音首字母,不在则调用其它函数处理 // 检查该unicode码是否在处理范围之内,在则返回该码对映汉字的拼音首字母,不在则调用其它函数处理
arrResult.push(_checkPYCh(ch)); arrResult.push(_checkPYCh(ch));
} }
// 处理arrResult,返回所有可能的拼音首字母串数组 // 处理arrResult,返回所有可能的拼音首字母串数组
return _mkPYRslt(arrResult, options); return _mkPYRslt(arrResult, options);
} }
Object.assign(BI, {
makeFirstPY
});

163
src/core/utils/color.js

@ -1,190 +1,201 @@
BI.DOM = BI.DOM || {}; import { parseInt, parseFloat, isNull, isKey } from "../2.base";
BI.extend(BI.DOM, {
isColor: function (color) { export const DOM = {
isColor(color) {
return color && (this.isRGBColor(color) || this.isHexColor(color)); return color && (this.isRGBColor(color) || this.isHexColor(color));
}, },
isRGBColor: function (color) { isRGBColor(color) {
if (!color) { if (!color) {
return false; return false;
} }
return color.substr(0, 3) === "rgb"; return color.substr(0, 3) === "rgb";
}, },
isHexColor: function (color) { isHexColor(color) {
if (!color) { if (!color) {
return false; return false;
} }
return color[0] === "#" && color.length === 7; return color[0] === "#" && color.length === 7;
}, },
isDarkColor: function (hex) { isDarkColor(hex) {
if (!hex || !this.isHexColor(hex)) { if (!hex || !this.isHexColor(hex)) {
return false; return false;
} }
var rgb = this.rgb2json(this.hex2rgb(hex)); const rgb = this.rgb2json(this.hex2rgb(hex));
var grayLevel = Math.round(rgb.r * 0.299 + rgb.g * 0.587 + rgb.b * 0.114); const grayLevel = Math.round(rgb.r * 0.299 + rgb.g * 0.587 + rgb.b * 0.114);
if (grayLevel < 192/** 网上给的是140**/) { if (grayLevel < 192/** 网上给的是140**/) {
return true; return true;
} }
return false; return false;
}, },
// 获取对比颜色 // 获取对比颜色
getContrastColor: function (color) { getContrastColor(color) {
if (!color || !this.isColor(color)) { if (!color || !this.isColor(color)) {
return ""; return "";
} }
if (this.isDarkColor(color)) { if (this.isDarkColor(color)) {
return "#FFFFFF"; return "#FFFFFF";
} }
return "#3D4D66"; return "#3D4D66";
}, },
rgb2hex: function (rgbColour) { rgb2hex(rgbColour) {
if (!rgbColour || rgbColour.substr(0, 3) != "rgb") { if (!rgbColour || rgbColour.substr(0, 3) !== "rgb") {
return ""; return "";
} }
var rgbValues = rgbColour.match(/\d+(\.\d+)?/g); const rgbValues = rgbColour.match(/\d+(\.\d+)?/g);
var red = BI.parseInt(rgbValues[0]); const red = parseInt(rgbValues[0]);
var green = BI.parseInt(rgbValues[1]); const green = parseInt(rgbValues[1]);
var blue = BI.parseInt(rgbValues[2]); const blue = parseInt(rgbValues[2]);
var hexColour = "#" + this.int2hex(red) + this.int2hex(green) + this.int2hex(blue); const hexColour = `#${this.int2hex(red)}${this.int2hex(green)}${this.int2hex(blue)}`;
return hexColour; return hexColour;
}, },
_hue2rgb: function (m1, m2, h) { _hue2rgb(m1, m2, h) {
h = (h < 0) ? h + 1 : ((h > 1) ? h - 1 : h); h = (h < 0) ? h + 1 : ((h > 1) ? h - 1 : h);
if (h * 6 < 1) return m1 + (m2 - m1) * h * 6; if (h * 6 < 1) return m1 + (m2 - m1) * h * 6;
if (h * 2 < 1) return m2; if (h * 2 < 1) return m2;
if (h * 3 < 2) return m1 + (m2 - m1) * (0.66666 - h) * 6; if (h * 3 < 2) return m1 + (m2 - m1) * (0.66666 - h) * 6;
return m1; return m1;
}, },
hsl2rgb: function (hsl) { hsl2rgb(hsl) {
var m1, m2, r, g, b; const h = hsl[0], s = hsl[1], l = hsl[2];
var h = hsl[0], s = hsl[1], l = hsl[2]; const m2 = (l <= 0.5) ? l * (s + 1) : l + s - l * s;
m2 = (l <= 0.5) ? l * (s + 1) : l + s - l * s; const m1 = l * 2 - m2;
m1 = l * 2 - m2;
return [this._hue2rgb(m1, m2, h + 0.33333), return [this._hue2rgb(m1, m2, h + 0.33333),
this._hue2rgb(m1, m2, h), this._hue2rgb(m1, m2, h),
this._hue2rgb(m1, m2, h - 0.33333)]; this._hue2rgb(m1, m2, h - 0.33333)];
}, },
rgb2hsl: function (rgb) { rgb2hsl(rgb) {
var min, max, delta, h, s, l; let h, s;
var r = rgb[0], g = rgb[1], b = rgb[2]; const r = rgb[0], g = rgb[1], b = rgb[2];
min = Math.min(r, Math.min(g, b)); const min = Math.min(r, Math.min(g, b));
max = Math.max(r, Math.max(g, b)); const max = Math.max(r, Math.max(g, b));
delta = max - min; const delta = max - min;
l = (min + max) / 2; const l = (min + max) / 2;
s = 0; s = 0;
if (l > 0 && l < 1) { if (l > 0 && l < 1) {
s = delta / (l < 0.5 ? (2 * l) : (2 - 2 * l)); s = delta / (l < 0.5 ? (2 * l) : (2 - 2 * l));
} }
h = 0; h = 0;
if (delta > 0) { if (delta > 0) {
if (max == r && max != g) h += (g - b) / delta; if (max === r && max !== g) h += (g - b) / delta;
if (max == g && max != b) h += (2 + (b - r) / delta); if (max === g && max !== b) h += (2 + (b - r) / delta);
if (max == b && max != r) h += (4 + (r - g) / delta); if (max === b && max !== r) h += (4 + (r - g) / delta);
h /= 6; h /= 6;
} }
return [h, s, l]; return [h, s, l];
}, },
rgb2json: function (rgbColour) { rgb2json(rgbColour) {
if (!rgbColour) { if (!rgbColour) {
return {}; return {};
} }
if (!this.isRGBColor(rgbColour)) { if (!this.isRGBColor(rgbColour)) {
return {}; return {};
} }
var rgbValues = rgbColour.match(/\d+(\.\d+)?/g); const rgbValues = rgbColour.match(/\d+(\.\d+)?/g);
return { return {
r: BI.parseInt(rgbValues[0]), r: parseInt(rgbValues[0]),
g: BI.parseInt(rgbValues[1]), g: parseInt(rgbValues[1]),
b: BI.parseInt(rgbValues[2]) b: parseInt(rgbValues[2]),
}; };
}, },
rgba2json: function (rgbColour) { rgba2json(rgbColour) {
if (!rgbColour) { if (!rgbColour) {
return {}; return {};
} }
var rgbValues = rgbColour.match(/\d+(\.\d+)?/g); const rgbValues = rgbColour.match(/\d+(\.\d+)?/g);
return { return {
r: BI.parseInt(rgbValues[0]), r: parseInt(rgbValues[0]),
g: BI.parseInt(rgbValues[1]), g: parseInt(rgbValues[1]),
b: BI.parseInt(rgbValues[2]), b: parseInt(rgbValues[2]),
a: BI.parseFloat(rgbValues[3]) a: parseFloat(rgbValues[3]),
}; };
}, },
json2rgb: function (rgb) { json2rgb(rgb) {
if (!BI.isKey(rgb.r) || !BI.isKey(rgb.g) || !BI.isKey(rgb.b)) { if (!isKey(rgb.r) || !isKey(rgb.g) || !isKey(rgb.b)) {
return ""; return "";
} }
return "rgb(" + rgb.r + "," + rgb.g + "," + rgb.b + ")";
return `rgb(${rgb.r},${rgb.g},${rgb.b})`;
}, },
json2rgba: function (rgba) { json2rgba(rgba) {
if (!BI.isKey(rgba.r) || !BI.isKey(rgba.g) || !BI.isKey(rgba.b)) { if (!isKey(rgba.r) || !isKey(rgba.g) || !isKey(rgba.b)) {
return ""; return "";
} }
return "rgba(" + rgba.r + "," + rgba.g + "," + rgba.b + "," + rgba.a + ")";
return `rgba(${rgba.r},${rgba.g},${rgba.b},${rgba.a})`;
}, },
int2hex: function (strNum) { int2hex(strNum) {
var hexdig = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"]; const hexdig = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"];
return hexdig[strNum >>> 4] + "" + hexdig[strNum & 15]; return `${hexdig[strNum >>> 4]}${hexdig[strNum & 15]}`;
}, },
hex2rgb: function (color) { hex2rgb(color) {
if (!color) { if (!color) {
return ""; return "";
} }
if (!this.isHexColor(color)) { if (!this.isHexColor(color)) {
return color; return color;
} }
var tempValue = "rgb(", colorArray; let tempValue = "rgb(", colorArray;
if (color.length === 7) { if (color.length === 7) {
colorArray = [BI.parseInt("0x" + color.substring(1, 3)), colorArray = [parseInt(`0x${color.substring(1, 3)}`),
BI.parseInt("0x" + color.substring(3, 5)), parseInt(`0x${color.substring(3, 5)}`),
BI.parseInt("0x" + color.substring(5, 7))]; parseInt(`0x${color.substring(5, 7)}`)];
} else if (color.length === 4) { } else if (color.length === 4) {
colorArray = [BI.parseInt("0x" + color.substring(1, 2)), colorArray = [parseInt(`0x${color.substring(1, 2)}`),
BI.parseInt("0x" + color.substring(2, 3)), parseInt(`0x${color.substring(2, 3)}`),
BI.parseInt("0x" + color.substring(3, 4))]; parseInt(`0x${color.substring(3, 4)}`)];
} }
tempValue += colorArray[0] + ","; tempValue += `${colorArray[0]},`;
tempValue += colorArray[1] + ","; tempValue += `${colorArray[1]},`;
tempValue += colorArray[2] + ")"; tempValue += `${colorArray[2]})`;
return tempValue; return tempValue;
}, },
rgba2rgb: function (rgbColor, bgColor) { rgba2rgb(rgbColor, bgColor) {
if (BI.isNull(bgColor)) { if (isNull(bgColor)) {
bgColor = 1; bgColor = 1;
} }
if (rgbColor.substr(0, 4) != "rgba") { if (rgbColor.substr(0, 4) !== "rgba") {
return ""; return "";
} }
var rgbValues = rgbColor.match(/\d+(\.\d+)?/g); const rgbValues = rgbColor.match(/\d+(\.\d+)?/g);
if (rgbValues.length < 4) { if (rgbValues.length < 4) {
return ""; return "";
} }
var R = BI.parseFloat(rgbValues[0]); const R = parseFloat(rgbValues[0]);
var G = BI.parseFloat(rgbValues[1]); const G = parseFloat(rgbValues[1]);
var B = BI.parseFloat(rgbValues[2]); const B = parseFloat(rgbValues[2]);
var A = BI.parseFloat(rgbValues[3]); const A = parseFloat(rgbValues[3]);
return "rgb(" + Math.floor(255 * (bgColor * (1 - A)) + R * A) + "," + return `rgb(${Math.floor(255 * (bgColor * (1 - A)) + R * A)},${
Math.floor(255 * (bgColor * (1 - A)) + G * A) + "," + Math.floor(255 * (bgColor * (1 - A)) + G * A)},${
Math.floor(255 * (bgColor * (1 - A)) + B * A) + ")"; Math.floor(255 * (bgColor * (1 - A)) + B * A)})`;
} },
}); };

25
src/core/utils/events/eventlistener.js

@ -1,18 +1,22 @@
BI.EventListener = { import { emptyFn } from "../../constant";
export const EventListener = {
listen: function listen (target, eventType, callback) { listen: function listen (target, eventType, callback) {
if (target.addEventListener) { if (target.addEventListener) {
target.addEventListener(eventType, callback, false); target.addEventListener(eventType, callback, false);
return { return {
remove: function remove () { remove: function remove () {
target.removeEventListener(eventType, callback, false); target.removeEventListener(eventType, callback, false);
} },
}; };
} else if (target.attachEvent) { } else if (target.attachEvent) {
target.attachEvent("on" + eventType, callback); target.attachEvent(`on${eventType}`, callback);
return { return {
remove: function remove () { remove: function remove () {
target.detachEvent("on" + eventType, callback); target.detachEvent(`on${eventType}`, callback);
} },
}; };
} }
}, },
@ -20,18 +24,19 @@ BI.EventListener = {
capture: function capture (target, eventType, callback) { capture: function capture (target, eventType, callback) {
if (target.addEventListener) { if (target.addEventListener) {
target.addEventListener(eventType, callback, true); target.addEventListener(eventType, callback, true);
return { return {
remove: function remove () { remove: function remove () {
target.removeEventListener(eventType, callback, true); target.removeEventListener(eventType, callback, true);
} },
}; };
} }
return { return {
remove: BI.emptyFn remove: emptyFn,
}; };
}, },
registerDefault: function registerDefault () { registerDefault: function registerDefault () {
} },
}; };

3
src/core/utils/events/index.js

@ -0,0 +1,3 @@
export { EventListener } from "./eventlistener";
export { MouseMoveTracker } from "./mousemovetracker";
export { WheelHandler } from "./wheelhandler";

172
src/core/utils/events/mousemovetracker.js

@ -1,107 +1,107 @@
!(function () { import { EventListener } from "./eventlistener";
var cancelAnimationFrame = import { bind } from "../../2.base";
_global.cancelAnimationFrame ||
_global.webkitCancelAnimationFrame ||
_global.mozCancelAnimationFrame ||
_global.oCancelAnimationFrame ||
_global.msCancelAnimationFrame ||
_global.clearTimeout;
var requestAnimationFrame = _global.requestAnimationFrame || _global.webkitRequestAnimationFrame || _global.mozRequestAnimationFrame || _global.oRequestAnimationFrame || _global.msRequestAnimationFrame || _global.setTimeout; const cancelAnimationFrame =
_global.cancelAnimationFrame ||
_global.webkitCancelAnimationFrame ||
_global.mozCancelAnimationFrame ||
_global.oCancelAnimationFrame ||
_global.msCancelAnimationFrame ||
_global.clearTimeout;
const requestAnimationFrame = _global.requestAnimationFrame || _global.webkitRequestAnimationFrame || _global.mozRequestAnimationFrame || _global.oRequestAnimationFrame || _global.msRequestAnimationFrame || _global.setTimeout;
BI.MouseMoveTracker = function (onMove, onMoveEnd, domNode) { export class MouseMoveTracker {
constructor(onMove, onMoveEnd, domNode) {
this._isDragging = false; this._isDragging = false;
this._animationFrameID = null; this._animationFrameID = null;
this._domNode = domNode; this._domNode = domNode;
this._onMove = onMove; this._onMove = onMove;
this._onMoveEnd = onMoveEnd; this._onMoveEnd = onMoveEnd;
this._onMouseMove = bind(this._onMouseMove, this);
this._onMouseUp = bind(this._onMouseUp, this);
this._didMouseMove = bind(this._didMouseMove, this);
}
this._onMouseMove = BI.bind(this._onMouseMove, this); captureMouseMoves(event) {
this._onMouseUp = BI.bind(this._onMouseUp, this); if (!this._eventMoveToken && !this._eventUpToken) {
this._didMouseMove = BI.bind(this._didMouseMove, this); this._eventMoveToken = EventListener.listen(
}; this._domNode,
BI.MouseMoveTracker.prototype = { "mousemove",
constructor: BI.MouseMoveTracker, this._onMouseMove
captureMouseMoves: function (/* object*/ event) { );
if (!this._eventMoveToken && !this._eventUpToken) { this._eventUpToken = EventListener.listen(
this._eventMoveToken = BI.EventListener.listen( this._domNode,
this._domNode, "mouseup",
"mousemove", this._onMouseUp
this._onMouseMove );
); }
this._eventUpToken = BI.EventListener.listen(
this._domNode,
"mouseup",
this._onMouseUp
);
}
if (!this._isDragging) { if (!this._isDragging) {
this._deltaX = 0; this._deltaX = 0;
this._deltaY = 0; this._deltaY = 0;
this._isDragging = true; this._isDragging = true;
this._x = event.clientX; this._x = event.clientX;
this._y = event.clientY; this._y = event.clientY;
} }
// event.preventDefault ? event.preventDefault() : (event.returnValue = false); // event.preventDefault ? event.preventDefault() : (event.returnValue = false);
}, }
releaseMouseMoves: function () { releaseMouseMoves() {
if (this._eventMoveToken && this._eventUpToken) { if (this._eventMoveToken && this._eventUpToken) {
this._eventMoveToken.remove(); this._eventMoveToken.remove();
this._eventMoveToken = null; this._eventMoveToken = null;
this._eventUpToken.remove(); this._eventUpToken.remove();
this._eventUpToken = null; this._eventUpToken = null;
} }
if (this._animationFrameID !== null) { if (this._animationFrameID !== null) {
cancelAnimationFrame(this._animationFrameID); cancelAnimationFrame(this._animationFrameID);
this._animationFrameID = null; this._animationFrameID = null;
} }
if (this._isDragging) { if (this._isDragging) {
this._isDragging = false; this._isDragging = false;
this._x = null; this._x = null;
this._y = null; this._y = null;
} }
}, }
isDragging: function () /* boolean*/ { isDragging() /* boolean*/ {
return this._isDragging; return this._isDragging;
}, }
_onMouseMove: function (/* object*/ event) { _onMouseMove(/* object*/ event) {
var x = event.clientX; const x = event.clientX;
var y = event.clientY; const y = event.clientY;
this._deltaX += (x - this._x); this._deltaX += (x - this._x);
this._deltaY += (y - this._y); this._deltaY += (y - this._y);
if (this._animationFrameID === null) { if (this._animationFrameID === null) {
// The mouse may move faster then the animation frame does. // The mouse may move faster then the animation frame does.
// Use `requestAnimationFrame` to avoid over-updating. // Use `requestAnimationFrame` to avoid over-updating.
this._animationFrameID = this._animationFrameID =
requestAnimationFrame(this._didMouseMove); requestAnimationFrame(this._didMouseMove);
} }
this._x = x; this._x = x;
this._y = y; this._y = y;
event.preventDefault ? event.preventDefault() : (event.returnValue = false); event.preventDefault ? event.preventDefault() : (event.returnValue = false);
}, }
_didMouseMove: function () { _didMouseMove() {
this._animationFrameID = null; this._animationFrameID = null;
this._onMove(this._deltaX, this._deltaY); this._onMove(this._deltaX, this._deltaY);
this._deltaX = 0; this._deltaX = 0;
this._deltaY = 0; this._deltaY = 0;
}, }
_onMouseUp: function () { _onMouseUp() {
if (this._animationFrameID) { if (this._animationFrameID) {
this._didMouseMove(); this._didMouseMove();
}
this._onMoveEnd();
} }
}; this._onMoveEnd();
})(); }
}

228
src/core/utils/events/wheelhandler.js

@ -1,149 +1,131 @@
!(function () { import { bind } from "../../2.base";
var PIXEL_STEP = 10;
var LINE_HEIGHT = 40; const PIXEL_STEP = 10;
var PAGE_HEIGHT = 800; const LINE_HEIGHT = 40;
var requestAnimationFrame = _global.requestAnimationFrame || _global.webkitRequestAnimationFrame || _global.mozRequestAnimationFrame || _global.oRequestAnimationFrame || _global.msRequestAnimationFrame || _global.setTimeout; const PAGE_HEIGHT = 800;
const requestAnimationFrame = _global.requestAnimationFrame || _global.webkitRequestAnimationFrame || _global.mozRequestAnimationFrame || _global.oRequestAnimationFrame || _global.msRequestAnimationFrame || _global.setTimeout;
function normalizeWheel (/* object*/event) /* object*/ {
var sX = 0, function normalizeWheel (/* object*/event) /* object*/ {
sY = 0, let sX = 0,
// spinX, spinY sY = 0,
pX = 0, // spinX, spinY
pY = 0; // pixelX, pixelY pX = 0,
pY = 0; // pixelX, pixelY
// Legacy
if ("detail" in event) { // Legacy
sY = event.detail; if ("detail" in event) {
} sY = event.detail;
if ("wheelDelta" in event) { }
sY = -event.wheelDelta / 120; if ("wheelDelta" in event) {
} sY = -event.wheelDelta / 120;
if ("wheelDeltaY" in event) { }
sY = -event.wheelDeltaY / 120; if ("wheelDeltaY" in event) {
} sY = -event.wheelDeltaY / 120;
if ("wheelDeltaX" in event) { }
sX = -event.wheelDeltaX / 120; if ("wheelDeltaX" in event) {
} sX = -event.wheelDeltaX / 120;
}
// side scrolling on FF with DOMMouseScroll
if ("axis" in event && event.axis === event.HORIZONTAL_AXIS) {
sX = sY;
sY = 0;
}
pX = sX * PIXEL_STEP; // side scrolling on FF with DOMMouseScroll
pY = sY * PIXEL_STEP; if ("axis" in event && event.axis === event.HORIZONTAL_AXIS) {
sX = sY;
sY = 0;
}
if ("deltaY" in event) { pX = sX * PIXEL_STEP;
pY = event.deltaY; pY = sY * PIXEL_STEP;
}
if ("deltaX" in event) {
pX = event.deltaX;
}
if ((pX || pY) && event.deltaMode) { if ("deltaY" in event) {
if (event.deltaMode === 1) { pY = event.deltaY;
// delta in LINE units }
pX *= LINE_HEIGHT; if ("deltaX" in event) {
pY *= LINE_HEIGHT; pX = event.deltaX;
} else { }
// delta in PAGE units
pX *= PAGE_HEIGHT;
pY *= PAGE_HEIGHT;
}
}
// Fall-back if spin cannot be determined if ((pX || pY) && event.deltaMode) {
if (pX && !sX) { if (event.deltaMode === 1) {
sX = pX < 1 ? -1 : 1; // delta in LINE units
} pX *= LINE_HEIGHT;
if (pY && !sY) { pY *= LINE_HEIGHT;
sY = pY < 1 ? -1 : 1; } else {
// delta in PAGE units
pX *= PAGE_HEIGHT;
pY *= PAGE_HEIGHT;
} }
}
return { // Fall-back if spin cannot be determined
spinX: sX, if (pX && !sX) {
spinY: sY, sX = pX < 1 ? -1 : 1;
pixelX: pX,
pixelY: pY
};
} }
if (pY && !sY) {
sY = pY < 1 ? -1 : 1;
}
return {
spinX: sX,
spinY: sY,
pixelX: pX,
pixelY: pY,
};
}
BI.WheelHandler = function (onWheel, handleScrollX, handleScrollY, stopPropagation) { export class WheelHandler {
constructor(onWheel, handleScrollX, handleScrollY, stopPropagation) {
this._animationFrameID = null; this._animationFrameID = null;
this._deltaX = 0; this._deltaX = 0;
this._deltaY = 0; this._deltaY = 0;
this._didWheel = BI.bind(this._didWheel, this); this._didWheel = bind(this._didWheel, this);
if (typeof handleScrollX !== "function") { if (typeof handleScrollX !== "function") {
handleScrollX = handleScrollX ? handleScrollX = handleScrollX ? () => true : () => false;
function () {
return true;
} :
function () {
return false;
};
} }
if (typeof handleScrollY !== "function") { if (typeof handleScrollY !== "function") {
handleScrollY = handleScrollY ? handleScrollY = handleScrollY ? () => true : () => false;
function () {
return true;
} :
function () {
return false;
};
} }
if (typeof stopPropagation !== "function") { if (typeof stopPropagation !== "function") {
stopPropagation = stopPropagation ? stopPropagation = stopPropagation ? () => true : () => false;
function () {
return true;
} :
function () {
return false;
};
} }
this._handleScrollX = handleScrollX; this._handleScrollX = handleScrollX;
this._handleScrollY = handleScrollY; this._handleScrollY = handleScrollY;
this._stopPropagation = stopPropagation; this._stopPropagation = stopPropagation;
this._onWheelCallback = onWheel; this._onWheelCallback = onWheel;
this.onWheel = BI.bind(this.onWheel, this); this.onWheel = bind(this.onWheel, this);
}; }
BI.WheelHandler.prototype = {
constructor: BI.WheelHandler, onWheel(event) {
onWheel: function (/* object*/ event) { const normalizedEvent = normalizeWheel(event);
var normalizedEvent = normalizeWheel(event); const deltaX = this._deltaX + normalizedEvent.pixelX;
var deltaX = this._deltaX + normalizedEvent.pixelX; const deltaY = this._deltaY + normalizedEvent.pixelY;
var deltaY = this._deltaY + normalizedEvent.pixelY; const handleScrollX = this._handleScrollX(deltaX, deltaY);
var handleScrollX = this._handleScrollX(deltaX, deltaY); const handleScrollY = this._handleScrollY(deltaY, deltaX);
var handleScrollY = this._handleScrollY(deltaY, deltaX); if (!handleScrollX && !handleScrollY) {
if (!handleScrollX && !handleScrollY) { return;
return; }
}
this._deltaX += handleScrollX ? normalizedEvent.pixelX : 0;
this._deltaY += handleScrollY ? normalizedEvent.pixelY : 0;
event.preventDefault ? event.preventDefault() : (event.returnValue = false);
var changed; this._deltaX += handleScrollX ? normalizedEvent.pixelX : 0;
if (this._deltaX !== 0 || this._deltaY !== 0) { this._deltaY += handleScrollY ? normalizedEvent.pixelY : 0;
if (this._stopPropagation()) { event.preventDefault ? event.preventDefault() : (event.returnValue = false);
event.stopPropagation ? event.stopPropagation() : (event.cancelBubble = true);
}
changed = true;
}
if (changed === true && this._animationFrameID === null) { let changed;
this._animationFrameID = requestAnimationFrame(this._didWheel); if (this._deltaX !== 0 || this._deltaY !== 0) {
if (this._stopPropagation()) {
event.stopPropagation ? event.stopPropagation() : (event.cancelBubble = true);
} }
}, changed = true;
}
_didWheel: function () { if (changed === true && this._animationFrameID === null) {
this._animationFrameID = null; this._animationFrameID = requestAnimationFrame(this._didWheel);
this._onWheelCallback(this._deltaX, this._deltaY);
this._deltaX = 0;
this._deltaY = 0;
} }
}; }
})();
_didWheel() {
this._animationFrameID = null;
this._onWheelCallback(this._deltaX, this._deltaY);
this._deltaX = 0;
this._deltaY = 0;
}
}

83
src/core/utils/i18n.js

@ -1,51 +1,50 @@
!(function () { import { extend } from "../2.base";
var i18nStore = {}; import { replaceAll } from "../func";
var i18nFormatters = {}; let i18nStore = {};
const i18nFormatters = {};
BI._.extend(BI, { export function changeI18n(i18n) {
changeI18n: function (i18n) { if (i18n) {
if (i18n) { i18nStore = i18n;
i18nStore = i18n; }
} }
},
addI18n: function (i18n) {
BI.extend(i18nStore, i18n);
},
i18nText: function (key) { export function addI18n(i18n) {
var localeText = i18nStore[key] || (BI.i18n && BI.i18n[key]) || ""; extend(i18nStore, i18n);
if (!localeText) { }
localeText = key;
} export function i18nText(key) {
var len = arguments.length; let localeText = i18nStore[key] || (BI.i18n && BI.i18n[key]) || "";
if (len > 1) { if (!localeText) {
if (localeText.indexOf("{R1") > -1) { localeText = key;
for (var i = 1; i < len; i++) { }
var reg = new RegExp(`{R${i},(.*?)}`, "g"); const len = arguments.length;
if (len > 1) {
if (localeText.indexOf("{R1") > -1) {
for (let i = 1; i < len; i++) {
const reg = new RegExp(`{R${i},(.*?)}`, "g");
var result = reg.exec(localeText); const result = reg.exec(localeText);
if (result) { if (result) {
var formatName = result[1]; const formatName = result[1];
localeText = BI.replaceAll(localeText, reg, i18nFormatters[formatName](key, arguments[i])); localeText = replaceAll(localeText, reg, i18nFormatters[formatName](key, arguments[i]));
} else {
localeText = BI.replaceAll(localeText, `{R${i}}`, arguments[i] + "");
}
}
} else { } else {
var args = Array.prototype.slice.call(arguments); localeText = replaceAll(localeText, `{R${i}}`, `${arguments[i]}`);
var count = 1;
return BI.replaceAll(localeText, "\\{\\s*\\}", function () {
return args[count++] + "";
});
} }
} }
return localeText; } else {
}, const args = Array.prototype.slice.call(arguments);
let count = 1;
addI18nFormatter: function (formatName, fn) {
i18nFormatters[formatName] = fn; return replaceAll(localeText, "\\{\\s*\\}", () => `${args[count++]}`);
} }
}); }
})();
return localeText;
}
export function addI18nFormatter(formatName, fn) {
i18nFormatters[formatName] = fn;
}

4
src/core/utils/index.js

@ -0,0 +1,4 @@
export * from "./events";
export * from "./i18n";
export { makeFirstPY } from "./chinesePY";
export { DOM } from "./color";
Loading…
Cancel
Save