Skip to content

Commit 362b2c8

Browse files
committed
disabled change mode view for logout users
1 parent fdd4e08 commit 362b2c8

File tree

3 files changed

+76
-29
lines changed

3 files changed

+76
-29
lines changed

public/js/index.js

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@ import { preventXSS } from './render'
7777
import Editor from './lib/editor'
7878

7979
import getUIElements from './lib/editor/ui-elements'
80+
import uiElemsWithoutJquery from './lib/editor/ui-elems-without-jquery'
8081
import { emojifyImageDir } from './lib/editor/constants'
8182
import modeType from './lib/modeType'
8283
import appState from './lib/appState'
84+
//import { isLogicalNot } from 'vega-lite/build/src/logical'
8385

8486
require('../vendor/showup/showup')
8587

@@ -256,6 +258,7 @@ const statusType = {
256258

257259
// global vars
258260
window.loaded = false
261+
let isLogin = false
259262
let needRefresh = false
260263
let isDirty = false
261264
let editShown = false
@@ -319,6 +322,8 @@ defaultTextHeight = parseInt($('.CodeMirror').css('line-height'))
319322
// initalize ui reference
320323
const ui = getUIElements()
321324

325+
const uiByNativeJS = uiElemsWithoutJquery()
326+
322327
// page actions
323328
var opts = {
324329
lines: 11, // The number of lines to draw
@@ -423,7 +428,9 @@ Visibility.change(function (e, state) {
423428

424429
// when page ready
425430
$(document).ready(function () {
426-
if (ui.toolbar.edit.data('blockSource')) { replaceUrl(window.location.href) }
431+
if (ui.toolbar.edit.data('blockSource') && isLogin === false) { replaceUrl(window.location.href) }
432+
433+
console.log("personal info", onlineUsers)
427434

428435
idle.checkAway()
429436
checkResponsive()
@@ -473,14 +480,14 @@ $(document).ready(function () {
473480

474481
key.filter = function (e) { return true }
475482
key('ctrl+alt+e', function (e) {
476-
if (ui.toolbar.edit.data('blockSource')) return
483+
if (ui.toolbar.edit.data('blockSource') && isLogin === false) return
477484
changeMode(modeType.edit)
478485
})
479486
key('ctrl+alt+v', function (e) {
480487
changeMode(modeType.view)
481488
})
482489
key('ctrl+alt+b', function (e) {
483-
if (ui.toolbar.both.data('blockSource')) return
490+
if (ui.toolbar.both.data('blockSource') && isLogin === false) return
484491
changeMode(modeType.both)
485492
})
486493
// toggle-dropdown
@@ -505,18 +512,36 @@ $(window).on('error', function () {
505512
})
506513

507514
// replace url if user have not rights to veiw source code
508-
function replaceUrl(url) {
509-
const urlHasEdit = /\?edit/;
510-
const urlHasBoth = /\?both/;
511-
if (urlHasEdit.test(url)) {
512-
let newUrl = url.toString().replace(urlHasEdit, '?view');
513-
window.location.replace(newUrl);
514-
} else if (urlHasBoth.test(url)) {
515-
let newUrl = url.toString().replace(urlHasBoth, '?view');
515+
function replaceUrl (url) {
516+
const urlHasEditOrBoth = /\?edit|\?both/;
517+
if (urlHasEditOrBoth.test(url)) {
518+
let newUrl = url.toString().replace(urlHasEditOrBoth, '?view');
516519
window.location.replace(newUrl);
517520
}
518521
}
519522

523+
//
524+
function disableToolbarButtons (isLogin, elems) {
525+
if (isLogin === false) {
526+
elems.edit.setAttribute('disabled', null)
527+
elems.edit.setAttribute('title', 'You have no rights to edit this note')
528+
elems.both.setAttribute('disabled', null)
529+
elems.both.setAttribute('title', 'You have no rights to edit this note')
530+
} else {
531+
elems.edit.removeAttribute('disabled')
532+
elems.both.removeAttribute('disabled')
533+
}
534+
}
535+
536+
function userIsLogin (userPersonalInfo) {
537+
if (userPersonalInfo.hasOwnProperty('login')) {
538+
if (userPersonalInfo.login === true) {
539+
return isLogin = true
540+
}
541+
}
542+
return isLogin = false
543+
}
544+
520545
setupSyncAreas(ui.area.codemirrorScroll, ui.area.view, ui.area.markdown, editor)
521546

522547
function autoSyncscroll () {
@@ -1585,12 +1610,17 @@ function importFromUrl (url) {
15851610

15861611
// mode
15871612
ui.toolbar.mode.click(function () {
1588-
if (ui.toolbar.mode.data('blockSource')) return
1613+
if (personalInfo.userid && window.owner && personalInfo.userid === window.owner) {
1614+
toggleMode()
1615+
} else if (ui.toolbar.mode.data('blockSource')) return
15891616
toggleMode()
15901617
})
15911618
// edit
15921619
ui.toolbar.edit.click(function () {
1593-
if (ui.toolbar.edit.data('blockSource')) return
1620+
if (personalInfo.userid && window.owner && personalInfo.userid === window.owner) {
1621+
console.dir(personalInfo)
1622+
changeMode(modeType.edit)
1623+
} else if (ui.toolbar.edit.data('blockSource')) return
15941624
changeMode(modeType.edit)
15951625
})
15961626
// view
@@ -1599,7 +1629,9 @@ ui.toolbar.view.click(function () {
15991629
})
16001630
// both
16011631
ui.toolbar.both.click(function () {
1602-
if (ui.toolbar.both.data('blockSource')) return
1632+
if (personalInfo.userid && window.owner && personalInfo.userid === window.owner) {
1633+
changeMode(modeType.edit)
1634+
} else if (ui.toolbar.both.data('blockSource')) return
16031635
changeMode(modeType.both)
16041636
})
16051637

@@ -2189,6 +2221,9 @@ socket.on('online users', function (data) {
21892221
var user = data.users[i]
21902222
if (user.id !== socket.id) { buildCursor(user) } else { personalInfo = user }
21912223
}
2224+
if (ui.toolbar.edit.data('blockSource')) {
2225+
disableToolbarButtons(userIsLogin(personalInfo), uiByNativeJS)
2226+
}
21922227
})
21932228
socket.on('user status', function (data) {
21942229
if (debug) { console.debug(data) }
@@ -2238,6 +2273,12 @@ socket.on('cursor blur', function (data) {
22382273
}
22392274
})
22402275

2276+
//checked user is login
2277+
async function userPersonalInfo (personalInfo) {
2278+
let userInfo = await personalInfo
2279+
return userInfo
2280+
}
2281+
22412282
var options = {
22422283
valueNames: ['id', 'name'],
22432284
item: '<li class="ui-user-item">' +
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const uiElemsWithoutJquery = () => ({
2+
edit: document.querySelector('.ui-edit'),
3+
both: document.querySelector('.ui-both')
4+
})
5+
6+
export default uiElemsWithoutJquery

public/views/codimd/header.ejs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
<li role="presentation"><a role="menuitem" class="ui-help" href="#" data-toggle="modal" data-target=".help-modal"><i class="fa fa-question-circle fa-fw"></i> Help</a>
7777
</li>
7878
</ul>
79-
<% if(!allowVisibleSource && (permission === "limited" || "editable" || "freely")) { %>
79+
<% if(!allowVisibleSource) { %>
8080
<a class="btn btn-link ui-mode" data-block-source="<%= !allowVisibleSource %>">
8181
<i class="fa fa-pencil"></i>
8282
</a>
@@ -90,20 +90,20 @@
9090
<div class="collapse navbar-collapse">
9191
<ul class="nav navbar-nav navbar-form navbar-left" style="padding:0;">
9292
<div class="btn-group" data-toggle="buttons">
93-
<% if(!allowVisibleSource && (permission === "limited" || "editable" || "freely")) { %>
94-
<label class="btn btn-default ui-edit" data-block-source="<%= !allowVisibleSource %>" title="<%= __('You have no rights to edit this note') %>" disabled>
95-
<input type="radio" autocomplete="off"><i class="fa fa-pencil"></i>
96-
</label>
97-
<label class="btn btn-default ui-both" data-block-source="<%= !allowVisibleSource %>" title="<%= __('You have no rights to edit this note') %>" disabled>
98-
<input type="radio" autocomplete="off"><i class="fa fa-columns"></i>
99-
</label>
100-
<% } else { %>
101-
<label class="btn btn-default ui-edit" data-block-source="<%= !allowVisibleSource %>" title="<%= __('Edit') %> (Ctrl+Alt+E)">
102-
<input type="radio" name="mode" autocomplete="off"><i class="fa fa-pencil"></i>
103-
</label>
104-
<label class="btn btn-default ui-both" data-block-source="<%= !allowVisibleSource %>" title="<%= __('Both') %> (Ctrl+Alt+B)">
105-
<input type="radio" name="mode" autocomplete="off"><i class="fa fa-columns"></i>
106-
</label>
93+
<% if(!allowVisibleSource) { %>
94+
<label class="btn btn-default ui-edit" data-block-source="<%= !allowVisibleSource %>" title="<%= __('Edit') %> (Ctrl+Alt+E)">
95+
<input type="radio" autocomplete="off"><i class="fa fa-pencil"></i>
96+
</label>
97+
<label class="btn btn-default ui-both" data-block-source="<%= !allowVisibleSource %>" title="<%= __('You have no rights to edit this note') %>">
98+
<input type="radio" autocomplete="off"><i class="fa fa-columns"></i>
99+
</label>
100+
<% } else { %>
101+
<label class="btn btn-default ui-edit" data-block-source="<%= !allowVisibleSource %>" title="<%= __('Edit') %> (Ctrl+Alt+E)">
102+
<input type="radio" name="mode" autocomplete="off"><i class="fa fa-pencil"></i>
103+
</label>
104+
<label class="btn btn-default ui-both" data-block-source="<%= !allowVisibleSource %>" title="<%= __('Both') %> (Ctrl+Alt+B)">
105+
<input type="radio" name="mode" autocomplete="off"><i class="fa fa-columns"></i>
106+
</label>
107107
<% } %>
108108
<label class="btn btn-default ui-view" title="<%= __('View') %> (Ctrl+Alt+V)">
109109
<input type="radio" name="mode" autocomplete="off"><i class="fa fa-eye"></i>

0 commit comments

Comments
 (0)