From b2e1fa993de9a374a62697224fbd155a93529fc0 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Wed, 12 Oct 2022 14:06:11 +0200 Subject: [PATCH] Added JavaScript `rl-view-model.create` event for advanced code injections This commit contains one for Nextcloud, so you understand how it works --- dev/Knoin/Knoin.js | 2 ++ plugins/README.md | 6 ++++ plugins/nextcloud/index.php | 5 ++-- plugins/nextcloud/js/attachments.js | 43 +++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 plugins/nextcloud/js/attachments.js diff --git a/dev/Knoin/Knoin.js b/dev/Knoin/Knoin.js index 83126f8b5..2a40c47f5 100644 --- a/dev/Knoin/Knoin.js +++ b/dev/Knoin/Knoin.js @@ -35,6 +35,8 @@ const dialog = ViewTypePopup === vm.viewType, vmPlace = doc.getElementById(position); + fireEvent('rl-view-model.create', vm); + ViewModelClass.__builded = true; ViewModelClass.__vm = vm; diff --git a/plugins/README.md b/plugins/README.md index 7dc2f89c1..843471851 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -378,8 +378,14 @@ $Plugin->addHook('hook.name', 'functionName'); ## Misc ### idle ### rl-layout + +### rl-view-model.create + event.detail = the ViewModel class + Happens immediately after the ViewModel constructor + ### rl-view-model event.detail = the ViewModel class + Happens after the full build (vm.onBuild()) and contains viewModelDom ### sm-admin-login event.detail = FormData diff --git a/plugins/nextcloud/index.php b/plugins/nextcloud/index.php index ae5e630f1..01b81361e 100644 --- a/plugins/nextcloud/index.php +++ b/plugins/nextcloud/index.php @@ -15,7 +15,7 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin if (static::IsIntegrated()) { $this->addHook('main.fabrica', 'MainFabrica'); $this->addHook('filter.app-data', 'FilterAppData'); - $this->addHook('json.attachments', 'SaveAttachments'); + $this->addHook('json.attachments', 'DoAttachmentsActions'); $this->addJs('js/attachments.js'); } @@ -36,8 +36,7 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin return static::IsIntegrated() && \OC::$server->getUserSession()->isLoggedIn(); } - // DoAttachmentsActions - public function SaveAttachments(\SnappyMail\AttachmentsAction $data) + public function DoAttachmentsActions(\SnappyMail\AttachmentsAction $data) { if ('nextcloud' === $data->action) { $oFiles = \OCP\Files::getStorage('files'); diff --git a/plugins/nextcloud/js/attachments.js b/plugins/nextcloud/js/attachments.js new file mode 100644 index 000000000..c23ada8a5 --- /dev/null +++ b/plugins/nextcloud/js/attachments.js @@ -0,0 +1,43 @@ +(rl => { + if (rl) { + addEventListener('rl-view-model.create', e => { + if ('MailMessageView' === e.detail.viewModelTemplateID) { + let view = e.detail; + view.saveNextcloudError = ko.observable(false).extend({ falseTimeout: 7000 }); + view.saveNextcloudLoading = ko.observable(false); + view.saveNextcloud = () => { + const + hashes = (view.message()?.attachments || []) + .map(item => item?.checked() /*&& !item?.isLinked()*/ ? item.download : '') + .filter(v => v); + if (hashes.length) { + rl.fetchJSON('./?/Json/&q[]=/0/', {}, { + Action: 'AttachmentsActions', + Do: 'nextcloud', + Hashes: hashes + }) + .then(result => { + if (result?.Result) { + // success + } else { + view.saveNextcloudError(true); + } + }) + .catch(() => view.saveNextcloudError(true)); + + } + }; + } + }); + + let template = document.getElementById('MailMessageView'); + const attachmentsControls = template.content.querySelector('.attachmentsControls'); + if (attachmentsControls) { + attachmentsControls.append(Element.fromHTML('' + + '' + + '' + + 'Save Nextcloud' + + '')); + } + } +})(window.rl);