Skip to content

TagSpaces is an offline, open source, document manager with tagging support

implementing new search functionality

uggrock63aa991e728a
diff --git a/data/index.html b/data/index.html
index 873972432..c09e97ced 100644
--- a/data/index.html
+++ b/data/index.html
@@ -222,7 +222,7 @@
<div class="flexMaxWidth"></div>
<div id="searchToolbar">
<div class="flexLayout">
- <button class="btn btn-link" id="clearSearchButton">
+ <button class="btn btn-link" id="clearSearchButton" data-i18n="[title]ns.common:cancelSearchTooltip">
<i class="fa fa-lg fa-eraser"></i>
</button>
<input class="flexMaxWidth" id="searchBox" data-i18n="[placeholder]ns.common:searchPlaceholder" type="text">
@@ -877,28 +877,29 @@
<input type="text" class="form-control searchFormElements" id="searchTags" placeholder="Tags separated by spaces">
</div>
<div class="form-group">
- <label for="searchFileType">Type</label>
+ <label for="searchFileType">File Type (e.g. documents, pictures, ...)</label>
<select id="searchFileType" class="form-control searchFormElements">
<option value="">Specify file type</option>
- <option value="t:pictures">Pictures (JPG,PNG,GIF,...)</option>
- <option value="t:docs">Documents (PDF,ODF,DOCX,EXL,...)</option>
- <option value="t:audio">Audio (OGG,MP3,...)</option>
+ <option value="t:picture">Pictures (JPG,PNG,GIF,...)</option>
+ <option value="t:doc">Documents (PDF,ODF,DOCX,EXL,...)</option>
+ <option value="t:note">Notes (MD,TXT,HTML,...)</option>
+ <option value="t:audio">Audio (OGG,MP3,WAV,...)</option>
<option value="t:video">Video files (WEBM,OGV,MP4,...)</option>
- <option value="t:archives">Archives (ZIP,RAR,TGZ,...)</option>
+ <option value="t:archive">Archives (ZIP,RAR,TGZ,7Z,...)</option>
</select>
</div>
- <!-- div class="form-group">
- <label for="searchFileType" class="col-sm-2 control-label">History</label>
- <select id="" class="form-control">
- <option>of your last 15 searches</option>
+ <div class="form-group">
+ <label for="searchFileType" class="control-label">History of your last searches</label>
+ <select id="searchHistory" class="form-control searchFormElements" disabled title="TagSpaces PRO feature">
+ <option>Choose one of your last searches</option>
<option value="t:pictures +topsy">t:pictures +topsy</option>
<option value="t:pictures +danny">t:pictures +danny</option>
<option value="t:docs +work">t:docs +work</option>
<option value="t:archives +client1">t:archives +client1</option>
<option value="t:audio u2">t:audio u2</option>
- <option value="type:pictures +topsy">Clear all</option>
+ <option value="clear">Clear search history</option>
</select>
- </div -->
+ </div>
<div class="checkbox">
<label>
<input type="checkbox" id="searchRecursive" checked>&nbsp;<span>Search in subfolders</span>
diff --git a/data/js/search.js b/data/js/search.js
index bc507dd3e..c2630a57a 100644
--- a/data/js/search.js
+++ b/data/js/search.js
@@ -9,35 +9,17 @@ define(function(require, exports, module) {
var currentQuery = '';
var nextQuery = '';
- function calculateTags(data) {
- console.log('Calculating tags from search results');
- var allTags = [];
- data.forEach(function(fileEntry) {
- fileEntry.tags.forEach(function(tag) {
- allTags.push(tag.toLowerCase());
- });
- });
- var countData = _.countBy(allTags, function(obj) {
- return obj;
- });
- TSCORE.calculatedTags.length = 0;
- _.each(countData, function(count, tag) {
- TSCORE.calculatedTags.push({
- 'title': tag,
- 'type': 'plain',
- 'count': count
- });
- });
- TSCORE.generateTagGroups();
- }
+ var caseSensitiveSearch = false;
function prepareQuery(queryText) {
// cleaning up the query, reducing the spaces
var queryText = queryText.toLowerCase().replace(/^\s+|\s+$/g, '');
+
var recursive = queryText.indexOf('~') !== 0;
- if (recursive) {
+ if (!recursive) {
queryText = queryText.substring(1, queryText.length);
}
+
var queryTerms = queryText.split(' ');
var queryObj = {
includedTerms: [],
@@ -45,61 +27,70 @@ define(function(require, exports, module) {
includedTags: [],
excludedTags: [],
recursive: recursive,
+ fileTypeFilter: function() {
+ return true
+ },
};
- // parsing the query
+ // parsing the query terms
queryTerms.forEach(function(value) {
- if (value.length > 1) {
- if (value.indexOf('!') === 0) {
- queryObj.excludedTerms.push([
- value.substring(1, value.length),
- false
- ]);
- } else if (value.indexOf('+') === 0) {
- queryObj.includedTags.push([
- value.substring(1, value.length),
- true
- ]);
- } else if (value.indexOf('-') === 0) {
- queryObj.excludedTags.push([
- value.substring(1, value.length),
- true
- ]);
- } else {
- queryObj.includedTerms.push([
- value,
- false
- ]);
- }
+ if (value.length <= 1) {
+ return;
+ }
+
+ if (TSCORE.PRO && value.indexOf('t:picture') === 0) {
+ queryObj.fileTypeFilter = TSCORE.PRO.Search.filterPictures;
+ } else if (TSCORE.PRO && value.indexOf('t:note') === 0) {
+ queryObj.fileTypeFilter = TSCORE.PRO.Search.filterNotes;
+ } else if (TSCORE.PRO && value.indexOf('t:doc') === 0) {
+ queryObj.fileTypeFilter = TSCORE.PRO.Search.filterDocuments;
+ } else if (TSCORE.PRO && value.indexOf('t:audio') === 0) {
+ queryObj.fileTypeFilter = TSCORE.PRO.Search.filterAudioFiles;
+ } else if (TSCORE.PRO && value.indexOf('t:video') === 0) {
+ queryObj.fileTypeFilter = TSCORE.PRO.Search.filterVideoFiles;
+ } else if (TSCORE.PRO && value.indexOf('t:archive') === 0) {
+ queryObj.fileTypeFilter = TSCORE.PRO.Search.filterArchives;
+ } else if (value.indexOf('!') === 0) {
+ queryObj.excludedTerms.push([value.substring(1, value.length), false]);
+ } else if (value.indexOf('+') === 0) {
+ queryObj.includedTags.push([value.substring(1, value.length), true]);
+ } else if (value.indexOf('-') === 0) {
+ queryObj.excludedTags.push([value.substring(1, value.length), true]);
+ } else {
+ queryObj.includedTerms.push([value, false]);
}
});
return queryObj;
}
- function fileContentFilter(filePath) {
-
- return /\.(html|txt|xml|md|json)$/i.test(filePath);
+ function filterTextBasedFiles(filePath) {
+ // Returning only text based files
+ return /\.(html|htm|txt|xml|md|mdown|json)$/i.test(filePath);
}
function filterFileObject(fileEntry, queryObj) {
var parentDir = TSCORE.TagUtils.extractParentDirectoryPath(fileEntry.path).toLowerCase();
var searchIn = fileEntry.name.toLowerCase();
- var tags;
+ var fileNameTags;
+
+ //if(!queryObj.fileTypeFilter(searchIn)) {
+ // return false;
+ //}
- // TODO consider tags in .ts/meta.json and ./ts/ts.json
if (fileEntry.tags) {
- tags = fileEntry.tags;
+ fileNameTags = fileEntry.tags;
} else {
- tags = TSCORE.TagUtils.extractTags(fileEntry.path);
+ fileNameTags = TSCORE.TagUtils.extractTags(fileEntry.path);
}
var result = true;
- if (tags.length < 1 && queryObj.includedTags.length > 0) {
+ if (fileNameTags.length < 1 && queryObj.includedTags.length > 0) {
return false;
}
for (var i = 0; i < queryObj.includedTerms.length; i++) {
+ // Considers the parent directory name in the search results
if ((parentDir + searchIn).indexOf(queryObj.includedTerms[i][0]) >= 0) {
queryObj.includedTerms[i][1] = true;
} else {
@@ -115,8 +106,8 @@ define(function(require, exports, module) {
}
for (var i = 0; i < queryObj.includedTags.length; i++) {
queryObj.includedTags[i][1] = false;
- for (var j = 0; j < tags.length; j++) {
- if (tags[j].toLowerCase() == queryObj.includedTags[i][0]) {
+ for (var j = 0; j < fileNameTags.length; j++) {
+ if (fileNameTags[j].toLowerCase() == queryObj.includedTags[i][0]) {
queryObj.includedTags[i][1] = true;
}
}
@@ -126,8 +117,8 @@ define(function(require, exports, module) {
}
for (var i = 0; i < queryObj.excludedTags.length; i++) {
queryObj.excludedTags[i][1] = true;
- for (var j = 0; j < tags.length; j++) {
- if (tags[j].toLowerCase() == queryObj.excludedTags[i][0]) {
+ for (var j = 0; j < fileNameTags.length; j++) {
+ if (fileNameTags[j].toLowerCase() == queryObj.excludedTags[i][0]) {
queryObj.excludedTags[i][1] = false;
}
}
@@ -138,27 +129,23 @@ define(function(require, exports, module) {
return result;
}
+ function matchArrays(array1, array2) {
+ //var match = [];
+ for(i in array1) {
+ if(array2.indexOf( array1[i] ) > -1){
+ return true;
+ //match.push( array1[i] );
+ }
+ }
+ return false; //match;
+ }
+
function searchData(data, query) {
- //todo make a switch in gui for content search
- var searchInContent = (isChrome || isFirefox || isWeb) ? false : true;
+ // TODO make a switch in gui for content search
+ var searchContentSupported = (isChrome || isFirefox || isWeb) ? false : true;
var queryObj = prepareQuery(query);
-
var searchResults = [];
- var scan = function(content, fileEntry) {
- return new Promise(function(resolve, reject) {
- var found = false;
- queryObj.includedTerms.forEach(function(term) {
- if (content.indexOf(term[0]) >= 0) {
- found = true;
- }
- if (found) {
- console.log("Term " + term[0] + " found in " + fileEntry.path);
- searchResults.push(fileEntry);
- }
- });
- resolve();
- });
- };
+ var metaDirPattern = TSCORE.dirSeparator + TSCORE.metaFolder + TSCORE.dirSeparator;
if (query.length > 0) {
TSCORE.showWaitingDialog($.i18n.t("ns.common:waitDialogDiectoryIndexing"));
@@ -166,29 +153,64 @@ define(function(require, exports, module) {
TSCORE.IOUtils.walkDirectory(TSCORE.currentPath, {recursive: queryObj.recursive},
function(fileEntry) {
return new Promise(function(resolve, reject) {
- if (filterFileObject(fileEntry, queryObj)) {
+ var indexOfMetaDirectory = fileEntry.path.indexOf(metaDirPattern);
+
+ // Searching in file names while skipping paths containing '/.ts/'
+ if (indexOfMetaDirectory < 1 && filterFileObject(fileEntry, queryObj)
+ && queryObj.fileTypeFilter(fileEntry.name.toLowerCase())) {
searchResults.push(fileEntry);
+ resolve();
}
- if (searchInContent && fileContentFilter(fileEntry.name)) {
- TSCORE.IO.loadTextFilePromise(fileEntry.path).then(function(content) {
- //return scan(content, fileEntry);
- var found = false;
- queryObj.includedTerms.forEach(function(term) {
- if (content.indexOf(term[0]) >= 0) {
- console.log("Term " + term[0] + " found in " + fileEntry.path);
- found = true;
+
+ // Searching in content
+ if (searchContentSupported && filterTextBasedFiles(fileEntry.name)) { // Search in content
+ TSCORE.IO.getFileContentPromise(fileEntry.path, "text").then(function(content) {
+ var found;
+ var metaExtLocation = fileEntry.path.lastIndexOf(TSCORE.metaFileExt); // .json
+
+ // Checking for matching tags, parsing meta JSONs located in ../.ts/ folders
+ if (indexOfMetaDirectory > 0 && metaExtLocation > indexOfMetaDirectory) {
+ try {
+ var metaData = JSON.parse(content);
+ if(metaData.tags && metaData.tags.length > 0 && queryObj.includedTags.length > 0) {
+ // Checking if both tag arrays have same members
+ for (var i = 0; i < metaData.tags.length; i++) {
+ for (var j = 0; j < queryObj.includedTags.length; j++) {
+ if (queryObj.includedTags[j][0] === (metaData.tags[i].title.toLowerCase())){
+ queryObj.includedTags[j][1] = true;
+ found = true;
+ }
+ }
+ }
+ // Logicaling AND-ing the result
+ for (var j = 0; j < queryObj.includedTags.length; j++) {
+ found = found & queryObj.includedTags[j][1];
+ queryObj.includedTags[j][1] = false;
+ }
+ }
+ } catch(err) {
+ console.log("Error " + err + " parsing JSON from: " + fileEntry.path);
}
- });
+ }
+
+ if (!found) {
+ // Searching in the content
+ queryObj.includedTerms.forEach(function(term) {
+ if (content.indexOf(term[0]) >= 0) {
+ console.log("Term " + term[0] + " found in " + fileEntry.path);
+ found = true;
+ }
+ });
+ }
+
if (found) {
- var indexOfMetaDirectory = fileEntry.path.indexOf(TSCORE.dirSeparator + TSCORE.metaFolder + TSCORE.dirSeparator);
if (indexOfMetaDirectory > 0) { // file is in the meta folder
- var metaExtLocation = fileEntry.path.lastIndexOf(TSCORE.metaFileExt);
- var contentExtLocation = fileEntry.path.lastIndexOf(TSCORE.contentFileExt);
- var metaFolderLocation = fileEntry.path.lastIndexOf(TSCORE.metaFolderFile);
+
+ var contentExtLocation = fileEntry.path.lastIndexOf(TSCORE.contentFileExt); // .txt
+ var metaFolderLocation = fileEntry.path.lastIndexOf(TSCORE.metaFolderFile); // .ts
// file is meta file (json) and not tsm.json
if (metaExtLocation > indexOfMetaDirectory && metaFolderLocation < 0) {
- // TODO parse file and find tags
fileEntry.name = fileEntry.name.substring(0, fileEntry.name.indexOf(TSCORE.metaFileExt));
fileEntry.path = fileEntry.path.substring(0, indexOfMetaDirectory + 1) + fileEntry.name;
}
@@ -203,14 +225,28 @@ define(function(require, exports, module) {
if (metaFolderLocation > indexOfMetaDirectory) {
fileEntry.path = fileEntry.path.substring(0, indexOfMetaDirectory + 1);
fileEntry.name = TSCORE.TagUtils.extractDirectoryName(fileEntry.path) + "." + TSCORE.directoryExt;
+ fileEntry.isDirectory = true;
}
- // TODO try to get the properties of the file and update the size and lmdt
- fileEntry.size = 0; // evtl lmdt = 0
- searchResults.push(fileEntry);
+ if (!fileEntry.isDirectory) { // TODO check if the main file exists
+ // TSCORE.IO.getPropertiesPromise(fileEntry.path).then(function(mainFileEntry) {
+ // searchResults.push(mainFileEntry);
+ searchResults.push(fileEntry);
+ resolve();
+ // }, function() {
+ // console.log("main file does not exist anymore " + fileEntry.path);
+ // resolve();
+ // })
+ } else { // by tsm.json files
+ fileEntry.size = 0;
+ fileEntry.lmdt = 0;
+ searchResults.push(fileEntry);
+ resolve();
+ }
}
+ } else {
+ resolve();
}
- resolve();
}, function(err) {
resolve();
console.log("Failed loading content for: " + fileEntry.path);
@@ -239,18 +275,41 @@ define(function(require, exports, module) {
} else {
if (TSCORE.Config.getCalculateTags()) {
// Find all tags in the current search results
- exports.calculateTags(data);
+ calculateTags(data);
}
return data;
}
}
+ function calculateTags(data) {
+ console.log('Calculating tags from search results');
+
+ // TODO consider tags in sidecar files
+ var allTags = [];
+ data.forEach(function(fileEntry) {
+ fileEntry.tags.forEach(function(tag) {
+ allTags.push(tag.toLowerCase());
+ });
+ });
+ var countData = _.countBy(allTags, function(obj) {
+ return obj;
+ });
+ TSCORE.calculatedTags.length = 0;
+ _.each(countData, function(count, tag) {
+ TSCORE.calculatedTags.push({
+ 'title': tag,
+ 'type': 'plain',
+ 'count': count
+ });
+ });
+ TSCORE.generateTagGroups();
+ }
+
// Public variables definition
exports.currentQuery = currentQuery;
exports.nextQuery = nextQuery;
// Public API definition
exports.searchData = searchData;
- exports.searchForTag = searchForTag;
exports.calculateTags = calculateTags;
});
diff --git a/data/js/search.ui.js b/data/js/search.ui.js
index e0100d395..b692ae1c9 100644
--- a/data/js/search.ui.js
+++ b/data/js/search.ui.js
@@ -40,6 +40,7 @@ define(function(require, exports, module) {
$('#startSearchButton').on("click", function(e) {
e.preventDefault();
+ updateQuery();
startSearch();
});
@@ -49,7 +50,6 @@ define(function(require, exports, module) {
$('#searchOptions').on('click', '.close', function() {
$('#searchOptions').hide();
- cancelSearch();
});
$('#resetSearchButton').on('click', function(e) {
@@ -81,9 +81,7 @@ define(function(require, exports, module) {
});
};
- function parseQuery() {
-
- }
+ //function parseQuery() {}
function updateQuery() {
var query = "";
@@ -118,6 +116,7 @@ define(function(require, exports, module) {
console.log();
$('#searchBox').val(query);
+ TSCORE.Search.nextQuery = query;
}
function resetSearchOptions() {
@@ -125,6 +124,7 @@ define(function(require, exports, module) {
$('#searchTerms').val("");
$('#searchTags').val("");
$('#searchFileType').val("");
+ $('#searchHistory').val("");
}
function showSearchOptions() {