##// END OF EJS Templates
Fixed image popup margin in rare cases
neko259 -
r1357:3a3fc639 default
parent child Browse files
Show More
@@ -1,177 +1,181 b''
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 var IMAGE_POPUP_MARGIN = 10;
27
26
28
27 var IMAGE_VIEWERS = [
29 var IMAGE_VIEWERS = [
28 ['simple', new SimpleImageViewer()],
30 ['simple', new SimpleImageViewer()],
29 ['popup', new PopupImageViewer()]
31 ['popup', new PopupImageViewer()]
30 ];
32 ];
31
33
32 var FULL_IMG_CLASS = 'post-image-full';
34 var FULL_IMG_CLASS = 'post-image-full';
33
35
34 var ATTR_SCALE = 'scale';
36 var ATTR_SCALE = 'scale';
35
37
36
38
37 function ImageViewer() {}
39 function ImageViewer() {}
38 ImageViewer.prototype.view = function (post) {};
40 ImageViewer.prototype.view = function (post) {};
39
41
40 function SimpleImageViewer() {}
42 function SimpleImageViewer() {}
41 SimpleImageViewer.prototype.view = function (post) {
43 SimpleImageViewer.prototype.view = function (post) {
42 var images = post.find('img');
44 var images = post.find('img');
43 images.toggle();
45 images.toggle();
44
46
45 // When we first enlarge an image, a full image needs to be created
47 // When we first enlarge an image, a full image needs to be created
46 if (images.length == 1) {
48 if (images.length == 1) {
47 var thumb = images.first();
49 var thumb = images.first();
48
50
49 var width = thumb.attr('data-width');
51 var width = thumb.attr('data-width');
50 var height = thumb.attr('data-height');
52 var height = thumb.attr('data-height');
51
53
52 if (width == null || height == null) {
54 if (width == null || height == null) {
53 width = '100%';
55 width = '100%';
54 height = '100%';
56 height = '100%';
55 }
57 }
56
58
57 var parent = images.first().parent();
59 var parent = images.first().parent();
58 var link = parent.attr('href');
60 var link = parent.attr('href');
59
61
60 var fullImg = $('<img />')
62 var fullImg = $('<img />')
61 .addClass(FULL_IMG_CLASS)
63 .addClass(FULL_IMG_CLASS)
62 .attr('src', link)
64 .attr('src', link)
63 .attr('width', width)
65 .attr('width', width)
64 .attr('height', height);
66 .attr('height', height);
65
67
66 parent.append(fullImg);
68 parent.append(fullImg);
67 }
69 }
68 };
70 };
69
71
70 function PopupImageViewer() {}
72 function PopupImageViewer() {}
71 PopupImageViewer.prototype.view = function (post) {
73 PopupImageViewer.prototype.view = function (post) {
72 var margin = 20; //..change
73
74 var el = post;
74 var el = post;
75 var thumb_id = 'full' + el.find('img').attr('alt');
75 var thumb_id = 'full' + el.find('img').attr('alt');
76
76
77 var existingPopups = $('#' + thumb_id);
77 var existingPopups = $('#' + thumb_id);
78 if (!existingPopups.length) {
78 if (!existingPopups.length) {
79 var imgElement= el.find('img');
79 var imgElement= el.find('img');
80
80
81 var full_img_w = imgElement.attr('data-width');
81 var full_img_w = imgElement.attr('data-width');
82 var full_img_h = imgElement.attr('data-height');
82 var full_img_h = imgElement.attr('data-height');
83
83
84 var win = $(window);
84 var win = $(window);
85
85
86 var win_w = win.width();
86 var win_w = win.width();
87 var win_h = win.height();
87 var win_h = win.height();
88
88
89 // New image size
89 // New image size
90 var w_scale = 1;
90 var w_scale = 1;
91 var h_scale = 1;
91 var h_scale = 1;
92 if (full_img_w > win_w) {
92
93 w_scale = full_img_w / (win_w - margin);
93 var freeWidth = win_w - 2 * IMAGE_POPUP_MARGIN;
94 var freeHeight = win_h - 2 * IMAGE_POPUP_MARGIN;
95
96 if (full_img_w > freeWidth) {
97 w_scale = full_img_w / freeWidth;
94 }
98 }
95 if (full_img_h > win_h) {
99 if (full_img_h > freeHeight) {
96 h_scale = full_img_h / (win_h - margin);
100 h_scale = full_img_h / freeHeight;
97 }
101 }
98
102
99 var scale = Math.max(w_scale, h_scale)
103 var scale = Math.max(w_scale, h_scale)
100 var img_w = full_img_w / scale;
104 var img_w = full_img_w / scale;
101 var img_h = full_img_h / scale;
105 var img_h = full_img_h / scale;
102
106
103 var postNode = $(el);
107 var postNode = $(el);
104
108
105 var img_pv = new Image();
109 var img_pv = new Image();
106 var newImage = $(img_pv);
110 var newImage = $(img_pv);
107 newImage.addClass('img-full')
111 newImage.addClass('img-full')
108 .attr('id', thumb_id)
112 .attr('id', thumb_id)
109 .attr('src', postNode.attr('href'))
113 .attr('src', postNode.attr('href'))
110 .attr(ATTR_SCALE, scale)
114 .attr(ATTR_SCALE, scale)
111 .appendTo(postNode)
115 .appendTo(postNode)
112 .css({
116 .css({
113 'width': img_w,
117 'width': img_w,
114 'height': img_h,
118 'height': img_h,
115 'left': (win_w - img_w) / 2,
119 'left': (win_w - img_w) / 2,
116 'top': ((win_h - img_h) / 2)
120 'top': ((win_h - img_h) / 2)
117 })
121 })
118 //scaling preview
122 //scaling preview
119 .mousewheel(function(event, delta) {
123 .mousewheel(function(event, delta) {
120 var cx = event.originalEvent.clientX;
124 var cx = event.originalEvent.clientX;
121 var cy = event.originalEvent.clientY;
125 var cy = event.originalEvent.clientY;
122
126
123 var scale = newImage.attr(ATTR_SCALE) / (delta > 0 ? 1.25 : 0.8);
127 var scale = newImage.attr(ATTR_SCALE) / (delta > 0 ? 1.25 : 0.8);
124
128
125 var oldWidth = newImage.width();
129 var oldWidth = newImage.width();
126 var oldHeight = newImage.height();
130 var oldHeight = newImage.height();
127
131
128 var newIW = full_img_w / scale;
132 var newIW = full_img_w / scale;
129 var newIH = full_img_h / scale;
133 var newIH = full_img_h / scale;
130
134
131 newImage.width(newIW);
135 newImage.width(newIW);
132 newImage.height(newIH);
136 newImage.height(newIH);
133 newImage.attr(ATTR_SCALE, scale);
137 newImage.attr(ATTR_SCALE, scale);
134
138
135 // Set position
139 // Set position
136 var oldPosition = newImage.position();
140 var oldPosition = newImage.position();
137 newImage.css({
141 newImage.css({
138 left: parseInt(cx - (newIW/oldWidth) * (cx - parseInt(oldPosition.left, 10)), 10),
142 left: parseInt(cx - (newIW/oldWidth) * (cx - parseInt(oldPosition.left, 10)), 10),
139 top: parseInt(cy - (newIH/oldHeight) * (cy - parseInt(oldPosition.top, 10)), 10)
143 top: parseInt(cy - (newIH/oldHeight) * (cy - parseInt(oldPosition.top, 10)), 10)
140 });
144 });
141
145
142 return false;
146 return false;
143 }
147 }
144 )
148 )
145 .draggable({
149 .draggable({
146 addClasses: false,
150 addClasses: false,
147 stack: '.img-full'
151 stack: '.img-full'
148 });
152 });
149 } else {
153 } else {
150 existingPopups.remove();
154 existingPopups.remove();
151 }
155 }
152 };
156 };
153
157
154 function addImgPreview() {
158 function addImgPreview() {
155 var viewerName = $('body').attr('data-image-viewer');
159 var viewerName = $('body').attr('data-image-viewer');
156 var viewer = ImageViewer();
160 var viewer = ImageViewer();
157 for (var i = 0; i < IMAGE_VIEWERS.length; i++) {
161 for (var i = 0; i < IMAGE_VIEWERS.length; i++) {
158 var item = IMAGE_VIEWERS[i];
162 var item = IMAGE_VIEWERS[i];
159 if (item[0] === viewerName) {
163 if (item[0] === viewerName) {
160 viewer = item[1];
164 viewer = item[1];
161 break;
165 break;
162 }
166 }
163 }
167 }
164
168
165 //keybind
169 //keybind
166 $(document).on('keyup.removepic', function(e) {
170 $(document).on('keyup.removepic', function(e) {
167 if(e.which === 27) {
171 if(e.which === 27) {
168 $('.img-full').remove();
172 $('.img-full').remove();
169 }
173 }
170 });
174 });
171
175
172 $('body').on('click', '.thumb', function() {
176 $('body').on('click', '.thumb', function() {
173 viewer.view($(this));
177 viewer.view($(this));
174
178
175 return false;
179 return false;
176 });
180 });
177 }
181 }
General Comments 0
You need to be logged in to leave comments. Login now