CollectionComponent.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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. <div v-if="owner && !collection.published_at">
  9. <div class="alert alert-danger d-flex justify-content-center">
  10. <div class="media align-items-center">
  11. <i class="far fa-exclamation-triangle fa-3x mr-3"></i>
  12. <div class="media-body">
  13. <p class="font-weight-bold mb-0">
  14. This collection is unpublished.
  15. </p>
  16. <p class="small mb-0">
  17. This collection is not visible to anyone else until you publish it. <br />
  18. To publish, click on the <strong>Edit</strong> button and then click on the <strong>Publish</strong> button.
  19. </p>
  20. </div>
  21. </div>
  22. </div>
  23. </div>
  24. </div>
  25. <div class="col-12 p-0 mb-3">
  26. <picture class="d-flex align-items-center justify-content-center">
  27. <div class="dims"></div>
  28. <div style="z-index:500;position: absolute;" class="text-white">
  29. <p class="display-4 text-center pt-3">{{title || 'Untitled Collection'}}</p>
  30. <p class="lead text-center mb-3">{{description}}</p>
  31. <p class="text-center">
  32. <span v-if="owner && collection.visibility != 'public'">
  33. <span
  34. v-if="collection.visibility == 'draft'"
  35. class="btn btn-outline-light btn-sm text-capitalize py-0"
  36. style="font-size: 10px"
  37. >
  38. <i class="far fa-lock"></i> Draft
  39. </span>
  40. <span
  41. v-else-if="collection.visibility == 'private'"
  42. class="btn btn-outline-light btn-sm text-capitalize py-0"
  43. style="font-size: 10px"
  44. >
  45. Followers Only
  46. </span>
  47. <span>·</span>
  48. </span>
  49. <span>{{collection.post_count}} photos</span>
  50. <span>·</span>
  51. <span>by <a :href="'/' + profileUsername" class="font-weight-bold text-white">{{profileUsername}}</a></span>
  52. </p>
  53. <p v-if="owner == true" class="pt-3 text-center">
  54. <span>
  55. <button class="btn btn-outline-light btn-sm" @click.prevent="addToCollection" onclick="this.blur();">
  56. <span v-if="loadingPostList == false">Add Photo</span>
  57. <span v-else class="px-4">
  58. <div class="spinner-border spinner-border-sm" role="status">
  59. <span class="sr-only">Loading...</span>
  60. </div>
  61. </span>
  62. </button>
  63. &nbsp; &nbsp;
  64. <button class="btn btn-outline-light btn-sm" @click.prevent="editCollection" onclick="this.blur();">Edit</button>
  65. &nbsp; &nbsp;
  66. <button class="btn btn-outline-light btn-sm" @click.prevent="deleteCollection">Delete</button>
  67. </span>
  68. </p>
  69. </div>
  70. <img
  71. v-if="posts && posts.length"
  72. :src="previewUrl(posts[0])"
  73. alt=""
  74. style="width:100%; height: 400px; object-fit: cover;"
  75. >
  76. <div v-else class="bg-info" style="width:100%; height: 400px;"></div>
  77. </picture>
  78. </div>
  79. <div class="col-12 p-0">
  80. <!-- <masonry
  81. :cols="{default: 2, 700: 2, 400: 1}"
  82. :gutter="{default: '5px'}"
  83. > -->
  84. <div v-if="posts && posts.length > 0" class="row px-3 px-md-0">
  85. <div v-for="(s, index) in posts" class="col-6 col-md-4 feed">
  86. <!-- <a class="card info-overlay card-md-border-0 mb-4 square" :href="s.url">
  87. <img :src="previewUrl(s)" class="square-content w-100" style="object-fit: cover;">
  88. </a> -->
  89. <a v-if="s.hasOwnProperty('pf_type') && s.pf_type == 'video'" class="card info-overlay card-md-border-0" :href="statusUrl(s)">
  90. <div class="square">
  91. <div class="square-content">
  92. <div class="info-overlay-text-label rounded">
  93. <h5 class="text-white m-auto font-weight-bold">
  94. <span>
  95. <span class="far fa-video fa-2x p-2 d-flex-inline"></span>
  96. </span>
  97. </h5>
  98. </div>
  99. <blur-hash-canvas
  100. width="32"
  101. height="32"
  102. class="rounded"
  103. :hash="s.media_attachments[0].blurhash">
  104. </blur-hash-canvas>
  105. </div>
  106. </div>
  107. </a>
  108. <a v-else-if="s.sensitive" class="card info-overlay card-md-border-0" :href="statusUrl(s)">
  109. <div class="square">
  110. <div class="square-content">
  111. <div class="info-overlay-text-label rounded">
  112. <h5 class="text-white m-auto font-weight-bold">
  113. <span>
  114. <span class="far fa-eye-slash fa-lg p-2 d-flex-inline"></span>
  115. </span>
  116. </h5>
  117. </div>
  118. <blur-hash-canvas
  119. width="32"
  120. height="32"
  121. class="rounded"
  122. :hash="s.media_attachments[0].blurhash">
  123. </blur-hash-canvas>
  124. </div>
  125. </div>
  126. </a>
  127. <a v-else class="card info-overlay card-md-border-0" :href="statusUrl(s)">
  128. <div class="square">
  129. <div class="square-content">
  130. <!-- <img :src="previewUrl(s)" class="img-fluid w-100 rounded-lg" onerror="this.onerror=null;this.src='/storage/no-preview.png?v=0'">
  131. <span class="badge badge-light" style="position: absolute;bottom:2px;right:2px;opacity: 0.4;">
  132. {{ timeago(s.created_at) }}
  133. </span> -->
  134. <blur-hash-image
  135. width="32"
  136. height="32"
  137. class="rounded"
  138. :hash="s.media_attachments[0].blurhash"
  139. :src="previewUrl(s)" />
  140. </div>
  141. </div>
  142. </a>
  143. </div>
  144. <div v-if="canLoadMore" class="col-12">
  145. <intersect @enter="enterIntersect">
  146. <div class="card card-body shadow-none border">
  147. <div class="d-flex justify-content-center align-items-center flex-column">
  148. <b-spinner variant="muted" />
  149. <p class="text-lighter small mt-2 mb-0">Loading more...</p>
  150. </div>
  151. </div>
  152. </intersect>
  153. </div>
  154. </div>
  155. <!-- </masonry> -->
  156. </div>
  157. </div>
  158. <b-modal ref="editModal" id="edit-modal" hide-footer centered title="Edit Collection" body-class="">
  159. <form>
  160. <div class="form-group">
  161. <label for="title" class="font-weight-bold text-muted">Title</label>
  162. <input type="text" class="form-control" id="title" placeholder="Untitled Collection" v-model="title">
  163. </div>
  164. <div class="form-group">
  165. <label for="description" class="font-weight-bold text-muted">Description</label>
  166. <textarea class="form-control" id="description" placeholder="Add a description here ..." v-model="description" rows="3"></textarea>
  167. </div>
  168. <div class="form-group">
  169. <label for="visibility" class="font-weight-bold text-muted">Visibility</label>
  170. <select class="custom-select" v-model="visibility">
  171. <option value="public">Public</option>
  172. <option value="private">Followers Only</option>
  173. <option value="draft">Draft</option>
  174. </select>
  175. </div>
  176. <div class="d-flex justify-content-between align-items-center pt-3">
  177. <a
  178. class="text-primary font-weight-bold text-decoration-none"
  179. href="#"
  180. @click.prevent="showEditPhotosModal">
  181. Edit Photos
  182. </a>
  183. <div v-if="collection.published_at">
  184. <button
  185. type="button"
  186. class="btn btn-primary btn-sm py-1 font-weight-bold px-3 float-right"
  187. @click.prevent="updateCollection">
  188. Save
  189. </button>
  190. </div>
  191. <div v-else class="float-right">
  192. <button
  193. type="button"
  194. class="btn btn-outline-primary btn-sm py-1 font-weight-bold px-3"
  195. @click.prevent="publishCollection">
  196. Publish
  197. </button>
  198. <button
  199. type="button"
  200. class="btn btn-primary btn-sm py-1 font-weight-bold px-3"
  201. @click.prevent="updateCollection">
  202. Save
  203. </button>
  204. </div>
  205. </div>
  206. </form>
  207. </b-modal>
  208. <b-modal ref="addPhotoModal" id="add-photo-modal" hide-footer centered title="Add Photo" body-class="m-3">
  209. <div class="form-group">
  210. <label for="title" class="font-weight-bold text-muted">Add Recent Post</label>
  211. <div class="row m-1" v-if="postsList.length > 0" style="max-height: 360px; overflow-y: auto;">
  212. <div v-for="(p, index) in postsList" :key="'postList-'+index" class="col-4 p-1 cursor-pointer" @click="addRecentId(p)">
  213. <div class="square border">
  214. <div class="square-content" v-bind:style="'background-image: url(' + getPreviewUrl(p) + ');'"></div>
  215. </div>
  216. </div>
  217. <div class="col-12">
  218. <hr>
  219. </div>
  220. </div>
  221. </div>
  222. <form>
  223. <div class="form-group">
  224. <label for="title" class="font-weight-bold text-muted">Add Post by URL</label>
  225. <input type="text" class="form-control" placeholder="https://pixelfed.dev/p/admin/1" v-model="photoId">
  226. <p class="help-text small text-muted">Only local, public posts can be added</p>
  227. </div>
  228. <button type="button" class="btn btn-primary btn-sm py-1 font-weight-bold px-3 float-right" @click.prevent="pushId">
  229. <span v-if="addingPostToCollection" class="px-4">
  230. <div class="spinner-border spinner-border-sm" role="status">
  231. <span class="sr-only">Loading...</span>
  232. </div>
  233. </span>
  234. <span v-else>
  235. Add Photo
  236. </span>
  237. </button>
  238. </form>
  239. </b-modal>
  240. <b-modal ref="editPhotosModal" id="edit-photos-modal" hide-footer centered title="Edit Collection Photos" body-class="m-3">
  241. <div class="form-group">
  242. <p class="font-weight-bold text-dark text-center">Select a Photo to Delete</p>
  243. <div class="row m-1 scrollbar-hidden" v-if="posts.length > 0" style="max-height: 350px;overflow-y: auto;">
  244. <div v-for="(p, index) in posts" :key="'plm-'+index" class="col-4 p-1 cursor-pointer">
  245. <div :class="[markedForDeletion.indexOf(p.id) == -1 ? 'square' : 'square delete-border']" @click="markPhotoForDeletion(p.id)">
  246. <div class="square-content border" v-bind:style="'background-image: url(' + p.media_attachments[0].url + ');'"></div>
  247. </div>
  248. </div>
  249. </div>
  250. <div v-show="markedForDeletion.length > 0">
  251. <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>
  252. </div>
  253. </div>
  254. </b-modal>
  255. </div>
  256. </template>
  257. <style lang="scss" scoped>
  258. .dims {
  259. position: absolute;
  260. top: 0;
  261. right: 0;
  262. bottom: 0;
  263. left: 0;
  264. background: rgba(0,0,0,.68);
  265. z-index: 300;
  266. }
  267. .scrollbar-hidden::-webkit-scrollbar {
  268. display: none;
  269. }
  270. .delete-border {
  271. border: 4px solid #ff0000;
  272. }
  273. .delete-border .square-content {
  274. background-color: red;
  275. background-blend-mode: screen;
  276. }
  277. .info-overlay-text-field {
  278. font-size: 13.5px;
  279. margin-bottom: 2px;
  280. @media (min-width: 768px) {
  281. font-size: 20px;
  282. margin-bottom: 15px;
  283. }
  284. }
  285. .feed {
  286. .card.info-overlay {
  287. margin-bottom: 2rem;
  288. }
  289. }
  290. </style>
  291. <script type="text/javascript">
  292. import VueMasonry from 'vue-masonry-css';
  293. import Intersect from 'vue-intersect';
  294. export default {
  295. props: [
  296. 'collection-id',
  297. 'collection-title',
  298. 'collection-description',
  299. 'collection-visibility',
  300. 'profile-id',
  301. 'profile-username'
  302. ],
  303. components: {
  304. "intersect": Intersect,
  305. },
  306. data() {
  307. return {
  308. collection: {},
  309. config: window.App.config,
  310. loaded: false,
  311. posts: [],
  312. ids: [],
  313. user: false,
  314. owner: false,
  315. title: this.collectionTitle,
  316. description: this.collectionDescription,
  317. visibility: this.collectionVisibility,
  318. photoId: '',
  319. postsList: [],
  320. loadingPostList: false,
  321. addingPostToCollection: false,
  322. markedForDeletion: [],
  323. canLoadMore: false,
  324. isIntersecting: false,
  325. page: 1
  326. }
  327. },
  328. beforeMount() {
  329. this.fetchCollection();
  330. },
  331. methods: {
  332. enterIntersect() {
  333. if(this.isIntersecting) {
  334. return;
  335. }
  336. this.isIntersecting = true;
  337. this.page++;
  338. this.fetchItems();
  339. },
  340. statusUrl(s) {
  341. return '/i/web/post/' + s.id;
  342. },
  343. fetchCollection() {
  344. axios.get('/api/local/collection/' + this.collectionId)
  345. .then(res => {
  346. this.collection = res.data;
  347. if(this.collection.post_count > 9) {
  348. this.canLoadMore = true;
  349. }
  350. this.fetchCurrentUser();
  351. })
  352. },
  353. fetchCurrentUser() {
  354. if(document.querySelectorAll('body')[0].classList.contains('loggedIn') == true) {
  355. axios.get('/api/pixelfed/v1/accounts/verify_credentials').then(res => {
  356. this.user = res.data;
  357. this.owner = this.user.id == this.profileId;
  358. window._sharedData.curUser = res.data;
  359. window.App.util.navatar();
  360. this.fetchItems();
  361. });
  362. } else {
  363. this.fetchItems();
  364. }
  365. },
  366. fetchItems() {
  367. axios.get(
  368. '/api/local/collection/items/' + this.collectionId,
  369. {
  370. params: {
  371. page: this.page
  372. }
  373. }
  374. )
  375. .then(res => {
  376. if(res.data.length == 0) {
  377. console.log('no items found');
  378. this.loaded = true;
  379. this.isIntersecting = false;
  380. this.canLoadMore = false;
  381. return;
  382. }
  383. let data = res.data.filter(p => {
  384. return this.ids.indexOf(p.id) == -1;
  385. });
  386. this.posts.push(...data);
  387. this.ids = this.posts.map(p => {
  388. return p.id;
  389. });
  390. this.loaded = true;
  391. this.isIntersecting = false;
  392. if(data.length == 0) {
  393. this.canLoadMore = false;
  394. }
  395. });
  396. },
  397. previewUrl(status) {
  398. return status && status.sensitive ? '/storage/no-preview.png?v=' + new Date().getTime() : status.media_attachments[0].url;
  399. },
  400. previewBackground(status) {
  401. let preview = this.previewUrl(status);
  402. return 'background-image: url(' + preview + ');';
  403. },
  404. addToCollection() {
  405. let self = this;
  406. this.loadingPostList = true;
  407. if(this.postsList.length == 0) {
  408. axios.get('/api/v1/accounts/'+this.profileId+'/statuses', {
  409. params: {
  410. min_id: 1,
  411. limit: 40
  412. }
  413. })
  414. .then(res => {
  415. self.postsList = res.data.filter(l => {
  416. return self.ids.indexOf(l.id) == -1;
  417. });
  418. self.loadingPostList = false;
  419. self.$refs.addPhotoModal.show();
  420. }).catch(err => {
  421. self.loadingPostList = false;
  422. swal('An Error Occured', 'We cannot process your request at this time, please try again later.', 'error');
  423. })
  424. } else {
  425. this.$refs.addPhotoModal.show();
  426. this.loadingPostList = false;
  427. }
  428. },
  429. pushId() {
  430. let max = this.config.uploader.max_collection_length;
  431. let addingPostToCollection = true;
  432. let self = this;
  433. if(this.posts.length >= max) {
  434. swal('Error', 'You can only add ' + max + ' posts per collection', 'error');
  435. return;
  436. }
  437. let url = this.photoId;
  438. let origin = window.location.origin;
  439. let split = url.split('/');
  440. if(url.slice(0, origin.length) !== origin) {
  441. swal('Invalid URL', 'You can only add posts from this instance', 'error');
  442. this.photoId = '';
  443. }
  444. if(!url.includes('/i/web/post/') && !url.includes('/p/')) {
  445. swal('Invalid URL', 'Invalid URL', 'error');
  446. this.photoId = '';
  447. return;
  448. }
  449. let fragment = split[split.length - 1].split('?')[0];
  450. axios.post('/api/local/collection/item', {
  451. collection_id: this.collectionId,
  452. post_id: fragment
  453. }).then(res => {
  454. self.ids.push(...fragment);
  455. self.posts.push(res.data);
  456. self.collection.post_count++;
  457. self.id = '';
  458. }).catch(err => {
  459. swal('Invalid URL', 'The post you entered was invalid', 'error');
  460. this.photoId = '';
  461. });
  462. self.$refs.addPhotoModal.hide();
  463. // window.location.reload();
  464. },
  465. editCollection() {
  466. this.$refs.editModal.show();
  467. },
  468. deleteCollection() {
  469. if(this.owner == false) {
  470. return;
  471. }
  472. let confirmed = window.confirm('Are you sure you want to delete this collection?');
  473. if(confirmed) {
  474. axios.delete('/api/local/collection/' + this.collectionId)
  475. .then(res => {
  476. window.location.href = '/';
  477. });
  478. } else {
  479. return;
  480. }
  481. },
  482. publishCollection() {
  483. if(this.owner == false) {
  484. return;
  485. }
  486. let confirmed = window.confirm('Are you sure you want to publish this collection?');
  487. if(confirmed) {
  488. axios.post('/api/local/collection/' + this.collectionId + '/publish', {
  489. title: this.title,
  490. description: this.description,
  491. visibility: this.visibility
  492. })
  493. .then(res => {
  494. console.log(res.data);
  495. // window.location.href = res.data.url;
  496. });
  497. } else {
  498. return;
  499. }
  500. },
  501. updateCollection() {
  502. this.closeModals();
  503. axios.post('/api/local/collection/' + this.collectionId, {
  504. title: this.title,
  505. description: this.description,
  506. visibility: this.visibility
  507. }).then(res => {
  508. this.collection = res.data;
  509. });
  510. },
  511. showEditPhotosModal() {
  512. this.$refs.editModal.hide();
  513. this.$refs.editPhotosModal.show();
  514. },
  515. markPhotoForDeletion(id) {
  516. this.markedForDeletion.indexOf(id) == -1 ?
  517. this.markedForDeletion.push(id) :
  518. this.markedForDeletion = this.markedForDeletion.filter(d => {
  519. return d != id;
  520. });
  521. },
  522. confirmDeletion() {
  523. let self = this;
  524. let confirmed = window.confirm('Are you sure you want to delete this?');
  525. if(confirmed) {
  526. this.markedForDeletion.forEach(mfd => {
  527. axios.delete('/api/local/collection/item', {
  528. params: {
  529. collection_id: self.collectionId,
  530. post_id: mfd
  531. }
  532. })
  533. .then(res => {
  534. self.removeItem(mfd);
  535. this.collection.post_count = this.collection.post_count - 1;
  536. this.closeModals();
  537. })
  538. .catch(err => {
  539. swal(
  540. 'Oops!',
  541. 'An error occured with your request, please try again later.',
  542. 'error'
  543. );
  544. })
  545. });
  546. this.markedForDeletion = [];
  547. }
  548. },
  549. removeItem(id) {
  550. this.posts = this.posts.filter(post => {
  551. return post.id != id;
  552. });
  553. },
  554. addRecentId(post) {
  555. let self = this;
  556. axios.post('/api/local/collection/item', {
  557. collection_id: self.collectionId,
  558. post_id: post.id
  559. }).then(res => {
  560. // window.location.reload();
  561. this.closeModals();
  562. this.posts.push(res.data);
  563. this.collection.post_count++;
  564. }).catch(err => {
  565. swal('Oops!', 'An error occured, please try selecting another post.', 'error');
  566. this.photoId = '';
  567. });
  568. },
  569. timeago(ts) {
  570. return App.util.format.timeAgo(ts);
  571. },
  572. closeModals() {
  573. this.$refs.editModal.hide();
  574. this.$refs.addPhotoModal.hide();
  575. this.$refs.editPhotosModal.hide();
  576. },
  577. getPreviewUrl(post) {
  578. if(!post.media_attachments || !post.media_attachments.length) {
  579. return '/storage/no-preview.png';
  580. }
  581. let media = post.media_attachments[0];
  582. if(media.preview_url.endsWith('storage/no-preview.png')) {
  583. return media.type === 'image' ?
  584. media.url :
  585. '/storage/no-preview.png';
  586. }
  587. return media.preview_url;
  588. }
  589. }
  590. }
  591. </script>