-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommunitere.js
124 lines (99 loc) · 3.27 KB
/
communitere.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
Messages = new Meteor.Collection('messages');
Agencies = new Meteor.Collection('agencies');
var testAgencies = [
{name: 'Red Cross'},
{name: 'Save the Children'}
];
var testMessagesGenerator = {
'verbs' : ['need', 'request for', 'complaint about', 'poor quality'],
'things' : ['water', 'food', 'shelter', 'blankets'],
'locations' : ['tacloban', 'manila', 'cebu', 'boracay']
};
var categories = ['water', 'food', 'shelter', 'complaint'];
if (Meteor.isClient) {
Template.main.openMessages = function() {
return Messages.find({status: {$in: ['inprogress', 'new']}});
}
Template.main.doneMessages = function() {
return Messages.find({status: 'done'});
}
Template.main.events = {
'click tr.message' : function(evt) {
// NB: this has to account for multiple clients to avoid people working on the same ticket
var msgId = this._id;
// Unset the previous inprogress message
var prevMsg = Messages.findOne(Session.get('currentMessageId'));
if (prevMsg && prevMsg.status == 'inprogress') {
Messages.update(prevMsg._id, {$set: {status: 'new'}});
}
// Update the current
Session.set('currentMessageId', msgId);
Messages.update(msgId, {$set: {status: 'inprogress'}});
}
};
Template.main.helpers({
'currentMsgClass' : function(msg) {
return this._id == Session.get('currentMessageId') ? 'current-msg' : '';
}
})
Template.currentMessage.message = function() {
return Messages.findOne(Session.get('currentMessageId'));
}
Template.currentMessage.events = {
'submit form' : function(evt) {
var form = $(evt.target).closest('form');
var obj = {};
$.each(form.serializeArray(), function() {
obj[this.name] = this.value;
});
// Set done and write the blob to the current ticket
Messages.update(Session.get('currentMessageId'), {$set: {details: obj, status: 'done'}});
// Move to next message
var nextMsg = Messages.findOne({status: 'new'});
if (nextMsg) {
console.log('ere');
Session.set('currentMessageId', nextMsg._id);
Messages.update(nextMsg._id, {$set: {status: 'inprogress'}});
}
return false;
}
}
Template.currentMessage.allCategories = function() { return categories };
Template.debug.events({
'click .msgGen' : function() {
var msg = {};
msg['body'] = testMessagesGenerator['verbs'][_.random(0,3)] + ' ' + testMessagesGenerator['things'][_.random(0,3)] + ' in ' + testMessagesGenerator['locations'][_.random(0,3)];
msg['callback'] = '0917' + _.random(0,999999);
msg['received'] = _.random(0,999);
msg['status'] = 'new';
Messages.insert(msg);
},
'click .msgClear' : function() {
Messages.find().forEach(function(m) {Messages.remove(m._id)});
}
})
Template.agencyAutoComplete.settings = function() {
return {
position: "top",
limit: 5,
rules: [
{
token: "@",
collection: Agencies,
field: "name",
template: Template.autoCompletePill
}
]
}
};
}
if (Meteor.isServer) {
Meteor.startup(function () {
var agencies = Agencies.find();
if (agencies.count() == 0) {
_.each(testAgencies, function(agency) {
Agencies.insert(agency);
});
}
});
}