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