-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
262 lines (240 loc) · 6.46 KB
/
index.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
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
var _ = require('@sailshq/lodash');
var async = require('async');
var moment = require('moment');
module.exports = {
name: 'apostrophe-event',
alias: 'events',
label: 'Event',
extend: 'apostrophe-pieces',
moogBundle: {
modules: ['apostrophe-events-pages', 'apostrophe-events-widgets'],
directory: 'lib/modules'
},
beforeConstruct: function(self, options) {
options.sort = {
startDate: 1,
startTime: 1
};
options.addFields = [
{
name: 'startDate',
label: 'Date',
help: 'The date, or starting date, of the event',
type: 'date',
required: true
},
{
name: 'allDay',
label: 'Is this an all day event?',
type: 'boolean',
choices: [
{
label: 'Yes',
value: true
},
{
label: 'No',
value: false,
showFields: ['startTime', 'endTime']
}
],
def: false
},
{
name: 'startTime',
label: 'Start Time',
type: 'time',
def: '09:00:00',
required: true
},
{
name: 'endTime',
label: 'End Time',
type: 'time',
def: '17:30:00',
required: true
},
{
name: 'dateType',
label: 'What type of event is this?',
help: 'Select if the event is on a single day, consecutive days, or repeats.',
type: 'select',
choices: [
{
label: 'Single Day',
value: 'single'
},
{
label: 'Consecutive Days',
value: 'consecutive',
showFields: ['endDate']
},
{
label: 'Recurring',
value: 'repeat',
showFields: ['repeatInterval', 'repeatCount']
}
],
def: 'single'
},
{
name: 'endDate',
label: 'End Date',
type: 'date'
},
{
name: 'repeatInterval',
label: 'How often does the event repeat?',
type: 'select',
choices: [
{
label: 'Every week',
value: 'weeks'
},
{
label: 'Every month',
value: 'months'
}
]
},
{
name: 'repeatCount',
label: 'How many times does it repeat?',
type: 'integer',
def: 1
}
].concat(options.addFields || []);
options.arrangeFields = options.arrangeFields || [
{
name: 'basic',
label: 'Basics',
fields: ['title', 'slug', 'startDate', 'allDay', 'startTime', 'endTime']
},
{
name: 'advanced',
label: 'Advanced',
fields: ['dateType', 'endDate', 'repeatInterval', 'repeatCount']
},
{
name: 'meta',
label: 'Meta',
fields: ['tags', 'published']
}
];
options.addColumns = [
{
name: 'startDate',
label: 'Start Date'
}
].concat(options.addColumns || []);
options.addSorts = [
{
name: 'startDate',
label: 'By Start Date',
sort: { startDate: -1 }
}
].concat(options.addSorts || []);
options.addFilters = [
{
name: 'upcoming',
choices: [
{
value: true,
label: 'Upcoming'
},
{
value: false,
label: 'Past'
},
{
value: null,
label: 'Both'
}
],
def: true
}
].concat(options.addFilters || []);
},
construct: function(self, options) {
// Run migrations:
require('./lib/migrations.js')(self, options);
// limit the results of autocomplete for joins
// so they only include upcoming events
self.extendAutocompleteCursor = function(cursor) {
return cursor.upcoming(true);
};
self.beforeSave = function(req, piece, options, callback) {
self.denormalizeDatesAndTimes(piece);
return callback(null);
};
self.afterSave = function(req, piece, options, callback) {
if (piece._workflowPropagating) {
// Workflow is replicating this but also its existing
// scheduled repetitions, don't re-replicate them and cause problems
return callback(null);
}
if (piece.dateType !== 'repeat') {
return callback(null);
}
self
.find(req, { parentId: piece.workflowGuid, trash: false }, { _id: 1 })
.toArray(function(err, docs) {
if (err) {
return callback(err);
}
if (!docs.length) {
// Replicate event only if it has no replicated events already
return self.repeatEvent(req, piece, callback);
}
callback(null);
});
};
self.denormalizeDatesAndTimes = function(piece) {
// Parse our dates and times
var startTime = piece.startTime;
var startDate = piece.startDate;
var endTime = piece.endTime;
var endDate;
if (piece.dateType === 'consecutive') {
endDate = piece.endDate;
} else {
piece.endDate = piece.startDate;
endDate = piece.startDate;
}
if (piece.allDay) {
startTime = '00:00:00';
endTime = '23:59:59';
}
if (piece.dateType === 'repeat') {
piece.hasClones = true;
}
piece.start = new Date(startDate + ' ' + startTime);
piece.end = new Date(endDate + ' ' + endTime);
};
self.repeatEvent = function(req, piece, finalCallback) {
var i;
var repeat = parseInt(piece.repeatCount) + 1;
var multiplier = piece.repeatInterval;
var addDates = [];
for (i = 1; i < repeat; i++) {
addDates.push(moment(piece.startDate).add(i, multiplier).format('YYYY-MM-DD'));
}
return async.eachLimit(addDates, 5, function(date, callback) {
var eventCopy = _.cloneDeep(piece);
eventCopy._id = self.apos.utils.generateId();
if (piece.workflowGuid) {
eventCopy.workflowGuid = self.apos.utils.generateId();
}
eventCopy.parentId = piece.workflowGuid;
eventCopy.isClone = true;
eventCopy.hasClones = false;
eventCopy.startDate = date;
eventCopy.endDate = date;
eventCopy.slug = eventCopy.slug + '-' + date;
eventCopy.dateType = 'single';
self.denormalizeDatesAndTimes(eventCopy);
return self.insert(req, eventCopy, callback);
}, finalCallback);
};
}
};