/
ProjectView.js
258 lines (225 loc) · 6.44 KB
/
ProjectView.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
/* global window */
define([
'jquery', 'underscore', 'ko', 'timeago', 'tooltips',
'util', 'Issue', 'Notifier', 'UserManager', 'MessageList', 'IssueManager', 'error/NoSuchIssueError'
],
function ($, _, ko, timeago, tooltips, util, Issue, Notifier, UserManager, MessageList, IssueManager, NoSuchIssueError) {
var BAD_COMMAND_RESPONSES = [
'Oops.', 'This is not a Turing test.',
'The least you could do is be grammatical.',
'That does not compute.',
'I can haz parser.',
'These are not the droids you\'re looking for.'
];
var ProjectView = function ($nameInput, $messageInput, $form, $messageList, socket) {
var that = this;
this.socket = socket;
this.messageList = new MessageList($messageList, socket);
this.userManager = new UserManager($nameInput, socket);
this.issueManager = new IssueManager(socket);
this.notifier = new Notifier(this.userManager, socket);
this.$messageInput = $messageInput;
this.disconnected = ko.observable();
this.loading = ko.observable(true);
this.initTooltips();
$(window).bind('hashchange', _.bind(this.checkHashForBookmark, this));
this.hideClosed = ko.observable(true);
this.hideAssigned = ko.observable(false);
this.helpOpen = ko.observable(false);
this.addHtmlLinks = util.addHtmlLinks;
ko.applyBindings(this);
this.socket.on('connect', function () {
that.disconnected(false);
that.userManager.loginExistingUserIfAny();
that.loading(false);
});
this.socket.on('disconnect', function () {
that.disconnected(true);
});
this.socket.on('issues', function (issues) {
that.checkHashForBookmark();
});
};
ProjectView.prototype.initTooltips = function () {
var that = this;
$('#messages').tooltip({
selector: '.id',
placement: 'right',
delay: 0,
title: function () {
var issue = that.issueManager.findIssue($(this).data('id'));
var assignee = issue.assigneeLabel();
var tooltip = assignee ? assignee + ' ' : '';
return tooltip + issue.description();
}
});
};
function isCommand(input) {
return input.trim().charAt(0) === "/";
}
function ArgError(message) {
this.name = "ArgError";
this.message = "Invalid or missing argument.";
}
function requireArgument() {
_.each(arguments, function (arg) {
if (!arg) {
throw new ArgError();
}
});
}
function getArgument(string, argToReturn) {
if (!string) {
return null;
}
var match = string.match(/(\d+)(?:\s+(.+))?/);
return match ? match[argToReturn] : null;
}
// @VisibleForTesting
ProjectView.prototype.notifyOfBadCommand = function () {
window.alert(util.getRandomItem(BAD_COMMAND_RESPONSES) + ' Try /help.'); // TODO: style
};
ProjectView.prototype.notifyNoSuchIssue = function (error) {
window.alert(error.toString());
};
// TODO: extract command parsing etc.
ProjectView.prototype.handleInput = function () {
if (this.userManager.noUser()) {
this.userManager.attemptLogin();
return;
}
if (!this.userManager.loggedIn()) {
return;
}
var input = this.$messageInput.val();
this.$messageInput.val('');
if (!input || input.length < 1) {
return;
}
if (!isCommand(input)) {
this.send(input); // assume chat message
return;
}
try {
var matches = input.match(/[:\/]([\S]+)(?:\s+(.*))?/);
var cmd = matches[1] && matches[1].trim();
var rest = matches[2] && matches[2].trim();
var id;
switch (cmd.toLowerCase()) {
case 'help':
case '?':
this.helpOpen(!this.helpOpen());
break;
case 'add':
case 'create':
case 'nouveau':
case 'new':
case 'open':
requireArgument(rest);
this.issueManager.createIssue(rest);
break;
case 'close':
case 'resolve':
case 'done':
id = parseInt(rest, 10);
requireArgument(id);
this.issueManager.closeIssue(id);
break;
case 'reopen':
id = parseInt(rest, 10);
requireArgument(id);
this.issueManager.updateIssue(id, { closed: false });
break;
case 'unassign':
id = parseInt(rest, 10);
requireArgument(id);
this.issueManager.assignIssue(id, 'nobody');
break;
case 'assign':
case '@':
id = parseInt(getArgument(rest, 1), 10);
var assignee = getArgument(rest, 2);
requireArgument(id);
this.issueManager.assignIssue(id, assignee);
break;
case 'tag':
id = parseInt(getArgument(rest, 1), 10); // TODO: comma separated ids
var tag = getArgument(rest, 2);
requireArgument(id, tag);
this.issueManager.tagIssue(id, tag);
break;
case 'untag':
id = parseInt(getArgument(rest, 1), 10); // TODO: comma separated ids
requireArgument(id);
this.issueManager.untagIssue(id);
break;
case 'critical':
case 'urgent':
case '!':
case '*':
case 'star':
id = parseInt(getArgument(rest, 1), 10);
requireArgument(id);
this.issueManager.prioritizeIssue(id);
break;
case 'edit':
case 'update':
// only allow editing the description
id = parseInt(getArgument(rest, 1), 10);
var desc = getArgument(rest, 2);
requireArgument(id, desc);
this.issueManager.updateIssue(id, { description: desc });
break;
case 'reset':
this.reset();
break;
default:
this.notifyOfBadCommand();
break;
}
} catch (e) {
if (e instanceof ArgError) {
this.notifyOfBadCommand();
} else if (e instanceof NoSuchIssueError) {
this.notifyNoSuchIssue(e);
} else {
throw e;
}
}
};
ProjectView.prototype.send = function (message) {
this.socket.emit('user message', message);
};
ProjectView.prototype.reset = function () {
if (window.confirm('Warning: this will completely delete all issues from the server.')) {
if (window.confirm('I have a bad feeling about this. Are you absolutely sure?')) {
this.socket.emit('reset issues');
}
}
};
// doesn't highlight if filtering issues, but not a big deal
ProjectView.prototype.checkHashForBookmark = function () {
var bookmarked = parseInt(window.location.hash.substring(1), 10);
try {
this.issueManager.highlightIssue(bookmarked);
var $target = $(window.location.hash);
if ($target.length) {
var pos = $target.offset();
window.scrollTo(pos.left, pos.top);
}
} catch (e) {
if (!(e instanceof NoSuchIssueError)) {
throw e;
}
}
};
ProjectView.prototype.applyTimeago = function (elements) {
_.each(elements, function (element) {
var $time = $(element).find("time");
if ($time && $time.length) {
$time.timeago();
}
});
};
return ProjectView;
});