-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhelpers.js
More file actions
98 lines (78 loc) · 2.68 KB
/
helpers.js
File metadata and controls
98 lines (78 loc) · 2.68 KB
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
if (Meteor.is_client){
var lastDate;
Handlebars.registerHelper('ifDateChanged', function(date, options){
if (date){
var d1 = new Date(date);
}
if (lastDate){
var d2 = new Date(lastDate);
}
lastDate = date;
if((d1 && d2 && (d1.getFullYear() != d2.getFullYear() || d1.getMonth() != d2.getMonth() || d1.getDate() != d2.getDate()))){
return options.fn(this);
}
else{
return options.inverse(this);
}
});
Handlebars.registerHelper('lastMessageDate', function(options){
var msg = Messages.findOne({}, {sort: {time: 1}});
if (msg){
return options.fn({time: msg.time});
}
else{
return options.inverse(this);
}
});
Handlebars.registerHelper('dateString', function(val, options) {
if (!val) return "";
var d = new Date(val);
return d.toLocaleDateString();
});
Handlebars.registerHelper('datePlusOneString', function(val, options) {
if (!val) return "";
var d = new Date(val);
d.setDate(d.getDate()+1);
return d.toLocaleDateString();
});
Handlebars.registerHelper('timeString', function(val, options) {
if (!val) return "";
var d = new Date(val);
var hours = d.getHours();
if (hours < 10){
hours = "0"+hours;
}
var minutes = d.getMinutes();
if (minutes < 10){
minutes = "0"+minutes;
}
return hours+":"+minutes;
});
Handlebars.registerHelper('linkifyText', function(options) {
var val = this["text"];
if (!val) return "";
val = Handlebars._escape(val);
return Meteor.ui.chunk(function(){
var replaceText, replacePattern1, replacePattern2, replacePattern3;
//URLs starting with http://, https://, or ftp://
replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
replacedText = val.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');
//URLs starting with "www." (without // before it, or it'd re-link the ones done above).
replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');
//Change email addresses to mailto:: links.
replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');
return replacedText;
});
});
Handlebars.registerHelper('iflt', function(lvalue, rvalue, options) {
if (arguments.length < 3)
throw new Error("Handlebars Helper iflessthen needs 2 parameters");
if( lvalue < rvalue ) {
return options.inverse(this);
} else {
return options.fn(this);
}
});
}