StoryCompose.vue 13 KB

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