var changePerspective = function(viewType) { console.log('Change to ' + viewType + ' perspective.'); TSCORE.showLoadingAnimation(); // Loading first perspective by default if (viewType === undefined) { TSCORE.currentPerspectiveID = perspectives[0].ID; } else { //Setting the current view TSCORE.currentPerspectiveID = viewType; } if (TSCORE.currentPerspectiveID === undefined) { TSCORE.showAlertDialog('No Perspectives found', ''); return false; } hideAllPerspectives(); for (var i = 0; i < perspectives.length; i++) { if (perspectives[i].ID === viewType) { var $currentPerspectitveIcon = $('#currentPerspectitveIcon'), $currentPerspectitveName = $('#currentPerspectitveName'); $currentPerspectitveIcon.removeClass(); $currentPerspectitveIcon.addClass(perspectives[i].Icon); $currentPerspectitveIcon.addClass('fa-lg'); $currentPerspectitveName.text(' ' + perspectives[i].Title); $currentPerspectitveName.attr('title', perspectives[i].ID); perspectives[i].load(); $('#' + perspectives[i].ID + 'Container').show(); $('#' + perspectives[i].ID + 'Toolbar').show(); $('#' + perspectives[i].ID + 'Footer').show(); } } // Clear the list with the selected files clearSelectedFiles(); // Enabled the main toolbar e.g. search functionality TSCORE.enableTopToolbar(); };
var saveTextFile = function(filePath,content,overWrite) { console.log("Saving file: "+filePath); TSCORE.showLoadingAnimation(); /** TODO check if fileExist by saving needed if(overWrite) { // Current implementation } else { if (!pathUtils.existsSync(filePath)) { // Current implementation } } */ // Handling the UTF8 support for text files var UTF8_BOM = "\ufeff"; if(content.indexOf(UTF8_BOM) == 0) { // already has a UTF8 bom } else { content = UTF8_BOM+content; } var isNewFile = !fs.existsSync(filePath); fs.writeFile(filePath, content, 'utf8', function(error) { if (error) { console.log("Save to file "+filePath+" failed "+error); return; } TSPOSTIO.saveTextFile(filePath, isNewFile); }); };
var saveTextFile = function(filePath,content) { console.log("Saving file: "+filePath); TSCORE.showLoadingAnimation(); // Handling the UTF8 support for text files var UTF8_BOM = "\ufeff"; if(content.indexOf(UTF8_BOM) === 0) { console.log("Content beging with a UTF8 bom"); } else { content = UTF8_BOM+content; } filePath = normalizePath(filePath); fsRoot.getFile(filePath, {create: true, exclusive: false}, function(entry) { entry.createWriter( function(writer) { writer.onwriteend = function(evt) { TSPOSTIO.saveTextFile(fsRoot.fullPath+"/"+filePath); }; writer.write(content); }, function() { console.log("error creating writter file: "+filePath); } ); }, function() { console.log("Error getting file entry: "+filePath); } ); };
var renameDirectory = function(dirPath, newDirName) { var newDirPath = TSCORE.TagUtils.extractParentDirectoryPath(dirPath) + TSCORE.dirSeparator + newDirName; TSCORE.showLoadingAnimation(); dirPath = normalizePath(dirPath); var newDirParentPath = normalizePath(newDirPath.substring(0, newDirPath.lastIndexOf('/'))); // TODO check if the newFilePath exist or cause issues by renaming fsRoot.getDirectory(newDirParentPath, {create: false, exclusive: false}, function (parentDirEntry) { fsRoot.getDirectory(dirPath, {create: false, exclusive: false}, function(entry) { entry.moveTo( parentDirEntry, newDirName, function() { console.log("Directory renamed to: "+newDirPath+" Old name: "+entry.fullPath); TSPOSTIO.renameDirectory(entry.fullPath, newDirPath); }, function() { console.log("error renaming: "+dirPath); } ); }, function() { console.log("Error getting directory: "+dirPath); } ); }, function (error) { console.log("Getting dir: "+newDirParentPath+" failed with error code: " + error.code); } ); };
var deleteDirectory = function(dirPath) { console.log("Deleting directory: " + dirPath); TSCORE.showLoadingAnimation(); var path = normalizePath(dirPath); fsRoot.getDirectory(path, { create: false, exclusive: false }, function(entry) { entry.remove( function() { console.log("file deleted: " + path); TSPOSTIO.deleteDirectory(dirPath); }, function() { TSPOSTIO.deleteDirectoryFailed(path); console.log("error deleting dir: " + dirPath); } ); }, function() { console.log("error getting directory"); } ); };
var createDirectoryIndex = function(dirPath) { console.log("Creating index for directory: " + dirPath); TSCORE.showLoadingAnimation(); var directoryIndex = []; TSPOSTIO.createDirectoryIndex(directoryIndex); };
function listSubDirectories(dirPath) { console.log("Listing sub directories: " + dirPath); TSCORE.showLoadingAnimation(); TSCORE.IO.listDirectoryPromise(dirPath).then(function(entries) { var anotatedDirList = []; var firstEntry = 0; // skiping the first entry pointing to the parent directory if (isChrome) { firstEntry = 1; } for (var i = firstEntry; i < entries.length; i++) { if (!entries[i].isFile) { anotatedDirList.push({ "name": entries[i].name, "path": entries[i].path }); } } TSPOSTIO.listSubDirectories(anotatedDirList, dirPath); }).catch(function(error) { TSPOSTIO.errorOpeningPath(dirPath); TSCORE.hideLoadingAnimation(); console.error("Error listDirectory " + dirPath + " error: " + error); }); }
var listDirectory = function(dirPath) { console.log("Listing directory: " + dirPath); TSCORE.showLoadingAnimation(); try { fs.readdir(dirPath, function(error, dirList) { if (error) { TSPOSTIO.errorOpeningPath(dirPath); console.log("Listing directory: " + dirPath + " failed " + error); return; } var anotatedDirList = []; for (var i = 0; i < dirList.length; i++) { var path = dirPath + TSCORE.dirSeparator + dirList[i]; var stats = fs.lstatSync(path); if (stats !== undefined) { //console.log('stats: ' + JSON.stringify(stats)); anotatedDirList.push({ "name": dirList[i], "isFile": stats.isFile(), "size": stats.size, "lmdt": stats.mtime, "path": path }); } } TSPOSTIO.listDirectory(anotatedDirList); }); } catch (ex) { TSPOSTIO.errorOpeningPath(); console.error("Listing directory " + dirPath + " failed " + ex); } };
function createZipPrewiew(filePath, elementID) { var $parent = $('#' + elementID); var $previewElement = $('<div/>').css({'overflow': 'auto', 'padding': '5px', 'fontSize': 12}) .width($parent.width()).height($parent.height()).appendTo($parent); TSCORE.showLoadingAnimation(); TSCORE.IO.getFileContent(filePath, function(content) { var zipFile = new JSZip(content); $previewElement.append("<p> Contents of file " + filePath + "</p>"); var ulFiles = $previewElement.append("<ul/>"); for (var fileName in zipFile.files) { if (zipFile.files[fileName].dir === true) { continue; } var linkToFile = $('<a>').attr('href', '#').text(fileName); linkToFile.click(function(event) { event.preventDefault(); var containFile = zipFile.files[$(this).text()]; showContentFilePreviewDialog(containFile); }); var liFile = $('<li/>').css('list-style-type', 'none').append(linkToFile); ulFiles.append(liFile); } TSCORE.hideLoadingAnimation(); }, function(error) { $previewElement.append("<p> Error in getFileContent :" + error + "</p>"); }); }
var deleteElement = function(filePath) { console.log("Deleting: " + filePath); TSCORE.showLoadingAnimation(); var path = normalizePath(filePath); fsRoot.getFile(path, { create: false, exclusive: false }, function(entry) { entry.remove( function() { console.log("file deleted: " + path); TSPOSTIO.deleteElement(filePath); }, function() { console.log("error deleting: " + filePath); } ); }, function() { console.log("error getting file"); } ); };
var listDirectory = function (dirPath) { TSCORE.showLoadingAnimation(); dirPath = dirPath+"/"; // TODO make it platform independent console.log("Listing directory: " + dirPath); var stats = []; fsRoot.readdir(dirPath, function (err, contents, dirStat, stats) { var i, convertedStats = []; if (!err) { var i; var anotatedDirList = []; for (i = 0; i < stats.length; i++) { console.log("File: "+stats[i].name); anotatedDirList.push({ "name": stats[i].name, "isFile": stats[i].isFile, "size": stats[i].size, "lmdt": stats[i].modifiedAt, "path": stats[i].path }); } //console.log("Dir content: " + JSON.stringify(entries)); TSPOSTIO.listDirectory(anotatedDirList); } else { TSPOSTIO.errorOpeningPath(); console.log("Dir List Error: " + err); } }); };
var deleteElement = function(path) { console.log("Deleting: " + path); TSCORE.showLoadingAnimation(); TSPOSTIO.deleteElement(path); };
var renameFile = function(filePath, newFilePath) { TSCORE.showLoadingAnimation(); filePath = normalizePath(filePath); var newFileName = newFilePath.substring(newFilePath.lastIndexOf('/')+1); var newFileParentPath = normalizePath(newFilePath.substring(0, newFilePath.lastIndexOf('/'))); // TODO check if the newFilePath exist or cause issues by renaming fsRoot.getDirectory(newFileParentPath, {create: false, exclusive: false}, function (parentDirEntry) { fsRoot.getFile(filePath, {create: false, exclusive: false}, function(entry) { entry.moveTo( parentDirEntry, newFileName, function() { console.log("File renamed to: "+newFilePath+" Old name: "+entry.fullPath); TSPOSTIO.renameFile(entry.fullPath, newFilePath); }, function() { console.log("error renaming: "+filePath); } ); }, function() { console.log("Error getting file: "+filePath); } ); }, function (error) { console.log("Getting dir: "+newFileParentPath+" failed with error code: " + error.code); } ); };
var createDirectory = function(dirPath) { console.log("Creating directory: " + dirPath); TSCORE.showLoadingAnimation(); TSPOSTIO.createDirectory(dirPath); };
var loadTextFile = function(filePath) { console.log("Loading file: "+filePath); TSCORE.showLoadingAnimation(); filePath = normalizePath(filePath); fsRoot.getFile(filePath, {create: false, exclusive: false}, function(entry) { entry.file( function(file) { var reader = new FileReader(); reader.onloadend = function(evt) { TSPOSTIO.loadTextFile(evt.target.result); }; reader.readAsText(file); }, function() { console.log("error getting file: "+filePath); } ); }, function() { console.log("Error getting file entry: "+filePath); } ); };
var createDirectoryTree = function(dirPath) { TSCORE.showLoadingAnimation(); console.log("Creating directory index for: " + dirPath); var directoyTree = generateDirectoryTree(dirPath); //console.log(JSON.stringify(directoyTree)); TSPOSTIO.createDirectoryTree(directoyTree); };
var createDirectoryIndex = function(dirPath) { console.log("Creating index for directory: " + dirPath); TSCORE.showLoadingAnimation(); var directoryIndex = []; directoryIndex = scanDirectory(dirPath, directoryIndex); console.log(JSON.stringify(directoryIndex)); TSPOSTIO.createDirectoryIndex(directoryIndex); };
var checkNewVersion = function() { console.log("Checking for new version..."); TSCORE.showLoadingAnimation(); var event = document.createEvent('CustomEvent'); event.initCustomEvent("addon-message", true, true, {"detail":{ "command": "checkNewVersion", }}); document.documentElement.dispatchEvent(event); };
var loadSettings = function() { console.log("Loading setting from firefox preferences..."); TSCORE.showLoadingAnimation(); var event = document.createEvent('CustomEvent'); event.initCustomEvent("addon-message", true, true, {"detail":{ "command": "loadSettings" }}); document.documentElement.dispatchEvent(event); };
var createDirectoryIndex = function(dirPath) { console.log("Creating index for directory: " + dirPath); TSCORE.showWaitingDialog($.i18n.t("ns.common:waitDialogDiectoryIndexing")); TSCORE.showLoadingAnimation(); var directoryIndex = []; directoryIndex = scanDirectory(dirPath, directoryIndex); //console.log(JSON.stringify(directoryIndex)); TSPOSTIO.createDirectoryIndex(directoryIndex); };
var createDirectoryTree = function(dirPath) { console.log("Creating directory index for: "+dirPath); TSCORE.showLoadingAnimation(); TSCORE.showAlertDialog("Creating directory tree is not supported on Android yet."); /* var directoyTree = generateDirectoryTree(dirPath); //console.log(JSON.stringify(directoyTree)); TSPOSTIO.createDirectoryTree(directoyTree);*/ };
var createDirectoryTree = function(dirPath) { console.log("Creating directory tree for: "+dirPath); TSCORE.showLoadingAnimation(); var event = document.createEvent('CustomEvent'); event.initCustomEvent("addon-message", true, true, {"detail":{ "command": "createDirectoryTree", "path": dirPath }}); document.documentElement.dispatchEvent(event); };
var createDirectoryIndex = function(dirPath) { console.log("Creating index for directory: "+dirPath); TSCORE.showLoadingAnimation(); TSCORE.showAlertDialog("Creating directory index is not supported on Android yet."); /* var directoryIndex = []; directoryIndex = scanDirectory(dirPath, directoryIndex); console.log(JSON.stringify(directoryIndex)); TSPOSTIO.createDirectoryIndex(directoryIndex); */ };
var saveSettings = function(content) { console.log("Saving setting..."); TSCORE.showLoadingAnimation(); var event = document.createEvent('CustomEvent'); event.initCustomEvent("addon-message", true, true, {"detail":{ "command": "saveSettings", "content": content }}); document.documentElement.dispatchEvent(event); };
var loadTextFile = function(filePath) { console.log("Loading file: "+filePath); TSCORE.showLoadingAnimation(); var event = document.createEvent('CustomEvent'); event.initCustomEvent("addon-message", true, true, {"detail":{ "command": "loadTextFile", "path": filePath }}); document.documentElement.dispatchEvent(event); };
exports.getSubdirs = function(dirPath) { console.log("Getting subdirs: "+dirPath); TSCORE.showLoadingAnimation(); var event = document.createEvent('CustomEvent'); event.initCustomEvent("addon-message", true, true, {"detail":{ "command": "getSubdirs", "path": dirPath }}); document.documentElement.dispatchEvent(event); };
var getFileProperties = function(filePath) { console.log("Getting file properties..."); TSCORE.showLoadingAnimation(); var event = document.createEvent('CustomEvent'); event.initCustomEvent("addon-message", true, true, {"detail":{ "command": "getFileProperties", "path": filePath }}); document.documentElement.dispatchEvent(event); };
var deleteElement = function(path) { console.log("Deleting: "+path); TSCORE.showLoadingAnimation(); var event = document.createEvent('CustomEvent'); event.initCustomEvent("addon-message", true, true, {"detail":{ "command": "delete", "path": path }}); document.documentElement.dispatchEvent(event); };
var createDirectory = function(dirPath) { console.log("Creating directory: " + dirPath); TSCORE.showLoadingAnimation(); try { nativeIO.createDirectory(dirPath); TSPOSTIO.createDirectory(dirPath); } catch (ex) { console.error("Deleting file failed " + ex); } };
var renameFile = function(filePath, newFilePath) { console.log("Renaming "+filePath+" to "+newFilePath); TSCORE.showLoadingAnimation(); var event = document.createEvent('CustomEvent'); event.initCustomEvent("addon-message", true, true, {"detail":{ "command": "rename", "path": filePath, "newPath": newFilePath }}); document.documentElement.dispatchEvent(event); };