nodebb-plugin-group-gallery 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/.editorconfig +8 -0
  2. package/.gitattributes +22 -0
  3. package/LICENSE +8 -0
  4. package/README.md +3 -0
  5. package/lib/comments.js +178 -0
  6. package/lib/config.js +47 -0
  7. package/lib/gallery.js +206 -0
  8. package/lib/nodebb.js +19 -0
  9. package/lib/sockets.js +113 -0
  10. package/lib/upgrade.js +101 -0
  11. package/library.js +258 -0
  12. package/package.json +34 -0
  13. package/plugin.json +30 -0
  14. package/static/js/lib/comments.js +163 -0
  15. package/static/js/lib/modal.js +111 -0
  16. package/static/js/lib/overview.js +15 -0
  17. package/static/js/lib/single.js +64 -0
  18. package/static/js/lib/uploader.js +37 -0
  19. package/static/js/main.js +147 -0
  20. package/static/less/style.less +145 -0
  21. package/static/templates/group-gallery/comments.tpl +15 -0
  22. package/static/templates/group-gallery/gallery.tpl +30 -0
  23. package/static/templates/group-gallery/modal/wrap.tpl +25 -0
  24. package/static/templates/group-gallery/single.tpl +75 -0
  25. package/static/templates/group-gallery/widget.tpl +18 -0
  26. package/static/vendor/fancybox/blank.gif +0 -0
  27. package/static/vendor/fancybox/fancybox_loading.gif +0 -0
  28. package/static/vendor/fancybox/fancybox_loading@2x.gif +0 -0
  29. package/static/vendor/fancybox/fancybox_overlay.png +0 -0
  30. package/static/vendor/fancybox/fancybox_sprite.png +0 -0
  31. package/static/vendor/fancybox/fancybox_sprite@2x.png +0 -0
  32. package/static/vendor/fancybox/jquery.fancybox-thumbs.css +55 -0
  33. package/static/vendor/fancybox/jquery.fancybox-thumbs.js +162 -0
  34. package/static/vendor/fancybox/jquery.fancybox.css +274 -0
  35. package/static/vendor/fancybox/jquery.fancybox.pack.js +46 -0
package/lib/upgrade.js ADDED
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+
3
+ var pjson = require('../package.json'),
4
+ async = require('async'),
5
+ NodeBB = require('./nodebb'),
6
+
7
+ Gallery = require('./gallery'),
8
+
9
+ Upgrade = {};
10
+
11
+ Upgrade.doUpgrade = function(oldVersion, newVersion, callback) {
12
+ var thisVersion;
13
+
14
+ async.series([
15
+ function(next) {
16
+ thisVersion = '0.0.2';
17
+
18
+ if (oldVersion < thisVersion) {
19
+ getAllImageIds(function(err, ids) {
20
+ async.each(ids, function(id, next) {
21
+ async.parallel([
22
+ function(next) {
23
+ NodeBB.db.setObjectField('group-gallery:images:' + id, 'caption', '', next);
24
+ },
25
+ function(next) {
26
+ NodeBB.db.setObjectField('group-gallery:images:' + id, 'viewcount', 0, next);
27
+ },
28
+ function(next) {
29
+ NodeBB.db.getSortedSetRevRange('groups:createtime', 0, -1, function(err, groupNames) {
30
+ async.each(groupNames, function(groupName, next) {
31
+ groupName = NodeBB.utils.slugify(groupName);
32
+ async.waterfall([
33
+ function(next) {
34
+ NodeBB.db.exists('group-gallery:group:' + groupName + ':images', next);
35
+ },
36
+ function(exists, next) {
37
+ if (err || exists) {
38
+ next();
39
+ } else {
40
+ NodeBB.db.rename(
41
+ 'group-gallery:group:' + groupName,
42
+ 'group-gallery:group:' + groupName + ':images',
43
+ next
44
+ );
45
+ }
46
+ }
47
+ ], next);
48
+ }, next);
49
+ });
50
+ }
51
+ ], next);
52
+ }, next);
53
+ });
54
+ } else {
55
+ next();
56
+ }
57
+ }
58
+ ], function(err) {
59
+ if (err) {
60
+ error(err);
61
+ } else {
62
+ done();
63
+ }
64
+ });
65
+
66
+ function done() {
67
+ NodeBB.winston.info('[' + pjson.name + '] Upgraded from ' + oldVersion + ' to ' + newVersion);
68
+ callback();
69
+ }
70
+
71
+ function error(err) {
72
+ NodeBB.winston.error(err);
73
+ NodeBB.winston.info('[' + pjson.name + '] No upgrade performed, old version was ' + oldVersion + ' and new version is ' + newVersion);
74
+ callback();
75
+ }
76
+ };
77
+
78
+ function getAllImages(fields, callback) {
79
+ getAllImageIds(function(err, ids) {
80
+ if (err || !ids || !ids.length) return callback(err);
81
+
82
+ var keys = ids.map(function (id) {
83
+ return 'group-gallery:images:' + id;
84
+ });
85
+
86
+ NodeBB.db.getObjectsFields(keys, fields, callback);
87
+ });
88
+ }
89
+
90
+ function getAllImageIds(callback) {
91
+ NodeBB.db.getObjectField('group-gallery', 'nextImageId', function(err, result) {
92
+ var list = [];
93
+ for (var i = 1, l = parseInt(result, 10); i <= l; i++) {
94
+ list.push(i);
95
+ }
96
+
97
+ callback(err, list);
98
+ });
99
+ }
100
+
101
+ module.exports = Upgrade;
package/library.js ADDED
@@ -0,0 +1,258 @@
1
+ "use strict";
2
+
3
+ var NodeBB = require('./lib/nodebb'),
4
+ UploadsController = NodeBB.UploadsController,
5
+ ControllerHelpers = NodeBB.ControllerHelpers,
6
+
7
+ Config = require('./lib/config'),
8
+ Gallery = require('./lib/gallery'),
9
+
10
+ async = require('async'),
11
+
12
+ GroupGallery = {},
13
+ app;
14
+
15
+ GroupGallery.init = function(params, callback) {
16
+ var router = params.router,
17
+ middleware = params.middleware,
18
+ multipartMiddleware = require('connect-multiparty')();
19
+
20
+ router.get('/groups/:name/gallery', middleware.checkGlobalPrivacySettings, middleware.buildHeader,
21
+ groupChecks, renderImages);
22
+ router.get('/groups/:name/gallery/:image_id', middleware.checkGlobalPrivacySettings, middleware.buildHeader,
23
+ groupChecks, increaseViewCount, renderSingleImage);
24
+ router.get('/api/groups/:name/gallery', middleware.checkGlobalPrivacySettings,
25
+ groupChecks, renderImages);
26
+ router.get('/api/groups/:name/gallery/:image_id', middleware.checkGlobalPrivacySettings,
27
+ groupChecks, increaseViewCount, renderSingleImage);
28
+
29
+ router.post('/groups/:name/gallery/upload', multipartMiddleware, middleware.applyCSRF,
30
+ middleware.authenticate, middleware.checkGlobalPrivacySettings, groupChecks, uploadImage);
31
+
32
+ NodeBB.SocketAdmin[Config.plugin.id] = Config.adminSockets;
33
+ NodeBB.SocketPlugins[Config.plugin.id] = require('./lib/sockets');
34
+
35
+ app = params.app;
36
+
37
+ Config.init(callback);
38
+ };
39
+
40
+ GroupGallery.defineWidget = function(widgets, callback) {
41
+ widgets.push({
42
+ name: Config.plugin.name,
43
+ widget: Config.plugin.id,
44
+ description: Config.plugin.description,
45
+ content: ''
46
+ });
47
+
48
+ callback(null, widgets);
49
+ };
50
+
51
+ GroupGallery.renderWidget = function(widget, callback) {
52
+ if (widget.area.template.indexOf('groups') === 0) {
53
+ var parts = decodeURIComponent(widget.area.url).split('/');
54
+ if (Array.isArray(parts) && parts.length) {
55
+ // I have been assured by baris and julian that this is solid enough.
56
+ var groupName = parts[parts.length - 1];
57
+ Gallery.getImagesByGroupName(groupName, 0, 2, function(err, images) {
58
+ app.render('group-gallery/widget', {images: images}, callback);
59
+ });
60
+ } else {
61
+ callback(null, '<div class="alert alert-warning">An error occurred trying to render this widget.</div>');
62
+ }
63
+ } else {
64
+ NodeBB.User.isAdministrator(widget.uid, function(err, isAdmin) {
65
+ var html = '';
66
+ if (isAdmin) {
67
+ html = '<div class="alert alert-warning">The Group Gallery widget only works on group pages. <br><a href="/admin/extend/widgets">&#187; Widget settings</a></a></div>';
68
+ }
69
+
70
+ callback(null, html);
71
+ });
72
+ }
73
+ };
74
+
75
+ GroupGallery.groupRename = function(data) {
76
+ Gallery.renameGroup(data.old, data.new);
77
+ };
78
+
79
+ function renderImages(req, res, next) {
80
+ var imagesPerPage = 4,
81
+ uid = req.query.uid && req.query.uid.length ? parseInt(req.query.uid, 10) : 0;
82
+
83
+ async.waterfall([
84
+ function(next) {
85
+ if (!isNaN(uid) && uid > 0) {
86
+ Gallery.getGroupImageCountByUid(req.params.name, uid, next);
87
+ } else {
88
+ Gallery.getGroupImageCount(req.params.name, next);
89
+ }
90
+ },
91
+ function(imageCount, next) {
92
+ var pageCount = Math.max(1, Math.ceil(imageCount / imagesPerPage));
93
+
94
+ if (req.query.page < 1 || req.query.page > pageCount) {
95
+ return ControllerHelpers.notFound(req, res);
96
+ }
97
+
98
+ var page = parseInt(req.query.page, 10) || 1;
99
+ var start = (page - 1) * imagesPerPage,
100
+ end = start + imagesPerPage - 1;
101
+
102
+ async.parallel({
103
+ images: function(next) {
104
+ if (!isNaN(uid) && uid > 0) {
105
+ Gallery.getImagesByUid(req.params.name, uid, start, end, next);
106
+ } else {
107
+ Gallery.getImagesByGroupName(req.params.name, start, end, next);
108
+ }
109
+ },
110
+ group: function(next) {
111
+ NodeBB.Groups.getGroupNameByGroupSlug(req.params.name, function(err, groupName) {
112
+ NodeBB.Groups.get(groupName, {}, next);
113
+ });
114
+ }
115
+ }, function(err, results) {
116
+ next(err, {
117
+ pageCount: pageCount,
118
+ currentPage: page,
119
+ images: results.images,
120
+ group: results.group
121
+ });
122
+ });
123
+ }
124
+ ], function(err, data) {
125
+ data.pagination = NodeBB.Pagination.create(data.currentPage, data.pageCount);
126
+ res.render('group-gallery/gallery', data);
127
+ });
128
+ }
129
+
130
+ function renderSingleImage(req, res, next) {
131
+ var id = parseInt(req.params.image_id, 10);
132
+ if (isNaN(id)) {
133
+ next(new Error('invalid-image'));
134
+ } else {
135
+ Gallery.getImagesByIds([id], function(err, image) {
136
+ if (err || !image.length) {
137
+ next(new Error('invalid-image'));
138
+ } else {
139
+ image = image[0];
140
+
141
+ async.parallel({
142
+ nextImages: function(next) {
143
+ // Get the next 3 images
144
+ NodeBB.db.getSortedSetRangeByScore(
145
+ 'group-gallery:group:' + req.params.name + ':images',
146
+ 1, 3, image.timestamp, Date.now(),
147
+ function(err, result) {
148
+ Gallery.getImagesByIds(result, next);
149
+ }
150
+ );
151
+ },
152
+ prevId: function(next) {
153
+ // Get the previous image
154
+ NodeBB.db.getSortedSetRangeByScore(
155
+ 'group-gallery:group:' + req.params.name + ':images',
156
+ 0, 0, 0, image.timestamp - 1,
157
+ function(err, result) {
158
+ next(null, result.length ? parseInt(result[result.length - 1], 10) : null);
159
+ }
160
+ );
161
+ },
162
+ group: function(next) {
163
+ NodeBB.Groups.getGroupNameByGroupSlug(req.params.name, function(err, groupName) {
164
+ NodeBB.Groups.get(groupName, {}, next);
165
+ });
166
+ }
167
+ }, function(err, result) {
168
+ if (err) {
169
+ next(new Error('invalid-image'));
170
+ } else {
171
+ result.image = image;
172
+ result.nextId = result.nextImages.length ? result.nextImages[0].id : null;
173
+ res.render('group-gallery/single', result);
174
+ }
175
+ });
176
+ }
177
+ });
178
+ }
179
+ }
180
+
181
+ function increaseViewCount(req, res, next) {
182
+ var id = parseInt(req.params.image_id, 10);
183
+ if (isNaN(id)) {
184
+ next(new Error('invalid-image'));
185
+ } else {
186
+ Gallery.increaseViewCount(id, function(err, result) {
187
+ next();
188
+ });
189
+ }
190
+ }
191
+
192
+ function uploadImage(req, res, next) {
193
+ async.parallel({
194
+ isAdminOrGlobalMod: function (next) {
195
+ NodeBB.User.isAdminOrGlobalMod(req.user.uid, next);
196
+ },
197
+ isMember: function (next) {
198
+ NodeBB.Groups.isMember(req.user.uid, req.params.name, next);
199
+ }
200
+ }, function(err, results) {
201
+ if (err) {
202
+ return next(err);
203
+ }
204
+ if (!results.isAdminOrGlobalMod && !results.isMember) {
205
+ return next(new Error('[[error:no-privileges]]'));
206
+ }
207
+ UploadsController.upload(req, res, function(file, next) {
208
+ var params = JSON.parse(req.body.params);
209
+ if (params && params.caption && params.caption.length && NodeBB.Plugins.hasListeners('filter:uploadImage')) {
210
+ NodeBB.Plugins.fireHook('filter:uploadImage', {image: file, uid: req.user.uid}, function(err, data) {
211
+ if (err) {
212
+ return next(err);
213
+ }
214
+
215
+ Gallery.addImage({
216
+ uid: req.user.uid,
217
+ url: data.url,
218
+ group: req.params.name,
219
+ caption: params.caption
220
+ }, function(err, image) {
221
+ NodeBB.SocketIndex.server.sockets.emit('event:group-gallery.newImage', image);
222
+ next(err, data);
223
+ });
224
+ });
225
+ } else {
226
+ next(new Error('no-upload-plugin'))
227
+ }
228
+ }, next);
229
+ });
230
+ }
231
+
232
+ function groupChecks(req, res, next) {
233
+ async.parallel({
234
+ exists: function (next) {
235
+ NodeBB.Groups.exists(req.params.name, next);
236
+ },
237
+ isHidden: function (next) {
238
+ NodeBB.Groups.isHidden(req.params.name, next);
239
+ },
240
+ isAdminOrGlobalMod: function (next) {
241
+ NodeBB.User.isAdminOrGlobalMod(req.user.uid, next);
242
+ },
243
+ isMember: function (next) {
244
+ NodeBB.Groups.isMember(req.user.uid, req.params.name, next);
245
+ }
246
+ }, function (err, results) {
247
+ if (err) {
248
+ return next(err);
249
+ }
250
+ if (!results.exists || (results.isHidden && !results.isAdminOrGlobalMod && !results.isMember)) {
251
+ ControllerHelpers.notFound(req, res);
252
+ return;
253
+ }
254
+ next();
255
+ });
256
+ }
257
+
258
+ module.exports = GroupGallery;
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "nodebb-plugin-group-gallery",
3
+ "version": "0.0.3",
4
+ "description": "Exposes photo uploading capability for groups, and introduces a custom widget for placement into group pages",
5
+ "main": "library.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/nodebb/nodebb-plugin-group-gallery"
9
+ },
10
+ "keywords": [
11
+ "nodebb",
12
+ "plugin",
13
+ "gallery",
14
+ "photos",
15
+ "upload"
16
+ ],
17
+ "author": {
18
+ "name": "Erik Schamper",
19
+ "email": "erikschamper@outlook.com"
20
+ },
21
+ "license": "MIT",
22
+ "bugs": {
23
+ "url": "https://github.com/nodebb/nodebb-plugin-group-gallery/issues"
24
+ },
25
+ "readmeFilename": "README.md",
26
+ "dependencies": {
27
+ "async": "~0.9.0",
28
+ "connect-multiparty": "^1.2.4",
29
+ "string": "^3.0.0"
30
+ },
31
+ "nbbpm": {
32
+ "compatibility": "^0.7.0"
33
+ }
34
+ }
package/plugin.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "id": "nodebb-plugin-group-gallery",
3
+ "name": "Group Galleries",
4
+ "description": "Exposes photo uploading capability for groups, and introduces a custom widget for placement into group pages",
5
+ "url": "https://github.com/NodeBB/nodebb-plugin-group-gallery",
6
+ "library": "./library.js",
7
+ "hooks": [
8
+ { "hook": "static:app.load", "method": "init" },
9
+ { "hook": "filter:widgets.getWidgets", "method": "defineWidget" },
10
+ { "hook": "filter:widget.render:group-gallery", "method": "renderWidget" },
11
+ { "hook": "action:group.rename", "method": "groupRename" }
12
+ ],
13
+ "staticDirs": {
14
+ "static": "./static"
15
+ },
16
+ "less": [
17
+ "static/less/style.less"
18
+ ],
19
+ "css": [
20
+ "static/vendor/fancybox/jquery.fancybox.css",
21
+ "static/vendor/fancybox/jquery.fancybox-thumbs.css"
22
+ ],
23
+ "scripts": [
24
+ "static/vendor/fancybox/jquery.fancybox.pack.js",
25
+ "static/vendor/fancybox/jquery.fancybox-thumbs.js",
26
+ "static/js/main.js",
27
+ "static/js/lib"
28
+ ],
29
+ "templates": "static/templates"
30
+ }
@@ -0,0 +1,163 @@
1
+ "use strict";
2
+
3
+ (function(GroupGallery) {
4
+
5
+ var Comments = {},
6
+ currentImageId,
7
+ socketEvents = {
8
+ newComment: 'event:group-gallery.newComment',
9
+ removeComment: 'event:group-gallery.removeComment',
10
+ editComment: 'event:group-gallery.editComment'
11
+ };
12
+
13
+ Comments.load = function(id) {
14
+ socket.emit('plugins.group-gallery.getComments', {imageId: id}, function(err, comments) {
15
+ currentImageId = id;
16
+ Comments.add(comments);
17
+ });
18
+ };
19
+
20
+ Comments.add = function(comments) {
21
+ comments = comments.map(function(el) {
22
+ el.isMod = parseInt(app.user.uid, 10) === parseInt(el.uid, 10) || app.user.isAdmin;
23
+ return el;
24
+ });
25
+
26
+ templates.parse('group-gallery/comments', {
27
+ comments: comments.reverse()
28
+ }, function(html) {
29
+ $('.group-gallery-comments').prepend(html);
30
+ });
31
+ };
32
+
33
+ Comments.send = function(comment) {
34
+ if (!isNaN(currentImageId)) {
35
+ socket.emit('plugins.group-gallery.addComment', {
36
+ imageId: currentImageId,
37
+ comment: comment
38
+ });
39
+ }
40
+ };
41
+
42
+ Comments.remove = function(id) {
43
+ if (!isNaN(parseInt(id, 10))) {
44
+ socket.emit('plugins.group-gallery.removeComment', {
45
+ commentId: id
46
+ });
47
+ }
48
+ };
49
+
50
+ Comments.edit = function(id, content) {
51
+ if (!isNaN(parseInt(id, 10))) {
52
+ socket.emit('plugins.group-gallery.getRawComment', {commentId: id}, function(err, result) {
53
+ var commentEl = $('[data-comment-id="' + id + '"]'),
54
+ el = commentEl.find('.group-gallery-comment-content'),
55
+ current = result[0].content,
56
+ currentHtml = el.html();
57
+
58
+ commentEl.find('.group-gallery-comment-options').hide();
59
+ el.html(
60
+ '<div class="form-group">' +
61
+ '<input id="group-gallery-comment-input-edit" type="text" class="form-control" value="' + current + '">' +
62
+ '</div>')
63
+ .on('keypress.group-gallery', '#group-gallery-comment-input-edit', function(e) {
64
+ if (e.which === 13 && !e.shiftKey) {
65
+ socket.emit('plugins.group-gallery.editComment', {
66
+ commentId: id,
67
+ content: e.currentTarget.value
68
+ });
69
+ cleanup();
70
+ } else if (e.which === 0) {
71
+ cancel();
72
+ }
73
+ });
74
+
75
+ function cleanup() {
76
+ el.off('keypress.edit', '#group-gallery-comment-input-edit');
77
+ commentEl.find('.group-gallery-comment-options').show();
78
+ }
79
+
80
+ function cancel() {
81
+ el.html(currentHtml);
82
+ cleanup();
83
+ }
84
+ });
85
+ }
86
+ };
87
+
88
+ Comments.clear = function() {
89
+ $('.group-gallery-comments').html('');
90
+ currentImageId = null;
91
+ };
92
+
93
+ Comments.bindEvents = function() {
94
+ var commentInput = $('#group-gallery-comment-input'),
95
+ commentButton = $('[data-func="group-gallery.comment"]'),
96
+ commentsDiv = $('.group-gallery-comments'),
97
+ editCommentButtonSelector = '[data-func="group-gallery.comment.edit"]',
98
+ removeCommentButtonSelector = '[data-func="group-gallery.comment.remove"]';
99
+
100
+ commentInput.on('keypress.group-gallery', function(e) {
101
+ if (e.which === 13 && !e.shiftKey) {
102
+ Comments.send(commentInput.val());
103
+ commentInput.val('');
104
+ }
105
+ });
106
+
107
+ commentButton.on('click.group-gallery', function(e){
108
+ Comments.send(commentInput.val());
109
+ commentInput.val('');
110
+ return false;
111
+ });
112
+
113
+ commentsDiv.on('click.group-gallery', editCommentButtonSelector, function(e) {
114
+ var commentId = $(e.currentTarget).parents('[data-comment-id]').data('commentId');
115
+ Comments.edit(commentId);
116
+
117
+ e.preventDefault();
118
+ return false;
119
+ });
120
+
121
+ commentsDiv.on('click.group-gallery', removeCommentButtonSelector, function(e) {
122
+ var commentId = $(e.currentTarget).parents('[data-comment-id]').data('commentId');
123
+ Comments.remove(commentId);
124
+
125
+ e.preventDefault();
126
+ return false;
127
+ });
128
+
129
+ socket.on(socketEvents.newComment, function(data) {
130
+ if (parseInt(data[0].image, 10) === currentImageId) {
131
+ Comments.add(data);
132
+ }
133
+ });
134
+
135
+ socket.on(socketEvents.removeComment, function(id) {
136
+ $('[data-comment-id="' + id + '"]').remove();
137
+ });
138
+
139
+ socket.on(socketEvents.editComment, function(data) {
140
+ var comment = data[0];
141
+ $('[data-comment-id="' + comment.id + '"] .group-gallery-comment-content').html(comment.content);
142
+ });
143
+ };
144
+
145
+ Comments.unbindEvents = function() {
146
+ var commentInput = $('#group-gallery-comment-input'),
147
+ commentButton = $('[data-func="group-gallery.comment"]'),
148
+ commentsDiv = $('.group-gallery-comments'),
149
+ editCommentButtonSelector = '[data-func="group-gallery.comment.edit"]',
150
+ removeCommentButtonSelector = '[data-func="group-gallery.comment.remove"]';
151
+
152
+ commentInput.off('keypress.group-gallery');
153
+ commentButton.off('click.group-gallery');
154
+ commentsDiv.off('click.group-gallery', editCommentButtonSelector);
155
+ commentsDiv.off('click.group-gallery', removeCommentButtonSelector);
156
+
157
+ socket.off(socketEvents.newComment);
158
+ socket.off(socketEvents.removeComment);
159
+ };
160
+
161
+ GroupGallery.comments = Comments;
162
+
163
+ })(GroupGallery);
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+
3
+ (function(GroupGallery) {
4
+
5
+ var socketEvents = {
6
+ removeImage: 'event:group-gallery.removeImage'
7
+ },
8
+
9
+ Modal = {};
10
+
11
+ Modal.open = function(event) {
12
+ var el = $(event.currentTarget),
13
+ id = el.data('group-gallery-id'),
14
+ index = parseInt(GroupGallery.vars.indexLookup[id], 10);
15
+
16
+ Modal.openOnIndex(index);
17
+
18
+ event.preventDefault();
19
+ return false;
20
+ };
21
+
22
+ Modal.openOnIndex = function(index) {
23
+ GroupGallery.vars.lightboxOptions.index = isNaN(index) || index < 0 ? 0 : index;
24
+ GroupGallery.vars.lightboxOptions.beforeShow = beforeShow;
25
+ GroupGallery.vars.lightboxOptions.afterShow = afterShow;
26
+ GroupGallery.vars.lightboxOptions.beforeClose = beforeClose;
27
+ $.fancybox(GroupGallery.vars.lightboxImages, GroupGallery.vars.lightboxOptions);
28
+ };
29
+
30
+ function beforeShow() {
31
+ GroupGallery.comments.clear();
32
+ }
33
+
34
+ function afterShow() {
35
+ var id = parseInt(GroupGallery.vars.idLookup[this.index], 10);
36
+ if (!isNaN(id)) {
37
+ var _setDimension = $.fancybox._setDimension;
38
+ $.fancybox._setDimension = function() {
39
+ _setDimension();
40
+ GroupGallery.comments.load(id);
41
+ $.fancybox._setDimension = _setDimension;
42
+ };
43
+
44
+ unbindEvents.apply(this);
45
+ bindEvents.apply(this, [id]);
46
+ }
47
+ }
48
+
49
+ function beforeClose() {
50
+ unbindEvents.apply(this);
51
+ }
52
+
53
+ function removeImage(id) {
54
+ if (!isNaN(id)) {
55
+ socket.emit('plugins.group-gallery.removeImage', {imageId: id});
56
+ }
57
+ }
58
+
59
+ function bindEvents(id) {
60
+ var removeImageButton = $('[data-func="group-gallery.remove"]'),
61
+ self = this;
62
+
63
+ if (parseInt(GroupGallery.vars.groupImages[this.index].uid, 10) === parseInt(app.user.uid, 10) || app.user.isAdmin) {
64
+ removeImageButton.removeClass('hidden');
65
+ } else {
66
+ removeImageButton.addClass('hidden');
67
+ }
68
+
69
+ removeImageButton.on('click.group-gallery', function(e) {
70
+ removeImage.apply(self, [id]);
71
+
72
+ e.preventDefault();
73
+ return false;
74
+ });
75
+
76
+ socket.on(socketEvents.removeImage, function(imageId) {
77
+ var index = GroupGallery.vars.indexLookup[parseInt(imageId, 10)];
78
+
79
+ if (!isNaN(index)) {
80
+ // Remove image from the image list, reindex them
81
+ GroupGallery.vars.groupImages.splice(index, 1);
82
+ GroupGallery.indexImages();
83
+ self.group.splice(index, 1);
84
+
85
+ if (parseInt(imageId, 10) === id) {
86
+ if (self.group.length < 1) {
87
+ $.fancybox.close();
88
+ } else {
89
+ // We also removed it from the fancybox list, so the same index will be the next image
90
+ $.fancybox.jumpto(index);
91
+ }
92
+ }
93
+ }
94
+ });
95
+
96
+ GroupGallery.comments.bindEvents();
97
+ }
98
+
99
+ function unbindEvents() {
100
+ var removeImageButton = $('[data-func="group-gallery.remove"]');
101
+
102
+ removeImageButton.off('click.group-gallery');
103
+
104
+ socket.off(socketEvents.removeImage);
105
+
106
+ GroupGallery.comments.unbindEvents();
107
+ }
108
+
109
+ GroupGallery.modal = Modal;
110
+
111
+ })(GroupGallery);