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