Skip to content

Commit 7759b30

Browse files
committed
remove timeout for call fn; update condition for call replaceUrl fn; create disable\enabel fn's
1 parent b86f8db commit 7759b30

File tree

1 file changed

+34
-40
lines changed

1 file changed

+34
-40
lines changed

public/js/index.js

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,6 @@ Visibility.change(function (e, state) {
429429

430430
// when page ready
431431
$(document).ready(function () {
432-
if (ui.toolbar.edit.data('blockSource') && !isLogin) { replaceUrl(window.location.href) }
433-
434432
idle.checkAway()
435433
checkResponsive()
436434
// if in smaller screen, we don't need advanced scrollbar
@@ -519,79 +517,80 @@ function replaceUrl (url) {
519517
}
520518
}
521519

522-
//
520+
//denied access to view or both mode if user have not permission for that
523521
function allowVisibleSource (isLogin, permission) {
524-
console.log('isWokr?', isLogin, permission)
525522
switch(permission) {
526523
case 'freely':
527524
blockSourceView = false
528525
break
529526
case 'editable':
530527
if (!isLogin) {
531528
blockSourceView = true
532-
uiByNativeJS.toolbar.edit.setAttribute('disabled', null)
533-
uiByNativeJS.toolbar.edit.setAttribute('title', 'You have no rights to edit this note')
534-
uiByNativeJS.toolbar.both.setAttribute('disabled', null)
535-
uiByNativeJS.toolbar.both.setAttribute('title', 'You have no rights to edit this note')
529+
disableControls()
536530
} else {
537531
blockSourceView = false
538-
uiByNativeJS.toolbar.edit.removeAttribute('disabled')
539-
uiByNativeJS.toolbar.edit.setAttribute('title', 'Edit (Ctrl+Alt+E)')
540-
uiByNativeJS.toolbar.both.removeAttribute('disabled')
541-
uiByNativeJS.toolbar.both.setAttribute('title', 'Both (Ctrl+Alt+B)')
532+
enableControls()
542533
}
543534
break
544535
case 'limited':
545536
if (!isLogin) {
546537
blockSourceView = true
547-
uiByNativeJS.toolbar.edit.setAttribute('disabled', null)
548-
uiByNativeJS.toolbar.edit.setAttribute('title', 'You have no rights to edit this note')
549-
uiByNativeJS.toolbar.both.setAttribute('disabled', null)
550-
uiByNativeJS.toolbar.both.setAttribute('title', 'You have no rights to edit this note')
538+
disableControls()
551539
} else {
552540
blockSourceView = false
553-
uiByNativeJS.toolbar.edit.removeAttribute('disabled')
554-
uiByNativeJS.toolbar.edit.setAttribute('title', 'Edit (Ctrl+Alt+E)')
555-
uiByNativeJS.toolbar.both.removeAttribute('disabled')
556-
uiByNativeJS.toolbar.both.setAttribute('title', 'Both (Ctrl+Alt+B)')
541+
enableControls()
557542
}
558543
break
559544
case 'locked':
560545
if (personalInfo.userid && window.owner && personalInfo.userid === window.owner) {
561546
blockSourceView = false
562547
} else {
563548
blockSourceView = true
564-
uiByNativeJS.toolbar.edit.setAttribute('disabled', null)
565-
uiByNativeJS.toolbar.edit.setAttribute('title', 'You have no rights to edit this note')
566-
uiByNativeJS.toolbar.both.setAttribute('disabled', null)
567-
uiByNativeJS.toolbar.both.setAttribute('title', 'You have no rights to edit this note')
549+
disableControls()
568550
}
569551
break
570552
case 'protected':
571553
if (personalInfo.userid && window.owner && personalInfo.userid === window.owner) {
572554
blockSourceView = false
573555
} else {
574556
blockSourceView = true
575-
uiByNativeJS.toolbar.edit.setAttribute('disabled', null)
576-
uiByNativeJS.toolbar.edit.setAttribute('title', 'You have no rights to edit this note')
577-
uiByNativeJS.toolbar.both.setAttribute('disabled', null)
578-
uiByNativeJS.toolbar.both.setAttribute('title', 'You have no rights to edit this note')
557+
disableControls()
579558
}
580559
break
581560
case 'private':
582561
if (personalInfo.userid && window.owner && personalInfo.userid === window.owner) {
583562
blockSourceView = false
584563
} else {
585564
blockSourceView = true
586-
uiByNativeJS.toolbar.edit.setAttribute('disabled', null)
587-
uiByNativeJS.toolbar.edit.setAttribute('title', 'You have no rights to edit this note')
588-
uiByNativeJS.toolbar.both.setAttribute('disabled', null)
589-
uiByNativeJS.toolbar.both.setAttribute('title', 'You have no rights to edit this note')
565+
disableControls()
590566
}
591567
break
592568
}
593569
}
594570

571+
//set disable attr for UI
572+
function disableControls () {
573+
ui.toolbar.edit.attr({
574+
disabled: 'true',
575+
title: 'You have no rights to edit this note'
576+
})
577+
ui.toolbar.both.attr({
578+
disabled: 'true',
579+
title: 'You have no rights to edit this note'
580+
})
581+
}
582+
//remove disable attr from UI
583+
function enableControls () {
584+
ui.toolbar.edit.removeAttr('disabled')
585+
ui.toolbar.both.removeAttr('disabled')
586+
ui.toolbar.edit.attr({
587+
title: 'Edit (Ctrl+Alt+E)'
588+
})
589+
ui.toolbar.both.attr({
590+
title: 'Both (Ctrl+Alt+B)'
591+
})
592+
}
593+
595594
//checking is user log in
596595
function userIsLogin (userPersonalInfo) {
597596
if (userPersonalInfo.hasOwnProperty('login')) {
@@ -2160,6 +2159,8 @@ socket.on('refresh', function (data) {
21602159
updateInfo(data)
21612160
updatePermission(data.permission)
21622161
currentPermission = data.permission
2162+
if (ui.toolbar.edit.data('blockSource')) { allowVisibleSource(userIsLogin(personalInfo), currentPermission) }
2163+
if (ui.toolbar.edit.data('blockSource') && blockSourceView) { replaceUrl(window.location.href) }
21632164
if (!window.loaded) {
21642165
// auto change mode if no content detected
21652166
var nocontent = editor.getValue().length <= 0
@@ -2262,6 +2263,7 @@ socket.on('operation', function () {
22622263
})
22632264

22642265
socket.on('online users', function (data) {
2266+
console.log('who faster? online users')
22652267
if (debug) { console.debug(data) }
22662268
onlineUsers = data.users
22672269
updateOnlineStatus()
@@ -2281,14 +2283,6 @@ socket.on('online users', function (data) {
22812283
var user = data.users[i]
22822284
if (user.id !== socket.id) { buildCursor(user) } else { personalInfo = user }
22832285
}
2284-
if (ui.toolbar.edit.data('blockSource')) {
2285-
setTimeout(() => {
2286-
allowVisibleSource(userIsLogin(personalInfo), currentPermission)
2287-
console.log('per in timeout', currentPermission)
2288-
}, 10000)
2289-
//allowVisibleSource(userIsLogin(personalInfo), currentPermission)
2290-
//console.log('per', currentPermission)
2291-
}
22922286
})
22932287
socket.on('user status', function (data) {
22942288
if (debug) { console.debug(data) }

0 commit comments

Comments
 (0)