Skip to content

Commit a40d301

Browse files
committed
feat(contacts-menu): implement custom javascript hook action
Signed-off-by: Richard Steinmetz <richard@steinmetz.cloud>
1 parent 47322e8 commit a40d301

5 files changed

Lines changed: 136 additions & 3 deletions

File tree

core/src/components/ContactsMenu/Contact.vue

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,33 @@
2222
<NcActions v-if="actions.length"
2323
:inline="contact.topAction ? 1 : 0">
2424
<template v-for="(action, idx) in actions">
25-
<NcActionLink v-if="action.hyperlink !== '#'"
26-
:key="idx"
25+
<NcActionLink v-if="action.type === 'LinkAction' && action.hyperlink !== '#'"
26+
:key="`${idx}-link`"
2727
:href="action.hyperlink"
2828
class="other-actions">
2929
<template #icon>
3030
<img aria-hidden="true" class="contact__action__icon" :src="action.icon">
3131
</template>
3232
{{ action.title }}
3333
</NcActionLink>
34-
<NcActionText v-else :key="idx" class="other-actions">
34+
<NcActionText v-else-if="action.type === 'LinkAction'"
35+
:key="`${idx}-text`"
36+
class="other-actions">
3537
<template #icon>
3638
<img aria-hidden="true" class="contact__action__icon" :src="action.icon">
3739
</template>
3840
{{ action.title }}
3941
</NcActionText>
42+
<NcActionButton v-else-if="action.type === 'JavascriptAction' && hasContactsMenuHook(action.hook)"
43+
:key="`${idx}-hook`"
44+
:close-after-click="true"
45+
class="other-actions"
46+
@click="callContactsMenuHook(action.hook)">
47+
<template #icon>
48+
<img aria-hidden="true" class="contact__action__icon" :src="action.icon">
49+
</template>
50+
{{ action.title }}
51+
</NcActionButton>
4052
</template>
4153
</NcActions>
4254
</li>
@@ -45,14 +57,20 @@
4557
<script>
4658
import NcActionLink from '@nextcloud/vue/dist/Components/NcActionLink.js'
4759
import NcActionText from '@nextcloud/vue/dist/Components/NcActionText.js'
60+
import NcActionButton from '@nextcloud/vue/dist/Components/NcActionButton.js'
4861
import NcActions from '@nextcloud/vue/dist/Components/NcActions.js'
4962
import NcAvatar from '@nextcloud/vue/dist/Components/NcAvatar.js'
63+
import {
64+
callContactsMenuHook,
65+
hasContactsMenuHook,
66+
} from '@nextcloud/vue/dist/Functions/contactsMenu.js'
5067
5168
export default {
5269
name: 'Contact',
5370
components: {
5471
NcActionLink,
5572
NcActionText,
73+
NcActionButton,
5674
NcActions,
5775
NcAvatar,
5876
},
@@ -80,6 +98,10 @@ export default {
8098
return undefined
8199
},
82100
},
101+
methods: {
102+
hasContactsMenuHook,
103+
callContactsMenuHook,
104+
},
83105
}
84106
</script>
85107

lib/private/Contacts/ContactsMenu/ActionFactory.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
*/
66
namespace OC\Contacts\ContactsMenu;
77

8+
use OC\Contacts\ContactsMenu\Actions\JavascriptAction;
89
use OC\Contacts\ContactsMenu\Actions\LinkAction;
10+
use OCP\Contacts\ContactsMenu\IAction;
911
use OCP\Contacts\ContactsMenu\IActionFactory;
1012
use OCP\Contacts\ContactsMenu\ILinkAction;
1113

@@ -28,4 +30,21 @@ public function newLinkAction(string $icon, string $name, string $href, string $
2830
public function newEMailAction(string $icon, string $name, string $email, string $appId = ''): ILinkAction {
2931
return $this->newLinkAction($icon, $name, 'mailto:' . $email, $appId);
3032
}
33+
34+
/**
35+
* {@inheritDoc}
36+
*/
37+
public function newJavascriptAction(
38+
string $icon,
39+
string $name,
40+
string $hook,
41+
string $appId = '',
42+
): IAction {
43+
$action = new JavascriptAction();
44+
$action->setIcon($icon);
45+
$action->setName($name);
46+
$action->setHook($hook);
47+
$action->setAppId($appId);
48+
return $action;
49+
}
3150
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OC\Contacts\ContactsMenu\Actions;
11+
12+
use OCP\Contacts\ContactsMenu\IAction;
13+
14+
/**
15+
* @since 31.0.0
16+
*/
17+
class JavascriptAction implements IAction {
18+
public const TYPE = 'JavascriptAction';
19+
20+
private string $icon = '';
21+
private string $name = '';
22+
private int $priority = 10;
23+
private string $appId = '';
24+
private string $hook = '';
25+
26+
public function setIcon(string $icon) {
27+
$this->icon = $icon;
28+
}
29+
30+
public function getName(): string {
31+
return $this->name;
32+
}
33+
34+
public function setName(string $name) {
35+
$this->name = $name;
36+
}
37+
38+
public function setPriority(int $priority) {
39+
$this->priority = $priority;
40+
}
41+
42+
public function getPriority(): int {
43+
return $this->priority;
44+
}
45+
46+
public function setAppId(string $appId) {
47+
$this->appId = $appId;
48+
}
49+
50+
public function getAppId(): string {
51+
return $this->appId;
52+
}
53+
54+
public function getHook(): string {
55+
return $this->hook;
56+
}
57+
58+
public function setHook(string $hook): void {
59+
$this->hook = $hook;
60+
}
61+
62+
function jsonSerialize() {
63+
return [
64+
'type' => self::TYPE,
65+
'title' => $this->name,
66+
'icon' => $this->icon,
67+
'appId' => $this->appId,
68+
'hook' => $this->hook,
69+
];
70+
}
71+
}

lib/private/Contacts/ContactsMenu/Actions/LinkAction.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use OCP\Contacts\ContactsMenu\ILinkAction;
99

1010
class LinkAction implements ILinkAction {
11+
public const TYPE = 'LinkAction';
12+
1113
private string $icon = '';
1214
private string $name = '';
1315
private string $href = '';
@@ -64,6 +66,7 @@ public function getAppId(): string {
6466
*/
6567
public function jsonSerialize(): array {
6668
return [
69+
'type' => self::TYPE,
6770
'title' => $this->name,
6871
'icon' => $this->icon,
6972
'hyperlink' => $this->href,

lib/public/Contacts/ContactsMenu/IActionFactory.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,22 @@ public function newLinkAction(string $icon, string $name, string $href, string $
3434
* @return ILinkAction
3535
*/
3636
public function newEMailAction(string $icon, string $name, string $email, string $appId = ''): ILinkAction;
37+
38+
/**
39+
* Construct and return a new javascript hook action for the contacts menu
40+
*
41+
* @since 31.0.0
42+
*
43+
* @param string $icon full path to the action's icon
44+
* @param string $name localized name of the action
45+
* @param string $hook id of the javascript hook as registered in the frontend
46+
* @param string $appId the appName registering the action
47+
* @return IAction
48+
*/
49+
public function newJavascriptAction(
50+
string $icon,
51+
string $name,
52+
string $hook,
53+
string $appId = '',
54+
): IAction;
3755
}

0 commit comments

Comments
 (0)