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.
 
 
 
 
 

130 lines
3.5 KiB

  1. /**
  2. * Rename dialog for end users
  3. *
  4. * @author Andreas Gohr <gohr@cosmocode.de>
  5. */
  6. (function () {
  7. if (!JSINFO || !JSINFO.move_renameokay) return;
  8. // basic dialog template
  9. const $dialog = jQuery(
  10. '<div>' +
  11. '<form>' +
  12. '<label>' + LANG.plugins.move.newname + '<br>' +
  13. '<input type="text" name="id" style="width:100%">' +
  14. '</label>' +
  15. '</form>' +
  16. '</div>'
  17. );
  18. /**
  19. * Executes the renaming based on the form contents
  20. * @return {boolean}
  21. */
  22. const renameFN = function () {
  23. const newid = $dialog.find('input[name=id]').val();
  24. if (!newid) return false;
  25. // remove buttons and show throbber
  26. $dialog.html(
  27. '<img src="' + DOKU_BASE + 'lib/images/throbber.gif" /> ' +
  28. LANG.plugins.move.inprogress
  29. );
  30. $dialog.dialog('option', 'buttons', []);
  31. // post the data
  32. jQuery.post(
  33. DOKU_BASE + 'lib/exe/ajax.php',
  34. {
  35. call: 'plugin_move_rename',
  36. id: JSINFO.id,
  37. newid: newid
  38. },
  39. // redirect or display error
  40. function (result) {
  41. if (result.error) {
  42. $dialog.html(result.error.msg);
  43. } else {
  44. window.location.href = result.redirect_url;
  45. }
  46. }
  47. );
  48. return false;
  49. };
  50. /**
  51. * Create the actual dialog modal and show it
  52. */
  53. const showDialog = function () {
  54. $dialog.dialog({
  55. title: LANG.plugins.move.rename + ' ' + JSINFO.id,
  56. width: 800,
  57. height: 200,
  58. dialogClass: 'plugin_move_dialog',
  59. modal: true,
  60. buttons: [
  61. {
  62. text: LANG.plugins.move.cancel,
  63. click: function () {
  64. $dialog.dialog("close");
  65. }
  66. },
  67. {
  68. text: LANG.plugins.move.rename,
  69. click: renameFN
  70. }
  71. ],
  72. // remove HTML from DOM again
  73. close: function () {
  74. jQuery(this).remove();
  75. }
  76. });
  77. $dialog.find('input[name=id]').val(JSINFO.id);
  78. $dialog.find('form').submit(renameFN);
  79. };
  80. /**
  81. * Bind an event handler as the first handler
  82. *
  83. * @param {jQuery} $owner
  84. * @param {string} event
  85. * @param {function} handler
  86. * @link https://stackoverflow.com/a/4700103
  87. */
  88. const bindFirst = function ($owner, event, handler) {
  89. $owner.unbind(event, handler);
  90. $owner.bind(event, handler);
  91. const events = jQuery._data($owner[0])['events'][event];
  92. events.unshift(events.pop());
  93. jQuery._data($owner[0])['events'][event] = events;
  94. };
  95. // attach handler to menu item
  96. jQuery('.plugin_move_page')
  97. .show()
  98. .click(function (e) {
  99. e.preventDefault();
  100. showDialog();
  101. });
  102. // attach handler to mobile menu entry
  103. const $mobileMenuOption = jQuery('form select[name=do] option[value=plugin_move]');
  104. if ($mobileMenuOption.length === 1) {
  105. bindFirst($mobileMenuOption.closest('select[name=do]'), 'change', function (e) {
  106. const $select = jQuery(this);
  107. if ($select.val() !== 'plugin_move') return;
  108. e.preventDefault();
  109. e.stopPropagation();
  110. e.stopImmediatePropagation();
  111. $select.val('');
  112. showDialog();
  113. });
  114. }
  115. })();