CollectionComponent.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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" onclick="this.blur();">
  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" onclick="this.blur();">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. <div class="d-flex justify-content-between align-items-center pt-3">
  70. <a
  71. class="text-primary font-weight-bold text-decoration-none"
  72. href="#"
  73. @click.prevent="showEditPhotosModal">
  74. Edit Photos
  75. </a>
  76. <div v-if="collection.published_at">
  77. <button
  78. type="button"
  79. class="btn btn-primary btn-sm py-1 font-weight-bold px-3 float-right"
  80. @click.prevent="updateCollection">
  81. Save
  82. </button>
  83. </div>
  84. <div v-else class="float-right">
  85. <button
  86. type="button"
  87. class="btn btn-outline-primary btn-sm py-1 font-weight-bold px-3"
  88. @click.prevent="publishCollection">
  89. Publish
  90. </button>
  91. <button
  92. type="button"
  93. class="btn btn-primary btn-sm py-1 font-weight-bold px-3"
  94. @click.prevent="updateCollection">
  95. Save
  96. </button>
  97. </div>
  98. </div>
  99. </form>
  100. </b-modal>
  101. <b-modal ref="addPhotoModal" id="add-photo-modal" hide-footer centered title="Add Photo" body-class="m-3">
  102. <div class="form-group">
  103. <label for="title" class="font-weight-bold text-muted">Add Recent Post</label>
  104. <div class="row m-1" v-if="postsList.length > 0">
  105. <div v-for="(p, index) in postsList" :key="'postList-'+index" class="col-4 p-1 cursor-pointer" @click="addRecentId(p)">
  106. <div class="square">
  107. <div class="square-content" v-bind:style="'background-image: url(' + p.media_attachments[0].url + ');'"></div>
  108. </div>
  109. </div>
  110. <div class="col-12">
  111. <hr>
  112. </div>
  113. </div>
  114. </div>
  115. <form>
  116. <div class="form-group">
  117. <label for="title" class="font-weight-bold text-muted">Add Post by URL</label>
  118. <input type="text" class="form-control" placeholder="https://pixelfed.dev/p/admin/1" v-model="photoId">
  119. <p class="help-text small text-muted">Only local, public posts can be added</p>
  120. </div>
  121. <button type="button" class="btn btn-primary btn-sm py-1 font-weight-bold px-3 float-right" @click.prevent="pushId">
  122. <span v-if="addingPostToCollection" class="px-4">
  123. <div class="spinner-border spinner-border-sm" role="status">
  124. <span class="sr-only">Loading...</span>
  125. </div>
  126. </span>
  127. <span v-else>
  128. Add Photo
  129. </span>
  130. </button>
  131. </form>
  132. </b-modal>
  133. <b-modal ref="editPhotosModal" id="edit-photos-modal" hide-footer centered title="Edit Collection Photos" body-class="m-3">
  134. <div class="form-group">
  135. <p class="font-weight-bold text-dark text-center">Select a Photo to Delete</p>
  136. <div class="row m-1 scrollbar-hidden" v-if="posts.length > 0" style="max-height: 350px;overflow-y: auto;">
  137. <div v-for="(p, index) in posts" :key="'plm-'+index" class="col-4 p-1 cursor-pointer">
  138. <div :class="[markedForDeletion.indexOf(p.id) == -1 ? 'square' : 'square delete-border']" @click="markPhotoForDeletion(p.id)">
  139. <div class="square-content" v-bind:style="'background-image: url(' + p.media_attachments[0].url + ');'"></div>
  140. </div>
  141. </div>
  142. </div>
  143. <div v-show="markedForDeletion.length > 0">
  144. <button type="button" @click.prevent="confirmDeletion" class="btn btn-primary font-weight-bold py-0 btn-block mb-0 mt-4">Delete {{markedForDeletion.length}} {{markedForDeletion.length == 1 ? 'photo':'photos'}}</button>
  145. </div>
  146. </div>
  147. </b-modal>
  148. </div>
  149. </template>
  150. <style type="text/css" scoped>
  151. .dims {
  152. position: absolute;
  153. top: 0;
  154. right: 0;
  155. bottom: 0;
  156. left: 0;
  157. background: rgba(0,0,0,.68);
  158. z-index: 300;
  159. }
  160. .scrollbar-hidden::-webkit-scrollbar {
  161. display: none;
  162. }
  163. .delete-border {
  164. border: 4px solid #ff0000;
  165. }
  166. .delete-border .square-content {
  167. background-color: red;
  168. background-blend-mode: screen;
  169. }
  170. </style>
  171. <script type="text/javascript">
  172. import VueMasonry from 'vue-masonry-css'
  173. Vue.use(VueMasonry);
  174. export default {
  175. props: [
  176. 'collection-id',
  177. 'collection-title',
  178. 'collection-description',
  179. 'collection-visibility',
  180. 'profile-id',
  181. 'profile-username'
  182. ],
  183. data() {
  184. return {
  185. collection: {},
  186. config: window.App.config,
  187. loaded: false,
  188. posts: [],
  189. ids: [],
  190. currentUser: false,
  191. owner: false,
  192. title: this.collectionTitle,
  193. description: this.collectionDescription,
  194. visibility: this.collectionVisibility,
  195. photoId: '',
  196. postsList: [],
  197. loadingPostList: false,
  198. addingPostToCollection: false,
  199. markedForDeletion: []
  200. }
  201. },
  202. beforeMount() {
  203. this.fetchCollection();
  204. },
  205. mounted() {
  206. },
  207. methods: {
  208. fetchCollection() {
  209. axios.get('/api/local/collection/' + this.collectionId)
  210. .then(res => {
  211. this.collection = res.data;
  212. this.fetchCurrentUser();
  213. })
  214. },
  215. fetchCurrentUser() {
  216. if(document.querySelectorAll('body')[0].classList.contains('loggedIn') == true) {
  217. axios.get('/api/pixelfed/v1/accounts/verify_credentials').then(res => {
  218. this.currentUser = res.data;
  219. this.owner = this.currentUser.id == this.profileId;
  220. window._sharedData.curUser = res.data;
  221. window.App.util.navatar();
  222. this.fetchItems();
  223. });
  224. } else {
  225. this.fetchItems();
  226. }
  227. },
  228. fetchItems() {
  229. axios.get('/api/local/collection/items/' + this.collectionId)
  230. .then(res => {
  231. this.posts = res.data;
  232. this.ids = this.posts.map(p => {
  233. return p.id;
  234. });
  235. this.loaded = true;
  236. });
  237. },
  238. previewUrl(status) {
  239. return status.sensitive ? '/storage/no-preview.png?v=' + new Date().getTime() : status.media_attachments[0].preview_url;
  240. },
  241. previewBackground(status) {
  242. let preview = this.previewUrl(status);
  243. return 'background-image: url(' + preview + ');';
  244. },
  245. addToCollection() {
  246. let self = this;
  247. this.loadingPostList = true;
  248. if(this.postsList.length == 0) {
  249. axios.get('/api/pixelfed/v1/accounts/'+this.profileId+'/statuses', {
  250. params: {
  251. min_id: 1,
  252. limit: 13
  253. }
  254. })
  255. .then(res => {
  256. self.postsList = res.data.filter(l => {
  257. return self.ids.indexOf(l.id) == -1;
  258. }).splice(0,9);
  259. self.loadingPostList = false;
  260. self.$refs.addPhotoModal.show();
  261. }).catch(err => {
  262. self.loadingPostList = false;
  263. swal('An Error Occured', 'We cannot process your request at this time, please try again later.', 'error');
  264. })
  265. } else {
  266. this.$refs.addPhotoModal.show();
  267. this.loadingPostList = false;
  268. }
  269. },
  270. pushId() {
  271. let max = this.config.uploader.max_collection_length;
  272. let addingPostToCollection = true;
  273. let self = this;
  274. if(this.posts.length >= max) {
  275. swal('Error', 'You can only add ' + max + ' posts per collection', 'error');
  276. return;
  277. }
  278. let url = this.photoId;
  279. let origin = window.location.origin;
  280. let split = url.split('/');
  281. if(url.slice(0, origin.length) !== origin) {
  282. swal('Invalid URL', 'You can only add posts from this instance', 'error');
  283. this.photoId = '';
  284. }
  285. if(url.slice(0, origin.length + 3) !== origin + '/p/' || split.length !== 6) {
  286. swal('Invalid URL', 'Invalid URL', 'error');
  287. this.photoId = '';
  288. }
  289. axios.post('/api/local/collection/item', {
  290. collection_id: this.collectionId,
  291. post_id: split[5]
  292. }).then(res => {
  293. self.ids.push(...split[5]);
  294. }).catch(err => {
  295. swal('Invalid URL', 'The post you entered was invalid', 'error');
  296. this.photoId = '';
  297. });
  298. self.$refs.addPhotoModal.hide();
  299. window.location.reload();
  300. },
  301. editCollection() {
  302. this.$refs.editModal.show();
  303. },
  304. deleteCollection() {
  305. if(this.owner == false) {
  306. return;
  307. }
  308. let confirmed = window.confirm('Are you sure you want to delete this collection?');
  309. if(confirmed) {
  310. axios.delete('/api/local/collection/' + this.collectionId)
  311. .then(res => {
  312. window.location.href = '/';
  313. });
  314. } else {
  315. return;
  316. }
  317. },
  318. publishCollection() {
  319. if(this.owner == false) {
  320. return;
  321. }
  322. let confirmed = window.confirm('Are you sure you want to publish this collection?');
  323. if(confirmed) {
  324. axios.post('/api/local/collection/' + this.collectionId + '/publish', {
  325. title: this.title,
  326. description: this.description,
  327. visibility: this.visibility
  328. })
  329. .then(res => {
  330. window.location.href = '/';
  331. });
  332. } else {
  333. return;
  334. }
  335. },
  336. updateCollection() {
  337. this.$refs.editModal.hide();
  338. axios.post('/api/local/collection/' + this.collectionId, {
  339. title: this.title,
  340. description: this.description,
  341. visibility: this.visibility
  342. }).then(res => {
  343. console.log(res.data);
  344. });
  345. },
  346. showEditPhotosModal() {
  347. this.$refs.editModal.hide();
  348. this.$refs.editPhotosModal.show();
  349. },
  350. markPhotoForDeletion(id) {
  351. this.markedForDeletion.indexOf(id) == -1 ?
  352. this.markedForDeletion.push(id) :
  353. this.markedForDeletion = this.markedForDeletion.filter(d => {
  354. return d != id;
  355. });
  356. },
  357. confirmDeletion() {
  358. let self = this;
  359. let confirmed = window.confirm('Are you sure you want to delete this?');
  360. if(confirmed) {
  361. this.markedForDeletion.forEach(mfd => {
  362. axios.delete('/api/local/collection/item', {
  363. params: {
  364. collection_id: self.collectionId,
  365. post_id: mfd
  366. }
  367. })
  368. .then(res => {
  369. self.removeItem(mfd);
  370. this.$refs.editPhotosModal.hide();
  371. })
  372. .catch(err => {
  373. swal(
  374. 'Oops!',
  375. 'An error occured with your request, please try again later.',
  376. 'error'
  377. );
  378. })
  379. });
  380. this.markedForDeletion = [];
  381. }
  382. },
  383. removeItem(id) {
  384. this.posts = this.posts.filter(post => {
  385. return post.id != id;
  386. });
  387. },
  388. addRecentId(post) {
  389. let self = this;
  390. axios.post('/api/local/collection/item', {
  391. collection_id: self.collectionId,
  392. post_id: post.id
  393. }).then(res => {
  394. window.location.reload();
  395. }).catch(err => {
  396. swal('Oops!', 'An error occured, please try selecting another post.', 'error');
  397. this.photoId = '';
  398. });
  399. }
  400. }
  401. }
  402. </script>