Change some global variables to local

This commit is contained in:
Yukai Huang 2017-04-11 11:48:39 +08:00
parent 432f215a45
commit 18a6f9063e
1 changed files with 31 additions and 31 deletions

View File

@ -258,14 +258,14 @@ var defaultMode = modeType.view
// global vars // global vars
window.loaded = false window.loaded = false
window.needRefresh = false let needRefresh = false
window.isDirty = false let isDirty = false
window.editShown = false let editShown = false
window.visibleXS = false let visibleXS = false
window.visibleSM = false let visibleSM = false
window.visibleMD = false let visibleMD = false
window.visibleLG = false let visibleLG = false
window.isTouchDevice = 'ontouchstart' in document.documentElement const isTouchDevice = 'ontouchstart' in document.documentElement
window.currentMode = defaultMode window.currentMode = defaultMode
window.currentStatus = statusType.offline window.currentStatus = statusType.offline
window.lastInfo = { window.lastInfo = {
@ -393,7 +393,7 @@ function setRefreshModal (status) {
} }
function setNeedRefresh () { function setNeedRefresh () {
window.needRefresh = true needRefresh = true
editor.setOption('readOnly', true) editor.setOption('readOnly', true)
socket.disconnect() socket.disconnect()
showStatus(statusType.offline) showStatus(statusType.offline)
@ -415,7 +415,7 @@ Visibility.change(function (e, state) {
} }
} else { } else {
if (wasFocus) { if (wasFocus) {
if (!window.visibleXS) { if (!visibleXS) {
editor.focus() editor.focus()
editor.refresh() editor.refresh()
} }
@ -432,7 +432,7 @@ $(document).ready(function () {
checkResponsive() checkResponsive()
// if in smaller screen, we don't need advanced scrollbar // if in smaller screen, we don't need advanced scrollbar
var scrollbarStyle var scrollbarStyle
if (window.visibleXS) { if (visibleXS) {
scrollbarStyle = 'native' scrollbarStyle = 'native'
} else { } else {
scrollbarStyle = 'overlay' scrollbarStyle = 'overlay'
@ -443,7 +443,7 @@ $(document).ready(function () {
} }
checkEditorStyle() checkEditorStyle()
/* we need this only on touch devices */ /* we need this only on touch devices */
if (window.isTouchDevice) { if (isTouchDevice) {
/* cache dom references */ /* cache dom references */
var $body = $('body') var $body = $('body')
@ -553,12 +553,12 @@ function editorHasFocus () {
// 768-792px have a gap // 768-792px have a gap
function checkResponsive () { function checkResponsive () {
window.visibleXS = $('.visible-xs').is(':visible') visibleXS = $('.visible-xs').is(':visible')
window.visibleSM = $('.visible-sm').is(':visible') visibleSM = $('.visible-sm').is(':visible')
window.visibleMD = $('.visible-md').is(':visible') visibleMD = $('.visible-md').is(':visible')
window.visibleLG = $('.visible-lg').is(':visible') visibleLG = $('.visible-lg').is(':visible')
if (window.visibleXS && window.currentMode === modeType.both) { if (visibleXS && window.currentMode === modeType.both) {
if (editorHasFocus()) { changeMode(modeType.edit) } else { changeMode(modeType.view) } if (editorHasFocus()) { changeMode(modeType.edit) } else { changeMode(modeType.view) }
} }
@ -780,9 +780,9 @@ function changeMode (type) {
case modeType.edit: case modeType.edit:
ui.area.edit.show() ui.area.edit.show()
ui.area.view.hide() ui.area.view.hide()
if (!window.editShown) { if (!editShown) {
editor.refresh() editor.refresh()
window.editShown = true editShown = true
} }
break break
case modeType.view: case modeType.view:
@ -1764,11 +1764,11 @@ var socket = io.connect({
// overwrite original event for checking login state // overwrite original event for checking login state
var on = socket.on var on = socket.on
socket.on = function () { socket.on = function () {
if (!checkLoginStateChanged() && !window.needRefresh) { return on.apply(socket, arguments) } if (!checkLoginStateChanged() && !needRefresh) { return on.apply(socket, arguments) }
} }
var emit = socket.emit var emit = socket.emit
socket.emit = function () { socket.emit = function () {
if (!checkLoginStateChanged() && !window.needRefresh) { emit.apply(socket, arguments) } if (!checkLoginStateChanged() && !needRefresh) { emit.apply(socket, arguments) }
} }
socket.on('info', function (data) { socket.on('info', function (data) {
console.error(data) console.error(data)
@ -1814,7 +1814,7 @@ socket.on('disconnect', function (data) {
if (!editor.getOption('readOnly')) { editor.setOption('readOnly', true) } if (!editor.getOption('readOnly')) { editor.setOption('readOnly', true) }
if (!retryTimer) { if (!retryTimer) {
retryTimer = setInterval(function () { retryTimer = setInterval(function () {
if (!window.needRefresh) socket.connect() if (!needRefresh) socket.connect()
}, 1000) }, 1000)
} }
}) })
@ -2089,7 +2089,7 @@ socket.on('refresh', function (data) {
// auto change mode if no content detected // auto change mode if no content detected
var nocontent = editor.getValue().length <= 0 var nocontent = editor.getValue().length <= 0
if (nocontent) { if (nocontent) {
if (window.visibleXS) { window.currentMode = modeType.edit } else { window.currentMode = modeType.both } if (visibleXS) { window.currentMode = modeType.edit } else { window.currentMode = modeType.both }
} }
// parse mode from url // parse mode from url
if (window.location.search.length > 0) { if (window.location.search.length > 0) {
@ -2097,7 +2097,7 @@ socket.on('refresh', function (data) {
if (urlMode) window.currentMode = urlMode if (urlMode) window.currentMode = urlMode
} }
changeMode(window.currentMode) changeMode(window.currentMode)
if (nocontent && !window.visibleXS) { if (nocontent && !visibleXS) {
editor.focus() editor.focus()
editor.refresh() editor.refresh()
} }
@ -2169,7 +2169,7 @@ socket.on('doc', function (obj) {
} }
if (setDoc && bodyMismatch) { if (setDoc && bodyMismatch) {
window.isDirty = true isDirty = true
updateView() updateView()
} }
@ -2177,12 +2177,12 @@ socket.on('doc', function (obj) {
}) })
socket.on('ack', function () { socket.on('ack', function () {
window.isDirty = true isDirty = true
updateView() updateView()
}) })
socket.on('operation', function () { socket.on('operation', function () {
window.isDirty = true isDirty = true
updateView() updateView()
}) })
@ -2395,7 +2395,7 @@ var userStatusCache = null
function emitUserStatus (force) { function emitUserStatus (force) {
if (!window.loaded) return if (!window.loaded) return
var type = null var type = null
if (window.visibleXS) { type = 'xs' } else if (window.visibleSM) { type = 'sm' } else if (window.visibleMD) { type = 'md' } else if (window.visibleLG) { type = 'lg' } if (visibleXS) { type = 'xs' } else if (visibleSM) { type = 'sm' } else if (visibleMD) { type = 'md' } else if (visibleLG) { type = 'lg' }
window.personalInfo['idle'] = idle.isAway window.personalInfo['idle'] = idle.isAway
window.personalInfo['type'] = type window.personalInfo['type'] = type
@ -2800,7 +2800,7 @@ function restoreInfo () {
// view actions // view actions
function refreshView () { function refreshView () {
ui.area.markdown.html('') ui.area.markdown.html('')
window.isDirty = true isDirty = true
updateViewInner() updateViewInner()
} }
@ -2812,7 +2812,7 @@ var lastResult = null
var postUpdateEvent = null var postUpdateEvent = null
function updateViewInner () { function updateViewInner () {
if (window.currentMode === modeType.edit || !window.isDirty) return if (window.currentMode === modeType.edit || !isDirty) return
var value = editor.getValue() var value = editor.getValue()
var lastMeta = md.meta var lastMeta = md.meta
md.meta = {} md.meta = {}
@ -2860,7 +2860,7 @@ function updateViewInner () {
generateScrollspy() generateScrollspy()
updateScrollspy() updateScrollspy()
smoothHashScroll() smoothHashScroll()
window.isDirty = false isDirty = false
clearMap() clearMap()
// buildMap(); // buildMap();
updateTitleReminder() updateTitleReminder()