summaryrefslogtreecommitdiffstats
path: root/src/components/ShareItem.vue
blob: c7898330cee897732059a39cfee5f0600d7c36f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<template>
	<NcModal @close="$emit('close')">
		<div id="share-item">
			<form name="feedform">
				<fieldset>
					<input v-model="userName"
						type="text"
						:placeholder="t('news', 'User Name')"
						name="user"
						pattern="[^\s]+"
						required
						autofocus
						@keyup="debounceSearchUsers()">

					<div class="user-bubble-container">
						<NcLoadingIcon v-if="searching" />
						<NcUserBubble v-for="user in users"
							v-else-if="!searching"
							:key="user.shareName"
							:size="30"
							:display-name="user.displayName"
							:primary="selected.map((val) => { return val.shareName }).includes(user.shareName)"
							:user="user.shareName"
							@click="clickUser(user)" />
					</div>

					<NcButton :wide="true"
						type="primary"
						:disabled="selected.length === 0"
						@click="share()">
						<template v-if="selected.length === 0">
							{{ t("news", "Share") }}
						</template>
						<template v-else-if="selected.length === 1">
							{{ t("news", "Share with") + ' ' + selected[0].displayName }}
						</template>
						<template v-else-if="selected.length > 1">
							{{ t("news", "Share with {num} users", { num: selected.length }) }}
						</template>
					</NcButton>
				</fieldset>
			</form>
		</div>
	</NcModal>
</template>

<script lang="ts">

import Vue from 'vue'
import _ from 'lodash'

import NcModal from '@nextcloud/vue/dist/Components/NcModal.js'
import NcButton from '@nextcloud/vue/dist/Components/NcButton.js'
import NcUserBubble from '@nextcloud/vue/dist/Components/NcUserBubble.js'
import NcLoadingIcon from '@nextcloud/vue/dist/Components/NcLoadingIcon.js'

import { ShareService } from '../dataservices/share.service'

export default Vue.extend({
	components: {
		NcModal,
		NcButton,
		NcUserBubble,
		NcLoadingIcon,
	},
	props: {
		itemId: {
			type: Number,
			required: true,
		},
	},
	data: () => {
		return {
			userName: '',
			users: [],
			selected: [],
			searching: false,
		} as any
	},
	created() {
		this.debounceSearchUsers = _.debounce(this.searchUsers, 800)
	},
	methods: {
		clickUser(user: any) {
			const selectedUsers = this.selected.map((val: any) => { return val.shareName })
			if (selectedUsers.includes(user.shareName)) {
				this.selected.splice(selectedUsers.indexOf(user.shareName), 1)
			} else {
				this.selected.push(user)
			}
		},

		async searchUsers() {
			this.users = []
			this.searching = true
			const response = await ShareService.fetchUsers(this.userName)
			this.searching = false

			for (const user of response.data.ocs.data.users) {
				this.users.push({ displayName: user.label, shareName: user.value.shareWith })
			}
		},

		/**
		 * Shares an item with another use in the same nextcloud instance
		 */
		async share() {
			await ShareService.share(this.itemId, this.selected.map((val: any) => { return val.shareName }))

			this.$emit('close')
		},
	},
})

</script>

<style>
#share-item .user-bubble__content * {
  cursor: pointer;
}

#share-item fieldset {
  padding: 16px;
}

#share-item input {
  width: 90%;
}

#share-item .user-bubble-container {
  margin: 10px;
}

#share-item .user-bubble__wrapper {
  margin: 5px 10px;
}

</style>