Stackedit/js/file-manager.js

520 lines
17 KiB
JavaScript
Raw Normal View History

2013-04-13 18:11:54 +00:00
define(["jquery", "google-helper", "dropbox-helper", "github-helper", "synchronizer", "publisher", "underscore"],
2013-04-11 22:38:41 +00:00
function($, googleHelper, dropboxHelper, githubHelper, synchronizer, publisher) {
2013-04-02 18:42:47 +00:00
var fileManager = {};
2013-04-10 18:14:59 +00:00
// Dependencies
var core = undefined;
2013-04-13 18:11:54 +00:00
// Defines the current file
var currentFileIndex = localStorage["file.current"];
fileManager.getCurrentFileIndex = function() {
return currentFileIndex;
};
fileManager.isCurrentFileIndex = function(fileIndex) {
return fileIndex == currentFileIndex;
};
fileManager.setCurrentFileIndex = function(fileIndex) {
currentFileIndex = fileIndex;
// Sanity check since we are going to modify current file in localStorage
core.checkWindowUnique();
if(fileIndex === undefined) {
localStorage.removeItem("file.current");
}
else {
localStorage["file.current"] = fileIndex;
}
};
2013-04-02 18:42:47 +00:00
// Caution: this function recreate the editor (reset undo operations)
var fileDescList = [];
fileManager.selectFile = function(fileIndex) {
// If no file create one
if (localStorage["file.list"].length === 1) {
fileIndex = this.createFile();
}
if(fileIndex !== undefined) {
2013-04-13 18:11:54 +00:00
fileManager.setCurrentFileIndex(fileIndex);
2013-04-02 18:42:47 +00:00
}
// Update the file titles
2013-04-10 23:13:31 +00:00
fileManager.updateFileTitles();
2013-04-05 23:59:59 +00:00
refreshManageSync();
2013-04-14 13:24:29 +00:00
publisher.notifyPublish();
2013-04-02 18:42:47 +00:00
// Recreate the editor
2013-04-13 18:11:54 +00:00
fileIndex = fileManager.getCurrentFileIndex();
2013-04-02 18:42:47 +00:00
$("#wmd-input").val(localStorage[fileIndex + ".content"]);
core.createEditor(function() {
fileManager.saveFile();
});
};
fileManager.createFile = function(title, content, syncIndexes) {
content = content || "";
syncIndexes = syncIndexes || [];
if (!title) {
// Create a file title
title = DEFAULT_FILE_TITLE;
var indicator = 2;
2013-04-13 18:11:54 +00:00
while(_.some(fileDescList, function(fileDesc) {
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
var fileIndex = undefined;
do {
fileIndex = "file." + core.randomString();
2013-04-13 18:11:54 +00:00
} while(_.has(localStorage, fileIndex + ".title"));
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-04-13 18:11:54 +00:00
var sync = _.reduce(syncIndexes, function(sync, syncIndex) {
return sync + syncIndex + ";";
}, ";");
2013-04-02 18:42:47 +00:00
localStorage[fileIndex + ".sync"] = sync;
2013-04-09 07:58:06 +00:00
localStorage[fileIndex + ".publish"] = ";";
2013-04-02 18:42:47 +00:00
localStorage["file.list"] += fileIndex + ";";
return fileIndex;
};
fileManager.deleteFile = function(fileIndex) {
2013-04-13 18:11:54 +00:00
fileIndex = fileIndex || fileManager.getCurrentFileIndex();
if(fileManager.isCurrentFileIndex(fileIndex)) {
// Unset the current fileIndex
fileManager.setCurrentFileIndex();
2013-04-02 18:42:47 +00:00
}
// Remove synchronized locations
2013-04-13 18:11:54 +00:00
var syncIndexList = _.compact(localStorage[fileIndex + ".sync"].split(";"));
2013-04-02 18:42:47 +00:00
localStorage.removeItem(fileIndex + ".sync");
2013-04-13 18:11:54 +00:00
_.each(syncIndexList, function(syncIndex) {
fileManager.removeSync(syncIndex);
});
2013-04-11 22:38:41 +00:00
// Remove publish locations
2013-04-13 18:11:54 +00:00
var publishIndexList = _.compact(localStorage[fileIndex + ".publish"].split(";"));
localStorage.removeItem(fileIndex + ".publish");
_.each(publishIndexList, function(publishIndex) {
2013-04-11 22:38:41 +00:00
fileManager.removePublish(publishIndex);
2013-04-13 18:11:54 +00:00
});
2013-04-02 18:42:47 +00:00
localStorage["file.list"] = localStorage["file.list"].replace(";"
+ fileIndex + ";", ";");
localStorage.removeItem(fileIndex + ".title");
localStorage.removeItem(fileIndex + ".content");
};
fileManager.saveFile = function() {
var content = $("#wmd-input").val();
2013-04-13 18:11:54 +00:00
var fileIndex = fileManager.getCurrentFileIndex();
2013-04-02 18:42:47 +00:00
localStorage[fileIndex + ".content"] = content;
2013-04-10 18:14:59 +00:00
synchronizer.notifyChange(fileIndex);
2013-04-02 18:42:47 +00:00
};
fileManager.updateFileTitles = function() {
$("#file-selector").empty();
2013-04-14 13:24:29 +00:00
fileDescList = _.chain(localStorage["file.list"].split(";")).compact()
2013-04-13 18:11:54 +00:00
.reduce(function(fileDescList, fileIndex) {
var title = localStorage[fileIndex + ".title"];
fileDescList.push({ index : fileIndex, title : title });
return fileDescList;
}, [])
.sortBy(function(fileDesc) {
return fileDesc.title.toLowerCase();
}).value();
2013-04-02 18:42:47 +00:00
2013-04-13 18:11:54 +00:00
var fileIndex = fileManager.getCurrentFileIndex();
2013-04-02 18:42:47 +00:00
// If no default file take first one
2013-04-13 18:11:54 +00:00
if (fileIndex === undefined) {
2013-04-02 18:42:47 +00:00
fileIndex = fileDescList[0].index;
2013-04-13 18:11:54 +00:00
fileManager.setCurrentFileIndex(fileIndex);
2013-04-02 18:42:47 +00:00
}
var useGoogleDrive = false;
2013-04-07 15:22:13 +00:00
var useDropbox = false;
2013-04-02 18:42:47 +00:00
function composeTitle(fileIndex) {
2013-04-07 15:22:13 +00:00
var result = " " + localStorage[fileIndex + ".title"];
2013-04-02 18:42:47 +00:00
var sync = localStorage[fileIndex + ".sync"];
2013-04-07 15:22:13 +00:00
if (sync.indexOf(";" + SYNC_PROVIDER_DROPBOX) !== -1) {
useDropbox = true;
result = '<i class="icon-dropbox"></i>' + result;
}
2013-04-02 18:42:47 +00:00
if (sync.indexOf(";" + SYNC_PROVIDER_GDRIVE) !== -1) {
useGoogleDrive = true;
2013-04-07 15:22:13 +00:00
result = '<i class="icon-gdrive"></i>' + result;
2013-04-02 18:42:47 +00:00
}
return result;
}
// Update the file title
var title = localStorage[fileIndex + ".title"];
document.title = "StackEdit - " + title;
$("#file-title").html(composeTitle(fileIndex));
$(".file-title").text(title);
$("#file-title-input").val(title);
// Update the file selector
$("#file-selector").empty();
2013-04-14 13:24:29 +00:00
_.each(fileDescList, function(fileDesc) {
2013-04-02 18:42:47 +00:00
var a = $("<a>").html(composeTitle(fileDesc.index));
var li = $("<li>").append(a);
if (fileDesc.index == fileIndex) {
li.addClass("disabled");
} else {
a.prop("href", "#").click((function(fileIndex) {
return function() {
2013-04-13 18:11:54 +00:00
fileManager.selectFile(fileIndex);
2013-04-02 18:42:47 +00:00
};
})(fileDesc.index));
}
2013-04-14 13:24:29 +00:00
$("#file-selector").append(li);
});
2013-04-03 22:52:29 +00:00
synchronizer.useGoogleDrive = useGoogleDrive;
2013-04-07 15:22:13 +00:00
synchronizer.useDropbox = useDropbox;
2013-04-02 18:42:47 +00:00
};
2013-04-14 13:24:29 +00:00
// Remove a syncIndex (synchronized location)
2013-04-13 18:11:54 +00:00
fileManager.removeSync = function(syncIndex) {
2013-04-14 13:24:29 +00:00
var fileIndex = fileManager.getFileIndexFromSync(syncIndex);
2013-04-02 18:42:47 +00:00
if(fileIndex !== undefined) {
localStorage[fileIndex + ".sync"] = localStorage[fileIndex + ".sync"].replace(";"
2013-04-13 18:11:54 +00:00
+ syncIndex + ";", ";");
2013-04-14 13:24:29 +00:00
if(fileManager.isCurrentFileIndex(fileIndex)) {
2013-04-02 18:42:47 +00:00
refreshManageSync();
}
}
2013-04-09 07:58:06 +00:00
// Remove ETAG, version, CRCs (if any)
2013-04-13 18:11:54 +00:00
localStorage.removeItem(syncIndex + ".etag");
localStorage.removeItem(syncIndex + ".version");
localStorage.removeItem(syncIndex + ".contentCRC");
localStorage.removeItem(syncIndex + ".titleCRC");
2013-04-02 18:42:47 +00:00
};
2013-04-14 13:24:29 +00:00
// Get the fileIndex associated to a syncIndex
2013-04-13 18:11:54 +00:00
fileManager.getFileIndexFromSync = function(syncIndex) {
2013-04-14 13:24:29 +00:00
return _.chain(localStorage["file.list"].split(";")).compact()
.find(function(fileIndex) {
var sync = localStorage[fileIndex + ".sync"];
return sync.indexOf(";" + syncIndex + ";") !== -1;
}).value();
2013-04-02 18:42:47 +00:00
};
2013-04-14 13:24:29 +00:00
// Remove a publishIndex (publish location)
2013-04-11 22:38:41 +00:00
fileManager.removePublish = function(publishIndex) {
2013-04-14 13:24:29 +00:00
var fileIndex = fileManager.getFileIndexFromPublish(publishIndex);
2013-04-11 22:38:41 +00:00
if(fileIndex !== undefined) {
localStorage[fileIndex + ".publish"] = localStorage[fileIndex + ".publish"].replace(";"
+ publishIndex + ";", ";");
2013-04-14 13:24:29 +00:00
if(fileManager.isCurrentFileIndex(fileIndex)) {
publisher.notifyPublish();
2013-04-11 22:38:41 +00:00
}
}
// Remove publish object
localStorage.removeItem(publishIndex);
};
2013-04-14 13:24:29 +00:00
// Get the fileIndex associated to a publishIndex
2013-04-11 22:38:41 +00:00
fileManager.getFileIndexFromPublish = function(publishIndex) {
2013-04-14 13:24:29 +00:00
return _.chain(localStorage["file.list"].split(";")).compact()
.find(function(fileIndex) {
var sync = localStorage[fileIndex + ".publish"];
return sync.indexOf(";" + publishIndex + ";") !== -1;
}).value();
2013-04-11 22:38:41 +00:00
};
2013-04-09 07:58:06 +00:00
function uploadGdrive(fileId, folderId) {
2013-04-13 18:11:54 +00:00
var fileIndex = fileManager.getCurrentFileIndex();
2013-04-02 18:42:47 +00:00
var content = localStorage[fileIndex + ".content"];
var title = localStorage[fileIndex + ".title"];
2013-04-13 18:11:54 +00:00
googleHelper.upload(fileId, folderId, title, content, function(syncIndex) {
if (syncIndex === undefined) {
2013-04-03 22:52:29 +00:00
return;
2013-04-02 18:42:47 +00:00
}
2013-04-09 07:58:06 +00:00
var contentCRC = core.crc32(content);
2013-04-13 18:11:54 +00:00
localStorage[syncIndex + ".contentCRC"] = contentCRC;
2013-04-09 07:58:06 +00:00
var titleCRC = core.crc32(title);
2013-04-13 18:11:54 +00:00
localStorage[syncIndex + ".titleCRC"] = titleCRC;
localStorage[fileIndex + ".sync"] += syncIndex + ";";
2013-04-03 22:52:29 +00:00
refreshManageSync();
fileManager.updateFileTitles();
core.showMessage('"' + title
+ '" will now be synchronized on Google Drive.');
2013-04-02 18:42:47 +00:00
});
}
2013-04-03 22:52:29 +00:00
2013-04-09 07:58:06 +00:00
function manualGdrive(fileId) {
if(!fileId) {
return;
}
// Check that file is not synchronized with an other one
2013-04-13 18:11:54 +00:00
var syncIndex = SYNC_PROVIDER_GDRIVE + fileId;
var fileIndex = fileManager.getFileIndexFromSync(syncIndex);
2013-04-09 07:58:06 +00:00
if(fileIndex !== undefined) {
var title = localStorage[fileIndex + ".title"];
core.showError('File ID is already synchronized with "' + title + '"');
return;
}
uploadGdrive(fileId);
}
2013-04-05 23:59:59 +00:00
function importGdrive(ids) {
if(ids === undefined) {
return;
}
var importIds = [];
for(var i=0; i<ids.length; i++) {
var fileId = ids[i];
2013-04-13 18:11:54 +00:00
var syncIndex = SYNC_PROVIDER_GDRIVE + fileId;
var fileIndex = fileManager.getFileIndexFromSync(syncIndex);
2013-04-05 23:59:59 +00:00
if(fileIndex !== undefined) {
var title = localStorage[fileIndex + ".title"];
2013-04-06 01:07:45 +00:00
core.showError('"' + title + '" was already imported');
2013-04-05 23:59:59 +00:00
continue;
}
importIds.push(fileId);
}
2013-04-09 07:58:06 +00:00
googleHelper.importFiles(importIds);
2013-04-02 18:42:47 +00:00
}
2013-04-07 15:22:13 +00:00
function manualDropbox(path) {
if(!path) {
return;
}
2013-04-09 07:58:06 +00:00
path = dropboxHelper.checkPath(path);
2013-04-07 15:22:13 +00:00
if(path === undefined) {
return;
}
// Check that file is not synchronized with an other one
2013-04-13 18:11:54 +00:00
var syncIndex = SYNC_PROVIDER_DROPBOX + encodeURIComponent(path.toLowerCase());
var fileIndex = fileManager.getFileIndexFromSync(syncIndex);
2013-04-07 15:22:13 +00:00
if(fileIndex !== undefined) {
var title = localStorage[fileIndex + ".title"];
core.showError('Path "' + path + '" is already synchronized with "' + title + '"');
return;
}
2013-04-13 18:11:54 +00:00
var fileIndex = fileManager.getCurrentFileIndex();
2013-04-07 15:22:13 +00:00
var content = localStorage[fileIndex + ".content"];
var title = localStorage[fileIndex + ".title"];
2013-04-13 18:11:54 +00:00
dropboxHelper.upload(path, content, function(syncIndex) {
if (syncIndex === undefined) {
2013-04-07 15:22:13 +00:00
return;
}
2013-04-09 07:58:06 +00:00
var contentCRC = core.crc32(content);
2013-04-13 18:11:54 +00:00
localStorage[syncIndex + ".contentCRC"] = contentCRC;
localStorage[fileIndex + ".sync"] += syncIndex + ";";
2013-04-07 15:22:13 +00:00
refreshManageSync();
fileManager.updateFileTitles();
core.showMessage('"' + title
+ '" will now be synchronized on Dropbox.');
});
}
function importDropbox(paths) {
if(paths === undefined) {
return;
}
var importPaths = [];
for(var i=0; i<paths.length; i++) {
var filePath = paths[i];
2013-04-13 18:11:54 +00:00
var syncIndex = SYNC_PROVIDER_DROPBOX + encodeURIComponent(filePath.toLowerCase());
var fileIndex = fileManager.getFileIndexFromSync(syncIndex);
2013-04-07 15:22:13 +00:00
if(fileIndex !== undefined) {
var title = localStorage[fileIndex + ".title"];
core.showError('"' + title + '" was already imported');
continue;
}
importPaths.push(filePath);
}
2013-04-09 07:58:06 +00:00
dropboxHelper.importFiles(importPaths);
2013-04-07 15:22:13 +00:00
}
2013-04-02 18:42:47 +00:00
function refreshManageSync() {
2013-04-13 18:11:54 +00:00
var fileIndex = fileManager.getCurrentFileIndex();
2013-04-14 13:24:29 +00:00
var syncIndexList = _.compact(localStorage[fileIndex + ".sync"].split(";"));
2013-04-02 18:42:47 +00:00
$(".msg-no-sync, .msg-sync-list").addClass("hide");
$("#manage-sync-list .input-append").remove();
2013-04-14 13:24:29 +00:00
if (syncIndexList.length > 0) {
2013-04-02 18:42:47 +00:00
$(".msg-sync-list").removeClass("hide");
} else {
$(".msg-no-sync").removeClass("hide");
}
2013-04-14 13:24:29 +00:00
_.each(syncIndexList, function(syncIndex) {
var line = $("<div>").addClass("input-prepend input-append");
if (syncIndex.indexOf(SYNC_PROVIDER_GDRIVE) === 0) {
line.append($("<span>").addClass("add-on").prop("title", "Google Drive").html(
'<i class="icon-gdrive"></i>'));
line.append($("<input>").prop("type", "text").prop(
"disabled", true).addClass("span5").val(
syncIndex.substring(SYNC_PROVIDER_GDRIVE.length)));
}
else if (syncIndex.indexOf(SYNC_PROVIDER_DROPBOX) === 0) {
line.append($("<span>").addClass("add-on").prop("title", "Dropbox").html(
'<i class="icon-dropbox"></i>'));
line.append($("<input>").prop("type", "text").prop(
"disabled", true).addClass("span5").val(
decodeURIComponent(syncIndex.substring(SYNC_PROVIDER_DROPBOX.length))));
}
line.append($("<a>").addClass("btn").html(
2013-04-11 22:38:41 +00:00
'<i class="icon-trash"></i>').prop("title",
"Remove this location").click(function() {
2013-04-14 13:24:29 +00:00
fileManager.removeSync(syncIndex);
fileManager.updateFileTitles();
}));
$("#manage-sync-list").append(line);
});
2013-04-11 22:38:41 +00:00
}
2013-04-10 23:13:31 +00:00
// Initialize the "New publication" dialog
var newPublishProvider = undefined;
function initNewPublish(provider, defaultPublishFormat) {
defaultPublishFormat = defaultPublishFormat || "markdown";
newPublishProvider = provider;
// Show/hide controls depending on provider
2013-04-12 23:09:34 +00:00
$('div[class*=" modal-publish-"]').hide().filter(".modal-publish-" + provider).show();
2013-04-10 23:13:31 +00:00
// Reset fields
core.resetModalInputs();
$("input:radio[name=radio-publish-format][value=" + defaultPublishFormat + "]").prop("checked", true);
// Open dialog box
$("#modal-publish").modal();
}
2013-04-11 22:38:41 +00:00
// Create a new publication on GitHub
function newPublishGithub(event) {
2013-04-14 21:15:40 +00:00
var publishAttributes = {};
publishAttributes.repository = core.getInputValue($("#input-publish-github-reponame"), event);
publishAttributes.branch = core.getInputValue($("#input-publish-github-branch"), event);
publishAttributes.path = core.getInputValue($("#input-publish-github-path"), event);
publishAttributes.provider = newPublishProvider;
2013-04-11 22:38:41 +00:00
if(event.isPropagationStopped()) {
return;
}
2013-04-14 21:15:40 +00:00
publisher.newLocation(publishAttributes);
2013-04-11 22:38:41 +00:00
}
// Create a new publication on Blogger
2013-04-10 23:13:31 +00:00
function newPublishBlogger(event) {
var blogUrl = core.getInputValue($("#input-publish-blogger-url"), event);
if(event.isPropagationStopped()) {
return;
}
googleHelper.getBlogByUrl(blogUrl, function(blog) {
console.log(blog);
});
}
2013-04-02 18:42:47 +00:00
2013-04-10 18:14:59 +00:00
fileManager.init = function(coreModule) {
core = coreModule;
fileManager.selectFile();
$(".action-create-file").click(function() {
var fileIndex = fileManager.createFile();
fileManager.selectFile(fileIndex);
$("#file-title").click();
});
$(".action-remove-file").click(function() {
fileManager.deleteFile();
fileManager.selectFile();
});
$("#file-title").click(function() {
$(this).hide();
$("#file-title-input").show().focus();
});
$("#file-title-input").blur(function() {
var title = $.trim($(this).val());
if (title) {
2013-04-13 18:11:54 +00:00
var fileIndexTitle = fileManager.getCurrentFileIndex() + ".title";
2013-04-10 18:14:59 +00:00
if (title != localStorage[fileIndexTitle]) {
localStorage[fileIndexTitle] = title;
fileManager.updateFileTitles();
fileManager.saveFile();
}
}
$(this).hide();
$("#file-title").show();
});
$(".action-download-md").click(
function() {
var content = $("#wmd-input").val();
var uriContent = "data:application/octet-stream;base64,"
+ core.encodeBase64(content);
window.open(uriContent, 'file');
});
$(".action-download-html").click(
function() {
var content = $("#wmd-preview").html();
var uriContent = "data:application/octet-stream;base64,"
+ core.encodeBase64(content);
window.open(uriContent, 'file');
2013-04-14 13:24:29 +00:00
});
$(".action-download-template").click(
function() {
var content = publisher.applyTemplate();
var uriContent = "data:application/octet-stream;base64,"
+ core.encodeBase64(content);
window.open(uriContent, 'file');
2013-04-10 18:14:59 +00:00
});
2013-04-10 23:13:31 +00:00
// Synchronize actions
2013-04-10 18:14:59 +00:00
$(".action-upload-gdrive-root").click(function() {
uploadGdrive();
});
$(".action-upload-gdrive-select").click(function() {
// This action is not available because picker does not support
// folder selection
googleHelper.picker(function(ids) {
if(ids !== undefined && ids.length !== 0) {
uploadGdrive(undefined, ids[0]);
}
}, true);
});
$(".action-download-gdrive").click(function() {
googleHelper.picker(importGdrive);
});
$(".action-manual-gdrive").click(function(event) {
var fileId = core.getInputValue($("#manual-gdrive-fileid"), event);
manualGdrive(fileId);
});
$(".action-download-dropbox").click(function() {
dropboxHelper.picker(importDropbox);
});
$(".action-upload-dropbox").click(function(event) {
var path = core.getInputValue($("#upload-dropbox-path"), event);
manualDropbox(path);
});
$(".action-manual-dropbox").click(function(event) {
var path = core.getInputValue($("#manual-dropbox-path"), event);
manualDropbox(path);
});
2013-04-10 23:13:31 +00:00
// Publish actions
$(".action-publish-github").click(function() {
2013-04-14 13:24:29 +00:00
initNewPublish(PROVIDER_GITHUB);
2013-04-10 23:13:31 +00:00
});
$(".action-publish-blogger").click(function() {
2013-04-14 13:24:29 +00:00
initNewPublish(PROVIDER_BLOGGER, "html");
2013-04-10 23:13:31 +00:00
});
$(".action-process-publish").click(function(e) {
2013-04-14 13:24:29 +00:00
if(newPublishProvider == PROVIDER_GITHUB) {
2013-04-11 22:38:41 +00:00
newPublishGithub(e);
}
2013-04-14 13:24:29 +00:00
else if(newPublishProvider == PROVIDER_BLOGGER) {
2013-04-10 23:13:31 +00:00
newPublishBlogger(e);
}
});
2013-04-10 18:14:59 +00:00
};
2013-04-02 18:42:47 +00:00
return fileManager;
});