add.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import $ from 'jquery';
  2. import '../../utils/jquery-utils';
  3. import request from '../../utils/request';
  4. import { config } from 'grav-config';
  5. let custom = false;
  6. let folder = $('[data-remodal-id="modal"] input[name="data[folder]"], [data-remodal-id="modular"] input[name="data[folder]"]');
  7. let title = $('[data-remodal-id="modal"] input[name="data[title]"], [data-remodal-id="modular"] input[name="data[title]"]');
  8. let getFields = (type, target) => {
  9. target = $(target);
  10. let query = `[data-remodal-id="${target.closest('[data-remodal-id]').data('remodal-id')}"]`;
  11. return {
  12. title: type === 'title' ? $(target) : $(`${query} input[name="data[title]"]`),
  13. folder: type === 'folder' ? $(target) : $(`${query} input[name="data[folder]"]`)
  14. };
  15. };
  16. title.on('input focus blur', (event) => {
  17. if (custom) { return true; }
  18. let elements = getFields('title', event.currentTarget);
  19. let slug = $.slugify(elements.title.val(), {custom: {"'": ''}});
  20. elements.folder.val(slug);
  21. });
  22. folder.on('input', (event) => {
  23. let elements = getFields('folder', event.currentTarget);
  24. let input = elements.folder.get(0);
  25. let value = elements.folder.val();
  26. let selection = {
  27. start: input.selectionStart,
  28. end: input.selectionEnd
  29. };
  30. value = value.toLowerCase().replace(/\s/g, '-').replace(/[^a-z0-9_\-]/g, '');
  31. elements.folder.val(value);
  32. custom = !!value;
  33. // restore cursor position
  34. input.setSelectionRange(selection.start, selection.end);
  35. });
  36. folder.on('focus blur', (event) => getFields('title', event.currentTarget).title.trigger('input'));
  37. $(document).on('change', '[name="data[route]"]', (event) => {
  38. const rawroute = $(event.currentTarget).val();
  39. const pageTemplate = $('[name="data[name]"]');
  40. const URI = `${config.base_url_relative}/ajax.json/task${config.param_sep}getChildTypes`;
  41. if (pageTemplate.length === 0) {
  42. return;
  43. }
  44. request(URI, {
  45. method: 'post',
  46. body: { rawroute }
  47. }, (response) => {
  48. const type = response.child_type;
  49. if (type !== '' && type !== 'default') {
  50. pageTemplate.val(type);
  51. pageTemplate.data('selectize').setValue(type);
  52. }
  53. });
  54. });