2013-03-24 19:42:15 +00:00
|
|
|
function showError(msg) {
|
|
|
|
alert(msg);
|
|
|
|
}
|
2013-03-24 14:54:26 +00:00
|
|
|
|
2013-03-24 19:42:15 +00:00
|
|
|
var fileManager = (function($) {
|
2013-03-24 14:54:26 +00:00
|
|
|
|
|
|
|
var fileManager = {};
|
|
|
|
|
|
|
|
fileManager.init = function() {
|
2013-03-24 23:21:55 +00:00
|
|
|
fileManager.selectFile();
|
2013-03-24 14:54:26 +00:00
|
|
|
window.setInterval(function() {
|
|
|
|
fileManager.saveFile();
|
|
|
|
}, 5000);
|
2013-03-26 22:41:37 +00:00
|
|
|
$(".action-create-file").click(function() {
|
2013-03-24 23:21:55 +00:00
|
|
|
fileManager.saveFile();
|
|
|
|
fileManager.createFile();
|
|
|
|
fileManager.selectFile();
|
|
|
|
});
|
2013-03-26 22:41:37 +00:00
|
|
|
$(".action-remove-file").click(function() {
|
|
|
|
fileManager.deleteFile();
|
|
|
|
fileManager.selectFile();
|
|
|
|
});
|
2013-03-24 23:21:55 +00:00
|
|
|
$("#file-title").click(function() {
|
|
|
|
$(this).hide();
|
|
|
|
$("#file-title-input").show().focus();
|
|
|
|
});
|
|
|
|
$("#file-title-input").blur(function() {
|
|
|
|
var title = $.trim($(this).val());
|
2013-03-26 23:32:36 +00:00
|
|
|
if (title) {
|
2013-03-25 12:03:22 +00:00
|
|
|
var fileIndex = localStorage["file.current"];
|
2013-03-24 23:21:55 +00:00
|
|
|
localStorage[fileIndex + ".title"] = title;
|
|
|
|
}
|
|
|
|
$(this).hide();
|
|
|
|
$("#file-title").show();
|
2013-03-26 22:41:37 +00:00
|
|
|
fileManager.updateFileDescList();
|
2013-03-25 12:03:22 +00:00
|
|
|
fileManager.updateFileTitleUI();
|
2013-03-24 23:21:55 +00:00
|
|
|
});
|
2013-03-27 20:19:12 +00:00
|
|
|
$(".action-upload-gdrive").click(function() {
|
2013-03-27 22:09:27 +00:00
|
|
|
$(".file-sync-indicator").removeClass("hide");
|
2013-03-27 20:19:12 +00:00
|
|
|
var fileIndex = localStorage["file.current"];
|
|
|
|
var content = localStorage[fileIndex + ".content"];
|
|
|
|
var title = localStorage[fileIndex + ".title"];
|
2013-03-27 22:09:27 +00:00
|
|
|
gdrive.createFile(title, content, function(file) {
|
|
|
|
$(".file-sync-indicator").addClass("hide");
|
|
|
|
console.log(file);
|
|
|
|
});
|
2013-03-27 20:19:12 +00:00
|
|
|
});
|
2013-03-24 14:54:26 +00:00
|
|
|
};
|
2013-03-26 23:32:36 +00:00
|
|
|
|
2013-03-24 23:21:55 +00:00
|
|
|
fileManager.selectFile = function() {
|
|
|
|
// If file system does not exist
|
2013-03-26 23:32:36 +00:00
|
|
|
if (!localStorage["file.count"]) {
|
2013-03-24 23:21:55 +00:00
|
|
|
localStorage.clear();
|
|
|
|
localStorage["file.count"] = 0;
|
|
|
|
}
|
2013-03-26 22:41:37 +00:00
|
|
|
this.updateFileDescList();
|
2013-03-24 23:21:55 +00:00
|
|
|
// If no file create one
|
2013-03-26 23:32:36 +00:00
|
|
|
if (this.fileDescList.length === 0) {
|
2013-03-24 23:21:55 +00:00
|
|
|
this.createFile();
|
2013-03-26 22:41:37 +00:00
|
|
|
this.updateFileDescList();
|
2013-03-24 23:21:55 +00:00
|
|
|
}
|
|
|
|
// If no default file take first one
|
2013-03-26 23:32:36 +00:00
|
|
|
if (!localStorage["file.current"]) {
|
2013-03-26 22:41:37 +00:00
|
|
|
localStorage["file.current"] = this.fileDescList[0].index;
|
2013-03-24 23:21:55 +00:00
|
|
|
}
|
|
|
|
// Update the editor and the file title
|
|
|
|
var fileIndex = localStorage["file.current"];
|
|
|
|
$("#wmd-input").val(localStorage[fileIndex + ".content"]);
|
2013-03-27 15:28:31 +00:00
|
|
|
core.createEditor();
|
2013-03-26 23:32:36 +00:00
|
|
|
this.updateFileTitleUI();
|
2013-03-24 14:54:26 +00:00
|
|
|
};
|
|
|
|
|
2013-03-24 23:21:55 +00:00
|
|
|
fileManager.createFile = function(title) {
|
2013-03-26 23:32:36 +00:00
|
|
|
if (!title) {
|
2013-03-25 23:17:34 +00:00
|
|
|
title = "Filename";
|
2013-03-24 23:21:55 +00:00
|
|
|
}
|
2013-03-26 22:41:37 +00:00
|
|
|
// Find a fileIndex
|
|
|
|
var fileCount = parseInt(localStorage["file.count"]);
|
|
|
|
var i;
|
2013-03-26 23:32:36 +00:00
|
|
|
for (i = 0; i < fileCount; i++) {
|
|
|
|
if (!localStorage["file." + i + ".title"]) {
|
2013-03-26 22:41:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var fileIndex = "file." + i;
|
|
|
|
// Create the file in the localStorag
|
2013-03-24 23:21:55 +00:00
|
|
|
localStorage[fileIndex + ".content"] = "";
|
2013-03-26 22:41:37 +00:00
|
|
|
localStorage[fileIndex + ".title"] = title;
|
2013-03-24 23:21:55 +00:00
|
|
|
localStorage["file.current"] = fileIndex;
|
2013-03-26 23:32:36 +00:00
|
|
|
if (i == fileCount) {
|
2013-03-26 22:41:37 +00:00
|
|
|
localStorage["file.count"] = fileCount + 1;
|
|
|
|
}
|
|
|
|
};
|
2013-03-26 23:32:36 +00:00
|
|
|
|
2013-03-26 22:41:37 +00:00
|
|
|
fileManager.deleteFile = function() {
|
|
|
|
var fileIndex = localStorage["file.current"];
|
|
|
|
localStorage.removeItem("file.current");
|
|
|
|
localStorage.removeItem(fileIndex + ".title");
|
|
|
|
localStorage.removeItem(fileIndex + ".content");
|
2013-03-24 23:21:55 +00:00
|
|
|
};
|
2013-03-26 23:32:36 +00:00
|
|
|
|
2013-03-26 22:41:37 +00:00
|
|
|
fileManager.updateFileDescList = function() {
|
2013-03-24 23:21:55 +00:00
|
|
|
var fileCount = parseInt(localStorage["file.count"]);
|
2013-03-26 22:41:37 +00:00
|
|
|
var lastIndex = -1;
|
|
|
|
this.fileDescList = [];
|
2013-03-24 23:21:55 +00:00
|
|
|
$("#file-selector").empty();
|
2013-03-26 23:32:36 +00:00
|
|
|
for ( var i = 0; i < fileCount; i++) {
|
2013-03-24 23:21:55 +00:00
|
|
|
var fileIndex = "file." + i;
|
|
|
|
var title = localStorage[fileIndex + ".title"];
|
2013-03-26 23:32:36 +00:00
|
|
|
if (title) {
|
2013-03-26 22:41:37 +00:00
|
|
|
lastIndex = i;
|
2013-03-26 23:32:36 +00:00
|
|
|
this.fileDescList
|
|
|
|
.push({ "index" : fileIndex, "title" : title });
|
2013-03-25 12:03:22 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-26 22:41:37 +00:00
|
|
|
localStorage["file.count"] = lastIndex + 1;
|
|
|
|
this.fileDescList.sort(function(a, b) {
|
|
|
|
if (a.title.toLowerCase() < b.title.toLowerCase())
|
|
|
|
return -1;
|
|
|
|
if (a.title.toLowerCase() > b.title.toLowerCase())
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
});
|
2013-03-25 12:03:22 +00:00
|
|
|
};
|
2013-03-26 23:32:36 +00:00
|
|
|
|
|
|
|
fileManager.updateFileTitleUI = function() {
|
|
|
|
// Update the editor and the file title
|
2013-03-25 12:03:22 +00:00
|
|
|
var fileIndex = localStorage["file.current"];
|
|
|
|
var title = localStorage[fileIndex + ".title"];
|
2013-03-26 22:41:37 +00:00
|
|
|
document.title = "StackEdit - " + title;
|
|
|
|
$(".file-title").text(title);
|
2013-03-25 12:03:22 +00:00
|
|
|
$("#file-title-input").val(title);
|
|
|
|
$("#file-selector").empty();
|
2013-03-26 23:32:36 +00:00
|
|
|
|
|
|
|
for ( var i = 0; i < this.fileDescList.length; i++) {
|
2013-03-26 22:41:37 +00:00
|
|
|
var fileDesc = this.fileDescList[i];
|
|
|
|
var a = $("<a>").text(fileDesc.title);
|
|
|
|
var li = $("<li>").append(a);
|
2013-03-26 23:32:36 +00:00
|
|
|
if (fileDesc.index == fileIndex) {
|
2013-03-26 22:41:37 +00:00
|
|
|
li.addClass("disabled");
|
2013-03-26 23:32:36 +00:00
|
|
|
} else {
|
|
|
|
a.attr("href", "javascript:void(0);").click(
|
|
|
|
(function(fileIndex) {
|
|
|
|
return function() {
|
|
|
|
localStorage["file.current"] = fileIndex;
|
|
|
|
fileManager.selectFile();
|
|
|
|
};
|
|
|
|
})(fileDesc.index));
|
2013-03-26 22:41:37 +00:00
|
|
|
}
|
|
|
|
$("#file-selector").append(li);
|
2013-03-24 23:21:55 +00:00
|
|
|
}
|
2013-03-26 23:32:36 +00:00
|
|
|
};
|
2013-03-24 14:54:26 +00:00
|
|
|
|
|
|
|
fileManager.saveFile = function() {
|
2013-03-24 23:21:55 +00:00
|
|
|
var content = $("#wmd-input").val();
|
|
|
|
var fileIndex = localStorage["file.current"];
|
|
|
|
localStorage[fileIndex + ".content"] = content;
|
2013-03-26 23:32:36 +00:00
|
|
|
// insertFile(this.currentFile, this.content);
|
2013-03-24 19:42:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return fileManager;
|
|
|
|
})(jQuery);
|
|
|
|
|
2013-03-27 15:28:31 +00:00
|
|
|
var core = (function($) {
|
|
|
|
var core = {};
|
|
|
|
|
|
|
|
core.init = function() {
|
|
|
|
this.loadSettings();
|
|
|
|
this.saveSettings();
|
|
|
|
this.createLayout();
|
|
|
|
|
|
|
|
$(".action-apply-settings").click(function() {
|
|
|
|
core.saveSettings();
|
|
|
|
fileManager.saveFile();
|
|
|
|
location.reload();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var settings = {};
|
|
|
|
core.loadSettings = function() {
|
|
|
|
if(localStorage.settings) {
|
|
|
|
settings = JSON.parse(localStorage.settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Layout orientation
|
|
|
|
$("#radio-layout-orientation-horizontal").attr('checked', true);
|
|
|
|
if(settings.layoutOrientation == "vertical") {
|
|
|
|
$("#radio-layout-orientation-vertical").attr('checked', true);
|
|
|
|
}
|
|
|
|
};
|
2013-03-26 23:32:36 +00:00
|
|
|
|
2013-03-27 15:28:31 +00:00
|
|
|
core.saveSettings = function() {
|
|
|
|
|
|
|
|
// Layout orientation
|
|
|
|
settings.layoutOrientation = "horizontal";
|
|
|
|
if($("#radio-layout-orientation-vertical").is(":checked")) {
|
|
|
|
settings.layoutOrientation = "vertical";
|
|
|
|
}
|
|
|
|
|
|
|
|
localStorage.settings = JSON.stringify(settings);
|
|
|
|
};
|
2013-03-26 22:41:37 +00:00
|
|
|
|
2013-03-27 15:28:31 +00:00
|
|
|
core.createLayout = function() {
|
|
|
|
var layout = undefined;
|
|
|
|
var layoutGlobalConfig = { closable : true, resizable : false,
|
|
|
|
slidable : false, livePaneResizing : true, spacing_open : 20,
|
|
|
|
spacing_closed : 20, togglerLength_open : 90,
|
|
|
|
togglerLength_closed : 90, center__minWidth : 100, center__minHeight : 100,
|
|
|
|
stateManagement__enabled : false, };
|
|
|
|
if (settings.layoutOrientation == "horizontal") {
|
|
|
|
$(".ui-layout-east").addClass("well").attr("id", "wmd-preview");
|
|
|
|
layout = $('body').layout(
|
|
|
|
$.extend(layoutGlobalConfig,
|
|
|
|
{ east__resizable : true, east__size : .5, east__minSize : 200,
|
|
|
|
south__closable : false, }));
|
|
|
|
} else if (settings.layoutOrientation == "vertical") {
|
|
|
|
$(".ui-layout-east").remove();
|
|
|
|
$(".ui-layout-south").addClass("well").attr("id", "wmd-preview");
|
|
|
|
layout = $('body').layout(
|
|
|
|
$.extend(layoutGlobalConfig, { south__resizable : true,
|
|
|
|
south__size : .5, south__minSize : 200, }));
|
|
|
|
}
|
|
|
|
$(".ui-layout-toggler-north").addClass("btn").append(
|
|
|
|
$("<b>").addClass("caret"));
|
|
|
|
$(".ui-layout-toggler-south").addClass("btn").append(
|
|
|
|
$("<b>").addClass("caret"));
|
|
|
|
$(".ui-layout-toggler-east").addClass("btn").append(
|
|
|
|
$("<b>").addClass("caret"));
|
|
|
|
$("#navbar").click(function() {
|
|
|
|
layout.allowOverflow('north');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
core.createEditor = function() {
|
|
|
|
$("#wmd-button-bar").empty();
|
|
|
|
var converter = Markdown.getSanitizingConverter();
|
|
|
|
var editor = new Markdown.Editor(converter);
|
|
|
|
editor.run();
|
|
|
|
|
|
|
|
$(".wmd-button-row").addClass("btn-group").find("li:not(.wmd-spacer)")
|
|
|
|
.addClass("btn").css("left", 0).find("span").hide();
|
|
|
|
$("#wmd-bold-button").append($("<i>").addClass("icon-bold"));
|
|
|
|
$("#wmd-italic-button").append($("<i>").addClass("icon-italic"));
|
|
|
|
$("#wmd-link-button").append($("<i>").addClass("icon-globe"));
|
|
|
|
$("#wmd-quote-button").append($("<i>").addClass("icon-indent-left"));
|
|
|
|
$("#wmd-code-button").append($("<i>").addClass("icon-code"));
|
|
|
|
$("#wmd-image-button").append($("<i>").addClass("icon-picture"));
|
|
|
|
$("#wmd-olist-button").append($("<i>").addClass("icon-numbered-list"));
|
|
|
|
$("#wmd-ulist-button").append($("<i>").addClass("icon-list"));
|
|
|
|
$("#wmd-heading-button").append($("<i>").addClass("icon-text-height"));
|
|
|
|
$("#wmd-hr-button").append($("<i>").addClass("icon-hr"));
|
|
|
|
$("#wmd-undo-button").append($("<i>").addClass("icon-undo"));
|
|
|
|
$("#wmd-redo-button").append($("<i>").addClass("icon-share-alt"));
|
|
|
|
};
|
|
|
|
|
|
|
|
return core;
|
|
|
|
})(jQuery);
|
2013-03-25 12:03:22 +00:00
|
|
|
|
2013-03-24 19:42:15 +00:00
|
|
|
(function($) {
|
|
|
|
|
|
|
|
$(function() {
|
2013-03-25 12:03:22 +00:00
|
|
|
|
2013-03-27 15:28:31 +00:00
|
|
|
core.init();
|
2013-03-24 19:42:15 +00:00
|
|
|
if (typeof (Storage) !== "undefined") {
|
|
|
|
fileManager.init();
|
|
|
|
} else {
|
|
|
|
showError("Web storage is not available");
|
2013-03-26 22:41:37 +00:00
|
|
|
}
|
2013-03-24 19:42:15 +00:00
|
|
|
});
|
|
|
|
|
2013-03-24 14:54:26 +00:00
|
|
|
})(jQuery);
|