This repository has been archived by the owner on Jan 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
services.js
290 lines (260 loc) · 7.8 KB
/
services.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Services -------------------------------------------------------------------
angular.module('myApp.services', ['ngResource'])
.constant('config', window.eventsConfig)
.constant('analytics', window.analytics)
.factory('loadGoogleMaps', ['$window',
function ($window) {
function initialize(callback) {
$window.onInit = callback;
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://maps.googleapis.com/maps/api/js?libraries=places&sensor=false&' +
'callback=onInit';
document.body.appendChild(script);
}
return {
ready: function (callback) {
if ($window.google) {
callback();
} else {
initialize(callback);
}
}
};
}
])
.factory('eventService', ['$rootScope', '$resource', 'config',
function ($rootScope, $resource, config) {
return function (customHeaders) {
return $resource(config.eventsLocation + '/events/:id', {
organizerId: '@organizerId',
userId: '@userId',
after: '@after',
dedupe: '@dedupe',
tag: '@tag'
}, {
get: {
method: 'GET',
withCredentials: true
},
query: {
method: 'GET',
isArray: true,
withCredentials: true,
headers: customHeaders || null
},
save: {
method: 'POST',
withCredentials: true
},
'delete': {
method: 'DELETE',
withCredentials: true
},
update: {
method: 'PUT',
withCredentials: true
}
});
};
}
])
.factory('tokenService', ['$rootScope', '$resource', 'config',
function ($rootScope, $resource, config) {
return $resource(config.eventsLocation + '/verify/token/:token', {
eventId: '@eventId'
}, {
verifyToken: {
method: 'GET',
withCredentials: true
}
});
}
])
.factory('attendeeService', ['$resource', 'config',
function ($resource, config) {
return $resource(config.eventsLocation + '/attendee', {
userid: '@userid',
email: '@email',
eventid: '@eventid',
checkin: '@checkin',
rsvp: '@rsvp',
isPrivate: '@isPrivate'
}, {
save: {
method: 'POST',
withCredentials: true
}
});
}
])
.factory('attendeeInfoService', ['$resource', 'config',
function ($resource, config) {
return $resource(config.eventsLocation + '/attendee/user/:userid', {}, {
get: {
isArray: true,
method: 'GET',
withCredentials: true
}
});
}
])
.factory('attendeeListService', ['$resource', 'config',
function ($resource, config) {
return $resource(config.eventsLocation + '/attendee/event/:eventid', {}, {
get: {
isArray: true,
method: 'GET',
withCredentials: true
}
});
}
])
.factory('usernameService', ['$resource', 'config',
function ($resource, config) {
return $resource('/check-username', {
username: '@username'
}, {
post: {
method: 'POST'
}
});
}
])
.factory('eventFormatter', ['$rootScope', 'moment',
function ($rootScope, moment) {
return function (form, eventData) {
if (!form || !eventData) {
console.warn('You must provide a form instance and event data on a $scope');
return;
}
if (form.$invalid) {
// prevent form from being sent if there are invalid fields
console.warn('Form is invalid.');
window.scrollTo(0, 0);
return;
}
// Create a serialized event object to avoid modifying $scope
var serializedEvent = angular.copy(eventData);
if (eventData.duration !== 'unknown') {
serializedEvent.endDate = moment(eventData.beginDate).add('hours', parseFloat(eventData.duration, 10)).toISOString();
// Don't send an end date if duration is not specific
} else {
delete serializedEvent.endDate;
}
if (typeof serializedEvent.registerLink === 'string' && serializedEvent.registerLink.trim() === '') {
delete serializedEvent.registerLink;
}
// Remove nonexistant DB values from client event object
delete serializedEvent.duration;
// Convert CSV tags to array of Strings
if (serializedEvent.tags && typeof serializedEvent.tags === 'string') {
var tagArray = serializedEvent.tags.split(',');
tagArray.forEach(function (tag, index) {
tagArray[index] = tag.trim();
});
serializedEvent.tags = tagArray;
} else {
serializedEvent.tags = [];
}
return serializedEvent;
};
}
])
.factory('moment', ['$window', 'config',
function ($window, config) {
var moment = $window.moment;
moment.lang(config.lang);
return moment;
}
])
.factory('dateIsToday', ['moment',
function (moment) {
return function (date) {
var todayMoment = moment();
var eventMoment = moment(date);
if (todayMoment.year() === eventMoment.year() &&
todayMoment.dayOfYear() === eventMoment.dayOfYear()) {
return true;
} else {
return false;
}
};
}
])
.factory('authService', ['$rootScope', 'config',
function authService($rootScope, config) {
// This is needed to apply scope changes for events that happen in
// async callbacks.
function apply() {
if (!$rootScope.$$phase) {
$rootScope.$apply();
}
}
var auth = new WebmakerAuthClient({
handleNewUserUI: false
});
// Set up login/logout functions
$rootScope.login = auth.login;
$rootScope.logout = auth.logout;
// Set up user data
$rootScope._user = {};
// Set locale information
if (config.supported_languages.indexOf(config.lang) > 0) {
$rootScope.lang = config.lang;
} else {
$rootScope.lang = config.defaultLang;
}
$rootScope.direction = config.direction;
$rootScope.arrowDir = config.direction === 'rtl' ? 'left' : 'right';
$rootScope.ga_account = config.ga_account;
$rootScope.ga_domain = config.ga_domain;
$rootScope.eventsLocation = config.eventsLocation;
auth.on('login', function (user) {
$rootScope._user = user;
apply();
});
auth.on('logout', function (why) {
$rootScope._user = {};
apply();
});
auth.on('error', function (message, xhr) {
console.log('error', message, xhr);
});
return auth;
}
])
.factory('eventEditableService', ['$rootScope',
function ($rootScope) {
function isCoorganizer(event) {
if (!event.coorganizers) {
return false;
}
return event.coorganizers.some(function (c) {
return c.userId === $rootScope._user.id;
});
}
function isOrganizer(event) {
return event.organizerId === $rootScope._user.username;
}
function isAdmin() {
return $rootScope._user.isAdmin;
}
return {
isMentor: function (event) {
if (!event.mentors) {
return false;
}
return event.mentors.some(function (m) {
return m.userId === $rootScope._user.id;
});
},
isCoorganizer: isCoorganizer,
isOrganizer: isOrganizer,
isAdmin: isAdmin,
canEdit: function (event) {
return isCoorganizer(event) || isOrganizer(event) || isAdmin();
}
};
}
]);