diff options
author | Omri Bar-Zik <omri@bar-zik.com> | 2021-07-01 08:50:14 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-01 08:50:14 +0300 |
commit | b192839e938e4f1df7a275dcedaa281268b99ee3 (patch) | |
tree | 31e7e1ffa233d8d102737f3e26019a9519cc6d79 | |
parent | 2b8ce89c88b0c49a7ca13c65cd1a992a42024be0 (diff) |
fix: dockly crash when copy to Clipboard fails (#178)v3.21.4
-rw-r--r-- | hooks/containers.hook.js | 26 | ||||
-rw-r--r-- | hooks/images.hook.js | 26 | ||||
-rw-r--r-- | hooks/services.hook.js | 26 | ||||
-rw-r--r-- | src/widgetsTemplates/base.hook.template.js | 34 |
4 files changed, 58 insertions, 54 deletions
diff --git a/hooks/containers.hook.js b/hooks/containers.hook.js index d11785a..ca31cb9 100644 --- a/hooks/containers.hook.js +++ b/hooks/containers.hook.js @@ -1,10 +1,8 @@ 'use strict' -const EventEmitter = require('events') -const baseWidget = require('../src/baseWidget') -const clipboardy = require('clipboardy') +const baseHook = require('../src/widgetsTemplates/base.hook.template') -class hook extends baseWidget(EventEmitter) { +class hook extends baseHook { init () { // on startup we first emit data from the docker server this.getFreshData((err, data) => { @@ -45,7 +43,7 @@ class hook extends baseWidget(EventEmitter) { } if (keyString === 'c') { - this.copyContainerIdToClipboard() + this.copyItemIdToClipboard() } }) @@ -167,20 +165,12 @@ class hook extends baseWidget(EventEmitter) { } } - copyContainerIdToClipboard () { - if (this.widgetsRepo && this.widgetsRepo.has('containerList')) { - const containerId = this.widgetsRepo.get('containerList').getSelectedContainer() - if (containerId) { - clipboardy.writeSync(containerId) - - const actionStatus = this.widgetsRepo.get('actionStatus') - const message = `Container Id ${containerId} was copied to the clipboard` - - actionStatus.emit('message', { - message: message - }) - } + getSelectedItem () { + if (!this.widgetsRepo.has('containerList')) { + return null } + + return this.widgetsRepo.get('containerList').getSelectedContainer() } } diff --git a/hooks/images.hook.js b/hooks/images.hook.js index cfb4625..3599d0b 100644 --- a/hooks/images.hook.js +++ b/hooks/images.hook.js @@ -1,10 +1,8 @@ 'use strict' -const EventEmitter = require('events') -const baseWidget = require('../src/baseWidget') -const clipboardy = require('clipboardy') +const baseHook = require('../src/widgetsTemplates/base.hook.template') -class hook extends baseWidget(EventEmitter) { +class hook extends baseHook { init () { if (!this.widgetsRepo.has('toolbar')) { return null @@ -21,25 +19,17 @@ class hook extends baseWidget(EventEmitter) { } if (keyString === 'c') { - this.copyImageIdToClipboard() + this.copyItemIdToClipboard() } }) } - copyImageIdToClipboard () { - if (this.widgetsRepo && this.widgetsRepo.has('imageList')) { - const imageId = this.widgetsRepo.get('imageList').getSelectedImage() - if (imageId) { - clipboardy.writeSync(imageId) - - const actionStatus = this.widgetsRepo.get('actionStatus') - const message = `Image Id ${imageId} was copied to the clipboard` - - actionStatus.emit('message', { - message: message - }) - } + getSelectedItem () { + if (!this.widgetsRepo.has('imageList')) { + return null } + + return this.widgetsRepo.get('imageList').getSelectedImage() } notifyOnImageUpdate () { diff --git a/hooks/services.hook.js b/hooks/services.hook.js index 60e296d..9ac3de7 100644 --- a/hooks/services.hook.js +++ b/hooks/services.hook.js @@ -1,10 +1,8 @@ 'use strict' -const EventEmitter = require('events') -const baseWidget = require('../src/baseWidget') -const clipboardy = require('clipboardy') +const baseHook = require('../src/widgetsTemplates/base.hook.template') -class hook extends baseWidget(EventEmitter) { +class hook extends baseHook { init () { // on startup we first emit data from the docker server this.getFreshData((err, data) => { @@ -36,7 +34,7 @@ class hook extends baseWidget(EventEmitter) { } if (keyString === 'c') { - this.copyServiceIdToClipboard() + this.copyItemIdToClipboard() } }) @@ -85,20 +83,12 @@ class hook extends baseWidget(EventEmitter) { }) } - copyServiceIdToClipboard () { - if (this.widgetsRepo && this.widgetsRepo.has('servicesList')) { - const serviceId = this.widgetsRepo.get('servicesList').getSelectedService() - if (serviceId) { - clipboardy.writeSync(serviceId) - - const actionStatus = this.widgetsRepo.get('actionStatus') - const message = `Service Id ${serviceId} was copied to the clipboard` - - actionStatus.emit('message', { - message: message - }) - } + getSelectedItem () { + if (!this.widgetsRepo.has('servicesList')) { + return null } + + return this.widgetsRepo.get('servicesList').getSelectedService() } } diff --git a/src/widgetsTemplates/base.hook.template.js b/src/widgetsTemplates/base.hook.template.js new file mode 100644 index 0000000..406cf46 --- /dev/null +++ b/src/widgetsTemplates/base.hook.template.js @@ -0,0 +1,34 @@ + +const baseWidget = require('../baseWidget') +const EventEmitter = require('events') +const clipboardy = require('clipboardy') + +class myWidget extends baseWidget(EventEmitter) { + copyItemIdToClipboard () { + const itemId = this.getSelectedItem() + if (!itemId) { + return + } + + let message + try { + clipboardy.writeSync(itemId) + + message = `Container Id ${itemId} was copied to the clipboard` + } catch (error) { + message = error + } finally { + const actionStatus = this.widgetsRepo.get('actionStatus') + + actionStatus.emit('message', { + message: message + }) + } + } + + getSelectedItem () { + throw new Error('method getSelectedItem not implemented') + } +} + +module.exports = myWidget |