-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathfolderController.js
More file actions
224 lines (198 loc) · 7.7 KB
/
folderController.js
File metadata and controls
224 lines (198 loc) · 7.7 KB
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
import openNote from "../openNote.js";
openNote.controller("folderController", ["$scope",
"$rootScope",
"$location",
"$routeParams",
"tagService",
"storageService",
"config",
"$timeout",
function($scope,
$rootScope,
$location,
$routeParams,
tagService,
storageService,
config,
$timeout) {
$rootScope.buttons = [];
$scope.folderEditMode = false;
$scope.currentFolder = {};
$scope.parentFolder = null;
$scope.currentFolderContents = [];
//add buttons
if ($routeParams.id)
$rootScope.buttons.push({
id: "newNote",
text: "New note",
action: function() {
$scope.fadeOutFoldersAndNotes(function() {
$location.url("/note/").search("folderID", $scope.currentFolder._id);
});
}
});
//Create a folder
$rootScope.buttons.push({
id: "newFolder",
text: "New folder",
action: function() {
var prompt = "Please enter a name for the new folder";
if ($scope.currentFolder.name)
prompt += " that will be created in " + $scope.currentFolder.name;
alertify.prompt(
prompt,
function(confirm, data) {
if (!confirm)
return;
var folder = {
parentFolderID: $scope.currentFolder._id,
name: data
};
createFolder(folder);
},
"");
}
});
if ($routeParams.id)
$rootScope.buttons.push({
text: "Cut",
action: function() {
$rootScope.clipboard = $scope.currentFolder;
alertify.success("Folder copied to clipboard");
}
});
if ($rootScope.clipboard && $rootScope.clipboard != $scope.currentFolder)
$rootScope.buttons.push({
text: "Paste",
action: function() {
$rootScope.$emit("moveKey", { //fire off an event to tell everyone we just modified a folder
destFolder: $scope.currentFolder,
moveObject: $rootScope.clipboard
});
$rootScope.clipboard = null;
}
});
$rootScope.buttons.push({
text: "Search",
action: function() {
$location.url("/search/");
}
});
/**
* Load current folder contents
*/
$scope.loadCurrentFolder = function() {
//Load the folder
if (!$routeParams.id) {
$scope.currentFolder = { //FIXME multiple DBs
_id: null,
name: "Home"
};
loadCurrentFolderContents();
} else {
storageService.get($routeParams.id).then(function(doc) {
$scope.currentFolder = doc;
loadCurrentFolderContents();
if (!$scope.currentFolder.parentFolderID)
$scope.parentFolder = {
name: "Home"
};
else
storageService.get($scope.currentFolder.parentFolderID).then(function(doc) {
$scope.parentFolder = doc;
$scope.$apply();
});
});
}
};
/**
* Activate folder edit mode if we are not in the home folder
*/
$scope.activateFolderEditMode = function() {
if ($scope.currentFolder._id)
$scope.folderEditMode = !$scope.folderEditMode;
};
/**
* Rename the current folder
*/
$scope.renameFolder = function() {
alertify.prompt("Rename " + $scope.currentFolder.name + " to:",
function(confirm, data) {
if (!confirm)
return;
$scope.currentFolder.name = data;
storageService.put($scope.currentFolder).then(function(result) {
$scope.currentFolder._rev = result.rev;
$rootScope.$emit("reloadListView", {});
$scope.$apply();
}).catch(function(error) {
throw error;
console.error(error);
//FIXME conflict resolution
});
},
$scope.currentFolder.name //show the current folder name
);
};
/**
* Remove this folder and all sub items
*/
$scope.removeFolder = function() { //FIXME Clear orphans
alertify.confirm("Are you sure you want to delete " + $scope.currentFolder.name + " and all subfolders and notes it contains?",
function(confirm) {
if (!confirm)
return;
var parentFolderID = $scope.currentFolder.parentFolderID;
tagService.deleteFolder($scope.currentFolder).then(function(){ // This needs to be done synchronously instead of an event because its possible for the storage service delete loop to get ahead and destoy the notes before the tag service has a change to delete them.
storageService.deleteFolder($scope.currentFolder, function() {
$rootScope.$emit("reloadListView", {});
if (!parentFolderID)
$location.url("/folder/");
else
$location.url("/folder/" + parentFolderID);
$scope.$apply();
});
});
});
};
/**
* Listen to changed folder events to see if its the current open folder
*/
$rootScope.$on("changedFolder", function(event, request) {
if (request.folder.parentFolderID == $scope.currentFolder.id || $scope.currentFolder.id == request.oldparentFolderID) { //does the change effect us?
$scope.loadCurrentFolder(); //reload
}
});
/**
* Create a folder object
*/
var createFolder = function(folder) {
folder.type = "folder";
storageService.post(folder).then(function(response) {
if (!response.ok){
alertify.error("There was an error creating the folder");
console.error(response);
throw response;
}
$rootScope.$emit("reloadListView", {});
$location.url("/folder/" + response.id);
$scope.$apply();
}).catch(function(error) {
alertify.error("There was an error creating the folder");
console.error(error);
throw error;
});
};
/**
* Load the current folders contents
*/
var loadCurrentFolderContents = function() {
storageService.loadFolderContents($scope.currentFolder._id).then(function(results) {
$scope.currentFolderContents = results.rows;
$scope.$apply();
});
};
//Load current folder
$timeout($scope.loadCurrentFolder);
}
]);