ComposeClassic.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. <template>
  2. <div>
  3. <input type="file" name="media" class="d-none file-input" multiple="" v-bind:accept="config.uploader.media_types">
  4. <div class="timeline">
  5. <div class="card status-card card-md-rounded-0">
  6. <div class="card-header d-inline-flex align-items-center bg-white">
  7. <img v-bind:src="profile.avatar" width="32px" height="32px" style="border-radius: 32px;" class="box-shadow">
  8. <a class="username font-weight-bold pl-2 text-dark" v-bind:href="profile.url">
  9. {{profile.username}}
  10. </a>
  11. <div class="text-right" style="flex-grow:1;">
  12. <div class="dropdown">
  13. <button class="btn btn-link text-dark no-caret dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" title="Post options">
  14. <span class="fas fa-ellipsis-v fa-lg text-muted"></span>
  15. </button>
  16. <div class="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenuButton">
  17. <div class="dropdown-item small font-weight-bold" v-on:click="createCollection">Create Collection</div>
  18. <div class="dropdown-divider"></div>
  19. <div class="dropdown-item small font-weight-bold" v-on:click="about">About</div>
  20. <div class="dropdown-item small font-weight-bold" v-on:click="closeModal">Close</div>
  21. </div>
  22. </div>
  23. </div>
  24. </div>
  25. <div class="postPresenterContainer">
  26. <div v-if="uploading">
  27. <div class="w-100 h-100 bg-light py-5" style="border-bottom: 1px solid #f1f1f1">
  28. <div class="p-5">
  29. <b-progress :value="uploadProgress" :max="100" striped :animated="true"></b-progress>
  30. <p class="text-center mb-0 font-weight-bold">Uploading ... ({{uploadProgress}}%)</p>
  31. </div>
  32. </div>
  33. </div>
  34. <div v-else>
  35. <div v-if="ids.length > 0 && ids.length != config.uploader.album_limit" class="card-header py-2 bg-primary m-2 rounded cursor-pointer" v-on:click="addMedia($event)">
  36. <p class="text-center mb-0 font-weight-bold text-white"><i class="fas fa-plus mr-1"></i> Add Photo</p>
  37. </div>
  38. <div v-if="ids.length == 0" class="w-100 h-100 bg-light py-5 cursor-pointer" style="border-bottom: 1px solid #f1f1f1" v-on:click="addMedia($event)">
  39. <div class="p-5">
  40. <p class="text-center font-weight-bold">{{composeMessage()}}</p>
  41. <p class="text-muted mb-0 small text-center">Accepted Formats: <b>{{acceptedFormats()}}</b></p>
  42. <p class="text-muted mb-0 small text-center">Max File Size: <b>{{maxSize()}}</b></p>
  43. <p class="text-muted mb-0 small text-center">Albums can contain up to <b>{{config.uploader.album_limit}}</b> photos or videos</p>
  44. </div>
  45. </div>
  46. <div v-if="ids.length > 0">
  47. <b-carousel id="p-carousel"
  48. style="text-shadow: 1px 1px 2px #333;"
  49. controls
  50. indicators
  51. background="#ffffff"
  52. :interval="0"
  53. v-model="carouselCursor"
  54. >
  55. <b-carousel-slide v-if="ids.length > 0" v-for="(preview, index) in media" :key="'preview_media_'+index">
  56. <div slot="img" :class="[media[index].filter_class?media[index].filter_class:'']" style="display:flex;min-height: 320px;align-items: center;">
  57. <img class="d-block img-fluid w-100" :src="preview.url" :alt="preview.description" :title="preview.description">
  58. </div>
  59. </b-carousel-slide>
  60. </b-carousel>
  61. </div>
  62. <div v-if="ids.length > 0 && media[carouselCursor].type == 'Image'" class="bg-dark align-items-center">
  63. <ul class="nav media-drawer-filters text-center">
  64. <li class="nav-item">
  65. <div class="p-1 pt-3">
  66. <img :src="media[carouselCursor].url" width="100px" height="60px" v-on:click.prevent="toggleFilter($event, null)" class="cursor-pointer">
  67. </div>
  68. <a :class="[media[carouselCursor].filter_class == null ? 'nav-link text-white active' : 'nav-link text-muted']" href="#" v-on:click.prevent="toggleFilter($event, null)">No Filter</a>
  69. </li>
  70. <li class="nav-item" v-for="(filter, index) in filters">
  71. <div class="p-1 pt-3">
  72. <img :src="media[carouselCursor].url" width="100px" height="60px" :class="filter[1]" v-on:click.prevent="toggleFilter($event, filter[1])">
  73. </div>
  74. <a :class="[media[carouselCursor].filter_class == filter[1] ? 'nav-link text-white active' : 'nav-link text-muted']" href="#" v-on:click.prevent="toggleFilter($event, filter[1])">{{filter[0]}}</a>
  75. </li>
  76. </ul>
  77. </div>
  78. </div>
  79. <div v-if="ids.length > 0 && ['Image', 'Video'].indexOf(media[carouselCursor].type) != -1" class="bg-lighter p-2 row">
  80. <div v-if="media[carouselCursor].type == 'Image'" class="col-12">
  81. <div class="form-group">
  82. <input type="text" class="form-control" v-model="media[carouselCursor].alt" placeholder="Optional image description">
  83. </div>
  84. <div class="form-group">
  85. <input type="text" class="form-control" v-model="media[carouselCursor].license" placeholder="Optional media license">
  86. </div>
  87. </div>
  88. <!-- <div class="col-6 pt-2">
  89. <button class="btn btn-outline-secondary btn-sm mr-1"><i class="fas fa-map-marker-alt"></i></button>
  90. <button class="btn btn-outline-secondary btn-sm"><i class="fas fa-tools"></i></button>
  91. </div> -->
  92. <div class="col-12 text-right pt-2">
  93. <button class="btn btn-outline-danger btn-sm font-weight-bold mr-1" v-on:click="deleteMedia()">Delete Media</button>
  94. </div>
  95. </div>
  96. </div>
  97. <div class="card-body p-0 border-top">
  98. <div class="caption">
  99. <textarea class="form-control mb-0 border-0 rounded-0" rows="3" placeholder="Add an optional caption" v-model="composeText"></textarea>
  100. </div>
  101. </div>
  102. <div class="card-footer">
  103. <div class="d-flex justify-content-between align-items-center">
  104. <div>
  105. <div class="custom-control custom-switch d-inline mr-3">
  106. <input type="checkbox" class="custom-control-input" id="nsfwToggle" v-model="nsfw">
  107. <label class="custom-control-label small font-weight-bold text-muted pt-1" for="nsfwToggle">NSFW</label>
  108. </div>
  109. <div class="dropdown d-inline">
  110. <button class="btn btn-outline-secondary btn-sm py-0 dropdown-toggle" type="button" id="visibility" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  111. {{visibility[0].toUpperCase() + visibility.slice(1)}}
  112. </button>
  113. <div class="dropdown-menu" aria-labelledby="visibility" style="width: 200px;">
  114. <a :class="[visibility=='public'?'dropdown-item active':'dropdown-item']" href="#" data-id="public" data-title="Public" v-on:click.prevent="visibility = 'public'">
  115. <div class="row">
  116. <div class="d-none d-block-sm col-sm-2 px-0 text-center">
  117. <i class="fas fa-globe"></i>
  118. </div>
  119. <div class="col-12 col-sm-10 pl-2">
  120. <p class="font-weight-bold mb-0">Public</p>
  121. <p class="small mb-0">Anyone can see</p>
  122. </div>
  123. </div>
  124. </a>
  125. <a :class="[visibility=='private'?'dropdown-item active':'dropdown-item']" href="#" data-id="private" data-title="Followers Only" v-on:click.prevent="visibility = 'private'">
  126. <div class="row">
  127. <div class="d-none d-block-sm col-sm-2 px-0 text-center">
  128. <i class="fas fa-lock"></i>
  129. </div>
  130. <div class="col-12 col-sm-10 pl-2">
  131. <p class="font-weight-bold mb-0">Followers Only</p>
  132. <p class="small mb-0">Only followers can see</p>
  133. </div>
  134. </div>
  135. </a>
  136. <a :class="[visibility=='unlisted'?'dropdown-item active':'dropdown-item']" href="#" data-id="private" data-title="Unlisted" v-on:click.prevent="visibility = 'unlisted'">
  137. <div class="row">
  138. <div class="d-none d-block-sm col-sm-2 px-0 text-center">
  139. <i class="fas fa-lock"></i>
  140. </div>
  141. <div class="col-12 col-sm-10 pl-2">
  142. <p class="font-weight-bold mb-0">Unlisted</p>
  143. <p class="small mb-0">Not listed on public timelines</p>
  144. </div>
  145. </div>
  146. </a>
  147. <!-- <a class="dropdown-item" href="#" data-id="circle" data-title="Circle">
  148. <div class="row">
  149. <div class="col-12 col-sm-2 px-0 text-center">
  150. <i class="far fa-circle"></i>
  151. </div>
  152. <div class="col-12 col-sm-10 pl-2">
  153. <p class="font-weight-bold mb-0">Circle</p>
  154. <p class="small mb-0">Select a circle</p>
  155. </div>
  156. </div>
  157. </a>
  158. <a class="dropdown-item" href="#" data-id="direct" data-title="Direct Message">
  159. <div class="row">
  160. <div class="col-12 col-sm-2 px-0 text-center">
  161. <i class="fas fa-envelope"></i>
  162. </div>
  163. <div class="col-12 col-sm-10 pl-2">
  164. <p class="font-weight-bold mb-0">Direct Message</p>
  165. <p class="small mb-0">Recipients only</p>
  166. </div>
  167. </div>
  168. </a> -->
  169. </div>
  170. </div>
  171. </div>
  172. <div class="small text-muted font-weight-bold">
  173. {{composeText.length}} / {{config.uploader.max_caption_length}}
  174. </div>
  175. <div class="pl-md-5">
  176. <!-- <div class="btn-group">
  177. <button type="button" class="btn btn-primary btn-sm font-weight-bold" v-on:click="compose()">{{composeState[0].toUpperCase() + composeState.slice(1)}}</button>
  178. <button type="button" class="btn btn-primary btn-sm dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  179. <span class="sr-only">Toggle Dropdown</span>
  180. </button>
  181. <div class="dropdown-menu dropdown-menu-right">
  182. <a :class="[composeState == 'publish' ?'dropdown-item font-weight-bold active':'dropdown-item font-weight-bold ']" href="#" v-on:click.prevent="composeState = 'publish'">Publish now</a>
  183. <!- - <a :class="[composeState == 'draft' ?'dropdown-item font-weight-bold active':'dropdown-item font-weight-bold ']" href="#" v-on:click.prevent="composeState = 'draft'">Save as draft</a>
  184. <a :class="[composeState == 'schedule' ?'dropdown-item font-weight-bold active':'dropdown-item font-weight-bold ']" href="#" v-on:click.prevent="composeState = 'schedule'">Schedule for later</a>
  185. <div class="dropdown-divider"></div>
  186. <a :class="[composeState == 'delete' ?'dropdown-item font-weight-bold active':'dropdown-item font-weight-bold ']" href="#" v-on:click.prevent="composeState = 'delete'">Delete</a> - ->
  187. </div>
  188. </div> -->
  189. <button class="btn btn-primary btn-sm font-weight-bold px-3" v-on:click="compose()">Publish</button>
  190. </div>
  191. </div>
  192. </div>
  193. </div>
  194. </div>
  195. </div>
  196. </template>
  197. <style type="text/css" scoped>
  198. .media-drawer-filters {
  199. overflow-x: scroll;
  200. flex-wrap:unset;
  201. }
  202. .media-drawer-filters .nav-link {
  203. min-width:100px;
  204. padding-top: 1rem;
  205. padding-bottom: 1rem;
  206. }
  207. .media-drawer-filters .active {
  208. color: #fff;
  209. font-weight: bold;
  210. }
  211. @media (hover: none) and (pointer: coarse) {
  212. .media-drawer-filters::-webkit-scrollbar {
  213. display: none;
  214. }
  215. }
  216. </style>
  217. <script type="text/javascript">
  218. export default {
  219. data() {
  220. return {
  221. config: window.App.config,
  222. profile: {},
  223. composeText: '',
  224. composeTextLength: 0,
  225. nsfw: false,
  226. filters: [],
  227. ids: [],
  228. media: [],
  229. carouselCursor: 0,
  230. visibility: 'public',
  231. mediaDrawer: false,
  232. composeState: 'publish',
  233. uploading: false,
  234. uploadProgress: 0,
  235. composeType: false
  236. }
  237. },
  238. beforeMount() {
  239. this.fetchProfile();
  240. },
  241. mounted() {
  242. this.mediaWatcher();
  243. this.filters = [
  244. ['1977','filter-1977'],
  245. ['Aden','filter-aden'],
  246. ['Amaro','filter-amaro'],
  247. ['Ashby','filter-ashby'],
  248. ['Brannan','filter-brannan'],
  249. ['Brooklyn','filter-brooklyn'],
  250. ['Charmes','filter-charmes'],
  251. ['Clarendon','filter-clarendon'],
  252. ['Crema','filter-crema'],
  253. ['Dogpatch','filter-dogpatch'],
  254. ['Earlybird','filter-earlybird'],
  255. ['Gingham','filter-gingham'],
  256. ['Ginza','filter-ginza'],
  257. ['Hefe','filter-hefe'],
  258. ['Helena','filter-helena'],
  259. ['Hudson','filter-hudson'],
  260. ['Inkwell','filter-inkwell'],
  261. ['Kelvin','filter-kelvin'],
  262. ['Kuno','filter-juno'],
  263. ['Lark','filter-lark'],
  264. ['Lo-Fi','filter-lofi'],
  265. ['Ludwig','filter-ludwig'],
  266. ['Maven','filter-maven'],
  267. ['Mayfair','filter-mayfair'],
  268. ['Moon','filter-moon'],
  269. ['Nashville','filter-nashville'],
  270. ['Perpetua','filter-perpetua'],
  271. ['Poprocket','filter-poprocket'],
  272. ['Reyes','filter-reyes'],
  273. ['Rise','filter-rise'],
  274. ['Sierra','filter-sierra'],
  275. ['Skyline','filter-skyline'],
  276. ['Slumber','filter-slumber'],
  277. ['Stinson','filter-stinson'],
  278. ['Sutro','filter-sutro'],
  279. ['Toaster','filter-toaster'],
  280. ['Valencia','filter-valencia'],
  281. ['Vesper','filter-vesper'],
  282. ['Walden','filter-walden'],
  283. ['Willow','filter-willow'],
  284. ['X-Pro II','filter-xpro-ii']
  285. ];
  286. },
  287. methods: {
  288. fetchProfile() {
  289. axios.get('/api/pixelfed/v1/accounts/verify_credentials').then(res => {
  290. this.profile = res.data;
  291. if(res.data.locked == true) {
  292. this.visibility = 'private';
  293. }
  294. }).catch(err => {
  295. });
  296. },
  297. addMedia(event) {
  298. let el = $(event.target);
  299. el.attr('disabled', '');
  300. let fi = $('.file-input[name="media"]');
  301. fi.trigger('click');
  302. el.blur();
  303. el.removeAttr('disabled');
  304. },
  305. mediaWatcher() {
  306. let self = this;
  307. $(document).on('change', '.file-input', function(e) {
  308. let io = document.querySelector('.file-input');
  309. Array.prototype.forEach.call(io.files, function(io, i) {
  310. self.uploading = true;
  311. if(self.media && self.media.length + i >= self.config.uploader.album_limit) {
  312. swal('Error', 'You can only upload ' + self.config.uploader.album_limit + ' photos per album', 'error');
  313. return;
  314. }
  315. let type = io.type;
  316. let acceptedMimes = self.config.uploader.media_types.split(',');
  317. let validated = $.inArray(type, acceptedMimes);
  318. if(validated == -1) {
  319. swal('Invalid File Type', 'The file you are trying to add is not a valid mime type. Please upload a '+self.config.uploader.media_types+' only.', 'error');
  320. return;
  321. }
  322. let form = new FormData();
  323. form.append('file', io);
  324. let xhrConfig = {
  325. onUploadProgress: function(e) {
  326. let progress = Math.round( (e.loaded * 100) / e.total );
  327. self.uploadProgress = progress;
  328. }
  329. };
  330. axios.post('/api/pixelfed/v1/media', form, xhrConfig)
  331. .then(function(e) {
  332. self.uploadProgress = 100;
  333. self.ids.push(e.data.id);
  334. self.media.push(e.data);
  335. setTimeout(function() {
  336. self.uploading = false;
  337. }, 1000);
  338. }).catch(function(e) {
  339. self.uploading = false;
  340. io.value = null;
  341. swal('Oops, something went wrong!', 'An unexpected error occurred.', 'error');
  342. });
  343. io.value = null;
  344. self.uploadProgress = 0;
  345. });
  346. });
  347. },
  348. toggleFilter(e, filter) {
  349. this.media[this.carouselCursor].filter_class = filter;
  350. },
  351. updateMedia() {
  352. this.mediaDrawer = false;
  353. },
  354. deleteMedia() {
  355. if(window.confirm('Are you sure you want to delete this media?') == false) {
  356. return;
  357. }
  358. let id = this.media[this.carouselCursor].id;
  359. axios.delete('/api/pixelfed/v1/media', {
  360. params: {
  361. id: id
  362. }
  363. }).then(res => {
  364. if(this.media.length == 1) {
  365. this.mediaDrawer = false;
  366. this.ids = [];
  367. this.media = [];
  368. this.carouselCursor = 0;
  369. }
  370. this.ids.splice(this.carouselCursor, 1);
  371. this.media.splice(this.carouselCursor, 1);
  372. }).catch(err => {
  373. swal('Whoops!', 'An error occured when attempting to delete this, please try again', 'error');
  374. });
  375. },
  376. mediaAltText() {
  377. return;
  378. // deprecate
  379. swal({
  380. text: 'Add a media description',
  381. content: "input"
  382. }).then(val => {
  383. let media = this.media[this.carouselCursor];
  384. media.alt = val;
  385. });
  386. },
  387. mediaLicense() {
  388. return;
  389. // deprecate
  390. swal({
  391. text: 'Add a media license',
  392. content: "input",
  393. button: {
  394. text: "Update",
  395. closeModal: true,
  396. },
  397. }).then(val => {
  398. let media = this.media[this.carouselCursor];
  399. media.license = val;
  400. });
  401. },
  402. compose() {
  403. let state = this.composeState;
  404. if(this.uploadProgress != 100 || this.ids.length == 0) {
  405. return;
  406. }
  407. if(this.composeText.length > this.config.uploader.max_caption_length) {
  408. swal('Error', 'Caption is too long', 'error');
  409. return;
  410. }
  411. switch(state) {
  412. case 'publish' :
  413. if(this.media.length == 0) {
  414. swal('Whoops!', 'You need to add media before you can save this!', 'warning');
  415. return;
  416. }
  417. if(this.composeText == 'Add optional caption...') {
  418. this.composeText = '';
  419. }
  420. let data = {
  421. media: this.media,
  422. caption: this.composeText,
  423. visibility: this.visibility,
  424. cw: this.nsfw
  425. };
  426. axios.post('/api/local/status/compose', data)
  427. .then(res => {
  428. let data = res.data;
  429. window.location.href = data;
  430. }).catch(err => {
  431. let msg = err.response.data.message ? err.response.data.message : 'An unexpected error occured.'
  432. swal('Oops, something went wrong!', msg, 'error');
  433. });
  434. return;
  435. break;
  436. case 'delete' :
  437. this.mediaDrawer = false;
  438. this.ids = [];
  439. this.media = [];
  440. this.carouselCursor = 0;
  441. this.composeText = '';
  442. this.composeTextLength = 0;
  443. $('#composeModal').modal('hide');
  444. return;
  445. break;
  446. }
  447. },
  448. about() {
  449. let text = document.createElement('div');
  450. text.innerHTML = `
  451. <p class="small font-weight-bold">Please visit the <a href="/site/kb/sharing-media">Sharing Media</a> page for more info.</p>
  452. `;
  453. swal({
  454. title: 'Compose UI v3',
  455. content: text,
  456. icon: 'info'
  457. });
  458. },
  459. closeModal() {
  460. this.composeType = '';
  461. $('#composeModal').modal('hide');
  462. },
  463. composeMessage() {
  464. let config = this.config;
  465. let composeType = this.composeType;
  466. let video = config.uploader.media_types.includes('video/mp4');
  467. return video ?
  468. 'Click here to add photos or videos' :
  469. 'Click here to add photos';
  470. },
  471. createCollection() {
  472. window.location.href = '/i/collections/create';
  473. },
  474. maxSize() {
  475. let limit = this.config.uploader.max_photo_size;
  476. return limit / 1000 + ' MB';
  477. },
  478. acceptedFormats() {
  479. let formats = this.config.uploader.media_types;
  480. return formats.split(',').map(f => {
  481. return ' ' + f.split('/')[1];
  482. }).toString();
  483. }
  484. }
  485. }
  486. </script>