##// END OF EJS Templates
Don't jquery-select image's post twice
neko259 -
r1352:60237ae8 default
parent child Browse files
Show More
@@ -1,168 +1,170 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
86
87 // New image size
87 // New image size
88 var w_scale = 1;
88 var w_scale = 1;
89 var h_scale = 1;
89 var h_scale = 1;
90 if (img_w > win_w) {
90 if (img_w > win_w) {
91 w_scale = img_w / (win_w - margin);
91 w_scale = img_w / (win_w - margin);
92 }
92 }
93 if (img_h > win_h) {
93 if (img_h > win_h) {
94 h_scale = img_h / (win_h - margin);
94 h_scale = img_h / (win_h - margin);
95 }
95 }
96
96
97 var scale = Math.max(w_scale, h_scale)
97 var scale = Math.max(w_scale, h_scale)
98 img_w = img_w / scale;
98 img_w = img_w / scale;
99 img_h = img_h / scale;
99 img_h = img_h / scale;
100
100
101 var postNode = $(el);
102
101 var img_pv = new Image();
103 var img_pv = new Image();
102 var newImage = $(img_pv);
104 var newImage = $(img_pv);
103 newImage.addClass('img-full')
105 newImage.addClass('img-full')
104 .attr('id', thumb_id)
106 .attr('id', thumb_id)
105 .attr('src', $(el).attr('href'))
107 .attr('src', postNode.attr('href'))
106 .appendTo($(el))
108 .appendTo(postNode)
107 .css({
109 .css({
108 'width': img_w,
110 'width': img_w,
109 'height': img_h,
111 'height': img_h,
110 'left': (win_w - img_w) / 2,
112 'left': (win_w - img_w) / 2,
111 'top': ((win_h - img_h) / 2)
113 'top': ((win_h - img_h) / 2)
112 })
114 })
113 //scaling preview
115 //scaling preview
114 .mousewheel(function(event, delta) {
116 .mousewheel(function(event, delta) {
115 var cx = event.originalEvent.clientX;
117 var cx = event.originalEvent.clientX;
116 var cy = event.originalEvent.clientY;
118 var cy = event.originalEvent.clientY;
117
119
118 var i_w = parseFloat(newImage.width());
120 var i_w = parseFloat(newImage.width());
119 var i_h = parseFloat(newImage.height());
121 var i_h = parseFloat(newImage.height());
120
122
121 var newIW = i_w * (delta > 0 ? 1.25 : 0.8);
123 var newIW = i_w * (delta > 0 ? 1.25 : 0.8);
122 var newIH = i_h * (delta > 0 ? 1.25 : 0.8);
124 var newIH = i_h * (delta > 0 ? 1.25 : 0.8);
123
125
124 newImage.width(newIW);
126 newImage.width(newIW);
125 newImage.height(newIH);
127 newImage.height(newIH);
126 // Set position
128 // Set position
127 var oldPosition = newImage.position();
129 var oldPosition = newImage.position();
128 newImage.css({
130 newImage.css({
129 left: parseInt(cx - (newIW/i_w) * (cx - parseInt(oldPosition.left, 10)), 10),
131 left: parseInt(cx - (newIW/i_w) * (cx - parseInt(oldPosition.left, 10)), 10),
130 top: parseInt(cy - (newIH/i_h) * (cy - parseInt(oldPosition.top, 10)), 10)
132 top: parseInt(cy - (newIH/i_h) * (cy - parseInt(oldPosition.top, 10)), 10)
131 });
133 });
132
134
133 return false;
135 return false;
134 }
136 }
135 )
137 )
136 .draggable({
138 .draggable({
137 addClasses: false,
139 addClasses: false,
138 stack: '.img-full'
140 stack: '.img-full'
139 });
141 });
140 } else {
142 } else {
141 existingPopups.remove();
143 existingPopups.remove();
142 }
144 }
143 };
145 };
144
146
145 function addImgPreview() {
147 function addImgPreview() {
146 var viewerName = $('body').attr('data-image-viewer');
148 var viewerName = $('body').attr('data-image-viewer');
147 var viewer = ImageViewer();
149 var viewer = ImageViewer();
148 for (var i = 0; i < IMAGE_VIEWERS.length; i++) {
150 for (var i = 0; i < IMAGE_VIEWERS.length; i++) {
149 var item = IMAGE_VIEWERS[i];
151 var item = IMAGE_VIEWERS[i];
150 if (item[0] === viewerName) {
152 if (item[0] === viewerName) {
151 viewer = item[1];
153 viewer = item[1];
152 break;
154 break;
153 }
155 }
154 }
156 }
155
157
156 //keybind
158 //keybind
157 $(document).on('keyup.removepic', function(e) {
159 $(document).on('keyup.removepic', function(e) {
158 if(e.which === 27) {
160 if(e.which === 27) {
159 $('.img-full').remove();
161 $('.img-full').remove();
160 }
162 }
161 });
163 });
162
164
163 $('body').on('click', '.thumb', function() {
165 $('body').on('click', '.thumb', function() {
164 viewer.view($(this));
166 viewer.view($(this));
165
167
166 return false;
168 return false;
167 });
169 });
168 }
170 }
General Comments 0
You need to be logged in to leave comments. Login now