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