Change more global var to global

This commit is contained in:
Yukai Huang 2017-04-11 12:07:04 +08:00
parent 68ccee20b3
commit 88c0c68856

View file

@ -124,7 +124,7 @@ var supportHeaders = [
search: '###### tags:' search: '###### tags:'
} }
] ]
var supportReferrals = [ const supportReferrals = [
{ {
text: '[reference link]', text: '[reference link]',
search: '[]' search: '[]'
@ -170,7 +170,7 @@ var supportReferrals = [
search: '[]' search: '[]'
} }
] ]
var supportExternals = [ const supportExternals = [
{ {
text: '{%youtube youtubeid %}', text: '{%youtube youtubeid %}',
search: 'youtube' search: 'youtube'
@ -196,12 +196,12 @@ var supportExternals = [
search: 'pdf' search: 'pdf'
} }
] ]
var supportExtraTags = [ const supportExtraTags = [
{ {
text: '[name tag]', text: '[name tag]',
search: '[]', search: '[]',
command: function () { command: function () {
return '[name=' + window.personalInfo.name + ']' return '[name=' + personalInfo.name + ']'
} }
}, },
{ {
@ -215,7 +215,7 @@ var supportExtraTags = [
text: '[my color tag]', text: '[my color tag]',
search: '[]', search: '[]',
command: function () { command: function () {
return '[color=' + window.personalInfo.color + ']' return '[color=' + personalInfo.color + ']'
} }
}, },
{ {
@ -227,7 +227,7 @@ var supportExtraTags = [
} }
} }
] ]
var statusType = { const statusType = {
connected: { connected: {
msg: 'CONNECTED', msg: 'CONNECTED',
label: 'label-warning', label: 'label-warning',
@ -244,7 +244,7 @@ var statusType = {
fa: 'fa-plug' fa: 'fa-plug'
} }
} }
var defaultMode = modeType.view const defaultMode = modeType.view
// global vars // global vars
window.loaded = false window.loaded = false
@ -257,8 +257,8 @@ let visibleMD = false
let visibleLG = false let visibleLG = false
const isTouchDevice = 'ontouchstart' in document.documentElement const isTouchDevice = 'ontouchstart' in document.documentElement
window.currentMode = defaultMode window.currentMode = defaultMode
window.currentStatus = statusType.offline let currentStatus = statusType.offline
window.lastInfo = { let lastInfo = {
needRestore: false, needRestore: false,
cursor: null, cursor: null,
scroll: null, scroll: null,
@ -281,9 +281,9 @@ window.lastInfo = {
}, },
history: null history: null
} }
window.personalInfo = {} let personalInfo = {}
window.onlineUsers = [] let onlineUsers = []
window.fileTypes = { const fileTypes = {
'pl': 'perl', 'pl': 'perl',
'cgi': 'perl', 'cgi': 'perl',
'js': 'javascript', 'js': 'javascript',
@ -295,7 +295,7 @@ window.fileTypes = {
} }
// editor settings // editor settings
var textit = document.getElementById('textit') const textit = document.getElementById('textit')
if (!textit) { if (!textit) {
throw new Error('There was no textit area!') throw new Error('There was no textit area!')
} }
@ -522,8 +522,8 @@ function windowResizeInner (callback) {
autoSyncscroll() autoSyncscroll()
editor.setOption('viewportMargin', viewportMargin) editor.setOption('viewportMargin', viewportMargin)
// add or update user cursors // add or update user cursors
for (var i = 0; i < window.onlineUsers.length; i++) { for (var i = 0; i < onlineUsers.length; i++) {
if (window.onlineUsers[i].id !== window.personalInfo.id) { buildCursor(window.onlineUsers[i]) } if (onlineUsers[i].id !== personalInfo.id) { buildCursor(onlineUsers[i]) }
} }
updateScrollspy() updateScrollspy()
if (callback && typeof callback === 'function') { callback() } if (callback && typeof callback === 'function') { callback() }
@ -696,7 +696,7 @@ function checkTocStyle () {
} }
function showStatus (type, num) { function showStatus (type, num) {
window.currentStatus = type currentStatus = type
var shortStatus = ui.toolbar.shortStatus var shortStatus = ui.toolbar.shortStatus
var status = ui.toolbar.status var status = ui.toolbar.status
var label = $('<span class="label"></span>') var label = $('<span class="label"></span>')
@ -707,7 +707,7 @@ function showStatus (type, num) {
shortStatus.html('') shortStatus.html('')
status.html('') status.html('')
switch (window.currentStatus) { switch (currentStatus) {
case statusType.connected: case statusType.connected:
label.addClass(statusType.connected.label) label.addClass(statusType.connected.label)
fa.addClass(statusType.connected.fa) fa.addClass(statusType.connected.fa)
@ -1533,7 +1533,7 @@ $('#snippetImportModalConfirm').click(function () {
if (raw) { if (raw) {
content += '\n\n' content += '\n\n'
if (fileInfo[1] !== 'md') { if (fileInfo[1] !== 'md') {
content += '```' + window.fileTypes[fileInfo[1]] + '\n' content += '```' + fileTypes[fileInfo[1]] + '\n'
} }
content += raw content += raw
if (fileInfo[1] !== 'md') { if (fileInfo[1] !== 'md') {
@ -1706,7 +1706,7 @@ function updatePermission (newPermission) {
title = 'Only owner can view & edit' title = 'Only owner can view & edit'
break break
} }
if (window.personalInfo.userid && window.owner && window.personalInfo.userid === window.owner) { if (personalInfo.userid && window.owner && personalInfo.userid === window.owner) {
label += ' <i class="fa fa-caret-down"></i>' label += ' <i class="fa fa-caret-down"></i>'
ui.infobar.permission.label.removeClass('disabled') ui.infobar.permission.label.removeClass('disabled')
} else { } else {
@ -1723,7 +1723,7 @@ function havePermission () {
break break
case 'editable': case 'editable':
case 'limited': case 'limited':
if (!window.personalInfo.login) { if (!personalInfo.login) {
bool = false bool = false
} else { } else {
bool = true bool = true
@ -1732,7 +1732,7 @@ function havePermission () {
case 'locked': case 'locked':
case 'private': case 'private':
case 'protected': case 'protected':
if (!window.owner || window.personalInfo.userid !== window.owner) { if (!window.owner || personalInfo.userid !== window.owner) {
bool = false bool = false
} else { } else {
bool = true bool = true
@ -1779,7 +1779,7 @@ socket.on('error', function (data) {
if (data.message && data.message.indexOf('AUTH failed') === 0) { location.href = serverurl + '/403' } if (data.message && data.message.indexOf('AUTH failed') === 0) { location.href = serverurl + '/403' }
}) })
socket.on('delete', function () { socket.on('delete', function () {
if (window.personalInfo.login) { if (personalInfo.login) {
deleteServerHistory(noteid, function (err, data) { deleteServerHistory(noteid, function (err, data) {
if (!err) location.href = serverurl if (!err) location.href = serverurl
}) })
@ -1799,7 +1799,7 @@ socket.on('disconnect', function (data) {
showStatus(statusType.offline) showStatus(statusType.offline)
if (window.loaded) { if (window.loaded) {
saveInfo() saveInfo()
window.lastInfo.history = editor.getHistory() lastInfo.history = editor.getHistory()
} }
if (!editor.getOption('readOnly')) { editor.setOption('readOnly', true) } if (!editor.getOption('readOnly')) { editor.setOption('readOnly', true) }
if (!retryTimer) { if (!retryTimer) {
@ -1817,7 +1817,7 @@ socket.on('reconnect', function (data) {
socket.on('connect', function (data) { socket.on('connect', function (data) {
clearInterval(retryTimer) clearInterval(retryTimer)
retryTimer = null retryTimer = null
window.personalInfo['id'] = socket.id personalInfo['id'] = socket.id
showStatus(statusType.connected) showStatus(statusType.connected)
socket.emit('version') socket.emit('version')
}) })
@ -2135,8 +2135,8 @@ socket.on('doc', function (obj) {
} else { } else {
// if current doc is equal to the doc before disconnect // if current doc is equal to the doc before disconnect
if (setDoc && bodyMismatch) editor.clearHistory() if (setDoc && bodyMismatch) editor.clearHistory()
else if (window.lastInfo.history) editor.setHistory(window.lastInfo.history) else if (lastInfo.history) editor.setHistory(lastInfo.history)
window.lastInfo.history = null lastInfo.history = null
} }
if (!cmClient) { if (!cmClient) {
@ -2178,7 +2178,7 @@ socket.on('operation', function () {
socket.on('online users', function (data) { socket.on('online users', function (data) {
if (debug) { console.debug(data) } if (debug) { console.debug(data) }
window.onlineUsers = data.users onlineUsers = data.users
updateOnlineStatus() updateOnlineStatus()
$('.CodeMirror-other-cursors').children().each(function (key, value) { $('.CodeMirror-other-cursors').children().each(function (key, value) {
var found = false var found = false
@ -2194,14 +2194,14 @@ socket.on('online users', function (data) {
}) })
for (var i = 0; i < data.users.length; i++) { for (var i = 0; i < data.users.length; i++) {
var user = data.users[i] var user = data.users[i]
if (user.id !== socket.id) { buildCursor(user) } else { window.personalInfo = user } if (user.id !== socket.id) { buildCursor(user) } else { personalInfo = user }
} }
}) })
socket.on('user status', function (data) { socket.on('user status', function (data) {
if (debug) { console.debug(data) } if (debug) { console.debug(data) }
for (var i = 0; i < window.onlineUsers.length; i++) { for (var i = 0; i < onlineUsers.length; i++) {
if (window.onlineUsers[i].id === data.id) { if (onlineUsers[i].id === data.id) {
window.onlineUsers[i] = data onlineUsers[i] = data
} }
} }
updateOnlineStatus() updateOnlineStatus()
@ -2209,9 +2209,9 @@ socket.on('user status', function (data) {
}) })
socket.on('cursor focus', function (data) { socket.on('cursor focus', function (data) {
if (debug) { console.debug(data) } if (debug) { console.debug(data) }
for (var i = 0; i < window.onlineUsers.length; i++) { for (var i = 0; i < onlineUsers.length; i++) {
if (window.onlineUsers[i].id === data.id) { if (onlineUsers[i].id === data.id) {
window.onlineUsers[i].cursor = data.cursor onlineUsers[i].cursor = data.cursor
} }
} }
if (data.id !== socket.id) { buildCursor(data) } if (data.id !== socket.id) { buildCursor(data) }
@ -2223,18 +2223,18 @@ socket.on('cursor focus', function (data) {
}) })
socket.on('cursor activity', function (data) { socket.on('cursor activity', function (data) {
if (debug) { console.debug(data) } if (debug) { console.debug(data) }
for (var i = 0; i < window.onlineUsers.length; i++) { for (var i = 0; i < onlineUsers.length; i++) {
if (window.onlineUsers[i].id === data.id) { if (onlineUsers[i].id === data.id) {
window.onlineUsers[i].cursor = data.cursor onlineUsers[i].cursor = data.cursor
} }
} }
if (data.id !== socket.id) { buildCursor(data) } if (data.id !== socket.id) { buildCursor(data) }
}) })
socket.on('cursor blur', function (data) { socket.on('cursor blur', function (data) {
if (debug) { console.debug(data) } if (debug) { console.debug(data) }
for (var i = 0; i < window.onlineUsers.length; i++) { for (var i = 0; i < onlineUsers.length; i++) {
if (window.onlineUsers[i].id === data.id) { if (onlineUsers[i].id === data.id) {
window.onlineUsers[i].cursor = null onlineUsers[i].cursor = null
} }
} }
if (data.id !== socket.id) { buildCursor(data) } if (data.id !== socket.id) { buildCursor(data) }
@ -2259,7 +2259,7 @@ var shortOnlineUserList = new List('short-online-user-list', options)
function updateOnlineStatus () { function updateOnlineStatus () {
if (!window.loaded || !socket.connected) return if (!window.loaded || !socket.connected) return
var _onlineUsers = deduplicateOnlineUsers(window.onlineUsers) var _onlineUsers = deduplicateOnlineUsers(onlineUsers)
showStatus(statusType.online, _onlineUsers.length) showStatus(statusType.online, _onlineUsers.length)
var items = onlineUserList.items var items = onlineUserList.items
// update or remove current list items // update or remove current list items
@ -2310,8 +2310,8 @@ function sortOnlineUserList (list) {
sortFunction: function (a, b) { sortFunction: function (a, b) {
var usera = a.values() var usera = a.values()
var userb = b.values() var userb = b.values()
var useraIsSelf = (usera.id === window.personalInfo.id || (usera.login && usera.userid === window.personalInfo.userid)) var useraIsSelf = (usera.id === personalInfo.id || (usera.login && usera.userid === personalInfo.userid))
var userbIsSelf = (userb.id === window.personalInfo.id || (userb.login && userb.userid === window.personalInfo.userid)) var userbIsSelf = (userb.id === personalInfo.id || (userb.login && userb.userid === personalInfo.userid))
if (useraIsSelf && !userbIsSelf) { if (useraIsSelf && !userbIsSelf) {
return -1 return -1
} else if (!useraIsSelf && userbIsSelf) { } else if (!useraIsSelf && userbIsSelf) {
@ -2362,7 +2362,7 @@ function deduplicateOnlineUsers (list) {
for (var j = 0; j < _onlineUsers.length; j++) { for (var j = 0; j < _onlineUsers.length; j++) {
if (_onlineUsers[j].userid === user.userid) { if (_onlineUsers[j].userid === user.userid) {
// keep self color when login // keep self color when login
if (user.id === window.personalInfo.id) { if (user.id === personalInfo.id) {
_onlineUsers[j].color = user.color _onlineUsers[j].color = user.color
} }
// keep idle state if any of self client not idle // keep idle state if any of self client not idle
@ -2387,12 +2387,12 @@ function emitUserStatus (force) {
var type = null var type = null
if (visibleXS) { type = 'xs' } else if (visibleSM) { type = 'sm' } else if (visibleMD) { type = 'md' } else if (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 personalInfo['idle'] = idle.isAway
window.personalInfo['type'] = type personalInfo['type'] = type
for (var i = 0; i < window.onlineUsers.length; i++) { for (var i = 0; i < onlineUsers.length; i++) {
if (window.onlineUsers[i].id === window.personalInfo.id) { if (onlineUsers[i].id === personalInfo.id) {
window.onlineUsers[i] = window.personalInfo onlineUsers[i] = personalInfo
} }
} }
@ -2653,12 +2653,12 @@ editorInstance.on('changes', function (editor, changes) {
} }
}) })
editorInstance.on('focus', function (editor) { editorInstance.on('focus', function (editor) {
for (var i = 0; i < window.onlineUsers.length; i++) { for (var i = 0; i < onlineUsers.length; i++) {
if (window.onlineUsers[i].id === window.personalInfo.id) { if (onlineUsers[i].id === personalInfo.id) {
window.onlineUsers[i].cursor = editor.getCursor() onlineUsers[i].cursor = editor.getCursor()
} }
} }
window.personalInfo['cursor'] = editor.getCursor() personalInfo['cursor'] = editor.getCursor()
socket.emit('cursor focus', editor.getCursor()) socket.emit('cursor focus', editor.getCursor())
}) })
@ -2666,12 +2666,12 @@ const cursorActivity = _.debounce(cursorActivityInner, cursorActivityDebounce)
function cursorActivityInner (editor) { function cursorActivityInner (editor) {
if (editorHasFocus() && !Visibility.hidden()) { if (editorHasFocus() && !Visibility.hidden()) {
for (var i = 0; i < window.onlineUsers.length; i++) { for (var i = 0; i < onlineUsers.length; i++) {
if (window.onlineUsers[i].id === window.personalInfo.id) { if (onlineUsers[i].id === personalInfo.id) {
window.onlineUsers[i].cursor = editor.getCursor() onlineUsers[i].cursor = editor.getCursor()
} }
} }
window.personalInfo['cursor'] = editor.getCursor() personalInfo['cursor'] = editor.getCursor()
socket.emit('cursor activity', editor.getCursor()) socket.emit('cursor activity', editor.getCursor())
} }
} }
@ -2713,12 +2713,12 @@ editorInstance.on('beforeSelectionChange', function (doc, selections) {
}) })
editorInstance.on('blur', function (cm) { editorInstance.on('blur', function (cm) {
for (var i = 0; i < window.onlineUsers.length; i++) { for (var i = 0; i < onlineUsers.length; i++) {
if (window.onlineUsers[i].id === window.personalInfo.id) { if (onlineUsers[i].id === personalInfo.id) {
window.onlineUsers[i].cursor = null onlineUsers[i].cursor = null
} }
} }
window.personalInfo['cursor'] = null personalInfo['cursor'] = null
socket.emit('cursor blur') socket.emit('cursor blur')
}) })
@ -2729,61 +2729,61 @@ function saveInfo () {
switch (window.currentMode) { switch (window.currentMode) {
case modeType.edit: case modeType.edit:
if (scrollbarStyle === 'native') { if (scrollbarStyle === 'native') {
window.lastInfo.edit.scroll.left = left lastInfo.edit.scroll.left = left
window.lastInfo.edit.scroll.top = top lastInfo.edit.scroll.top = top
} else { } else {
window.lastInfo.edit.scroll = editor.getScrollInfo() lastInfo.edit.scroll = editor.getScrollInfo()
} }
break break
case modeType.view: case modeType.view:
window.lastInfo.view.scroll.left = left lastInfo.view.scroll.left = left
window.lastInfo.view.scroll.top = top lastInfo.view.scroll.top = top
break break
case modeType.both: case modeType.both:
window.lastInfo.edit.scroll = editor.getScrollInfo() lastInfo.edit.scroll = editor.getScrollInfo()
window.lastInfo.view.scroll.left = ui.area.view.scrollLeft() lastInfo.view.scroll.left = ui.area.view.scrollLeft()
window.lastInfo.view.scroll.top = ui.area.view.scrollTop() lastInfo.view.scroll.top = ui.area.view.scrollTop()
break break
} }
window.lastInfo.edit.cursor = editor.getCursor() lastInfo.edit.cursor = editor.getCursor()
window.lastInfo.edit.selections = editor.listSelections() lastInfo.edit.selections = editor.listSelections()
window.lastInfo.needRestore = true lastInfo.needRestore = true
} }
function restoreInfo () { function restoreInfo () {
var scrollbarStyle = editor.getOption('scrollbarStyle') var scrollbarStyle = editor.getOption('scrollbarStyle')
if (window.lastInfo.needRestore) { if (lastInfo.needRestore) {
var line = window.lastInfo.edit.cursor.line var line = lastInfo.edit.cursor.line
var ch = window.lastInfo.edit.cursor.ch var ch = lastInfo.edit.cursor.ch
editor.setCursor(line, ch) editor.setCursor(line, ch)
editor.setSelections(window.lastInfo.edit.selections) editor.setSelections(lastInfo.edit.selections)
switch (window.currentMode) { switch (window.currentMode) {
case modeType.edit: case modeType.edit:
if (scrollbarStyle === 'native') { if (scrollbarStyle === 'native') {
$(window).scrollLeft(window.lastInfo.edit.scroll.left) $(window).scrollLeft(lastInfo.edit.scroll.left)
$(window).scrollTop(window.lastInfo.edit.scroll.top) $(window).scrollTop(lastInfo.edit.scroll.top)
} else { } else {
let left = window.lastInfo.edit.scroll.left let left = lastInfo.edit.scroll.left
let top = window.lastInfo.edit.scroll.top let top = lastInfo.edit.scroll.top
editor.scrollIntoView() editor.scrollIntoView()
editor.scrollTo(left, top) editor.scrollTo(left, top)
} }
break break
case modeType.view: case modeType.view:
$(window).scrollLeft(window.lastInfo.view.scroll.left) $(window).scrollLeft(lastInfo.view.scroll.left)
$(window).scrollTop(window.lastInfo.view.scroll.top) $(window).scrollTop(lastInfo.view.scroll.top)
break break
case modeType.both: case modeType.both:
let left = window.lastInfo.edit.scroll.left let left = lastInfo.edit.scroll.left
let top = window.lastInfo.edit.scroll.top let top = lastInfo.edit.scroll.top
editor.scrollIntoView() editor.scrollIntoView()
editor.scrollTo(left, top) editor.scrollTo(left, top)
ui.area.view.scrollLeft(window.lastInfo.view.scroll.left) ui.area.view.scrollLeft(lastInfo.view.scroll.left)
ui.area.view.scrollTop(window.lastInfo.view.scroll.top) ui.area.view.scrollTop(lastInfo.view.scroll.top)
break break
} }
window.lastInfo.needRestore = false lastInfo.needRestore = false
} }
} }