##// END OF EJS Templates
New fast recursion-free tree render engine
New fast recursion-free tree render engine

File last commit:

r1405:0b31d703 default
r1473:d5bf9a5c default
Show More
image.js
202 lines | 5.5 KiB | application/javascript | JavascriptLexer
neko259
Added copyright to the JS code.
r332 /*
@licstart The following is the entire license notice for the
JavaScript code in this page.
Copyright (C) 2013 neko259
The JavaScript code in this page is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this page.
*/
neko259
Fixed image popup margin in rare cases
r1357 var IMAGE_POPUP_MARGIN = 10;
neko259
Setting for image view mode: in post (simple) or in popup
r1122
var IMAGE_VIEWERS = [
['simple', new SimpleImageViewer()],
['popup', new PopupImageViewer()]
];
neko259
Load full images only on click is simple image viewer is enabled
r1128 var FULL_IMG_CLASS = 'post-image-full';
neko259
Compute new image size in popup from the original size, not the previous...
r1355 var ATTR_SCALE = 'scale';
neko259
Setting for image view mode: in post (simple) or in popup
r1122
neko259
Fixed SVG popup preview
r1405 // Init image viewer
var viewerName = $('body').attr('data-image-viewer');
var viewer = ImageViewer();
for (var i = 0; i < IMAGE_VIEWERS.length; i++) {
var item = IMAGE_VIEWERS[i];
if (item[0] === viewerName) {
viewer = item[1];
break;
}
}
function getFullImageWidth(previewImage) {
var full_img_w = previewImage.attr('data-width');
if (full_img_w == null) {
full_img_w = previewImage[0].naturalWidth;
}
return full_img_w;
}
function getFullImageHeight(previewImage) {
var full_img_h = previewImage.attr('data-height');
if (full_img_h == null) {
full_img_h = previewImage[0].naturalHeight;
}
return full_img_h;
}
neko259
Setting for image view mode: in post (simple) or in popup
r1122 function ImageViewer() {}
ImageViewer.prototype.view = function (post) {};
function SimpleImageViewer() {}
SimpleImageViewer.prototype.view = function (post) {
neko259
Load full images only on click is simple image viewer is enabled
r1128 var images = post.find('img');
images.toggle();
// When we first enlarge an image, a full image needs to be created
if (images.length == 1) {
neko259
Set width and height of a full image in simple image viewer
r1283 var thumb = images.first();
neko259
Fixed SVG popup preview
r1405 var width = getFullImageWidth(thumb);
var height = getFullImageHeight(thumb);
neko259
Set width and height of a full image in simple image viewer
r1283
neko259
Resize svg images just like all others
r1285 if (width == null || height == null) {
width = '100%';
height = '100%';
}
neko259
Load full images only on click is simple image viewer is enabled
r1128 var parent = images.first().parent();
var link = parent.attr('href');
var fullImg = $('<img />')
.addClass(FULL_IMG_CLASS)
neko259
Set width and height of a full image in simple image viewer
r1283 .attr('src', link)
.attr('width', width)
.attr('height', height);
neko259
Load full images only on click is simple image viewer is enabled
r1128
parent.append(fullImg);
}
neko259
Setting for image view mode: in post (simple) or in popup
r1122 };
function PopupImageViewer() {}
PopupImageViewer.prototype.view = function (post) {
var el = post;
var thumb_id = 'full' + el.find('img').attr('alt');
var existingPopups = $('#' + thumb_id);
neko259
Don't jquery-select image's post twice
r1352 if (!existingPopups.length) {
neko259
Fixed SVG popup preview
r1405 var imgElement = el.find('img');
neko259
Setting for image view mode: in post (simple) or in popup
r1122
neko259
Fixed SVG popup preview
r1405 var full_img_w = getFullImageWidth(imgElement);
var full_img_h = getFullImageHeight(imgElement);
neko259
Setting for image view mode: in post (simple) or in popup
r1122
var win = $(window);
var win_w = win.width();
var win_h = win.height();
neko259
Fixed image popup scale
r1351
// New image size
var w_scale = 1;
var h_scale = 1;
neko259
Fixed image popup margin in rare cases
r1357
var freeWidth = win_w - 2 * IMAGE_POPUP_MARGIN;
var freeHeight = win_h - 2 * IMAGE_POPUP_MARGIN;
if (full_img_w > freeWidth) {
w_scale = full_img_w / freeWidth;
neko259
Setting for image view mode: in post (simple) or in popup
r1122 }
neko259
Fixed image popup margin in rare cases
r1357 if (full_img_h > freeHeight) {
h_scale = full_img_h / freeHeight;
neko259
Setting for image view mode: in post (simple) or in popup
r1122 }
neko259
Fixed image popup scale
r1351 var scale = Math.max(w_scale, h_scale)
neko259
Compute new image size in popup from the original size, not the previous...
r1355 var img_w = full_img_w / scale;
var img_h = full_img_h / scale;
neko259
Fixed image popup scale
r1351
neko259
Don't jquery-select image's post twice
r1352 var postNode = $(el);
neko259
Setting for image view mode: in post (simple) or in popup
r1122 var img_pv = new Image();
var newImage = $(img_pv);
newImage.addClass('img-full')
.attr('id', thumb_id)
neko259
Don't jquery-select image's post twice
r1352 .attr('src', postNode.attr('href'))
neko259
Compute new image size in popup from the original size, not the previous...
r1355 .attr(ATTR_SCALE, scale)
neko259
Setting for image view mode: in post (simple) or in popup
r1122 .css({
'width': img_w,
'height': img_h,
'left': (win_w - img_w) / 2,
'top': ((win_h - img_h) / 2)
})
neko259
Autodetect only a white list of mimetypes
r1372 .appendTo(postNode)
neko259
Setting for image view mode: in post (simple) or in popup
r1122 //scaling preview
.mousewheel(function(event, delta) {
neko259
Fixed image popup scale
r1351 var cx = event.originalEvent.clientX;
var cy = event.originalEvent.clientY;
neko259
Compute new image size in popup from the original size, not the previous...
r1355 var scale = newImage.attr(ATTR_SCALE) / (delta > 0 ? 1.25 : 0.8);
neko259
Fixed image popup scale
r1351
neko259
Compute new image size in popup from the original size, not the previous...
r1355 var oldWidth = newImage.width();
var oldHeight = newImage.height();
var newIW = full_img_w / scale;
var newIH = full_img_h / scale;
neko259
Setting for image view mode: in post (simple) or in popup
r1122
newImage.width(newIW);
newImage.height(newIH);
neko259
Compute new image size in popup from the original size, not the previous...
r1355 newImage.attr(ATTR_SCALE, scale);
neko259
Fixed image popup scale
r1351 // Set position
var oldPosition = newImage.position();
neko259
Setting for image view mode: in post (simple) or in popup
r1122 newImage.css({
neko259
Compute new image size in popup from the original size, not the previous...
r1355 left: parseInt(cx - (newIW/oldWidth) * (cx - parseInt(oldPosition.left, 10)), 10),
top: parseInt(cy - (newIH/oldHeight) * (cy - parseInt(oldPosition.top, 10)), 10)
neko259
Setting for image view mode: in post (simple) or in popup
r1122 });
return false;
}
)
.draggable({
addClasses: false,
stack: '.img-full'
});
} else {
existingPopups.remove();
}
};
rt@lightning
Add image preview
r238 function addImgPreview() {
//keybind
$(document).on('keyup.removepic', function(e) {
if(e.which === 27) {
$('.img-full').remove();
}
});
rt@lightning
delegate, small fix for gallery mode,
r252 $('body').on('click', '.thumb', function() {
neko259
Setting for image view mode: in post (simple) or in popup
r1122 viewer.view($(this));
rt@lightning
Add image preview
r238
rt@lightning
delegate, small fix for gallery mode,
r252 return false;
rt@lightning
Add image preview
r238 });
neko259
Refactored image preview code
r1081 }