2013-05-02 00:02:57 +00:00
|
|
|
define(["jquery", "core", "synchronizer", "publisher", "sharing", "text!../WELCOME.md", "underscore"],
|
|
|
|
function($, core, synchronizer, publisher, sharing, welcomeContent) {
|
2013-05-04 00:05:58 +00:00
|
|
|
|
|
|
|
var TEMPORARY_FILE_INDEX = "file.tempIndex";
|
2013-04-02 18:42:47 +00:00
|
|
|
|
|
|
|
var fileManager = {};
|
|
|
|
|
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;
|
|
|
|
if(fileIndex === undefined) {
|
|
|
|
localStorage.removeItem("file.current");
|
|
|
|
}
|
2013-05-04 00:05:58 +00:00
|
|
|
else if(fileIndex != TEMPORARY_FILE_INDEX) {
|
2013-04-13 18:11:54 +00:00
|
|
|
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) {
|
2013-04-27 23:16:38 +00:00
|
|
|
fileIndex = fileManager.createFile(WELCOME_DOCUMENT_TITLE, welcomeContent);
|
2013-04-02 18:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-20 00:14:20 +00:00
|
|
|
synchronizer.refreshManageSync();
|
2013-04-14 13:24:29 +00:00
|
|
|
publisher.notifyPublish();
|
2013-04-02 18:42:47 +00:00
|
|
|
|
2013-05-04 00:05:58 +00:00
|
|
|
// Hide the viewer pencil button
|
|
|
|
if(fileIndex == TEMPORARY_FILE_INDEX) {
|
|
|
|
$(".action-edit-document").removeClass("hide");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$(".action-edit-document").addClass("hide");
|
|
|
|
}
|
|
|
|
|
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() {
|
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-04 00:05:58 +00:00
|
|
|
fileManager.createFile = function(title, content, syncIndexes, isTemporary) {
|
2013-05-14 23:05:02 +00:00
|
|
|
content = content !== undefined ? content : core.settings.defaultContent;
|
2013-04-02 18:42:47 +00:00
|
|
|
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
|
2013-05-04 00:05:58 +00:00
|
|
|
var fileIndex = TEMPORARY_FILE_INDEX;
|
|
|
|
if(!isTemporary) {
|
|
|
|
do {
|
|
|
|
fileIndex = "file." + core.randomString();
|
|
|
|
} 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-05-18 13:40:16 +00:00
|
|
|
// Store syncIndexes associated to the file
|
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-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-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-04-02 18:42:47 +00:00
|
|
|
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(";"));
|
|
|
|
_.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(";"));
|
|
|
|
_.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
|
|
|
|
2013-05-18 13:40:16 +00:00
|
|
|
// Remove the index from the file list
|
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-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-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() {
|
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
|
|
|
}
|
|
|
|
|
2013-04-20 17:40:05 +00:00
|
|
|
synchronizer.resetSyncFlags();
|
2013-05-04 00:05:58 +00:00
|
|
|
function composeTitle(fileIndex, refreshSharing) {
|
2013-04-29 21:41:10 +00:00
|
|
|
var result = [];
|
2013-05-02 00:02:57 +00:00
|
|
|
var syncAttributesList = synchronizer.getSyncAttributesFromFile(fileIndex);
|
|
|
|
var publishAttributesList = publisher.getPublishAttributesFromFile(fileIndex);
|
|
|
|
var attributesList = syncAttributesList.concat(publishAttributesList);
|
2013-05-04 00:05:58 +00:00
|
|
|
if(refreshSharing === true) {
|
|
|
|
sharing.refreshDocumentSharing(attributesList);
|
|
|
|
}
|
2013-05-02 00:02:57 +00:00
|
|
|
_.chain(attributesList).sortBy(function(attributes) {
|
|
|
|
return attributes.provider;
|
|
|
|
}).each(function(attributes) {
|
|
|
|
result.push('<i class="icon-' + attributes.provider + '"></i>');
|
2013-04-20 17:40:05 +00:00
|
|
|
});
|
2013-04-29 21:41:10 +00:00
|
|
|
result.push(" ");
|
|
|
|
result.push(localStorage[fileIndex + ".title"]);
|
|
|
|
return result.join("");
|
2013-04-02 18:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the file title
|
|
|
|
var title = localStorage[fileIndex + ".title"];
|
|
|
|
document.title = "StackEdit - " + title;
|
2013-05-02 00:02:57 +00:00
|
|
|
$("#file-title").html(composeTitle(fileIndex, true));
|
2013-04-02 18:42:47 +00:00
|
|
|
$(".file-title").text(title);
|
|
|
|
$("#file-title-input").val(title);
|
|
|
|
|
|
|
|
// Update the file selector
|
2013-05-19 23:26:05 +00:00
|
|
|
$("#file-selector li:not(.stick)").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-27 12:25:02 +00:00
|
|
|
|
|
|
|
core.layoutRefresh();
|
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-20 00:14:20 +00:00
|
|
|
synchronizer.refreshManageSync();
|
2013-04-02 18:42:47 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-20 00:14:20 +00:00
|
|
|
// Remove sync attributes
|
|
|
|
localStorage.removeItem(syncIndex);
|
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
|
|
|
}
|
|
|
|
}
|
2013-04-20 00:14:20 +00:00
|
|
|
// Remove publish attributes
|
2013-04-11 22:38:41 +00:00
|
|
|
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-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() {
|
|
|
|
var fileIndex = fileManager.createFile();
|
|
|
|
fileManager.selectFile(fileIndex);
|
2013-05-22 21:54:14 +00:00
|
|
|
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();
|
2013-05-22 21:54:14 +00:00
|
|
|
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) {
|
|
|
|
var title = $.trim(input.val());
|
|
|
|
var fileIndexTitle = fileManager.getCurrentFileIndex() + ".title";
|
2013-04-10 18:14:59 +00:00
|
|
|
if (title) {
|
|
|
|
if (title != localStorage[fileIndexTitle]) {
|
|
|
|
localStorage[fileIndexTitle] = title;
|
|
|
|
fileManager.updateFileTitles();
|
|
|
|
fileManager.saveFile();
|
|
|
|
}
|
|
|
|
}
|
2013-04-20 00:14:20 +00:00
|
|
|
input.hide().val(localStorage[fileIndexTitle]);
|
2013-04-10 18:14:59 +00:00
|
|
|
$("#file-title").show();
|
2013-04-27 12:25:02 +00:00
|
|
|
core.layoutRefresh();
|
2013-05-22 21:54:14 +00:00
|
|
|
$("#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();
|
|
|
|
var title = localStorage[fileManager.getCurrentFileIndex() + ".title"];
|
|
|
|
var fileIndex = fileManager.createFile(title, content);
|
|
|
|
fileManager.selectFile(fileIndex);
|
|
|
|
window.location.href = ".";
|
|
|
|
});
|
2013-04-22 23:10:08 +00:00
|
|
|
$(".action-download-md").click(function() {
|
|
|
|
var content = $("#wmd-input").val();
|
|
|
|
var title = localStorage[fileManager.getCurrentFileIndex() + ".title"];
|
|
|
|
core.saveFile(content, title + ".md");
|
|
|
|
});
|
|
|
|
$(".action-download-html").click(function() {
|
|
|
|
var content = $("#wmd-preview").html();
|
|
|
|
var title = localStorage[fileManager.getCurrentFileIndex() + ".title"];
|
|
|
|
core.saveFile(content, title + ".html");
|
|
|
|
});
|
|
|
|
$(".action-download-template").click(function() {
|
|
|
|
var content = publisher.applyTemplate();
|
|
|
|
var title = localStorage[fileManager.getCurrentFileIndex() + ".title"];
|
|
|
|
core.saveFile(content, title + ".txt");
|
|
|
|
});
|
2013-04-27 23:16:38 +00:00
|
|
|
$(".action-welcome-file").click(function() {
|
|
|
|
var fileIndex = fileManager.createFile(WELCOME_DOCUMENT_TITLE, welcomeContent);
|
|
|
|
fileManager.selectFile(fileIndex);
|
|
|
|
});
|
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-04-02 18:42:47 +00:00
|
|
|
return fileManager;
|
|
|
|
});
|