');
$elt.find('a').click(performPayment);
modalBodyElt.insertBefore($elt[0], modalBodyElt.firstChild);
$alerts = $alerts.add($elt);
});
}
});
}, 1000);
eventMgr.addListener('onOfflineChanged', checkPayment);
// Other initialization that are not prioritary
eventMgr.addListener("onReady", function() {
$('.modal').on('shown.bs.modal', function() {
var $elt = $(this);
setTimeout(function() {
// When modal opens focus on the first button
$elt.find('.btn:first').focus();
// Or on the first link if any
$elt.find('button:first').focus();
// Or on the first input if any
$elt.find("input:enabled:visible:first").focus();
}, 50);
}).on('hidden.bs.modal', function() {
// Focus on the editor when modal is gone
editor.focus();
// Revert to current theme when settings modal is closed
applyTheme(window.theme);
}).keyup(function(e) {
// Handle enter key in modals
if(e.which == 13 && !$(e.target).is("textarea")) {
$(this).find(".modal-footer a:last").click();
}
});
// Click events on "insert link" and "insert image" dialog buttons
$(".action-insert-link").click(function(e) {
var value = utils.getInputTextValue($("#input-insert-link"), e);
if(value !== undefined) {
core.insertLinkCallback(value);
core.insertLinkCallback = undefined;
}
});
$(".action-insert-image").click(function(e) {
var value = utils.getInputTextValue($("#input-insert-image"), e);
if(value !== undefined) {
core.insertLinkCallback(value);
core.insertLinkCallback = undefined;
}
});
// Hide events on "insert link" and "insert image" dialogs
$(".modal-insert-link, .modal-insert-image").on('hidden.bs.modal', function() {
if(core.insertLinkCallback !== undefined) {
core.insertLinkCallback(null);
core.insertLinkCallback = undefined;
}
});
// Settings loading/saving
$(".action-load-settings").click(function() {
loadSettings();
});
$(".action-apply-settings").click(function(e) {
saveSettings(e);
if(!e.isPropagationStopped()) {
window.location.reload();
}
});
$('.action-add-google-drive-account').click(function() {
if(settings.gdriveMultiAccount === 3) {
return;
}
settings.gdriveMultiAccount++;
storage.settings = JSON.stringify(settings);
window.location.reload();
});
// Hot theme switcher in the settings
var currentTheme = window.theme;
function applyTheme(theme) {
theme = theme || 'default';
if(currentTheme != theme) {
var themeModule = "less!themes/" + theme;
if(window.baseDir.indexOf('-min') !== -1) {
themeModule = "css!themes/" + theme;
}
// Undefine the module in RequireJS
requirejs.undef(themeModule);
// Then reload the style
require([
themeModule
]);
currentTheme = theme;
}
}
$themeInputElt = $("#input-settings-theme");
$themeInputElt.on("change", function() {
applyTheme(this.value);
});
// Import docs and settings
$(".action-import-docs-settings").click(function() {
$("#input-file-import-docs-settings").click();
});
var newstorage;
$("#input-file-import-docs-settings").change(function(evt) {
var files = (evt.dataTransfer || evt.target).files;
$(".modal-settings").modal("hide");
_.each(files, function(file) {
var reader = new FileReader();
reader.onload = (function(importedFile) {
return function(e) {
try {
newstorage = JSON.parse(e.target.result);
// Compare storage version
var newVersion = parseInt(newstorage.version.match(/^v(\d+)$/)[1], 10);
var currentVersion = parseInt(storage.version.match(/^v(\d+)$/)[1], 10);
if(newVersion > currentVersion) {
// We manage storage upgrade, not downgrade
eventMgr.onError("Incompatible version. Please upgrade StackEdit.");
} else {
$('.modal-import-docs-settings').modal('show');
}
}
catch(exc) {
eventMgr.onError("Wrong format: " + importedFile.name);
}
$("#input-file-import-docs-settings").val('');
};
})(file);
reader.readAsText(file);
});
});
$(".action-import-docs-settings-confirm").click(function() {
storage.clear();
var allowedKeys = /^file\.|^folder\.|^publish\.|^settings$|^sync\.|^google\.|^author\.|^themeV4$|^version$/;
_.each(newstorage, function(value, key) {
//if(allowedKeys.test(key)) {
storage[key] = value;
//}
});
window.location.reload();
});
// Export settings
$(".action-export-docs-settings").click(function() {
utils.saveAs(JSON.stringify(storage), "StackEdit local storage.json");
});
$(".action-default-settings").click(function() {
storage.removeItem("settings");
storage.removeItem("theme");
if(!settings.dropboxFullAccess) {
storage.removeItem('dropbox.lastChangeId');
}
window.location.reload();
});
$(".action-app-reset").click(function() {
storage.clear();
window.location.reload();
});
// Reset inputs
$(".action-reset-input").click(function() {
utils.resetModalInputs();
});
utils.createTooltip(".tooltip-lazy-rendering", 'Disable preview rendering while typing in order to offload CPU. Refresh preview after 500 ms of inactivity.');
utils.createTooltip(".tooltip-default-content", [
'Thanks for supporting StackEdit by adding a backlink in your documents!