mobinge
Version:
Client-heavy webcomic site display system
107 lines (98 loc) • 3.35 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var PanelMover = function () {
function PanelMover(width) {
_classCallCheck(this, PanelMover);
this.landfall = false;
this.boundary = 30;
this.dragComponent = null;
this.chunkWidth = width;
}
_createClass(PanelMover, [{
key: "draggable",
value: function draggable(myDraggable) {
if (myDraggable) {
this.dragComponent = myDraggable;
return this;
}
return dragComponent;
}
}, {
key: "grab",
value: function grab(e, data) {
if (this.landfall === false) {
this.landfall = data.x;
}
}
}, {
key: "release",
value: function release(e, data) {
this.snap(data.x);
this.landfall = false;
}
}, {
key: "snap",
value: function snap(mouseX) {
var dragX = this.dragComponent.state.x;
if (isNaN(dragX)) console.log('state', this.dragComponent.state);
var chunkOffset = dragX % this.chunkWidth;
var direction = this.toBoundary(Math.abs(chunkOffset)) || this.toDirection(mouseX);
this.dragComponent.snapTo(this.getSnapDestination(dragX, chunkOffset, direction));
}
}, {
key: "snapDistance",
value: function snapDistance(distance) {
if (distance % this.chunkWidth) {
throw "Distance isn't divisible by the item width ${this.chunkWidth}.";
}
this.dragComponent.snapDistance(distance);
}
}, {
key: "toDirection",
value: function toDirection(lastPosition) {
return lastPosition < this.landfall ? 1 : -1;
}
}, {
key: "toBoundary",
value: function toBoundary(offset) {
if (offset > this.chunkWidth - this.boundary) {
return 1;
}
if (offset < this.boundary) {
return -1;
}
return 0;
}
}, {
key: "getSnapDestination",
value: function getSnapDestination(xPosition, offset, direction) {
if (direction == -1) {
return xPosition - offset;
}
return xPosition - (this.chunkWidth + offset);
}
//
// xFromEvent(e) {
// let event = e;
// if (e.nativeEvent !== undefined) {
// event = event.nativeEvent;
// }
// if ((e.touches !== undefined) && (e.touches.length)) {
// event = event.touches[0];
// }
// if ((e.changedTouches !== undefined) && (e.changedTouches.length)) {
// e = e.changedTouches[0];
// }
// if (e.screenX) {
// return e.screenX;
// }
// return undefined;
// }
}]);
return PanelMover;
}();
exports.default = PanelMover;