|
| 1 | +<template> |
| 2 | + <div> |
| 3 | + <b-button |
| 4 | + v-b-tooltip.hover |
| 5 | + title="Share" |
| 6 | + :disabled="showPopover" |
| 7 | + :variant="hasValidShareLink ? 'secondary' : 'info'" |
| 8 | + class="mr-2" |
| 9 | + ref="shareButton" |
| 10 | + > |
| 11 | + <fa icon="share-square"/> |
| 12 | + </b-button> |
| 13 | + <!-- Our popover title and content render container --> |
| 14 | + <!-- We use placement 'auto' so popover fits in the best spot on viewport --> |
| 15 | + <!-- We specify the same container as the trigger button, so that popover is close to button --> |
| 16 | + <b-popover v-if="mounted" |
| 17 | + :target="getShareButtonRef()" |
| 18 | + triggers="click" |
| 19 | + :show.sync="showPopover" |
| 20 | + placement="auto" |
| 21 | + container="myContainer" |
| 22 | + ref="popover" |
| 23 | + @show="onShow"> |
| 24 | + <template slot="title"> |
| 25 | + <b-btn @click="onClose" class="close" aria-label="Close" variant="link"> |
| 26 | + <span class="d-inline-block" aria-hidden="true">×</span> |
| 27 | + </b-btn> |
| 28 | + Share |
| 29 | + </template> |
| 30 | + <div v-if="!hasValidShareLink"> |
| 31 | + <p class="mb-2">Share live captions with others.</p> |
| 32 | + <p class="mb-1"> |
| 33 | + <b-btn @click="getLink()" size="sm" class="px-2 py-1" :disabled="gettingLink">Get Link <fa v-if="gettingLink" icon="spinner" spin /></b-btn> |
| 34 | + </p> |
| 35 | + </div> |
| 36 | + <div v-else style="width:500px; min-width:200px; max-width:100%"> |
| 37 | + <p class="mb-2">Use this link to share live captions with others.</p> |
| 38 | + <input @focus="shareLinkSelect()" @click="shareLinkSelect()" ref="shareLinkInput" type="text" class="form-control small mb-2" style="font-size:.7rem" readonly :value="shareLink" :disabled="expiringLink"/> |
| 39 | + <p class="small text-muted mb-2">Link expires <timeago :datetime="expireDate"></timeago></p> |
| 40 | + <b-dropdown text="Options" variant="outline-secondary" size="sm" toggle-class="px-2 py-1" :disabled="expiringLink"> |
| 41 | + <template slot="button-content"> |
| 42 | + <fa icon="cog"/> |
| 43 | + </template> |
| 44 | + <b-dropdown-item :to="localePath('captioner-share')">Customize Link</b-dropdown-item> |
| 45 | + <b-dropdown-divider/> |
| 46 | + <b-dropdown-item @click="expireLink()">Expire Now</b-dropdown-item> |
| 47 | + </b-dropdown> <span class="pl-2 text-secondary"><fa v-if="expiringLink" icon="spinner" spin /></span> |
| 48 | + <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> |
| 49 | + </div> |
| 50 | + <p v-if="somethingWentWrong" class="text-danger small mt-2 mb-1 font-weight-bold">Something went wrong.</p> |
| 51 | + </b-popover> |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + <b-modal :title="$t('googleCast.castingFailed')" :hide-header="true" ref="castFailedModal" :ok-only="true" ok-variant="secondary" :hide-header-close="true"> |
| 57 | + <div class="py-2"> |
| 58 | + <div class="pb-2 h4"><fa icon="exclamation-triangle" size="3x" /></div> |
| 59 | + <h2>{{$t('googleCast.unableToCast')}}</h2> |
| 60 | + <p class="lead">{{$t('googleCast.pleaseTryAgain')}}</p> |
| 61 | + </div> |
| 62 | + </b-modal> |
| 63 | + </div> |
| 64 | +</template> |
| 65 | + |
| 66 | +<style scoped> |
| 67 | +</style> |
| 68 | + |
| 69 | + |
| 70 | +<script> |
| 71 | +export default { |
| 72 | + name: 'shareButton', |
| 73 | + data: () => { |
| 74 | + return { |
| 75 | + somethingWentWrong: false, |
| 76 | + gettingLink: false, |
| 77 | + expiringLink: false, |
| 78 | + showPopover: false, |
| 79 | + mounted: false, |
| 80 | + }; |
| 81 | + }, |
| 82 | + mounted: function() { |
| 83 | + this.mounted = true; |
| 84 | + }, |
| 85 | + computed: { |
| 86 | + shareLink: function() { |
| 87 | + return this.$store.state.settings.share.url; |
| 88 | + }, |
| 89 | + roomId: function() { |
| 90 | + return this.$store.state.settings.share.roomId; |
| 91 | + }, |
| 92 | + expireDate: function() { |
| 93 | + return this.$store.state.settings.share.expireDate; |
| 94 | + }, |
| 95 | + hasValidShareLink() { |
| 96 | + return this.shareLink && this.roomId && this.expireDate; |
| 97 | + }, |
| 98 | + twitterShareLink() { |
| 99 | + return 'https://twitter.com/intent/tweet?' + 'text=' + encodeURIComponent('I\'m now captioning live with @WebCaptioner') + '&url='+ this.shareLink; |
| 100 | + }, |
| 101 | + }, |
| 102 | + methods: { |
| 103 | + getShareButtonRef() { |
| 104 | + return this.$refs.shareButton; |
| 105 | + }, |
| 106 | + async getLink() { |
| 107 | + this.somethingWentWrong = false; |
| 108 | + this.gettingLink = true; |
| 109 | +
|
| 110 | + try { |
| 111 | + const {roomId, ownerKey, url, expireDate} = await this.$axios.$post('/api/rooms'); |
| 112 | +
|
| 113 | + this.$store.commit('SET_SHARE_ROOM_ID', { roomId }); |
| 114 | + this.$store.commit('SET_SHARE_OWNER_KEY', { ownerKey }); |
| 115 | + this.$store.commit('SET_SHARE_URL', { url }); |
| 116 | + this.$store.commit('SET_SHARE_EXPIRE_DATE', { expireDate }); |
| 117 | +
|
| 118 | +
|
| 119 | + this.$socket.sendObj({ |
| 120 | + action: 'authenticateRoomOwner', |
| 121 | + roomId: this.$store.state.settings.share.roomId, |
| 122 | + ownerKey: this.$store.state.settings.share.ownerKey, |
| 123 | + }); |
| 124 | + } |
| 125 | + catch (e) { |
| 126 | + this.somethingWentWrong = true; |
| 127 | + } |
| 128 | + finally { |
| 129 | + this.gettingLink = false; |
| 130 | + } |
| 131 | + }, |
| 132 | + async expireLink() { |
| 133 | + this.expiringLink = true; |
| 134 | + const ownerKey = this.$store.state.settings.share.ownerKey; |
| 135 | + |
| 136 | + try { |
| 137 | + await this.$axios.$delete('/api/rooms/' + this.roomId + '?ownerKey=' + ownerKey) === 'OK'; |
| 138 | + this.nullLinkProperties(); |
| 139 | + } |
| 140 | + catch(e) { |
| 141 | + // We might get a 403 if the wrong ownerKey is provided or 404 if the given roomKey doesn't |
| 142 | + // exist for some reason. We won't invalidate the link server-side, but as far as this client |
| 143 | + // is concerned, we'll remove our references to that link so they |
| 144 | + // can generate a new one. |
| 145 | + this.nullLinkProperties(); |
| 146 | + } |
| 147 | + finally { |
| 148 | + this.expiringLink = false; |
| 149 | + } |
| 150 | + }, |
| 151 | + nullLinkProperties() { |
| 152 | + this.$store.commit('SET_SHARE_ROOM_ID', { roomId: null }); |
| 153 | + this.$store.commit('SET_SHARE_OWNER_KEY', { ownerKey: null }); |
| 154 | + this.$store.commit('SET_SHARE_URL', { url: null }); |
| 155 | + this.$store.commit('SET_SHARE_EXPIRE_DATE', { expireDate: null }); |
| 156 | + }, |
| 157 | + shareLinkSelect() { |
| 158 | + this.$nextTick(function () { |
| 159 | + if (this.$refs.shareLinkInput) { |
| 160 | + this.$refs.shareLinkInput.focus(); |
| 161 | + this.$refs.shareLinkInput.select(); |
| 162 | + } |
| 163 | + }); |
| 164 | + }, |
| 165 | +
|
| 166 | + onClose () { |
| 167 | + this.showPopover = false; |
| 168 | + }, |
| 169 | + onOk () { |
| 170 | +
|
| 171 | + }, |
| 172 | + onShow () { |
| 173 | + // Called just before popover is shown |
| 174 | + // Hide any tooltips that may be open on the cast button |
| 175 | + this.$root.$emit('bv::hide::tooltip'); |
| 176 | + if (this.$refs.shareLinkInput) { |
| 177 | + this.$refs.shareLinkInput.focus(); |
| 178 | + this.$refs.shareLinkInput.select(); |
| 179 | + } |
| 180 | + }, |
| 181 | + }, |
| 182 | + watch: { |
| 183 | + 'hasValidShareLink': function () { |
| 184 | + this.shareLinkSelect(); |
| 185 | + }, |
| 186 | + }, |
| 187 | +} |
| 188 | +</script> |
0 commit comments