-
Notifications
You must be signed in to change notification settings - Fork 286
/
script.js
102 lines (86 loc) · 2.68 KB
/
script.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
var app = angular.module("sampleApp", ['firebase']);
app.controller("MainCtrl", ["$firebaseArray", '$scope', '$filter', function($firebaseArray, $scope, $filter) {
var ref = new Firebase("https://blinding-torch-6662.firebaseio.com/messages");
$scope.boardId = window.location.hash.substring(1);
$scope.messages = $firebaseArray(ref.orderByChild("board").equalTo($scope.boardId));
function calculateAllHeights(messages) {
var orderedArray = $filter('orderBy')(messages, ['-votes', 'date']);
orderedArray.forEach(function(message) {
var filtered = orderedArray.filter(function(item) {
return item.type.id === message.type.id;
});
message.currentHeight = filtered.indexOf(message) * 125 + 120 + 'px';
$scope.messages.$save(message);
});
}
$scope.messages.$loaded().then(function(messages) {
calculateAllHeights(messages);
});
$scope.messageTypes = [{
id: 1,
value: "Went well"
}, {
id: 2,
value: "To improve"
}, {
id: 3,
value: "Action Items"
}];
$scope.addVote = function(key, votes) {
if(!localStorage.getItem(key)) {
ref.child(key).update({ votes: votes + 1, date: Firebase.ServerValue.TIMESTAMP });
localStorage.setItem(key, 1);
}
calculateAllHeights($scope.messages);
}
$scope.deleteMessage = function(message) {
if(confirm('Are you sure you want to delete this note?')) {
$scope.messages.$remove(message).then(function() {
calculateAllHeights($scope.messages);
});
}
}
$scope.getHeight = function(message, index) {
if(!message.currentHeight) {
return index * 125 + 120 + 'px';
} else {
return message.currentHeight;
}
}
$scope.alreadyVoted = function(key) {
return localStorage.getItem(key);
}
$scope.focusElement = function(id) {
$('#' + id).find('textarea').focus();
};
$scope.createNewBoard = function() {
window.location.href = "http://glauberramos.github.com/firedeaz/#" + $scope.newBoardId;
};
$scope.addNew = function(type) {
$scope.messages.$add({
text: '',
board: $scope.boardId,
type: type,
date: Firebase.ServerValue.TIMESTAMP,
votes: 0
}).then(function(ref) {
var id = ref.key();
angular.element($('#' + id)).scope().isEditing = true;
$('#' + id).find('textarea').focus();
calculateAllHeights($scope.messages);
});
}
}]);
app.directive('enterClick', function () {
return {
restrict: 'A',
link: function (scope, elem, attrs) {
elem.bind('keydown', function(event) {
if (event.keyCode === 13 && event.shiftKey) {
event.preventDefault();
$(elem[0]).find('button').click();
}
});
}
}
});