Skip to content

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

Refactoring tagsutils to support AMD

uggrock1abff27af5b9
diff --git a/data/css/tagspaces.less b/data/css/tagspaces.less
index f972cb794..62abe1cae 100644
--- a/data/css/tagspaces.less
+++ b/data/css/tagspaces.less
@@ -243,7 +243,7 @@ li hr {
#tagsContainer {
width: 100%;
- height: 100%;
+ height: 50px;
}
#viewToolbars {
diff --git a/data/ext/view.basic.js b/data/ext/view.basic.js
index dcace2367..9850c78c9 100644
--- a/data/ext/view.basic.js
+++ b/data/ext/view.basic.js
@@ -146,7 +146,7 @@ exports.init = function init() {
"aTargets": [ 4 ]
},
{ // Last changed date column
- "mRender": function ( data, type, row ) { return TSAPI.formatDateTime(data, true) },
+ "mRender": function ( data, type, row ) { return UIAPI.TagUtils.formatDateTime(data, true) },
"aTargets": [ 2 ]
},
{ "bVisible": false, "aTargets": [ 5 ] },
@@ -175,7 +175,7 @@ exports.init = function init() {
$(".ui-selected", this).each(function(){
var rowData = fileTable.fnGetData( this );
// Add the filename which is located in the first column to the list of selected filenames
- UIAPI.selectedFiles.push(UIAPI.currentPath + TSAPI.getDirSeparator() + rowData[0]);
+ UIAPI.selectedFiles.push(UIAPI.currentPath + UIAPI.TagUtils.DIR_SEPARATOR + rowData[0]);
});
console.debug("Selected files: "+UIAPI.selectedFiles);
}
@@ -222,12 +222,12 @@ exports.load = function load() {
$(this).toggleClass("ui-selected");
- var targetFilePath = UIAPI.currentPath + TSAPI.getDirSeparator() + targetFile;
+ var targetFilePath = UIAPI.currentPath + UIAPI.TagUtils.DIR_SEPARATOR + targetFile;
exports.clearSelectedFiles();
UIAPI.selectedFiles.push(targetFilePath);
- TSAPI.addTag(UIAPI.selectedFiles, tagName);
+ UIAPI.TagUtils.addTag(UIAPI.selectedFiles, tagName);
IOAPI.listDirectory(UIAPI.currentPath);
}
@@ -236,7 +236,7 @@ exports.load = function load() {
console.debug("Opening file...");
var rowData = fileTable.fnGetData( this );
- UIAPI.openFile(UIAPI.currentPath+TSAPI.getDirSeparator()+rowData[0]);
+ UIAPI.openFile(UIAPI.currentPath+UIAPI.TagUtils.DIR_SEPARATOR+rowData[0]);
} );
fileTable.$('.fileButton')
@@ -277,6 +277,15 @@ exports.load = function load() {
.dropdown( 'attach' , '#extensionMenu' );
fileTable.$('.tagButton')
+ .draggable({
+ cancel:false,
+ appendTo: "body",
+ helper: "clone",
+ revert: true,
+ start: function() {
+ selectFile(this, $(this).attr("filepath"));
+ }
+ })
.click( function() {
selectFile(this, $(this).attr("filepath"));
TagsUI.openTagMenu(this, $(this).attr("tag"), $(this).attr("filepath"));
diff --git a/data/ext/view.search.js b/data/ext/view.search.js
index 4de91de1c..905d5689b 100644
--- a/data/ext/view.search.js
+++ b/data/ext/view.search.js
@@ -90,11 +90,11 @@ exports.init = function init() {
"aTargets": [ 1 ]
},
{ // Filesize column
- "mRender": function ( data, type, row ) { return TSAPI.formatFileSize(data) },
+ "mRender": function ( data, type, row ) { return UIAPI.TagUtils.formatFileSize(data) },
"aTargets": [ 2 ]
},
{ // Last changed date column
- "mRender": function ( data, type, row ) { return TSAPI.formatDateTime(data, true) },
+ "mRender": function ( data, type, row ) { return UIAPI.TagUtils.formatDateTime(data, true) },
"aTargets": [ 3 ]
},
// { "bVisible": false, "aTargets": [ 5 ] },
@@ -175,9 +175,9 @@ var enhanceIndexData = function(index) {
(!TSSETTINGS.Settings["showUnixHiddenEntries"] && (index[i].name.indexOf(".") != 0))) {
filename = index[i].name;
path = index[i].path;
- tags = TSAPI.extractTags(path);
- title = TSAPI.extractTitle(path);
- ext = TSAPI.extractFileExtension(path)
+ tags = UIAPI.TagUtils.extractTags(path);
+ title = UIAPI.TagUtils.extractTitle(path);
+ ext = UIAPI.TagUtils.extractFileExtension(path)
fileSize = index[i].size;
fileLMDT = index[i].lmdt;
diff --git a/data/ext/viewThumb/extension.js b/data/ext/viewThumb/extension.js
index ff2ff8847..f0e553c7f 100644
--- a/data/ext/viewThumb/extension.js
+++ b/data/ext/viewThumb/extension.js
@@ -77,8 +77,8 @@ exports.load = function load() {
for (var i=0; i < UIAPI.fileList.length; i++) {
var fileName = UIAPI.fileList[i][0];
- var fileExt = TSAPI.extractFileExtension(fileName);
- var filePath = UIAPI.currentPath+TSAPI.getDirSeparator()+fileName;
+ var fileExt = UIAPI.TagUtils.extractFileExtension(fileName);
+ var filePath = UIAPI.currentPath+UIAPI.TagUtils.DIR_SEPARATOR+fileName;
if(supportedFileTypeThumnailing.indexOf(fileExt) >= 0) {
$("#"+exports.ID+"SelectableFiles").append(
$('<li>', { title: fileName, filepath: filePath, class: 'ui-widget-content' }).append(
diff --git a/data/js/core.api.js b/data/js/core.api.js
index 193cf268b..b2d6e91fd 100644
--- a/data/js/core.api.js
+++ b/data/js/core.api.js
@@ -37,6 +37,8 @@ UIAPI.selectedTagData = "";
UIAPI.ViewManager = undefined;
+UIAPI.TagUtils = undefined;
+
UIAPI.setCurrentPath = function(path) {
console.debug("Setting current path to: "+path);
UIAPI.currentPath = path;
@@ -82,6 +84,7 @@ UIAPI.updateFileBrowserData = function(dirList) {
var title = undefined;
var fileSize = undefined;
var fileLMDT = undefined;
+ var path = undefined;
// Sort the dir list alphabetically before displaying
// TODO sorting files not working correctly
dirList.sort(function(a,b) { return a.name.localeCompare(b.name); });
@@ -90,9 +93,9 @@ UIAPI.updateFileBrowserData = function(dirList) {
// Considering Unix HiddenEntries (. in the beginning)
if (TSSETTINGS.Settings["showUnixHiddenEntries"] ||
(!TSSETTINGS.Settings["showUnixHiddenEntries"] && (dirList[i].name.indexOf(".") != 0))) {
- path = UIAPI.currentPath + TSAPI.getDirSeparator() + dirList[i].name;
- tags = TSAPI.extractTags(path);
- title = TSAPI.extractTitle(path);
+ path = UIAPI.currentPath + UIAPI.TagUtils.DIR_SEPARATOR + dirList[i].name;
+ tags = UIAPI.TagUtils.extractTags(path);
+ title = UIAPI.TagUtils.extractTitle(path);
if(dirList[i].name.lastIndexOf(".") > 0) {
// title = dirList[i].name.substring(0, dirList[i].name.lastIndexOf("."));
ext = dirList[i].name.substring(dirList[i].name.lastIndexOf(".")+1,dirList[i].name.length);
@@ -116,7 +119,7 @@ UIAPI.updateFileBrowserData = function(dirList) {
UIAPI.changeDirectory = function(newDir) {
console.debug("Change direcotory to: "+newDir);
var newPath = UIAPI.currentPath;
- if(TSAPI.isWindows()) {
+ if(UIAPI.TagUtils.isWindows()) {
// Cutting trailig \\ or \\\\ .
if(UIAPI.currentPath.lastIndexOf("\\")+1 == UIAPI.currentPath.length) {
newPath = UIAPI.currentPath.substring(0,UIAPI.currentPath.length-1);
diff --git a/data/js/core.ui.js b/data/js/core.ui.js
index 855c41646..e203f8b97 100644
--- a/data/js/core.ui.js
+++ b/data/js/core.ui.js
@@ -141,7 +141,7 @@ var initDialogs = function() {
bValid = false;
}
if ( bValid ) {
- IOAPI.saveTextFile(UIAPI.currentPath+TSAPI.getDirSeparator()+$( "#newFileName" ).val(),fileContent);
+ IOAPI.saveTextFile(UIAPI.currentPath+UIAPI.TagUtils.DIR_SEPARATOR+$( "#newFileName" ).val(),fileContent);
$( this ).dialog( "close" );
IOAPI.listDirectory(UIAPI.currentPath);
}
@@ -172,10 +172,10 @@ var initDialogs = function() {
bValid = bValid && checkLength( renamedFileName, "filename", 3, 200 );
// bValid = bValid && checkRegexp( renamedFileName, /^[a-z]([0-9a-z_.])+$/i, "Filename may consist of a-z, 0-9, underscores, begin with a letter." );
if ( bValid ) {
- var containingDir = TSAPI.extractContainingDirectoryPath(UIAPI.selectedFiles[0]);
+ var containingDir = UIAPI.TagUtils.extractContainingDirectoryPath(UIAPI.selectedFiles[0]);
IOAPI.renameFile(
UIAPI.selectedFiles[0],
- containingDir+TSAPI.getDirSeparator()+renamedFileName.val()
+ containingDir+UIAPI.TagUtils.DIR_SEPARATOR+renamedFileName.val()
);
$( this ).dialog( "close" );
}
@@ -188,7 +188,7 @@ var initDialogs = function() {
allFields.val( "" ).removeClass( "ui-state-error" );
},
open: function() {
- $( "#renamedFileName" ).val(TSAPI.extractFileName(UIAPI.selectedFiles[0]));
+ $( "#renamedFileName" ).val(UIAPI.TagUtils.extractFileName(UIAPI.selectedFiles[0]));
}
});
@@ -217,7 +217,7 @@ var initDialogs = function() {
buttons: {
"Add tags": function() {
var tags = $("#tags").val().split(",");
- TSAPI.writeTagsToFile(UIAPI.selectedFiles[0], tags);
+ UIAPI.TagUtils.writeTagsToFile(UIAPI.selectedFiles[0], tags);
// TODO ? search
IOAPI.listDirectory(UIAPI.currentPath);
@@ -295,7 +295,7 @@ var initDialogs = function() {
modal: true,
buttons: {
"Save": function() {
- TSAPI.renameTag(UIAPI.selectedFiles[0], UIAPI.selectedTag, $( "#newTag" ).val());
+ UIAPI.TagUtils.renameTag(UIAPI.selectedFiles[0], UIAPI.selectedTag, $( "#newTag" ).val());
IOAPI.listDirectory(UIAPI.currentPath);
$( this ).dialog( "close" );
},
diff --git a/data/js/directories.ui.js b/data/js/directories.ui.js
index 91242267a..3aca9da05 100644
--- a/data/js/directories.ui.js
+++ b/data/js/directories.ui.js
@@ -80,10 +80,10 @@ DirectoriesUI.generateDirPath = function() {
hoverClass: "dirButtonActive",
drop: function( event, ui ) {
var filePath = ui.draggable.attr("filepath");
- var fileName = TSAPI.extractFileName(filePath);
+ var fileName = UIAPI.TagUtils.extractFileName(filePath);
var targetDir = $(this).attr("key");
console.log("Moving file: "+filePath+" to "+targetDir);
- IOAPI.renameFile(filePath, targetDir+TSAPI.getDirSeparator()+fileName);
+ IOAPI.renameFile(filePath, targetDir+UIAPI.TagUtils.DIR_SEPARATOR+fileName);
}
})
.click(function() {
@@ -123,10 +123,10 @@ DirectoriesUI.generateDirPath = function() {
hoverClass: "dirButtonActive",
drop: function( event, ui ) {
var filePath = ui.draggable.attr("filepath");
- var fileName = TSAPI.extractFileName(filePath);
+ var fileName = UIAPI.TagUtils.extractFileName(filePath);
var targetDir = $(this).attr("key");
console.log("Moving file: "+filePath+" to "+targetDir);
- IOAPI.renameFile(filePath, targetDir+TSAPI.getDirSeparator()+fileName);
+ IOAPI.renameFile(filePath, targetDir+UIAPI.TagUtils.DIR_SEPARATOR+fileName);
}
})
.click( function() {
@@ -196,7 +196,7 @@ DirectoriesUI.navigateToDirectory = function(directoryPath) {
// If directory path not in history then add it to the history
if(directoryFoundOn < 0) {
- var parentLocation = directoryPath.substring(0, directoryPath.lastIndexOf(TSAPI.getDirSeparator()));
+ var parentLocation = directoryPath.substring(0, directoryPath.lastIndexOf(UIAPI.TagUtils.DIR_SEPARATOR));
var parentFound = -1;
for(var i=0; i < DirectoriesUI.directoryHistory.length; i++) {
if(DirectoriesUI.directoryHistory[i].key == parentLocation) {
@@ -210,7 +210,7 @@ DirectoriesUI.navigateToDirectory = function(directoryPath) {
}
}
- var locationTitle = directoryPath.substring(directoryPath.lastIndexOf(TSAPI.getDirSeparator())+1,directoryPath.length);
+ var locationTitle = directoryPath.substring(directoryPath.lastIndexOf(UIAPI.TagUtils.DIR_SEPARATOR)+1,directoryPath.length);
DirectoriesUI.directoryHistory.push({
"title": locationTitle,
"key" : directoryPath,
@@ -355,7 +355,7 @@ DirectoriesUI.initDialogs = function() {
// bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. [email protected]" );
// bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
if ( bValid ) {
- IOAPI.createDirectory(DirectoriesUI.dir4ContextMenu+TSAPI.getDirSeparator()+newDirName.val());
+ IOAPI.createDirectory(DirectoriesUI.dir4ContextMenu+UIAPI.TagUtils.DIR_SEPARATOR+newDirName.val());
DirectoriesUI.navigateToDirectory(DirectoriesUI.dir4ContextMenu);
$( this ).dialog( "close" );
}
diff --git a/data/js/fileviewer.ui.js b/data/js/fileviewer.ui.js
index 56ad5f073..e067af131 100644
--- a/data/js/fileviewer.ui.js
+++ b/data/js/fileviewer.ui.js
@@ -18,7 +18,7 @@ FileViewer.openFile = function(filePath) {
var openedFilePath = filePath;
$("#selectedFilePath").val(openedFilePath.replace("\\\\","\\"));
- var fileExt = TSAPI.extractFileExtension(filePath);
+ var fileExt = UIAPI.TagUtils.extractFileExtension(filePath);
this.constructFileViewerUI(filePath);
@@ -61,7 +61,7 @@ FileViewer.updateEditorContent = function(fileContent) {
// Should return false if no editor found
FileViewer.getFileEditor = function(filePath) {
- var fileExt = TSAPI.extractFileExtension(filePath);
+ var fileExt = UIAPI.TagUtils.extractFileExtension(filePath);
// Getting the editor for the file extension/type
var editorExt = TSSETTINGS.getFileTypeEditor(fileExt);
@@ -99,7 +99,7 @@ FileViewer.saveFile = function(filePath) {
FileViewer.constructFileViewerUI = function(filePath) {
// Adding tag buttons to the filetoolbox
- var tags = TSAPI.extractTags(filePath);
+ var tags = UIAPI.TagUtils.extractTags(filePath);
$( "#fileTitle" ).text();
@@ -122,7 +122,7 @@ FileViewer.constructFileViewerUI = function(filePath) {
drop: function( event, ui ) {
var tagName = ui.draggable.attr("tag");
console.log("Tagging file: "+tagName+" to "+filePath);
- TSAPI.addTag([filePath], tagName);
+ UIAPI.TagUtils.addTag([filePath], tagName);
}
})
@@ -148,9 +148,9 @@ FileViewer.constructFileViewerUI = function(filePath) {
}
FileViewer.initTagSuggestionMenu = function(filePath) {
- var tags = TSAPI.extractTags(filePath);
+ var tags = UIAPI.TagUtils.extractTags(filePath);
- var suggTags = TSAPI.suggestTags(filePath);
+ var suggTags = UIAPI.TagUtils.suggestTags(filePath);
var tsMenu = $( "#tagSuggestionsMenu" );
@@ -173,7 +173,7 @@ FileViewer.initTagSuggestionMenu = function(filePath) {
var tagName = $(this).attr( "tagname" );
var filePath = $(this).attr( "filepath" );
console.debug("Tag suggestion clicked: "+tagName);
- TSAPI.writeTagsToFile(filePath, [tagName]);
+ UIAPI.TagUtils.writeTagsToFile(filePath, [tagName]);
return false;
})
));
diff --git a/data/js/main.js b/data/js/main.js
index 838d138cc..8d349322f 100644
--- a/data/js/main.js
+++ b/data/js/main.js
@@ -36,20 +36,20 @@ var initApp = function(){
var layoutContainer = undefined;
- // TODO refactor all libs for integration of backbone.js
require([
+ "js/tagutils",
"js/view.manager",
"js/settings.api",
"js/fileviewer.ui",
"js/core.ui",
"js/tags.ui",
"js/core.api",
- "js/tagspace.api",
"js/directories.ui",
],
- function(viewManager) {
+ function(tagutils, viewManager) {
+ UIAPI.TagUtils = tagutils;
UIAPI.ViewManager = viewManager;
-
+
TagsUI.initContextMenus();
TagsUI.initDialogs();
@@ -95,6 +95,7 @@ var initApp = function(){
// Show start hint
$( "#selectTagSpace" ).tooltip( "open" );
+
});
}
diff --git a/data/js/tags.ui.js b/data/js/tags.ui.js
index 56c6ddd9f..a7103ee81 100644
--- a/data/js/tags.ui.js
+++ b/data/js/tags.ui.js
@@ -55,7 +55,7 @@ TagsUI.initContextMenus = function() {
break;
case "removeTag":
$( this ).hide();
- TSAPI.removeTag(UIAPI.selectedFiles[0],UIAPI.selectedTag);
+ UIAPI.TagUtils.removeTag(UIAPI.selectedFiles[0],UIAPI.selectedTag);
break;
}
}
@@ -67,7 +67,7 @@ TagsUI.initContextMenus = function() {
console.debug("Tag menu action: "+ui.item.attr( "action" )+" for tag: "+UIAPI.selectedTag);
switch (ui.item.attr( "action" )) {
case "addTagToFile":
- TSAPI.addTag(UIAPI.selectedFiles, UIAPI.selectedTag);
+ UIAPI.TagUtils.addTag(UIAPI.selectedFiles, UIAPI.selectedTag);
break;
case "addTagAsFilter":
$("#filterBox").val(UIAPI.selectedTag);
@@ -197,7 +197,7 @@ TagsUI.initDialogs = function() {
bValid = bValid && checkLength( smartTag, "tagname", 2, 40 );
if ( bValid ) {
for (var i=0; i < UIAPI.selectedFiles.length; i++) {
- TSAPI.writeTagsToFile(UIAPI.selectedFiles[i], [smartTag.val()]);
+ UIAPI.TagUtils.writeTagsToFile(UIAPI.selectedFiles[i], [smartTag.val()]);
};
$( this ).dialog( "close" );
IOAPI.listDirectory(UIAPI.currentPath);
@@ -223,7 +223,7 @@ TagsUI.initDialogs = function() {
modal: true,
buttons: {
"Remove": function() {
- TSAPI.removeTag(UIAPI.selectedTag);
+ UIAPI.TagUtils.removeTag(UIAPI.selectedTag);
$( this ).dialog( "close" );
IOAPI.listDirectory(UIAPI.currentPath);
},
@@ -421,7 +421,7 @@ TagsUI.generateTagButtons = function(commaSeparatedTags, fileExtension, fileName
var tagString = ""+commaSeparatedTags;
var wrapper = $('<span>');
if(filePath == undefined) {
- filePath = UIAPI.currentPath+TSAPI.getDirSeparator()+fileName;
+ filePath = UIAPI.currentPath+UIAPI.TagUtils.DIR_SEPARATOR+fileName;
}
if(fileExtension.length > 0) {
wrapper.append($('<button>', {
@@ -455,7 +455,7 @@ TagsUI.buttonizeTitle = function(title, fileName, filePath) {
title = "n/a";
}
if(filePath == undefined) {
- filePath = UIAPI.currentPath+TSAPI.getDirSeparator()+fileName;
+ filePath = UIAPI.currentPath+UIAPI.TagUtils.DIR_SEPARATOR+fileName;
}
return $('<span>').append($('<button>', {
title: fileName,
@@ -468,7 +468,7 @@ TagsUI.buttonizeTitle = function(title, fileName, filePath) {
// Helper function user by basic and search views
TagsUI.buttonizeFileName = function(fileName, filePath) {
if(filePath == undefined) {
- filePath = UIAPI.currentPath+TSAPI.getDirSeparator()+fileName;
+ filePath = UIAPI.currentPath+UIAPI.TagUtils.DIR_SEPARATOR+fileName;
}
return $('<span>').append($('<button>', {
title: fileName,
diff --git a/data/js/tagutils.js b/data/js/tagutils.js
index e745628d6..fd1c4d0c7 100644
--- a/data/js/tagutils.js
+++ b/data/js/tagutils.js
@@ -1,237 +1,262 @@
/* Copyright (c) 2012 The Tagspaces Authors. All rights reserved.
* Use of this source code is governed by a AGPL3 license that
* can be found in the LICENSE file. */
-console.debug("Loading tsAPI.js..");
-var TSAPI = (typeof TSAPI == "object" && TSAPI != null) ? TSAPI : {};
-
-TSAPI.beginTagContainer = "[";
-TSAPI.endTagContainer = "]";
-TSAPI.tagDelimiter = " ";
-
-TSAPI.getDirSeparator = function() {
- return TSAPI.isWindows()?"\\":"/";
-}
-
-TSAPI.isWindows = function() {
- return (navigator.appVersion.indexOf("Win")!=-1) ;
-}
-
-TSAPI.extractFileName = function(filePath) {
- return filePath.substring(filePath.lastIndexOf(TSAPI.getDirSeparator())+1,filePath.length);
-}
-
-TSAPI.extractContainingDirectoryPath = function(filePath) {
- return filePath.substring(0, filePath.lastIndexOf(TSAPI.getDirSeparator()));
-}
-
-TSAPI.extractFileExtension = function(filePath) {
- var ext = filePath.substring(filePath.lastIndexOf(".")+1,filePath.length).toLowerCase().trim();
- if(filePath.lastIndexOf(".") < 0) { ext = ""; }
- return ext;
-}
-
-TSAPI.extractTitle = function(filePath) {
- console.debug("Extracting title from: "+filePath);
- fileName = TSAPI.extractFileName(filePath);
-
- var beginTagContainer = fileName.indexOf(TSAPI.beginTagContainer);
- var indexExtensionSepartor = fileName.lastIndexOf(".");
- if( (indexExtensionSepartor <= 0) || (indexExtensionSepartor < beginTagContainer) ) {
- return fileName.trim();
- } else if( beginTagContainer < 0 ) {
- return fileName.slice(0,indexExtensionSepartor).trim();
- } else if( beginTagContainer >= 0 ) {
- return fileName.slice(0,beginTagContainer).trim();
- }
-}
-
-TSAPI.formatFileSize = function(fileSize) {
- // TODO implement format file size
- return fileSize;
-}
-
-TSAPI.formatDateTime = function(date, includeTime) {
- if( (date == undefined) || (date == "") ) return "";
- var d = new Date(date);
- var cDate = d.getDate();
- cDate = cDate + "";
- if (cDate.length == 1) { cDate = "0" + cDate; }
- var cMonth = d.getMonth(); cMonth++;
- cMonth = cMonth + "";
- if (cMonth.length == 1) { cMonth = "0" + cMonth; }
- var cYear = d.getFullYear();
- var cHour = d.getHours();
- cHour = cHour + "";
- if (cHour.length == 1) { cHour = "0" + cHour; }
- var cMinute = d.getMinutes();
- cMinute = cMinute + "";
- if (cMinute.length == 1) { cMinute = "0" + cMinute; }
- var cSecond = d.getSeconds();
- cSecond = cSecond + "";
- if (cSecond.length == 1) { cSecond = "0" + cSecond; }
- var time = "";
- if (includeTime) {
- time = " - "+cHour+":"+cMinute+":"+cSecond;
- }
- return cYear+"."+cMonth+"."+cDate+time;
-}
-
-TSAPI.extractTags = function(filePath) {
- console.debug("Extracting tags from: "+filePath);
-
- fileName = TSAPI.extractFileName(filePath);
-
- var tags = [];
- var beginTagContainer = fileName.indexOf(TSAPI.beginTagContainer);
- var endTagContainer = fileName.indexOf(TSAPI.endTagContainer);
- if( ( beginTagContainer < 0 ) || ( endTagContainer < 0 ) || ( beginTagContainer >= endTagContainer ) ) {
- console.debug("Filename does not contains tags. Aborting extraction.");
- return tags;
- }
- var tagContainer = fileName.slice(beginTagContainer+1,endTagContainer).trim();
- tags = tagContainer.split(TSAPI.tagDelimiter);
-
- var cleanedTags = [];
- for (var i=0; i < tags.length; i++) {
- if(tags[i].trim().length > 1) {
- cleanedTags.push(tags[i]);
- }
- }
- return cleanedTags;
-}
-
-
-TSAPI.suggestTags = function(filePath) {
- console.debug("Suggesting tags for: "+filePath);
-
- fileName = TSAPI.extractFileName(filePath);
-
- var tags = [];
- var tagContainer;
- var beginTagContainer = fileName.indexOf(TSAPI.beginTagContainer);
- if(beginTagContainer < 0) {
- tagContainer = fileName.slice(0,fileName.lastIndexOf(".")).trim();
- } else {
- tagContainer = fileName.slice(0,beginTagContainer).trim();
- }
-
- // Splitting filename with space, comma, plus, underscore and score delimiters
- tags = tagContainer.split(/[\s,+_-]+/);
-
- var cleanedTags = [];
- for (var i=0; i < tags.length; i++) {
- if(tags[i].trim().length > 1) {
- cleanedTags.push(tags[i]);
- }
- }
- return cleanedTags;
-}
-
-TSAPI.generateFileName = function(fileName, tags) {
- var tagsString = "";
- // Creating the string will all the tags by more that 0 tags
- if(tags.length > 0){
- tagsString = TSAPI.beginTagContainer;
- for (var i=0; i < tags.length; i++) {
- tagsString += tags[i]+" ";
- };
- tagsString = tagsString.trim();
- tagsString += TSAPI.endTagContainer;
- }
- console.debug("The tags string: "+tagsString);
-
- var fileExt = TSAPI.extractFileExtension(fileName);
- console.debug("Filename: "+fileName+" file extenstion: "+fileExt);
-
- // Assembling the new filename with the tags
- var newFileName = "";
- var beginTagContainer = fileName.indexOf(TSAPI.beginTagContainer);
- var endTagContainer = fileName.indexOf(TSAPI.endTagContainer);
- var lastDotPosition = fileName.lastIndexOf(".");
-
- if( ( beginTagContainer < 0 ) || ( endTagContainer < 0 ) || ( beginTagContainer >= endTagContainer ) ) {
- // Filename does not contains tags.
- if(lastDotPosition < 0) {
- // File does not have an extension
- newFileName = fileName +tagsString;
- } else {
- // File has an extension
- newFileName = fileName.substring(0,lastDotPosition)+tagsString+"."+fileExt;
- }
- } else {
- // File does not have an extension
- newFileName = fileName.substring(0,beginTagContainer)+tagsString+fileName.substring(endTagContainer+1,fileName.length);
- }
- return newFileName;
-}
-
-TSAPI.writeTagsToFile = function(filePath, tags) {
- console.debug("Add the tags to: "+filePath);
-
- var containingDirectoryPath = TSAPI.extractContainingDirectoryPath(filePath);
-
- var extractedTags = TSAPI.extractTags(filePath);
-
- for (var i=0; i < tags.length; i++) {
- // check if tag is already in the tag array
- if(extractedTags.indexOf(tags[i].trim()) < 0) {
- // Adding the new tag
- extractedTags.push(tags[i].trim());
- }
- };
-
- var newFileName = TSAPI.generateFileName(fileName, extractedTags);
-
- IOAPI.renameFile(filePath, containingDirectoryPath+TSAPI.getDirSeparator()+newFileName);
-}
-
-TSAPI.renameTag = function(filePath, oldTag, newTag) {
- console.debug("Rename tag for file: "+filePath);
-
- var containingDirectoryPath = TSAPI.extractContainingDirectoryPath(filePath);
-
- var extractedTags = TSAPI.extractTags(filePath);
-
- for (var i=0; i < extractedTags.length; i++) {
- // check if tag is already in the tag array
- if(extractedTags[i] == oldTag) {
- extractedTags[i] = newTag.trim();
- }
- };
-
- var newFileName = TSAPI.generateFileName(fileName, extractedTags);
-
- IOAPI.renameFile(filePath, containingDirectoryPath+TSAPI.getDirSeparator()+newFileName);
-
-}
-
-// Removing a tag from a filename
-TSAPI.removeTag = function(filePath, tagName) {
- console.debug("Removing tag: "+tagName+" from "+filePath);
-
- var containingDirectoryPath = TSAPI.extractContainingDirectoryPath(filePath);
-
- var tags = TSAPI.extractTags(filePath);
-
- var newTags = [];
- for (var i=0; i < tags.length; i++) {
- if(tags[i] != tagName) {
- newTags.push(tags[i]);
- }
- };
-
- var newFileName = TSAPI.generateFileName(fileName, newTags);
-
- IOAPI.renameFile(filePath, containingDirectoryPath+TSAPI.getDirSeparator()+newFileName);
-
-}
-
-TSAPI.addTag = function(filePathArray, tagName) {
- console.debug("Adding tag: "+tagName+" to the files, first file: "+filePathArray[0]);
+define([
+ 'require',
+ 'exports',
+ 'module',
+],function(require, exports, module) {
+ "use strict";
+
+ // var UIAPI = require("js/core.ui.js"),
+
+ console.debug("Loading tagutils.js ...");
+
+ var BEGIN_TAG_CONTAINER = "[";
+ var END_TAG_CONTAINER = "]";
+ var TAG_DELIMITER = " ";
+
+ var DIR_SEPARATOR = isWindows()?"\\":"/";
+
+ function isWindows() {
+ return (navigator.appVersion.indexOf("Win")!=-1) ;
+ }
+
+ function extractFileName(filePath) {
+ return filePath.substring(filePath.lastIndexOf(DIR_SEPARATOR)+1,filePath.length);
+ }
+
+ function extractContainingDirectoryPath(filePath) {
+ return filePath.substring(0, filePath.lastIndexOf(DIR_SEPARATOR));
+ }
+
+ function extractFileExtension(filePath) {
+ var ext = filePath.substring(filePath.lastIndexOf(".")+1,filePath.length).toLowerCase().trim();
+ if(filePath.lastIndexOf(".") < 0) { ext = ""; }
+ return ext;
+ }
+
+ function extractTitle(filePath) {
+ console.debug("Extracting title from: "+filePath);
+ var fileName = extractFileName(filePath);
+
+ var beginTagContainer = fileName.indexOf(BEGIN_TAG_CONTAINER);
+ var indexExtensionSepartor = fileName.lastIndexOf(".");
+ if( (indexExtensionSepartor <= 0) || (indexExtensionSepartor < beginTagContainer) ) {
+ return fileName.trim();
+ } else if( beginTagContainer < 0 ) {
+ return fileName.slice(0,indexExtensionSepartor).trim();
+ } else if( beginTagContainer >= 0 ) {
+ return fileName.slice(0,beginTagContainer).trim();
+ }
+ }
+
+ function formatFileSize(fileSize) {
+ // TODO implement format file size
+ return fileSize;
+ }
+
+ function formatDateTime(date, includeTime) {
+ if( (date == undefined) || (date == "") ) return "";
+ var d = new Date(date);
+ var cDate = d.getDate();
+ cDate = cDate + "";
+ if (cDate.length == 1) { cDate = "0" + cDate; }
+ var cMonth = d.getMonth(); cMonth++;
+ cMonth = cMonth + "";
+ if (cMonth.length == 1) { cMonth = "0" + cMonth; }
+ var cYear = d.getFullYear();
+ var cHour = d.getHours();
+ cHour = cHour + "";
+ if (cHour.length == 1) { cHour = "0" + cHour; }
+ var cMinute = d.getMinutes();
+ cMinute = cMinute + "";
+ if (cMinute.length == 1) { cMinute = "0" + cMinute; }
+ var cSecond = d.getSeconds();
+ cSecond = cSecond + "";
+ if (cSecond.length == 1) { cSecond = "0" + cSecond; }
+ var time = "";
+ if (includeTime) {
+ time = " - "+cHour+":"+cMinute+":"+cSecond;
+ }
+ return cYear+"."+cMonth+"."+cDate+time;
+ }
+
+ function extractTags(filePath) {
+ console.debug("Extracting tags from: "+filePath);
+
+ var fileName = extractFileName(filePath);
+
+ var tags = [];
+ var beginTagContainer = fileName.indexOf(BEGIN_TAG_CONTAINER);
+ var endTagContainer = fileName.indexOf(END_TAG_CONTAINER);
+ if( ( beginTagContainer < 0 ) || ( endTagContainer < 0 ) || ( beginTagContainer >= endTagContainer ) ) {
+ console.debug("Filename does not contains tags. Aborting extraction.");
+ return tags;
+ }
+ var tagContainer = fileName.slice(beginTagContainer+1,endTagContainer).trim();
+ tags = tagContainer.split(TAG_DELIMITER);
+
+ var cleanedTags = [];
+ for (var i=0; i < tags.length; i++) {
+ if(tags[i].trim().length > 1) {
+ cleanedTags.push(tags[i]);
+ }
+ }
+
+ console.debug("Extracting finished ");
+
+ return cleanedTags;
+ }
+
+ function suggestTags(filePath) {
+ console.debug("Suggesting tags for: "+filePath);
+
+ var fileName = extractFileName(filePath);
+
+ var tags = [];
+ var tagContainer;
+ var beginTagContainer = fileName.indexOf(BEGIN_TAG_CONTAINER);
+ if(beginTagContainer < 0) {
+ tagContainer = fileName.slice(0,fileName.lastIndexOf(".")).trim();
+ } else {
+ tagContainer = fileName.slice(0,beginTagContainer).trim();
+ }
- for (var i=0; i < filePathArray.length; i++) {
- TSAPI.writeTagsToFile(filePathArray[i], [tagName]);
- };
+ // Splitting filename with space, comma, plus, underscore and score delimiters
+ tags = tagContainer.split(/[\s,+_-]+/);
+
+ var cleanedTags = [];
+ for (var i=0; i < tags.length; i++) {
+ if(tags[i].trim().length > 1) {
+ cleanedTags.push(tags[i]);
+ }
+ }
+ return cleanedTags;
+ }
-}
\ No newline at end of file
+ // Internal
+ function generateFileName(fileName, tags) {
+ var tagsString = "";
+ // Creating the string will all the tags by more that 0 tags
+ if(tags.length > 0){
+ tagsString = BEGIN_TAG_CONTAINER;
+ for (var i=0; i < tags.length; i++) {
+ tagsString += tags[i]+" ";
+ };
+ tagsString = tagsString.trim();
+ tagsString += END_TAG_CONTAINER;
+ }
+ console.debug("The tags string: "+tagsString);
+
+ var fileExt = extractFileExtension(fileName);
+ console.debug("Filename: "+fileName+" file extenstion: "+fileExt);
+
+ // Assembling the new filename with the tags
+ var newFileName = "";
+ var beginTagContainer = fileName.indexOf(BEGIN_TAG_CONTAINER);
+ var endTagContainer = fileName.indexOf(END_TAG_CONTAINER);
+ var lastDotPosition = fileName.lastIndexOf(".");
+
+ if( ( beginTagContainer < 0 ) || ( endTagContainer < 0 ) || ( beginTagContainer >= endTagContainer ) ) {
+ // Filename does not contains tags.
+ if(lastDotPosition < 0) {
+ // File does not have an extension
+ newFileName = fileName +tagsString;
+ } else {
+ // File has an extension
+ newFileName = fileName.substring(0,lastDotPosition)+tagsString+"."+fileExt;
+ }
+ } else {
+ // File does not have an extension
+ newFileName = fileName.substring(0,beginTagContainer)+tagsString+fileName.substring(endTagContainer+1,fileName.length);
+ }
+ return newFileName;
+ }
+
+ function writeTagsToFile(filePath, tags) {
+ console.debug("Add the tags to: "+filePath);
+
+ var containingDirectoryPath = extractContainingDirectoryPath(filePath);
+
+ var extractedTags = extractTags(filePath);
+
+ for (var i=0; i < tags.length; i++) {
+ // check if tag is already in the tag array
+ if(extractedTags.indexOf(tags[i].trim()) < 0) {
+ // Adding the new tag
+ extractedTags.push(tags[i].trim());
+ }
+ };
+
+ var newFileName = generateFileName(fileName, extractedTags);
+
+ IOAPI.renameFile(filePath, containingDirectoryPath+DIR_SEPARATOR+newFileName);
+ }
+
+ function renameTag(filePath, oldTag, newTag) {
+ console.debug("Rename tag for file: "+filePath);
+
+ var containingDirectoryPath = extractContainingDirectoryPath(filePath);
+
+ var extractedTags = extractTags(filePath);
+
+ for (var i=0; i < extractedTags.length; i++) {
+ // check if tag is already in the tag array
+ if(extractedTags[i] == oldTag) {
+ extractedTags[i] = newTag.trim();
+ }
+ };
+
+ var newFileName = generateFileName(fileName, extractedTags);
+
+ IOAPI.renameFile(filePath, containingDirectoryPath+DIR_SEPARATOR+newFileName);
+
+ }
+
+ // Removing a tag from a filename
+ function removeTag(filePath, tagName) {
+ console.debug("Removing tag: "+tagName+" from "+filePath);
+
+ var containingDirectoryPath = extractContainingDirectoryPath(filePath);
+
+ var tags = extractTags(filePath);
+
+ var newTags = [];
+ for (var i=0; i < tags.length; i++) {
+ if(tags[i] != tagName) {
+ newTags.push(tags[i]);
+ }
+ };
+
+ var newFileName = generateFileName(fileName, newTags);
+
+ IOAPI.renameFile(filePath, containingDirectoryPath+DIR_SEPARATOR+newFileName);
+ }
+
+ function addTag(filePathArray, tagName) {
+ console.debug("Adding tag: "+tagName+" to the files, first file: "+filePathArray[0]);
+
+ for (var i=0; i < filePathArray.length; i++) {
+ writeTagsToFile(filePathArray[i], [tagName]);
+ };
+ }
+
+ // Public API definition
+ exports.DIR_SEPARATOR = DIR_SEPARATOR;
+ exports.isWindows = isWindows;
+ exports.extractFileName = extractFileName;
+ exports.extractContainingDirectoryPath = extractContainingDirectoryPath;
+ exports.extractFileExtension = extractFileExtension;
+ exports.extractTitle = extractTitle;
+ exports.formatFileSize = formatFileSize;
+ exports.formatDateTime = formatDateTime;
+ exports.extractTags = extractTags;
+ exports.suggestTags = suggestTags;
+ exports.writeTagsToFile = writeTagsToFile;
+ exports.renameTag = renameTag;
+ exports.removeTag = removeTag;
+ exports.addTag = addTag;
+
+});
\ No newline at end of file