vanilla-drag
Version:
JavaScript draggable HTML elements
147 lines (143 loc) • 5.26 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else {
var a = factory();
for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
}
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var VanillaDrag = /** @class */ (function () {
/**
* VanillaDrag
* @param root
* @param element
*/
function VanillaDrag(root, element) {
this.root = root;
this.element = element;
this.dragging = false;
this.origin = { x: 0, y: 0 };
this.element.style.position = 'fixed';
this.element.addEventListener('mousedown', this.handleMouseDown.bind(this));
this.element.addEventListener('mouseup', this.handleMouseUp.bind(this));
this.root.addEventListener('mousemove', this.handleMouseMove.bind(this));
this.root.addEventListener('resize', this.handleMouseMove.bind(this));
}
/**
* Handle drag start
* @param event
*/
VanillaDrag.prototype.handleMouseDown = function (event) {
this.elementRectCache = this.element.getBoundingClientRect();
this.dragging = true;
this.origin.x = event.pageX - this.elementRectCache.left;
this.origin.y = event.pageY - this.elementRectCache.top;
};
VanillaDrag.prototype.handleMouseMove = function (event) {
var pageX = event.pageX, pageY = event.pageY;
if (this.outOfBounds(pageX, pageY, 0, this.root.innerWidth, 0, this.root.innerHeight)) {
this.dragging = false;
}
if (this.dragging || event.type === 'resize') {
var _a = this.elementRectCache || this.element.getBoundingClientRect(), width = _a.width, height = _a.height;
var _b = this.origin, x = _b.x, y = _b.y;
this.element.style.right = this.root.innerWidth - (pageX - x + width) + "px";
this.element.style.bottom = this.root.innerHeight - (pageY - y + height) + "px";
}
};
/**
* Handle drag end
*/
VanillaDrag.prototype.handleMouseUp = function () {
this.dragging = false;
};
/**
* Check if an element is out of bounds
* @param nodeX
* @param nodeY
* @param boundsMinX
* @param boundsMaxX
* @param boundsMinY
* @param boundsMaxY
*/
VanillaDrag.prototype.outOfBounds = function (x, y, boundsMinX, boundsMaxX, boundsMinY, boundsMaxY) {
return x <= boundsMinX || x >= boundsMaxX || y <= boundsMinY || y >= boundsMaxY;
};
return VanillaDrag;
}());
exports.default = VanillaDrag;
/***/ })
/******/ ]);
});