2013-11-07 23:10:38 +00:00
/*globals Markdown, requirejs */
2013-05-26 22:59:03 +00:00
define ( [
"jquery" ,
2013-05-29 19:55:23 +00:00
"underscore" ,
2013-08-08 23:32:03 +00:00
"crel" ,
2014-03-18 01:10:22 +00:00
"editor" ,
2014-04-14 00:21:06 +00:00
"layout" ,
2013-11-05 23:03:38 +00:00
"constants" ,
2013-05-26 22:59:03 +00:00
"utils" ,
2013-11-05 23:03:38 +00:00
"storage" ,
2013-05-27 19:45:33 +00:00
"settings" ,
2013-07-30 08:46:36 +00:00
"eventMgr" ,
2013-08-04 15:49:14 +00:00
"text!html/bodyIndex.html" ,
"text!html/bodyViewer.html" ,
2013-06-10 21:22:32 +00:00
"text!html/settingsTemplateTooltip.html" ,
2014-04-17 23:41:35 +00:00
"text!html/settingsShortcutsExtensionTooltip.html" ,
2013-07-07 20:07:11 +00:00
"text!html/settingsUserCustomExtensionTooltip.html" ,
2013-05-26 22:59:03 +00:00
"storage" ,
2014-03-20 00:24:56 +00:00
'pagedown' ,
2014-04-17 23:41:35 +00:00
] , function ( $ , _ , crel , editor , layout , constants , utils , storage , settings , eventMgr , bodyIndexHTML , bodyViewerHTML , settingsTemplateTooltipHTML , settingsShortcutsExtensionTooltipHTML , settingsUserCustomExtensionTooltipHTML ) {
2013-04-02 18:42:47 +00:00
2013-05-29 19:55:23 +00:00
var core = { } ;
2014-03-03 23:30:55 +00:00
2013-05-29 19:55:23 +00:00
// Used for periodic tasks
2013-11-07 23:10:38 +00:00
var intervalId ;
2013-05-29 19:55:23 +00:00
// Used to detect user activity
2013-08-04 00:53:46 +00:00
var isUserReal = false ;
2013-05-29 19:55:23 +00:00
var userActive = false ;
var windowUnique = true ;
var userLastActivity = 0 ;
function setUserActive ( ) {
2013-08-04 00:53:46 +00:00
isUserReal = true ;
2013-05-29 19:55:23 +00:00
userActive = true ;
2013-08-04 00:53:46 +00:00
var currentTime = utils . currentTime ;
if ( currentTime > userLastActivity + 1000 ) {
userLastActivity = currentTime ;
eventMgr . onUserActive ( ) ;
}
2013-05-29 19:55:23 +00:00
}
function isUserActive ( ) {
2013-11-05 23:03:38 +00:00
if ( utils . currentTime - userLastActivity > constants . USER _IDLE _THRESHOLD ) {
2013-05-29 19:55:23 +00:00
userActive = false ;
}
return userActive && windowUnique ;
}
2014-03-03 23:30:55 +00:00
2013-05-29 19:55:23 +00:00
// Used to only have 1 window of the application in the same browser
2013-11-07 23:10:38 +00:00
var windowId ;
2013-05-29 19:55:23 +00:00
function checkWindowUnique ( ) {
2013-08-04 00:53:46 +00:00
if ( isUserReal === false || windowUnique === false ) {
2013-05-29 19:55:23 +00:00
return ;
}
if ( windowId === undefined ) {
windowId = utils . randomString ( ) ;
2013-11-05 23:03:38 +00:00
storage . frontWindowId = windowId ;
2013-05-29 19:55:23 +00:00
}
2013-11-05 23:03:38 +00:00
var frontWindowId = storage . frontWindowId ;
2013-05-29 19:55:23 +00:00
if ( frontWindowId != windowId ) {
windowUnique = false ;
if ( intervalId !== undefined ) {
clearInterval ( intervalId ) ;
}
$ ( ".modal" ) . modal ( "hide" ) ;
2013-09-19 21:54:50 +00:00
$ ( '.modal-non-unique' ) . modal ( "show" ) ;
2013-12-14 22:44:59 +00:00
// Attempt to close the window
window . close ( ) ;
2013-05-29 19:55:23 +00:00
}
}
// Offline management
2013-08-04 00:53:46 +00:00
var isOffline = false ;
2013-05-29 19:55:23 +00:00
var offlineTime = utils . currentTime ;
core . setOffline = function ( ) {
offlineTime = utils . currentTime ;
2013-08-04 00:53:46 +00:00
if ( isOffline === false ) {
isOffline = true ;
2013-07-30 08:46:36 +00:00
eventMgr . onOfflineChanged ( true ) ;
2013-05-29 19:55:23 +00:00
}
} ;
function setOnline ( ) {
2013-08-04 00:53:46 +00:00
if ( isOffline === true ) {
isOffline = false ;
2013-07-30 08:46:36 +00:00
eventMgr . onOfflineChanged ( false ) ;
2013-05-29 19:55:23 +00:00
}
}
function checkOnline ( ) {
// Try to reconnect if we are offline but we have some network
2013-11-05 23:03:38 +00:00
if ( isOffline === true && navigator . onLine === true && offlineTime + constants . CHECK _ONLINE _PERIOD < utils . currentTime ) {
2013-05-29 19:55:23 +00:00
offlineTime = utils . currentTime ;
// Try to download anything to test the connection
$ . ajax ( {
url : "//www.google.com/jsapi" ,
2013-11-05 23:03:38 +00:00
timeout : constants . AJAX _TIMEOUT ,
2013-05-29 19:55:23 +00:00
dataType : "script"
} ) . done ( function ( ) {
setOnline ( ) ;
} ) ;
}
}
// Load settings in settings dialog
2013-11-07 23:10:38 +00:00
var $themeInputElt ;
2013-05-29 19:55:23 +00:00
function loadSettings ( ) {
// Layout orientation
utils . setInputRadio ( "radio-layout-orientation" , settings . layoutOrientation ) ;
// Theme
2013-11-07 23:10:38 +00:00
utils . setInputValue ( $themeInputElt , window . theme ) ;
2013-08-20 22:40:19 +00:00
$themeInputElt . change ( ) ;
2013-05-29 19:55:23 +00:00
// Lazy rendering
utils . setInputChecked ( "#input-settings-lazy-rendering" , settings . lazyRendering ) ;
2014-04-15 23:16:08 +00:00
// Editor font class
utils . setInputRadio ( "radio-settings-editor-font-class" , settings . editorFontClass ) ;
2014-04-21 01:12:57 +00:00
// Font size ratio
utils . setInputValue ( "#input-settings-font-size" , settings . fontSizeRatio ) ;
// Max width ratio
utils . setInputValue ( "#input-settings-max-width" , settings . maxWidthRatio ) ;
2013-05-29 19:55:23 +00:00
// Default content
utils . setInputValue ( "#textarea-settings-default-content" , settings . defaultContent ) ;
2014-03-20 00:24:56 +00:00
// Edit mode
2014-04-17 23:41:35 +00:00
utils . setInputRadio ( "radio-settings-edit-mode" , settings . editMode ) ;
2013-05-29 19:55:23 +00:00
// Commit message
utils . setInputValue ( "#input-settings-publish-commit-msg" , settings . commitMsg ) ;
2013-12-23 22:41:33 +00:00
// Gdrive multi-accounts
utils . setInputValue ( "#input-settings-gdrive-multiaccount" , settings . gdriveMultiAccount ) ;
2013-10-06 14:34:40 +00:00
// Gdrive full access
utils . setInputChecked ( "#input-settings-gdrive-full-access" , settings . gdriveFullAccess ) ;
2013-12-23 22:41:33 +00:00
// Dropbox full access
utils . setInputChecked ( "#input-settings-dropbox-full-access" , settings . dropboxFullAccess ) ;
2013-12-27 21:12:04 +00:00
// GitHub full access
utils . setInputChecked ( "#input-settings-github-full-access" , settings . githubFullAccess ) ;
2013-05-29 19:55:23 +00:00
// Template
utils . setInputValue ( "#textarea-settings-publish-template" , settings . template ) ;
2013-09-23 22:12:35 +00:00
// PDF template
2013-10-27 19:19:56 +00:00
utils . setInputValue ( "#textarea-settings-pdf-template" , settings . pdfTemplate ) ;
// PDF page size
utils . setInputValue ( "#input-settings-pdf-page-size" , settings . pdfPageSize ) ;
2013-05-29 19:55:23 +00:00
// SSH proxy
utils . setInputValue ( "#input-settings-ssh-proxy" , settings . sshProxy ) ;
2014-03-03 23:30:55 +00:00
2013-05-29 19:55:23 +00:00
// Load extension settings
2013-07-30 08:46:36 +00:00
eventMgr . onLoadSettings ( ) ;
2013-05-29 19:55:23 +00:00
}
// Save settings from settings dialog
function saveSettings ( event ) {
var newSettings = { } ;
// Layout orientation
newSettings . layoutOrientation = utils . getInputRadio ( "radio-layout-orientation" ) ;
// Theme
2013-08-20 22:40:19 +00:00
var theme = utils . getInputValue ( $themeInputElt ) ;
2013-05-29 19:55:23 +00:00
// Lazy Rendering
newSettings . lazyRendering = utils . getInputChecked ( "#input-settings-lazy-rendering" ) ;
2014-04-15 23:16:08 +00:00
// Editor font class
newSettings . editorFontClass = utils . getInputRadio ( "radio-settings-editor-font-class" ) ;
2014-04-21 01:12:57 +00:00
// Font size ratio
newSettings . fontSizeRatio = utils . getInputFloatValue ( "#input-settings-font-size" , event , 0.1 , 10 ) ;
// Max width ratio
newSettings . maxWidthRatio = utils . getInputFloatValue ( "#input-settings-max-width" , event , 0.1 , 10 ) ;
2013-05-29 19:55:23 +00:00
// Default content
newSettings . defaultContent = utils . getInputValue ( "#textarea-settings-default-content" ) ;
2014-03-20 00:24:56 +00:00
// Edit mode
2014-04-17 23:41:35 +00:00
newSettings . editMode = utils . getInputRadio ( "radio-settings-edit-mode" ) ;
2013-05-29 19:55:23 +00:00
// Commit message
newSettings . commitMsg = utils . getInputTextValue ( "#input-settings-publish-commit-msg" , event ) ;
2013-12-23 22:41:33 +00:00
// Gdrive multi-accounts
newSettings . gdriveMultiAccount = utils . getInputIntValue ( "#input-settings-gdrive-multiaccount" ) ;
2013-10-06 14:34:40 +00:00
// Gdrive full access
newSettings . gdriveFullAccess = utils . getInputChecked ( "#input-settings-gdrive-full-access" ) ;
2013-12-23 22:41:33 +00:00
// Drobox full access
newSettings . dropboxFullAccess = utils . getInputChecked ( "#input-settings-dropbox-full-access" ) ;
2013-12-27 21:12:04 +00:00
// GitHub full access
newSettings . githubFullAccess = utils . getInputChecked ( "#input-settings-github-full-access" ) ;
2013-05-29 19:55:23 +00:00
// Template
newSettings . template = utils . getInputTextValue ( "#textarea-settings-publish-template" , event ) ;
2013-09-23 22:12:35 +00:00
// PDF template
2013-10-27 19:19:56 +00:00
newSettings . pdfTemplate = utils . getInputTextValue ( "#textarea-settings-pdf-template" , event ) ;
// PDF page size
newSettings . pdfPageSize = utils . getInputValue ( "#input-settings-pdf-page-size" ) ;
2013-05-29 19:55:23 +00:00
// SSH proxy
newSettings . sshProxy = utils . checkUrl ( utils . getInputTextValue ( "#input-settings-ssh-proxy" , event ) , true ) ;
// Save extension settings
newSettings . extensionSettings = { } ;
2013-07-30 08:46:36 +00:00
eventMgr . onSaveSettings ( newSettings . extensionSettings , event ) ;
2013-05-29 19:55:23 +00:00
if ( ! event . isPropagationStopped ( ) ) {
2013-12-23 22:41:33 +00:00
if ( settings . dropboxFullAccess !== newSettings . dropboxFullAccess ) {
storage . removeItem ( 'dropbox.lastChangeId' ) ;
}
2013-05-29 19:55:23 +00:00
$ . extend ( settings , newSettings ) ;
2013-11-05 23:03:38 +00:00
storage . settings = JSON . stringify ( settings ) ;
2013-12-02 23:29:48 +00:00
storage . themeV3 = theme ;
2013-05-29 19:55:23 +00:00
}
}
2014-04-17 23:41:35 +00:00
/ *
2013-12-01 15:43:46 +00:00
var $navbarElt ;
var $leftBtnElts ;
var $rightBtnElts ;
2014-03-10 23:53:03 +00:00
var $btnDropdown ;
var $titleContainer ;
var marginWidth = 18 * 2 + 25 + 25 ;
2013-12-05 20:59:57 +00:00
var titleWidth = 18 + 348 ;
2014-04-07 23:19:47 +00:00
var leftButtonsWidth = 18 * 5 + 80 + 160 + 160 + 40 + 80 ;
2014-03-12 00:53:15 +00:00
var rightButtonsWidth = 18 + 80 ;
2014-03-10 23:53:03 +00:00
var buttonsDropdownWidth = 40 ;
2013-12-01 15:43:46 +00:00
function adjustWindow ( ) {
2013-12-02 00:09:39 +00:00
if ( ! window . viewerMode ) {
2014-03-10 23:53:03 +00:00
var maxWidth = $navbarElt . width ( ) ;
2013-12-02 00:09:39 +00:00
if ( marginWidth + titleWidth + leftButtonsWidth + rightButtonsWidth > maxWidth ) {
2014-03-10 23:53:03 +00:00
$btnDropdown . show ( ) . find ( '.dropdown-menu' ) . append ( $leftBtnElts ) ;
if ( marginWidth + titleWidth + rightButtonsWidth + buttonsDropdownWidth > maxWidth ) {
$btnDropdown . find ( '.dropdown-menu' ) . append ( $rightBtnElts ) ;
2013-12-02 00:09:39 +00:00
}
else {
2014-03-10 23:53:03 +00:00
$titleContainer . before ( $rightBtnElts ) ;
2013-12-02 00:09:39 +00:00
}
2013-12-01 15:43:46 +00:00
}
else {
2014-03-10 23:53:03 +00:00
$btnDropdown . hide ( ) . after ( $leftBtnElts ) ;
$titleContainer . before ( $rightBtnElts ) ;
2013-12-02 00:09:39 +00:00
}
2013-12-01 15:43:46 +00:00
}
}
2014-04-17 23:41:35 +00:00
* /
2013-05-29 19:55:23 +00:00
// Create the PageDown editor
2014-03-18 01:10:22 +00:00
var pagedownEditor ;
2013-11-07 23:10:38 +00:00
var fileDesc ;
2013-06-19 20:33:46 +00:00
core . initEditor = function ( fileDescParam ) {
2013-07-18 00:07:22 +00:00
if ( fileDesc !== undefined ) {
2013-07-30 08:46:36 +00:00
eventMgr . onFileClosed ( fileDesc ) ;
2013-07-18 00:07:22 +00:00
}
2013-06-03 22:19:52 +00:00
fileDesc = fileDescParam ;
2013-09-09 00:08:55 +00:00
2014-03-18 01:10:22 +00:00
if ( pagedownEditor !== undefined ) {
2013-06-03 22:19:52 +00:00
// If the editor is already created
2014-04-06 00:59:32 +00:00
editor . undoMgr . init ( ) ;
return pagedownEditor . uiManager . setUndoRedoButtonStates ( ) ;
2013-06-02 00:38:23 +00:00
}
2013-09-09 00:08:55 +00:00
2013-06-10 21:22:32 +00:00
// Create the converter and the editor
2013-05-29 19:55:23 +00:00
var converter = new Markdown . Converter ( ) ;
2013-12-27 20:07:49 +00:00
var options = {
_DoItalicsAndBold : function ( text ) {
// Restore original markdown implementation
text = text . replace ( /(\*\*|__)(?=\S)(.+?[*_]*)(?=\S)\1/g ,
"<strong>$2</strong>" ) ;
text = text . replace ( /(\*|_)(?=\S)(.+?)(?=\S)\1/g ,
"<em>$2</em>" ) ;
return text ;
}
} ;
converter . setOptions ( options ) ;
2014-03-26 00:29:34 +00:00
pagedownEditor = new Markdown . Editor ( converter , undefined , {
2014-04-05 00:54:06 +00:00
undoManager : editor . undoMgr
2014-03-26 00:29:34 +00:00
} ) ;
2013-09-15 01:35:58 +00:00
2013-12-05 00:25:17 +00:00
// Custom insert link dialog
2014-03-18 01:10:22 +00:00
pagedownEditor . hooks . set ( "insertLinkDialog" , function ( callback ) {
2013-12-05 00:25:17 +00:00
core . insertLinkCallback = callback ;
utils . resetModalInputs ( ) ;
$ ( ".modal-insert-link" ) . modal ( ) ;
return true ;
} ) ;
// Custom insert image dialog
2014-03-18 01:10:22 +00:00
pagedownEditor . hooks . set ( "insertImageDialog" , function ( callback ) {
2013-12-05 00:25:17 +00:00
core . insertLinkCallback = callback ;
if ( core . catchModal ) {
2013-09-12 23:25:25 +00:00
return true ;
2013-12-05 00:25:17 +00:00
}
utils . resetModalInputs ( ) ;
$ ( ".modal-insert-image" ) . modal ( ) ;
return true ;
} ) ;
2014-03-03 23:30:55 +00:00
2014-03-18 01:10:22 +00:00
eventMgr . onPagedownConfigure ( pagedownEditor ) ;
pagedownEditor . hooks . chain ( "onPreviewRefresh" , eventMgr . onAsyncPreview ) ;
2014-03-20 00:24:56 +00:00
pagedownEditor . run ( ) ;
2014-04-05 00:54:06 +00:00
editor . undoMgr . init ( ) ;
2013-05-29 19:55:23 +00:00
// Hide default buttons
2013-08-10 01:19:32 +00:00
$ ( ".wmd-button-row li" ) . addClass ( "btn btn-success" ) . css ( "left" , 0 ) . find ( "span" ) . hide ( ) ;
2013-05-29 19:55:23 +00:00
// Add customized buttons
2013-08-22 00:19:59 +00:00
var $btnGroupElt = $ ( '.wmd-button-group1' ) ;
$ ( "#wmd-bold-button" ) . append ( $ ( '<i class="icon-bold">' ) ) . appendTo ( $btnGroupElt ) ;
$ ( "#wmd-italic-button" ) . append ( $ ( '<i class="icon-italic">' ) ) . appendTo ( $btnGroupElt ) ;
2013-11-07 23:10:38 +00:00
$btnGroupElt = $ ( '.wmd-button-group2' ) ;
2013-08-22 00:19:59 +00:00
$ ( "#wmd-link-button" ) . append ( $ ( '<i class="icon-globe">' ) ) . appendTo ( $btnGroupElt ) ;
$ ( "#wmd-quote-button" ) . append ( $ ( '<i class="icon-indent-right">' ) ) . appendTo ( $btnGroupElt ) ;
$ ( "#wmd-code-button" ) . append ( $ ( '<i class="icon-code">' ) ) . appendTo ( $btnGroupElt ) ;
$ ( "#wmd-image-button" ) . append ( $ ( '<i class="icon-picture">' ) ) . appendTo ( $btnGroupElt ) ;
2013-11-07 23:10:38 +00:00
$btnGroupElt = $ ( '.wmd-button-group3' ) ;
2013-08-22 00:19:59 +00:00
$ ( "#wmd-olist-button" ) . append ( $ ( '<i class="icon-list-numbered">' ) ) . appendTo ( $btnGroupElt ) ;
$ ( "#wmd-ulist-button" ) . append ( $ ( '<i class="icon-list-bullet">' ) ) . appendTo ( $btnGroupElt ) ;
$ ( "#wmd-heading-button" ) . append ( $ ( '<i class="icon-text-height">' ) ) . appendTo ( $btnGroupElt ) ;
$ ( "#wmd-hr-button" ) . append ( $ ( '<i class="icon-ellipsis">' ) ) . appendTo ( $btnGroupElt ) ;
2014-04-07 23:19:47 +00:00
$btnGroupElt = $ ( '.wmd-button-group5' ) ;
2013-08-22 00:19:59 +00:00
$ ( "#wmd-undo-button" ) . append ( $ ( '<i class="icon-reply">' ) ) . appendTo ( $btnGroupElt ) ;
$ ( "#wmd-redo-button" ) . append ( $ ( '<i class="icon-forward">' ) ) . appendTo ( $btnGroupElt ) ;
2013-07-18 23:30:28 +00:00
} ;
2014-03-03 23:30:55 +00:00
2013-08-04 15:49:14 +00:00
// Initialize multiple things and then fire eventMgr.onReady
core . onReady = function ( ) {
2014-03-20 00:24:56 +00:00
// Add RTL class
2014-04-17 23:41:35 +00:00
document . body . className += ' ' + settings . editMode ;
2014-03-20 00:24:56 +00:00
2013-11-07 23:10:38 +00:00
if ( window . viewerMode === true ) {
2013-08-22 00:19:59 +00:00
document . body . innerHTML = bodyViewerHTML ;
2013-08-04 15:49:14 +00:00
}
else {
2013-08-22 00:19:59 +00:00
document . body . innerHTML = bodyIndexHTML ;
2013-08-04 15:49:14 +00:00
}
2014-03-19 00:33:57 +00:00
2014-01-13 18:57:59 +00:00
// Initialize utils library
utils . init ( ) ;
2014-03-03 23:30:55 +00:00
2013-05-29 19:55:23 +00:00
// listen to online/offline events
$ ( window ) . on ( 'offline' , core . setOffline ) ;
$ ( window ) . on ( 'online' , setOnline ) ;
if ( navigator . onLine === false ) {
core . setOffline ( ) ;
}
// Detect user activity
$ ( document ) . mousemove ( setUserActive ) . keypress ( setUserActive ) ;
2014-04-16 23:29:51 +00:00
layout . init ( ) ;
2014-04-15 23:16:08 +00:00
editor . init ( ) ;
2014-03-20 00:24:56 +00:00
2013-08-04 00:53:46 +00:00
// Do periodic tasks
intervalId = window . setInterval ( function ( ) {
utils . updateCurrentTime ( ) ;
checkWindowUnique ( ) ;
2013-11-07 23:10:38 +00:00
if ( isUserActive ( ) === true || window . viewerMode === true ) {
2013-08-04 00:53:46 +00:00
eventMgr . onPeriodicRun ( ) ;
checkOnline ( ) ;
}
} , 1000 ) ;
eventMgr . onReady ( ) ;
} ;
// Other initialization that are not prioritary
eventMgr . addListener ( "onReady" , function ( ) {
2014-03-03 23:30:55 +00:00
2014-04-14 00:21:06 +00:00
$ ( '.modal' ) . on ( 'shown.bs.modal' , function ( ) {
2013-12-01 15:43:46 +00:00
var $elt = $ ( this ) ;
2013-09-09 23:32:24 +00:00
setTimeout ( function ( ) {
2013-12-01 15:43:46 +00:00
// 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 ( ) ;
2013-09-09 23:32:24 +00:00
} , 50 ) ;
2013-08-14 23:44:51 +00:00
} ) . on ( 'hidden.bs.modal' , function ( ) {
// Focus on the editor when modal is gone
2014-04-06 23:39:24 +00:00
editor . focus ( ) ;
2013-09-03 12:01:26 +00:00
// Revert to current theme when settings modal is closed
2013-12-02 23:29:48 +00:00
applyTheme ( window . theme ) ;
2013-08-14 23:44:51 +00:00
} ) . keyup ( function ( e ) {
// Handle enter key in modals
if ( e . which == 13 && ! $ ( e . target ) . is ( "textarea" ) ) {
$ ( this ) . find ( ".modal-footer a:last" ) . click ( ) ;
}
} ) ;
2014-03-03 23:30:55 +00:00
2013-05-29 19:55:23 +00:00
// 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 ) {
2013-06-02 00:38:23 +00:00
core . insertLinkCallback ( value ) ;
2013-06-02 19:35:44 +00:00
core . insertLinkCallback = undefined ;
2013-05-29 19:55:23 +00:00
}
} ) ;
$ ( ".action-insert-image" ) . click ( function ( e ) {
var value = utils . getInputTextValue ( $ ( "#input-insert-image" ) , e ) ;
if ( value !== undefined ) {
2013-06-02 00:38:23 +00:00
core . insertLinkCallback ( value ) ;
2013-06-02 19:35:44 +00:00
core . insertLinkCallback = undefined ;
2013-05-29 19:55:23 +00:00
}
} ) ;
2013-06-10 21:22:32 +00:00
2013-06-02 00:38:23 +00:00
// Hide events on "insert link" and "insert image" dialogs
2013-08-11 00:52:05 +00:00
$ ( ".modal-insert-link, .modal-insert-image" ) . on ( 'hidden.bs.modal' , function ( ) {
2013-06-02 00:38:23 +00:00
if ( core . insertLinkCallback !== undefined ) {
core . insertLinkCallback ( null ) ;
2013-06-02 19:35:44 +00:00
core . insertLinkCallback = undefined ;
2013-06-02 00:38:23 +00:00
}
2013-05-29 19:55:23 +00:00
} ) ;
// Settings loading/saving
$ ( ".action-load-settings" ) . click ( function ( ) {
loadSettings ( ) ;
} ) ;
$ ( ".action-apply-settings" ) . click ( function ( e ) {
saveSettings ( e ) ;
if ( ! e . isPropagationStopped ( ) ) {
window . location . reload ( ) ;
}
} ) ;
2014-04-21 01:12:57 +00:00
$ ( '.action-add-google-drive-account' ) . click ( function ( ) {
if ( settings . gdriveMultiAccount === 3 ) {
return ;
}
settings . gdriveMultiAccount ++ ;
storage . settings = JSON . stringify ( settings ) ;
window . location . reload ( ) ;
} ) ;
2013-08-20 22:40:19 +00:00
// Hot theme switcher in the settings
2013-11-07 23:10:38 +00:00
var currentTheme = window . theme ;
2013-08-20 22:40:19 +00:00
function applyTheme ( theme ) {
theme = theme || 'default' ;
if ( currentTheme != theme ) {
2013-08-23 23:50:14 +00:00
var themeModule = "less!themes/" + theme ;
2013-11-07 23:10:38 +00:00
if ( window . baseDir . indexOf ( '-min' ) !== - 1 ) {
2013-08-23 23:50:14 +00:00
themeModule = "css!themes/" + theme ;
}
2013-08-20 22:40:19 +00:00
// Undefine the module in RequireJS
requirejs . undef ( themeModule ) ;
// Then reload the style
require ( [
2013-08-23 23:50:14 +00:00
themeModule
2013-08-20 22:40:19 +00:00
] ) ;
currentTheme = theme ;
}
}
$themeInputElt = $ ( "#input-settings-theme" ) ;
$themeInputElt . on ( "change" , function ( ) {
applyTheme ( this . value ) ;
} ) ;
2013-10-12 23:10:14 +00:00
// Import docs and settings
2013-11-07 23:10:38 +00:00
$ ( ".action-import-docs-settings" ) . click ( function ( ) {
2013-10-12 23:10:14 +00:00
$ ( "#input-file-import-docs-settings" ) . click ( ) ;
2013-07-03 22:29:53 +00:00
} ) ;
2013-11-07 23:10:38 +00:00
var newstorage ;
2013-10-12 23:10:14 +00:00
$ ( "#input-file-import-docs-settings" ) . change ( function ( evt ) {
2013-07-03 22:29:53 +00:00
var files = ( evt . dataTransfer || evt . target ) . files ;
2013-08-11 00:52:05 +00:00
$ ( ".modal-settings" ) . modal ( "hide" ) ;
2013-07-03 22:29:53 +00:00
_ . each ( files , function ( file ) {
var reader = new FileReader ( ) ;
reader . onload = ( function ( importedFile ) {
return function ( e ) {
try {
2013-11-05 23:03:38 +00:00
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 ) ;
2013-10-19 22:59:17 +00:00
if ( newVersion > currentVersion ) {
2013-11-05 23:03:38 +00:00
// We manage storage upgrade, not downgrade
2013-10-19 22:59:17 +00:00
eventMgr . onError ( "Incompatible version. Please upgrade StackEdit." ) ;
} else {
$ ( '.modal-import-docs-settings' ) . modal ( 'show' ) ;
2013-10-12 23:10:14 +00:00
}
2013-07-03 22:29:53 +00:00
}
2013-11-07 23:10:38 +00:00
catch ( exc ) {
2013-10-12 23:10:14 +00:00
eventMgr . onError ( "Wrong format: " + importedFile . name ) ;
2013-07-03 22:29:53 +00:00
}
2013-10-12 23:36:45 +00:00
$ ( "#input-file-import-docs-settings" ) . val ( '' ) ;
2013-07-03 22:29:53 +00:00
} ;
} ) ( file ) ;
2013-10-12 23:10:14 +00:00
reader . readAsText ( file ) ;
2013-07-03 22:29:53 +00:00
} ) ;
} ) ;
2013-11-07 23:10:38 +00:00
$ ( ".action-import-docs-settings-confirm" ) . click ( function ( ) {
2013-11-05 23:03:38 +00:00
storage . clear ( ) ;
2014-03-24 00:22:46 +00:00
var allowedKeys = /^file\.|^folder\.|^publish\.|^settings$|^sync\.|^google\.|^author\.|^themeV3$|^version$/ ;
2013-11-05 23:03:38 +00:00
_ . each ( newstorage , function ( value , key ) {
2013-10-12 23:10:14 +00:00
if ( allowedKeys . test ( key ) ) {
2013-11-05 23:03:38 +00:00
storage [ key ] = value ;
2013-10-12 23:10:14 +00:00
}
} ) ;
window . location . reload ( ) ;
} ) ;
2013-07-03 22:29:53 +00:00
// Export settings
2013-11-07 23:10:38 +00:00
$ ( ".action-export-docs-settings" ) . click ( function ( ) {
2013-11-05 23:03:38 +00:00
utils . saveAs ( JSON . stringify ( storage ) , "StackEdit local storage.json" ) ;
2013-07-03 22:29:53 +00:00
} ) ;
2013-05-29 19:55:23 +00:00
$ ( ".action-default-settings" ) . click ( function ( ) {
2013-11-05 23:03:38 +00:00
storage . removeItem ( "settings" ) ;
storage . removeItem ( "theme" ) ;
2014-01-13 01:39:28 +00:00
if ( ! settings . dropboxFullAccess ) {
storage . removeItem ( 'dropbox.lastChangeId' ) ;
}
2013-05-29 19:55:23 +00:00
window . location . reload ( ) ;
} ) ;
$ ( ".action-app-reset" ) . click ( function ( ) {
2013-11-05 23:03:38 +00:00
storage . clear ( ) ;
2013-05-29 19:55:23 +00:00
window . location . reload ( ) ;
} ) ;
2013-06-22 23:48:57 +00:00
// Reset inputs
$ ( ".action-reset-input" ) . click ( function ( ) {
utils . resetModalInputs ( ) ;
} ) ;
2013-05-29 19:55:23 +00:00
// Tooltips
2013-12-01 15:43:46 +00:00
var openedTooltip ;
2013-12-02 00:09:39 +00:00
function createTooltip ( selector , content ) {
_ . each ( document . querySelectorAll ( selector ) , function ( tooltipElt ) {
var $tooltipElt = $ ( tooltipElt ) ;
$tooltipElt . tooltip ( {
html : true ,
container : $tooltipElt . parents ( '.modal-content' ) ,
placement : 'right' ,
trigger : 'manual' ,
title : content
} ) . click ( function ( ) {
var elt = this ;
if ( openedTooltip && openedTooltip [ 0 ] === elt ) {
return ;
}
2014-04-08 23:20:48 +00:00
utils . defer ( function ( ) {
2013-12-02 00:09:39 +00:00
$ ( document ) . on ( "click.close-tooltip" , function ( ) {
openedTooltip && openedTooltip . tooltip ( 'hide' ) ;
openedTooltip = undefined ;
$ ( document ) . off ( "click.close-tooltip" ) ;
} ) ;
openedTooltip = $ ( elt ) . tooltip ( 'show' ) ;
} ) ;
2013-12-01 15:43:46 +00:00
} ) ;
} ) ;
}
2014-03-03 23:30:55 +00:00
2013-12-02 00:09:39 +00:00
createTooltip ( ".tooltip-lazy-rendering" , 'Disable preview rendering while typing in order to offload CPU. Refresh preview after 500 ms of inactivity.' ) ;
createTooltip ( ".tooltip-default-content" , [
'Thanks for supporting StackEdit by adding a backlink in your documents!<br/><br/>' ,
'<b class="text-danger">NOTE: Backlinks in Stack Exchange Q/A are not welcome.</b>'
] . join ( '' ) ) ;
2014-04-17 23:41:35 +00:00
createTooltip ( ".tooltip-shortcuts-extension" , settingsShortcutsExtensionTooltipHTML ) ;
2013-12-02 00:09:39 +00:00
createTooltip ( ".tooltip-usercustom-extension" , settingsUserCustomExtensionTooltipHTML ) ;
createTooltip ( ".tooltip-template" , settingsTemplateTooltipHTML ) ;
2013-08-04 00:53:46 +00:00
2013-07-07 17:12:25 +00:00
// Avoid dropdown panels to close on click
$ ( "div.dropdown-menu" ) . click ( function ( e ) {
e . stopPropagation ( ) ;
} ) ;
2013-09-09 00:08:55 +00:00
2013-09-19 21:54:50 +00:00
// Non unique window dialog
$ ( '.modal-non-unique' ) . modal ( {
backdrop : "static" ,
keyboard : false ,
show : false
} ) ;
2014-03-03 23:30:55 +00:00
2013-08-23 23:50:14 +00:00
// Load images
_ . each ( document . querySelectorAll ( 'img' ) , function ( imgElt ) {
var $imgElt = $ ( imgElt ) ;
var src = $imgElt . data ( 'stackeditSrc' ) ;
if ( src ) {
2013-11-07 23:10:38 +00:00
$imgElt . attr ( 'src' , window . baseDir + '/img/' + src ) ;
2013-08-23 23:50:14 +00:00
}
} ) ;
2013-05-29 19:55:23 +00:00
2013-11-07 23:10:38 +00:00
if ( window . viewerMode === false ) {
2013-08-21 00:16:10 +00:00
// Load theme list
2013-11-05 23:03:38 +00:00
var themeOptions = _ . reduce ( constants . THEME _LIST , function ( themeOptions , name , value ) {
2013-08-21 00:16:10 +00:00
return themeOptions + '<option value="' + value + '">' + name + '</option>' ;
} , '' ) ;
document . getElementById ( 'input-settings-theme' ) . innerHTML = themeOptions ;
}
2013-05-29 19:55:23 +00:00
} ) ;
return core ;
} ) ;