-
Notifications
You must be signed in to change notification settings - Fork 2
/
component.coffee
360 lines (302 loc) · 11.5 KB
/
component.coffee
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# @nodoc
expirationMsFromDuration = (duration) ->
# Default values from moment/src/lib/duration/humanize.js.
thresholds =
s: 45 # seconds to minute
m: 45 # minutes to hour
h: 22 # hours to day
seconds = Math.round(duration.as 's')
minutes = Math.round(duration.as 'm')
hours = Math.round(duration.as 'h')
if seconds < thresholds.s
(thresholds.s - seconds) * 1000 + 500
else if minutes < thresholds.m
(60 - seconds % 60) * 1000 + 500
else if hours < thresholds.h
((60 * 60) - seconds % (60 * 60)) * 1000 + 500
else
((24 * 60 * 60) - seconds % (24 * 60 * 60)) * 1000 + 500
# @nodoc
invalidateAfter = (expirationMs) ->
computation = Tracker.currentComputation
handle = Meteor.setTimeout =>
computation.invalidate()
,
expirationMs
computation.onInvalidate =>
Meteor.clearTimeout handle if handle
handle = null
# A base class for components with additional methods for various useful features.
#
# In addition to methods/template helpers available when using this class as a base
# class, [`insertDOMElement`](https://github.com/peerlibrary/meteor-blaze-components#user-content-reference_instance_insertDOMElement),
# [`moveDOMElement`](https://github.com/peerlibrary/meteor-blaze-components#user-content-reference_instance_moveDOMElement),
# and [`removeDOMElement`](https://github.com/peerlibrary/meteor-blaze-components#user-content-reference_instance_removeDOMElement) have been
# configured to call corresponding methods in mixins, if they exist, as it is
# described [in Blaze Components documentation](https://github.com/peerlibrary/meteor-blaze-components#animations).
# This allows you to use mixins which add animations to your components.
class CommonComponent extends share.CommonComponentBase
# @nodoc
insertDOMElement: (parent, node, before, next) ->
next ?= =>
super parent, node, before
true
return next() unless @callFirstWith @, 'insertDOMElement', parent, node, before, next
# It has been handled.
true
# @nodoc
moveDOMElement: (parent, node, before, next) ->
next ?= =>
super parent, node, before
true
return next() unless @callFirstWith @, 'moveDOMElement', parent, node, before, next
# It has been handled.
true
# @nodoc
removeDOMElement: (parent, node, next) ->
next ?= =>
super parent, node
true
return next() unless @callFirstWith @, 'removeDOMElement', parent, node, next
# It has been handled.
true
# Template helper which resolves [Flow Router](https://github.com/kadirahq/flow-router) path definition and arguments to
# URL paths using [`FlowRouter.path`](https://github.com/kadirahq/flow-router#flowrouterpathpathdef-params-queryparams).
# It works when Flow Router package is available.
#
# @example
# {{pathFor 'Post.edit' params=data}}
#
# @param [String] pathName Path name or path definition.
# @param [Object] kwargs
# @option kwargs [Object] params Parameters to resolve variables in the path.
# @option kwargs [Object] query Query string values to be added to the URL path.
# @return [String]
pathFor: (pathName, kwargs) ->
kwargs = kwargs.hash if kwargs instanceof Spacebars.kw
params = kwargs?.params or {}
queryParams = kwargs?.query or {}
FlowRouter = Package['peerlibrary:flow-router']?.FlowRouter or Package['kadira:flow-router']?.FlowRouter
throw new Error "FlowRouter package missing." unless FlowRouter
FlowRouter.path pathName, params, queryParams
# Returns the [`Meteor.userId()`](http://docs.meteor.com/#/full/meteor_users) value.
# Use it instead of [`currentUser`](http://docs.meteor.com/#/full/template_currentuser) template helper when you want
# to check only if user is logged in or not.
#
# @example
# {{#if currentUserId}}
# ...
# {{/if}}
#
# @return [String]
currentUserId: ->
Meteor.userId()
# Extended version of [`currentUser`](http://docs.meteor.com/#/full/template_currentuser) template helper which
# can optionally limit fields returned in the user object. This limits template helper's reactivity as well.
# It works when [peerlibrary:user-extra](https://github.com/peerlibrary/meteor-user-extra) package is available
# and falls back to old behavior if it is not.
#
# @param [String] userId
# @param [Object] fields [MongoDB fields specifier](http://docs.meteor.com/#/full/fieldspecifiers).
# @return [Object]
currentUser: (userId, fields) ->
if not fields and _.isObject userId
fields = userId
userId = null
fields = fields.hash if fields instanceof Spacebars.kw
Meteor.user userId, fields
# Returns `true` if any of the arguments is true.
#
# @example
# {{#if $or isAdmin isModerator}}
# ...
# {{/if}}
#
# @return [Boolean]
$or: (args...) ->
# Removing kwargs.
args.pop() if args[args.length - 1] instanceof Spacebars.kw
_.some args
# Returns `true` if all of the arguments are true.
#
# @example
# {{#if $and currentUserId subscriptionReady}}
# ...
# {{/if}}
#
# @return [Boolean]
$and: (args...) ->
# Removing kwargs.
args.pop() if args[args.length - 1] instanceof Spacebars.kw
_.every args
# Returns `true` if the first argument is false, `false` otherwise.
#
# @example
# {{#if $not isRobot}}
# ...
# {{/if}}
#
# @return [Boolean]
$not: (args...) ->
# Removing kwargs.
args.pop() if args[args.length - 1] instanceof Spacebars.kw
not args[0]
# Joins arguments using the `delimiter`.
#
# @example
# {{> EditorComponent args id=($join '-' 'edit-body' _id)}}
#
# @param [String] delimiter
# @return [String]
$join: (delimiter, args...) ->
# Removing kwargs.
args.pop() if args[args.length - 1] instanceof Spacebars.kw
args.join delimiter
# @property [String] Default localized date-time format. Example: `Thu, Sep 4 1986 8:30 PM`.
DEFAULT_DATETIME_FORMAT:
'llll'
# @property [String] Default localized date format. Example: `Sep 4 1986`.
DEFAULT_DATE_FORMAT:
'll'
# @property [String] Default localized time format. Example: `8:30 PM`.
DEFAULT_TIME_FORMAT:
'LT'
# [Format](http://momentjs.com/docs/#/displaying/format/) a datetime using provided `format` string.
#
# @example
# {{formatDate createdAt DEFAULT_DATETIME_FORMAT}}
#
# @param [Date] date
# @param [String] format
# @return [String]
formatDate: (date, format) ->
format = null if format instanceof Spacebars.kw
moment(date).format format
# Reactively format a datetime into a relative from now and localized string. As times progresses, string is
# automatically updated. Strings are made using [moment.js `fromNow` function](http://momentjs.com/docs/#/displaying/fromnow/).
#
# Example output: `3 months ago`.
#
# @example
# <span class="timestamp" title="{{formatDate createdAt DEFAULT_DATETIME_FORMAT}}">{{fromNow createdAt}}</span>
#
# @param [Date] date
# @param [Boolean] withoutSuffix Should `ago` suffix be omitted, default is `false`.
# @return [String]
fromNow: (date, withoutSuffix) ->
withoutSuffix = false if withoutSuffix instanceof Spacebars.kw
momentDate = moment(date)
if Tracker.active
absoluteDuration = moment.duration(to: momentDate, from: moment()).abs()
expirationMs = expirationMsFromDuration absoluteDuration
invalidateAfter expirationMs
momentDate.fromNow withoutSuffix
# Format a datetime into a relative from now and localized string using friendly day names.
# Strings are made using [moment.js `calendar` function](http://momentjs.com/docs/#/displaying/calendar-time/).
#
# Example output: `last Sunday at 2:30 AM`.
#
# @example
# <span title="{{formatDate playStart DEFAULT_DATETIME_FORMAT}}">{{calendarDate playStart}}</span>
#
# @param [Date] date
# @return [String]
calendarDate: (date) ->
moment(date).calendar null,
lastDay: '[yesterday at] LT',
sameDay: '[today at] LT',
nextDay: '[tomorrow at] LT',
lastWeek: '[last] dddd [at] LT',
nextWeek: 'dddd [at] LT',
sameElse: @DEFAULT_DATETIME_FORMAT
# Similar to [moment.js `humanize` function](http://momentjs.com/docs/#/durations/humanize/) it returns
# a friendly string representing the duration.
#
# It is build from `size` number of units. For example, for `size` equals 2, the string could be `2 days 1 hour`.
# For `size` equals 3, `2 days 1 hour 44 minutes`. If you omit `size`, full precision is used.
#
# If `from` or `to` are `null`, the output is reactive.
#
# @example
# <span title="{{formatDuration startedAt endedAt}}">{{formatDuration startedAt endedAt 2}}</span>
#
# @example
# <span title="{{formatDuration startedAt null}}">{{formatDuration startedAt null 3}}</span>
#
# @param [Date] from If `null`, current time is used.
# @param [Date] to If `null`, current time is used.
# @param [Number] size Duration description precision: from how many units it should be build.
# @return [String]
# @todo Support localization.
formatDuration: (from, to, size) ->
size = null if size instanceof Spacebars.kw
reactive = not (from and to)
from ?= new Date()
to ?= new Date()
duration = moment.duration({from, to}).abs()
minutes = Math.round(duration.as 'm') % 60
hours = Math.round(duration.as 'h') % 24
days = Math.round(duration.as 'd') % 7
weeks = Math.floor(Math.round(duration.as 'd') / 7)
partials = [
key: 'week'
value: weeks
,
key: 'day'
value: days
,
key: 'hour'
value: hours
,
key: 'minute'
value: minutes
]
# Trim zero values from the left.
while partials.length and partials[0].value is 0
partials.shift()
# Cut the length to provided size.
partials = partials[0...size] if size
if reactive and Tracker.active
seconds = Math.round(duration.as 's')
if partials.length
lastPartial = partials[partials.length - 1].key
if lastPartial is 'minute'
expirationMs = (60 - seconds % 60) * 1000 + 500
else if lastPartial is 'hour'
expirationMs = ((60 * 60) - seconds % (60 * 60)) * 1000 + 500
else
expirationMs = ((24 * 60 * 60) - seconds % (24 * 60 * 60)) * 1000 + 500
else
assert seconds < 60, seconds
expirationMs = (60 - seconds) * 1000 + 500
invalidateAfter expirationMs
partials = for {key, value} in partials
# Maybe there are some zero values in-between, skip them.
continue if value is 0
key = "#{key}s" if value isnt 1
"#{value} #{key}"
if partials.length
partials.join ' '
else
"less than a minute"
# Returns the CSS prefix used by the current browser.
#
# @return [String]
cssPrefix: ->
unless '_cssPrefix' of @
styles = window.getComputedStyle document.documentElement, ''
@_cssPrefix = (_.toArray(styles).join('').match(/-(moz|webkit|ms)-/) or (styles.OLink is '' and ['', 'o']))[1]
@_cssPrefix
# Construct a `Date` object from inputs of HTML5 form fields of type `date` and `time`.
#
# @example
# this.constructDatetime(this.$('[name="start-date"]').val(), this.$('[name="start-time"]').val())
#
# @param [String] date
# @param [String] time
# @return [Date]
constructDatetime: (date, time) ->
# TODO: Make a warning or something?
throw new Error "Both date and time fields are required together." if (date and not time) or (time and not date)
return null unless date and time
moment("#{date} #{time}", 'YYYY-MM-DD HH:mm').toDate()