/*globals Markdown, requirejs */ define([ "jquery", "underscore", "crel", "ace", "constants", "utils", "storage", "settings", "eventMgr", "shortcutMgr", "mousetrap", "text!html/bodyIndex.html", "text!html/bodyViewer.html", "text!html/settingsTemplateTooltip.html", "text!html/settingsUserCustomExtensionTooltip.html", "storage", "uilayout", 'pagedown-ace', 'pagedown-light', 'libs/ace_mode', 'ace/requirejs/text!ace/css/editor.css', 'ace/requirejs/text!ace/theme/textmate.css', 'ace/ext/spellcheck', 'ace/ext/searchbox' ], function($, _, crel, ace, constants, utils, storage, settings, eventMgr, shortcutMgr, mousetrap, bodyIndexHTML, bodyViewerHTML, settingsTemplateTooltipHTML, settingsUserCustomExtensionTooltipHTML) { var core = {}; // Used for periodic tasks var intervalId; // Used to detect user activity var isUserReal = false; var userActive = false; var windowUnique = true; var userLastActivity = 0; function setUserActive() { isUserReal = true; userActive = true; var currentTime = utils.currentTime; if(currentTime > userLastActivity + 1000) { userLastActivity = currentTime; eventMgr.onUserActive(); } } function isUserActive() { if(utils.currentTime - userLastActivity > constants.USER_IDLE_THRESHOLD) { userActive = false; } return userActive && windowUnique; } // Used to only have 1 window of the application in the same browser var windowId; function checkWindowUnique() { if(isUserReal === false || windowUnique === false) { return; } if(windowId === undefined) { windowId = utils.randomString(); storage.frontWindowId = windowId; } var frontWindowId = storage.frontWindowId; if(frontWindowId != windowId) { windowUnique = false; if(intervalId !== undefined) { clearInterval(intervalId); } $(".modal").modal("hide"); $('.modal-non-unique').modal("show"); } } // Offline management var isOffline = false; var offlineTime = utils.currentTime; core.setOffline = function() { offlineTime = utils.currentTime; if(isOffline === false) { isOffline = true; eventMgr.onOfflineChanged(true); } }; function setOnline() { if(isOffline === true) { isOffline = false; eventMgr.onOfflineChanged(false); } } function checkOnline() { // Try to reconnect if we are offline but we have some network if(isOffline === true && navigator.onLine === true && offlineTime + constants.CHECK_ONLINE_PERIOD < utils.currentTime) { offlineTime = utils.currentTime; // Try to download anything to test the connection $.ajax({ url: "//www.google.com/jsapi", timeout: constants.AJAX_TIMEOUT, dataType: "script" }).done(function() { setOnline(); }); } } // Load settings in settings dialog var $themeInputElt; function loadSettings() { // Layout orientation utils.setInputRadio("radio-layout-orientation", settings.layoutOrientation); // Theme utils.setInputValue($themeInputElt, window.theme); $themeInputElt.change(); // Lazy rendering utils.setInputChecked("#input-settings-lazy-rendering", settings.lazyRendering); // Editor font family utils.setInputValue("#input-settings-editor-font-family", settings.editorFontFamily); // Editor font size utils.setInputValue("#input-settings-editor-font-size", settings.editorFontSize); // Max width utils.setInputValue("#input-settings-max-width", settings.maxWidth); // Default content utils.setInputValue("#textarea-settings-default-content", settings.defaultContent); // Mode utils.setInputRadio("radio-settings-mode", storage.mode || '_ace_'); // Commit message utils.setInputValue("#input-settings-publish-commit-msg", settings.commitMsg); // Gdrive full access utils.setInputChecked("#input-settings-gdrive-full-access", settings.gdriveFullAccess); // Template utils.setInputValue("#textarea-settings-publish-template", settings.template); // PDF template utils.setInputValue("#textarea-settings-pdf-template", settings.pdfTemplate); // PDF page size utils.setInputValue("#input-settings-pdf-page-size", settings.pdfPageSize); // SSH proxy utils.setInputValue("#input-settings-ssh-proxy", settings.sshProxy); // Load shortcuts settings shortcutMgr.loadSettings(); // Load extension settings eventMgr.onLoadSettings(); } // Save settings from settings dialog function saveSettings(event) { var newSettings = {}; // Layout orientation newSettings.layoutOrientation = utils.getInputRadio("radio-layout-orientation"); // Theme var theme = utils.getInputValue($themeInputElt); // Lazy Rendering newSettings.lazyRendering = utils.getInputChecked("#input-settings-lazy-rendering"); // Editor font family newSettings.editorFontFamily = utils.getInputTextValue("#input-settings-editor-font-family", event); // Editor font size newSettings.editorFontSize = utils.getInputIntValue("#input-settings-editor-font-size", event, 1, 99); // Max width newSettings.maxWidth = utils.getInputIntValue("#input-settings-max-width", event, 1); // Default content newSettings.defaultContent = utils.getInputValue("#textarea-settings-default-content"); // Mode var mode = utils.getInputRadio("radio-settings-mode"); // Commit message newSettings.commitMsg = utils.getInputTextValue("#input-settings-publish-commit-msg", event); // Gdrive full access newSettings.gdriveFullAccess = utils.getInputChecked("#input-settings-gdrive-full-access"); // Template newSettings.template = utils.getInputTextValue("#textarea-settings-publish-template", event); // PDF template newSettings.pdfTemplate = utils.getInputTextValue("#textarea-settings-pdf-template", event); // PDF page size newSettings.pdfPageSize = utils.getInputValue("#input-settings-pdf-page-size"); // SSH proxy newSettings.sshProxy = utils.checkUrl(utils.getInputTextValue("#input-settings-ssh-proxy", event), true); // Save shortcuts settings shortcutMgr.saveSettings(newSettings); // Save extension settings newSettings.extensionSettings = {}; eventMgr.onSaveSettings(newSettings.extensionSettings, event); if(!event.isPropagationStopped()) { $.extend(settings, newSettings); storage.settings = JSON.stringify(settings); storage.themeV3 = theme; storage.mode = mode; } } // Set the panels visibility var layout; var $menuPanelElt; var $documentPanelElt; function setPanelVisibility(forceHide) { if(forceHide === true || layout.state.north.isClosed) { $menuPanelElt.hide(); $documentPanelElt.hide(); } else { $menuPanelElt.show(); $documentPanelElt.show(); } } // Set the preview button visibility var $previewButtonsElt; function setPreviewButtonsVisibility(forceHide) { if(forceHide === true || layout.state.east.isClosed) { $previewButtonsElt.hide(); } else { $previewButtonsElt.show(); } } // Create ACE editor var aceEditor; function createAceEditor() { aceEditor = ace.edit("wmd-input"); aceEditor.setOption("spellcheck", true); aceEditor.renderer.setShowGutter(false); aceEditor.renderer.setPrintMarginColumn(false); aceEditor.renderer.setPadding(constants.EDITOR_DEFAULT_PADDING); aceEditor.session.setUseWrapMode(true); aceEditor.session.setNewLineMode("unix"); aceEditor.session.setMode("libs/ace_mode"); // Make bold titles... (function(self) { function checkLine(currentLine) { var line = self.lines[currentLine]; if(line.length !== 0) { if(line[0].type.indexOf("markup.heading.multi") === 0) { _.each(self.lines[currentLine - 1], function(previousLineObject) { previousLineObject.type = "markup.heading.prev.multi"; }); } } } function customWorker() { // Duplicate from background_tokenizer.js if(!self.running) { return; } var workerStart = new Date(); var currentLine = self.currentLine; var endLine = -1; var doc = self.doc; while (self.lines[currentLine]) { currentLine++; } var startLine = currentLine; var len = doc.getLength(); var processedLines = 0; self.running = false; while (currentLine < len) { self.$tokenizeRow(currentLine); endLine = currentLine; do { checkLine(currentLine); // benweet currentLine++; } while (self.lines[currentLine]); // only check every 5 lines processedLines++; if((processedLines % 5 === 0) && (new Date() - workerStart) > 20) { self.running = setTimeout(customWorker, 20); // benweet self.currentLine = currentLine; return; } } self.currentLine = currentLine; if(startLine <= endLine) { self.fireUpdateEvent(startLine, endLine); } } self.$worker = function() { self.lines.splice(0, self.lines.length); self.states.splice(0, self.states.length); self.currentLine = 0; customWorker(); }; })(aceEditor.session.bgTokenizer); shortcutMgr.configureAce(aceEditor); eventMgr.onAceCreated(aceEditor); } // Create the layout var $editorButtonsElt; function createLayout() { var layoutGlobalConfig = { closable: true, resizable: false, slidable: false, livePaneResizing: true, enableCursorHotkey: false, resizerDblClickToggle: false, resizeWithWindow: false, north__spacing_open: 1, north__spacing_closed: 1, spacing_open: 35, spacing_closed: 35, togglerLength_open: 60, togglerLength_closed: 60, stateManagement__enabled: false, north__minSize: 49, center__minWidth: 250, center__minHeight: 250, fxSettings: { easing: "easeInOutQuad", duration: 350 }, onopen: function() { setPanelVisibility(); setPreviewButtonsVisibility(); }, onclose_start: function(paneName) { if(paneName == 'north') { setPanelVisibility(true); } else if(paneName == 'east') { setPreviewButtonsVisibility(true); } }, onresize_end: function(paneName) { if(aceEditor !== undefined && paneName == 'center') { aceEditor.resize(); var bottomMargin = (aceEditor.renderer.$size.scrollerHeight - aceEditor.renderer.lineHeight) / 2; bottomMargin < 0 && (bottomMargin = 0); aceEditor.renderer.setScrollMargin(0, bottomMargin, 0, 0); setTimeout(function() { var padding = (aceEditor.renderer.$size.scrollerWidth - settings.maxWidth) / 2; if(padding < constants.EDITOR_DEFAULT_PADDING) { padding = constants.EDITOR_DEFAULT_PADDING; } if(padding !== aceEditor.renderer.$padding) { aceEditor.renderer.setPadding(padding); aceEditor.resize(true); } }, 5); } eventMgr.onLayoutResize(paneName); }, }; eventMgr.onLayoutConfigure(layoutGlobalConfig); if(settings.layoutOrientation == "horizontal") { $(".ui-layout-south").remove(); $(".preview-container").html('