-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
144 lines (138 loc) · 5.17 KB
/
app.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
// handy snippets for the JavaScript console:
// angular.element(document.body).scope()
// angular.element(document.body).injector().get('board')
var app = angular.module("trenchesApp", ["ngRoute", "firebase"]);
app.constant('FBURL', "https://trenches.firebaseio.com/");
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/', { templateUrl: 'boards.html', controller: 'BoardListController' })
.when('/board/:boardId', { templateUrl: 'board.html', controller: 'BoardController' })
.when('/board/:boardId/card/:cardId', { templateUrl: 'card.html', controller: 'CardController' })
.when('/board/:boardId/admin', { templateUrl: 'admin.html', controller: 'AdminController' })
.otherwise({ redirectTo: '/' });
}]);
app.factory('Auth', function(FBURL, $firebaseAuth) {
return $firebaseAuth(new Firebase(FBURL));
});
app.factory('board', function($firebaseObject, $firebaseArray, Auth, FBURL, $routeParams) {
var ref = new Firebase(FBURL+"boards/"+$routeParams.boardId);
var settings = $firebaseObject(ref.child('settings'));
var cards = $firebaseArray(ref.child('cards'));
var states = $firebaseArray(ref.child('states'));
var users = $firebaseArray(ref.child('users'));
function updateCard(id, update) {
Auth.$requireAuth().then(function(user) {
ref.child('cards').child(id).update({ owned_by: user[user.provider].username });
});
};
function addCard(state, callback) {
Auth.$requireAuth().then(function(user) {
var card = { title: 'Card '+(cards.length+1), created_by: user[user.provider].username, created_at: Firebase.ServerValue.TIMESTAMP, state: state.$value };
cards.$add(card).then(function(added) {
if (callback) callback(added);
});
});
};
function removeCard(id) {
return cards.$remove(cards.$getRecord(id));
};
return {
ref: ref,
id: $routeParams.boardId,
settings: settings,
states: states,
cards: cards,
users: users,
updateCard: updateCard,
addCard: addCard,
removeCard: removeCard
}
});
app.controller("TrenchesController", function TrenchesController($scope, FBURL, Auth, $timeout) {
$scope.auth = Auth;
$scope.user = Auth.$getAuth();
$scope.auth.$onAuth(function(authData) {
// user logged on or off -> update scope
$timeout(function() {
$scope.auth = Auth;
$scope.user = Auth.$getAuth();
});
});
});
app.controller('BoardListController', function BoardListController($scope, $rootScope, FBURL, $firebaseArray) {
$rootScope.title = 'Trenches';
var ref = new Firebase(FBURL+"boards");
$scope.boards = $firebaseArray(ref);
});
app.controller('BoardController', function BoardController($scope, $rootScope, FBURL, $firebaseObject, Auth, $location, $routeParams, board) {
$scope.boardId = board.id;
$scope.states = board.states;
board.settings.$bindTo($scope, 'settings').then(function () {
$rootScope.title = board.settings.title + ' - Trenches';
});
$scope.cards = board.cards;
$scope.auth = Auth; // TODO: figure out why this and next line are needed, since they should already be inherited from the scope of TrenchesController
$scope.user = Auth.$getAuth();
$scope.updateCard = board.updateCard;
Auth.$requireAuth().then(function(user) {
console.log(user);
$scope.me = $firebaseObject(board.ref.child('users/'+user.uid));
});
$scope.addCard = function(state) {
board.addCard(state, function(card) {
$location.path('/board/'+$scope.boardId+'/card/'+card.key());
});
};
});
app.controller('CardController', function CardController($scope, $rootScope, FBURL, $firebaseObject, $location, $routeParams, board) {
var ref = new Firebase(FBURL+"boards/"+$routeParams.boardId+'/cards/'+$routeParams.cardId);
$scope.boardId = $routeParams.boardId;
$scope.board = board.settings;
var card = $firebaseObject(ref);
card.$bindTo($scope, 'card').then(function() {
$rootScope.title = card.title + ' - Trenches';
});
$scope.removeCard = function() {
if (confirm("Are you sure you want to delete '"+card.title+"'?")) {
board.removeCard($routeParams.cardId).then(function() {
$location.path('/board/'+$scope.boardId);
}, function (error) {
alert(error);
});
}
};
});
app.controller('AdminController', function AdminController($scope, $rootScope, board) {
$scope.board = board;
});
// Custom directive for handling drag-and-drop
var isIE9 = (navigator.appVersion.indexOf('MSIE 9.0') >= 0);
app.directive('draggable', function($document) {
return function(scope, element, attr) {
if (!scope.user) return;
element.attr('draggable', 'true');
if (isIE9) {
element.prepend("<a class='draghandle' draggable='true' id='"+scope.card.$id+"' href='#'><i class='fa fa-arrows'></i></a>");
}
element.bind('dragstart touchstart', function(e) {
e.dataTransfer.setData('text', scope.card.$id)
});
}
});
app.directive('droparea', function() {
return function(scope, element, attr, ctrl) {
element.bind('dragover', function(e) {
e.preventDefault();
});
element.bind('drop touchend', function(e) {
var id = e.dataTransfer.getData("text");
var state = e.target.getAttribute('state');
if (id && state) {
scope.updateCard(id, { state: state, owned_by: 'auth.user.username' });
}
if (e.stopPropagation) e.stopPropagation();
if (e.preventDefault) e.preventDefault();
return false;
});
}
});