Stackedit/public/res/fileMgr.js

210 lines
7.3 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-11-05 23:03:38 +00:00
"constants",
2013-05-26 22:59:03 +00:00
"core",
"utils",
2013-11-05 23:03:38 +00:00
"storage",
2013-05-27 19:45:33 +00:00
"settings",
2013-07-30 08:46:36 +00:00
"eventMgr",
2013-06-10 21:22:32 +00:00
"fileSystem",
2013-06-16 10:47:35 +00:00
"classes/FileDescriptor",
2013-10-19 22:59:17 +00:00
"text!WELCOME.md"
2013-11-05 23:03:38 +00:00
], function($, _, constants, core, utils, storage, settings, eventMgr, fileSystem, FileDescriptor, welcomeContent) {
2013-05-29 19:55:23 +00:00
var fileMgr = {};
// Defines the current file
2013-06-19 20:33:46 +00:00
fileMgr.currentFile = undefined;
2013-05-29 19:55:23 +00:00
// Set the current file and refresh the editor
2013-05-29 19:55:23 +00:00
fileMgr.selectFile = function(fileDesc) {
2013-06-19 20:33:46 +00:00
fileDesc = fileDesc || fileMgr.currentFile;
2013-05-29 19:55:23 +00:00
if(fileDesc === undefined) {
var fileSystemSize = _.size(fileSystem);
if(fileSystemSize === 0) {
2013-06-16 10:47:35 +00:00
// If fileSystem empty create one file
2013-11-05 23:03:38 +00:00
fileDesc = fileMgr.createFile(constants.WELCOME_DOCUMENT_TITLE, welcomeContent);
2013-05-29 19:55:23 +00:00
}
else {
2013-06-16 10:47:35 +00:00
// Select the last selected file
fileDesc = _.max(fileSystem, function(fileDesc) {
return fileDesc.selectTime || 0;
});
2013-05-29 19:55:23 +00:00
}
}
2013-06-19 20:33:46 +00:00
if(fileMgr.currentFile !== fileDesc) {
fileMgr.currentFile = fileDesc;
2013-06-09 09:49:19 +00:00
fileDesc.selectTime = new Date().getTime();
2013-05-29 19:55:23 +00:00
// Notify extensions
2013-07-30 08:46:36 +00:00
eventMgr.onFileSelected(fileDesc);
2013-05-29 19:55:23 +00:00
// Hide the viewer pencil button
2013-11-05 23:03:38 +00:00
$(".action-edit-document").toggleClass("hide", fileDesc.fileIndex != constants.TEMPORARY_FILE_INDEX);
2013-05-29 19:55:23 +00:00
}
2013-08-22 00:19:59 +00:00
// Refresh the editor (even if it's the same file)
2013-06-19 20:33:46 +00:00
core.initEditor(fileDesc);
2013-05-29 19:55:23 +00:00
};
2014-03-28 00:49:49 +00:00
fileMgr.createFile = function(title, content, discussionListJSON, syncLocations, isTemporary) {
2013-05-29 19:55:23 +00:00
content = content !== undefined ? content : settings.defaultContent;
if(!title) {
// Create a file title
2013-11-05 23:03:38 +00:00
title = constants.DEFAULT_FILE_TITLE;
2013-05-29 19:55:23 +00:00
var indicator = 2;
2013-11-05 23:03:38 +00:00
var checkTitle = function (fileDesc) {
2013-05-29 19:55:23 +00:00
return fileDesc.title == title;
2013-11-05 23:03:38 +00:00
};
while (_.some(fileSystem, checkTitle)) {
title = constants.DEFAULT_FILE_TITLE + indicator++;
2013-05-29 19:55:23 +00:00
}
}
// Generate a unique fileIndex
2013-11-05 23:03:38 +00:00
var fileIndex = constants.TEMPORARY_FILE_INDEX;
2013-05-29 19:55:23 +00:00
if(!isTemporary) {
do {
fileIndex = "file." + utils.randomString();
} while (_.has(fileSystem, fileIndex));
}
// syncIndex associations
syncLocations = syncLocations || {};
var sync = _.reduce(syncLocations, function(sync, syncAttributes) {
2013-06-16 10:47:35 +00:00
utils.storeAttributes(syncAttributes);
return sync + syncAttributes.syncIndex + ";";
2013-05-29 19:55:23 +00:00
}, ";");
2013-11-05 23:03:38 +00:00
storage[fileIndex + ".title"] = title;
storage[fileIndex + ".content"] = content;
storage[fileIndex + ".sync"] = sync;
storage[fileIndex + ".publish"] = ";";
2013-05-29 19:55:23 +00:00
// Create the file descriptor
var fileDesc = new FileDescriptor(fileIndex, title, syncLocations);
2014-03-28 00:49:49 +00:00
discussionListJSON && (fileDesc.discussionListJSON = discussionListJSON);
2013-05-29 19:55:23 +00:00
// 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;
2013-07-30 08:46:36 +00:00
eventMgr.onFileCreated(fileDesc);
2013-05-29 19:55:23 +00:00
}
return fileDesc;
};
fileMgr.deleteFile = function(fileDesc) {
2013-06-19 20:33:46 +00:00
fileDesc = fileDesc || fileMgr.currentFile;
2014-03-20 00:24:56 +00:00
2013-12-02 00:09:39 +00:00
// Unassociate file from folder
if(fileDesc.folder) {
fileDesc.folder.removeFile(fileDesc);
eventMgr.onFoldersChanged();
}
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
// Don't bother with fields in localStorage, they will be removed on next page load
2013-06-19 20:33:46 +00:00
if(fileMgr.currentFile === fileDesc) {
2013-05-29 19:55:23 +00:00
// Unset the current fileDesc
2013-06-19 20:33:46 +00:00
fileMgr.currentFile = undefined;
2013-06-16 10:47:35 +00:00
// Refresh the editor with another file
2013-05-29 19:55:23 +00:00
fileMgr.selectFile();
}
2013-07-30 08:46:36 +00:00
eventMgr.onFileDeleted(fileDesc);
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 the file descriptor associated to a publishIndex
fileMgr.getFileFromPublishIndex = function(publishIndex) {
return _.find(fileSystem, function(fileDesc) {
return _.has(fileDesc.publishLocations, publishIndex);
});
};
2013-09-09 23:32:24 +00:00
eventMgr.addListener("onReady", function() {
2013-11-05 23:03:38 +00:00
var $editorElt = $("#wmd-input");
2013-05-29 19:55:23 +00:00
fileMgr.selectFile();
2013-08-22 00:19:59 +00:00
var $fileTitleElt = $('.file-title-navbar');
var $fileTitleInputElt = $(".input-file-title");
2013-05-29 19:55:23 +00:00
$(".action-create-file").click(function() {
2014-03-10 23:53:03 +00:00
setTimeout(function() {
var fileDesc = fileMgr.createFile();
fileMgr.selectFile(fileDesc);
$fileTitleElt.click();
}, 400);
2013-05-29 19:55:23 +00:00
});
$('.action-remove-file-confirm').click(function() {
$('.modal-remove-file-confirm').modal('show');
});
2013-05-29 19:55:23 +00:00
$(".action-remove-file").click(function() {
fileMgr.deleteFile();
});
2013-08-22 00:19:59 +00:00
$fileTitleElt.click(function() {
2013-11-05 23:03:38 +00:00
if(window.viewerMode === true) {
2013-05-29 19:55:23 +00:00
return;
}
2013-08-22 00:19:59 +00:00
$fileTitleElt.addClass('hide');
var fileTitleInput = $fileTitleInputElt.removeClass('hide');
2014-04-08 23:20:48 +00:00
setTimeout(function() {
2013-05-29 19:55:23 +00:00
fileTitleInput.focus().get(0).select();
2014-04-08 23:20:48 +00:00
}, 10);
2013-05-29 19:55:23 +00:00
});
2013-08-06 23:52:58 +00:00
function applyTitle() {
2013-08-22 00:19:59 +00:00
$fileTitleInputElt.addClass('hide');
$fileTitleElt.removeClass('hide');
var title = $.trim($fileTitleInputElt.val());
2013-06-19 20:33:46 +00:00
var fileDesc = fileMgr.currentFile;
2013-05-29 19:55:23 +00:00
if(title && title != fileDesc.title) {
fileDesc.title = title;
2013-07-30 08:46:36 +00:00
eventMgr.onTitleChanged(fileDesc);
2013-05-29 19:55:23 +00:00
}
2013-08-22 00:19:59 +00:00
$fileTitleInputElt.val(fileDesc.title);
2014-03-20 00:24:56 +00:00
$editorElt.focus();
2013-05-29 19:55:23 +00:00
}
2013-08-22 00:19:59 +00:00
$fileTitleInputElt.blur(function() {
2013-08-06 23:52:58 +00:00
applyTitle();
2013-05-29 19:55:23 +00:00
}).keyup(function(e) {
if(e.keyCode == 13) {
2013-08-06 23:52:58 +00:00
applyTitle();
2013-05-29 19:55:23 +00:00
}
if(e.keyCode == 27) {
2013-08-22 00:19:59 +00:00
$fileTitleInputElt.val("");
2013-08-06 23:52:58 +00:00
applyTitle();
2013-05-29 19:55:23 +00:00
}
});
$(".action-open-stackedit").click(function() {
window.location.href = ".";
});
$(".action-edit-document").click(function() {
2013-09-14 16:59:40 +00:00
var content = $editorElt.val();
2013-06-19 20:33:46 +00:00
var title = fileMgr.currentFile.title;
2013-05-29 19:55:23 +00:00
var fileDesc = fileMgr.createFile(title, content);
fileMgr.selectFile(fileDesc);
window.location.href = ".";
});
$(".action-welcome-file").click(function() {
2013-11-05 23:03:38 +00:00
var fileDesc = fileMgr.createFile(constants.WELCOME_DOCUMENT_TITLE, welcomeContent);
2013-05-29 19:55:23 +00:00
fileMgr.selectFile(fileDesc);
});
});
2013-07-30 08:46:36 +00:00
eventMgr.onFileMgrCreated(fileMgr);
2013-05-29 19:55:23 +00:00
return fileMgr;
2013-04-02 18:42:47 +00:00
});