StoryCompose.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <template>
  2. <div class="container mt-2 mt-md-5">
  3. <input type="file" id="pf-dz" name="media" class="d-none file-input" v-bind:accept="config.mimes">
  4. <div class="row">
  5. <div class="col-12 col-md-6 offset-md-3">
  6. <!-- LANDING -->
  7. <div v-if="page == 'landing'" class="card card-body bg-transparent border-0 shadow-none d-flex justify-content-center" style="height: 90vh;">
  8. <div class="text-center flex-fill mt-5 pt-5">
  9. <img src="/img/pixelfed-icon-grey.svg" width="60px" height="60px">
  10. <p class="font-weight-bold lead text-lighter mt-1">Stories</p>
  11. </div>
  12. <div class="flex-fill">
  13. <div class="card w-100 shadow-none">
  14. <div class="list-group">
  15. <a class="list-group-item text-center lead text-decoration-none text-dark" href="#" @click.prevent="upload()">Add Photo</a>
  16. <a v-if="stories.length" class="list-group-item text-center lead text-decoration-none text-dark" href="#" @click.prevent="edit()">Edit Story</a>
  17. </div>
  18. </div>
  19. </div>
  20. <div class="text-center flex-fill">
  21. <p class="text-lighter small text-uppercase">
  22. <a href="/" class="text-muted font-weight-bold">Home</a>
  23. <span class="px-2 text-lighter">|</span>
  24. <a href="/i/my/story" class="text-muted font-weight-bold">View My Story</a>
  25. <span class="px-2 text-lighter">|</span>
  26. <a href="/site/help" class="text-muted font-weight-bold">Help</a>
  27. </p>
  28. </div>
  29. </div>
  30. <!-- CROP -->
  31. <div v-if="page == 'crop'" class="card card-body bg-transparent border-0 shadow-none d-flex justify-content-center" style="height: 95vh;">
  32. <div class="text-center pt-5 mb-3 d-flex justify-content-between align-items-center">
  33. <div>
  34. <button class="btn btn-outline-lighter btn-sm py-0 px-md-3"><i class="pr-2 fas fa-chevron-left fa-sm"></i> Delete</button>
  35. </div>
  36. <div class="d-flex align-items-center">
  37. <img class="d-inline-block mr-2" src="/img/pixelfed-icon-grey.svg" width="30px" height="30px">
  38. <span class="font-weight-bold lead text-lighter">Stories</span>
  39. </div>
  40. <div>
  41. <button class="btn btn-outline-success btn-sm py-0 px-md-3">Crop <i class="pl-2 fas fa-chevron-right fa-sm"></i></button>
  42. </div>
  43. </div>
  44. <div class="flex-fill">
  45. <div class="card w-100 mt-3">
  46. <div class="card-body p-0">
  47. <vue-cropper
  48. ref="cropper"
  49. :relativeZoom="cropper.zoom"
  50. :aspectRatio="cropper.aspectRatio"
  51. :viewMode="cropper.viewMode"
  52. :zoomable="cropper.zoomable"
  53. :rotatable="true"
  54. :src="mediaUrl"
  55. >
  56. </vue-cropper>
  57. </div>
  58. </div>
  59. </div>
  60. <div class="text-center flex-fill">
  61. <p class="text-lighter small text-uppercase pt-2">
  62. <!-- <a href="#" class="text-muted font-weight-bold">Home</a>
  63. <span class="px-2 text-lighter">|</span>
  64. <a href="#" class="text-muted font-weight-bold">View My Story</a>
  65. <span class="px-2 text-lighter">|</span> -->
  66. <a href="/site/help" class="text-muted font-weight-bold mb-0">Help</a>
  67. </p>
  68. </div>
  69. </div>
  70. <!-- ERROR -->
  71. <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;">
  72. <p class="h3 mb-0">Oops!</p>
  73. <p class="text-muted lead">An error occurred, please try again later.</p>
  74. <p class="text-muted mb-0">
  75. <a class="btn btn-outline-secondary py-0 px-5 font-weight-bold" href="/">Go back</a>
  76. </p>
  77. </div>
  78. <div v-if="page == 'edit'" class="card card-body bg-transparent border-0 shadow-none d-flex justify-content-center" style="height: 90vh;">
  79. <div class="text-center flex-fill mt-5 pt-5">
  80. <img src="/img/pixelfed-icon-grey.svg" width="60px" height="60px">
  81. <p class="font-weight-bold lead text-lighter mt-1">Stories</p>
  82. </div>
  83. <div class="flex-fill py-5">
  84. <div class="card w-100 shadow-none" style="max-height: 500px; overflow-y: auto">
  85. <div class="list-group">
  86. <div v-for="(story, index) in stories" class="list-group-item text-center text-dark" href="#">
  87. <div class="media align-items-center">
  88. <img :src="story.src" class="img-fluid mr-3 cursor-pointer" width="70px" height="70px" @click="showLightbox(story)">
  89. <div class="media-body">
  90. <p class="mb-0">Expires</p>
  91. <p class="mb-0 text-muted small"><span>{{expiresTimestamp(story.expires_at)}}</span></p>
  92. </div>
  93. <div class="float-right">
  94. <button @click="deleteStory(story, index)" class="btn btn-danger btn-sm font-weight-bold text-uppercase">Delete</button>
  95. </div>
  96. </div>
  97. </div>
  98. </div>
  99. </div>
  100. </div>
  101. <div class="flex-fill text-center">
  102. <a class="btn btn-outline-secondary py-0 px-5 font-weight-bold" href="/i/stories/new">Go back</a>
  103. </div>
  104. </div>
  105. </div>
  106. </div>
  107. <b-modal
  108. id="lightbox"
  109. ref="lightboxModal"
  110. hide-header
  111. hide-footer
  112. centered
  113. size="lg"
  114. body-class="p-0"
  115. >
  116. <div v-if="lightboxMedia" class="w-100 h-100">
  117. <img :src="lightboxMedia.url" style="max-height: 100%; max-width: 100%">
  118. </div>
  119. </b-modal>
  120. </div>
  121. </template>
  122. <style type="text/css" scoped>
  123. </style>
  124. <script type="text/javascript">
  125. import VueTimeago from 'vue-timeago';
  126. import VueCropper from 'vue-cropperjs';
  127. import 'cropperjs/dist/cropper.css';
  128. export default {
  129. components: {
  130. VueCropper,
  131. VueTimeago
  132. },
  133. props: ['profile-id'],
  134. data() {
  135. return {
  136. config: window.App.config,
  137. mimes: [
  138. 'image/jpeg',
  139. 'image/png'
  140. ],
  141. page: 'landing',
  142. pages: [
  143. 'landing',
  144. 'crop',
  145. 'edit',
  146. 'confirm',
  147. 'error'
  148. ],
  149. uploading: false,
  150. uploadProgress: 100,
  151. cropper: {
  152. aspectRatio: 9/16,
  153. viewMode: 1,
  154. zoomable: true,
  155. zoom: null
  156. },
  157. mediaUrl: null,
  158. stories: [],
  159. lightboxMedia: false,
  160. };
  161. },
  162. mounted() {
  163. this.mediaWatcher();
  164. axios.get('/api/stories/v1/fetch/' + this.profileId)
  165. .then(res => this.stories = res.data);
  166. },
  167. methods: {
  168. upload() {
  169. let fi = $('.file-input[name="media"]');
  170. fi.trigger('click');
  171. },
  172. mediaWatcher() {
  173. let self = this;
  174. $(document).on('change', '#pf-dz', function(e) {
  175. self.triggerUpload();
  176. });
  177. },
  178. triggerUpload() {
  179. let self = this;
  180. self.uploading = true;
  181. let io = document.querySelector('#pf-dz');
  182. Array.prototype.forEach.call(io.files, function(io, i) {
  183. if(self.media && self.media.length + i >= self.config.uploader.album_limit) {
  184. swal('Error', 'You can only upload ' + self.config.uploader.album_limit + ' photos per album', 'error');
  185. self.uploading = false;
  186. self.page = 2;
  187. return;
  188. }
  189. let type = io.type;
  190. let validated = $.inArray(type, self.mimes);
  191. if(validated == -1) {
  192. swal('Invalid File Type', 'The file you are trying to add is not a valid mime type. Please upload a '+self.mimes+' only.', 'error');
  193. self.uploading = false;
  194. self.page = 'error';
  195. return;
  196. }
  197. let form = new FormData();
  198. form.append('file', io);
  199. let xhrConfig = {
  200. onUploadProgress: function(e) {
  201. let progress = Math.round( (e.loaded * 100) / e.total );
  202. self.uploadProgress = progress;
  203. }
  204. };
  205. axios.post('/api/stories/v1/add', form, xhrConfig)
  206. .then(function(e) {
  207. self.uploadProgress = 100;
  208. self.uploading = false;
  209. window.location.href = '/i/my/story';
  210. self.mediaUrl = e.data.media_url;
  211. }).catch(function(e) {
  212. self.uploading = false;
  213. io.value = null;
  214. let msg = e.response.data.message ? e.response.data.message : 'Something went wrong.'
  215. swal('Oops!', msg, 'warning');
  216. });
  217. io.value = null;
  218. self.uploadProgress = 0;
  219. });
  220. },
  221. expiresTimestamp(ts) {
  222. ts = new Date(ts * 1000);
  223. return ts.toDateString() + ' ' + ts.toLocaleTimeString();
  224. },
  225. edit() {
  226. this.page = 'edit';
  227. },
  228. showLightbox(story) {
  229. this.lightboxMedia = {
  230. url: story.src
  231. }
  232. this.$refs.lightboxModal.show();
  233. },
  234. deleteStory(story, index) {
  235. if(window.confirm('Are you sure you want to delete this Story?') != true) {
  236. return;
  237. }
  238. axios.delete('/api/stories/v1/delete/' + story.id)
  239. .then(res => {
  240. this.stories.splice(index, 1);
  241. if(this.stories.length == 0) {
  242. window.location.href = '/i/stories/new';
  243. }
  244. });
  245. }
  246. }
  247. }
  248. </script>