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