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