summaryrefslogtreecommitdiff
path: root/UI/elements.html
blob: e27c3ec0321f59d463c20c6b50f8913307b8886c (plain)
  1. <?lsmb 
  2. default_keys = ['id', 'class', 'title'] # Defaults for all attributes
  3. input_keys = ['type', 'name', 'disabled', 'size', 'value'] # Defaults for input attributes
  4. # ELEMENT DEFAULTS
  5. #checkbox
  6. checkbox_defaults = {
  7. value = '1'
  8. }
  9. #file
  10. file_defaults = {
  11. size => '60'
  12. }
  13. #password
  14. password_defaults = {
  15. size = '60'
  16. }
  17. # text
  18. text_defaults = {
  19. size = '60',
  20. maxlength = '255'
  21. }
  22. # textarea
  23. textarea_defaults = {
  24. rows = '5',
  25. cols = '60'
  26. }
  27. #button
  28. button_defaults = {
  29. type = 'submit'
  30. }
  31. ?>
  32. <?lsmb # INPUT ELEMENT ?>
  33. <?lsmb BLOCK input ?>
  34. <?lsmb IF element_data # Only process element if one exists. ?>
  35. <?lsmb
  36. input_defaults = {} # Some inputs have no defaults, so make sure everything is empty to start with.
  37. element_type = 'input';
  38. PROCESS auto_id;
  39. ?>
  40. <?lsmb SWITCH element_data.type; # Merge in type-specific attributes.
  41. CASE 'file';
  42. input_type_keys = input_keys.merge(['accept']);
  43. input_defaults = file_defaults;
  44. CASE 'image';
  45. input_type_keys = input_keys.merge(['alt', 'src']);
  46. CASE ['checkbox'];
  47. input_type_keys = input_keys.merge(['checked']);
  48. input_defaults = checkbox_defaults;
  49. CASE ['radio'];
  50. input_type_keys = input_keys.merge(['checked']);
  51. CASE ['password'];
  52. input_defaults = password_defaults;
  53. CASE 'text';
  54. input_type_keys = input_keys.merge(['maxlength', 'readonly']);
  55. input_defaults = text_defaults;
  56. CASE;
  57. input_type_keys = input_keys;
  58. END;
  59. ?>
  60. <?lsmb PROCESS attributes # Process regular attributes.
  61. attribute_data = element_data
  62. attribute_defaults = input_defaults
  63. element_keys = input_type_keys
  64. ?>
  65. <?lsmb PROCESS custom_attributes # Process custom attributes.
  66. custom_attribute_data=element_data.attributes
  67. ?>
  68. <?lsmb PROCESS auto_label # Process element label. ?>
  69. <input<?lsmb all_attributes ?><?lsmb all_custom_attributes ?> />
  70. <?lsmb END ?>
  71. <?lsmb END ?>
  72. <?lsmb # TEXTAREA ELEMENT ?>
  73. <?lsmb BLOCK textarea ?>
  74. <?lsmb IF element_data # Only process element if one exists. ?>
  75. <?lsmb
  76. element_type = 'textarea';
  77. PROCESS auto_id;
  78. ?>
  79. <?lsmb PROCESS attributes # Process regular attributes.
  80. attribute_data=element_data
  81. attribute_defaults = textarea_defaults
  82. element_keys = ['name', 'cols', 'rows', 'disabled', 'readonly', 'tabindex', 'accesskey', 'value'] # Attributes that apply to textareas.
  83. ?>
  84. <?lsmb PROCESS custom_attributes # Process custom attributes.
  85. custom_attribute_data=element_data.attributes
  86. ?>
  87. <?lsmb PROCESS auto_label # Process element label. ?>
  88. <textarea<?lsmb all_attributes ?><?lsmb all_custom_attributes ?>><?lsmb element_data.text ?></textarea>
  89. <?lsmb END ?>
  90. <?lsmb END ?>
  91. <?lsmb # SELECT ELEMENT ?>
  92. <?lsmb BLOCK select ?>
  93. <?lsmb IF element_data # Only process element if one exists. ?>
  94. <?lsmb
  95. element_type = 'select';
  96. PROCESS auto_id;
  97. ?>
  98. <?lsmb PROCESS attributes # Process regular attributes.
  99. attribute_data=element_data
  100. attribute_defaults = {} # Make sure old defaults are cleared out.
  101. element_keys=['name', 'size', 'multiple', 'disabled', 'accesskey', 'tabindex'] # Attributes that apply to selects.
  102. ?>
  103. <?lsmb PROCESS custom_attributes # Process custom attributes.
  104. custom_attribute_data=element_data.attributes
  105. ?>
  106. <?lsmb PROCESS auto_label # Process element label. ?>
  107. <select<?lsmb all_attributes ?><?lsmb all_custom_attributes ?>>
  108. <?lsmb # Build options.
  109. FOREACH option_data IN element_data.options;
  110. PROCESS option;
  111. END;
  112. ?>
  113. </select>
  114. <?lsmb END ?>
  115. <?lsmb END ?>
  116. <?lsmb # OPTION ELEMENT ?>
  117. <?lsmb BLOCK option ?>
  118. <?lsmb # Selected is a special case -- no attribute key, so it's handled here by looking for the option value in the default_values key.
  119. IF element_data.defined('default_values') AND element_data.default_values.grep('^' _ option_data.value _ '$').size;
  120. option_data.selected = " selected";
  121. ELSE;
  122. option_data.selected = "";
  123. END;
  124. ?>
  125. <?lsmb PROCESS attributes # Process regular attributes.
  126. attribute_data=option_data
  127. element_keys=['tabindex', 'disabled', 'value'] # Attributes that apply to options.
  128. ?>
  129. <?lsmb PROCESS custom_attributes # Process custom attributes.
  130. custom_attribute_data=option_data.attributes
  131. ?>
  132. <option<?lsmb all_attributes ?><?lsmb all_custom_attributes ?><?lsmb option_data.selected ?>><?lsmb option_data.text ?></option>
  133. <?lsmb END ?>
  134. <?lsmb # BUTTON ELEMENT ?>
  135. <?lsmb BLOCK button ?>
  136. <?lsmb IF element_data # Only process element if one exists. ?>
  137. <?lsmb
  138. element_type = 'button';
  139. PROCESS auto_id;
  140. ?>
  141. <?lsmb PROCESS attributes # Process regular attributes.
  142. attribute_data=element_data
  143. attribute_defaults = button_defaults
  144. element_keys=['name', 'value', 'accesskey', 'type', 'disabled', 'tabindex'] # Attributes that apply to buttons.
  145. ?>
  146. <?lsmb PROCESS custom_attributes # Process custom attributes.
  147. custom_attribute_data=element_data.attributes
  148. ?>
  149. <?lsmb PROCESS auto_label # Process element label. ?>
  150. <button<?lsmb all_attributes ?><?lsmb all_custom_attributes ?>><?lsmb element_data.text ?></button>
  151. <?lsmb END ?>
  152. <?lsmb END ?>
  153. <?lsmb # LABEL ELEMENT ?>
  154. <?lsmb BLOCK label ?>
  155. <?lsmb IF element_data # Only process element if one exists. ?>
  156. <?lsmb
  157. element_type = 'label';
  158. PROCESS auto_id;
  159. ?>
  160. <?lsmb PROCESS attributes
  161. attribute_data=element_data
  162. attribute_defaults = {} # Make sure old defaults are cleared out.
  163. element_keys=['for'] # Attributes that apply to labels.
  164. ?>
  165. <?lsmb PROCESS custom_attributes
  166. custom_attribute_data=element_data.attributes
  167. ?>
  168. <label<?lsmb all_attributes ?><?lsmb all_custom_attributes ?>><?lsmb element_data.text ?></label>
  169. <?lsmb END ?>
  170. <?lsmb END ?>
  171. <?lsmb # REGULAR ATTRIBUTE PROCESSING -- all explicitly allowed attributes are processed here. ?>
  172. <?lsmb BLOCK attributes ?>
  173. <?lsmb
  174. all_attributes = ""
  175. all_keys = default_keys.merge(element_keys) # Merge in attributes that apply to this element.
  176. ?>
  177. <?lsmb FOREACH element_attribute IN all_keys # Loop through each allowed attribute. ?>
  178. <?lsmb
  179. IF attribute_data.defined(element_attribute); # Add the attribute to the element if it's been set.
  180. all_attributes = all_attributes _ " " _ element_attribute _ '="' _ attribute_data.${element_attribute} _ '"';
  181. ELSIF attribute_defaults.defined(element_attribute); # Fall back to default value if one is supplied.
  182. all_attributes = all_attributes _ " " _ element_attribute _ '="' _ attribute_defaults.${element_attribute} _ '"';
  183. END;
  184. ?>
  185. <?lsmb END ?>
  186. <?lsmb END ?>
  187. <?lsmb # CUSTOM ATTRIBUTE PROCESSING -- any other attributes passed in the 'attributes' key are processed here. ?>
  188. <?lsmb BLOCK custom_attributes ?>
  189. <?lsmb all_custom_attributes = "" ?>
  190. <?lsmb # Loop through each attribute and add it to the custom attribute string.
  191. FOREACH element_attribute IN custom_attribute_data;
  192. all_custom_attributes = all_custom_attributes _ " " _ element_attribute.key _ '="' _ element_attribute.value _ '"';
  193. END;
  194. ?>
  195. <?lsmb END ?>
  196. <?lsmb BLOCK auto_id # Automatically builds the id tag for the element if possible. ?>
  197. <?lsmb UNLESS element_data.defined('id') # id attribute should always be set, so auto-set it if it's not defined. ?>
  198. <?lsmb element_id = "" ?>
  199. <?lsmb # Labal id's default to [for]-label.
  200. IF element_type == 'label' AND element_data.defined('for');
  201. element_id = element_data.for _ "-label";
  202. ELSIF ((element_type == 'input' AND element_data.type == 'radio') OR element_type == 'button') AND element_data.defined('name') AND element_data.defined('value');
  203. element_id = element_data.name _ "-" _ element_data.value; # radios and buttons get name-value for uniqueness.
  204. ELSIF (element_type == 'input' OR element_type == 'textarea' OR element_type == 'select') AND element_data.defined('name');
  205. element_id = element_data.name;
  206. END;
  207. ?>
  208. <?lsmb # Add the id if it's been generated. Replace all non alphanumeric characters with dashes -- nicer CSS.
  209. IF element_id;
  210. element_data.id = element_id.replace('[^\p{IsAlnum}]', '-');
  211. END;
  212. ?>
  213. <?lsmb END ?>
  214. <?lsmb END ?>
  215. <?lsmb BLOCK auto_label # Sets a label for a form element if the special 'label' key is passed. ?>
  216. <?lsmb IF element_data.defined('label') # Check for label. ?>
  217. <?lsmb # Add a for attribute for the label if possible.
  218. IF element_data.defined('id');
  219. label_id = ' id="' _ element_data.id _ '-label"';
  220. label_for = ' for="' _ element_data.id _ '"';
  221. ELSE;
  222. label_id = "";
  223. label_for = "";
  224. END;
  225. ?>
  226. <?lsmb # Label inherits class of the related element if possible.
  227. IF element_data.defined('class');
  228. label_class = ' class="' _ element_data.class _ '"';
  229. ELSE;
  230. label_class = "";
  231. END;
  232. ?>
  233. <label<?lsmb label_id ?><?lsmb label_for ?><?lsmb label_class ?>><?lsmb text(element_data.label) ?></label>
  234. <?lsmb END ?>
  235. <?lsmb END ?>