-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcardCreate.js
323 lines (244 loc) · 9.05 KB
/
cardCreate.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
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
/**
* Main management interface
*/
var tc = new TrelloClient()
, chosenLabels = {}
, possibleLabels = ["red", "orange", "yellow", "purple", "blue", "green"]
, $leftPane = $('#left-pane')
, $topPane = $('#top-pane')
;
// Change a color's opacity, given that _oldColor is passed as rgb() or rgba() as is the case in jQuery
function changeOpacity(_oldColor, opacity) {
var oldColor = _oldColor.split(',')
, newColor = 'rgba(';
newColor += oldColor[0].split('(')[1] + ',' + oldColor[1] + ',' + oldColor[2].split(')')[0] + ',' + (opacity || 1) + ')';
return newColor;
}
function populateBoardsList(cb) {
var callback = cb || function() {};
tc.getAllBoards(function(err) {
var options = "";
if (err) { return callback(err); }
tc.openBoards.sort(function (a, b) { return a.name < b.name ? -1 : 1; }).forEach(function (board) {
options += '<option value="' + board.id + '">' + board.name + '</option>';
});
$('#boardsList').html(options);
// Use remembered value if there is one
if (localStorage.currentBoardId) {
$('#boardsList option[value="' + localStorage.currentBoardId + '"]').prop('selected', true);
}
return callback(null);
});
}
function populateLabelNamesList(cb) {
var callback = cb || function() {}
, selectedBoardId = $('#boardsList option:selected').val()
;
tc.getAllLabelsNames(selectedBoardId, function (err) {
if (err) { return callback(err); }
Object.keys(tc.currentLabels).forEach(function(color) {
$('.label-pickers div.' + color).html(tc.currentLabels[color] + " "); // Small hack ...
});
});
}
function populateListsList(cb) {
var callback = cb || function() {}
, selectedBoardId = $('#boardsList option:selected').val()
;
tc.getAllCurrentLists(selectedBoardId, function (err) {
var options = "";
if (err) { return callback(err); }
tc.currentLists.sort(function (a, b) { return a.name < b.name ? -1 : 1; }).forEach(function (list) {
options += '<option value="' + list.id + '">' + list.name + '</option>';
});
$('#listsList').html(options);
// Use remembered value if there is one
if (localStorage.currentListId) {
$('#listsList option[value="' + localStorage.currentListId + '"]').prop('selected', true);
}
return callback(null);
});
}
function getSelectedLabels() {
var labels = [];
$('.label-pickers div.selected').each(function(i, d){
$(d).attr('class').split(' ').forEach(function (clazz) {
if (clazz !== 'selected') { labels.push(clazz); }
});
})
return labels;
}
// Takes as input an XMLHttpRequestProgressEvent e
function updateUploadProgress(e) {
var progress = Math.floor(100 * (e.loaded / e.total));
$('#progress-bar').css('width', progress + '%');
if (progress === 100) {
setTimeout(function () {
$('#cardWasCreated').css('display', 'block');
}, 1000);
}
}
// Give feedback to user that card was created and close page
function cardWasCreated() {
console.log("Card is created, closing window");
window.close();
}
// Validation. Quite custom but not a real issue here ...
// Only validate text length. Wouldn't work if lower bound is greater than 1 of course but we're lucky here !
function validateText(inputId, lowerBound, upperBound) {
return function() {
var $input = $(inputId)
, value = $input.val()
, $parentDiv = $input.parent()
, $errorMessage = $parentDiv.find('div.alert')
;
if (value.length >= lowerBound && value.length <= upperBound) {
$parentDiv.removeClass('has-error');
$errorMessage.css('display', 'none');
return true;
} else {
$parentDiv.addClass('has-error');
$errorMessage.css('display', 'block');
return false;
}
}
}
var validateCardName = validateText('#cardName', 1, 16384);
$('#cardName').on('keyup', validateCardName);
var validateCardDesc = validateText('#cardDesc', 0, 16384);
$('#cardDesc').on('keyup', validateCardDesc);
// =================================================
// Initialization
// =================================================
// Panes sizes and left pane behaviour, plus canvas
var totalHeight = $('body').height()
, totalWidth = $('body').width()
, topPaneHeight = 50
, screenshotPaneHeight = totalHeight - topPaneHeight
, rightPanesWidth = Math.floor(totalWidth * screenshotPaneHeight / totalHeight)
, leftPaneWidth = Math.floor(0.2 * totalWidth)
, baseLeftPanePosition = Math.max(totalWidth - rightPanesWidth - leftPaneWidth, 20 - leftPaneWidth)
;
$topPane.css('width', rightPanesWidth + 'px');
$topPane.css('height', topPaneHeight + 'px');
$topPane.css('line-height', topPaneHeight + 'px'); // Vertically center contents on a single line
$topPane.css('padding-left', (30 + leftPaneWidth - totalWidth + rightPanesWidth) + 'px'); // 30px from border of left pane when it's totally on screen
$('#screenshot-pane').css('width', rightPanesWidth + 'px');
$('#screenshot-pane').css('height', screenshotPaneHeight + 'px');
// Show left pane until screenshot appears, then slide it to the left
$leftPane.css('width', leftPaneWidth + 'px');
$('body').on('trelloCapture.screenshotTaken', function () {
$leftPane.css('left', baseLeftPanePosition + 'px');
});
$leftPane.on('mouseover', function() {
$leftPane.css('left', '0');
})
$leftPane.on('mouseout', function() {
$leftPane.css('left', baseLeftPanePosition + 'px');
})
// Drawing board
var ms = new ModifiedScreenshot();
ms.initColorPicker();
ms.initShapePicker();
// ms.initializeDrawingMode(); // TODO: remove, for testing only
$('#clear-board').on('click', function () {
ms.clearAllDrawings();
});
window.ms = ms; // TODO: remove, for testing only
// Manage background-color behavior on click for labels
possibleLabels.forEach(function(label) {
var $label = $('.' + label);
// Initial state
$label.css('background-color', changeOpacity($label.css('background-color'), 0.35));
$label.on("click", function () {
$label.toggleClass('selected');
if ($label.hasClass('selected')) {
$label.css('background-color', changeOpacity($label.css('background-color'), 1));
} else {
$label.css('background-color', changeOpacity($label.css('background-color'), 0.35));
}
});
});
$('#boardsList').on('change', function() {
var selectedBoardId = $('#boardsList option:selected').val();
localStorage.currentBoardId = selectedBoardId; // Remember this setting, user probably wants the same board all the time
populateListsList();
populateLabelNamesList();
});
$('#listsList').on('change', function() {
localStorage.currentListId = $('#listsList option:selected').val(); // Remember this setting, user probably wants the same list all the time
});
$('#on-top').on('change', function () {
localStorage.cardOnTop = $('#on-top').prop('checked');
});
$('#createCard').on('click', function () {
if (!ms.currentBase64Image) { return; }
if (!validateCardName() || !validateCardDesc()) { return; }
var selectedListId = $('#listsList option:selected').val();
// Create card
tc.createCardAtBottomOfCurrentList(selectedListId, $('#cardName').val(), $('#cardDesc').val(), getSelectedLabels(), function (err, cardId) {
async.waterfall([
function (cb) {
if ($('#on-top').prop('checked')) {
tc.putCardOnTopOfList(cardId, cb);
} else {
return cb();
}
}
], function () {
$('#progress-bar-container').css('display', 'block');
ms.persistCurrentScreenshot(function () {
tc.attachBase64ImageToCard(cardId, ms.currentBase64Image, updateUploadProgress, cardWasCreated);
});
});
});
});
// Initialization
function initializeBoardsAndLists() {
populateBoardsList(function() {
$('#boardsList').trigger('change');
});
}
if (localStorage.cardOnTop) {
$('#on-top').prop('checked', true);
}
// ============================
// Entry point
// ============================
tc.getLoggedUsername(function (err) {
if (tc.username) {
getTrelloCredentialsAndInitialize();
} else {
$('#login').css('display', 'block'); // TODO: uncomment
}
});
// Makes the assumption the user is logged in to Trello
function getTrelloCredentialsAndInitialize (cb) {
var callback = cb || function () {};
tc.getLoggedUsername(function (err) {
tc.getApiCredentials(function (err) {
tc.getClientToken(function (err) {
initializeBoardsAndLists();
});
});
});
}
function tryToLogIn() {
$('#login div.alert-danger').css('display', 'none');
tc.logUserIn($('#login-email').val(), $('#login-password').val(), function (err, loggedIn) {
if (loggedIn) {
$('#login').css('display', 'none');
getTrelloCredentialsAndInitialize();
} else {
$('#login div.alert-danger').css('display', 'block');
}
});
}
$('#login-button').on('click', tryToLogIn);
$('#login-box').on('keypress', function(evt) {
if (evt.keyCode === 13) { tryToLogIn(); }
});
// When we receive an image
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
ms.setAsBackground(request.imageData);
});