/
renderer.js
629 lines (543 loc) · 19.8 KB
/
renderer.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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
'use strict'
// stuff to build window
const electron = require('electron');
// Module to control application life.
const app = electron.app;
// Module to create native browser window.
var remote = electron.remote;
const {BrowserWindow} = require('electron').remote;
// db stuff
const low = require('lowdb');
const storage = require('lowdb/lib/file-sync');
// TODO rename this db to auth_db, now that we have multiple db files
const db = low('auth.json');
const board_db = low('board.json');
const card_db = low('card.json');
// default stuff for board_db and card_db
board_db.defaults({ boards: [] }).value();
card_db.defaults({ cards: [] }).value();
// we need this to build the requests
const request = require('request');
// underscore
const _ = require('underscore');
// get the size of db entries
let hasAccessToken = db.get('access_token').size().value();
console.log(hasAccessToken);
let hasProfileData = db.get('profile_data').size().value();
// get the access token from the db if hasAccessToken > 0
if(hasAccessToken){
// get the token and set it to a var
var access_token = db.get('access_token').take(1).value();
var trelloToken = access_token[0].access_token;
}
// Trello stuff
var Trello = require("node-trello");
const t = new Trello("f4c23306bf38a3ec4ca351f999ee05d3", trelloToken);
// Define the electrello app module and some config for angular material
var electrello = angular.module('electrello', ['ngMaterial', 'ngMessages', 'ngRoute', 'ngResource', 'dndLists', 'xeditable']).config(function($mdThemingProvider) {
$mdThemingProvider.theme('default').dark()
.primaryPalette('red', {'default':'400'})
.accentPalette('yellow', {'default':'500'});
});
// set up some font icons
electrello.config(function($mdIconProvider) {
$mdIconProvider
.defaultIconSet('images/mdi.svg');
});
electrello.config(['$resourceProvider', function($resourceProvider) {
// Don't strip trailing slashes from calculated URLs
$resourceProvider.defaults.stripTrailingSlashes = false;
}]);
// filter to check to see if the object is empty
electrello.filter('isEmpty', [function() {
return function(object) {
//return angular.equals({}, object);
return Object.keys(object).length === 0;
}
}]);
// route controller
electrello.config(function($routeProvider) {
$routeProvider
// route for the home page
.when('/', {
templateUrl : 'views/home.html',
controller : 'AuthController',
resolve : {
"check" : function($location) {
// if they've got an access token we're good
if(hasAccessToken) {
// Send em to the dashboard
$location.path('/dashboard');
} else {
// redirect user to home to authorize their account
$location.path('/');
}
}
}
})
.when('/dashboard', {
templateUrl : 'views/dashboard.html',
controller : 'DashboardController',
resolve : {
"check" : function($location) {
// if they've got an access token we're good
if(!hasAccessToken) {
// Send em home to authorize
$location.path('/');
} else {
// redirect user to dashboard
$location.path('/dashboard');
}
}
}
})
.when('/profile', {
templateUrl : 'views/profile.html',
controller : 'ProfileController',
resolve : {
"check" : function($location) {
// if they've got an access token we're good
if(!hasAccessToken) {
// Send em home to authorize
$location.path('/');
} else {
// redirect user to dashboard
$location.path('/profile');
}
}
}
})
.when('/board/:boardID', {
templateUrl : 'views/board.html',
controller : 'BoardController'
});
});
// dashboard controller
electrello.controller('DashboardController', ['$scope', '$rootScope', '$route', '$location', '$window', '$mdDialog',
function($scope, $rootScope, $route, $location, $window, $mdDialog){
// // get the updated list of boards
// let num_of_boards = db.get('board_names').size().value();
// $scope.board_names = db.get('board_names').take(num_of_boards).value();
var local_boards_length = board_db.get('boards').size().value();
var local_boards;
if (local_boards_length) {
local_boards = board_db.get('boards').take(local_boards_length).value();
local_boards = local_boards[0].data;
}
// TODO first show the local boards, and then check Trello to see if anythings updated, if so then reload $state and update the local db
// get user id from DB
let data = db.get('profile_data').take(1).value();
let userID = data[0].profile_data.id;
// get updated list of boards straight from Trello
t.get("/1/members/" + userID + "/boards", function(err, data) {
if (err) throw err;
$scope.board_names = data;
$scope.$apply();
// check to see if local matches Trello data
if (local_boards) {
var board_up_to_date = _.isEqual(data, local_boards);
}
// if it doesnt match then update the local db
if (!board_up_to_date) {
board_db.get('boards')
.push({data})
.value();
}
});
// show a board
$scope.show_this_board = function( boardID ) {
$rootScope.pageClass = 'board';
$location.path('/board/' + boardID);
}
// warn em before deleting anything
$scope.showAlert = function(ev, boardID) {
var confirm = $mdDialog.confirm()
.parent(angular.element(document.querySelector('#popupContainer')))
.clickOutsideToClose(true)
.title('Absolutely, positively, positive about that?')
.textContent('This will instantly mark the board as closed.')
.ariaLabel('Sure?')
.ok('Make It so!')
.cancel('Nevermind')
.targetEvent();
$mdDialog.show(confirm).then(function() {
$scope.close_board(boardID);
}, function() {
console.log('changed your mind, eh?');
});
};
// mark the board as closed
$scope.close_board = function(boardID) {
t.put("/1/boards/" + boardID + '/closed', { value: "true" }, function(err, data) {
if (err) throw err;
console.log(data);
});
};
// open modal to get board name / details
$scope.newBoardModal = function(ev) {
// Appending dialog to document.body to cover sidenav in docs app
var title = $mdDialog.prompt()
.title('Add A New Board')
.textContent('What will you name it?')
.placeholder('Trello Board Name')
.ariaLabel('Trello Board Name')
.initialValue('')
.targetEvent(ev)
.ok('Create!')
.cancel('Nevermind!');
$mdDialog.show(title).then(function(result) {
create_board(result);
}, function() {
console.log('cancelled');
});
}
// add a new board
let create_board = function(board_name) {
t.post("/1/boards/", { name: board_name }, function(err, data) {
if (err) throw err;
console.log(data);
});
};
}]);
// board controller
electrello.controller('BoardController', ['$scope', '$route', '$routeParams', '$location', '$mdDialog', '$window', '$rootScope',
function($scope, $route, $routeParams, $location, $mdDialog, $window, $rootScope){
// get the id from the route
let boardID = $routeParams.boardID;
$scope.board_data = [];
$scope.board_lists = [];
$scope.list_cards = [];
$scope.masterListObject = {};
// TODO check for existing board data / card data and load that
// meanwhile check Trello for anything new and if it doesnt match then reload $state with updated info and update the local db
//
// maybe store all cards by their board ID, if i can figure out a way to accomplish that?
var local_cards_length = card_db.get('cards').size().value();
var local_cards;
if (local_cards_length) {
local_cards = card_db.get('cards').take(local_cards_length).value();
local_cards = local_cards[0].data;
}
// get some board data
let get_board_data = function( boardID ) {
t.get("/1/boards/" + boardID, function(err, data) {
if (err) throw err;
$scope.board_data = data;
// check to see if local matches Trello
if (local_cards) {
var card_up_to_date = _.isEqual(data, local_cards);
}
// if it doesnt match then update the local db
if (!card_up_to_date) {
card_db.get('cards')
.push({data})
.value();
}
// set the background
if ( data.prefs.backgroundColor != null ) {
$rootScope.board_background_color = data.prefs.backgroundColor;
}
if ( data.prefs.backgroundImage != null ) {
$rootScope.board_background_image = data.prefs.backgroundImage;
}
// apply it to the scopes
$scope.$apply();
$rootScope.$apply();
});
}
get_board_data( boardID );
// get cards
let get_list_cards = function( boardID ) {
t.get("/1/boards/" + boardID + "/cards", { fields : 'all', stickers : true, sticker_fields : 'all' },function(err, data) {
if (err) throw err;
$scope.list_cards = data;
// check for checklists on each card
// TODO save checklist to db
_.each($scope.list_cards, function(card) {
if(card.idChecklists.length) {
get_checklist(card.idChecklists[0]);
}
});
// build an object that the dragndrop directive handles better
if ( $scope.list_cards ) {
for ( let i = 0; i < $scope.masterListObject.length; i++ ) {
$scope.masterListObject[i]['cards'] = [];
$scope.list_cards.forEach( function(card) {
if( $scope.masterListObject[i].id == card.idList ) {
$scope.masterListObject[i]['cards'].push(card);
}
});
}
}
$scope.$apply();
});
};
// get checklist
let get_checklist = function( checklistID ) {
t.get("/1/checklists/" + checklistID, function(err, data) {
if (err) throw err;
// TODO assign checklist to list somehow
// ALSO! figure out local storage, lodash isnt great for boards obviously
$scope.check_lists = data;
$scope.$apply();
});
};
// update checklist item
$scope.updateChecklist = function(item, status, checklist) {
let checklistID = item.idChecklist;
let checkItemID = item.id;
let cardID = checklist.idCard;
if (status == 'markAsComplete') {
status = 'true';
}
else {
status = 'false';
}
// delete checklist item in Trello
t.put("/1/cards/" + cardID + "/checklist/" + checklistID + "/checkItem/" + checkItemID + "/state", { idChecklist : checklistID, idCheckItem : checkItemID, value : status}, function(err, data) {
if (err) throw err;
// TODO update view with new mark
get_checklist(checklistID);
$scope.$apply();
});
};
// delete checklist item
$scope.deleteChecklistItem = function(item) {
let checklistID = item.idChecklist;
let checkItemID = item.id;
// delete checklist item in Trello
t.del("/1/checklists/" + checklistID + "/checkItems/" + checkItemID, { idCheckItem : checkItemID}, function(err, data) {
if (err) throw err;
get_checklist(checklistID);
});
};
// get lists
let get_board_lists = function( boardID ) {
t.get("/1/boards/" + boardID + "/lists", function(err, data) {
if (err) throw err;
$scope.board_lists = data;
// build an object that the dragndrop directive handles better
if ( $scope.board_lists ) {
$scope.masterListObject = data;
}
get_list_cards( boardID );
// $scope.$apply();
});
};
get_board_lists( boardID );
// move card
$scope.moveCard = function(event, index, item, type, external, destination) {
// list/card ID
let listID = destination.id;
let cardID = item.id;
// add new item to destination's card array
destination.cards.splice(index, 0, item);
// update Trello first
t.put("/1/cards/" + cardID, { idList : listID, pos : index }, function(err, data) {
if (err) throw err;
// get_list_cards( boardID );
});
// return item so directive knows to insert into new list
return true;
};
// move list
$scope.moveList = function(event, index, item, type, external) {
// list/card ID
let listID = item.id;
// add new item to destination's card array
// $scope.masterListObject.splice(index, 0, item);
// update Trello first
t.put("/1/lists/" + listID, { pos : index }, function(err, data) {
if (err) throw err;
});
// return true so directive knows we are handling the move
return item;
};
// rename board/list/card/description
$scope.renameItem = function(itemType, itemId, newName) {
// update description
if (itemType == 'description') {
// update Trello with the new description
t.put("/1/cards/" + itemId + "/desc", { value : newName }, function(err, data) {
if (err) throw err;
});
}
else {
// update Trello with the new name
t.put("/1/" + itemType + "/" + itemId, { name : newName }, function(err, data) {
if (err) throw err;
});
}
};
// open modal to get list name / details
$scope.newListModal = function(ev) {
// Appending dialog to document.body to cover sidenav in docs app
var title = $mdDialog.prompt()
.title('Add A New List')
.textContent('What will you name it?')
.placeholder('Trello List Name')
.ariaLabel('Trello List Name')
.initialValue('')
.targetEvent(ev)
.ok('Create!')
.cancel('Nevermind!');
$mdDialog.show(title).then(function(result) {
create_list(result);
}, function() {
console.log('cancelled');
});
}
// add a new list
let create_list = function(list_name) {
t.post("/1/lists/", { name: list_name, idBoard: boardID }, function(err, data) {
if (err) throw err;
console.log(data);
});
};
// open modal to get card name / details
$scope.newCardModal = function(ev, listID) {
$mdDialog.show({
locals: { listID: listID},
templateUrl: 'views/card-modal.ng.html',
clickOutsideToClose: true,
controller: ['$scope', 'listID', function($scope, listID) {
$scope.listID = listID;
// add a new card
$scope.create_card = function(card, listID) {
t.post("/1/cards/", { name: card.name, idList: listID, desc: card.desc }, function(err, data) {
if (err) throw err;
// after we make it, close it
$scope.closeDialog();
});
};
// close the dialog
$scope.closeDialog = function() {
$mdDialog.hide();
}
}]
});
};
}]);
// menu controller
electrello.controller('MenuController', ['$scope', '$route', '$routeParams', '$location', '$mdDialog', '$window', '$rootScope', function($scope, $route, $routeParams, $location, $mdDialog, $window, $rootScope){
// set the body class
$rootScope.pageClass = 'dashboard';
// handle the menu
var originatorEv;
$scope.openMenu = function($mdOpenMenu, ev) {
originatorEv = ev;
$mdOpenMenu(ev);
}
// show the profile data we got upon authorization
$scope.show_profile = function() {
$rootScope.pageClass = 'dashboard profile';
$location.path('/profile');
}
// show the main dashboard
$scope.show_main_dashboard = function() {
$rootScope.pageClass = 'dashboard';
$location.path('/dashboard');
}
// delete the tokens
$scope.delete_token = function() {
db.get('access_token').remove().value();
db.get('profile_data').remove().value();
board_db.get('boards').remove().value();
card_db.get('cards').remove().value();
console.log('deleted token');
}
// warn em of impending deleting
$scope.showAlert = function(ev, alertType) {
var confirm = $mdDialog.confirm()
.parent(angular.element(document.querySelector('#popupContainer')))
.clickOutsideToClose(true)
.title('Are you absolutely sure about that?')
.textContent('Once this is done, there is no going back. Only re-syncing your data.')
.ariaLabel('Sure?')
.ok('Make It so!')
.cancel('Nevermind')
.targetEvent();
$mdDialog.show(confirm).then(function() {
if(alertType === 'profile'){
$scope.delete_profile_data();
} else if (alertType === 'token') {
$scope.delete_token();
}
}, function() {
console.log('changed your mind, eh?');
});
};
// delete profile data
$scope.delete_profile_data = function() {
db.get('profile_data').remove().value();
console.log('deleted profile data');
}
}]);
// profile data controller
electrello.controller('ProfileController', ['$scope', function ($scope) {
// pass the profile info to the view
let data = db.get('profile_data').take(1).value();
$scope.profile_data = data[0].profile_data;
let num_of_orgs = db.get('organizations').size().value();
$scope.organizations = db.get('organizations').take(num_of_orgs).value();
// get the organization logo
let get_organization_logo = function(organizationName) {
t.get('/1/organizations/' + organizationName + '/logoHash', function(err, data) {
var org_logo;
if (err) {
throw err;
}
org_logo = data;
return db.get('organizations').find({ name: organizationName }).assign({ logoHash: org_logo._value }).value();
});
}
if(num_of_orgs > 0) {
let organizations = $scope.organizations;
let l = 0;
while (l < organizations.length){
get_organization_logo(organizations[l].name);
l++
}
}
}]);
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let authWindow;
// this URL is set up by the node server we started up
const authUrl = 'http://127.0.0.1:6080/login';
// authorization stuffs
electrello.controller('AuthController', ['$scope', '$location', '$route', '$routeParams', '$window', '$rootScope', function($scope, $location, $route, $routeParams, $window, $rootScope){
// set the body class
$rootScope.pageClass = 'authorize';
// authorize their app
$scope.oAuthTokenRequest = function()
{
// make the auth window
authWindow = new BrowserWindow({
width: 360,
height: 440,
'webPreferences': {
'nodeIntegration': false,
'webSecurity': false
},
frame: false
});
// call the handleCallback function when we get response details back
authWindow.webContents.on('did-get-response-details', function (originalURL, httpResponseCode, requestMethod, event, resourceType, newURL, referrer)
{
$scope.handleCallback(originalURL, httpResponseCode, requestMethod, event, resourceType, newURL, referrer);
});
// open the Trello auth page
authWindow.loadURL(authUrl);
}
// this is fired when electron gets response data
$scope.handleCallback = function(newURL, originalURL, httpResponseCode, requestMethod, event, resourceType, referrer)
{
// if the httpResponseCode matches the regex, kill the screen
if (/^http:\/\/127\.0\.0\.1/.test(httpResponseCode)) {
authWindow.destroy();
// if we have the access_token send the user to the dashboard
$window.location.reload();
}
}
}]);