This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
ShareButton.vue
188 lines (174 loc) · 6.49 KB
/
ShareButton.vue
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<template>
<div>
<b-button
v-b-tooltip.hover
title="Share"
:disabled="showPopover"
:variant="hasValidShareLink ? 'secondary' : 'info'"
class="mr-2"
ref="shareButton"
>
<fa icon="share-square"/>
</b-button>
<!-- Our popover title and content render container -->
<!-- We use placement 'auto' so popover fits in the best spot on viewport -->
<!-- We specify the same container as the trigger button, so that popover is close to button -->
<b-popover v-if="mounted"
:target="getShareButtonRef()"
triggers="click"
:show.sync="showPopover"
placement="auto"
container="myContainer"
ref="popover"
@show="onShow">
<template slot="title">
<b-btn @click="onClose" class="close" aria-label="Close" variant="link">
<span class="d-inline-block" aria-hidden="true">×</span>
</b-btn>
Share
</template>
<div v-if="!hasValidShareLink">
<p class="mb-2">Share live captions with others.</p>
<p class="mb-1">
<b-btn @click="getLink()" size="sm" class="px-2 py-1" :disabled="gettingLink">Get Link <fa v-if="gettingLink" icon="spinner" spin /></b-btn>
</p>
</div>
<div v-else style="width:500px; min-width:200px; max-width:100%">
<p class="mb-2">Use this link to share live captions with others.</p>
<input @focus="shareLinkSelect()" @click="shareLinkSelect()" ref="shareLinkInput" type="text" class="form-control small mb-2" style="font-size:.7rem" readonly :value="shareLink" :disabled="expiringLink"/>
<p class="small text-muted mb-2">Link expires <timeago :datetime="expireDate"></timeago></p>
<b-dropdown text="Options" variant="outline-secondary" size="sm" toggle-class="px-2 py-1" :disabled="expiringLink">
<template slot="button-content">
<fa icon="cog"/>
</template>
<b-dropdown-item :to="localePath('captioner-share')">Customize Link</b-dropdown-item>
<b-dropdown-divider/>
<b-dropdown-item @click="expireLink()">Expire Now</b-dropdown-item>
</b-dropdown> <span class="pl-2 text-secondary"><fa v-if="expiringLink" icon="spinner" spin /></span>
<b-btn size="sm" variant="light" class="text-white px-2 py-1 float-right" style="background:#1b95e0;border-color:#1b95e0;font-family:'Roboto', sans-serif;text-transform:none" :href="twitterShareLink"><fa :icon="['fab', 'twitter']" /> Tweet</b-btn>
</div>
<p v-if="somethingWentWrong" class="text-danger small mt-2 mb-1 font-weight-bold">Something went wrong.</p>
</b-popover>
<b-modal :title="$t('googleCast.castingFailed')" :hide-header="true" ref="castFailedModal" :ok-only="true" ok-variant="secondary" :hide-header-close="true">
<div class="py-2">
<div class="pb-2 h4"><fa icon="exclamation-triangle" size="3x" /></div>
<h2>{{$t('googleCast.unableToCast')}}</h2>
<p class="lead">{{$t('googleCast.pleaseTryAgain')}}</p>
</div>
</b-modal>
</div>
</template>
<style scoped>
</style>
<script>
export default {
name: 'shareButton',
data: () => {
return {
somethingWentWrong: false,
gettingLink: false,
expiringLink: false,
showPopover: false,
mounted: false,
};
},
mounted: function() {
this.mounted = true;
},
computed: {
shareLink: function() {
return this.$store.state.settings.share.url;
},
roomId: function() {
return this.$store.state.settings.share.roomId;
},
expireDate: function() {
return this.$store.state.settings.share.expireDate;
},
hasValidShareLink() {
return this.shareLink && this.roomId && this.expireDate;
},
twitterShareLink() {
return 'https://twitter.com/intent/tweet?' + 'text=' + encodeURIComponent('I\'m now captioning live with @WebCaptioner') + '&url='+ this.shareLink;
},
},
methods: {
getShareButtonRef() {
return this.$refs.shareButton;
},
async getLink() {
this.somethingWentWrong = false;
this.gettingLink = true;
try {
const {roomId, ownerKey, url, expireDate} = await this.$axios.$post('/api/rooms');
this.$store.commit('SET_SHARE_ROOM_ID', { roomId });
this.$store.commit('SET_SHARE_OWNER_KEY', { ownerKey });
this.$store.commit('SET_SHARE_URL', { url });
this.$store.commit('SET_SHARE_EXPIRE_DATE', { expireDate });
this.$socket.sendObj({
action: 'authenticateRoomOwner',
roomId: this.$store.state.settings.share.roomId,
ownerKey: this.$store.state.settings.share.ownerKey,
});
}
catch (e) {
this.somethingWentWrong = true;
}
finally {
this.gettingLink = false;
}
},
async expireLink() {
this.expiringLink = true;
const ownerKey = this.$store.state.settings.share.ownerKey;
try {
await this.$axios.$delete('/api/rooms/' + this.roomId + '?ownerKey=' + ownerKey) === 'OK';
this.nullLinkProperties();
}
catch(e) {
// We might get a 403 if the wrong ownerKey is provided or 404 if the given roomKey doesn't
// exist for some reason. We won't invalidate the link server-side, but as far as this client
// is concerned, we'll remove our references to that link so they
// can generate a new one.
this.nullLinkProperties();
}
finally {
this.expiringLink = false;
}
},
nullLinkProperties() {
this.$store.commit('SET_SHARE_ROOM_ID', { roomId: null });
this.$store.commit('SET_SHARE_OWNER_KEY', { ownerKey: null });
this.$store.commit('SET_SHARE_URL', { url: null });
this.$store.commit('SET_SHARE_EXPIRE_DATE', { expireDate: null });
},
shareLinkSelect() {
this.$nextTick(function () {
if (this.$refs.shareLinkInput) {
this.$refs.shareLinkInput.focus();
this.$refs.shareLinkInput.select();
}
});
},
onClose () {
this.showPopover = false;
},
onOk () {
},
onShow () {
// Called just before popover is shown
// Hide any tooltips that may be open on the cast button
this.$root.$emit('bv::hide::tooltip');
if (this.$refs.shareLinkInput) {
this.$refs.shareLinkInput.focus();
this.$refs.shareLinkInput.select();
}
},
},
watch: {
'hasValidShareLink': function () {
this.shareLinkSelect();
},
},
}
</script>