Skip to content

Commit 6001882

Browse files
committed
DRYed up folder logic for use in search. Use alldocs to greatly increase speed of search
1 parent 0324402 commit 6001882

File tree

10 files changed

+140
-203
lines changed

10 files changed

+140
-203
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: node_js
22
node_js:
3-
- 0.10
3+
- node
44
before_script:
55
- npm install -g grunt-cli

index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<!DOCTYPE html>
2-
<html ng-app="openNote" manifest="openNote.appcache" ng-strict-di>
2+
<!-- manifest="openNote.appcache" FIXME -->
3+
<html ng-app="openNote" ng-strict-di>
34
<head>
45
<meta charset="UTF-8">
56
<meta name="description" content="description">

openNote/controllers/folderController.js

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ openNote.controller("folderController", ["$scope",
8484
$rootScope.buttons.push({
8585
text: "Search",
8686
action: function() {
87-
$location.url("/search/" + $scope.currentFolder.id);
87+
$location.url("/search/");
8888
}
8989
});
9090

@@ -125,45 +125,7 @@ openNote.controller("folderController", ["$scope",
125125
$scope.folderEditMode = !$scope.folderEditMode;
126126
};
127127

128-
/**
129-
* fade out all folders
130-
*/
131-
$scope.fadeOutFoldersAndNotes = function(callback) {
132-
if ($scope.currentFolder.foldersInside || $scope.currentFolder.notesInside) {
133-
$(".note").fadeTo(config.fadeSpeedShort(), 0, function() {
134-
$scope.$apply(function() {
135-
callback();
136-
});
137-
});
138128

139-
$(".folder").fadeTo(config.fadeSpeedShort(), 0, function() {
140-
$scope.$apply(function() {
141-
callback();
142-
});
143-
});
144-
} else
145-
callback();
146-
};
147-
148-
/**
149-
* Load a folder
150-
* @param folder- the folder to load
151-
*/
152-
$scope.loadFolder = function(folder) {
153-
$scope.fadeOutFoldersAndNotes(function() {
154-
$location.url("/folder/" + folder.doc._id);
155-
});
156-
};
157-
158-
/**
159-
* Load a note
160-
* @param note - load a note
161-
*/
162-
$scope.loadNote = function(note) {
163-
$scope.fadeOutFoldersAndNotes(function() {
164-
$location.url("/note/" + note.id);
165-
});
166-
};
167129

168130
/**
169131
* Rename the current folder
@@ -250,20 +212,6 @@ openNote.controller("folderController", ["$scope",
250212
});
251213
};
252214

253-
/**
254-
* Filter out everything but type folder
255-
*/
256-
$scope.folderFilter = function(object) {
257-
return storageService.folderFilter(object);
258-
};
259-
260-
/**
261-
* Filter out everything but type note
262-
*/
263-
$scope.noteFilter = function(object) {
264-
return storageService.noteFilter(object);
265-
};
266-
267215
//Load current folder
268216
$timeout($scope.loadCurrentFolder);
269217
}

openNote/controllers/searchController.js

Lines changed: 25 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -7,99 +7,46 @@ openNote.controller("searchController", ["$scope",
77
"config",
88
"storageService",
99
"$location",
10+
"$routeParams",
11+
"$timeout",
1012
function($scope,
1113
$rootScope,
1214
config,
1315
storageService,
14-
$location) {
15-
/**
16-
* Default valie
17-
*/
18-
$scope.searchRequest = {
19-
type: "Both",
20-
field: "Both",
21-
search: ""
22-
};
16+
$location,
17+
$routeParams,
18+
$timeout) {
2319

24-
$scope.notes = null;
25-
$scope.folders = null;
20+
$scope.searchString = $routeParams.id; //Default
2621

27-
/**
28-
* Search the database
29-
*/
3022
$scope.search = function() {
23+
$location.url("/search/" + $scope.searchString);
24+
};
25+
26+
$scope.loadResults = function() {
27+
if (!$routeParams.id)
28+
return;
29+
3130
alertify.log("Search started");
32-
$scope.notes = [];
33-
$scope.folders = [];
31+
$scope.results = [];
3432

35-
var removeDuplicates = function(array) {
36-
var listOfIDs = [];
37-
array.forEach(function(element) { //for each is synchronous
38-
var index = listOfIDs.indexOf(element.id);
39-
if (index == -1) {
40-
listOfIDs.push(element.id);
41-
} else
42-
array.splice(index, 1);
33+
storageService.allDocs().then(function(result) {
34+
result.rows.filter(storageService.folderFilter).forEach(function(folder) { // search folders
35+
if (folder.doc.name.match($routeParams.id)) //search folder name
36+
return $scope.results.push(folder);
4337
});
44-
return array;
45-
};
4638

47-
var appendNotes = function(notes) {
48-
$scope.notes = removeDuplicates($scope.notes.concat(notes));
49-
$scope.$apply();
50-
};
51-
52-
var appendFolders = function(folders) {
53-
$scope.folders = $scope.folders.concat(folders);
54-
alertify.success(folders.length + " objects found");
39+
result.rows.filter(storageService.noteFilter).forEach(function(note) { //Search notes
40+
if (note.doc.title.match($routeParams.id) || note.doc.note.match($routeParams.id)) //search note name and title
41+
return $scope.results.push(note);
42+
});
5543
$scope.$apply();
56-
};
57-
58-
var type = $scope.searchRequest.type;
59-
var search = $scope.searchRequest.search;
60-
var field = $scope.searchRequest.field;
61-
62-
if (type == "Both" || type == "Folders")
63-
storageService.searchFolderNames(search, appendFolders);
64-
65-
if (type == "Both" || type == "Notes") {
66-
if (field == "Both" || type == "Title")
67-
storageService.searchNoteTitles(search, appendNotes);
68-
69-
if (field == "Both" || type == "Body")
70-
storageService.searchNoteBody(search, appendNotes);
71-
}
72-
};
73-
74-
/**
75-
* Load a folder
76-
* @param folder- the folder to load
77-
*/
78-
$scope.loadFolder = function(folder) {
79-
$scope.fadeOutBoxes(function() {
80-
$location.url("/folder/" + folder.doc._id);
44+
alertify.success($scope.results.length + " objects found");
8145
});
82-
};
8346

84-
/**
85-
* Load a note
86-
* @param note - load a note
87-
*/
88-
$scope.loadNote = function(note) {
89-
$scope.fadeOutBoxes(function() {
90-
$location.url("/note/" + note.doc._id);
91-
});
9247
};
9348

94-
/**
95-
* fade out all boxes
96-
*/
97-
$scope.fadeOutBoxes = function(callback) {
98-
$(".box").fadeOut(config.fadeSpeedShort(), function() {
99-
$scope.$apply(function() {
100-
callback();
101-
});
102-
});
103-
};
49+
//Load results
50+
$timeout($scope.loadResults);
10451
}
10552
]);

openNote/directives/fadeOutDirective.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import openNote from "../openNote.js";
2-
/**
3-
*randomly fade in element to have a wave effect
4-
*/
2+
53
openNote.directive("fadeOutDirective", function() {
64
return {
75
restrict: "C",//class
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import openNote from "../openNote.js";
2+
3+
openNote.directive("folderContentsDirective", [
4+
"config",
5+
"storageService",
6+
"$location",
7+
function(config, storageService, $location) {
8+
return {
9+
restrict: "E", //class
10+
templateUrl: "openNote/partials/directives/folderContentsPartial.html",
11+
scope: {
12+
fadeOut: "=", //basically make the fadeOut method public
13+
contents: "<" //Accept contents as a parameter
14+
},
15+
link: function($scope) {
16+
/**
17+
* fade out all folders
18+
*/
19+
$scope.fadeOut = function(callback) {
20+
$(".note").fadeTo(config.fadeSpeedShort(), 0, function() {
21+
$scope.$apply(function() {
22+
callback();
23+
});
24+
});
25+
26+
$(".folder").fadeTo(config.fadeSpeedShort(), 0, function() {
27+
$scope.$apply(function() {
28+
callback();
29+
});
30+
});
31+
};
32+
33+
/**
34+
* Load a folder
35+
* @param folder- the folder to load
36+
*/
37+
$scope.loadFolder = function(folder) { //TODO DRY
38+
$scope.fadeOut(function() {
39+
$location.url("/folder/" + folder.doc._id);
40+
});
41+
};
42+
43+
/**
44+
* Load a note
45+
* @param note - load a note
46+
*/
47+
$scope.loadNote = function(note) { //TODO DRY
48+
$scope.fadeOut(function() {
49+
$location.url("/note/" + note.id);
50+
});
51+
};
52+
53+
/**
54+
* Filter out everything but type folder
55+
*/
56+
$scope.folderFilter = function(object) {
57+
return storageService.folderFilter(object);
58+
};
59+
60+
/**
61+
* Filter out everything but type note
62+
*/
63+
$scope.noteFilter = function(object) {
64+
return storageService.noteFilter(object);
65+
};
66+
}
67+
};
68+
}]);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<div ng-repeat="folder in contents.filter(folderFilter) | orderBy: 'doc.name'"
2+
class="startHidden randomFadeInDirective box folder"
3+
ng-click="loadFolder(folder);">
4+
<h4>
5+
{{folder.doc.name}}
6+
</h4>
7+
<p class="box_description"></p>
8+
<p class="options">
9+
Folder
10+
</p>
11+
</div>
12+
13+
<div ng-repeat="note in contents.filter(noteFilter) | orderBy: 'doc.title'"
14+
class="startHidden box randomFadeInDirective note"
15+
ng-click="loadNote(note);">
16+
<h4>
17+
{{note.doc.title}}
18+
</h4>
19+
<p class="box_description"></p>
20+
<p class="options">
21+
<span class="right">Note</span>
22+
</p>
23+
</div>

openNote/partials/folderPartial.html

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,7 @@
1616

1717
</div>
1818

19-
<div ng-repeat="folder in currentFolderContents.filter(folderFilter) | orderBy: 'doc.name'"
20-
class="startHidden randomFadeInDirective box folder"
21-
ng-click="loadFolder(folder);">
22-
<h4>
23-
{{folder.doc.name}}
24-
</h4>
25-
<p class="box_description"></p>
26-
<p class="options">
27-
Folder
28-
</p>
29-
</div>
30-
31-
<div ng-repeat="note in currentFolderContents.filter(noteFilter) | orderBy: 'doc.title'"
32-
class="startHidden box randomFadeInDirective note"
33-
ng-click="loadNote(note);">
34-
<h4>
35-
{{note.doc.title}}
36-
</h4>
37-
<p class="box_description"></p>
38-
<p class="options">
39-
<span class="right">Note</span>
40-
</p>
41-
</div>
19+
<folder-contents-directive contents="currentFolderContents" fade-out="fadeOutFoldersAndNotes"></folder-contents-directive>
4220

4321
<div class="noFolderHelp startHidden randomFadeInDirective" ng-if="!currentFolderContents.length">
4422
<h4 ng-if="!currentFolder._id">It looks like you dont have any folders. You can create one using the "New Folder" button in the top right of the page. If you need to pull your remote notes <a href='#/settings/database'>click here</a>.</h4>

0 commit comments

Comments
 (0)