You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

common-global.js 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import {mqBinarySearch} from '../utils.js';
  2. import createDropzone from './dropzone.js';
  3. import {initCompColorPicker} from './comp/ColorPicker.js';
  4. import 'jquery.are-you-sure';
  5. const {csrfToken} = window.config;
  6. export function initGlobalFormDirtyLeaveConfirm() {
  7. // Warn users that try to leave a page after entering data into a form.
  8. // Except on sign-in pages, and for forms marked as 'ignore-dirty'.
  9. if ($('.user.signin').length === 0) {
  10. $('form:not(.ignore-dirty)').areYouSure();
  11. }
  12. }
  13. export function initHeadNavbarContentToggle() {
  14. const content = $('#navbar');
  15. const toggle = $('#navbar-expand-toggle');
  16. let isExpanded = false;
  17. toggle.on('click', () => {
  18. isExpanded = !isExpanded;
  19. if (isExpanded) {
  20. content.addClass('shown');
  21. toggle.addClass('active');
  22. } else {
  23. content.removeClass('shown');
  24. toggle.removeClass('active');
  25. }
  26. });
  27. }
  28. export function initFootLanguageMenu() {
  29. function linkLanguageAction() {
  30. const $this = $(this);
  31. $.post($this.data('url')).always(() => {
  32. window.location.reload();
  33. });
  34. }
  35. $('.language-menu a[lang]').on('click', linkLanguageAction);
  36. }
  37. export function initGlobalEnterQuickSubmit() {
  38. $('.js-quick-submit').on('keydown', function (e) {
  39. if (((e.ctrlKey && !e.altKey) || e.metaKey) && (e.keyCode === 13 || e.keyCode === 10)) {
  40. $(this).closest('form').trigger('submit');
  41. }
  42. });
  43. }
  44. export function initGlobalButtonClickOnEnter() {
  45. $(document).on('keypress', '.ui.button', (e) => {
  46. if (e.keyCode === 13 || e.keyCode === 32) { // enter key or space bar
  47. $(e.target).trigger('click');
  48. }
  49. });
  50. }
  51. export function initGlobalCommon() {
  52. // Show exact time
  53. $('.time-since').each(function () {
  54. $(this)
  55. .addClass('poping up')
  56. .attr('data-content', $(this).attr('title'))
  57. .attr('data-variation', 'inverted tiny')
  58. .attr('title', '');
  59. });
  60. // Undo Safari emoji glitch fix at high enough zoom levels
  61. if (navigator.userAgent.match('Safari')) {
  62. $(window).resize(() => {
  63. const px = mqBinarySearch('width', 0, 4096, 1, 'px');
  64. const em = mqBinarySearch('width', 0, 1024, 0.01, 'em');
  65. if (em * 16 * 1.25 - px <= -1) {
  66. $('body').addClass('safari-above125');
  67. } else {
  68. $('body').removeClass('safari-above125');
  69. }
  70. });
  71. }
  72. // Semantic UI modules.
  73. $('.dropdown:not(.custom)').dropdown({
  74. fullTextSearch: 'exact'
  75. });
  76. $('.jump.dropdown').dropdown({
  77. action: 'hide',
  78. onShow() {
  79. $('.poping.up').popup('hide');
  80. },
  81. fullTextSearch: 'exact'
  82. });
  83. $('.slide.up.dropdown').dropdown({
  84. transition: 'slide up',
  85. fullTextSearch: 'exact'
  86. });
  87. $('.upward.dropdown').dropdown({
  88. direction: 'upward',
  89. fullTextSearch: 'exact'
  90. });
  91. $('.ui.checkbox').checkbox();
  92. $('.ui.progress').progress({
  93. showActivity: false
  94. });
  95. $('.poping.up').attr('data-variation', 'inverted tiny').popup();
  96. $('.top.menu .poping.up').popup({
  97. onShow() {
  98. if ($('.top.menu .menu.transition').hasClass('visible')) {
  99. return false;
  100. }
  101. }
  102. });
  103. $('.tabular.menu .item').tab();
  104. $('.tabable.menu .item').tab();
  105. $('.toggle.button').on('click', function () {
  106. $($(this).data('target')).slideToggle(100);
  107. });
  108. // make table <tr> and <td> elements clickable like a link
  109. $('tr[data-href], td[data-href]').on('click', function (e) {
  110. const href = $(this).data('href');
  111. if (e.target.nodeName === 'A') {
  112. // if a user clicks on <a>, then the <tr> or <td> should not act as a link.
  113. return;
  114. }
  115. if (e.ctrlKey || e.metaKey) {
  116. // ctrl+click or meta+click opens a new window in modern browsers
  117. window.open(href);
  118. } else {
  119. window.location = href;
  120. }
  121. });
  122. }
  123. export function initGlobalDropzone() {
  124. // Dropzone
  125. for (const el of document.querySelectorAll('.dropzone')) {
  126. const $dropzone = $(el);
  127. const _promise = createDropzone(el, {
  128. url: $dropzone.data('upload-url'),
  129. headers: {'X-Csrf-Token': csrfToken},
  130. maxFiles: $dropzone.data('max-file'),
  131. maxFilesize: $dropzone.data('max-size'),
  132. acceptedFiles: (['*/*', ''].includes($dropzone.data('accepts'))) ? null : $dropzone.data('accepts'),
  133. addRemoveLinks: true,
  134. dictDefaultMessage: $dropzone.data('default-message'),
  135. dictInvalidFileType: $dropzone.data('invalid-input-type'),
  136. dictFileTooBig: $dropzone.data('file-too-big'),
  137. dictRemoveFile: $dropzone.data('remove-file'),
  138. timeout: 0,
  139. thumbnailMethod: 'contain',
  140. thumbnailWidth: 480,
  141. thumbnailHeight: 480,
  142. init() {
  143. this.on('success', (_file, data) => {
  144. const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
  145. $dropzone.find('.files').append(input);
  146. });
  147. this.on('removedfile', (file) => {
  148. $(`#${file.uuid}`).remove();
  149. if ($dropzone.data('remove-url')) {
  150. $.post($dropzone.data('remove-url'), {
  151. file: file.uuid,
  152. _csrf: csrfToken,
  153. });
  154. }
  155. });
  156. },
  157. });
  158. }
  159. }
  160. export function initGlobalLinkActions() {
  161. function showDeletePopup() {
  162. const $this = $(this);
  163. const dataArray = $this.data();
  164. let filter = '';
  165. if ($this.data('modal-id')) {
  166. filter += `#${$this.data('modal-id')}`;
  167. }
  168. const dialog = $(`.delete.modal${filter}`);
  169. dialog.find('.name').text($this.data('name'));
  170. for (const [key, value] of Object.entries(dataArray)) {
  171. if (key && key.startsWith('data')) {
  172. dialog.find(`.${key}`).text(value);
  173. }
  174. }
  175. dialog.modal({
  176. closable: false,
  177. onApprove() {
  178. if ($this.data('type') === 'form') {
  179. $($this.data('form')).trigger('submit');
  180. return;
  181. }
  182. const postData = {
  183. _csrf: csrfToken,
  184. };
  185. for (const [key, value] of Object.entries(dataArray)) {
  186. if (key && key.startsWith('data')) {
  187. postData[key.substr(4)] = value;
  188. }
  189. if (key === 'id') {
  190. postData['id'] = value;
  191. }
  192. }
  193. $.post($this.data('url'), postData).done((data) => {
  194. window.location.href = data.redirect;
  195. });
  196. }
  197. }).modal('show');
  198. return false;
  199. }
  200. function showAddAllPopup() {
  201. const $this = $(this);
  202. let filter = '';
  203. if ($this.attr('id')) {
  204. filter += `#${$this.attr('id')}`;
  205. }
  206. const dialog = $(`.addall.modal${filter}`);
  207. dialog.find('.name').text($this.data('name'));
  208. dialog.modal({
  209. closable: false,
  210. onApprove() {
  211. if ($this.data('type') === 'form') {
  212. $($this.data('form')).trigger('submit');
  213. return;
  214. }
  215. $.post($this.data('url'), {
  216. _csrf: csrfToken,
  217. id: $this.data('id')
  218. }).done((data) => {
  219. window.location.href = data.redirect;
  220. });
  221. }
  222. }).modal('show');
  223. return false;
  224. }
  225. function linkAction(e) {
  226. e.preventDefault();
  227. const $this = $(this);
  228. const redirect = $this.data('redirect');
  229. $.post($this.data('url'), {
  230. _csrf: csrfToken
  231. }).done((data) => {
  232. if (data.redirect) {
  233. window.location.href = data.redirect;
  234. } else if (redirect) {
  235. window.location.href = redirect;
  236. } else {
  237. window.location.reload();
  238. }
  239. });
  240. }
  241. // Helpers.
  242. $('.delete-button').on('click', showDeletePopup);
  243. $('.link-action').on('click', linkAction);
  244. // FIXME: this function is only used once, and not common, not well designed. should be refactored later
  245. $('.add-all-button').on('click', showAddAllPopup);
  246. // FIXME: this is only used once, and should be replace with `link-action` instead
  247. $('.undo-button').on('click', function () {
  248. const $this = $(this);
  249. $.post($this.data('url'), {
  250. _csrf: csrfToken,
  251. id: $this.data('id')
  252. }).done((data) => {
  253. window.location.href = data.redirect;
  254. });
  255. });
  256. }
  257. export function initGlobalButtons() {
  258. $('.show-panel.button').on('click', function () {
  259. $($(this).data('panel')).show();
  260. });
  261. $('.hide-panel.button').on('click', function (event) {
  262. $($(this).data('panel')).hide();
  263. event.preventDefault();
  264. });
  265. $('.show-modal.button').on('click', function () {
  266. $($(this).data('modal')).modal('show');
  267. const colorPickers = $($(this).data('modal')).find('.color-picker');
  268. if (colorPickers.length > 0) {
  269. initCompColorPicker();
  270. }
  271. });
  272. $('.delete-post.button').on('click', function () {
  273. const $this = $(this);
  274. $.post($this.data('request-url'), {
  275. _csrf: csrfToken
  276. }).done(() => {
  277. window.location.href = $this.data('done-url');
  278. });
  279. });
  280. }