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