app.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. require('./polyfill');
  2. window._ = require('lodash');
  3. window.Popper = require('popper.js').default;
  4. window.pixelfed = window.pixelfed || {};
  5. window.$ = window.jQuery = require('jquery');
  6. require('bootstrap');
  7. window.axios = require('axios');
  8. window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
  9. require('readmore-js');
  10. window.blurhash = require("blurhash");
  11. let token = document.head.querySelector('meta[name="csrf-token"]');
  12. if (token) {
  13. window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
  14. } else {
  15. console.error('CSRF token not found.');
  16. }
  17. window.App = window.App || {};
  18. window.App.redirect = function() {
  19. document.querySelectorAll('a').forEach(function(i,k) {
  20. let a = i.getAttribute('href');
  21. if(a && a.length > 5 && a.startsWith('https://')) {
  22. let url = new URL(a);
  23. if(url.host !== window.location.host && url.pathname !== '/i/redirect') {
  24. i.setAttribute('href', '/i/redirect?url=' + encodeURIComponent(a));
  25. }
  26. }
  27. });
  28. }
  29. window.App.boot = function() {
  30. new Vue({ el: '#content'});
  31. }
  32. window.addEventListener("load", () => {
  33. if ("serviceWorker" in navigator) {
  34. navigator.serviceWorker.register("/sw.js");
  35. }
  36. });
  37. window.App.util = {
  38. compose: {
  39. post: (function() {
  40. let path = window.location.pathname;
  41. let whitelist = [
  42. '/',
  43. '/timeline/public'
  44. ];
  45. if(whitelist.includes(path)) {
  46. $('#composeModal').modal('show');
  47. } else {
  48. window.location.href = '/?a=co';
  49. }
  50. }),
  51. circle: (function() {
  52. console.log('Unsupported method.');
  53. }),
  54. collection: (function() {
  55. console.log('Unsupported method.');
  56. }),
  57. loop: (function() {
  58. console.log('Unsupported method.');
  59. }),
  60. story: (function() {
  61. console.log('Unsupported method.');
  62. }),
  63. },
  64. time: (function() {
  65. return new Date;
  66. }),
  67. version: 1,
  68. format: {
  69. count: (function(count = 0, locale = 'en-GB', notation = 'compact') {
  70. if(count < 1) {
  71. return 0;
  72. }
  73. return new Intl.NumberFormat(locale, { notation: notation , compactDisplay: "short" }).format(count);
  74. }),
  75. timeAgo: (function(ts) {
  76. let date = Date.parse(ts);
  77. let seconds = Math.floor((new Date() - date) / 1000);
  78. let interval = Math.floor(seconds / 63072000);
  79. if (interval >= 1) {
  80. return interval + "y";
  81. }
  82. interval = Math.floor(seconds / 604800);
  83. if (interval >= 1) {
  84. return interval + "w";
  85. }
  86. interval = Math.floor(seconds / 86400);
  87. if (interval >= 1) {
  88. return interval + "d";
  89. }
  90. interval = Math.floor(seconds / 3600);
  91. if (interval >= 1) {
  92. return interval + "h";
  93. }
  94. interval = Math.floor(seconds / 60);
  95. if (interval >= 1) {
  96. return interval + "m";
  97. }
  98. return Math.floor(seconds) + "s";
  99. }),
  100. rewriteLinks: (function(i) {
  101. let tag = i.innerText;
  102. if(i.href.startsWith(window.location.origin)) {
  103. return i.href;
  104. }
  105. if(tag.startsWith('#') == true) {
  106. tag = '/discover/tags/' + tag.substr(1) +'?src=rph';
  107. } else if(tag.startsWith('@') == true) {
  108. tag = '/' + i.innerText + '?src=rpp';
  109. } else {
  110. tag = '/i/redirect?url=' + encodeURIComponent(tag);
  111. }
  112. return tag;
  113. })
  114. },
  115. filters: [
  116. ['1977','filter-1977'],
  117. ['Aden','filter-aden'],
  118. ['Amaro','filter-amaro'],
  119. ['Ashby','filter-ashby'],
  120. ['Brannan','filter-brannan'],
  121. ['Brooklyn','filter-brooklyn'],
  122. ['Charmes','filter-charmes'],
  123. ['Clarendon','filter-clarendon'],
  124. ['Crema','filter-crema'],
  125. ['Dogpatch','filter-dogpatch'],
  126. ['Earlybird','filter-earlybird'],
  127. ['Gingham','filter-gingham'],
  128. ['Ginza','filter-ginza'],
  129. ['Hefe','filter-hefe'],
  130. ['Helena','filter-helena'],
  131. ['Hudson','filter-hudson'],
  132. ['Inkwell','filter-inkwell'],
  133. ['Kelvin','filter-kelvin'],
  134. ['Kuno','filter-juno'],
  135. ['Lark','filter-lark'],
  136. ['Lo-Fi','filter-lofi'],
  137. ['Ludwig','filter-ludwig'],
  138. ['Maven','filter-maven'],
  139. ['Mayfair','filter-mayfair'],
  140. ['Moon','filter-moon'],
  141. ['Nashville','filter-nashville'],
  142. ['Perpetua','filter-perpetua'],
  143. ['Poprocket','filter-poprocket'],
  144. ['Reyes','filter-reyes'],
  145. ['Rise','filter-rise'],
  146. ['Sierra','filter-sierra'],
  147. ['Skyline','filter-skyline'],
  148. ['Slumber','filter-slumber'],
  149. ['Stinson','filter-stinson'],
  150. ['Sutro','filter-sutro'],
  151. ['Toaster','filter-toaster'],
  152. ['Valencia','filter-valencia'],
  153. ['Vesper','filter-vesper'],
  154. ['Walden','filter-walden'],
  155. ['Willow','filter-willow'],
  156. ['X-Pro II','filter-xpro-ii']
  157. ],
  158. filterCss: {
  159. 'filter-1977': 'sepia(.5) hue-rotate(-30deg) saturate(1.4)',
  160. 'filter-aden': 'sepia(.2) brightness(1.15) saturate(1.4)',
  161. 'filter-amaro': 'sepia(.35) contrast(1.1) brightness(1.2) saturate(1.3)',
  162. 'filter-ashby': 'sepia(.5) contrast(1.2) saturate(1.8)',
  163. 'filter-brannan': 'sepia(.4) contrast(1.25) brightness(1.1) saturate(.9) hue-rotate(-2deg)',
  164. 'filter-brooklyn': 'sepia(.25) contrast(1.25) brightness(1.25) hue-rotate(5deg)',
  165. 'filter-charmes': 'sepia(.25) contrast(1.25) brightness(1.25) saturate(1.35) hue-rotate(-5deg)',
  166. 'filter-clarendon': 'sepia(.15) contrast(1.25) brightness(1.25) hue-rotate(5deg)',
  167. 'filter-crema': 'sepia(.5) contrast(1.25) brightness(1.15) saturate(.9) hue-rotate(-2deg)',
  168. 'filter-dogpatch': 'sepia(.35) saturate(1.1) contrast(1.5)',
  169. 'filter-earlybird': 'sepia(.25) contrast(1.25) brightness(1.15) saturate(.9) hue-rotate(-5deg)',
  170. 'filter-gingham': 'contrast(1.1) brightness(1.1)',
  171. 'filter-ginza': 'sepia(.25) contrast(1.15) brightness(1.2) saturate(1.35) hue-rotate(-5deg)',
  172. 'filter-hefe': 'sepia(.4) contrast(1.5) brightness(1.2) saturate(1.4) hue-rotate(-10deg)',
  173. 'filter-helena': 'sepia(.5) contrast(1.05) brightness(1.05) saturate(1.35)',
  174. 'filter-hudson': 'sepia(.25) contrast(1.2) brightness(1.2) saturate(1.05) hue-rotate(-15deg)',
  175. 'filter-inkwell': 'brightness(1.25) contrast(.85) grayscale(1)',
  176. 'filter-kelvin': 'sepia(.15) contrast(1.5) brightness(1.1) hue-rotate(-10deg)',
  177. 'filter-juno': 'sepia(.35) contrast(1.15) brightness(1.15) saturate(1.8)',
  178. 'filter-lark': 'sepia(.25) contrast(1.2) brightness(1.3) saturate(1.25)',
  179. 'filter-lofi': 'saturate(1.1) contrast(1.5)',
  180. 'filter-ludwig': 'sepia(.25) contrast(1.05) brightness(1.05) saturate(2)',
  181. 'filter-maven': 'sepia(.35) contrast(1.05) brightness(1.05) saturate(1.75)',
  182. 'filter-mayfair': 'contrast(1.1) brightness(1.15) saturate(1.1)',
  183. 'filter-moon': 'brightness(1.4) contrast(.95) saturate(0) sepia(.35)',
  184. 'filter-nashville': 'sepia(.25) contrast(1.5) brightness(.9) hue-rotate(-15deg)',
  185. 'filter-perpetua': 'contrast(1.1) brightness(1.25) saturate(1.1)',
  186. 'filter-poprocket': 'sepia(.15) brightness(1.2)',
  187. 'filter-reyes': 'sepia(.75) contrast(.75) brightness(1.25) saturate(1.4)',
  188. 'filter-rise': 'sepia(.25) contrast(1.25) brightness(1.2) saturate(.9)',
  189. 'filter-sierra': 'sepia(.25) contrast(1.5) brightness(.9) hue-rotate(-15deg)',
  190. 'filter-skyline': 'sepia(.15) contrast(1.25) brightness(1.25) saturate(1.2)',
  191. 'filter-slumber': 'sepia(.35) contrast(1.25) saturate(1.25)',
  192. 'filter-stinson': 'sepia(.35) contrast(1.25) brightness(1.1) saturate(1.25)',
  193. 'filter-sutro': 'sepia(.4) contrast(1.2) brightness(.9) saturate(1.4) hue-rotate(-10deg)',
  194. 'filter-toaster': 'sepia(.25) contrast(1.5) brightness(.95) hue-rotate(-15deg)',
  195. 'filter-valencia': 'sepia(.25) contrast(1.1) brightness(1.1)',
  196. 'filter-vesper': 'sepia(.35) contrast(1.15) brightness(1.2) saturate(1.3)',
  197. 'filter-walden': 'sepia(.35) contrast(.8) brightness(1.25) saturate(1.4)',
  198. 'filter-willow': 'brightness(1.2) contrast(.85) saturate(.05) sepia(.2)',
  199. 'filter-xpro-ii': 'sepia(.45) contrast(1.25) brightness(1.75) saturate(1.3) hue-rotate(-5deg)'
  200. },
  201. emoji: ['😂','💯','❤️','🙌','👏','👌','😍','😯','😢','😅','😁','🙂','😎','😀','🤣','😃','😄','😆','😉','😊','😋','😘','😗','😙','😚','🤗','🤩','🤔','🤨','😐','😑','😶','🙄','😏','😣','😥','😮','🤐','😪','😫','😴','😌','😛','😜','😝','🤤','😒','😓','😔','😕','🙃','🤑','😲','🙁','😖','😞','😟','😤','😭','😦','😧','😨','😩','🤯','😬','😰','😱','😳','🤪','😵','😡','😠','🤬','😷','🤒','🤕','🤢','🤮','🤧','😇','🤠','🤡','🤥','🤫','🤭','🧐','🤓','😈','👿','👹','👺','💀','👻','👽','🤖','💩','😺','😸','😹','😻','😼','😽','🙀','😿','😾','🤲','👐','🤝','👍','👎','👊','✊','🤛','🤜','🤞','✌️','🤟','🤘','👈','👉','👆','👇','☝️','✋','🤚','🖐','🖖','👋','🤙','💪','🖕','✍️','🙏','💍','💄','💋','👄','👅','👂','👃','👣','👁','👀','🧠','🗣','👤','👥'
  202. ],
  203. embed: {
  204. post: (function(url, caption = true, likes = false, layout = 'full') {
  205. let u = url + '/embed?';
  206. u += caption ? 'caption=true&' : 'caption=false&';
  207. u += likes ? 'likes=true&' : 'likes=false&';
  208. u += layout == 'compact' ? 'layout=compact' : 'layout=full';
  209. return '<iframe src="'+u+'" class="pixelfed__embed" style="max-width: 100%; border: 0" width="400" allowfullscreen="allowfullscreen"></iframe><script async defer src="'+window.location.origin +'/embed.js"><\/script>';
  210. }),
  211. profile: (function(url) {
  212. let u = url + '/embed';
  213. return '<iframe src="'+u+'" class="pixelfed__embed" style="max-width: 100%; border: 0" width="400" allowfullscreen="allowfullscreen"></iframe><script async defer src="'+window.location.origin +'/embed.js"><\/script>';
  214. })
  215. },
  216. clipboard: (function(data) {
  217. return navigator.clipboard.writeText(data);
  218. }),
  219. navatar: (function() {
  220. $('#navbarDropdown .far').addClass('d-none');
  221. $('#navbarDropdown img').attr('src',window._sharedData.curUser.avatar)
  222. .removeClass('d-none')
  223. .addClass('rounded-circle border shadow')
  224. .attr('width', 34).attr('height', 34);
  225. })
  226. };