Stackedit/js/file-manager.js

379 lines
12 KiB
JavaScript
Raw Normal View History

2013-05-26 22:59:03 +00:00
define([
"jquery",
"core",
"utils",
"extension-manager",
"synchronizer",
"publisher",
"sharing",
"text!../WELCOME.md",
"underscore"
], function($, core, utils, extensionManager, synchronizer, publisher, sharing, welcomeContent) {
2013-05-04 00:05:58 +00:00
2013-04-02 18:42:47 +00:00
var fileManager = {};
2013-05-26 22:59:03 +00:00
// Load file descriptors from localStorage and store in a map
var fileSystemDescriptor = _.chain(localStorage["file.list"].split(";"))
.compact()
.reduce(function(fileSystemDescriptor, fileIndex) {
var title = localStorage[fileIndex + ".title"];
var fileDesc = {
2013-05-26 23:38:13 +00:00
fileIndex : fileIndex,
2013-05-26 22:59:03 +00:00
title : title,
syncLocations: {},
publishLocations: {}
};
synchronizer.populateSyncLocations(fileDesc),
publisher.populatePublishLocations(fileDesc),
fileSystemDescriptor[fileIndex] = fileDesc;
return fileSystemDescriptor;
}, {})
.value();
extensionManager.onFileSystemLoaded(fileSystemDescriptor);
fileManager.getFileList = function() {
return _.values(fileSystemDescriptor);
};
2013-04-13 18:11:54 +00:00
// Defines the current file
2013-05-26 22:59:03 +00:00
var currentFile = (function() {
var currentFileIndex = localStorage["file.current"];
if(currentFileIndex !== undefined) {
return fileSystemDescriptor[currentFileIndex];
}
})();
fileManager.getCurrentFile = function() {
return currentFile;
2013-04-13 18:11:54 +00:00
};
2013-05-26 22:59:03 +00:00
fileManager.isCurrentFile = function(fileDesc) {
return fileDesc === currentFile;
2013-04-13 18:11:54 +00:00
};
2013-05-26 22:59:03 +00:00
fileManager.setCurrentFile = function(fileDesc) {
currentFile = fileDesc;
if(fileDesc === undefined) {
2013-04-13 18:11:54 +00:00
localStorage.removeItem("file.current");
}
2013-05-26 23:38:13 +00:00
else if(fileDesc.fileIndex != TEMPORARY_FILE_INDEX) {
localStorage["file.current"] = fileDesc.fileIndex;
2013-04-13 18:11:54 +00:00
}
};
2013-04-02 18:42:47 +00:00
// Caution: this function recreate the editor (reset undo operations)
2013-05-26 22:59:03 +00:00
fileManager.selectFile = function(fileDesc) {
2013-04-02 18:42:47 +00:00
// If no file create one
2013-05-26 22:59:03 +00:00
if (_.size(fileSystemDescriptor) === 0) {
fileDesc = fileManager.createFile(WELCOME_DOCUMENT_TITLE, welcomeContent);
2013-04-02 18:42:47 +00:00
}
2013-05-26 22:59:03 +00:00
if(fileDesc === undefined) {
// If no file is selected take the last created
fileDesc = fileSystemDescriptor[_.keys(fileSystemDescriptor)[fileSystemDescriptor.length - 1]];
2013-04-02 18:42:47 +00:00
}
2013-05-26 22:59:03 +00:00
fileManager.setCurrentFile(fileDesc);
2013-04-02 18:42:47 +00:00
// Update the file titles
2013-04-10 23:13:31 +00:00
fileManager.updateFileTitles();
2013-04-14 13:24:29 +00:00
publisher.notifyPublish();
2013-04-02 18:42:47 +00:00
2013-05-26 22:59:03 +00:00
// Notify extensions
extensionManager.onFileSelected(fileDesc);
2013-05-04 00:05:58 +00:00
// Hide the viewer pencil button
2013-05-26 23:38:13 +00:00
if(fileDesc.fileIndex == TEMPORARY_FILE_INDEX) {
2013-05-04 00:05:58 +00:00
$(".action-edit-document").removeClass("hide");
}
else {
$(".action-edit-document").addClass("hide");
}
2013-04-02 18:42:47 +00:00
// Recreate the editor
2013-05-26 23:38:13 +00:00
$("#wmd-input").val(localStorage[fileDesc.fileIndex + ".content"]);
2013-04-02 18:42:47 +00:00
core.createEditor(function() {
2013-05-18 13:40:16 +00:00
// Callback to save content when textarea changes
2013-04-02 18:42:47 +00:00
fileManager.saveFile();
});
};
2013-05-26 22:59:03 +00:00
fileManager.createFile = function(title, content, syncLocations, isTemporary) {
content = content !== undefined ? content : core.settings.defaultContent;
2013-05-26 22:59:03 +00:00
syncLocations = syncLocations || {};
2013-04-02 18:42:47 +00:00
if (!title) {
// Create a file title
title = DEFAULT_FILE_TITLE;
var indicator = 2;
2013-05-26 22:59:03 +00:00
while(_.some(fileSystemDescriptor, function(fileDesc) {
2013-04-13 18:11:54 +00:00
return fileDesc.title == title;
})) {
2013-04-02 18:42:47 +00:00
title = DEFAULT_FILE_TITLE + indicator++;
}
}
2013-04-11 22:38:41 +00:00
// Generate a unique fileIndex
2013-05-04 00:05:58 +00:00
var fileIndex = TEMPORARY_FILE_INDEX;
if(!isTemporary) {
do {
2013-05-25 18:13:59 +00:00
fileIndex = "file." + utils.randomString();
2013-05-26 22:59:03 +00:00
} while(_.has(fileSystemDescriptor, fileIndex));
2013-05-04 00:05:58 +00:00
}
2013-04-11 22:38:41 +00:00
2013-04-02 18:42:47 +00:00
// Create the file in the localStorage
localStorage[fileIndex + ".content"] = content;
localStorage[fileIndex + ".title"] = title;
2013-05-18 13:40:16 +00:00
// Store syncIndexes associated to the file
2013-05-26 22:59:03 +00:00
var sync = _.reduce(syncLocations, function(sync, syncAttributes, syncIndex) {
2013-04-13 18:11:54 +00:00
return sync + syncIndex + ";";
}, ";");
2013-04-02 18:42:47 +00:00
localStorage[fileIndex + ".sync"] = sync;
2013-05-18 13:40:16 +00:00
// Store publishIndexes associated to the file
2013-04-09 07:58:06 +00:00
localStorage[fileIndex + ".publish"] = ";";
2013-05-26 22:59:03 +00:00
// Create the file descriptor
var fileDesc = {
2013-05-26 23:38:13 +00:00
fileIndex : fileIndex,
2013-05-26 22:59:03 +00:00
title : title,
syncLocations: syncLocations,
publishLocations: {}
};
2013-05-18 13:40:16 +00:00
// Add the index to the file list
2013-05-04 00:05:58 +00:00
if(!isTemporary) {
localStorage["file.list"] += fileIndex + ";";
2013-05-26 22:59:03 +00:00
fileSystemDescriptor[fileIndex] = fileDesc;
extensionManager.onFileCreated(fileDesc);
2013-05-04 00:05:58 +00:00
}
2013-05-26 22:59:03 +00:00
return fileDesc;
2013-04-02 18:42:47 +00:00
};
2013-05-26 22:59:03 +00:00
fileManager.deleteFile = function(fileDesc) {
fileDesc = fileDesc || fileManager.getCurrentFile();
if(fileManager.isCurrentFile(fileDesc)) {
// Unset the current fileDesc
fileManager.setCurrentFile();
2013-04-02 18:42:47 +00:00
}
// Remove synchronized locations
2013-05-26 23:38:13 +00:00
_.each(fileDesc.syncLocations, function(syncAttributes) {
fileManager.removeSync(syncAttributes, true);
2013-04-13 18:11:54 +00:00
});
2013-04-11 22:38:41 +00:00
// Remove publish locations
2013-05-26 23:38:13 +00:00
_.each(fileDesc.publishLocations, function(publishAttributes) {
fileManager.removePublish(publishAttributes, true);
2013-04-13 18:11:54 +00:00
});
2013-04-02 18:42:47 +00:00
2013-05-18 13:40:16 +00:00
// Remove the index from the file list
2013-05-26 23:38:13 +00:00
var fileIndex = fileDesc.fileIndex;
2013-04-02 18:42:47 +00:00
localStorage["file.list"] = localStorage["file.list"].replace(";"
+ fileIndex + ";", ";");
localStorage.removeItem(fileIndex + ".title");
localStorage.removeItem(fileIndex + ".content");
2013-05-18 13:40:16 +00:00
localStorage.removeItem(fileIndex + ".sync");
localStorage.removeItem(fileIndex + ".publish");
2013-05-26 22:59:03 +00:00
fileSystemDescriptor.removeItem(fileIndex);
extensionManager.onFileDeleted(fileDesc);
2013-04-02 18:42:47 +00:00
};
2013-05-18 13:40:16 +00:00
// Save current file in localStorage
2013-04-02 18:42:47 +00:00
fileManager.saveFile = function() {
var content = $("#wmd-input").val();
2013-05-26 22:59:03 +00:00
var fileDesc = fileManager.getCurrentFile();
2013-05-26 23:38:13 +00:00
localStorage[fileDesc.fileIndex + ".content"] = content;
2013-05-26 22:59:03 +00:00
extensionManager.onFileChanged(fileDesc);
synchronizer.notifyChange(fileDesc);
2013-04-02 18:42:47 +00:00
};
2013-05-26 23:38:13 +00:00
// Add a synchronized location to a file
fileManager.addSync = function(fileDesc, syncAttributes) {
localStorage[fileDesc.fileIndex + ".sync"] += syncAttributes.syncIndex + ";";
fileDesc.syncLocations[syncAttributes.syncIndex] = syncAttributes;
2013-05-26 22:59:03 +00:00
// addSync is only used for export, not for import
2013-05-26 23:38:13 +00:00
extensionManager.onSyncExportSuccess(fileDesc, syncAttributes);
2013-04-02 18:42:47 +00:00
};
2013-05-26 22:59:03 +00:00
2013-05-26 23:38:13 +00:00
// Remove a synchronized location
fileManager.removeSync = function(syncAttributes, skipExtensions) {
var fileDesc = fileManager.getFileFromSyncIndex(syncAttributes.syncIndex);
2013-05-26 22:59:03 +00:00
if(fileDesc !== undefined) {
2013-05-26 23:38:13 +00:00
localStorage[fileDesc.fileIndex + ".sync"] = localStorage[fileDesc.fileIndex + ".sync"].replace(";"
+ syncAttributes.syncIndex + ";", ";");
2013-04-02 18:42:47 +00:00
}
2013-04-20 00:14:20 +00:00
// Remove sync attributes
2013-05-26 23:38:13 +00:00
localStorage.removeItem(syncAttributes.syncIndex);
2013-05-26 22:59:03 +00:00
fileDesc.syncLocations.removeItem(syncIndex);
if(!skipExtensions) {
extensionManager.onSyncRemoved(fileDesc, syncAttributes);
}
2013-04-02 18:42:47 +00:00
};
2013-05-26 22:59:03 +00:00
// Get the file descriptor associated to a syncIndex
2013-05-26 23:38:13 +00:00
fileManager.getFileFromSyncIndex = function(syncIndex) {
2013-05-26 22:59:03 +00:00
return _.find(fileSystemDescriptor, function(fileDesc) {
return _.has(fileDesc.syncLocations, syncIndex);
});
};
// Get syncAttributes from syncIndex
fileManager.getSyncAttributes = function(syncIndex) {
2013-05-26 23:38:13 +00:00
var fileDesc = fileManager.getFileFromSyncIndex(syncIndex);
2013-05-26 22:59:03 +00:00
return fileDesc && fileDesc.syncLocations[syncIndex];
};
// Returns true if provider has locations to synchronize
fileManager.hasSync = function(provider) {
return _.some(fileSystemDescriptor, function(fileDesc) {
return _.some(fileDesc.syncLocations, function(syncAttributes) {
syncAttributes.provider == provider.providerId;
});
});
2013-04-02 18:42:47 +00:00
};
2013-05-26 22:59:03 +00:00
// Add a publishIndex (publish location) to a file
2013-05-26 23:38:13 +00:00
fileManager.addPublish = function(fileDesc, publishAttributes) {
localStorage[fileDesc.fileIndex + ".publish"] += publishAttributes.publishIndex + ";";
fileDesc.publishLocations[publishAttributes.publishIndex] = publishAttributes;
extensionManager.onNewPublishSuccess(fileDesc, publishAttributes);
2013-05-26 22:59:03 +00:00
};
2013-04-14 13:24:29 +00:00
// Remove a publishIndex (publish location)
2013-05-26 23:38:13 +00:00
fileManager.removePublish = function(publishAttributes, skipExtensions) {
var fileDesc = fileManager.getFileFromPublish(publishAttributes.publishIndex);
2013-05-26 22:59:03 +00:00
if(fileDesc !== undefined) {
2013-05-26 23:38:13 +00:00
localStorage[fileDesc.fileIndex + ".publish"] = localStorage[fileDesc.fileIndex + ".publish"].replace(";"
+ publishAttributes.publishIndex + ";", ";");
2013-05-26 22:59:03 +00:00
if(fileManager.isCurrentFile(fileDesc)) {
2013-04-14 13:24:29 +00:00
publisher.notifyPublish();
2013-04-11 22:38:41 +00:00
}
}
2013-04-20 00:14:20 +00:00
// Remove publish attributes
2013-05-26 23:38:13 +00:00
localStorage.removeItem(publishAttributes.publishIndex);
2013-05-26 22:59:03 +00:00
fileDesc.publishLocations.removeItem(publishIndex);
if(!skipExtensions) {
extensionManager.onPublishRemoved(fileDesc, publishAttributes);
}
2013-04-11 22:38:41 +00:00
};
2013-05-26 22:59:03 +00:00
// Get the file descriptor associated to a publishIndex
fileManager.getFileFromPublish = function(publishIndex) {
return _.find(fileSystemDescriptor, function(fileDesc) {
return _.has(fileDesc.publishLocations, publishIndex);
});
2013-04-11 22:38:41 +00:00
};
2013-05-19 23:26:05 +00:00
// Filter for search input in file selector
function filterFileSelector(filter) {
var liList = $("#file-selector li:not(.stick)");
liList.show();
if(filter) {
var words = filter.toLowerCase().split(/\s+/);
liList.each(function() {
var fileTitle = $(this).text().toLowerCase();
if(_.some(words, function(word) {
return fileTitle.indexOf(word) === -1;
})) {
$(this).hide();
}
});
}
}
2013-04-22 01:04:12 +00:00
core.onReady(function() {
2013-04-10 18:14:59 +00:00
fileManager.selectFile();
$(".action-create-file").click(function() {
2013-05-26 22:59:03 +00:00
var fileDesc = fileManager.createFile();
fileManager.selectFile(fileDesc);
var wmdInput = $("#wmd-input").focus().get(0);
if(wmdInput.setSelectionRange) {
wmdInput.setSelectionRange(0, 0);
}
2013-04-10 18:14:59 +00:00
$("#file-title").click();
});
$(".action-remove-file").click(function() {
fileManager.deleteFile();
fileManager.selectFile();
});
$("#file-title").click(function() {
2013-04-30 23:02:19 +00:00
if(viewerMode === true) {
return;
}
2013-04-10 18:14:59 +00:00
$(this).hide();
var fileTitleInput = $("#file-title-input").show();
_.defer(function() {
fileTitleInput.focus().get(0).select();
});
2013-04-10 18:14:59 +00:00
});
2013-04-20 00:14:20 +00:00
function applyTitle(input) {
2013-05-26 22:59:03 +00:00
input.hide();
$("#file-title").show();
2013-04-20 00:14:20 +00:00
var title = $.trim(input.val());
2013-05-26 22:59:03 +00:00
var fileDesc = fileManager.getCurrentFile();
2013-05-26 23:38:13 +00:00
var fileIndexTitle = fileDesc.fileIndex + ".title";
2013-04-10 18:14:59 +00:00
if (title) {
if (title != localStorage[fileIndexTitle]) {
localStorage[fileIndexTitle] = title;
2013-05-26 22:59:03 +00:00
fileDesc.title = title;
2013-04-10 18:14:59 +00:00
fileManager.updateFileTitles();
2013-05-26 22:59:03 +00:00
synchronizer.notifyChange(fileDesc);
extensionManager.onTitleChanged(fileDesc);
2013-04-10 18:14:59 +00:00
}
}
2013-05-26 22:59:03 +00:00
input.val(localStorage[fileIndexTitle]);
$("#wmd-input").focus();
2013-04-20 00:14:20 +00:00
}
$("#file-title-input").blur(function() {
applyTitle($(this));
}).keyup(function(e) {
if (e.keyCode == 13) {
applyTitle($(this));
}
if (e.keyCode == 27) {
$(this).val("");
applyTitle($(this));
}
2013-04-10 18:14:59 +00:00
});
2013-05-19 23:26:05 +00:00
$(".action-open-file").click(function() {
filterFileSelector();
_.defer(function() {
$("#file-search").val("").focus();
});
});
$("#file-search").keyup(function() {
filterFileSelector($(this).val());
}).click(function(event) {
event.stopPropagation();
});
2013-05-04 00:05:58 +00:00
$(".action-open-stackedit").click(function() {
window.location.href = ".";
});
$(".action-edit-document").click(function() {
var content = $("#wmd-input").val();
2013-05-26 22:59:03 +00:00
var title = fileManager.getCurrentFile().title;
var fileDesc = fileManager.createFile(title, content);
fileManager.selectFile(fileDesc);
2013-05-04 00:05:58 +00:00
window.location.href = ".";
});
2013-04-22 23:10:08 +00:00
$(".action-download-md").click(function() {
var content = $("#wmd-input").val();
2013-05-26 22:59:03 +00:00
var title = fileManager.getCurrentFile().title;
2013-04-22 23:10:08 +00:00
core.saveFile(content, title + ".md");
});
$(".action-download-html").click(function() {
var content = $("#wmd-preview").html();
2013-05-26 22:59:03 +00:00
var title = fileManager.getCurrentFile().title;
2013-04-22 23:10:08 +00:00
core.saveFile(content, title + ".html");
});
$(".action-download-template").click(function() {
var content = publisher.applyTemplate();
2013-05-26 22:59:03 +00:00
var title = fileManager.getCurrentFile().title;
2013-04-22 23:10:08 +00:00
core.saveFile(content, title + ".txt");
});
2013-04-27 23:16:38 +00:00
$(".action-welcome-file").click(function() {
2013-05-26 22:59:03 +00:00
var fileDesc = fileManager.createFile(WELCOME_DOCUMENT_TITLE, welcomeContent);
fileManager.selectFile(fileDesc);
2013-04-27 23:16:38 +00:00
});
2013-04-21 00:07:27 +00:00
});
2013-04-10 18:14:59 +00:00
2013-04-22 01:10:02 +00:00
core.setFileManager(fileManager);
2013-05-26 22:59:03 +00:00
extensionManager.onFileManagerCreated(fileManager);
2013-04-02 18:42:47 +00:00
return fileManager;
});