-
Notifications
You must be signed in to change notification settings - Fork 1
/
banner.js
146 lines (120 loc) · 4.36 KB
/
banner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
_____ _________________________________________ __________
/ \ \_ _____/\__ ___/\_ _____/\_____ \ \______ \
/ \ / \ | __)_ | | | __)_ / | \ | _/
/ Y \ | \ | | | \/ | \ | | \
\____|__ //_______ / |____| /_______ /\_______ / |____|_ /
\/ \/ \/ \/ \/
_____ _________________________________ ____ ___ __________
/ \ \_ _____/\_ _____/\__ ___/| | \\______ \
/ \ / \ | __)_ | __)_ | | | | / | ___/
/ Y \ | \ | \ | | | | / | |
\____|__ //_______ //_______ / |____| |______/ |____|
\/ \/ \/
__________ _____ _______ _______ _____________________
\______ \ / _ \ \ \ \ \ \_ _____/\______ \
| | _/ / /_\ \ / | \ / | \ | __)_ | _/
| | \/ | \/ | \/ | \ | \ | | \
|______ /\____|__ /\____|__ /\____|__ //_______ / |____|_ /
\/ \/ \/ \/ \/ \/
*/
Banners = new Mongo.Collection('banners')
Banners.allow({
insert: function () { return true },
update: function (userId, doc, fieldNames, modifier) {
if (fieldNames.length != 2) return false
if (fieldNames.some(function(item){ return item === 'count'}) &&
fieldNames.some(function(item){ return item === 'updatedAt'})) {
return true
}
return false
}
})
if (Meteor.isClient) {
Session.setDefault('strapline', '')
Session.setDefault('logo', false)
var logo = new Image()
logo.src = '/img/meteor-logo-light.png'
logo.onload = function(){
//TODO: Force a re-render one the image has loaded
Session.set('logo', true)
}
Template.bannerEditor.events({
'keydown input': debounce(function (evt, tpl) {
var strapline = tpl.find('.strapline-input').value
Session.set('strapline', strapline)
// console.log('keydown', strapline, tpl.find('.strapline-input'))
}, 250),
'submit': function (evt) {
evt.preventDefault()
// force people to press the button, otherwise it's too easy to spam
},
'click .btn-meteor': function (evt, tpl) {
if (!Session.get('strapline')) return;
var bannerDoc = Banners.findOne({strapline: Session.get('strapline')})
if (bannerDoc) {
Banners.update(bannerDoc._id, {$set: {'updatedAt': Date.now()}, $inc: { count: 1 } })
} else {
bannerDoc = {
strapline:Session.get('strapline'),
createdAt: Date.now(),
updatedAt: Date.now(),
count:1
}
Banners.insert(bannerDoc)
}
}
})
Template.bannerEditor.helpers({
dataUri: function(){
return drawBanner(Session.get('strapline'), Session.get('logo'))
},
filename: function () {
return 'meteor-' + Session.get('strapline').toLowerCase() + '-banner.png'
}
})
Template.recent.helpers({
banners: function () {
return Banners.find({}, {sort: [['updatedAt', 'desc']]})
},
dataUri: function (strapline) {
return drawBanner(strapline, Session.get('logo'))
}
})
function drawBanner (strapline, logoReady) {
var canvas = document.getElementById('banner-canvas')
if(!canvas) return
var ctx = canvas.getContext('2d')
// draw a black box
ctx.fillStyle = '#000000'
ctx.fillRect(0,0,canvas.width, canvas.height)
// Add the logo
ctx.drawImage(logo, (canvas.width / 2) - 121, 20)
// Add the custom strapline
ctx.textBaseline = 'top'
ctx.textAlign = 'center'
ctx.fillStyle = '#3C5471'
ctx.font = '400 30px Roboto, sans-serif'
ctx.fillText(strapline, (canvas.width / 2) + 8, 89);
// console.log('drawBanner', strapline)
return canvas.toDataURL('image/png')
}
// https://remysharp.com/2010/07/21/throttling-function-calls
function debounce(fn, delay) {
var timer = null;
return function () {
var context = this, args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
fn.apply(context, args);
}, delay);
}
}
Meteor.subscribe('banners', 3)
}
if (Meteor.isServer) {
Meteor.publish('banners', function (limit) {
check(limit, Number)
return Banners.find({}, {limit: limit, sort: [['updatedAt', 'desc']]})
})
}