Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. $(document).ready(function() {
  2. var formString="Client:text:required;Date of Order:text:date,required;Campaign Name:text:required;Start Date:text:date,required;End Date:text:date,required;Campaign Budget:text:;Campaign Type:select:required:values[Choose One|,Display,Video,Both];Geographic Targeting:select:required:values[Choose One|,Pre-defined Client Territory,Zip Codes,Cities, Counties, States];Demographic Targeting:select:required:values[18-24,25-34,35-44,45-64,65+,unknown];Gender:select:required:values[Male,Female,Unknown];Parental Status:select:required:values[Parent,Non-Parent,Unknown];";
  3.  
  4. formBuilder(formString,$("#result"));
  5. });
  6.  
  7.  
  8.  
  9. formBuilder = function(str,putIn) {
  10. var fields = str.split(";");
  11. var htmlString = "";
  12. var templates={
  13. input:'<div class="block fm-line"> \
  14. <label for="{safename}">{name}</label> \
  15. <input name="{safename}" type="{safename}" id="{safename}" class="{classes}" value="{value}" /> \
  16. <div class="fm-help"></div> \
  17. </div>',
  18. select: '<div class="block fm-line"> \
  19. <label for="{safename}">{name}</label> \
  20. <select name="{safename}" id="{safename}" class="{classes}">{value}</select> \
  21. <div class="fm-help"></div> \
  22. </div>',
  23. textarea: '<div class="block fm-line"> \
  24. <label for="{safename}">{name}</label> \
  25. <textarea name="{safename}" id="{safename}" class="{classes}">{value}</textarea> \
  26. <div class="fm-help"></div> \
  27. </div>',
  28. option: '<option value="{value}" {selected}>{name}</option>'
  29. };
  30.  
  31.  
  32. var allFields = storeFields(str);
  33. var html = buildHTML(allFields);
  34.  
  35.  
  36. //console.log("all the fields:",allFields)
  37. console.log("html:",html);
  38.  
  39. putIn.html(html);
  40.  
  41.  
  42. function storeFields() {
  43. allFields = [];
  44. for (i in fields) {
  45. if (fields[i] == "") {console.warn("Empty input. Exiting!"); continue;};
  46. var field = {
  47. name: "",
  48. safename: "",
  49. type: "text",
  50. classes: "",
  51. options: [],
  52. value: ""
  53. };
  54. var attributes = fields[i].split(":");
  55. //console.log("attributes:",attributes);
  56. for (attr in attributes) {//console.log("item:",item==0,"attriutes:",attributes)
  57. switch(attr) {
  58. case "0"://field name always first!
  59. field.name = attributes[attr];
  60. field.safename = attributes[attr].trim().replace(/[^a-z0-9]/gi,"-");
  61. break;
  62. case "1": //field type goes next!
  63. field.type = attributes[attr];
  64. break;
  65. case "2": //required/validation classes next!
  66. //classes should be space-denoted in HTML, but we like the formbuilder input string to be comma-seperated.
  67. field.classes= attributes[attr].replace(","," ").replace("required","req");//also note we use the .req class for validation, not .required
  68.  
  69. if (field.classes !== "") {
  70. field.classes += " validate";//add the validation class so our JS will validate it. The other classes denote the TYPE of validation to do, but this class actually denotes to DO IT.
  71. }
  72. break;
  73. case "3"://input value, if not left blank.
  74. if (attributes[attr].indexOf("values[") !== "") {
  75. var optionList = attributes[attr].replace("values[",'').replace(']','').split(",");;//clean up and split into multiple options
  76. for (n in optionList) {//loop through each option
  77. var option = optionList[n].split("|");//split into name/value pairs.
  78. var thisOpt = {
  79. name: option[0],
  80. value: ''
  81. };
  82. if (typeof option[1] !== "undefined") {
  83. //then use specified value
  84. thisOpt.value = option[1].trim().replace(/[^a-z0-9]/gi,"-")
  85. } else {
  86. //else use option name.
  87. thisOpt.value = option[0].trim().replace(/[^a-z0-9]/gi,"-")
  88. }
  89. //This can be a list of values for a select box.
  90. field.options.push(thisOpt)
  91. }
  92. } else {
  93. //else is probably standard input or textarea value.
  94. field.value = attributes[attr];
  95. }
  96. break;
  97. }//switch
  98. }//end inner loop
  99. allFields.push(field);
  100. }//end outer loop
  101.  
  102. return allFields;
  103. }//storeFields()
  104.  
  105. function buildHTML(fieldList) {
  106. /**
  107. * this function uses the input template and select/option template to generate the HTML for each form element.
  108. */
  109. for (i in fieldList) {
  110. var curField = fieldList[i];
  111. console.log(curField,curField.type)
  112. switch(curField.type) {
  113. case "select"://then use <select> tag HTML.
  114. var curFieldHTML = templates.select;
  115. //generate options list.
  116. for (n in curField.options) {
  117. //generate HTML, put in the {value} field.
  118. var optionHTML = templates.option;
  119. optionHTML = optionHTML.replace(/\{value\}/gi,curField.options[n].value);
  120. optionHTML = optionHTML.replace(/\{name\}/gi,curField.options[n].name);
  121. optionHTML = optionHTML.replace(/\{selected\}/gi,'');//unused for now
  122. curField.value += optionHTML;//add it to the object so that it'll be grabbed by our generic {value} line down below.
  123. }
  124. break;
  125. case "textarea"://then use textarea tag HTML
  126. var curFieldHTML = templates.textarea;
  127. default: //default is <input> tag
  128. var curFieldHTML = templates.input;
  129. break;
  130. }
  131.  
  132. //generic - classes, field name, label, value, etc.
  133. curFieldHTML = curFieldHTML.replace(/\{safename\}/gi,curField.safename);//escaped input name
  134. curFieldHTML = curFieldHTML.replace(/\{classes\}/gi,curField.classes);//text in label
  135. curFieldHTML = curFieldHTML.replace(/\{value\}/gi,curField.value);//input value OR list of <option> tags.
  136. curFieldHTML = curFieldHTML.replace(/\{name\}/gi,curField.name);//validation classes
  137. htmlString += curFieldHTML;
  138.  
  139.  
  140. }
  141. return htmlString;
  142. }
  143. };//formBuilder func
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement