CollectionComponent.vue 20 KB

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