create.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. let questionIndex = 0;
  2. // Initialize SortableJS
  3. document.addEventListener("DOMContentLoaded", function() {
  4. var canvas = document.getElementById('builder-canvas');
  5. if (canvas) {
  6. new Sortable(canvas, {
  7. animation: 150,
  8. handle: '.builder-handle',
  9. ghostClass: 'sortable-ghost',
  10. });
  11. }
  12. });
  13. function addQuestion(type) {
  14. const canvas = document.getElementById('builder-canvas');
  15. const emptyState = document.getElementById('empty-state');
  16. if (emptyState) {
  17. emptyState.remove();
  18. }
  19. questionIndex++;
  20. const id = 'q_' + Date.now() + '_' + questionIndex;
  21. let typeLabel = '';
  22. const needsOptions = ['single_choice', 'multiple_choice', 'dropdown'].includes(type);
  23. switch(type) {
  24. case 'text': typeLabel = 'Text Input'; break;
  25. case 'textarea': typeLabel = 'Text Area'; break;
  26. case 'single_choice': typeLabel = 'Single Choice'; break;
  27. case 'multiple_choice': typeLabel = 'Multiple Choice'; break;
  28. case 'dropdown': typeLabel = 'Dropdown'; break;
  29. }
  30. const div = document.createElement('div');
  31. div.className = 'builder-item';
  32. div.dataset.id = id;
  33. div.dataset.type = type;
  34. let html = `
  35. <div class="builder-handle">
  36. <span>☰ ${typeLabel}</span>
  37. <button type="button" class="modal-close" style="position:relative; top:0; right:0;" onclick="removeQuestion(this)">&times;</button>
  38. </div>
  39. <div class="form-group" style="margin-bottom:0;">
  40. <label>Question Label</label>
  41. <input type="text" class="question-label" required placeholder="Enter your question here">
  42. </div>
  43. `;
  44. if (needsOptions) {
  45. html += `
  46. <div class="form-group mt-2 mb-0">
  47. <label>Options (one per line)</label>
  48. <textarea class="question-options" rows="3" required placeholder="Option 1\nOption 2\nOption 3"></textarea>
  49. </div>
  50. `;
  51. }
  52. div.innerHTML = html;
  53. canvas.appendChild(div);
  54. }
  55. function removeQuestion(btn) {
  56. btn.closest('.builder-item').remove();
  57. const canvas = document.getElementById('builder-canvas');
  58. if (canvas.children.length === 0) {
  59. canvas.innerHTML = '<div class="alert alert-info" id="empty-state">No questions added yet. Use the buttons above to add one.</div>';
  60. }
  61. }
  62. function prepareSubmission(e) {
  63. const questions = [];
  64. const items = document.querySelectorAll('.builder-item');
  65. if (items.length === 0) {
  66. e.preventDefault();
  67. alert("Please add at least one question.");
  68. return;
  69. }
  70. let hasEmptyLabel = false;
  71. let hasEmptyOptions = false;
  72. items.forEach((item) => {
  73. let labelInput = item.querySelector('.question-label');
  74. let optionsInput = item.querySelector('.question-options');
  75. let label = labelInput ? labelInput.value.trim() : '';
  76. if (!label) {
  77. hasEmptyLabel = true;
  78. }
  79. let options = [];
  80. if (optionsInput) {
  81. options = optionsInput.value.split('\\n').map(o => o.trim()).filter(o => o);
  82. if (options.length === 0) {
  83. hasEmptyOptions = true;
  84. }
  85. }
  86. questions.push({
  87. id: item.dataset.id,
  88. type: item.dataset.type,
  89. label: label,
  90. options: options
  91. });
  92. });
  93. if (hasEmptyLabel) {
  94. e.preventDefault();
  95. alert("Please fill out all question labels.");
  96. return;
  97. }
  98. if (hasEmptyOptions) {
  99. e.preventDefault();
  100. alert("Please provide at least one option for your choice/dropdown questions.");
  101. return;
  102. }
  103. document.getElementById('questions_input').value = JSON.stringify(questions);
  104. }