-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyou-R-here.js
More file actions
429 lines (369 loc) · 14.8 KB
/
you-R-here.js
File metadata and controls
429 lines (369 loc) · 14.8 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
var _express = require("express");
var _app = _express();
var _http = require("http");
var _server = _http.createServer(_app);
var _io = require("socket.io").listen(_server);
var _gravatar = require("gravatar");
var _url = require("url");
var _path = require("path");
var _fs = require("fs");
var _mime = require("mime");
var _uuid = require("node-uuid"); //Creates "guids" for use as unique object ids
var _ = require("underscore");
_.str = require("underscore.string"); //there are name conflicts with underscore.string
_.mixin(_.str.exports()); //put the non-conflicting methods in _ var
_.str.include("underscore.string", "string"); //put all conflicting methods in _.str
_io.set('log level', 1); //reduce logging
var moment = require('moment');
var appConfig = require('./app.config');
var _address = appConfig.app.serverAddress;
var _port = appConfig.app.serverPort;
var pkg = require('./package.json');
_server.listen(_port);
var _plugin = require('./plugins/' + appConfig.app.plugin).plugin;
var config = _plugin.config;
var _users = []; //users currently connected
var _demoItems = []; //user stories and bugs
var _iteration = {endDate: new Date()}; //information about the current iteration to which the demo items belong
var _activeItemId = 0;
var _staticContentItems = {
title: appConfig.app.title,
scripts: [
"/socket.io/socket.io.js",
"https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js",
"https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js",
"http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js",
//"http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone.js",
"/client_libs/backbone.js", //development version
"http://cdn.jsdelivr.net/jgrowl/1.2.6/jquery.jgrowl_minimized.js",
"client_libs/jquery.ui.touch-punch.min.js",
"/client_libs/backbone.iobind.js",
"/client_libs/backbone.iosync.js",
"/client_libs/moment.min.js",
//"/client_libs/safetyfirst.js",
//"/client_libs/json2.js",
"/client_script/model.js?v=" + pkg.version,
"/client_script/views.js?v=" + pkg.version
],
styles: [
"/bootstrap/css/bootstrap.min.css",
"/css/jquery.jgrowl.css",
"http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/themes/base/jquery-ui.css",
"/css/index.css?v=" + pkg.version
],
version: pkg.version, //get the version from the package.json file and hand it off to the views
address: _address,
port: _port
};
var _headerInfo;
//routing
_app.get("/", function(req, res) {
res.render("spectator.jade", _staticContentItems);
});
_app.get("/admin*", function (req, res) {
res.render("admin.jade", _staticContentItems);
});
_app.get("/organizer*", function (req, res) {
res.render("organizer.jade", _staticContentItems);
});
_app.get("/presenter*", function (req, res) {
res.render("presenter.jade", _staticContentItems);
});
_app.get("/image/*", function(req, res){
getImage(req, res);
});
_app.get("/*", function(req, res){
var uri = _url.parse(req.url).pathname;
var filename = _path.join(process.cwd(), uri);
_fs.exists(filename, function(exists){
if(!exists){
res.writeHead(404, {"Content-Type" : "text/plain"});
res.write("Content not found");
res.end();
return;
}
_fs.readFile(filename, "binary", function(err, file){
res.writeHead(200, {"Content-Type" : _mime.lookup(filename) });
res.write(file, "binary");
res.end();
});
});
});
_io.sockets.on("connection", function (socket) {
socket.on('headerinfo:read', function(data, callback){
_headerInfo = buildHeaderInfo();
callback(null, _headerInfo);
//this probably means that I'm doing something wrong in creation/init of HeaderInfoView
//but this update assures that organizer first page load gets correct header info
_io.sockets.emit('headerinfo:update', _headerInfo);
});
socket.on("iteration:read", function(data, callback) {
if (data.init) {
//iteration.fetch signals arrival of the organizer...
enterHere(function(data) {
//reset the demo items for anyone already listening
_demoItems = [];
_io.sockets.emit("demoitems:refresh", _demoItems);
callback(null, _iteration);
sendHeaderInfo();
});
} else {
callback(null, _iteration);
}
});
socket.on("iteration:create", function(iteration, callback) {
_iteration = iteration;
//TODO: move this into the Iteratoin model...
//endDate matters for the TargetProcess plugin
var date = _.map(_iteration.sprints, function(val) {
if (val.id == iteration.sprintId) {
return val.endDate;
}
});
_iteration.endDate = date;
refreshEntities(function() {
socket.broadcast.emit("iteration:update", _iteration);
_io.sockets.emit("demoitems:refresh", _demoItems);
sendHeaderInfo();
});
});
// called when .fetch() is called on DemoItems collection on client side
socket.on("demoitems:read", function (data, callback) {
//console.log('demoitems:read');
//console.log(data);
if (data.init) {
refreshEntities(function() {
callback(null, _demoItems);
_io.sockets.emit("demoitems:refresh", _demoItems);
sendHeaderInfo();
});
} else {
callback(null, _demoItems);
}
});
socket.on("demoitems:reset", function(data) {
//showItems(_demoItems,"demoItems:reset ===> ");
});
// called when .save() is called on DemoItem model
socket.on("demoitems:update", function (updatedDemoItem, callback) {
//update in-memory demo items list
var oldDemoItem = _.find(_demoItems, function (e) { return e.id === updatedDemoItem.id; }),
idx = _demoItems.indexOf(oldDemoItem);
if (idx < 0) {
logIt('WARN: idx less than zero! oldDemoItem NOT FOUND! for id: ' + updatedDemoItem.id);
//showItems(_demoItems,"WARN: idx < zero ==> ");
return;
}
//there can be only one active item!
if (updatedDemoItem.active) _.each(_demoItems, function (e) { e.active = false; });
_demoItems[idx] = updatedDemoItem;
//showItems(_demoItems,"BEFORE ==> ");
var nextIdx = idx + 1,
len = _demoItems.length;
if (updatedDemoItem.nextId == -2) {
_demoItems.splice(idx, 1);
_demoItems.push(updatedDemoItem);
}
//update the order of demoItems if nextId != -1
if (nextIdx < len && updatedDemoItem.nextId != -2 && updatedDemoItem.nextId != -1) {
if (_demoItems[idx+1] && updatedDemoItem.nextId != _demoItems[idx+1].id) {
var nextDemoItem = _.find(_demoItems, function (e) {
var found = e.id == updatedDemoItem.nextId;
if (!found) {
found = e.id === updatedDemoItem.nextId;
}
return found;
}),
nextIdx = _demoItems.indexOf(nextDemoItem);
if (idx < nextIdx) nextIdx--; //account for the position we're vacating
if (nextIdx >= 0) {
//reorder!
_demoItems.splice(idx, 1);
_demoItems.splice(nextIdx, 0, updatedDemoItem);
} else {
//console.log('WARN: "nextDemoItem not found! nextIdx: ' + nextIdx + '; _demoItems.len: ' + len);
//showItems(_demoItems,"WARN: nextDemoItem not found! ==> ");
}
} else {
if (updatedDemoItem.nextId == _demoItems[idx+1].id) {
//logIt('No move required!')
} else {
//console.log('WARN: nextIdx: ' + nextIdx + '; _demoItems.len: ' + len);
//showItems(_demoItems,'WARN: nextIdx: ' + nextIdx + '; _demoItems.len: ' + len + ' ==> ');
}
}
}
showItems(_demoItems,"AFTER ==> ");
//tell everyone else what happened
var activeChanged = !oldDemoItem.active && updatedDemoItem.active,
action = (activeChanged) ? "activeChanged" : "update";
socket.broadcast.emit("demoitems/" + updatedDemoItem.id + ":" + action, updatedDemoItem);
callback(null, updatedDemoItem);
});
// called when .fetch() is called on Users collection on client side
socket.on("users:read", function (data, callback) {
callback(null, _users);
});
socket.on("user:create", function (newUser, callback) {
if (!newUser.email || _.trim(newUser.email) === "") return;
newUser.id = _uuid.v4(); //backbone.iobind loses its mind if ids are not unique
newUser.gravatarUrl = _gravatar.url(newUser.email, { size: "32", default: "identicon" });
_users.push(newUser);
socket.userid = newUser.id;
socket.email = newUser.email;
socket.emit("users:create", newUser);
socket.broadcast.emit("users:create", newUser);
callback(null, newUser);
});
//handle client disconnects
socket.on("disconnect", function () {
logIt("DISCONNECT --------------------------");
logIt(socket.userid + " has disconnected");
//tell everyone else that the user disconnected
var user = _.where(_users, { id: socket.userid });
socket.broadcast.emit("user/" + socket.userid + ":delete", user);
//remove the user from the array
for (var i = 0, len = _users.length; i < len; i++) {
if (_users[i].id === socket.userid) {
logIt("deleting item " + i);
_users.splice(i, 1);
break;
}
}
});
});
enterHere(function(){
refreshEntities();
});
function enterHere(callback) {
//auto-load entities list for most recent iteration when app starts
_plugin.api("getMostRecentIterationBoundary", function (err, boundaryData) {
if (boundaryData.data) {
_iteration.endDate = boundaryData.date;
_iteration.sprintId = boundaryData.data.sprintId;
_iteration.sprints = boundaryData.data.sprints;
_iteration.sprintName = boundaryData.data.sprintName;
} else {
_iteration.endDate = boundaryData.date;
}
callback(_iteration);
});
}
function refreshEntities(callback) {
_plugin.api("getEntitiesForActiveIteration",
function (err, data) {
_demoItems = data;
if (callback) {
callback();
}
},
{
date: _iteration.endDate,
sprintId: _iteration.sprintId
});
}
function getItem(itemId) {
var id = parseInt(itemId),
item = null;
for (var i = 0, len = _demoItems.length; i < len; i++) {
if (parseInt(_demoItems[i].id) === id) {
item = _demoItems[i];
break;
}
};
return item;
}
function logIt(msg) {
if (console && console.log) {
console.log(msg);
} else {
//growl it!
}
}
function showItems(demoItems, prefix) {
return;
var msg = prefix + " length: " + demoItems.length + ". ";
_.each(demoItems, function(val) {
msg += val.id + (val.active ? "," + val.active : "") + ";";
});
logIt(msg);
}
function buildHeaderInfo() {
var dateFormat = 'MMM D [\']YY', //use 'll' with moment 2.0.x
itemCount = (_demoItems) ? _demoItems.length : 0,
bugRegex = new RegExp('Bug', 'i'),
userStoryRegex = new RegExp('UserStory', 'i'),
impedimentRegex = new RegExp('Impediment', 'i'),
bugList = _.filter(_demoItems, function(item) {
//console.log(item.type);
return bugRegex.test(item.type);
}),
userStoryList = _.filter(_demoItems, function(item) {
//console.log(item.type);
return userStoryRegex.test(item.type);
}),
impedimentList = _.filter(_demoItems, function(item) {
//console.log(item.type);
return impedimentRegex.test(item.type);
}),
bugCount = (itemCount < 1) ? 0 : bugList.length,
userStoryCount = (itemCount < 1) ? 0 : userStoryList.length,
impedimentCount = (itemCount < 1) ? 0 : impedimentList.length,
priorityCounts = {},
priorityList = [];
_.each(_demoItems, function(item){
if (priorityCounts[item.priority]) {
priorityCounts[item.priority].count++;
} else {
priorityCounts[item.priority] = { id: item.priorityId, name: item.priority, count: 1 };
}
});
for(key in priorityCounts){
priorityList.push(priorityCounts[key]);
}
priorityList.sort(function(a,b){
return a.id - b.id;
});
var headerinfo = {
startDate: moment(_iteration.endDate).subtract('weeks', config.info.iterationDurationInWeeks).format(dateFormat),
endDate: moment(_iteration.endDate).format(dateFormat),
itemCount: itemCount,
bugCount: bugCount,
userStoryCount: userStoryCount,
impedimentCount: impedimentCount,
orgName: config.info.orgName,
sprintName: _iteration.sprintName,
priorities: priorityList
};
//console.log(headerinfo);
return headerinfo;
};
function sendHeaderInfo() {
//console.log('sendHeaderInfo');
_headerInfo = buildHeaderInfo();
_io.sockets.emit('headerinfo:update', _headerInfo);
}
function getImage(req, res) {
_plugin.api("imagePassthrough",
function(err, data){
res.writeHead(200, {"Content-Type" : _mime.lookup('file.png'), "Content-Length" : data.length} );
_.each(data.chunks, function(chunk){
//console.log('writing...');
res.write(chunk);
});
//console.log('done writing...');
res.end();
}, req);
}
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith
if (!String.prototype.startsWith) {
Object.defineProperty(String.prototype, 'startsWith', {
enumerable: false,
configurable: false,
writable: false,
value: function (searchString, position) {
position = position || 0;
return this.indexOf(searchString, position) === position;
}
});
}