post_wizard.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Post Type Wizard - repeatable custom field rows.
  2. //
  3. // Every row contributes exactly one param_name, param_type, param_label,
  4. // param_placeholder and param_required, so the five arrive server side as
  5. // aligned arrays. This is why 'required' is a <select> and not a checkbox --
  6. // an unchecked checkbox submits nothing and would shift every later row.
  7. function addParam() {
  8. var tpl = document.getElementById('wizard-param-template');
  9. var host = document.getElementById('wizard-params');
  10. if ( tpl === null || host === null ) {
  11. console.log('post wizard param template missing');
  12. return false;
  13. }
  14. host.appendChild(document.importNode(tpl.content, true));
  15. // The row we just added is the last one; set its relation controls to
  16. // match whatever its type select says.
  17. var rows = host.querySelectorAll('.wizard-param-row');
  18. if (rows.length) {
  19. var added = rows[rows.length - 1].querySelector('.param-type');
  20. if (added) {
  21. syncRelationRow(added);
  22. }
  23. }
  24. return false;
  25. }
  26. // The relation selects are only meaningful for a relation field, but they must
  27. // stay in the DOM and keep submitting whatever the type is -- the server zips
  28. // the param_* arrays together positionally, so a row that submits fewer values
  29. // than its neighbours shifts every row after it. Hide, never remove.
  30. function syncRelationRow(select) {
  31. var row = select.closest('.wizard-param-row');
  32. if (!row) {
  33. return false;
  34. }
  35. var relation = row.querySelector('.wizard-relation');
  36. if (relation) {
  37. relation.style.display = select.value === 'relation' ? '' : 'none';
  38. }
  39. return false;
  40. }
  41. function delParam(button) {
  42. var row = button.closest('.wizard-param-row');
  43. if (row) {
  44. row.parentNode.removeChild(row);
  45. }
  46. return false;
  47. }
  48. // Start with one blank row, otherwise the feature isn't discoverable.
  49. document.addEventListener("DOMContentLoaded", function(event) {
  50. if ( document.getElementById('wizard-params') !== null ) {
  51. addParam();
  52. }
  53. });