-
Notifications
You must be signed in to change notification settings - Fork 218
/
eggly-app.finish.js
131 lines (106 loc) · 4.42 KB
/
eggly-app.finish.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
angular.module('Eggly', [
])
.controller('MainCtrl', function ($scope) {
$scope.categories = [
{"id": 0, "name": "Development"},
{"id": 1, "name": "Design"},
{"id": 2, "name": "Exercise"},
{"id": 3, "name": "Humor"}
];
$scope.bookmarks = [
{"id": 0, "title": "AngularJS", "url": "http://angularjs.org", "category": "Development" },
{"id": 1, "title": "Egghead.io", "url": "http://angularjs.org", "category": "Development" },
{"id": 2, "title": "A List Apart", "url": "http://alistapart.com/", "category": "Design" },
{"id": 3, "title": "One Page Love", "url": "http://onepagelove.com/", "category": "Design" },
{"id": 4, "title": "MobilityWOD", "url": "http://www.mobilitywod.com/", "category": "Exercise" },
{"id": 5, "title": "Robb Wolf", "url": "http://robbwolf.com/", "category": "Exercise" },
{"id": 6, "title": "Senor Gif", "url": "http://memebase.cheezburger.com/senorgif", "category": "Humor" },
{"id": 7, "title": "Wimp", "url": "http://wimp.com", "category": "Humor" },
{"id": 8, "title": "Dump", "url": "http://dump.com", "category": "Humor" }
];
$scope.isCreating = false;
$scope.isEditing = false;
$scope.currentCategory = null;
$scope.editedBookmark = null;
function isCurrentCategory(category) {
return $scope.currentCategory !== null && category.name === $scope.currentCategory.name;
}
function setCurrentCategory(category) {
$scope.currentCategory = category;
cancelCreating();
cancelEditing();
}
$scope.isCurrentCategory = isCurrentCategory;
$scope.setCurrentCategory = setCurrentCategory;
function setEditedBookmark(bookmark) {
$scope.editedBookmark = angular.copy(bookmark);
}
function isSelectedBookmark(bookmarkId) {
return $scope.editedBookmark !== null && $scope.editedBookmark.id === bookmarkId;
}
$scope.setEditedBookmark = setEditedBookmark;
$scope.isSelectedBookmark = isSelectedBookmark;
function resetCreateForm() {
$scope.newBookmark = {
title: '',
url: '',
category: $scope.currentCategory.name
};
}
//-------------------------------------------------------------------------------------------------
// CRUD
//-------------------------------------------------------------------------------------------------
function createBookmark(bookmark) {
bookmark.id = $scope.bookmarks.length;
$scope.bookmarks.push(bookmark);
resetCreateForm();
}
function updateBookmark(bookmark) {
var index = _.findIndex($scope.bookmarks, function (b) {
return b.id == bookmark.id
});
$scope.bookmarks[index] = bookmark;
$scope.editedBookmark = null;
$scope.isEditing = false;
}
function deleteBookmark(bookmark) {
_.remove($scope.bookmarks, function (b) {
return b.id == bookmark.id;
});
}
$scope.createBookmark = createBookmark;
$scope.updateBookmark = updateBookmark;
$scope.deleteBookmark = deleteBookmark;
//-------------------------------------------------------------------------------------------------
// CREATING AND EDITING STATES
//-------------------------------------------------------------------------------------------------
function shouldShowCreating() {
return $scope.currentCategory && !$scope.isEditing;
}
function startCreating() {
$scope.isCreating = true;
$scope.isEditing = false;
resetCreateForm();
}
function cancelCreating() {
$scope.isCreating = false;
}
$scope.shouldShowCreating = shouldShowCreating;
$scope.startCreating = startCreating;
$scope.cancelCreating = cancelCreating;
function shouldShowEditing() {
return $scope.isEditing && !$scope.isCreating;
}
function startEditing() {
$scope.isCreating = false;
$scope.isEditing = true;
}
function cancelEditing() {
$scope.isEditing = false;
$scope.editedBookmark = null;
}
$scope.startEditing = startEditing;
$scope.cancelEditing = cancelEditing;
$scope.shouldShowEditing = shouldShowEditing;
})
;