image.js
103 lines
| 3.5 KiB
| application/javascript
|
JavascriptLexer
neko259
|
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. | ||||
*/ | ||||
rt@lightning
|
r238 | function addImgPreview() { | ||
rt@lightning
|
r266 | var margin = 20; //..change | ||
rt@lightning
|
r263 | |||
rt@lightning
|
r238 | //keybind | ||
$(document).on('keyup.removepic', function(e) { | ||||
if(e.which === 27) { | ||||
$('.img-full').remove(); | ||||
} | ||||
}); | ||||
rt@lightning
|
r252 | $('body').on('click', '.thumb', function() { | ||
var el = $(this); | ||||
var thumb_id = 'full' + el.find('img').attr('alt'); | ||||
rt@lightning
|
r238 | |||
neko259
|
r1082 | var existingPopups = $('#' + thumb_id); | ||
if(!existingPopups.length) { | ||||
neko259
|
r1081 | var imgElement= el.find('img'); | ||
var img_w = imgElement.attr('data-width'); | ||||
var img_h = imgElement.attr('data-height'); | ||||
rt@lightning
|
r238 | |||
neko259
|
r1081 | var win = $(window); | ||
var win_w = win.width(); | ||||
var win_h = win.height(); | ||||
rt@lightning
|
r252 | //new image size | ||
if (img_w > win_w) { | ||||
rt@lightning
|
r263 | img_h = img_h * (win_w/img_w) - margin; | ||
img_w = win_w - margin; | ||||
rt@lightning
|
r238 | } | ||
rt@lightning
|
r252 | if (img_h > win_h) { | ||
rt@lightning
|
r263 | img_w = img_w * (win_h/img_h) - margin; | ||
img_h = win_h - margin; | ||||
rt@lightning
|
r238 | } | ||
rt@lightning
|
r252 | var img_pv = new Image(); | ||
neko259
|
r1081 | var newImage = $(img_pv); | ||
newImage.addClass('img-full') | ||||
rt@lightning
|
r252 | .attr('id', thumb_id) | ||
.attr('src', $(el).attr('href')) | ||||
.appendTo($(el)) | ||||
.css({ | ||||
'width': img_w, | ||||
'height': img_h, | ||||
'left': (win_w - img_w) / 2, | ||||
'top': ((win_h - img_h) / 2) | ||||
}) | ||||
//scaling preview | ||||
.mousewheel(function(event, delta) { | ||||
var cx = event.originalEvent.clientX, | ||||
cy = event.originalEvent.clientY, | ||||
neko259
|
r1081 | i_w = parseFloat(newImage.width()), | ||
i_h = parseFloat(newImage.height()), | ||||
rt@lightning
|
r252 | newIW = i_w * (delta > 0 ? 1.25 : 0.8), | ||
newIH = i_h * (delta > 0 ? 1.25 : 0.8); | ||||
neko259
|
r1081 | newImage.width(newIW); | ||
newImage.height(newIH); | ||||
rt@lightning
|
r252 | //set position | ||
neko259
|
r1081 | newImage.css({ | ||
rt@lightning
|
r252 | left: parseInt(cx - (newIW/i_w) * (cx - parseInt($(img_pv).position().left, 10)), 10), | ||
top: parseInt(cy - (newIH/i_h) * (cy - parseInt($(img_pv).position().top, 10)), 10) | ||||
}); | ||||
return false; | ||||
} | ||||
neko259
|
r1082 | ) | ||
.draggable({ | ||||
addClasses: false, | ||||
stack: '.img-full' | ||||
}); | ||||
neko259
|
r1081 | } else { | ||
neko259
|
r1082 | existingPopups.remove(); | ||
rt@lightning
|
r252 | } | ||
//prevent default | ||||
return false; | ||||
rt@lightning
|
r238 | }); | ||
neko259
|
r1081 | } | ||