Add tooltip to permission and throw error

pull/3094/head
Grandschtroumpf 6 years ago committed by François
parent 923c636219
commit 9b30703c10
  1. 3
      src/app/files/fileManager.js
  2. 4
      src/app/ui/styles/tooltip-styles.js
  3. 13
      src/app/ui/tooltip.js
  4. 37
      src/persmission-handler.js

@ -49,7 +49,8 @@ class FileManager extends ApiFactory {
name: 'fileManager', name: 'fileManager',
methods: ['getFilesFromPath', 'getCurrentFile', 'getFile', 'setFile'], methods: ['getFilesFromPath', 'getCurrentFile', 'getFile', 'setFile'],
events: ['currentFileChanged'], events: ['currentFileChanged'],
description: 'service - read/write to any files or folders, require giving permissions' description: 'service - read/write to any files or folders, require giving permissions',
permission: true
} }
} }

@ -3,7 +3,9 @@ var csjs = require('csjs-inject')
var css = csjs` var css = csjs`
.tooltip { .tooltip {
z-index: 1001; z-index: 1001;
display: inline-block; display: flex;
justify-content: space-between;
align-items: center;
position: fixed; position: fixed;
color: var(--primary) color: var(--primary)
min-height: 50px; min-height: 50px;

@ -1,8 +1,17 @@
var yo = require('yo-yo') var yo = require('yo-yo')
var css = require('./styles/tooltip-styles') var css = require('./styles/tooltip-styles')
module.exports = function addTooltip (tooltipText) { /**
var tooltip = yo`<div class="${css.tooltip} bg-secondary">${tooltipText}</div>` * Open a tooltip
* @param {string} tooltipText The text shown by the tooltip
* @param {HTMLElement} [action] An HTMLElement to display for action
*/
module.exports = function addTooltip (tooltipText, action) {
var tooltip = yo`
<div class="${css.tooltip} bg-secondary">
<span>${tooltipText}</span>
${action}
</div>`
document.body.appendChild(tooltip) document.body.appendChild(tooltip)
setTimeout(function () { setTimeout(function () {
document.body.removeChild(tooltip) document.body.removeChild(tooltip)

@ -1,37 +1,46 @@
/* global localStorage */ /* global localStorage */
const yo = require('yo-yo') const yo = require('yo-yo')
const csjs = require('csjs-inject') const csjs = require('csjs-inject')
const addTooltip = require('./app/ui/tooltip')
const modalDialog = require('./app/ui/modaldialog') const modalDialog = require('./app/ui/modaldialog')
const css = csjs` const css = csjs`
.permission p { .permission h4 {
text-align: center; text-align: center;
} }
.images { .images {
display: flex; display: flex;
justify-content: center; justify-content: center;
align-item: center; align-items: center;
padding: 10px;
} }
.images img { .images img {
width: 40px; width: 40px;
height: 40px; height: 40px;
} }
.images span {
marign: 0 20px;
}
` `
function notAllowWarning(from, to) {
return `${from.displayName || from.name} is not allowed to call ${to.displayName || to.name}.`
}
export class PermissionHandler { export class PermissionHandler {
constructor () { constructor () {
const permission = localStorage.getItem('plugin-permissions') const permission = localStorage.getItem('plugins/permissions')
this.permissions = permission ? JSON.parse(permission) : {} this.permissions = permission ? JSON.parse(permission) : {}
} }
persistPermissions () { persistPermissions () {
const permissions = JSON.stringify(this.permissions) const permissions = JSON.stringify(this.permissions)
localStorage.setItem('plugin-permissions', permissions) localStorage.setItem('plugins/permissions', permissions)
} }
clear () { clear () {
localStorage.removeItem('plugin-permissions') localStorage.removeItem('plugins/permissions')
} }
/** /**
@ -52,7 +61,7 @@ export class PermissionHandler {
this.permissions[to.name][from.name].allow = true this.permissions[to.name][from.name].allow = true
this.persistPermissions() this.persistPermissions()
} }
resolve(true) resolve()
} }
}, },
{ {
@ -62,7 +71,7 @@ export class PermissionHandler {
this.permissions[to.name][from.name].allow = false this.permissions[to.name][from.name].allow = false
this.persistPermissions() this.persistPermissions()
} }
resolve(false) reject(notAllowWarning(from, to))
} }
} }
) )
@ -80,7 +89,11 @@ export class PermissionHandler {
if (!this.permissions[to.name][from.name]) return this.openPermission(from, to) if (!this.permissions[to.name][from.name]) return this.openPermission(from, to)
const { allow, hash } = this.permissions[to.name][from.name] const { allow, hash } = this.permissions[to.name][from.name]
if (!allow) return false if (!allow) {
const warning = notAllowWarning(from, to)
addTooltip(warning)
throw new Error(warning)
}
return hash === from.hash return hash === from.hash
? true // Allow ? true // Allow
: this.openPermission(from, to) // New version of a plugin : this.openPermission(from, to) // New version of a plugin
@ -93,7 +106,7 @@ export class PermissionHandler {
*/ */
form (from, to) { form (from, to) {
const fromName = from.displayName || from.name const fromName = from.displayName || from.name
const toName = from.displayName || from.name const toName = to.displayName || to.name
const remember = this.permissions[to.name][from.name] const remember = this.permissions[to.name][from.name]
const switchMode = (e) => { const switchMode = (e) => {
@ -103,7 +116,7 @@ export class PermissionHandler {
} }
const rememberSwitch = remember const rememberSwitch = remember
? yo`<input type="checkbox" onchange="${switchMode}" checkbox class="custom-control-input" id="remember">` ? yo`<input type="checkbox" onchange="${switchMode}" checkbox class="custom-control-input" id="remember">`
: yo`<input type="checkbox" class="custom-control-input" id="remember">` : yo`<input type="checkbox" onchange="${switchMode}" class="custom-control-input" id="remember">`
const message = remember const message = remember
? `${fromName} has changed and would like to access the plugin ${toName}.` ? `${fromName} has changed and would like to access the plugin ${toName}.`
: `${fromName} would like to access plugin ${toName}.` : `${fromName} would like to access plugin ${toName}.`
@ -112,10 +125,10 @@ export class PermissionHandler {
<section class="${css.permission}"> <section class="${css.permission}">
<article class="${css.images}"> <article class="${css.images}">
<img src="${from.icon}" /> <img src="${from.icon}" />
<span> -> </span> <span> ---> </span>
<img src="${to.icon}" /> <img src="${to.icon}" />
</article> </article>
<p>${message}</p> <h4>${message}</h4>
<div class="custom-control custom-checkbox"> <div class="custom-control custom-checkbox">
${rememberSwitch} ${rememberSwitch}
<label class="custom-control-label" for="remember">Remember this choice</label> <label class="custom-control-label" for="remember">Remember this choice</label>

Loading…
Cancel
Save