StoryCompose.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 @click="deleteStory(story, index)" class="btn btn-link btn-sm">
  161. <i class="fas fa-trash-alt fa-lg text-muted"></i>
  162. </button>
  163. </div>
  164. </div>
  165. </div>
  166. </div>
  167. </div>
  168. </div>
  169. <div class="flex-fill text-center">
  170. <a class="btn btn-outline-secondary btn-block px-5 font-weight-bold" href="/i/stories/new" @click.prevent="goBack()">Go back</a>
  171. </div>
  172. </div>
  173. </div>
  174. </div>
  175. <b-modal
  176. id="lightbox"
  177. ref="lightboxModal"
  178. hide-header
  179. hide-footer
  180. centered
  181. size="md"
  182. class="bg-transparent"
  183. body-class="p-0 bg-transparent"
  184. >
  185. <div v-if="lightboxMedia" class="w-100 h-100 bg-transparent">
  186. <img :src="lightboxMedia.url" style="max-height: 90vh; width: 100%; object-fit: contain;">
  187. </div>
  188. </b-modal>
  189. </div>
  190. </template>
  191. <style type="text/css">
  192. .bg-black {
  193. background-color: #262626;
  194. }
  195. #lightbox .modal-content {
  196. background: transparent;
  197. }
  198. </style>
  199. <script type="text/javascript">
  200. import VueTimeago from 'vue-timeago';
  201. import VueCropper from 'vue-cropperjs';
  202. import 'cropperjs/dist/cropper.css';
  203. export default {
  204. components: {
  205. VueCropper,
  206. VueTimeago
  207. },
  208. props: ['profile-id'],
  209. data() {
  210. return {
  211. loaded: false,
  212. config: window.App.config,
  213. mimes: [
  214. 'image/jpeg',
  215. 'image/png',
  216. // 'video/mp4'
  217. ],
  218. page: 'landing',
  219. pages: [
  220. 'landing',
  221. 'crop',
  222. 'edit',
  223. 'confirm',
  224. 'error',
  225. 'uploading'
  226. ],
  227. uploading: false,
  228. uploadProgress: 0,
  229. cropper: {
  230. aspectRatio: 9/16,
  231. viewMode: 2,
  232. zoomable: true,
  233. zoom: null
  234. },
  235. mediaUrl: null,
  236. mediaId: null,
  237. stories: [],
  238. lightboxMedia: false,
  239. duration: 3
  240. };
  241. },
  242. mounted() {
  243. $('body').addClass('bg-black');
  244. this.mediaWatcher();
  245. axios.get('/api/stories/v0/fetch/' + this.profileId)
  246. .then(res => {
  247. this.stories = res.data;
  248. this.loaded = true;
  249. });
  250. },
  251. methods: {
  252. upload() {
  253. let fi = $('.file-input[name="media"]');
  254. fi.trigger('click');
  255. },
  256. mediaWatcher() {
  257. let self = this;
  258. $(document).on('change', '#pf-dz', function(e) {
  259. self.triggerUpload();
  260. });
  261. },
  262. triggerUpload() {
  263. let self = this;
  264. self.uploading = true;
  265. let io = document.querySelector('#pf-dz');
  266. self.page = 'uploading';
  267. Array.prototype.forEach.call(io.files, function(io, i) {
  268. if(self.media && self.media.length + i >= self.config.uploader.album_limit) {
  269. swal('Error', 'You can only upload ' + self.config.uploader.album_limit + ' photos per album', 'error');
  270. self.uploading = false;
  271. self.page = 2;
  272. return;
  273. }
  274. let type = io.type;
  275. let validated = $.inArray(type, self.mimes);
  276. if(validated == -1) {
  277. swal('Invalid File Type', 'The file you are trying to add is not a valid mime type. Please upload a '+self.mimes+' only.', 'error');
  278. self.uploading = false;
  279. self.page = 'error';
  280. return;
  281. }
  282. let form = new FormData();
  283. form.append('file', io);
  284. let xhrConfig = {
  285. onUploadProgress: function(e) {
  286. let progress = Math.floor( (e.loaded * 100) / e.total );
  287. self.uploadProgress = progress;
  288. }
  289. };
  290. io.value = null;
  291. axios.post('/api/stories/v0/add', form, xhrConfig)
  292. .then(function(e) {
  293. self.uploadProgress = 100;
  294. self.uploading = false;
  295. self.mediaUrl = e.data.media_url;
  296. self.mediaId = e.data.media_id;
  297. self.page = e.data.media_type === 'video' ? 'preview' : 'crop';
  298. // window.location.href = '/i/my/story';
  299. }).catch(function(e) {
  300. self.uploading = false;
  301. io.value = null;
  302. let msg = e.response.data.message ? e.response.data.message : 'Something went wrong.'
  303. swal('Oops!', msg, 'warning');
  304. self.page = 'error';
  305. });
  306. self.uploadProgress = 0;
  307. });
  308. },
  309. expiresTimestamp(ts) {
  310. ts = new Date(ts * 1000);
  311. return ts.toDateString() + ' ' + ts.toLocaleTimeString();
  312. },
  313. edit() {
  314. this.page = 'edit';
  315. },
  316. showLightbox(story) {
  317. this.lightboxMedia = {
  318. url: story.src
  319. }
  320. this.$refs.lightboxModal.show();
  321. },
  322. deleteStory(story, index) {
  323. if(window.confirm('Are you sure you want to delete this Story?') != true) {
  324. return;
  325. }
  326. axios.delete('/api/stories/v0/delete/' + story.id)
  327. .then(res => {
  328. this.stories.splice(index, 1);
  329. if(this.stories.length == 0) {
  330. window.location.href = '/i/stories/new';
  331. }
  332. });
  333. },
  334. navigateTo(path = '/') {
  335. window.location.href = path;
  336. },
  337. goBack() {
  338. this.page = 'landing';
  339. },
  340. performCrop() {
  341. this.page = 'cropping';
  342. let data = this.$refs.croppa.getData();
  343. axios.post('/api/stories/v0/crop', {
  344. media_id: this.mediaId,
  345. width: data.width,
  346. height: data.height,
  347. x: data.x,
  348. y: data.y
  349. }).then(res => {
  350. this.page = 'preview';
  351. });
  352. },
  353. deleteCurrentStory() {
  354. let story = {
  355. id: this.mediaId
  356. };
  357. this.deleteStory(story);
  358. this.page = 'landing';
  359. },
  360. shareStoryToFollowers() {
  361. axios.post('/api/stories/v0/publish', {
  362. media_id: this.mediaId,
  363. duration: this.duration
  364. }).then(res => {
  365. window.location.href = '/i/my/story?id=' + this.mediaId;
  366. })
  367. },
  368. viewMyStory() {
  369. window.location.href = '/i/my/story';
  370. }
  371. }
  372. }
  373. </script>