lesca-click
Advanced tools
Comparing version 4.0.0 to 4.0.1
239
lib/index.js
@@ -6,9 +6,240 @@ "use strict"; | ||
}); | ||
exports["default"] = void 0; | ||
exports.setPreventDefault = exports.remove = exports.install = exports["default"] = exports.dataset = exports.clear = exports.addPreventExcept = exports.add = void 0; | ||
var _click = _interopRequireDefault(require("./click")); | ||
var _constants = require("./constants"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } | ||
var _default = _click["default"]; | ||
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } | ||
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
/** | ||
* event dataset | ||
*/ | ||
var dataset = {}; | ||
exports.dataset = dataset; | ||
var mousePropertyDown = { | ||
x: 0, | ||
y: 0 | ||
}; | ||
var mousePropertyMove = { | ||
x: mousePropertyDown.x, | ||
y: mousePropertyDown.y | ||
}; | ||
var extraEvent = { | ||
down: function down() {}, | ||
move: function move() {}, | ||
up: function up() {} | ||
}; | ||
var state = { | ||
device: false, | ||
isPress: false, | ||
deviation: 30, | ||
preventDefault: true | ||
}; | ||
var eventProperty = { | ||
passive: false, | ||
capture: false | ||
}; | ||
var moveOffsetProperty = { | ||
x: 0, | ||
y: 0 | ||
}; | ||
var exceptParentClassIDDataset = []; | ||
var checkDataset = function checkDataset(e) { | ||
var target = e.target; | ||
["".concat(target.id, "_id"), "".concat(target.className, "_class")].forEach(function (name) { | ||
var _dataset$name, _dataset; | ||
(_dataset$name = (_dataset = dataset)[name]) === null || _dataset$name === void 0 ? void 0 : _dataset$name.call(_dataset, e); | ||
}); | ||
}; | ||
var areWePreventDefault = function areWePreventDefault(e) { | ||
var preventDefault = state.preventDefault; | ||
var hasClassID = (0, _constants.CHECK_PARENT_HAS_CLASS)(e, exceptParentClassIDDataset); | ||
if (preventDefault && !hasClassID && e.cancelable && !e.defaultPrevented) { | ||
var n = e.target.localName; | ||
if (n != 'input' && n != 'button' && n != 'select') e.preventDefault(); | ||
} | ||
}; | ||
var down = function down(e) { | ||
state.isPress = true; | ||
var x = e.clientX || e.targetTouches[0].clientX || false; | ||
var y = e.clientY || e.targetTouches[0].clientY || false; | ||
if (!x || !y) return; | ||
areWePreventDefault(e); | ||
mousePropertyDown.x = x; | ||
mousePropertyDown.y = y; | ||
mousePropertyMove.x = x; | ||
mousePropertyMove.y = y; | ||
extraEvent.down(e); | ||
}; | ||
var move = function move(e) { | ||
if (!state.isPress) return; | ||
var x = e.clientX || e.targetTouches[0].clientX || false; | ||
var y = e.clientY || e.targetTouches[0].clientY || false; | ||
if (!x || !y) return; | ||
areWePreventDefault(e); | ||
var dx = mousePropertyDown.x, | ||
dy = mousePropertyDown.y; | ||
moveOffsetProperty.x = x - dx; | ||
moveOffsetProperty.y = y - dy; | ||
mousePropertyMove.x = x; | ||
mousePropertyMove.y = y; | ||
extraEvent.move(_objectSpread(_objectSpread({}, e), {}, { | ||
moveOffsetProperty: moveOffsetProperty | ||
})); | ||
}; | ||
var up = function up(e) { | ||
state.isPress = false; | ||
var dx = mousePropertyDown.x, | ||
dy = mousePropertyDown.y; | ||
var mx = mousePropertyMove.x, | ||
my = mousePropertyMove.y; | ||
var deviation = state.deviation; | ||
var m = Math.sqrt(Math.pow(mx - dx, 2) + Math.pow(my - dy, 2)); | ||
if (m < deviation) checkDataset(e); | ||
extraEvent.up(e); | ||
}; | ||
var addListener = function addListener(device) { | ||
if (device === 'mobile') { | ||
document.addEventListener('touchstart', down, eventProperty); | ||
document.addEventListener('touchmove', move, eventProperty); | ||
document.addEventListener('touchend', up); | ||
} else { | ||
document.addEventListener('mousedown', down); | ||
document.addEventListener('mousemove', move); | ||
document.addEventListener('mouseup', up); | ||
} | ||
}; | ||
var removeListener = function removeListener(device) { | ||
if (device === 'mobile') { | ||
document.removeEventListener('mousedown', down); | ||
document.removeEventListener('mousemove', move); | ||
document.removeEventListener('mouseup', up); | ||
} else { | ||
document.removeEventListener('touchstart', down); | ||
document.removeEventListener('touchmove', move); | ||
document.removeEventListener('touchend', up); | ||
} | ||
}; | ||
var eventTransform = function eventTransform() { | ||
var device = state.device; | ||
var d = (0, _constants.GET_DEVICE)(); | ||
if (!device) { | ||
state.device = d; | ||
addListener(d); | ||
} else if (device !== d) { | ||
state.device = d; | ||
removeListener(d); | ||
addListener(d); | ||
} | ||
}; | ||
/** | ||
* | ||
* @param {queryString} query make sure it's uni-name.(ex: .target || #target) | ||
*/ | ||
var addPreventExcept = function addPreventExcept(query) { | ||
var type = _typeof(query); | ||
if (type === 'string') exceptParentClassIDDataset.push(query);else if (Array.isArray(query)) { | ||
exceptParentClassIDDataset = _toConsumableArray(query); | ||
} | ||
}; | ||
/** | ||
* set preventDefault will call or not. | ||
* @param {boolean} value | ||
*/ | ||
exports.addPreventExcept = addPreventExcept; | ||
var setPreventDefault = function setPreventDefault(value) { | ||
state.preventDefault = value; | ||
}; | ||
/** | ||
* | ||
* @param {queryString} query make sure it's uni-name.(ex: .target || #target) | ||
* @param {function} callback call when click | ||
* @returns | ||
*/ | ||
exports.setPreventDefault = setPreventDefault; | ||
var add = function add(query, callback) { | ||
if (!callback) return; | ||
var type = query.slice(0, 1) === '.' ? '_class' : '_id'; | ||
var name = query.slice(1); | ||
var key = name + type; | ||
dataset[key] = callback; | ||
}; | ||
exports.add = add; | ||
var remove = function remove(query) { | ||
var type = query.slice(0, 1) == '.' ? '_class' : '_id'; | ||
var name = query.slice(1); | ||
var key = name + type; | ||
delete dataset[key]; | ||
}; | ||
/** | ||
* add events | ||
*/ | ||
exports.remove = remove; | ||
var install = function install() { | ||
eventTransform(); | ||
window.addEventListener('resize', eventTransform); | ||
}; | ||
/** | ||
* clear all dataset | ||
*/ | ||
exports.install = install; | ||
var clear = function clear() { | ||
exports.dataset = dataset = {}; | ||
}; | ||
exports.clear = clear; | ||
var _default = { | ||
install: install, | ||
dataset: dataset, | ||
addPreventExcept: addPreventExcept, | ||
setPreventDefault: setPreventDefault, | ||
add: add, | ||
clear: clear, | ||
remove: remove | ||
}; | ||
exports["default"] = _default; |
{ | ||
"name": "lesca-click", | ||
"version": "4.0.0", | ||
"version": "4.0.1", | ||
"description": "replace click / touch start on SPY page", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -31,9 +31,3 @@ [![dev by JamesHsu](https://img.shields.io/badge/Dev%20by-Jameshsu1125-green)](https://github.com/jameshsu1125/) [![made in Taiwan](https://img.shields.io/badge/Made%20in-Taiwan-orange)](https://github.com/jameshsu1125/) | ||
| clear() | | clear all functions | | | ||
| destory() | | remove all event | | | ||
# Properties | ||
| Properties | type | description | default | | ||
| :------------- | :----: | :-------------------------: | ------: | | ||
| preventDefault | bool | prevent Default by yourself | true | | ||
| deviation | number | click hand deviation | 30 | | ||
| setPreventDefault(value) | value | is set preventDefault | | | ||
| addPreventExcept(DOM-query) | DOM-query | .target or #target | | |
@@ -1,3 +0,172 @@ | ||
import Click from './click'; | ||
import { GET_DEVICE, CHECK_PARENT_HAS_CLASS } from './constants'; | ||
export default Click; | ||
/** | ||
* event dataset | ||
*/ | ||
export let dataset = {}; | ||
const mousePropertyDown = { x: 0, y: 0 }; | ||
const mousePropertyMove = { x: mousePropertyDown.x, y: mousePropertyDown.y }; | ||
const extraEvent = { down: () => {}, move: () => {}, up: () => {} }; | ||
const state = { device: false, isPress: false, deviation: 30, preventDefault: true }; | ||
const eventProperty = { passive: false, capture: false }; | ||
const moveOffsetProperty = { x: 0, y: 0 }; | ||
let exceptParentClassIDDataset = []; | ||
const checkDataset = (e) => { | ||
const { target } = e; | ||
[`${target.id}_id`, `${target.className}_class`].forEach((name) => { | ||
dataset[name]?.(e); | ||
}); | ||
}; | ||
const areWePreventDefault = (e) => { | ||
const { preventDefault } = state; | ||
const hasClassID = CHECK_PARENT_HAS_CLASS(e, exceptParentClassIDDataset); | ||
if (preventDefault && !hasClassID && e.cancelable && !e.defaultPrevented) { | ||
const n = e.target.localName; | ||
if (n != 'input' && n != 'button' && n != 'select') e.preventDefault(); | ||
} | ||
}; | ||
const down = (e) => { | ||
state.isPress = true; | ||
const x = e.clientX || e.targetTouches[0].clientX || false; | ||
const y = e.clientY || e.targetTouches[0].clientY || false; | ||
if (!x || !y) return; | ||
areWePreventDefault(e); | ||
mousePropertyDown.x = x; | ||
mousePropertyDown.y = y; | ||
mousePropertyMove.x = x; | ||
mousePropertyMove.y = y; | ||
extraEvent.down(e); | ||
}; | ||
const move = (e) => { | ||
if (!state.isPress) return; | ||
const x = e.clientX || e.targetTouches[0].clientX || false; | ||
const y = e.clientY || e.targetTouches[0].clientY || false; | ||
if (!x || !y) return; | ||
areWePreventDefault(e); | ||
const { x: dx, y: dy } = mousePropertyDown; | ||
moveOffsetProperty.x = x - dx; | ||
moveOffsetProperty.y = y - dy; | ||
mousePropertyMove.x = x; | ||
mousePropertyMove.y = y; | ||
extraEvent.move({ ...e, moveOffsetProperty }); | ||
}; | ||
const up = (e) => { | ||
state.isPress = false; | ||
const { x: dx, y: dy } = mousePropertyDown; | ||
const { x: mx, y: my } = mousePropertyMove; | ||
const { deviation } = state; | ||
const m = Math.sqrt((mx - dx) ** 2 + (my - dy) ** 2); | ||
if (m < deviation) checkDataset(e); | ||
extraEvent.up(e); | ||
}; | ||
const addListener = (device) => { | ||
if (device === 'mobile') { | ||
document.addEventListener('touchstart', down, eventProperty); | ||
document.addEventListener('touchmove', move, eventProperty); | ||
document.addEventListener('touchend', up); | ||
} else { | ||
document.addEventListener('mousedown', down); | ||
document.addEventListener('mousemove', move); | ||
document.addEventListener('mouseup', up); | ||
} | ||
}; | ||
const removeListener = (device) => { | ||
if (device === 'mobile') { | ||
document.removeEventListener('mousedown', down); | ||
document.removeEventListener('mousemove', move); | ||
document.removeEventListener('mouseup', up); | ||
} else { | ||
document.removeEventListener('touchstart', down); | ||
document.removeEventListener('touchmove', move); | ||
document.removeEventListener('touchend', up); | ||
} | ||
}; | ||
const eventTransform = () => { | ||
const { device } = state; | ||
const d = GET_DEVICE(); | ||
if (!device) { | ||
state.device = d; | ||
addListener(d); | ||
} else if (device !== d) { | ||
state.device = d; | ||
removeListener(d); | ||
addListener(d); | ||
} | ||
}; | ||
/** | ||
* | ||
* @param {queryString} query make sure it's uni-name.(ex: .target || #target) | ||
*/ | ||
export const addPreventExcept = (query) => { | ||
const type = typeof query; | ||
if (type === 'string') exceptParentClassIDDataset.push(query); | ||
else if (Array.isArray(query)) { | ||
exceptParentClassIDDataset = [...query]; | ||
} | ||
}; | ||
/** | ||
* set preventDefault will call or not. | ||
* @param {boolean} value | ||
*/ | ||
export const setPreventDefault = (value) => { | ||
state.preventDefault = value; | ||
}; | ||
/** | ||
* | ||
* @param {queryString} query make sure it's uni-name.(ex: .target || #target) | ||
* @param {function} callback call when click | ||
* @returns | ||
*/ | ||
export const add = (query, callback) => { | ||
if (!callback) return; | ||
const type = query.slice(0, 1) === '.' ? '_class' : '_id'; | ||
const name = query.slice(1); | ||
const key = name + type; | ||
dataset[key] = callback; | ||
}; | ||
export const remove = (query) => { | ||
var type = query.slice(0, 1) == '.' ? '_class' : '_id'; | ||
var name = query.slice(1); | ||
var key = name + type; | ||
delete dataset[key]; | ||
}; | ||
/** | ||
* add events | ||
*/ | ||
export const install = () => { | ||
eventTransform(); | ||
window.addEventListener('resize', eventTransform); | ||
}; | ||
/** | ||
* clear all dataset | ||
*/ | ||
export const clear = () => { | ||
dataset = {}; | ||
}; | ||
export default { install, dataset, addPreventExcept, setPreventDefault, add, clear, remove }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
526
21314
14
33