@zohodesk/dot
Version:
In this Library, we Provide Some Basic Components to Build Your Application
193 lines (158 loc) • 4.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ZoomEvent = exports.Zoom = void 0;
exports.checkAudioUrlValidity = checkAudioUrlValidity;
exports.checkFileSourcesValidation = checkFileSourcesValidation;
exports.checkImageValidity = checkImageValidity;
var _Attachment = require("./Attachment");
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
var Zoom = /*#__PURE__*/function () {
function Zoom() {
_classCallCheck(this, Zoom);
var matrix = [1, 0, 0, 1, 0, 0]; // current view transform
this.m = matrix; // alias
this.scale = 1; // current scale
this.pos = {
x: 0,
y: 0
}; // current position of origin
this.dirty = true;
}
_createClass(Zoom, [{
key: "applyTo",
value: function applyTo(el) {
var dirty = this.dirty,
m = this.m;
if (dirty) {
this.update();
}
el.style.transform = "matrix(".concat(m[0], ",").concat(m[1], ",").concat(m[2], ",").concat(m[3], ",").concat(m[4], ",").concat(m[5], ")");
el.style.transformOrigin = '0 0';
}
}, {
key: "update",
value: function update() {
// m[4], m[5] - need to set transforms based on positions. Currently set to 0 to zoom based on center
var m = this.m,
scale = this.scale,
pos = this.pos;
this.dirty = false;
m[3] = m[0] = scale;
m[2] = m[1] = 0;
m[4] = pos.x;
m[5] = pos.y;
}
}, {
key: "pan",
value: function pan(amount) {
if (this.dirty) {
this.update();
}
this.pos.x += amount.x;
this.pos.y += amount.y;
this.dirty = true;
}
}, {
key: "scaleAt",
value: function scaleAt(at, amount) {
// at in screen coords
var dirty = this.dirty,
pos = this.pos;
if (dirty) {
this.update();
}
this.scale *= amount;
this.pos.x = at.x - (at.x - pos.x) * amount;
this.pos.y = at.y - (at.y - pos.y) * amount;
this.dirty = true;
}
}]);
return Zoom;
}();
exports.Zoom = Zoom;
;
var ZoomEvent = /*#__PURE__*/function () {
function ZoomEvent() {
_classCallCheck(this, ZoomEvent);
this.pos = [];
}
_createClass(ZoomEvent, [{
key: "on",
value: function on(dir, ele, zoom, event) {
if (dir === 'in') {
var x = event.pageX - ele.width / 2;
var y = event.pageY - ele.height / 2;
this.pos.push({
x: x,
y: y
});
zoom.scaleAt({
x: x,
y: y
}, 1.6);
zoom.applyTo(ele);
} else if (this.pos.length) {
var _this$pos$pop = this.pos.pop(),
_x = _this$pos$pop.x,
_y = _this$pos$pop.y;
zoom.scaleAt({
x: _x,
y: _y
}, 1 / 1.6);
zoom.applyTo(ele);
}
event.preventDefault();
}
}]);
return ZoomEvent;
}();
exports.ZoomEvent = ZoomEvent;
function checkImageValidity(src) {
return new Promise(function (resolve) {
var img = new Image();
img.src = src;
img.onload = function () {
resolve(true);
};
img.onerror = function () {
resolve(false);
};
});
}
function checkAudioUrlValidity(url) {
return new Promise(function (resolve) {
var audio = new Audio(url);
audio.oncanplaythrough = function () {
resolve(true);
};
audio.onerror = function () {
resolve(false);
};
});
}
;
function checkFileSourcesValidation(_ref) {
var fileName = _ref.fileName,
viewURL = _ref.viewURL;
if ((0, _Attachment.isAudioFile)(fileName)) {
var audioValidityPromise = checkAudioUrlValidity(viewURL);
return audioValidityPromise.then(function (isURLValid) {
return {
isViewURLValid: isURLValid,
canZoom: false
};
});
} else {
var imageValidityPromise = checkImageValidity(viewURL);
return imageValidityPromise.then(function (isURLValid) {
return {
isViewURLValid: isURLValid,
canZoom: isURLValid
};
});
}
}