plugin_admin.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /** @licstart The following is the entire license notice for the
  2. * JavaScript code in this page.
  3. *
  4. * Copyright: (c) 2011-2015 Sébastien SAUVAGE <sebsauvage@sebsauvage.net>
  5. * (c) 2011-2017 The Shaarli Community, see AUTHORS
  6. *
  7. * This software is provided 'as-is', without any express or implied warranty.
  8. * In no event will the authors be held liable for any damages arising from
  9. * the use of this software.
  10. *
  11. * Permission is granted to anyone to use this software for any purpose,
  12. * including commercial applications, and to alter it and redistribute it
  13. * freely, subject to the following restrictions:
  14. *
  15. * 1. The origin of this software must not be misrepresented; you must not
  16. * claim that you wrote the original software. If you use this software
  17. * in a product, an acknowledgment in the product documentation would
  18. * be appreciated but is not required.
  19. *
  20. * 2. Altered source versions must be plainly marked as such, and must
  21. * not be misrepresented as being the original software.
  22. *
  23. * 3. This notice may not be removed or altered from any source distribution.
  24. *
  25. * @licend The above is the entire license notice
  26. * for the JavaScript code in this page.
  27. */
  28. /**
  29. * Change the position counter of a row.
  30. *
  31. * @param elem Element Node to change.
  32. * @param toPos int New position.
  33. */
  34. function changePos(elem, toPos)
  35. {
  36. var elemName = elem.getAttribute('data-line')
  37. elem.setAttribute('data-order', toPos);
  38. var hiddenInput = document.querySelector('[name="order_'+ elemName +'"]');
  39. hiddenInput.setAttribute('value', toPos);
  40. }
  41. /**
  42. * Move a row up or down.
  43. *
  44. * @param pos Element Node to move.
  45. * @param move int Move: +1 (down) or -1 (up)
  46. */
  47. function changeOrder(pos, move)
  48. {
  49. var newpos = parseInt(pos) + move;
  50. var lines = document.querySelectorAll('[data-order="'+ pos +'"]');
  51. var changelines = document.querySelectorAll('[data-order="'+ newpos +'"]');
  52. // If we go down reverse lines to preserve the rows order
  53. if (move > 0) {
  54. lines = [].slice.call(lines).reverse();
  55. }
  56. for (var i = 0 ; i < lines.length ; i++) {
  57. var parent = changelines[0].parentNode;
  58. changePos(lines[i], newpos);
  59. changePos(changelines[i], parseInt(pos));
  60. var changeItem = move < 0 ? changelines[0] : changelines[changelines.length - 1].nextSibling;
  61. parent.insertBefore(lines[i], changeItem);
  62. }
  63. }
  64. /**
  65. * Move a row up in the table.
  66. *
  67. * @param pos int row counter.
  68. *
  69. * @returns false
  70. */
  71. function orderUp(pos)
  72. {
  73. if (pos == 0) {
  74. return false;
  75. }
  76. changeOrder(pos, -1);
  77. return false;
  78. }
  79. /**
  80. * Move a row down in the table.
  81. *
  82. * @param pos int row counter.
  83. *
  84. * @returns false
  85. */
  86. function orderDown(pos)
  87. {
  88. var lastpos = document.querySelector('[data-order]:last-child').getAttribute('data-order');
  89. if (pos == lastpos) {
  90. return false;
  91. }
  92. changeOrder(pos, +1);
  93. return false;
  94. }