summaryrefslogtreecommitdiff
path: root/UI/elements.html
blob: c789383161f090159b96f509309233a636555a91 (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 IF element_data.defined('default_values') ?>
  95. <?lsmb # Undef items must be removed, or they choke in the options defaults check later.
  96. i = 0;
  97. FOREACH select_default IN element_data.default_values;
  98. UNLESS select_default.defined;
  99. element_data.default_values = element_data.default_values.splice(1, i);
  100. END;
  101. i = i + 1;
  102. END;
  103. ?>
  104. <?lsmb END ?>
  105. <?lsmb
  106. element_type = 'select';
  107. PROCESS auto_id;
  108. ?>
  109. <?lsmb PROCESS attributes # Process regular attributes.
  110. attribute_data=element_data
  111. attribute_defaults = {} # Make sure old defaults are cleared out.
  112. element_keys=['name', 'size', 'multiple', 'disabled', 'accesskey', 'tabindex'] # Attributes that apply to selects.
  113. ?>
  114. <?lsmb PROCESS custom_attributes # Process custom attributes.
  115. custom_attribute_data=element_data.attributes
  116. ?>
  117. <?lsmb PROCESS auto_label # Process element label. ?>
  118. <select<?lsmb all_attributes ?><?lsmb all_custom_attributes ?>>
  119. <?lsmb # Build options.
  120. FOREACH option_data IN element_data.options;
  121. PROCESS option;
  122. END;
  123. ?>
  124. </select>
  125. <?lsmb END ?>
  126. <?lsmb END ?>
  127. <?lsmb # OPTION ELEMENT ?>
  128. <?lsmb BLOCK option ?>
  129. <?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.
  130. IF element_data.defined('default_values') AND element_data.default_values.grep("^${option_data.value}$").size;
  131. option_data.selected = ' selected="selected"';
  132. ELSE;
  133. option_data.selected = "";
  134. END;
  135. ?>
  136. <?lsmb PROCESS attributes # Process regular attributes.
  137. attribute_data=option_data
  138. element_keys=['tabindex', 'disabled', 'value'] # Attributes that apply to options.
  139. ?>
  140. <?lsmb PROCESS custom_attributes # Process custom attributes.
  141. custom_attribute_data=option_data.attributes
  142. ?>
  143. <option<?lsmb all_attributes ?><?lsmb all_custom_attributes ?><?lsmb option_data.selected ?>><?lsmb option_data.text ?></option>
  144. <?lsmb END ?>
  145. <?lsmb # BUTTON ELEMENT ?>
  146. <?lsmb BLOCK button ?>
  147. <?lsmb IF element_data # Only process element if one exists. ?>
  148. <?lsmb
  149. element_type = 'button';
  150. PROCESS auto_id;
  151. ?>
  152. <?lsmb PROCESS attributes # Process regular attributes.
  153. attribute_data=element_data
  154. attribute_defaults = button_defaults
  155. element_keys=['name', 'value', 'accesskey', 'type', 'disabled', 'tabindex'] # Attributes that apply to buttons.
  156. ?>
  157. <?lsmb PROCESS custom_attributes # Process custom attributes.
  158. custom_attribute_data=element_data.attributes
  159. ?>
  160. <?lsmb PROCESS auto_label # Process element label. ?>
  161. <button<?lsmb all_attributes ?><?lsmb all_custom_attributes ?>><?lsmb element_data.text ?></button>
  162. <?lsmb END ?>
  163. <?lsmb END ?>
  164. <?lsmb # LABEL ELEMENT ?>
  165. <?lsmb BLOCK label ?>
  166. <?lsmb IF element_data # Only process element if one exists. ?>
  167. <?lsmb
  168. element_type = 'label';
  169. PROCESS auto_id;
  170. ?>
  171. <?lsmb PROCESS attributes
  172. attribute_data=element_data
  173. attribute_defaults = {} # Make sure old defaults are cleared out.
  174. element_keys=['for'] # Attributes that apply to labels.
  175. ?>
  176. <?lsmb PROCESS custom_attributes
  177. custom_attribute_data=element_data.attributes
  178. ?>
  179. <label<?lsmb all_attributes ?><?lsmb all_custom_attributes ?>><?lsmb element_data.text ?></label>
  180. <?lsmb END ?>
  181. <?lsmb END ?>
  182. <?lsmb # REGULAR ATTRIBUTE PROCESSING -- all explicitly allowed attributes are processed here. ?>
  183. <?lsmb BLOCK attributes ?>
  184. <?lsmb
  185. all_attributes = ""
  186. all_keys = default_keys.merge(element_keys) # Merge in attributes that apply to this element.
  187. ?>
  188. <?lsmb FOREACH element_attribute IN all_keys # Loop through each allowed attribute. ?>
  189. <?lsmb
  190. IF attribute_data.defined(element_attribute); # Add the attribute to the element if it's been set.
  191. all_attributes = all_attributes _ " " _ element_attribute _ '="' _ attribute_data.${element_attribute} _ '"';
  192. ELSIF attribute_defaults.defined(element_attribute); # Fall back to default value if one is supplied.
  193. all_attributes = all_attributes _ " " _ element_attribute _ '="' _ attribute_defaults.${element_attribute} _ '"';
  194. END;
  195. ?>
  196. <?lsmb END ?>
  197. <?lsmb END ?>
  198. <?lsmb # CUSTOM ATTRIBUTE PROCESSING -- any other attributes passed in the 'attributes' key are processed here. ?>
  199. <?lsmb BLOCK custom_attributes ?>
  200. <?lsmb all_custom_attributes = "" ?>
  201. <?lsmb # Loop through each attribute and add it to the custom attribute string.
  202. FOREACH element_attribute IN custom_attribute_data;
  203. all_custom_attributes = all_custom_attributes _ " " _ element_attribute.key _ '="' _ element_attribute.value _ '"';
  204. END;
  205. ?>
  206. <?lsmb END ?>
  207. <?lsmb BLOCK auto_id # Automatically builds the id tag for the element if possible. ?>
  208. <?lsmb UNLESS element_data.defined('id') # id attribute should always be set, so auto-set it if it's not defined. ?>
  209. <?lsmb element_id = "" ?>
  210. <?lsmb # Labal id's default to [for]-label.
  211. IF element_type == 'label' AND element_data.defined('for');
  212. element_id = element_data.for _ "-label";
  213. ELSIF ((element_type == 'input' AND element_data.type == 'radio') OR element_type == 'button') AND element_data.defined('name') AND element_data.defined('value');
  214. element_id = element_data.name _ "-" _ element_data.value; # radios and buttons get name-value for uniqueness.
  215. ELSIF (element_type == 'input' OR element_type == 'textarea' OR element_type == 'select') AND element_data.defined('name');
  216. element_id = element_data.name;
  217. END;
  218. ?>
  219. <?lsmb # Add the id if it's been generated. Replace all non alphanumeric characters with dashes -- nicer CSS.
  220. IF element_id;
  221. element_data.id = element_id.replace('[^\p{IsAlnum}]', '-');
  222. END;
  223. ?>
  224. <?lsmb END ?>
  225. <?lsmb END ?>
  226. <?lsmb BLOCK auto_label # Sets a label for a form element if the special 'label' key is passed. ?>
  227. <?lsmb IF element_data.defined('label') # Check for label. ?>
  228. <?lsmb # Add a for attribute for the label if possible.
  229. IF element_data.defined('id');
  230. label_id = ' id="' _ element_data.id _ '-label"';
  231. label_for = ' for="' _ element_data.id _ '"';
  232. ELSE;
  233. label_id = "";
  234. label_for = "";
  235. END;
  236. ?>
  237. <?lsmb # Label inherits class of the related element if possible.
  238. IF element_data.defined('class');
  239. label_class = ' class="' _ element_data.class _ '"';
  240. ELSE;
  241. label_class = "";
  242. END;
  243. ?>
  244. <label<?lsmb label_id ?><?lsmb label_for ?><?lsmb label_class ?>><?lsmb text(element_data.label) ?></label>
  245. <?lsmb END ?>
  246. <?lsmb END ?>