define(["jquery", "bootstrap", "jgrowl", "layout", "Markdown.Editor"], function($) { var core = {}; // Time shared by others modules core.currentTime = new Date().getTime(); core.updateCurrentTime = function() { core.currentTime = new Date().getTime(); }; // Usage: callback = callback || core.doNothing; core.doNothing = function() {}; // Useful function core.getInputValue = function(element, event) { var value = element.val(); if (value !== undefined) { value = value.replace(/^\s+|\s+$/g, ''); element.val(undefined); } if (value === undefined || value.length === 0) { element.stop(true, true).addClass("error").delay(400) .switchClass("error"); if(event !== undefined) { event.stopPropagation(); } return undefined; } return value; }; // Used by asyncTaskRunner core.showWorkingIndicator = function(show) { if (show === false) { $(".working-indicator").addClass("hide"); $("body").removeClass("working"); } else { $(".working-indicator").removeClass("hide"); $("body").addClass("working"); } }; // Used to show a notification message core.showMessage = function(msg, iconClass, options) { options = options || {}; iconClass = iconClass || "icon-info-sign"; $.jGrowl(" " + msg, options); }; // Used to show an error message core.showError = function(msg) { core.showMessage(msg, "icon-warning-sign"); }; // Offline management core.isOffline = false; var offlineTime = core.currentTime; var offlineListeners = []; core.addOfflineListener = function(listener) { offlineListeners.push(listener); }; core.setOffline = function() { offlineTime = core.currentTime; if(core.isOffline === false) { core.isOffline = true; core.showMessage("You are offline.", "icon-exclamation-sign msg-offline", { sticky : true, close : function() { core.showMessage("You are back online!", "icon-signal"); } }); for(var i=0; i").addClass("caret")); $(".ui-layout-toggler-south").addClass("btn").append( $("").addClass("caret")); $(".ui-layout-toggler-east").addClass("btn").append( $("").addClass("caret")); $("#navbar").click(function() { layout.allowOverflow('north'); }); }; // Create the PageDown editor var insertLinkCallback = undefined; core.createEditor = function(onTextChange) { $("#wmd-button-bar").empty(); var converter = Markdown.getSanitizingConverter(); var firstChange = true; converter.hooks.chain("preConversion", function(text) { if (!firstChange) { onTextChange(); } return text; }); var editor = new Markdown.Editor(converter); editor.hooks.set("insertLinkDialog", function (callback) { insertLinkCallback = callback; $("#modal-insert-link").modal('show'); return true; }); editor.hooks.set("insertImageDialog", function (callback) { insertLinkCallback = callback; $("#modal-insert-image").modal('show'); return true; }); editor.run(); firstChange = false; $(".wmd-button-row").addClass("btn-group").find("li:not(.wmd-spacer)") .addClass("btn").css("left", 0).find("span").hide(); $("#wmd-bold-button").append($("").addClass("icon-bold")); $("#wmd-italic-button").append($("").addClass("icon-italic")); $("#wmd-link-button").append($("").addClass("icon-globe")); $("#wmd-quote-button").append($("").addClass("icon-indent-left")); $("#wmd-code-button").append($("").addClass("icon-code")); $("#wmd-image-button").append($("").addClass("icon-picture")); $("#wmd-olist-button").append($("").addClass("icon-numbered-list")); $("#wmd-ulist-button").append($("").addClass("icon-list")); $("#wmd-heading-button").append($("").addClass("icon-text-height")); $("#wmd-hr-button").append($("").addClass("icon-hr")); $("#wmd-undo-button").append($("").addClass("icon-undo")); $("#wmd-redo-button").append($("").addClass("icon-share-alt")); }; // Base64 conversion core.encodeBase64 = function(str) { if (str.length === 0) { return ""; } // UTF-8 to byte array var bytes = [], offset = 0, length, char; str = encodeURI(str); length = str.length; while (offset < length) { char = str[offset]; offset += 1; if ('%' !== char) { bytes.push(char.charCodeAt(0)); } else { char = str[offset] + str[offset + 1]; bytes.push(parseInt(char, 16)); offset += 2; } } // byte array to base64 var padchar = '='; var alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; var i, b10; var x = []; var imax = bytes.length - bytes.length % 3; for (i = 0; i < imax; i += 3) { b10 = (bytes[i] << 16) | (bytes[i+1] << 8) | bytes[i+2]; x.push(alpha.charAt(b10 >> 18)); x.push(alpha.charAt((b10 >> 12) & 0x3F)); x.push(alpha.charAt((b10 >> 6) & 0x3f)); x.push(alpha.charAt(b10 & 0x3f)); } switch (bytes.length - imax) { case 1: b10 = bytes[i] << 16; x.push(alpha.charAt(b10 >> 18) + alpha.charAt((b10 >> 12) & 0x3F) + padchar + padchar); break; case 2: b10 = (bytes[i] << 16) | (bytes[i+1] << 8); x.push(alpha.charAt(b10 >> 18) + alpha.charAt((b10 >> 12) & 0x3F) + alpha.charAt((b10 >> 6) & 0x3f) + padchar); break; } return x.join(''); }; core.init = function() { // jGrowl configuration $.jGrowl.defaults.life = 5000; $.jGrowl.defaults.closer = false; $.jGrowl.defaults.closeTemplate = ''; $.jGrowl.defaults.position = 'bottom-right'; // listen to online/offline events $(window).on('offline', core.setOffline); $(window).on('online', core.setOnline); if (navigator.onLine === false) { core.setOffline(); } // Avoid dropdown to close when clicking on submenu $('.dropdown-submenu > a').click(function(e) { e.stopPropagation(); }); // Click events on "insert link" and "insert image" dialog buttons $(".action-insert-link").click(function(e) { var value = core.getInputValue($("#input-insert-link"), e); if(value !== undefined) { insertLinkCallback(value); } }); $(".action-insert-image").click(function(e) { var value = core.getInputValue($("#input-insert-image"), e); if(value !== undefined) { insertLinkCallback(value); } }); $(".action-close-insert-link").click(function(e) { insertLinkCallback(null); }); $("#menu-bar, .ui-layout-center, .ui-layout-east, .ui-layout-south").removeClass("hide"); this.loadSettings(); this.createLayout(); $(".action-load-settings").click(function() { core.loadSettings(); }); $(".action-apply-settings").click(function() { core.saveSettings(); location.reload(); }); }; return core; });