macros.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. {%- macro field_label(field) -%}
  2. <label for="{{ field.id }}">{{ field.label.text }}</label>
  3. {% endmacro %}
  4. {%- macro field_description(field) -%}
  5. {% if field.description %}
  6. <span class="help-block">{{ field.description|safe }}</span>
  7. {% endif %}
  8. {%- endmacro -%}
  9. {%- macro field_errors(field) -%}
  10. {% if field.errors %}
  11. {%- for error in field.errors -%}
  12. <span class="help-block">{{error}}</span>
  13. {%- endfor -%}
  14. {% endif %}
  15. {%- endmacro -%}
  16. {%- macro render_quickreply(field, rows, cols, div_class='') -%}
  17. {%- if kwargs['required'] or field.flags.required -%}
  18. {% if div_class %}
  19. {{ field(class=div_class, required="required", cols=cols, rows=rows, placeholder=field.label.text, **kwargs) }}
  20. {% else %}
  21. {{ field(class="new-message", required="required", cols=cols, rows=row, placeholder=field.label.text, **kwargs) }}
  22. {% endif %}
  23. {%- else -%}
  24. {% if div_class %}
  25. {{ field(class=div_class, cols=cols, rows=row, placeholder=field.label.text, **kwargs) }}
  26. {% else %}
  27. {{ field(class="new-message", cols=cols, rows=row, placeholder=field.label.text, **kwargs) }}
  28. {% endif %}
  29. {%- endif -%}
  30. {{ field_description(field) }}
  31. {{ field_errors(field) }}
  32. {%- endmacro -%}
  33. {# Renders a non bootstrap input field #}
  34. {%- macro render_input_field(field, div_class='', placeholder='') -%}
  35. {%- if div_class -%}
  36. <div class="{{ div_class }}">
  37. {%- endif -%}
  38. {%- if placeholder -%}
  39. {% set field_placeholder = placeholder %}
  40. {%- else -%}
  41. {% set field_placeholder = field.label.text %}
  42. {%- endif -%}
  43. {%- if kwargs['required'] or field.flags.required -%}
  44. {{ field(required="required", placeholder=field_placeholder) }}
  45. {%- else -%}
  46. {{ field(placeholder=field_placeholder) }}
  47. {% endif %}
  48. {%- if div_class -%}
  49. </div>
  50. {%- endif -%}
  51. {{ field_description(field) }}
  52. {{ field_errors(field) }}
  53. {%- endmacro -%}
  54. {%- macro render_boolean_field(field, inline=False) -%}
  55. <div class="checkbox{% if inline %}inline{% endif %}{% if field.errors %}has-error{% endif %}">
  56. <label>
  57. {{ field(**kwargs) }}
  58. {{ field.label.text }}
  59. </label>
  60. {{ field_description(field) }}
  61. {{ field_errors(field) }}
  62. </div>
  63. {%- endmacro -%}
  64. {%- macro render_select_field(field, div_class='', select_class="form-control") -%}
  65. <div class="form-group">
  66. {% if div_class %}
  67. <div class="{{ div_class }}">
  68. {% else %}
  69. <div class="col-sm-5">
  70. {% endif %}
  71. <label>{{ field.label.text }}</label>
  72. {% if field.type == 'QuerySelectMultipleField' or field.type == 'SelectMultipleField' %}
  73. {{ field(multiple=True, class=select_class) }}
  74. {% else %}
  75. {{ field(class=select_class) }}
  76. {%- endif -%}
  77. {{ field_description(field) }}
  78. {{ field_errors(field) }}
  79. </div>
  80. </div>
  81. {%- endmacro -%}
  82. {%- macro render_submit_field(field, div_class='', input_class='') -%}
  83. {% if div_class %}
  84. <div class="{{ div_class }}">
  85. {% endif %}
  86. {{ field(class=input_class or 'btn btn-success') }}
  87. {% if div_class %}
  88. </div>
  89. {% endif %}
  90. {%- endmacro -%}
  91. {%- macro render_field(field, with_label=True, div_class='', rows='') -%}
  92. <div class="form-group{%- if field.errors %} has-error{%- endif %}">
  93. <div class="{%- if div_class -%}{{ div_class }}{%- else -%}col-sm-5{%- endif -%}">
  94. {% if with_label %}
  95. <label>{{ field.label.text }}</label>
  96. {% endif %}
  97. {%- if kwargs['required'] or field.flags.required -%}
  98. {% if rows %}
  99. {{ field(class="form-control", required="required", rows=rows, placeholder=field.label.text, **kwargs) }}
  100. {% else %}
  101. {{ field(class="form-control", required="required", placeholder=field.label.text, **kwargs) }}
  102. {% endif %}
  103. {%- else -%}
  104. {% if rows %}
  105. {{ field(class="form-control", rows=rows, placeholder=field.label.text, **kwargs) }}
  106. {% else %}
  107. {{ field(class="form-control", placeholder=field.label.text, **kwargs) }}
  108. {% endif %}
  109. {%- endif -%}
  110. {{ field_description(field) }}
  111. {{ field_errors(field) }}
  112. </div>
  113. </div>
  114. {%- endmacro -%}
  115. {%- macro inline_field(field, label_text='', label_class='') -%}
  116. <div class="form-group {%- if field.errors %} has-error{%- endif %}">
  117. {{field.label(class="sr-only")}}
  118. <div class="col-sm-4">
  119. {%- if kwargs['required'] or field.flags.required -%}
  120. {% if label_text %}
  121. {{field(class='form-control', placeholder=label_text, required="required", **kwargs)}}
  122. {% else %}
  123. {{field(class='form-control', placeholder=field.label.text, required="required", **kwargs)}}
  124. {% endif %}
  125. {%- else -%}
  126. {% if label_text %}
  127. {{field(class='form-control', placeholder=label_text, **kwargs)}}
  128. {% else %}
  129. {{field(class='form-control', placeholder=field.label.text, **kwargs)}}
  130. {% endif %}
  131. {%- endif -%}
  132. {{ field_description(field) }}
  133. {{ field_errors(field) }}
  134. </div>
  135. </div>
  136. {%- endmacro -%}
  137. {%- macro group_field(field, label_text='', label_class='', css_class='form-control form-grouped') -%}
  138. <div class="form-group {%- if field.errors %} has-error{%- endif %}" style="margin-bottom: 0px;">
  139. {{field.label(class="sr-only")}}
  140. {%- if kwargs['required'] or field.flags.required -%}
  141. {% if label_text %}
  142. {{field(class=css_class, placeholder=label_text, required="required", **kwargs)}}
  143. {% else %}
  144. {{field(class=css_class, placeholder=field.label.text, required="required", **kwargs)}}
  145. {% endif %}
  146. {%- else -%}
  147. {% if label_text %}
  148. {{field(class=css_class, placeholder=label_text, **kwargs)}}
  149. {% else %}
  150. {{field(class=css_class, placeholder=field.label.text, **kwargs)}}
  151. {% endif %}
  152. {%- endif -%}
  153. {{ field_description(field) }}
  154. {{ field_errors(field) }}
  155. </div>
  156. {%- endmacro -%}
  157. {%- macro input_group_field(field, label_text='', css_class='form-control') -%}
  158. {%- if kwargs['required'] or field.flags.required -%}
  159. {% if label_text %}
  160. {{field(class=css_class, placeholder=label_text, required="required", **kwargs)}}
  161. {% else %}
  162. {{field(class=css_class, placeholder=field.label.text, required="required", **kwargs)}}
  163. {% endif %}
  164. {%- else -%}
  165. {% if label_text %}
  166. {{field(class=css_class, placeholder=label_text, **kwargs)}}
  167. {% else %}
  168. {{field(class=css_class, placeholder=field.label.text, **kwargs)}}
  169. {% endif %}
  170. {%- endif -%}
  171. {%- endmacro -%}
  172. {%- macro horizontal_select_field(field, div_class='', label_class='', select_class="form-control", surrounded_div="col-sm-4") -%}
  173. <div class="form-group row {%- if field.errors %} has-error{%- endif %}">
  174. {% if label_class %}
  175. {{ field.label(class=label_class) }}
  176. {% else %}
  177. {{ field.label(class="col-sm-3 control-label") }}
  178. {% endif %}
  179. {% if div_class %}
  180. <div class="{{ div_class }}">
  181. {% else %}
  182. <div class="col-sm-5">
  183. {% endif %}
  184. <div class="row">
  185. {% if field.type == 'QuerySelectMultipleField' or field.type == 'SelectMultipleField' %}
  186. {{ field(multiple=True, class=select_class, surrounded_div=surrounded_div) }}
  187. {% else %}
  188. {{ field(class=select_class, surrounded_div=surrounded_div) }}
  189. {%- endif -%}
  190. </div>
  191. {{ field_description(field) }}
  192. {{ field_errors(field) }}
  193. </div>
  194. </div>
  195. {%- endmacro -%}
  196. {%- macro horizontal_boolean_field(field, div_class='') -%}
  197. <div class="{%- if div_class -%}{{ div_class }}{%- else -%}col-sm-offset-3 col-sm-3{%- endif -%}">
  198. {{ render_boolean_field(field, **kwargs) }}
  199. </div>
  200. {%- endmacro -%}
  201. {%- macro horizontal_submit_field(field, div_class='', input_class='') -%}
  202. <div class="{%- if div_class -%}{{ div_class }}{%- else -%}col-sm-offset-3 col-sm-3{%- endif -%}">
  203. {{ field(class=input_class or 'btn btn-success') }}
  204. </div>
  205. {%- endmacro -%}
  206. {%- macro horizontal_field(field, label_text='', label_class='', div_class='', input_class='') -%}
  207. <div class="form-group row {%- if field.errors %} has-error{%- endif %}">
  208. {% if field.type == "BooleanField" or field.type == "SubmitField" %}
  209. {% if field.type == "BooleanField" %}
  210. {{ horizontal_boolean_field(field, div_class) }}
  211. {% else %}
  212. {{ horizontal_submit_field(field, div_class) }}
  213. {% endif %}
  214. {% else %}
  215. {% if label_class %}
  216. {{ field.label(class=label_class) }}
  217. {% else %}
  218. {{ field.label(class="col-sm-3 control-label") }}
  219. {% endif %}
  220. {% if div_class %}
  221. <div class="{{ div_class }}">
  222. {% else %}
  223. <div class="col-sm-4">
  224. {% endif %}
  225. {%- if kwargs['required'] or field.flags.required -%}
  226. {% if label_text %}
  227. {{ field(class='form-control', placeholder=label_text, required="required", **kwargs) }}
  228. {% else %}
  229. {{ field(class='form-control', placeholder=field.label.text, required="required", **kwargs) }}
  230. {% endif %}
  231. {%- else -%}
  232. {% if label_text %}
  233. {{ field(class='form-control', placeholder=label_text, **kwargs) }}
  234. {% else %}
  235. {{ field(class='form-control', placeholder=field.label.text, **kwargs) }}
  236. {% endif %}
  237. {%- endif -%}
  238. </div> <!-- end div_class -->
  239. {% endif %}
  240. {{ field_description(field) }}
  241. {{ field_errors(field) }}
  242. </div> <!-- end form-group -->
  243. {%- endmacro -%}
  244. {% macro topnav(endpoint, name, icon='', id='', active=False) %}
  245. <li {% if id %}id={{id}}{% endif %} {% if endpoint == request.endpoint or active == True %}class="active"{% endif %}>
  246. <a href={{ url_for(endpoint) }}>
  247. {% if icon %}<i class="{{ icon }}"></i> {% endif %}{{ name }}
  248. </a>
  249. </li>
  250. {% endmacro %}
  251. {% macro is_active(endpoint, active=False) %}
  252. {%- if endpoint == request.endpoint or active == True -%}
  253. active
  254. {%- endif -%}
  255. {% endmacro %}
  256. {% macro navlink(endpoint, name, icon='', active=False) %}
  257. <li {% if endpoint == request.endpoint or active %}class="active"{% endif %}>
  258. <a href="{{ url_for(endpoint) }}">{% if icon %}<i class="{{ icon }}"></i> {% endif %} {{ name }}</a>
  259. </li>
  260. {% endmacro %}
  261. {% macro tablink_href(endpoint, name, active=False) %}
  262. <li {% if endpoint == request.endpoint or active %}class="active"{% endif %} >
  263. <a href={{ endpoint }} role="tab" data-toggle="tab">{{ name }}</a>
  264. </li>
  265. {% endmacro %}
  266. {% macro render_pagination(page_obj, url, ul_class='') %}
  267. <ul class='{%- if ul_class -%}{{ ul_class }}{%- else -%}pagination{%- endif -%}'>
  268. <li class="disabled"><a href="#"><span class="pages-label">{% trans %}Pages{% endtrans %}:</span></a></li>
  269. {%- for page in page_obj.iter_pages() %}
  270. {% if page %}
  271. {% if page != page_obj.page %}
  272. <li><a href="{{ url }}?page={{ page }}">{{ page }}</a></li>
  273. {% else %}
  274. <li class="active"><a href="#">{{ page }}</a></li>
  275. {% endif %}
  276. {% endif %}
  277. {%- else -%}
  278. <li class="active"><a href="#">1</a></li>
  279. {%- endfor %}
  280. {% if page_obj.has_next %}
  281. <li><a href="{{ url }}?page={{ page_obj.next_num }}">&raquo;</a></li>
  282. {% endif %}
  283. </ul>
  284. {% endmacro %}
  285. {% macro render_topic_pagination(page_obj, url) %}
  286. <ul class="pagination pagelink pull-left">
  287. <li class="disabled"><a><span class="pages-label">Pages: </span></a></li>
  288. {%- for page in page_obj.iter_pages() %}
  289. {% if page %}
  290. {% if page != page_obj.page %}
  291. <li><a href="{{ url }}?page={{ page }}">{{ page }}</a></li>
  292. {% else %}
  293. <li class="disabled"><a href="#">{{ page }}</a></li>
  294. {% endif %}
  295. {% endif %}
  296. {%- else -%}
  297. <li class="disabled"><a href="#">1</a></li>
  298. {%- endfor %}
  299. {% if page_obj.has_next %}
  300. <li><a href="{{ url }}?page={{ page_obj.next_num }}">Next</a></li>
  301. {% endif %}
  302. </ul>
  303. {% endmacro %}
  304. {% macro message_pagination(page_obj, url) %}
  305. <ul class='{%- if ul_class -%}{{ ul_class }}{%- else -%}pagination pagination-sm{%- endif -%}'>
  306. {%- for page in page_obj.iter_pages() %}
  307. {% if page %}
  308. {% if page != page_obj.page %}
  309. <li><a href="{{ url }}?page={{ page }}">{{ page }}</a></li>
  310. {% else %}
  311. <li class="active"><a href="#">{{ page }}</a></li>
  312. {% endif %}
  313. {% endif %}
  314. {%- else -%}
  315. <li class="active"><a href="#">1</a></li>
  316. {%- endfor %}
  317. {% if page_obj.has_next %}
  318. <li><a href="{{ url }}?page={{ page_obj.next_num }}">&raquo;</a></li>
  319. {% endif %}
  320. </ul>
  321. {% endmacro %}
  322. {# Generates a some kind of pagination for the posts in topic in the forum view. #}
  323. {%- macro topic_pages(topic_obj, per_page=10) -%}
  324. {% set topic_pages = (topic_obj.post_count / per_page)|round|int %}
  325. {%- if topic_pages > 1 -%}
  326. [
  327. {%- for page in range(0, topic_pages) -%}
  328. <a href="{{ url_for('forum.view_topic', topic_id=topic_obj.id) }}?page={{ page+1 }}">{{ page+1 }}</a>{% if not loop.last %} {% endif %}
  329. {%- endfor -%}
  330. ]
  331. {%- endif -%}
  332. {%- endmacro -%}
  333. {# Generates a topic url with an anchor to the post #}
  334. {%- macro generate_post_url(topic, post, page) -%}
  335. {%- if page > 1 -%}
  336. {{ topic.url }}?page={{ page }}#pid{{ post.id }}
  337. {%- else -%}
  338. {{ topic.url }}#pid{{ post.id }}
  339. {%- endif -%}
  340. {%- endmacro -%}
  341. {# Generates the post id for the topic. Each topic starts with the post id 1 #}
  342. {%- macro generate_post_id(posts, page, per_page) -%}
  343. {%- if posts.page == 1 -%}
  344. {{ page }}
  345. {%- else -%}
  346. {{ page + (posts.page - 1) * per_page }}
  347. {%- endif -%}
  348. {%- endmacro -%}