File size: 4,029 Bytes
b5ae423
2e940ea
 
f5b314d
 
 
fbe3d46
b5ae423
 
f5b314d
 
b5ae423
f5b314d
 
2e940ea
 
 
 
 
b5ae423
 
 
 
 
392cc41
b5ae423
 
 
f5b314d
392cc41
 
f5b314d
 
 
 
 
 
 
 
 
b5ae423
f5b314d
 
 
 
 
 
 
 
 
 
 
 
 
 
2e940ea
 
 
392cc41
 
 
 
2e940ea
 
392cc41
2e940ea
 
 
 
 
 
 
 
 
392cc41
2e940ea
 
392cc41
 
 
 
2e940ea
 
 
 
 
392cc41
 
 
2e940ea
 
 
 
 
 
 
 
392cc41
 
2e940ea
 
392cc41
2e940ea
 
 
 
392cc41
2e940ea
392cc41
2e940ea
 
392cc41
 
2e940ea
392cc41
 
 
2e940ea
392cc41
2e940ea
392cc41
 
2e940ea
 
 
 
 
 
392cc41
 
 
 
 
f5b314d
 
 
 
 
b5ae423
 
 
 
f5b314d
2e940ea
a8b2966
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const descriptons = {
  estructurada: "Establece número de segmentos:",
  continua: "Establece la longitud de la escala:",
  atributos:
    "Con el estilo atributos elijes las palabras para evaluar los productos",
  vocabulario:
    "Los vocabularios son un conjunto de palabras específicas para evaluar aspectos de un producto",
};

let inputsScale;
let inputTamano;

let inputsStyle;
let helpStyle;
let sizeOptionsContainer;
const SIZE_OPTIONS = {
  estructurada: [5, 7, 9],
  continua: [9, 13, 15],
};

initPanel();

function initPanel() {
  initRadios();
  initSizeOptions();
}

function initRadios() {
  inputTamano = document.getElementsByName("tamano_escala").item(0);
  inputsScale = document.getElementsByName("tipo_escala");
  inputsScale.item(0).checked = true;

  for (let index = 0; index < inputsScale.length; index++) {
    let parent = inputsScale.item(index).parentElement;
    parent.addEventListener("click", () => {
      showDescriptionTamanoScale(parent);
    });
    if (inputsScale.item(index).checked) {
      showDescriptionTamanoScale(parent);
    }
  }

  labelsStyle = document.getElementsByClassName("ct-radio-estilo");
  helpStyle = document.getElementsByClassName("ct-ayuda-estilo")[0];
  inputsStyle = document.getElementsByName("estilo_palabras");

  for (let index = 0; index < inputsStyle.length; index++) {
    let parent = inputsStyle.item(index).parentElement;
    parent.addEventListener("click", () => {
      showDescriptionStyle(parent);
    });
    if (inputsStyle.item(index).checked) {
      showDescriptionStyle(parent);
    }
  }
}

function initSizeOptions() {
  sizeOptionsContainer = document.getElementsByClassName(
    "cts-options-size-scale"
  )[0];

  for (let i = 0; i < inputsScale.length; i++) {
    const radio = inputsScale.item(i);
    radio.addEventListener("change", () => {
      const tag = getTagFromLabel(radio);
      populateSizeOptions(tag);
    });
    if (radio.checked) {
      const tag = getTagFromLabel(radio);
      populateSizeOptions(tag);
    }
  }

  const form = document.querySelector("form");
  if (!form) return;

  form.addEventListener("submit", (e) => {
    const chosen = document.querySelector(
      'input[name="option_size_scale"]:checked'
    );
    if (chosen && inputTamano) {
      inputTamano.value = chosen.value;
    }

    if (sizeOptionsContainer) {
      const toRemove = sizeOptionsContainer.querySelectorAll(
        'input[name="option_size_scale"]'
      );
      toRemove.forEach((el) => el.remove());
    }
  });
}

function getTagFromLabel(radio) {
  try {
    const parent = radio.parentElement;
    if (!parent) return "";
    const text = parent.textContent || "";
    return text.trim().split(/\s+/)[0].toLowerCase();
  } catch (err) {
    return "";
  }
}

function populateSizeOptions(tag) {
  const options = SIZE_OPTIONS[tag] || SIZE_OPTIONS["estructurada"];
  if (!sizeOptionsContainer) return;
  sizeOptionsContainer.innerHTML = "";

  options.forEach((val) => {
    const label = document.createElement("label");
    label.className = "flex flex-col items-center cursor-pointer";

    const input = document.createElement("input");
    input.type = "radio";
    input.name = "option_size_scale";
    input.value = String(val);
    input.className = "radio radio-lg checked:bg-pink-500";

    const span = document.createElement("span");
    span.className = "mt-2 text-xl text-gray-700 font-medium";
    span.textContent = String(val);

    label.appendChild(input);
    label.appendChild(span);
    sizeOptionsContainer.appendChild(label);
  });

  const firstRadioSize = document
    .getElementsByName("option_size_scale")
    .item(0);
  firstRadioSize.checked = true;
}

function showDescriptionStyle(label) {
  const text = label.textContent.trim();
  helpStyle.textContent = descriptons[text];
}

function showDescriptionTamanoScale(label) {
  const text = label.textContent.trim();
  let parent = inputTamano.parentElement;
  parent.getElementsByTagName("p")[0].textContent = descriptons[text];
}