summaryrefslogtreecommitdiffstats
path: root/src/components/Settings/SettingsAddressbookShare.vue
blob: 3e9247898c71854052d5eb51234a102bc4f2020e (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<!--
  - @copyright Copyright (c) 2018 Team Popcorn <teampopcornberlin@gmail.com>
  -
  - @author Team Popcorn <teampopcornberlin@gmail.com>
  -
  - @license GNU AGPL version 3 or any later version
  -
  - This program is free software: you can redistribute it and/or modify
  - it under the terms of the GNU Affero General Public License as
  - published by the Free Software Foundation, either version 3 of the
  - License, or (at your option) any later version.
  -
  - This program is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  - GNU Affero General Public License for more details.
  -
  - You should have received a copy of the GNU Affero General Public License
  - along with this program. If not, see <http://www.gnu.org/licenses/>.
  -
-->

<template>
	<div class="addressbook__shares">
		<multiselect
			id="users-groups-search"
			:options="usersOrGroups"
			:searchable="true"
			:loading="isLoading"
			:internal-search="false"
			:options-limit="250"
			:limit="3"
			:max-height="600"
			:show-no-results="false"
			:placeholder="placeholder"
			track-by="match"
			label="match"
			open-direction="bottom"
			class="multiselect-vue"
			@search-change="asyncFind"
			@input="addSharee">
			<template slot="singleLabel" slot-scope="props"><span class="option__desc"><span class="option__title">{{ props.option.matchpattern }}</span></span></template>
			<template slot="option" slot-scope="props">
				<div class="option__desc">
					<span class="">{{ props.option.matchstart }}</span><span class="" style="font-weight: bold;">{{ props.option.matchpattern }}</span><span class="">{{ props.option.matchend }} {{ props.option.matchtag }}</span>
				</div>
			</template>
			<span slot="noResult">{{ noResult }} </span>
		</multiselect>
		<!-- list of user or groups addressbook is shared with -->
		<ul v-if="addressbook.shares.length > 0" class="addressbook__shares__list">
			<address-book-sharee v-for="sharee in addressbook.shares" :key="sharee.displayname" :sharee="sharee" />
		</ul>
	</div>
</template>

<script>
import Multiselect from 'vue-multiselect'
import addressBookSharee from './SettingsAddressbookSharee'

import clickOutside from 'vue-click-outside'

import api from '../../services/api'

export default {
	name: 'SettingsShareAddressBook',
	components: {
		clickOutside,
		Multiselect,
		addressBookSharee
	},
	directives: {
		clickOutside
	},
	props: {
		addressbook: {
			type: Object,
			default() {
				return {}
			}
		}
	},
	data() {
		return {
			isLoading: false,
			usersOrGroups: []
		}
	},
	computed: {
		placeholder() {
			return t('contacts', 'Share with users or groups')
		},
		noResult() {
			return t('contacts', 'Oops! No elements found. Consider changing the search query.')
		}
	},
	methods: {
		addSharee(chosenUserOrGroup) {
			let payload = []
			payload.push(this.addressbook)
			payload.push(chosenUserOrGroup.match)
			this.$store.dispatch('shareAddressbook', payload)
		},

		formatMatchResults(matches, query, matchTag) {
			// format response from axios.all and add them to the option array
			if (matches.length < 1) {
				return
			}
			for (let i = 0; i < matches.length; i++) {
				let regex = new RegExp(query, 'i')
				let matchResult = matches[i].split(regex)
				let newMatch = {
					match: matches[i] + ' ' + matchTag,
					matchstart: matchResult[0],
					matchpattern: matches[i].match(regex)[0],
					matchend: matchResult[1],
					matchtag: matchTag
				}
				this.usersOrGroups.push(newMatch)
			}
		},

		asyncFind(query) {
			this.isLoading = true
			this.usersOrGroups = []
			if (query.length > 0) {
				api.all([
					api.get(OC.linkToOCS('cloud', 2) + 'users?search=' + query),
					api.get(OC.linkToOCS('cloud', 2) + 'groups?search=' + query)
				]).then(response => {
					let matchingUsers = response[0].data.ocs.data.users
					let matchingGroups = response[1].data.ocs.data.groups
					try {
						this.formatMatchResults(matchingUsers, query, '(user)')
					} catch (error) {
						console.debug(error)
					}
					try {
						this.formatMatchResults(matchingGroups, query, '(group)')
					} catch (error) {
						console.debug(error)
					}
				}).then(() => {

					this.isLoading = false
				})
			}
		}
	}
}
</script>