imageviewer_terrydr
Version:
A simple jQuery image viewing plugin.
192 lines (160 loc) • 5.04 kB
JavaScript
render: function () {
this.initContainer();
this.initViewer();
this.initList();
this.renderViewer();
},
initContainer: function () {
this.container = {
width: $window.innerWidth(),
height: $window.innerHeight()
};
},
initViewer: function () {
var options = this.options;
var $parent = this.$parent;
var viewer;
if (options.inline) {
this.parent = viewer = {
width: max($parent.width(), options.minWidth),
height: max($parent.height(), options.minHeight)
};
}
if (this.isFulled || !viewer) {
viewer = this.container;
}
this.viewer = $.extend({}, viewer);
},
renderViewer: function () {
if (this.options.inline && !this.isFulled) {
this.$viewer.css(this.viewer);
}
},
initList: function () {
var options = this.options;
var $this = this.$element;
var $list = this.$list;
var list = [];
this.$images.each(function (i) {
var src = this.src;
var video = this.attributes["video"]==undefined?"":this.attributes["video"].nodeValue;
this.video=video;
var alt = this.alt || getImageName(src);
var url = options.url;
if (!src) {
return;
}
if (isString(url)) {
url = this.getAttribute(url);
} else if ($.isFunction(url)) {
url = url.call(this, this);
}
list.push(
'<li>' +
'<img' +
' src="' + src + '"' +
' data-action="view"' +
' data-index="' + i + '"' +
' data-original-url="' + (url || src) + '"' +
' video-original-url="' + (video) + '"' +
' alt="' + alt + '"' +
'>' +
'</li>'
);
});
$list.html(list.join('')).find(SELECTOR_IMG).one(EVENT_LOAD, {
filled: true
}, $.proxy(this.loadImage, this));
this.$items = $list.children();
if (options.transition) {
$this.one(EVENT_VIEWED, function () {
$list.addClass(CLASS_TRANSITION);
});
}
},
renderList: function (index) {
var i = index || this.index;
var width = this.$items.eq(i).width();
var outerWidth = width + 1; // 1 pixel of `margin-left` width
// Place the active item in the center of the screen
this.$list.css({
width: outerWidth * this.length,
marginLeft: (this.viewer.width - width) / 2 - outerWidth * i
});
},
resetList: function () {
this.$list.empty().removeClass(CLASS_TRANSITION).css('margin-left', 0);
},
initImage: function (callback) {
var options = this.options;
var $image = this.$image;
var viewer = this.viewer;
var footerHeight = this.$footer.height();
var viewerWidth = viewer.width;
var viewerHeight = max(viewer.height - footerHeight, footerHeight);
var oldImage = this.image || {};
getImageSize($image[0], $.proxy(function (naturalWidth, naturalHeight) {
var aspectRatio = naturalWidth / naturalHeight;
var width = viewerWidth;
var height = viewerHeight;
var initialImage;
var image;
if (viewerHeight * aspectRatio > viewerWidth) {
height = viewerWidth / aspectRatio;
} else {
width = viewerHeight * aspectRatio;
}
width = min(width * 0.9, naturalWidth);
height = min(height * 0.9, naturalHeight);
image = {
naturalWidth: naturalWidth,
naturalHeight: naturalHeight,
aspectRatio: aspectRatio,
ratio: width / naturalWidth,
width: width,
height: height,
left: (viewerWidth - width) / 2,
top: (viewerHeight - height) / 2
};
initialImage = $.extend({}, image);
if (options.rotatable) {
image.rotate = oldImage.rotate || 0;
initialImage.rotate = 0;
}
if (options.scalable) {
image.scaleX = oldImage.scaleX || 1;
image.scaleY = oldImage.scaleY || 1;
initialImage.scaleX = 1;
initialImage.scaleY = 1;
}
this.image = image;
this.initialImage = initialImage;
if ($.isFunction(callback)) {
callback();
}
}, this));
},
renderImage: function (callback) {
var image = this.image;
var $image = this.$image;
$image.css({
width: image.width,
height: image.height,
marginLeft: image.left,
marginTop: image.top,
transform: getTransform(image)
});
if ($.isFunction(callback)) {
if (this.transitioning) {
$image.one(EVENT_TRANSITIONEND, callback);
} else {
callback();
}
}
},
resetImage: function () {
if (this.$image) {
this.$image.remove();
this.$image = null;
}
},