CollectionComponent.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <template>
  2. <div class="w-100 h-100">
  3. <div v-if="!loaded" style="height: 80vh;" class="d-flex justify-content-center align-items-center">
  4. <img src="/img/pixelfed-icon-grey.svg" class="">
  5. </div>
  6. <div class="row mt-3" v-if="loaded">
  7. <div class="col-12 p-0 mb-3">
  8. <picture class="d-flex align-items-center justify-content-center">
  9. <div class="dims"></div>
  10. <div style="z-index:500;position: absolute;" class="text-white">
  11. <p class="display-4 text-center pt-3">{{title || 'Untitled Collection'}}</p>
  12. <p class="lead text-center mb-3">{{description}}</p>
  13. <p class="text-center">
  14. {{posts.length}} photos · by <a :href="'/' + profileUsername" class="font-weight-bold text-white">{{profileUsername}}</a>
  15. </p>
  16. <p v-if="owner == true" class="pt-3 text-center">
  17. <span>
  18. <button class="btn btn-outline-light btn-sm" @click.prevent="addToCollection">
  19. <span v-if="loadingPostList == false">Add Photo</span>
  20. <span v-else class="px-4">
  21. <div class="spinner-border spinner-border-sm" role="status">
  22. <span class="sr-only">Loading...</span>
  23. </div>
  24. </span>
  25. </button>
  26. &nbsp; &nbsp;
  27. <button class="btn btn-outline-light btn-sm" @click.prevent="editCollection">Edit</button>
  28. &nbsp; &nbsp;
  29. <button class="btn btn-outline-light btn-sm" @click.prevent="deleteCollection">Delete</button>
  30. </span>
  31. </p>
  32. </div>
  33. <img :src="previewUrl(posts[0])"
  34. alt=""
  35. style="width:100%; height: 600px; object-fit: cover;"
  36. >
  37. </picture>
  38. </div>
  39. <div class="col-12 p-0">
  40. <masonry
  41. :cols="{default: 2, 700: 2, 400: 1}"
  42. :gutter="{default: '5px'}"
  43. >
  44. <div v-for="(s, index) in posts">
  45. <a class="card info-overlay card-md-border-0 mb-1" :href="s.url">
  46. <img :src="previewUrl(s)" class="img-fluid w-100">
  47. </a>
  48. </div>
  49. </masonry>
  50. </div>
  51. </div>
  52. <b-modal ref="editModal" id="edit-modal" hide-footer centered title="Edit Collection" body-class="">
  53. <form>
  54. <div class="form-group">
  55. <label for="title" class="font-weight-bold text-muted">Title</label>
  56. <input type="text" class="form-control" id="title" placeholder="Untitled Collection" v-model="title">
  57. </div>
  58. <div class="form-group">
  59. <label for="description" class="font-weight-bold text-muted">Description</label>
  60. <textarea class="form-control" id="description" placeholder="Add a description here ..." v-model="description" rows="3"></textarea>
  61. </div>
  62. <div class="form-group">
  63. <label for="visibility" class="font-weight-bold text-muted">Visibility</label>
  64. <select class="custom-select" v-model="visibility">
  65. <option value="public">Public</option>
  66. <option value="private">Followers Only</option>
  67. </select>
  68. </div>
  69. <button type="button" class="btn btn-primary btn-sm py-1 font-weight-bold px-3 float-right" @click.prevent="updateCollection">Save</button>
  70. </form>
  71. </b-modal>
  72. <b-modal ref="addPhotoModal" id="add-photo-modal" hide-footer centered title="Add Photo" body-class="m-3">
  73. <div class="form-group">
  74. <label for="title" class="font-weight-bold text-muted">Add Recent Post</label>
  75. <div class="row m-1" v-if="postsList.length > 0">
  76. <div v-for="(p, index) in postsList" :key="'postList-'+index" class="col-4 p-1 cursor-pointer">
  77. <div class="square">
  78. <div class="square-content" v-bind:style="'background-image: url(' + p.media_attachments[0].url + ');'"></div>
  79. </div>
  80. </div>
  81. <div class="col-12">
  82. <hr>
  83. </div>
  84. </div>
  85. </div>
  86. <form>
  87. <div class="form-group">
  88. <label for="title" class="font-weight-bold text-muted">Add Post by URL</label>
  89. <input type="text" class="form-control" placeholder="https://pixelfed.dev/p/admin/1" v-model="photoId">
  90. <p class="help-text small text-muted">Only local, public posts can be added</p>
  91. </div>
  92. <button type="button" class="btn btn-primary btn-sm py-1 font-weight-bold px-3 float-right" @click.prevent="pushId">Add Photo</button>
  93. </form>
  94. </b-modal>
  95. </div>
  96. </template>
  97. <style type="text/css" scoped>
  98. .dims {
  99. position: absolute;
  100. top: 0;
  101. right: 0;
  102. bottom: 0;
  103. left: 0;
  104. background: rgba(0,0,0,.68);
  105. z-index: 300;
  106. }
  107. </style>
  108. <script type="text/javascript">
  109. import VueMasonry from 'vue-masonry-css'
  110. Vue.use(VueMasonry);
  111. export default {
  112. props: [
  113. 'collection-id',
  114. 'collection-title',
  115. 'collection-description',
  116. 'collection-visibility',
  117. 'profile-id',
  118. 'profile-username'
  119. ],
  120. data() {
  121. return {
  122. loaded: false,
  123. posts: [],
  124. ids: [],
  125. currentUser: false,
  126. owner: false,
  127. title: this.collectionTitle,
  128. description: this.collectionDescription,
  129. visibility: this.collectionVisibility,
  130. photoId: '',
  131. postsList: [],
  132. loadingPostList: false
  133. }
  134. },
  135. beforeMount() {
  136. this.fetchCurrentUser();
  137. this.fetchItems();
  138. },
  139. mounted() {
  140. },
  141. methods: {
  142. fetchCurrentUser() {
  143. if(document.querySelectorAll('body')[0].classList.contains('loggedIn') == true) {
  144. axios.get('/api/pixelfed/v1/accounts/verify_credentials').then(res => {
  145. this.currentUser = res.data;
  146. this.owner = this.currentUser.id == this.profileId;
  147. });
  148. }
  149. },
  150. fetchItems() {
  151. axios.get('/api/local/collection/items/' + this.collectionId)
  152. .then(res => {
  153. this.posts = res.data;
  154. this.ids = this.posts.map(p => {
  155. return p.id;
  156. });
  157. this.loaded = true;
  158. });
  159. },
  160. previewUrl(status) {
  161. return status.sensitive ? '/storage/no-preview.png?v=' + new Date().getTime() : status.media_attachments[0].preview_url;
  162. },
  163. previewBackground(status) {
  164. let preview = this.previewUrl(status);
  165. return 'background-image: url(' + preview + ');';
  166. },
  167. addToCollection() {
  168. let self = this;
  169. this.loadingPostList = true;
  170. if(this.postsList.length == 0) {
  171. axios.get('/api/pixelfed/v1/accounts/'+this.profileId+'/statuses', {
  172. params: {
  173. min_id: 1,
  174. limit: 13
  175. }
  176. })
  177. .then(res => {
  178. self.postsList = res.data.filter(l => {
  179. return self.ids.indexOf(l.id) == -1;
  180. }).splice(0,9);
  181. self.loadingPostList = false;
  182. self.$refs.addPhotoModal.show();
  183. }).catch(err => {
  184. self.loadingPostList = false;
  185. swal('An Error Occured', 'We cannot process your request at this time, please try again later.', 'error');
  186. })
  187. } else {
  188. this.$refs.addPhotoModal.show();
  189. this.loadingPostList = false;
  190. }
  191. },
  192. pushId() {
  193. let max = 18;
  194. let self = this;
  195. if(this.posts.length >= max) {
  196. swal('Error', 'You can only add ' + max + ' posts per collection', 'error');
  197. return;
  198. }
  199. let url = this.photoId;
  200. let origin = window.location.origin;
  201. let split = url.split('/');
  202. if(url.slice(0, origin.length) !== origin) {
  203. swal('Invalid URL', 'You can only add posts from this instance', 'error');
  204. this.photoId = '';
  205. }
  206. if(url.slice(0, origin.length + 3) !== origin + '/p/' || split.length !== 6) {
  207. swal('Invalid URL', 'Invalid URL', 'error');
  208. this.photoId = '';
  209. }
  210. axios.post('/api/local/collection/item', {
  211. collection_id: this.collectionId,
  212. post_id: split[5]
  213. }).then(res => {
  214. self.ids.push(...split[5]);
  215. }).catch(err => {
  216. swal('Invalid URL', 'The post you entered was invalid', 'error');
  217. this.photoId = '';
  218. });
  219. },
  220. editCollection() {
  221. this.$refs.editModal.show();
  222. },
  223. deleteCollection() {
  224. if(this.owner == false) {
  225. return;
  226. }
  227. let confirmed = window.confirm('Are you sure you want to delete this collection?');
  228. if(confirmed) {
  229. axios.delete('/api/local/collection/' + this.collectionId)
  230. .then(res => {
  231. window.location.href = '/';
  232. });
  233. } else {
  234. return;
  235. }
  236. },
  237. updateCollection() {
  238. this.$refs.editModal.hide();
  239. axios.post('/api/local/collection/' + this.collectionId, {
  240. title: this.title,
  241. description: this.description,
  242. visibility: this.visibility
  243. }).then(res => {
  244. console.log(res.data);
  245. });
  246. }
  247. }
  248. }
  249. </script>