StoryCompose.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <template>
  2. <div class="container mt-2 mt-md-5 bg-black">
  3. <input type="file" id="pf-dz" name="media" class="d-none file-input" v-bind:accept="config.mimes">
  4. <span class="fixed-top text-right m-3 cursor-pointer" @click="navigateTo()">
  5. <i class="fas fa-times fa-lg text-white"></i>
  6. </span>
  7. <div v-if="loaded" class="row">
  8. <div class="col-12 col-md-6 offset-md-3 bg-dark rounded-lg">
  9. <!-- LANDING -->
  10. <div v-if="page == 'landing'" class="card card-body bg-transparent border-0 shadow-none d-flex justify-content-center" style="height: 90vh;">
  11. <div class="text-center flex-fill pt-3">
  12. <p class="text-muted font-weight-light mb-1">
  13. <i class="fas fa-history fa-5x"></i>
  14. </p>
  15. <p class="text-muted font-weight-bold mb-0">STORIES</p>
  16. </div>
  17. <div class="flex-fill py-4">
  18. <div class="card w-100 shadow-none bg-transparent">
  19. <div class="list-group bg-transparent">
  20. <!-- <a class="list-group-item text-center lead text-decoration-none text-dark" href="#">Camera</a> -->
  21. <a class="list-group-item bg-transparent lead text-decoration-none text-light font-weight-bold border-light" href="#" @click.prevent="upload()">
  22. <i class="fas fa-plus-square mr-2"></i>
  23. Add to Story
  24. </a>
  25. <a v-if="stories.length" class="list-group-item bg-transparent lead text-decoration-none text-lighter font-weight-bold border-muted" href="#" @click.prevent="edit()">
  26. <i class="far fa-clone mr-2"></i>
  27. My Story
  28. </a>
  29. <a v-if="stories.length" class="list-group-item bg-transparent lead text-decoration-none text-lighter font-weight-bold border-muted" href="#" @click.prevent="viewMyStory()">
  30. <i class="fas fa-history mr-2"></i>
  31. View My Story
  32. </a>
  33. <!-- <a v-if="stories.length" class="list-group-item bg-transparent lead text-decoration-none text-lighter font-weight-bold border-muted" href="#" @click.prevent="edit()">
  34. <i class="fas fa-network-wired mr-1"></i>
  35. Audience
  36. </a> -->
  37. <!-- <a v-if="stories.length" class="list-group-item bg-transparent lead text-decoration-none text-lighter font-weight-bold border-muted" href="#" @click.prevent="edit()">
  38. <i class="far fa-chart-bar mr-2"></i>
  39. Stats
  40. </a> -->
  41. <!-- <a class="list-group-item bg-transparent lead text-decoration-none text-lighter font-weight-bold border-muted" href="#" @click.prevent="edit()">
  42. <i class="far fa-folder mr-2"></i>
  43. Archived
  44. </a> -->
  45. <!-- <a class="list-group-item bg-transparent lead text-decoration-none text-lighter font-weight-bold border-muted" href="#" @click.prevent="edit()">
  46. <i class="far fa-question-circle mr-2"></i>
  47. Help
  48. </a> -->
  49. <a class="list-group-item bg-transparent lead text-decoration-none text-lighter font-weight-bold border-muted" href="/">
  50. <i class="fas fa-arrow-left mr-2"></i>
  51. Go back
  52. </a>
  53. <!-- <a class="list-group-item text-center lead text-decoration-none text-dark" href="#">Options</a> -->
  54. </div>
  55. </div>
  56. </div>
  57. <div class="text-center flex-fill">
  58. <!-- <p class="text-lighter small text-uppercase">
  59. <a href="/" class="text-muted font-weight-bold">Home</a>
  60. <span class="px-2 text-lighter">|</span>
  61. <a href="/i/my/story" class="text-muted font-weight-bold">View My Story</a>
  62. <span class="px-2 text-lighter">|</span>
  63. <a href="/site/help" class="text-muted font-weight-bold">Help</a>
  64. </p> -->
  65. </div>
  66. </div>
  67. <!-- CROP -->
  68. <div v-if="page == 'crop'" class="card card-body bg-transparent border-0 shadow-none d-flex justify-content-center" style="height: 90vh;">
  69. <div class="text-center py-3 d-flex justify-content-between align-items-center">
  70. <div>
  71. <button class="btn btn-outline-lighter btn-sm py-1 px-md-3" @click="deleteCurrentStory()"><i class="pr-2 fas fa-chevron-left fa-sm"></i> Delete</button>
  72. </div>
  73. <div class="">
  74. <p class="text-muted font-weight-light mb-1">
  75. <i class="fas fa-history fa-5x"></i>
  76. </p>
  77. <p class="text-muted font-weight-bold mb-0">STORIES</p>
  78. </div>
  79. <div>
  80. <button class="btn btn-primary btn-sm py-1 px-md-3" @click="performCrop()">Crop <i class="pl-2 fas fa-chevron-right fa-sm"></i></button>
  81. </div>
  82. </div>
  83. <div class="flex-fill">
  84. <div class="card w-100 mt-3">
  85. <div class="card-body p-0">
  86. <vue-cropper
  87. ref="croppa"
  88. :relativeZoom="cropper.zoom"
  89. :aspectRatio="cropper.aspectRatio"
  90. :viewMode="cropper.viewMode"
  91. :zoomable="cropper.zoomable"
  92. :rotatable="true"
  93. :src="mediaUrl"
  94. >
  95. </vue-cropper>
  96. </div>
  97. </div>
  98. </div>
  99. </div>
  100. <!-- ERROR -->
  101. <div v-if="page == 'error'" class="card card-body bg-transparent border-0 shadow-none d-flex justify-content-center align-items-center" style="height: 90vh;">
  102. <p class="h3 mb-0 text-light">Oops!</p>
  103. <p class="text-muted lead">An error occurred, please try again later.</p>
  104. <p class="text-muted mb-0">
  105. <a class="btn btn-outline-secondary py-0 px-5 font-weight-bold" href="/">Go back</a>
  106. </p>
  107. </div>
  108. <!-- UPLOADING -->
  109. <div v-if="page == 'uploading'" class="card card-body bg-transparent border-0 shadow-none d-flex justify-content-center align-items-center" style="height: 90vh;">
  110. <p v-if="uploadProgress != 100" class="display-4 mb-0 text-muted">Uploading {{uploadProgress}}%</p>
  111. <p v-else class="display-4 mb-0 text-muted">Processing ...</p>
  112. </div>
  113. <!-- CROPPING -->
  114. <div v-if="page == 'cropping'" class="card card-body bg-transparent border-0 shadow-none d-flex justify-content-center align-items-center" style="height: 90vh;">
  115. <p class="display-4 mb-0 text-muted">Cropping ...</p>
  116. </div>
  117. <!-- PREVIEW -->
  118. <div v-if="page == 'preview'" class="card card-body bg-transparent border-0 shadow-none d-flex justify-content-center align-items-center" style="height: 90vh;">
  119. <div>
  120. <div class="form-group">
  121. <label for="durationSlider" class="text-light lead font-weight-bold">Story Duration</label>
  122. <input type="range" class="custom-range" min="3" max="10" id="durationSlider" v-model="duration">
  123. <p class="help-text text-center">
  124. <span class="text-light">{{duration}} seconds</span>
  125. </p>
  126. </div>
  127. <hr class="my-3">
  128. <a class="btn btn-primary btn-block px-5 font-weight-bold my-3" href="#" @click.prevent="shareStoryToFollowers()">
  129. Share Story with followers
  130. </a>
  131. <a class="btn btn-outline-muted btn-block px-5 font-weight-bold" href="/" @click.prevent="deleteCurrentStory()">
  132. Cancel
  133. </a>
  134. </div>
  135. <!-- <a class="btn btn-outline-secondary btn-block px-5 font-weight-bold" href="#">
  136. Share Story with everyone
  137. </a> -->
  138. </div>
  139. <!-- EDIT -->
  140. <div v-if="page == 'edit'" class="card card-body bg-transparent border-0 shadow-none d-flex justify-content-center" style="height: 90vh;">
  141. <div class="text-center flex-fill mt-5">
  142. <p class="text-muted font-weight-light mb-1">
  143. <i class="fas fa-history fa-5x"></i>
  144. </p>
  145. <p class="text-muted font-weight-bold mb-0">STORIES</p>
  146. </div>
  147. <div class="flex-fill py-4">
  148. <p class="lead font-weight-bold text-lighter">My Stories</p>
  149. <div class="card w-100 shadow-none bg-transparent" style="max-height: 50vh; overflow-y: scroll">
  150. <div class="list-group">
  151. <div v-for="(story, index) in stories" class="list-group-item bg-transparent text-center border-muted text-lighter" href="#">
  152. <div class="media align-items-center">
  153. <div class="mr-3 cursor-pointer" @click="showLightbox(story)">
  154. <img :src="story.src" class="rounded-circle border" width="40px" height="40px" style="object-fit: cover;">
  155. </div>
  156. <div class="media-body text-left">
  157. <p class="mb-0 text-muted font-weight-bold"><span>{{story.created_ago}} ago</span></p>
  158. </div>
  159. <div class="flex-grow-1 text-right">
  160. <button v-if="story.viewers.length" @click="toggleShowViewers(index)" class="btn btn-link btn-sm mr-1">
  161. <i class="fas fa-eye fa-lg text-muted"></i>
  162. </button>
  163. <button @click="deleteStory(story, index)" class="btn btn-link btn-sm">
  164. <i class="fas fa-trash-alt fa-lg text-muted"></i>
  165. </button>
  166. </div>
  167. </div>
  168. <div v-if="story.showViewers && story.viewers.length" class="m-2 text-left">
  169. <p class="font-weight-bold mb-2">Viewed By</p>
  170. <div v-for="viewer in story.viewers" class="d-flex">
  171. <img src="/storage/avatars/default.png" width="24" height="24" class="rounded-circle mr-2">
  172. <p class="mb-0 font-weight-bold">viewer.username</p>
  173. </div>
  174. </div>
  175. </div>
  176. </div>
  177. </div>
  178. </div>
  179. <div class="flex-fill text-center">
  180. <a class="btn btn-outline-secondary btn-block px-5 font-weight-bold" href="/i/stories/new" @click.prevent="goBack()">Go back</a>
  181. </div>
  182. </div>
  183. </div>
  184. </div>
  185. <b-modal
  186. id="lightbox"
  187. ref="lightboxModal"
  188. hide-header
  189. hide-footer
  190. centered
  191. size="md"
  192. class="bg-transparent"
  193. body-class="p-0 bg-transparent"
  194. >
  195. <div v-if="lightboxMedia" class="w-100 h-100 bg-transparent">
  196. <img :src="lightboxMedia.url" style="max-height: 90vh; width: 100%; object-fit: contain;">
  197. </div>
  198. </b-modal>
  199. </div>
  200. </template>
  201. <style type="text/css">
  202. .bg-black {
  203. background-color: #262626;
  204. }
  205. #lightbox .modal-content {
  206. background: transparent;
  207. }
  208. </style>
  209. <script type="text/javascript">
  210. import VueTimeago from 'vue-timeago';
  211. import VueCropper from 'vue-cropperjs';
  212. import 'cropperjs/dist/cropper.css';
  213. export default {
  214. components: {
  215. VueCropper,
  216. VueTimeago
  217. },
  218. props: ['profile-id'],
  219. data() {
  220. return {
  221. loaded: false,
  222. config: window.App.config,
  223. mimes: [
  224. 'image/jpeg',
  225. 'image/png',
  226. // 'video/mp4'
  227. ],
  228. page: 'landing',
  229. pages: [
  230. 'landing',
  231. 'crop',
  232. 'edit',
  233. 'confirm',
  234. 'error',
  235. 'uploading'
  236. ],
  237. uploading: false,
  238. uploadProgress: 0,
  239. cropper: {
  240. aspectRatio: 9/16,
  241. viewMode: 2,
  242. zoomable: true,
  243. zoom: null
  244. },
  245. mediaUrl: null,
  246. mediaId: null,
  247. stories: [],
  248. lightboxMedia: false,
  249. duration: 3
  250. };
  251. },
  252. mounted() {
  253. $('body').addClass('bg-black');
  254. this.mediaWatcher();
  255. axios.get('/api/stories/v0/fetch/' + this.profileId)
  256. .then(res => {
  257. this.stories = res.data.map(s => {
  258. s.showViewers = false;
  259. s.viewers = [];
  260. return s;
  261. });
  262. this.loaded = true;
  263. });
  264. },
  265. methods: {
  266. upload() {
  267. let fi = $('.file-input[name="media"]');
  268. fi.trigger('click');
  269. },
  270. mediaWatcher() {
  271. let self = this;
  272. $(document).on('change', '#pf-dz', function(e) {
  273. self.triggerUpload();
  274. });
  275. },
  276. triggerUpload() {
  277. let self = this;
  278. self.uploading = true;
  279. let io = document.querySelector('#pf-dz');
  280. self.page = 'uploading';
  281. Array.prototype.forEach.call(io.files, function(io, i) {
  282. if(self.media && self.media.length + i >= self.config.uploader.album_limit) {
  283. swal('Error', 'You can only upload ' + self.config.uploader.album_limit + ' photos per album', 'error');
  284. self.uploading = false;
  285. self.page = 2;
  286. return;
  287. }
  288. let type = io.type;
  289. let validated = $.inArray(type, self.mimes);
  290. if(validated == -1) {
  291. swal('Invalid File Type', 'The file you are trying to add is not a valid mime type. Please upload a '+self.mimes+' only.', 'error');
  292. self.uploading = false;
  293. self.page = 'error';
  294. return;
  295. }
  296. let form = new FormData();
  297. form.append('file', io);
  298. let xhrConfig = {
  299. onUploadProgress: function(e) {
  300. let progress = Math.floor( (e.loaded * 100) / e.total );
  301. self.uploadProgress = progress;
  302. }
  303. };
  304. io.value = null;
  305. axios.post('/api/stories/v0/add', form, xhrConfig)
  306. .then(function(e) {
  307. self.uploadProgress = 100;
  308. self.uploading = false;
  309. self.mediaUrl = e.data.media_url;
  310. self.mediaId = e.data.media_id;
  311. self.page = e.data.media_type === 'video' ? 'preview' : 'crop';
  312. // window.location.href = '/i/my/story';
  313. }).catch(function(e) {
  314. self.uploading = false;
  315. io.value = null;
  316. let msg = e.response.data.message ? e.response.data.message : 'Something went wrong.'
  317. swal('Oops!', msg, 'warning');
  318. self.page = 'error';
  319. });
  320. self.uploadProgress = 0;
  321. });
  322. },
  323. expiresTimestamp(ts) {
  324. ts = new Date(ts * 1000);
  325. return ts.toDateString() + ' ' + ts.toLocaleTimeString();
  326. },
  327. edit() {
  328. this.page = 'edit';
  329. },
  330. showLightbox(story) {
  331. this.lightboxMedia = {
  332. url: story.src
  333. }
  334. this.$refs.lightboxModal.show();
  335. },
  336. deleteStory(story, index) {
  337. if(window.confirm('Are you sure you want to delete this Story?') != true) {
  338. return;
  339. }
  340. axios.delete('/api/stories/v0/delete/' + story.id)
  341. .then(res => {
  342. this.stories.splice(index, 1);
  343. if(this.stories.length == 0) {
  344. window.location.href = '/i/stories/new';
  345. }
  346. });
  347. },
  348. navigateTo(path = '/') {
  349. window.location.href = path;
  350. },
  351. goBack() {
  352. this.page = 'landing';
  353. },
  354. performCrop() {
  355. this.page = 'cropping';
  356. let data = this.$refs.croppa.getData();
  357. axios.post('/api/stories/v0/crop', {
  358. media_id: this.mediaId,
  359. width: data.width,
  360. height: data.height,
  361. x: data.x,
  362. y: data.y
  363. }).then(res => {
  364. this.page = 'preview';
  365. });
  366. },
  367. deleteCurrentStory() {
  368. let story = {
  369. id: this.mediaId
  370. };
  371. this.deleteStory(story);
  372. this.page = 'landing';
  373. },
  374. shareStoryToFollowers() {
  375. axios.post('/api/stories/v0/publish', {
  376. media_id: this.mediaId,
  377. duration: this.duration
  378. }).then(res => {
  379. window.location.href = '/i/my/story?id=' + this.mediaId;
  380. })
  381. },
  382. viewMyStory() {
  383. window.location.href = '/i/my/story';
  384. },
  385. toggleShowViewers(index) {
  386. this.stories[index].showViewers = this.stories[index].showViewers ? false : true;
  387. }
  388. }
  389. }
  390. </script>