forked from belisarius222/Meteor-handlebar-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.operators.js
140 lines (111 loc) · 3.63 KB
/
helpers.operators.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
// Forward compability
if (typeof UI === 'undefined' || typeof UI.registerHelper !== 'function') {
UI = {
registerHelper: function(name, f) {
if (typeof Handlebars !== 'undefined') {
return Handlebars.registerHelper(name, f);
} else {
throw new Error('No UI or Handlebars found');
}
}
};
}
if (typeof UI !== 'undefined') {
UI.registerHelper('getLength', function (a) {
return a && a.length;
});
UI.registerHelper('isSelected', function (a, b) {
return (a === b) ? { selected: 'selected' } : null;
});
UI.registerHelper('isChecked', function (a, b) {
return (a === b) ? { checked: 'checked' } : null;
});
UI.registerHelper('cutString', function (str, len) {
return (str.length > len)?str.substr(0, Math.max(len-3, 0))+'...':str;
});
UI.registerHelper('$eq', function (a, b) {
return (a === b); //Only text, numbers, boolean - not array & objects
});
UI.registerHelper('$neq', function (a, b) {
return (a !== b); //Only text, numbers, boolean - not array & objects
});
UI.registerHelper('$in', function (a, b, c, d) {
return ( a === b || a === c || a === d);
});
UI.registerHelper('$nin', function (a, b, c, d) {
return ( a !== b && a !== c && a !== d);
});
UI.registerHelper('$exists', function (a) {
return ( a !== undefined);
});
UI.registerHelper('$lt', function (a, b) {
return (a < b);
});
UI.registerHelper('$gt', function (a, b) {
return (a > b);
});
UI.registerHelper('$lte', function (a, b) {
return (a <= b);
});
UI.registerHelper('$gte', function (a, b) {
return (a >= b);
});
UI.registerHelper('$and', function (a, b) {
return (a && b);
});
UI.registerHelper('$or', function (a, b) {
return (a || b);
});
UI.registerHelper('$not', function (a) {
return (!a);
});
UI.registerHelper('nl2br', function (text) {
var nl2br = (text + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + '<br>' + '$2');
return new Spacebars.SafeString(nl2br);
});
UI.registerHelper('getText', function (text, lang) { // Deprecating
var langKey = lang || null;
return Helpers.getText(text, langKey);
});
UI.registerHelper("$mapped", function(arr) {
if(!Array.isArray(arr)){
try {
arr = arr.fetch()
}
catch (e){
console.log("Error in $mapped: perhaps you aren't sending in a collection or array.")
return [];
}
}
var $length = arr.length;
var mappedArray = arr.map(function(item,index) {
item.$length = $length;
item.$index = index;
item.$first = index === 0;
item.$last = index === $length-1;
return item;
});
return mappedArray || [];
});
// UI.registerHelper('userRole', function ( /* arguments */) {
// var role = Session.get('currentRole');
// return _.any(arguments, function(value) { return (value == role); });
// });
/*
Then $uper helper - Credit goes to @belisarius222 aka Ted Blackman for sparking an idear for a solution
*/
Helpers.superScope = {};
Helpers.addScope = function(name, obj) {
// TODO: Get rid of underscore
Helpers.superScope[name] = _.bind(function() { return this; }, obj);
};
Helpers.removeScope = function(name) {
delete UI._globalHelpers[name];
delete Helpers.superScope[name];
};
Helpers.addScope('Session', Session);
Helpers.addScope('Meteor', Meteor);
UI.registerHelper('$', function() {
return Helpers.superScope;
});
}