imageviewer_terrydr
Version:
A simple jQuery image viewing plugin.
111 lines (85 loc) • 2.38 kB
JavaScript
function isString(s) {
return typeof s === 'string';
}
function isNumber(n) {
return typeof n === 'number' && !isNaN(n);
}
function isUndefined(u) {
return typeof u === 'undefined';
}
function toArray(obj, offset) {
var args = [];
if (isNumber(offset)) { // It's necessary for IE8
args.push(offset);
}
return args.slice.apply(obj, args);
}
// Custom proxy to avoid jQuery's guid
function proxy(fn, context) {
var args = toArray(arguments, 2);
return function () {
return fn.apply(context, args.concat(toArray(arguments)));
};
}
function getTransform(options) {
var transforms = [];
var rotate = options.rotate;
var scaleX = options.scaleX;
var scaleY = options.scaleY;
// Scale should come first before rotate
if (isNumber(scaleX) && isNumber(scaleY)) {
transforms.push('scale(' + scaleX + ',' + scaleY + ')');
}
if (isNumber(rotate)) {
transforms.push('rotate(' + rotate + 'deg)');
}
return transforms.length ? transforms.join(' ') : 'none';
}
// Force reflow to enable CSS3 transition
function forceReflow(element) {
return element.offsetWidth;
}
// e.g.: http://domain.com/path/to/picture.jpg?size=1280×960 -> picture.jpg
function getImageName(url) {
return isString(url) ? url.replace(/^.*\//, '').replace(/[\?&#].*$/, '') : '';
}
function getImageSize(image, callback) {
var newImage;
// Modern browsers
if (image.naturalWidth) {
return callback(image.naturalWidth, image.naturalHeight);
}
// IE8: Don't use `new Image()` here
newImage = document.createElement('img');
newImage.onload = function () {
callback(this.width, this.height);
};
newImage.src = image.src;
}
function getTouchesCenter(touches) {
var length = touches.length;
var pageX = 0;
var pageY = 0;
if (length) {
$.each(touches, function (i, touch) {
pageX += touch.pageX;
pageY += touch.pageY;
});
pageX /= length;
pageY /= length;
}
return {
pageX: pageX,
pageY: pageY
};
}
function getResponsiveClass(option) {
switch (option) {
case 2:
return CLASS_HIDE_XS_DOWN;
case 3:
return CLASS_HIDE_SM_DOWN;
case 4:
return CLASS_HIDE_MD_DOWN;
}
}