##// END OF EJS Templates
Set width and height of a full image in simple image viewer
neko259 -
r1283:6c658638 default
parent child Browse files
Show More
@@ -1,148 +1,155 b''
1 1 /*
2 2 @licstart The following is the entire license notice for the
3 3 JavaScript code in this page.
4 4
5 5
6 6 Copyright (C) 2013 neko259
7 7
8 8 The JavaScript code in this page is free software: you can
9 9 redistribute it and/or modify it under the terms of the GNU
10 10 General Public License (GNU GPL) as published by the Free Software
11 11 Foundation, either version 3 of the License, or (at your option)
12 12 any later version. The code is distributed WITHOUT ANY WARRANTY;
13 13 without even the implied warranty of MERCHANTABILITY or FITNESS
14 14 FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
15 15
16 16 As additional permission under GNU GPL version 3 section 7, you
17 17 may distribute non-source (e.g., minimized or compacted) forms of
18 18 that code without the copy of the GNU GPL normally required by
19 19 section 4, provided you include this license notice and a URL
20 20 through which recipients can access the Corresponding Source.
21 21
22 22 @licend The above is the entire license notice
23 23 for the JavaScript code in this page.
24 24 */
25 25
26 26
27 27 var IMAGE_VIEWERS = [
28 28 ['simple', new SimpleImageViewer()],
29 29 ['popup', new PopupImageViewer()]
30 30 ];
31 31
32 32 var FULL_IMG_CLASS = 'post-image-full';
33 33
34 34
35 35 function ImageViewer() {}
36 36 ImageViewer.prototype.view = function (post) {};
37 37
38 38 function SimpleImageViewer() {}
39 39 SimpleImageViewer.prototype.view = function (post) {
40 40 var images = post.find('img');
41 41 images.toggle();
42 42
43 43 // When we first enlarge an image, a full image needs to be created
44 44 if (images.length == 1) {
45 var thumb = images.first();
46
47 var width = thumb.attr('data-width');
48 var height = thumb.attr('data-height');
49
45 50 var parent = images.first().parent();
46 51 var link = parent.attr('href');
47 52
48 53 var fullImg = $('<img />')
49 54 .addClass(FULL_IMG_CLASS)
50 .attr('src', link);
55 .attr('src', link)
56 .attr('width', width)
57 .attr('height', height);
51 58
52 59 parent.append(fullImg);
53 60 }
54 61 };
55 62
56 63 function PopupImageViewer() {}
57 64 PopupImageViewer.prototype.view = function (post) {
58 65 var margin = 20; //..change
59 66
60 67 var el = post;
61 68 var thumb_id = 'full' + el.find('img').attr('alt');
62 69
63 70 var existingPopups = $('#' + thumb_id);
64 71 if(!existingPopups.length) {
65 72 var imgElement= el.find('img');
66 73
67 74 var img_w = imgElement.attr('data-width');
68 75 var img_h = imgElement.attr('data-height');
69 76
70 77 var win = $(window);
71 78
72 79 var win_w = win.width();
73 80 var win_h = win.height();
74 81 //new image size
75 82 if (img_w > win_w) {
76 83 img_h = img_h * (win_w/img_w) - margin;
77 84 img_w = win_w - margin;
78 85 }
79 86 if (img_h > win_h) {
80 87 img_w = img_w * (win_h/img_h) - margin;
81 88 img_h = win_h - margin;
82 89 }
83 90
84 91 var img_pv = new Image();
85 92 var newImage = $(img_pv);
86 93 newImage.addClass('img-full')
87 94 .attr('id', thumb_id)
88 95 .attr('src', $(el).attr('href'))
89 96 .appendTo($(el))
90 97 .css({
91 98 'width': img_w,
92 99 'height': img_h,
93 100 'left': (win_w - img_w) / 2,
94 101 'top': ((win_h - img_h) / 2)
95 102 })
96 103 //scaling preview
97 104 .mousewheel(function(event, delta) {
98 105 var cx = event.originalEvent.clientX,
99 106 cy = event.originalEvent.clientY,
100 107 i_w = parseFloat(newImage.width()),
101 108 i_h = parseFloat(newImage.height()),
102 109 newIW = i_w * (delta > 0 ? 1.25 : 0.8),
103 110 newIH = i_h * (delta > 0 ? 1.25 : 0.8);
104 111
105 112 newImage.width(newIW);
106 113 newImage.height(newIH);
107 114 //set position
108 115 newImage.css({
109 116 left: parseInt(cx - (newIW/i_w) * (cx - parseInt($(img_pv).position().left, 10)), 10),
110 117 top: parseInt(cy - (newIH/i_h) * (cy - parseInt($(img_pv).position().top, 10)), 10)
111 118 });
112 119
113 120 return false;
114 121 }
115 122 )
116 123 .draggable({
117 124 addClasses: false,
118 125 stack: '.img-full'
119 126 });
120 127 } else {
121 128 existingPopups.remove();
122 129 }
123 130 };
124 131
125 132 function addImgPreview() {
126 133 var viewerName = $('body').attr('data-image-viewer');
127 134 var viewer = ImageViewer();
128 135 for (var i = 0; i < IMAGE_VIEWERS.length; i++) {
129 136 var item = IMAGE_VIEWERS[i];
130 137 if (item[0] === viewerName) {
131 138 viewer = item[1];
132 139 break;
133 140 }
134 141 }
135 142
136 143 //keybind
137 144 $(document).on('keyup.removepic', function(e) {
138 145 if(e.which === 27) {
139 146 $('.img-full').remove();
140 147 }
141 148 });
142 149
143 150 $('body').on('click', '.thumb', function() {
144 151 viewer.view($(this));
145 152
146 153 return false;
147 154 });
148 155 }
General Comments 0
You need to be logged in to leave comments. Login now