Stackedit/js/fileMgr.js

364 lines
14 KiB
JavaScript
Raw Normal View History

2013-05-26 22:59:03 +00:00
define([
"jquery",
2013-05-27 19:45:33 +00:00
"underscore",
2013-05-26 22:59:03 +00:00
"core",
"utils",
2013-05-27 19:45:33 +00:00
"settings",
2013-06-10 21:22:32 +00:00
"extensionMgr",
"fileSystem",
"text!../WELCOME.md"
2013-05-27 19:45:33 +00:00
], function($, _, core, utils, settings, extensionMgr, fileSystem, welcomeContent) {
2013-05-29 19:55:23 +00:00
var fileMgr = {};
2013-06-03 22:19:52 +00:00
// Defines a file descriptor (fileDesc objects)
2013-05-29 19:55:23 +00:00
function FileDescriptor(fileIndex, title, syncLocations, publishLocations) {
this.fileIndex = fileIndex;
this._title = title;
2013-06-03 22:19:52 +00:00
this._editorScrollTop = parseInt(localStorage[fileIndex + ".editorScrollTop"]) || 0;
this._editorStart = parseInt(localStorage[fileIndex + ".editorStart"]) || 0;
this._editorEnd = parseInt(localStorage[fileIndex + ".editorEnd"]) || 0;
this._previewScrollTop = parseInt(localStorage[fileIndex + ".previewScrollTop"]) || 0;
2013-06-09 09:49:19 +00:00
this._selectTime = parseInt(localStorage[fileIndex + ".selectTime"]) || 0;
2013-06-03 22:19:52 +00:00
this.syncLocations = syncLocations || {};
this.publishLocations = publishLocations || {};
2013-06-09 09:49:19 +00:00
Object.defineProperty(this, 'title', {
get: function() {
return this._title;
},
set: function(title) {
this._title = title;
localStorage[this.fileIndex + ".title"] = title;
extensionMgr.onTitleChanged(this);
}
2013-06-03 22:19:52 +00:00
});
2013-06-09 09:49:19 +00:00
Object.defineProperty(this, 'content', {
get: function() {
return localStorage[this.fileIndex + ".content"];
},
set: function(content) {
localStorage[this.fileIndex + ".content"] = content;
extensionMgr.onContentChanged(this);
}
2013-06-03 22:19:52 +00:00
});
2013-06-09 09:49:19 +00:00
Object.defineProperty(this, 'editorScrollTop', {
get: function() {
return this._editorScrollTop;
},
set: function(editorScrollTop) {
this._editorScrollTop = editorScrollTop;
localStorage[this.fileIndex + ".editorScrollTop"] = editorScrollTop;
}
2013-06-03 22:19:52 +00:00
});
2013-06-09 09:49:19 +00:00
Object.defineProperty(this, 'editorStart', {
get: function() {
return this._editorStart;
},
set: function(editorStart) {
this._editorStart = editorStart;
localStorage[this.fileIndex + ".editorStart"] = editorStart;
}
2013-06-03 22:19:52 +00:00
});
2013-06-09 09:49:19 +00:00
Object.defineProperty(this, 'editorEnd', {
get: function() {
return this._editorEnd;
},
set: function(editorEnd) {
this._editorEnd = editorEnd;
localStorage[this.fileIndex + ".editorEnd"] = editorEnd;
}
2013-06-03 22:19:52 +00:00
});
2013-06-09 09:49:19 +00:00
Object.defineProperty(this, 'previewScrollTop', {
get: function() {
return this._previewScrollTop;
},
set: function(previewScrollTop) {
this._previewScrollTop = previewScrollTop;
localStorage[this.fileIndex + ".previewScrollTop"] = previewScrollTop;
}
2013-06-03 22:19:52 +00:00
});
2013-06-09 09:49:19 +00:00
Object.defineProperty(this, 'selectTime', {
get: function() {
return this._selectTime;
},
set: function(selectTime) {
this._selectTime = selectTime;
localStorage[this.fileIndex + ".selectTime"] = selectTime;
}
2013-06-03 22:19:52 +00:00
});
2013-05-29 19:55:23 +00:00
}
2013-06-03 22:19:52 +00:00
// Retrieve file descriptors from localStorage and populate fileSystem
_.each(utils.retrieveIndexArray("file.list"), function(fileIndex) {
2013-05-29 19:55:23 +00:00
fileSystem[fileIndex] = new FileDescriptor(fileIndex, localStorage[fileIndex + ".title"]);
});
// Defines the current file
var currentFile = undefined;
fileMgr.getCurrentFile = function() {
return currentFile;
};
fileMgr.isCurrentFile = function(fileDesc) {
return fileDesc === currentFile;
};
fileMgr.setCurrentFile = function(fileDesc) {
currentFile = fileDesc;
if(fileDesc === undefined) {
localStorage.removeItem("file.current");
}
else if(fileDesc.fileIndex != TEMPORARY_FILE_INDEX) {
localStorage["file.current"] = fileDesc.fileIndex;
}
};
fileMgr.selectFile = function(fileDesc) {
fileDesc = fileDesc || fileMgr.getCurrentFile();
if(fileDesc === undefined) {
var fileSystemSize = _.size(fileSystem);
// If fileSystem empty create one file
if(fileSystemSize === 0) {
fileDesc = fileMgr.createFile(WELCOME_DOCUMENT_TITLE, welcomeContent);
}
else {
var fileIndex = localStorage["file.current"];
// If no file is selected take the last created
if(fileIndex === undefined) {
fileIndex = _.keys(fileSystem)[fileSystemSize - 1];
}
fileDesc = fileSystem[fileIndex];
}
}
if(fileMgr.isCurrentFile(fileDesc) === false) {
fileMgr.setCurrentFile(fileDesc);
2013-06-09 09:49:19 +00:00
fileDesc.selectTime = new Date().getTime();
2013-05-29 19:55:23 +00:00
// Notify extensions
extensionMgr.onFileSelected(fileDesc);
// Hide the viewer pencil button
if(fileDesc.fileIndex == TEMPORARY_FILE_INDEX) {
$(".action-edit-document").removeClass("hide");
}
else {
$(".action-edit-document").addClass("hide");
}
}
2013-06-03 22:19:52 +00:00
// Refresh the editor
core.createEditor(fileDesc);
2013-05-29 19:55:23 +00:00
};
fileMgr.createFile = function(title, content, syncLocations, isTemporary) {
content = content !== undefined ? content : settings.defaultContent;
if(!title) {
// Create a file title
title = DEFAULT_FILE_TITLE;
var indicator = 2;
while (_.some(fileSystem, function(fileDesc) {
return fileDesc.title == title;
})) {
title = DEFAULT_FILE_TITLE + indicator++;
}
}
// Generate a unique fileIndex
var fileIndex = TEMPORARY_FILE_INDEX;
if(!isTemporary) {
do {
fileIndex = "file." + utils.randomString();
} while (_.has(fileSystem, fileIndex));
}
// syncIndex associations
syncLocations = syncLocations || {};
var sync = _.reduce(syncLocations, function(sync, syncAttributes, syncIndex) {
return sync + syncIndex + ";";
}, ";");
localStorage[fileIndex + ".title"] = title;
localStorage[fileIndex + ".content"] = content;
localStorage[fileIndex + ".sync"] = sync;
localStorage[fileIndex + ".publish"] = ";";
// Create the file descriptor
var fileDesc = new FileDescriptor(fileIndex, title, syncLocations);
// Add the index to the file list
if(!isTemporary) {
2013-06-03 22:19:52 +00:00
utils.appendIndexToArray("file.list", fileIndex);
2013-05-29 19:55:23 +00:00
fileSystem[fileIndex] = fileDesc;
extensionMgr.onFileCreated(fileDesc);
}
return fileDesc;
};
fileMgr.deleteFile = function(fileDesc) {
fileDesc = fileDesc || fileMgr.getCurrentFile();
2013-05-29 23:04:52 +00:00
// Remove the index from the file list
2013-06-03 22:19:52 +00:00
utils.removeIndexFromArray("file.list", fileDesc.fileIndex);
2013-05-29 23:04:52 +00:00
delete fileSystem[fileDesc.fileIndex];
2013-06-09 09:49:19 +00:00
2013-05-29 19:55:23 +00:00
if(fileMgr.isCurrentFile(fileDesc) === true) {
// Unset the current fileDesc
fileMgr.setCurrentFile();
// Refresh the editor with an other file
fileMgr.selectFile();
}
// Remove synchronized locations
_.each(fileDesc.syncLocations, function(syncAttributes) {
2013-06-03 22:19:52 +00:00
fileMgr.removeSync(syncAttributes);
2013-05-29 19:55:23 +00:00
});
// Remove publish locations
_.each(fileDesc.publishLocations, function(publishAttributes) {
2013-06-03 22:19:52 +00:00
fileMgr.removePublish(publishAttributes);
2013-05-29 19:55:23 +00:00
});
2013-05-29 23:04:52 +00:00
localStorage.removeItem(fileDesc.fileIndex + ".title");
localStorage.removeItem(fileDesc.fileIndex + ".content");
localStorage.removeItem(fileDesc.fileIndex + ".sync");
localStorage.removeItem(fileDesc.fileIndex + ".publish");
2013-05-29 19:55:23 +00:00
extensionMgr.onFileDeleted(fileDesc);
};
// Add a synchronized location to a file
fileMgr.addSync = function(fileDesc, syncAttributes) {
2013-06-03 22:19:52 +00:00
utils.appendIndexToArray(fileDesc.fileIndex + ".sync", syncAttributes.syncIndex);
2013-05-29 19:55:23 +00:00
fileDesc.syncLocations[syncAttributes.syncIndex] = syncAttributes;
// addSync is only used for export, not for import
extensionMgr.onSyncExportSuccess(fileDesc, syncAttributes);
};
// Remove a synchronized location
2013-06-03 22:19:52 +00:00
fileMgr.removeSync = function(syncAttributes) {
2013-05-29 19:55:23 +00:00
var fileDesc = fileMgr.getFileFromSyncIndex(syncAttributes.syncIndex);
if(fileDesc !== undefined) {
2013-06-03 22:19:52 +00:00
utils.removeIndexFromArray(fileDesc.fileIndex + ".sync", syncAttributes.syncIndex);
delete fileDesc.syncLocations[syncAttributes.syncIndex];
2013-05-29 19:55:23 +00:00
extensionMgr.onSyncRemoved(fileDesc, syncAttributes);
}
2013-06-03 22:19:52 +00:00
// Remove sync attributes from localStorage
localStorage.removeItem(syncAttributes.syncIndex);
2013-05-29 19:55:23 +00:00
};
// Get the file descriptor associated to a syncIndex
fileMgr.getFileFromSyncIndex = function(syncIndex) {
return _.find(fileSystem, function(fileDesc) {
return _.has(fileDesc.syncLocations, syncIndex);
});
};
// Get syncAttributes from syncIndex
fileMgr.getSyncAttributes = function(syncIndex) {
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
return fileDesc && fileDesc.syncLocations[syncIndex];
};
// Returns true if provider has locations to synchronize
fileMgr.hasSync = function(provider) {
return _.some(fileSystem, function(fileDesc) {
return _.some(fileDesc.syncLocations, function(syncAttributes) {
return syncAttributes.provider === provider;
});
});
};
// Add a publishIndex (publish location) to a file
fileMgr.addPublish = function(fileDesc, publishAttributes) {
2013-06-03 22:19:52 +00:00
utils.appendIndexToArray(fileDesc.fileIndex + ".publish", publishAttributes.publishIndex);
2013-05-29 19:55:23 +00:00
fileDesc.publishLocations[publishAttributes.publishIndex] = publishAttributes;
extensionMgr.onNewPublishSuccess(fileDesc, publishAttributes);
};
// Remove a publishIndex (publish location)
2013-06-03 22:19:52 +00:00
fileMgr.removePublish = function(publishAttributes) {
2013-05-29 19:55:23 +00:00
var fileDesc = fileMgr.getFileFromPublishIndex(publishAttributes.publishIndex);
if(fileDesc !== undefined) {
2013-06-03 22:19:52 +00:00
utils.removeIndexFromArray(fileDesc.fileIndex + ".publish", publishAttributes.publishIndex);
delete fileDesc.publishLocations[publishAttributes.publishIndex];
2013-05-29 19:55:23 +00:00
extensionMgr.onPublishRemoved(fileDesc, publishAttributes);
}
2013-06-03 22:19:52 +00:00
// Remove publish attributes from localStorage
localStorage.removeItem(publishAttributes.publishIndex);
2013-05-29 19:55:23 +00:00
};
// Get the file descriptor associated to a publishIndex
fileMgr.getFileFromPublishIndex = function(publishIndex) {
return _.find(fileSystem, function(fileDesc) {
return _.has(fileDesc.publishLocations, publishIndex);
});
};
core.onReady(function() {
fileMgr.selectFile();
$(".action-create-file").click(function() {
var fileDesc = fileMgr.createFile();
fileMgr.selectFile(fileDesc);
var wmdInput = $("#wmd-input").focus().get(0);
if(wmdInput.setSelectionRange) {
wmdInput.setSelectionRange(0, 0);
}
$("#file-title").click();
});
$(".action-remove-file").click(function() {
fileMgr.deleteFile();
});
$("#file-title").click(function() {
if(viewerMode === true) {
return;
}
$(this).hide();
var fileTitleInput = $("#file-title-input").show();
_.defer(function() {
fileTitleInput.focus().get(0).select();
});
});
function applyTitle(input) {
input.hide();
$("#file-title").show();
var title = $.trim(input.val());
var fileDesc = fileMgr.getCurrentFile();
if(title && title != fileDesc.title) {
fileDesc.title = title;
}
input.val(fileDesc.title);
$("#wmd-input").focus();
}
$("#file-title-input").blur(function() {
applyTitle($(this));
}).keyup(function(e) {
if(e.keyCode == 13) {
applyTitle($(this));
}
if(e.keyCode == 27) {
$(this).val("");
applyTitle($(this));
}
});
$(".action-open-stackedit").click(function() {
window.location.href = ".";
});
$(".action-edit-document").click(function() {
var content = $("#wmd-input").val();
var title = fileMgr.getCurrentFile().title;
var fileDesc = fileMgr.createFile(title, content);
fileMgr.selectFile(fileDesc);
window.location.href = ".";
});
$(".action-welcome-file").click(function() {
var fileDesc = fileMgr.createFile(WELCOME_DOCUMENT_TITLE, welcomeContent);
fileMgr.selectFile(fileDesc);
});
});
extensionMgr.onFileMgrCreated(fileMgr);
return fileMgr;
2013-04-02 18:42:47 +00:00
});