Skip to content

Commit 05cfe63

Browse files
author
Ville Jyrkkä
authored
Merge pull request #1928 from apinf/feature/cover-photo
Reintroduce cover photo
2 parents c0429cd + 937f963 commit 05cfe63

File tree

27 files changed

+306
-5
lines changed

27 files changed

+306
-5
lines changed

branding/client/branding.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ <h3 class="panel-title">
1212
</div>
1313
<div class="panel-body">
1414
{{# if branding }}
15-
{{> viewProjectLogo branding=branding }}
16-
{{> uploadProjectLogo branding=branding }}
15+
{{> viewProjectLogo branding=branding }}
16+
{{> uploadProjectLogo branding=branding }}
17+
{{> viewCoverPhoto branding=branding }}
18+
{{> uploadCoverPhoto branding=branding }}
1719
{{# autoForm
1820
id="brandingEdit"
1921
type="update"

branding/collection/schema.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Branding.schema = new SimpleSchema({
66
type: String,
77
optional: true,
88
},
9+
coverPhotoFileId: {
10+
type: String,
11+
optional: true,
12+
},
913
colors: {
1014
type: Object,
1115
optional: true,
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
// Meteor package import
2+
import { Meteor } from 'meteor/meteor';
3+
// Apinf collections import
14
import { Branding } from '/branding/collection';
25

36
Meteor.publish('branding', function() {
47
// Get Branding collection object
5-
return Branding.find({});
8+
return Branding.find();
69
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Import meteor packages
2+
import { Meteor } from 'meteor/meteor';
3+
import { sAlert } from 'meteor/juliancwirko:s-alert';
4+
import { TAPi18n } from 'meteor/tap:i18n';
5+
6+
// Import apinf collections
7+
import { Branding } from '/branding/collection';
8+
import CoverPhoto from '/branding/cover_photo/collection';
9+
import { fileNameEndsWith } from '/core/helper_functions/file_name_ends_with';
10+
11+
Meteor.startup(function () {
12+
// Set cover photo id to branding collection on Success
13+
CoverPhoto.resumable.on('fileSuccess', function (file) {
14+
15+
// Get the id from project logo file object
16+
const coverPhotoFileId = file.uniqueIdentifier;
17+
18+
// Get branding
19+
const branding = Branding.findOne();
20+
21+
// Update logo id field
22+
Branding.update(branding._id, { $set: { coverPhotoFileId } });
23+
24+
// Get upload success message translation
25+
const message = TAPi18n.__('uploadCoverPhoto_successfully_uploaded');
26+
27+
// Alert user of successful upload
28+
sAlert.success(message);
29+
});
30+
31+
CoverPhoto.resumable.on('fileAdded', function (file) {
32+
return CoverPhoto.insert({
33+
_id: file.uniqueIdentifier,
34+
filename: file.fileName,
35+
contentType: file.file.type,
36+
}, function (err) {
37+
if (err) {
38+
// Create & show a message about failed
39+
const message = `${TAPi18n.__('uploadCoverPhoto_acceptedExtensions_errorText')} ${err}`;
40+
sAlert.warning(message)
41+
return;
42+
}
43+
44+
// Available extensions for pictures
45+
const acceptedExtensions = ['jpg', 'jpeg', 'png', 'gif'];
46+
47+
// Check extensions for uploading file: is it a picture or not?
48+
if (fileNameEndsWith(file.file.name, acceptedExtensions)) {
49+
// Upload the cover photo
50+
return CoverPhoto.resumable.upload();
51+
}
52+
53+
// Get extension error message
54+
const message = TAPi18n.__('uploadCoverPhoto_acceptedExtensions');
55+
56+
// Alert user of extension error
57+
sAlert.error(message);
58+
});
59+
});
60+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.cover-photo-file {
2+
margin-bottom: 1em;
3+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<template name="uploadCoverPhoto">
2+
<div class="cover-photo-file">
3+
{{# unless uploadedCoverPhotoFile }}
4+
{{> uploadCoverPhotoButton }}
5+
{{ else }}
6+
<ul class="list-group">
7+
<li class="list-group-item">
8+
<i class="fa fa-file-text-o" aria-hidden="true"></i>
9+
{{ uploadedCoverPhotoFile.filename }}
10+
<span class="pull-right">
11+
<button class="btn btn-xs btn-danger delete-cover-photo">
12+
<i class="fa fa-trash-o fa-lg" aria-hidden="true"></i>
13+
</button>
14+
</span>
15+
</li>
16+
</ul>
17+
{{/ unless }}
18+
</div>
19+
</template>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Import meteor packages
2+
import { Template } from 'meteor/templating';
3+
import { Mongo } from 'meteor/mongo';
4+
import { ReactiveVar } from 'meteor/reactive-var';
5+
import { TAPi18n } from 'meteor/tap:i18n';
6+
import { sAlert } from 'meteor/juliancwirko:s-alert';
7+
// Import apinf collections
8+
import { Branding } from '/branding/collection';
9+
import CoverPhoto from '/branding/cover_photo/collection';
10+
11+
Template.uploadCoverPhoto.onCreated(function () {
12+
const instance = this;
13+
14+
// Subscribe to Branding collection
15+
instance.subscribe('branding');
16+
// Subscribe to Cover Photo collection
17+
instance.subscribe('coverPhoto');
18+
});
19+
20+
Template.uploadCoverPhoto.helpers({
21+
uploadedCoverPhotoFile () {
22+
// Get cover photo ID
23+
const currentCoverPhotoFileId = this.branding.coverPhotoFileId;
24+
25+
// Convert to Mongo ObjectID
26+
const objectId = new Mongo.Collection.ObjectID(currentCoverPhotoFileId);
27+
28+
// Check if cover photo file is available
29+
return CoverPhoto.findOne(objectId);
30+
},
31+
});
32+
33+
Template.uploadCoverPhoto.events({
34+
'click .delete-cover-photo': function () {
35+
// Show confirmation dialog to user
36+
const confirmation = confirm(TAPi18n.__('uploadCoverPhoto_confirm_delete'));
37+
38+
// Check if user clicked "OK"
39+
if (confirmation) {
40+
// Get cover photo file id from branding
41+
const coverPhotoFileId = this.branding.coverPhotoFileId;
42+
43+
// Convert to Mongo ObjectID
44+
const objectId = new Mongo.Collection.ObjectID(coverPhotoFileId);
45+
46+
// Remove the cover photo object
47+
CoverPhoto.remove(objectId);
48+
49+
// Remove the cover photo file id field
50+
Branding.update(this.branding._id, { $unset: { coverPhotoFileId: '' } });
51+
52+
// Get deletion success message translation
53+
const message = TAPi18n.__('uploadCoverPhoto_successfully_deleted');
54+
55+
// Alert user of successful delete
56+
sAlert.success(message);
57+
}
58+
},
59+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<template name="uploadCoverPhotoButton">
2+
<a class="btn btn-primary" id="cover-photo-browse">
3+
{{_ 'uploadCoverPhotoButton_textButton' }}
4+
</a>
5+
</template>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Import meteor packages
2+
import { Template } from 'meteor/templating';
3+
// Import apinf collections
4+
import CoverPhoto from '/branding/cover_photo/collection';
5+
6+
Template.uploadCoverPhotoButton.onRendered(function() {
7+
// Assign resumable browse to element
8+
CoverPhoto.resumable.assignBrowse($('#cover-photo-browse'));
9+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.view-cover-photo {
2+
width: 140px;
3+
margin-bottom: 1em;
4+
}

0 commit comments

Comments
 (0)