ComposeModal.vue 17 KB

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