Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.06 KB | None | 0 0
  1. function openPage(pageName, elmnt) {
  2. var i, tabcontent, tablinks;
  3. // Get all elements with class="tabcontent" and hide them
  4. tabcontent = document.getElementsByClassName("tabcontent");
  5. for (i = 0; i < tabcontent.length; i++) {
  6. tabcontent[i].style.display = "none";
  7. }
  8. //Get all elements with class="tablinks" and remove the class "active"
  9. tablinks = document.getElementsByClassName("tablink");
  10. for (i = 0; i < tablinks.length; i++) {
  11. tablinks[i].style.backgroundColor = "";
  12. }
  13. // Show the specific tab content
  14. document.getElementById(pageName).style.display = "block";
  15. // Add the specific color to the button used to open the tab content
  16. elmnt.style.backgroundColor = "white";
  17. }
  18.  
  19. // Get the element with id="defaultOpen" and click on it
  20. document.getElementById("defaultOpen").click();
  21.  
  22.  
  23.  
  24.  
  25.  
  26. var formulars = [ [{ formularName: "melisa"}] ];
  27. var formularForHide = "null";
  28. var elementNum = 1, btnId = 0; // elementNum is the order number of Element, and btnId is for setting add button
  29.  
  30.  
  31. function searchFormular(fName){
  32. if ( document.getElementById('Administration').getElementsByTagName('div').length != 0 ) {
  33. hideFormular(formularForHide);
  34. }
  35. for (var j = 0; j < formulars.length; j++){
  36. if ( fName == formulars[j][0].formularName ){
  37. if ( formularForHide != "null") {
  38. //hideFormular(formularForHide);
  39. //fName.parentNode.getElementsByName
  40. document.getElementById('Administration').getElementsByName(formularForHide)[0].style.display = "none";
  41. }
  42. /* displaysFormular(fName);
  43. return;
  44. */
  45. document.getElementById('Administration').getElementsByName(formularForHide)[0].style.display = "block";
  46. formularForHide = fName;
  47. return;
  48. }
  49. }
  50. // make a template for new formular
  51. /* elementNum = 1;
  52. btnId = 0;
  53. var d = document.getElementById('Administration');
  54. document.getElementById('saveButton').style.display = "block";
  55. addElement(); */
  56. makeFormular(fName);
  57. }
  58.  
  59. function displaysFormular(fName){
  60. var formElements = document.getElementsByClassName(fName);
  61. var max = formElements.length;
  62. for (var i = 0; i < max; i++ ){
  63. formElements[i].style.display = "block";
  64. }
  65. //document.getElementsByTagName('div')[1].getElementsByTagName('button')[0].style.visibility = "";
  66. var d = document.getElementById('Administration');
  67. document.getElementById('saveButton').style.display = "block";
  68. formularForHide = fName;
  69. }
  70.  
  71. // This function make a template for new formular
  72. function makeFormular(fName){
  73. elementNum = 1;
  74. btnId = 0;
  75. var tab = document.getElementById('Administration');
  76. var div = document.createElement('div');
  77. div.setAttribute("class",fName);
  78. div.style.display = "block";
  79. //tab.insertBefore(div,document.getElementById("saveButton"));
  80. tab.appendChild(div);
  81.  
  82. var saveBtn = document.createElement('button');
  83. saveBtn.setAttribute("id",'saveButton');
  84. saveBtn.setAttribute("onclick",'saveFormular(this)');
  85. saveBtn.innerHTML = "Save";
  86. div.appendChild(saveBtn);
  87. //saveBtn.style.display = "block";
  88. addElement(div);
  89. }
  90.  
  91.  
  92. // Adds an element to the document
  93. function addElement(parent) {
  94. var fName = document.getElementById('formularName').value;
  95. var div = document.createElement('div');
  96. //var parent = document.getElementById('Administration').getElementByName(fName);
  97. //div.setAttribute("class",fName);
  98. div.innerHTML = "Element " + elementNum + ": ";
  99. parent.insertBefore(div,parent.getElementsByTagName('button')[0]);
  100. //div.style.display = "block";
  101.  
  102. var input = document.createElement('input');
  103. div.appendChild(input);
  104.  
  105. var slc1 = addSelect(div,'firstSelect',['Textbox','Checkbox','Radio buttons']);
  106. slc1.setAttribute("onchange",'onChange(this)');
  107. var adSlc = addSelect(div,'additionalSelect',['',2,3,4,5,6,7]);
  108. adSlc.setAttribute("onchange",'onChange(this)');
  109. adSlc.style.visibility = "hidden";
  110. var slc2 = addSelect(div,'secondSelect',['Mandatory','None','Numeric']);
  111.  
  112. var butn = document.createElement('button');
  113. butn.innerHTML = "Add";
  114. console.log(btnId);
  115. if ( btnId != 0 ){
  116. document.getElementById(btnId).style.visibility = "hidden";
  117. }
  118. btnId = "btnId" + elementNum;
  119. console.log(btnId);
  120. butn.setAttribute("id",btnId);
  121. butn.setAttribute("onclick",'addElement(this.parentNode)');
  122.  
  123. div.appendChild(butn);
  124. elementNum += 1;
  125. }
  126.  
  127. // This function create select element
  128. function addSelect(parent,id,options){
  129. var select = document.createElement('select');
  130. for ( var j = 0; j < options.length; j++){
  131. var opt = document.createElement('option');
  132. opt.value = j + 1;
  133. opt.innerHTML = options[j];
  134. select.appendChild(opt);
  135. }
  136. select.setAttribute("id",id);
  137. parent.appendChild(select);
  138. return select;
  139. }
  140.  
  141. // This function show/hide third select
  142. // and it adds corresponding number of inputs
  143. function onChange(temp){
  144.  
  145. if ( temp.id == "firstSelect" ){
  146. if ( temp.selectedIndex == 2 ){ // show additionalSelect
  147. temp.nextElementSibling.style.visibility = "";
  148. temp.nextElementSibling.selectedIndex = 0;
  149. }
  150. else { // hide additionalSelect and inputs
  151. temp.nextElementSibling.style.visibility = "hidden";
  152. var max = temp.parentNode.getElementsByTagName('p').length;
  153. for (var k = 0; k < max; k++){
  154. var last = temp.parentNode.lastChild;
  155. temp.parentNode.removeChild(last);
  156. }
  157. }
  158. }
  159.  
  160. if ( temp.id == "additionalSelect"){ // add corresponding number of inputs
  161. var num = temp.selectedIndex + 1;
  162. var div = temp.parentNode;
  163. var numbOfInputs = div.getElementsByTagName("p").length;
  164. if ( num != 1 ){
  165. // if current number of inputs is bigger than previous
  166. if ( num > numbOfInputs ){ // add remainder inputs
  167. for (var k = numbOfInputs; k < num; k++){
  168. var p = document.createElement('p');
  169.  
  170. p.setAttribute("value",'');
  171. var input = document.createElement('input');
  172. p.appendChild(input);
  173. div.appendChild(p);
  174. }
  175.  
  176. } // if previous number of inputs is bigger than current
  177. else { // remove remainder inputs
  178. for (var k = num; k < numbOfInputs; k++){
  179. var last = div.lastChild;
  180. div.removeChild(last);
  181. }
  182.  
  183. }
  184. }
  185. else { // if we select one input, than remove all inputs
  186. for (var k = 0; k < numbOfInputs; k++){
  187. var last = div.lastChild;
  188. div.removeChild(last);
  189. }
  190. }
  191. }
  192. }
  193.  
  194.  
  195. // this function create array formular, where we store all inputs of client
  196. function saveFormular(saveBtn){
  197. var newLabel;
  198. var fName = document.getElementById('formularName').value;
  199. var parent = saveBtn.parentNode;
  200. var labels = parent.getElementsByTagName('div');
  201. var formular = [ { formularName: fName} ];
  202. for (var i=0; i < labels.length ; i++){
  203. var name = labels[i].getElementsByTagName('input')[0].value; // name of label
  204. var s = labels[i].getElementsByTagName('select');
  205. var type = s[0].value; // input type of label
  206. if ( type == '3'){
  207. var rBtn = []; // additional inputs for radio buttons
  208. nameRBtn = labels[i].getElementsByTagName('p');
  209. for (var j = 0; j < nameRBtn.length; j++){
  210. rBtn.push(nameRBtn[j].getElementsByTagName('input')[0].value);
  211. }
  212. }
  213. else {
  214. var rBtn = 'null' ;
  215. }
  216. var req = s[s.length-1].value; // required input
  217. newLabel = { labName: name, inputType: type, radBtn: rBtn, requiredInput: req};
  218. formular.push(newLabel);
  219. }
  220. formulars.push(formular); // store formular in one array named formulars
  221.  
  222. //hideFormular(fName);
  223. parent.style.display = "none";
  224. console.log(parent);
  225. //saveBtn.style.display = "none";
  226. alert('Saved');
  227. }
  228.  
  229. function hideFormular(fName){
  230. var formElements = document.getElementById('Administration').getElementsByClassName(fName);
  231. var max = formElements.length;
  232. for (var i = 0; i < max; i++ ){
  233. formElements[i].style.display = "none";
  234. }
  235. formularForHide = "null";
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement