Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

lib.HTML.asp 12KB

8 meses atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <%
  2. '=======================================================================================================================
  3. ' HTML HELPER
  4. '=======================================================================================================================
  5. Class HTML_Helper_Class
  6. 'Duplicate of Routes.NoCacheToken, copied to avoid extra lookup into the Routes object for such a trivial function.
  7. 'Allows caller to reference HTML.NoCacheToken in cases where it seems to feel right.
  8. Public Property Get NoCacheToken
  9. NoCacheToken = Timer() * 100
  10. End Property
  11. 'Ensures safe output
  12. Public Function Encode(ByVal value)
  13. If Not IsEmpty(value) and Not IsNull(value) then
  14. Encode = Server.HtmlEncode(value)
  15. End If
  16. End Function
  17. '---------------------------------------------------------------------------------------------------------------------
  18. 'LinkTo and its relatives DO NOT HTMLEncode the link_text! This allows use of HTML within the link, especially
  19. 'useful for Bootstrap icons and the like.
  20. '
  21. 'Bottom Line: If you need to HTMLEncode the link text YOU MUST DO IT YOURSELF! The H() method makes this easy!
  22. Public Function LinkTo(link_text, controller_name, action_name)
  23. LinkTo = LinkToExt(link_text, controller_name, action_name, empty, empty)
  24. End Function
  25. Public Function LinkToExt(link_text, controller_name, action_name, params_array, attribs_array)
  26. LinkToExt = "<a href='" & Encode(Routes.UrlTo(controller_name, action_name, params_array)) & "'" &_
  27. HtmlAttribs(attribs_array) & ">" & link_text & "</a>" & vbCR
  28. End Function
  29. Public Function LinkToIf(condition, link_text, controller_name, action_name)
  30. if condition then
  31. LinkToIf = LinkToExt(link_text, controller_name, action_name, empty, empty)
  32. end if
  33. End Function
  34. Public Function LinkToExtIf(condition, link_text, controller_name, action_name, params_array, attribs_array)
  35. if condition then
  36. LinkToExtIf = LinkToExt(link_text, controller_name, action_name, params_array, attribs_array)
  37. end if
  38. End Function
  39. Public Function LinkToUnless(condition, link_text, controller_name, action_name)
  40. if not condition then
  41. LinkToIf = LinkToExt(link_text, controller_name, action_name, empty, empty)
  42. end if
  43. End Function
  44. Public Function LinkToExtUnless(condition, link_text, controller_name, action_name, params_array, attribs_array)
  45. if not condition then
  46. LinkToExtUnless = LinkToExt(link_text, controller_name, action_name, params_array, attribs_array)
  47. end if
  48. End Function
  49. ''
  50. ' Creates a form button and a hidden form to enforce POST submissions. Params are in hidden fields.
  51. ''
  52. 'Public Function PostButtonLinkTo(controller_name, action_name, params)
  53. ' dim id : id = "post_button__" & controller_name & action_name
  54. ' dim s
  55. ' s = "<form id='" & id & "' action='" & Routes.UrlTo(controller_name, action_name, empty) & "' method='POST'>"
  56. ' dim i, key, val
  57. ' for i = 0 to ubound(params) step 2
  58. ' KeyVal params, i, key, val
  59. ' s = s & "<input type='hidden' name='" & key & "' value='" & val & "'>"
  60. ' next
  61. ' s = s & "<input type='submit' value='&gt;&gt;'>"
  62. ' s = s & "</form>"
  63. ' PostButtonLinkTo = s
  64. 'End Function
  65. Public Function PostButtonTo(button_contents, controller_name, action_name, form_fields)
  66. PostButtonTo = PostButtonToExt(button_contents, controller_name, action_name, form_fields, empty)
  67. End Function
  68. Public Function PostButtonToExt(button_contents, controller_name, action_name, form_fields, url_params)
  69. dim s : s = "<form action='" & Routes.UrlTo(controller_name, action_name, url_params) & "' method='POST' style='margin: 0;'>"
  70. dim i, key, val
  71. for i = 0 to ubound(form_fields) step 2
  72. KeyVal form_fields, i, key, val
  73. s = s & HTML.Hidden(key, val)
  74. next
  75. s = s & HTML.SubmitButton(button_contents)
  76. s = s & "</form>" & vbCR
  77. PostButtonToExt = s
  78. End Function
  79. Public Function AppStylesheetTag
  80. AppStylesheetTag = StylesheetTag(Routes.StylesheetsURL & "App.css")
  81. End Function
  82. Public Function ControllerStylesheetTag
  83. ControllerStylesheetTag = StylesheetTag(Routes.StylesheetsUrl & MVC.ControllerName & "Controller.css")
  84. End Function
  85. Public Function StylesheetTag(url)
  86. StylesheetTag = "<link rel='stylesheet' href='" & Encode(url) & "?" & Year(now) & Month(now) & Day(now) & Hour(now) & Minute(now) & Second(now) & "'>" & vbCR
  87. End Function
  88. Public Function JSTag(url)
  89. JSTag = "<script type='text/javascript' src='" & Encode(url) & "'></script>" & vbCR
  90. End Function
  91. '---------------------------------------------------------------------------------------------------------------------
  92. ' Form Helpers
  93. '---------------------------------------------------------------------------------------------------------------------
  94. Public Function FormTag(controller_name, action_name, route_attribs, form_attribs)
  95. FormTag = "<form action='" & Routes.UrlTo(controller_name, action_name, route_attribs) & "' method='POST' " & HtmlAttribs(form_attribs) & ">" & vbCR
  96. End Function
  97. Public Function Label(name, for_name)
  98. Label = LabelExt(name, for_name, empty)
  99. End Function
  100. Public Function LabelExt(name, for_name, attribs)
  101. LabelExt = "<label for='" & Encode(for_name) & "' " & HtmlAttribs(attribs) & ">" & Encode(name) & "</label>" & vbCR
  102. End Function
  103. Public Function Hidden(id, value)
  104. Hidden = HiddenExt(id, value, empty)
  105. End Function
  106. Public Function HiddenExt(id, value, attribs)
  107. HiddenExt = "<input type='hidden' id='" & Encode(id) & "' name='" & Encode(id) & "' value='" & Encode(value) & "' " & HtmlAttribs(attribs) & " >" & vbCR
  108. End Function
  109. Public Function TextBox(id, value)
  110. TextBox = TextBoxExt(id, value, empty)
  111. End Function
  112. Public Function TextBoxExt(id, value, attribs)
  113. TextBoxExt = "<input type='text' id='" & Encode(id) & "' name='" & Encode(id) & "' value='" & Encode(value) & "' " & HtmlAttribs(attribs) & " >" & vbCR
  114. End Function
  115. Public Function TextArea(id, value, rows, cols)
  116. TextArea = TextAreaExt(id, value, rows, cols, empty)
  117. End Function
  118. Public Function TextAreaExt(id, value, rows, cols, attribs)
  119. TextAreaExt = "<textarea id='" & Encode(id) & "' name='" & Encode(id) & "' cols='" & Encode(cols) & "' rows='" & Encode(rows) & "' " & HtmlAttribs(attribs) & " >" &_
  120. Encode(value) & "</textarea>" & vbCR
  121. End Function
  122. '---------------------------------------------------------------------------------------------------------------------
  123. 'If list is a recordset then option_value_field and option_text_field are required.
  124. 'If list is an array the method assumes it is a KVArray and those parameters are ignored.
  125. Public Function DropDownList(id, selected_value, list, option_value_field, option_text_field)
  126. DropDownList = DropDownListExt(id, selected_value, list, option_value_field, option_text_field, empty)
  127. End Function
  128. Public Function DropDownListExt(id, selected_value, list, option_value_field, option_text_field, attribs)
  129. If IsNull(selected_value) then
  130. selected_value = ""
  131. Else
  132. selected_value = CStr(selected_value)
  133. End If
  134. dim item, options, opt_val, opt_txt
  135. options = "<option value=''>" ' first value is "non-selected" blank state
  136. select case typename(list)
  137. case "Recordset"
  138. do until list.EOF
  139. If IsNull(list(option_value_field)) then
  140. opt_val = ""
  141. Else
  142. opt_val = CStr(list(option_value_field))
  143. End If
  144. opt_txt = list(option_text_field)
  145. If Not IsNull(opt_val) And Not IsEmpty(opt_val) then
  146. options = options & "<option value='" & Encode(opt_val) & "' " & Choice((CStr(opt_val) = CStr(selected_value)), "selected='selected'", "") & ">" & Encode(opt_txt) & "</option>" & vbCR
  147. End If
  148. list.MoveNext
  149. loop
  150. case "Variant()" 'assumes KVArray
  151. dim i
  152. for i = 0 to ubound(list) step 2
  153. KeyVal list, i, opt_val, opt_txt
  154. options = options & "<option value='" & Encode(opt_val) & "' " & Choice((CStr(opt_val) = CStr(selected_value)), "selected='selected'", "") & ">" & Encode(opt_txt) & "</option>" & vbCR
  155. next
  156. end select
  157. DropDownListExt = "<select id='" & Encode(id) & "' name='" & Encode(id) & "' " & HtmlAttribs(attribs) & " >" & vbCR & options & "</select>" & vbCR
  158. End Function
  159. Public Function Checkbox(id, value)
  160. Checkbox = CheckboxExt(id, value, empty)
  161. End Function
  162. Public Function CheckboxExt(id, value, attribs)
  163. CheckBoxExt = "<input type='checkbox' id='" & Encode(id) & "' name='" & Encode(id) & "' " & Choice( (value = 1) or (value = true) or (LCase(value) = "true") or (LCase(value) = "on"), "checked='checked'", "") & " " & HtmlAttribs(attribs) & ">" & vbCR
  164. End Function
  165. '---------------------------------------------------------------------------------------------------------------------
  166. 'Button text IS NOT ENCODED! As with LinkTo, this allows use of Bootstrap icons and other arbitrary HTML in the
  167. 'button. If you need to HTMLEncode the text you MUST do it yourself!
  168. Public Function SubmitButton(text)
  169. SubmitButton = "<button type='submit' class='btn'>" & text & "</button>" & vbCR
  170. End Function
  171. Public Function Button(button_type, text, class_name)
  172. Button = "<button type='" & Encode(button_type) & "' class='btn " & Encode(class_name) & "'>" & text & "</button>" & vbCR
  173. End Function
  174. Public Function ButtonExt(button_type, text, attribs_array)
  175. ButtonExt = "<button type='" & Encode(button_type) & "' " & HtmlAttribs(attribs_array) & ">" & text & "</button>" & vbCR
  176. End Function
  177. '---------------------------------------------------------------------------------------------------------------------
  178. Public Function Tag(Tag_name, attribs_array)
  179. Tag = "<" & Encode(tag_name) & " " & HtmlAttribs(attribs_array) & ">"
  180. End Function
  181. Public Function Tag_(Tag_name)
  182. Tag_ = "</" & Encode(tag_name) & ">"
  183. End Function
  184. '---------------------------------------------------------------------------------------------------------------------
  185. Public Function HtmlAttribs(attribs)
  186. dim result : result = ""
  187. if not IsEmpty(attribs) then
  188. if IsArray(attribs) then
  189. dim idx
  190. for idx = lbound(attribs) to ubound(attribs) step 2
  191. result = result & " " & HtmlAttrib(attribs, idx) & " "
  192. next
  193. else ' assume string or string-like default value
  194. result = attribs
  195. end if
  196. end if
  197. HtmlAttribs = result
  198. End Function
  199. Public Function HtmlAttrib(attribs_array, key_idx)
  200. dim key, val
  201. KeyVal attribs_array, key_idx, key, val
  202. HtmlAttrib = Encode(key) & "='" & Encode(val) & "'"
  203. End Function
  204. End Class
  205. dim HTML_Helper__Singleton : set HTML_Helper__Singleton = Nothing
  206. Function HTML()
  207. if HTML_Helper__Singleton Is Nothing then
  208. set HTML_Helper__Singleton = new HTML_Helper_Class
  209. End if
  210. set HTML = HTML_Helper__Singleton
  211. End Function
  212. %>

Powered by TurnKey Linux.