post_wizard.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Post Type Wizard - repeatable custom field rows.
  2. //
  3. // Every row contributes exactly one param_name, param_type, param_label,
  4. // param_placeholder, param_required, param_private and param_indexed, so they
  5. // arrive server side as aligned arrays. This is why 'required' is a <select>
  6. // and not a checkbox -- an unchecked checkbox submits nothing and would shift
  7. // every later row.
  8. //
  9. // 'Index this field' is a real checkbox because it reads like one, and keeps
  10. // the guarantee anyway: the checkbox itself has no name and never submits, and
  11. // the hidden input beside it -- which always submits, exactly once -- is what
  12. // syncIndexedRow keeps in step with it.
  13. function addParam() {
  14. var tpl = document.getElementById('wizard-param-template');
  15. var host = document.getElementById('wizard-params');
  16. if ( tpl === null || host === null ) {
  17. console.log('post wizard param template missing');
  18. return false;
  19. }
  20. host.appendChild(document.importNode(tpl.content, true));
  21. // The row we just added is the last one; set its relation controls to
  22. // match whatever its type select says.
  23. var rows = host.querySelectorAll('.wizard-param-row');
  24. if (rows.length) {
  25. var added = rows[rows.length - 1].querySelector('.param-type');
  26. if (added) {
  27. syncRelationRow(added);
  28. }
  29. }
  30. return false;
  31. }
  32. // The relation selects are only meaningful for a relation field, but they must
  33. // stay in the DOM and keep submitting whatever the type is -- the server zips
  34. // the param_* arrays together positionally, so a row that submits fewer values
  35. // than its neighbours shifts every row after it. Hide, never remove.
  36. function syncRelationRow(select) {
  37. var row = select.closest('.wizard-param-row');
  38. if (!row) {
  39. return false;
  40. }
  41. var relation = row.querySelector('.wizard-relation');
  42. if (relation) {
  43. relation.style.display = select.value === 'relation' ? '' : 'none';
  44. }
  45. return false;
  46. }
  47. // Copy a row's index checkbox into the hidden input that actually submits.
  48. function syncIndexedRow(checkbox) {
  49. var row = checkbox.closest('.wizard-param-row');
  50. if (!row) {
  51. return false;
  52. }
  53. var hidden = row.querySelector('input[name="param_indexed"]');
  54. if (hidden) {
  55. hidden.value = checkbox.checked ? '1' : '0';
  56. }
  57. return false;
  58. }
  59. function delParam(button) {
  60. var row = button.closest('.wizard-param-row');
  61. if (row) {
  62. row.parentNode.removeChild(row);
  63. }
  64. return false;
  65. }
  66. // Start with one blank row, otherwise the feature isn't discoverable.
  67. document.addEventListener("DOMContentLoaded", function(event) {
  68. if ( document.getElementById('wizard-params') !== null ) {
  69. addParam();
  70. }
  71. });