CollectionComponent.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. <div class="d-flex align-items-center justify-content-center overflow-hidden">
  27. <div class="dims"></div>
  28. <div style="z-index:500;position: absolute;" class="text-white mx-5">
  29. <p class="text-center pt-3 text-break" style="font-size: 3rem;line-height: 3rem;">{{title || 'Untitled Collection'}}</p>
  30. <div class="text-center mb-3 text-break read-more" style="overflow-y: hidden">{{description}}</div>
  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. </div>
  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" maxlength="50">
  163. <div class="text-right small text-muted">
  164. <span>{{title ? title.length : 0}}/50</span>
  165. </div>
  166. </div>
  167. <div class="form-group">
  168. <label for="description" class="font-weight-bold text-muted">Description</label>
  169. <textarea class="form-control" id="description" placeholder="Add a description here ..." v-model="description" rows="3" maxlength="500"></textarea>
  170. <div class="text-right small text-muted">
  171. <span>{{description ? description.length : 0}}/500</span>
  172. </div>
  173. </div>
  174. <div class="form-group">
  175. <label for="visibility" class="font-weight-bold text-muted">Visibility</label>
  176. <select class="custom-select" v-model="visibility">
  177. <option value="public">Public</option>
  178. <option value="private">Followers Only</option>
  179. <option value="draft">Draft</option>
  180. </select>
  181. </div>
  182. <div class="d-flex justify-content-between align-items-center pt-3">
  183. <a
  184. class="text-primary font-weight-bold text-decoration-none"
  185. href="#"
  186. @click.prevent="showEditPhotosModal">
  187. Edit Photos
  188. </a>
  189. <div v-if="collection.published_at">
  190. <button
  191. type="button"
  192. class="btn btn-primary btn-sm py-1 font-weight-bold px-3 float-right"
  193. @click.prevent="updateCollection">
  194. Save
  195. </button>
  196. </div>
  197. <div v-else class="float-right">
  198. <button
  199. type="button"
  200. class="btn btn-outline-primary btn-sm py-1 font-weight-bold px-3"
  201. @click.prevent="publishCollection">
  202. Publish
  203. </button>
  204. <button
  205. type="button"
  206. class="btn btn-primary btn-sm py-1 font-weight-bold px-3"
  207. @click.prevent="updateCollection">
  208. Save
  209. </button>
  210. </div>
  211. </div>
  212. </form>
  213. </b-modal>
  214. <b-modal ref="addPhotoModal" id="add-photo-modal" hide-footer centered title="Add Photo" body-class="m-3">
  215. <div class="form-group">
  216. <label for="title" class="font-weight-bold text-muted">Add Recent Post</label>
  217. <div class="row m-1" v-if="postsList.length > 0" style="max-height: 360px; overflow-y: auto;">
  218. <div v-for="(p, index) in postsList" :key="'postList-'+index" class="col-4 p-1 cursor-pointer" @click="addRecentId(p)">
  219. <div class="square border">
  220. <div class="square-content" v-bind:style="'background-image: url(' + getPreviewUrl(p) + ');'"></div>
  221. </div>
  222. </div>
  223. <div class="col-12">
  224. <hr>
  225. </div>
  226. </div>
  227. </div>
  228. <form>
  229. <div class="form-group">
  230. <label for="title" class="font-weight-bold text-muted">Add Post by URL</label>
  231. <input type="text" class="form-control" placeholder="https://pixelfed.dev/p/admin/1" v-model="photoId">
  232. <p class="help-text small text-muted">Only local, public posts can be added</p>
  233. </div>
  234. <button type="button" class="btn btn-primary btn-sm py-1 font-weight-bold px-3 float-right" @click.prevent="pushId">
  235. <span v-if="addingPostToCollection" class="px-4">
  236. <div class="spinner-border spinner-border-sm" role="status">
  237. <span class="sr-only">Loading...</span>
  238. </div>
  239. </span>
  240. <span v-else>
  241. Add Photo
  242. </span>
  243. </button>
  244. </form>
  245. </b-modal>
  246. <b-modal ref="editPhotosModal" id="edit-photos-modal" hide-footer centered title="Edit Collection Photos" body-class="m-3">
  247. <div class="form-group">
  248. <p class="font-weight-bold text-dark text-center">Select a Photo to Delete</p>
  249. <div class="row m-1 scrollbar-hidden" v-if="posts.length > 0" style="max-height: 350px;overflow-y: auto;">
  250. <div v-for="(p, index) in posts" :key="'plm-'+index" class="col-4 p-1 cursor-pointer">
  251. <div :class="[markedForDeletion.indexOf(p.id) == -1 ? 'square' : 'square delete-border']" @click="markPhotoForDeletion(p.id)">
  252. <div class="square-content border" v-bind:style="'background-image: url(' + p.media_attachments[0].url + ');'"></div>
  253. </div>
  254. </div>
  255. </div>
  256. <div v-show="markedForDeletion.length > 0">
  257. <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>
  258. </div>
  259. </div>
  260. </b-modal>
  261. </div>
  262. </template>
  263. <style lang="scss" scoped>
  264. .dims {
  265. position: absolute;
  266. top: 0;
  267. right: 0;
  268. bottom: 0;
  269. left: 0;
  270. background: rgba(0,0,0,.68);
  271. z-index: 300;
  272. }
  273. .scrollbar-hidden::-webkit-scrollbar {
  274. display: none;
  275. }
  276. .delete-border {
  277. border: 4px solid #ff0000;
  278. }
  279. .delete-border .square-content {
  280. background-color: red;
  281. background-blend-mode: screen;
  282. }
  283. .info-overlay-text-field {
  284. font-size: 13.5px;
  285. margin-bottom: 2px;
  286. @media (min-width: 768px) {
  287. font-size: 20px;
  288. margin-bottom: 15px;
  289. }
  290. }
  291. .feed {
  292. .card.info-overlay {
  293. margin-bottom: 2rem;
  294. }
  295. }
  296. </style>
  297. <script type="text/javascript">
  298. import VueMasonry from 'vue-masonry-css';
  299. import Intersect from 'vue-intersect';
  300. export default {
  301. props: [
  302. 'collection-id',
  303. 'collection-title',
  304. 'collection-description',
  305. 'collection-visibility',
  306. 'profile-id',
  307. 'profile-username'
  308. ],
  309. components: {
  310. "intersect": Intersect,
  311. },
  312. data() {
  313. return {
  314. collection: {},
  315. config: window.App.config,
  316. loaded: false,
  317. posts: [],
  318. ids: [],
  319. user: false,
  320. owner: false,
  321. title: this.collectionTitle,
  322. description: this.collectionDescription,
  323. visibility: this.collectionVisibility,
  324. photoId: '',
  325. postsList: [],
  326. loadingPostList: false,
  327. addingPostToCollection: false,
  328. markedForDeletion: [],
  329. canLoadMore: false,
  330. isIntersecting: false,
  331. page: 1
  332. }
  333. },
  334. beforeMount() {
  335. this.fetchCollection();
  336. },
  337. updated() {
  338. this.initReadMore();
  339. },
  340. methods: {
  341. enterIntersect() {
  342. if(this.isIntersecting) {
  343. return;
  344. }
  345. this.isIntersecting = true;
  346. this.page++;
  347. this.fetchItems();
  348. },
  349. statusUrl(s) {
  350. return '/i/web/post/' + s.id;
  351. },
  352. fetchCollection() {
  353. axios.get('/api/local/collection/' + this.collectionId)
  354. .then(res => {
  355. this.collection = res.data;
  356. if(this.collection.post_count > 9) {
  357. this.canLoadMore = true;
  358. }
  359. this.fetchCurrentUser();
  360. })
  361. },
  362. fetchCurrentUser() {
  363. if(document.querySelectorAll('body')[0].classList.contains('loggedIn') == true) {
  364. axios.get('/api/pixelfed/v1/accounts/verify_credentials').then(res => {
  365. this.user = res.data;
  366. this.owner = this.user.id == this.profileId;
  367. window._sharedData.curUser = res.data;
  368. window.App.util.navatar();
  369. this.fetchItems();
  370. });
  371. } else {
  372. this.fetchItems();
  373. }
  374. },
  375. fetchItems() {
  376. axios.get(
  377. '/api/local/collection/items/' + this.collectionId,
  378. {
  379. params: {
  380. page: this.page
  381. }
  382. }
  383. )
  384. .then(res => {
  385. if(res.data.length == 0) {
  386. console.log('no items found');
  387. this.loaded = true;
  388. this.isIntersecting = false;
  389. this.canLoadMore = false;
  390. return;
  391. }
  392. let data = res.data.filter(p => {
  393. return this.ids.indexOf(p.id) == -1;
  394. });
  395. this.posts.push(...data);
  396. this.ids = this.posts.map(p => {
  397. return p.id;
  398. });
  399. this.loaded = true;
  400. this.isIntersecting = false;
  401. if(data.length == 0) {
  402. this.canLoadMore = false;
  403. }
  404. });
  405. },
  406. previewUrl(status) {
  407. return status && status.sensitive ? '/storage/no-preview.png?v=' + new Date().getTime() : status.media_attachments[0].url;
  408. },
  409. previewBackground(status) {
  410. let preview = this.previewUrl(status);
  411. return 'background-image: url(' + preview + ');';
  412. },
  413. addToCollection() {
  414. let self = this;
  415. this.loadingPostList = true;
  416. if(this.postsList.length == 0) {
  417. axios.get('/api/v1/accounts/'+this.profileId+'/statuses', {
  418. params: {
  419. min_id: 1,
  420. limit: 40
  421. }
  422. })
  423. .then(res => {
  424. self.postsList = res.data.filter(l => {
  425. return self.ids.indexOf(l.id) == -1;
  426. });
  427. self.loadingPostList = false;
  428. self.$refs.addPhotoModal.show();
  429. }).catch(err => {
  430. self.loadingPostList = false;
  431. swal('An Error Occured', 'We cannot process your request at this time, please try again later.', 'error');
  432. })
  433. } else {
  434. this.$refs.addPhotoModal.show();
  435. this.loadingPostList = false;
  436. }
  437. },
  438. pushId() {
  439. let max = this.config.uploader.max_collection_length;
  440. let addingPostToCollection = true;
  441. let self = this;
  442. if(this.posts.length >= max) {
  443. swal('Error', 'You can only add ' + max + ' posts per collection', 'error');
  444. return;
  445. }
  446. let url = this.photoId;
  447. let origin = window.location.origin;
  448. let split = url.split('/');
  449. if(url.slice(0, origin.length) !== origin) {
  450. swal('Invalid URL', 'You can only add posts from this instance', 'error');
  451. this.photoId = '';
  452. }
  453. if(!url.includes('/i/web/post/') && !url.includes('/p/')) {
  454. swal('Invalid URL', 'Invalid URL', 'error');
  455. this.photoId = '';
  456. return;
  457. }
  458. let fragment = split[split.length - 1].split('?')[0];
  459. axios.post('/api/local/collection/item', {
  460. collection_id: this.collectionId,
  461. post_id: fragment
  462. }).then(res => {
  463. self.ids.push(...fragment);
  464. self.posts.push(res.data);
  465. self.collection.post_count++;
  466. self.id = '';
  467. }).catch(err => {
  468. swal('Invalid URL', 'The post you entered was invalid', 'error');
  469. this.photoId = '';
  470. });
  471. self.$refs.addPhotoModal.hide();
  472. // window.location.reload();
  473. },
  474. editCollection() {
  475. this.$refs.editModal.show();
  476. },
  477. deleteCollection() {
  478. if(this.owner == false) {
  479. return;
  480. }
  481. let confirmed = window.confirm('Are you sure you want to delete this collection?');
  482. if(confirmed) {
  483. axios.delete('/api/local/collection/' + this.collectionId)
  484. .then(res => {
  485. window.location.href = '/';
  486. });
  487. } else {
  488. return;
  489. }
  490. },
  491. publishCollection() {
  492. if(this.owner == false) {
  493. return;
  494. }
  495. let confirmed = window.confirm('Are you sure you want to publish this collection?');
  496. if(confirmed) {
  497. axios.post('/api/local/collection/' + this.collectionId + '/publish', {
  498. title: this.title,
  499. description: this.description,
  500. visibility: this.visibility
  501. })
  502. .then(res => {
  503. console.log(res.data);
  504. // window.location.href = res.data.url;
  505. });
  506. } else {
  507. return;
  508. }
  509. },
  510. updateCollection() {
  511. this.closeModals();
  512. axios.post('/api/local/collection/' + this.collectionId, {
  513. title: this.title,
  514. description: this.description,
  515. visibility: this.visibility
  516. }).then(res => {
  517. this.collection = res.data;
  518. });
  519. },
  520. showEditPhotosModal() {
  521. this.$refs.editModal.hide();
  522. this.$refs.editPhotosModal.show();
  523. },
  524. markPhotoForDeletion(id) {
  525. this.markedForDeletion.indexOf(id) == -1 ?
  526. this.markedForDeletion.push(id) :
  527. this.markedForDeletion = this.markedForDeletion.filter(d => {
  528. return d != id;
  529. });
  530. },
  531. confirmDeletion() {
  532. let self = this;
  533. let confirmed = window.confirm('Are you sure you want to delete this?');
  534. if(confirmed) {
  535. this.markedForDeletion.forEach(mfd => {
  536. axios.delete('/api/local/collection/item', {
  537. params: {
  538. collection_id: self.collectionId,
  539. post_id: mfd
  540. }
  541. })
  542. .then(res => {
  543. self.removeItem(mfd);
  544. this.collection.post_count = this.collection.post_count - 1;
  545. this.closeModals();
  546. })
  547. .catch(err => {
  548. swal(
  549. 'Oops!',
  550. 'An error occured with your request, please try again later.',
  551. 'error'
  552. );
  553. })
  554. });
  555. this.markedForDeletion = [];
  556. }
  557. },
  558. removeItem(id) {
  559. this.posts = this.posts.filter(post => {
  560. return post.id != id;
  561. });
  562. },
  563. addRecentId(post) {
  564. let self = this;
  565. axios.post('/api/local/collection/item', {
  566. collection_id: self.collectionId,
  567. post_id: post.id
  568. }).then(res => {
  569. // window.location.reload();
  570. this.closeModals();
  571. this.posts.push(res.data);
  572. this.collection.post_count++;
  573. }).catch(err => {
  574. swal('Oops!', 'An error occured, please try selecting another post.', 'error');
  575. this.photoId = '';
  576. });
  577. },
  578. timeago(ts) {
  579. return App.util.format.timeAgo(ts);
  580. },
  581. closeModals() {
  582. this.$refs.editModal.hide();
  583. this.$refs.addPhotoModal.hide();
  584. this.$refs.editPhotosModal.hide();
  585. },
  586. getPreviewUrl(post) {
  587. if(!post.media_attachments || !post.media_attachments.length) {
  588. return '/storage/no-preview.png';
  589. }
  590. let media = post.media_attachments[0];
  591. if(media.preview_url.endsWith('storage/no-preview.png')) {
  592. return media.type === 'image' ?
  593. media.url :
  594. '/storage/no-preview.png';
  595. }
  596. return media.preview_url;
  597. },
  598. initReadMore() {
  599. $('.read-more').each(function(k,v) {
  600. let el = $(this);
  601. let attr = el.attr('data-readmore');
  602. if(typeof attr !== typeof undefined && attr !== false) {
  603. return;
  604. }
  605. el.readmore({
  606. collapsedHeight: 38,
  607. heightMargin: 38,
  608. moreLink: '<a href="#" class="d-block text-center small font-weight-bold mt-n3 mb-2" style="color: rgba(255, 255, 255, 0.5)">Show more</a>',
  609. lessLink: '<a href="#" class="d-block text-center small font-weight-bold mt-n3 mb-2" style="color: rgba(255, 255, 255, 0.5)">Show less</a>',
  610. });
  611. });
  612. }
  613. }
  614. }
  615. </script>