-
Notifications
You must be signed in to change notification settings - Fork 4
/
templates.js
168 lines (159 loc) · 5.51 KB
/
templates.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
function colorize(str) {
return !!Constellation && Package["constellation:console"].Constellation.colorize(str) || str;
}
Template.registerHelper('ddpInspectorMessageStr', function () {
return (Template.parentData(1) === 'constellation_plugin_ddp-inspector') ? colorize(this.messageStr) : this.messageStr;
});
Template[DDP_INSPECTOR_PREFIX].created = function () {
var self = this;
self.messages = [];
self.autorun(function () {
UpdatePanelTracker.depend();
var criteria = {};
var search = Session.get(DDP_INSPECTOR_SEARCH_INPUT);
if (search) {
try {
criteria.messageStr = {
$regex: new RegExp(search, 'i')
};
} catch (e) {
// If for some reason the regex is messed up just fall back to this
criteria.messageStr = search;
}
}
if (Session.equals(DDP_INSPECTOR_SUPPRESS_PING, true)) {
criteria['message.msg'] = {
$nin: ['ping', 'pong']
};
}
var sessionLimit = Session.get(DDP_INSPECTOR_SEARCH_LIMIT);
var limit = typeof sessionLimit === 'number' ? sessionLimit : 50;
self.messages = DDPMessages.find(criteria, {
sort: {
__order: -1
},
limit: limit,
reactive: false
}).fetch();
});
};
Template[DDP_INSPECTOR_PREFIX].rendered = function () {
this.find(DDP_INSPECTOR_PANEL_ID)._uihooks = {
insertElement: function (node, next) {
$(node).addClass('inserted').insertBefore(next);
Meteor.setTimeout(function () {
$(node).removeClass('inserted');
}, 20);
},
}
};
Template[DDP_INSPECTOR_PREFIX].helpers({
ddpMessages: function () {
UpdatePanelTracker.depend();
// No other way to trigger reactivity for the other session vars
Session.get(DDP_INSPECTOR_SEARCH_LIMIT);
Session.get(DDP_INSPECTOR_SUPPRESS_PING);
return Template.instance().messages;
},
whichDDPTemplate: function () {
var message = this.message;
if (typeof message.collection !== 'undefined') {
return Template[DDP_INSPECTOR_COLLECTIONS_TEMPLATE];
} else if (message.msg === 'sub' || (message.msg === 'ready' && message.subs)) {
return Template[DDP_INSPECTOR_SUBS_TEMPLATE];
} else if (typeof message.method !== 'undefined' || (message.msg === 'updated' && message.methods) || (message.msg === 'result')) {
return Template[DDP_INSPECTOR_METHODS_TEMPLATE];
} else {
return Template[DDP_INSPECTOR_UNKNOWN_TEMPLATE];
}
},
searchTemplate: function () {
return Template[DDP_INSPECTOR_SEARCH_TEMPLATE];
},
constellation: function () {
// Widget needs to know whether it's standalone or in Constellation context
// So it can shift the search and reset inputs off to the Constellation menu bar
return !!Constellation && _.isString(this) && String(this) === 'constellation_plugin_ddp-inspector';
}
});
Template[DDP_INSPECTOR_SEARCH_TEMPLATE].helpers({
searchInput: function () {
return Session.get(DDP_INSPECTOR_SEARCH_INPUT);
}
});
Template[DDP_INSPECTOR_SEARCH_TEMPLATE].events({
'keyup .ddp-inspector-search': _.throttle(function (event, template) {
Session.setPersistent(DDP_INSPECTOR_SEARCH_INPUT, event.target.value);
UpdatePanelTracker.changed();
}, 300),
'click .ddp-inspector-reset': function () {
DDPMessages.remove({});
UpdatePanelTracker.changed();
// Clear search field
// otherwise user who has a current search term may be confused when they press reset
// then take a few actions and don't see any messages appearing in their ddp inspector
// Balancing this against the possibility of a user wanting to keep a search term in play
// after resetting and performing some specific actions that might produce that
// same search term in the DDP messages
$('.ddp-inspector-search').val('');
Session.setPersistent(DDP_INSPECTOR_SEARCH_INPUT, '');
}
});
Template[DDP_INSPECTOR_SUBS_TEMPLATE].helpers({
subscriptionName: function () {
var message = this.message;
if (message.name) {
return message.name;
} else if (message.subs) {
return DDPMessages.find({
'message.msg': 'sub',
'message.id': {
$in: message.subs
}
}, {
limit: message.subs.length,
reactive: false
}).map(function (msg) {
return msg.message.name;
});
}
}
});
Template[DDP_INSPECTOR_METHODS_TEMPLATE].helpers({
methodName: function () {
var message = this.message;
if (message.method) {
return message.method;
} else if (message.methods) {
return DDPMessages.find({
'message.msg': 'method',
'message.id': {
$in: message.methods
}
}, {
limit: message.methods.length,
reactive: false
}).map(function (msg) {
return msg.message.method;
});
} else if (message.msg === 'result' && message.id) {
var ddpMessage = DDPMessages.findOne({
'message.msg': 'method',
'message.id': message.id
}, {
reactive: false
});
if (ddpMessage) {
return ddpMessage.message.method;
} else if (MethodNameCache.hasOwnProperty(message.id)) {
// it is possible that the method call was not registered by ddp-inspector.
// this happens when the method was called berofe ddp-inspector had the chance to wrap _send().
// if the method had a callback, we cached the method name earlier...
return MethodNameCache[message.id];
} else {
// ...otherwise, we don't know the method name based on the id.
return '(unknown method)';
}
}
}
});