1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # HTML templates used by Nitdoc to generate API documentation
16 # Pages are assembled using `Template`
17 module html_components
20 import html
::bootstrap
23 # A label with a text content.
29 css_classes
.add
"label"
32 # Init this label from css classes.
33 init with_classes
(classes
: Array[String]) do
35 css_classes
.add_all classes
39 #########################
40 # general layout elements
41 #########################
43 # Something that can go in a section
44 # Sections are automatically collected to populate the menu
51 # Title to display if any
52 # if both `title` and `summary_title` are null then
53 # the section will not appear in the summary
54 var title
: nullable Writable = null is writable
56 # Subtitle to display if any
57 var subtitle
: nullable Writable = null is writable
59 # Title that appear in the summary
60 # if null use `title` instead
61 var summary_title
: nullable String = null is writable
63 # CSS classes to apply on the section element
64 var css_classes
= new Array[String]
66 # CSS classes to apply on the title heading element
67 var title_classes
= new Array[String]
69 # Parent article/section if any
70 var parent
: nullable TplSectionElt = null
72 init with_title
(id
: String, title
: Writable) do
77 # Level <hX> for HTML heading
78 protected fun hlvl
: Int do
79 if parent
== null then return 1
80 return parent
.hlvl
+ 1
83 # Elements contained by this section
84 var children
= new Array[TplSectionElt]
86 # Add an element in this section
87 fun add_child
(child
: TplSectionElt) do
92 # Is the section empty (no content at all)
93 fun is_empty
: Bool do return children
.is_empty
96 # A HTML <section> element
100 redef fun rendering
do
101 addn
"<section id='{id}' class='{css_classes.join(" ")}'>"
102 if title
!= null then
104 if lvl
== 2 then title_classes
.add
"well well-sm"
105 addn
"<h{lvl} class='{title_classes.join(" ")}'>"
106 addn title
.as(not null)
109 if subtitle
!= null then
110 addn
"<div class='info subtitle'>"
111 addn subtitle
.as(not null)
114 for child
in children
do
121 # A page article that can go in a section
125 # Content for this article
126 var content
: nullable Writable = null is writable
127 var source_link
: nullable Writable = null is writable
129 init with_content
(id
: String, title
: Writable, content
: Writable) do
130 with_title
(id
, title
)
131 self.content
= content
134 redef fun rendering
do
135 if is_empty
then return
136 addn
"<article id='{id}' class='{css_classes.join(" ")}'>"
137 if source_link
!= null then
138 add
"<div class='source-link'>"
139 add source_link
.as(not null)
142 if title
!= null then
144 if lvl
== 2 then title_classes
.add
"well well-sm"
145 add
"<h{lvl} class='{title_classes.join(" ")}'>"
146 add title
.as(not null)
149 if subtitle
!= null then
150 add
"<div class='info subtitle'>"
151 add subtitle
.as(not null)
154 if content
!= null then
155 add content
.as(not null)
157 for child
in children
do
160 addn
"""</article>"""
163 redef fun is_empty
: Bool do
164 return title
== null and subtitle
== null and content
== null and children
.is_empty
168 # A module / class / prop definition
173 var comment
: nullable Writable = null is writable
175 # Namespace for this definition
176 var namespace
: nullable Writable = null is writable
178 # Location link to display
179 var location
: nullable Writable = null is writable
181 private fun render_info
do
182 addn
"<div class='info text-right'>"
183 if namespace
!= null then
184 if comment
== null then
185 add
"<span class=\"noComment\
">no comment for </span>"
187 add namespace
.as(not null)
189 if location
!= null then
191 add location
.as(not null)
196 private fun render_comment
do
197 if comment
!= null then add comment
.as(not null)
200 redef fun rendering
do
201 addn
"<div class='definition'>"
209 class TplClassDefinition
212 var intros
= new Array[TplListElt]
213 var redefs
= new Array[TplListElt]
215 redef fun rendering
do
216 addn
"<div class='definition'>"
219 render_list
("Introduces", intros
)
220 render_list
("Redefines", redefs
)
224 private fun render_list
(name
: String, elts
: Array[TplListElt]) do
225 if elts
.is_empty
then return
226 addn
"<h5>{name.html_escape}</h5>"
227 addn
"<ul class='list-unstyled list-definition'>"
228 for elt
in elts
do add elt
233 # Layout for Search page
237 var modules
= new Array[Writable]
238 var classes
= new Array[Writable]
239 var props
= new Array[Writable]
241 redef fun rendering
do
242 var title
= self.title
243 if title
!= null then addn
"<h1>{title.to_s.html_escape}</h1>"
244 addn
"<div class='container-fluid'>"
245 addn
" <div class='row'>"
246 if not modules
.is_empty
then
247 addn
"<div class='col-xs-4'>"
248 addn
"<h3>Modules</h3>"
258 if not classes
.is_empty
then
259 addn
"<div class='col-xs-4'>"
260 addn
"<h3>Classes</h3>"
270 if not props
.is_empty
then
271 addn
"<div class='col-xs-4'>"
272 addn
"<h3>Properties</h3>"
287 #####################
288 # Basiv HTML elements
289 #####################
296 var href
: String is writable
298 # The raw HTML content to display in the link
299 var text
: Writable is writable
301 # The unescaped optional title.
302 var title
: nullable String = null is writable
304 init with_title
(href
, text
, title
: String) do
309 redef fun rendering
do
313 if title
!= null then
315 add title.as(not null).html_escape
328 # Elements contained in this list
329 # can be <li> or <ul> elements
330 var elts
= new Array[TplListElt]
332 # CSS classes of the <ul> element
333 var css_classes
= new Array[String]
335 # Add content wrapped in a <li> element
336 fun add_li
(item
: TplListItem) do elts
.add item
338 init with_classes
(classes
: Array[String]) do self.css_classes
= classes
340 fun is_empty
: Bool do return elts
.is_empty
342 redef fun rendering
do
343 if elts
.is_empty
then return
344 addn
"<ul class='{css_classes.join(" ")}'>"
345 for elt
in elts
do add elt
350 # Something that can be added to a TplList
359 # Content of the list item
360 var content
= new Template
362 # CSS classes of the <li> element
363 var css_classes
= new Array[String]
365 init with_content
(content
: Writable) do append
(content
)
367 init with_classes
(content
: Writable, classes
: Array[String]) do
368 with_content
(content
)
369 css_classes
= classes
372 # Append `content` to the item
373 # similar to `self.content.add`
374 fun append
(content
: Writable) do self.content
.add content
376 redef fun rendering
do
377 add
"<li class='{css_classes.join(" ")}'>"
383 # A Bootstrap tab component that contains `TplTabPanel`.
387 # Panels contained in the tab.
388 var panels
= new Array[TplTabPanel]
391 fun add_panel
(panel
: TplTabPanel) do panels
.add panel
393 # CSS classes of the tab component.
394 var css_classes
= new Array[String]
396 redef fun rendering
do
397 addn
"<div class='tab-content'>"
398 for panel
in panels
do add panel
403 # A panel that goes in a `TplTab`.
407 # CSS classes of the pane element.
408 var css_classes
= new Array[String]
412 # Used to show/hide panel.
413 var id
: String is noinit
417 # Displayed in the tab header or in the pointing link.
420 # Is the panel visible by default?
421 var is_active
= false is writable
424 var content
: nullable Writable = null is writable
426 # Get a link pointing to this panel.
427 fun tpl_link_to
: Writable do
428 var lnk
= new Template
429 lnk
.add
"<a data-target='#{id}' data-toggle='pill'>"
435 redef fun rendering
do
436 add
"<div class='tab-pane {css_classes.join(" ")}"
437 if is_active
then add
"active"
439 if content
!= null then add content
.as(not null)
444 # A label with a text content
448 # Content of the label if any
449 var content
: nullable Writable = null is writable
451 # CSS classes of the <span> element
452 var css_classes
= new Array[String]
454 init with_content
(content
: Writable) do self.content
= content
455 init with_classes
(classes
: Array[String]) do self.css_classes
= classes
457 redef fun rendering
do
458 add
"<span class='label {css_classes.join(" ")}'>"
459 if content
!= null then add content
.as(not null)
464 # A label with an icon
469 # see: http://getbootstrap.com/components/#glyphicons
472 init with_icon
(icon
: String) do self.icon
= icon
474 redef fun rendering
do
475 add
"<span class='glyphicon glyphicon-{icon} {css_classes.join(" ")}'>"
476 if content
!= null then add content
.as(not null)
481 # A HTML tag attribute
482 # `<tag attr="value">`
485 # var attr: TagAttribute
487 # attr = new TagAttribute("foo", null)
488 # assert attr.write_to_string == " foo=\"\""
490 # attr = new TagAttribute("foo", "bar<>")
491 # assert attr.write_to_string == " foo=\"bar<>\""
497 var value
: nullable String
499 redef fun rendering
do
500 var value
= self.value
501 if value
== null then
502 # SEE: http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes
503 add
" {name.html_escape}=\"\
""
505 add
" {name.html_escape}=\"{value.html_escape}\
""
514 var attrs
= new Array[TagAttribute]
515 var content
: nullable Writable = null is writable
518 attrs
.add
(new TagAttribute("type", "text/javascript"))
521 protected fun render_content
do
522 if content
!= null then add content
.as(not null)
525 redef fun rendering
do
527 for attr
in attrs
do add attr
534 # JS script for Piwik Tracker
538 var tracker_url
: String
541 redef fun render_content
do
542 var site_id
= self.site_id
.to_json
543 var tracker_url
= self.tracker_url
.trim
544 if tracker_url
.chars
.last
!= '/' then tracker_url
+= "/"
545 tracker_url
= "://{tracker_url}".to_json
547 addn
"<!-- Piwik -->"
548 addn
"var _paq = _paq || [];"
549 addn
" _paq.push([\"trackPageView\
"]);"
550 addn
" _paq.push([\"enableLinkTracking\
"]);"
551 addn
"(function() \{"
552 addn
" var u=((\"https
:\
" == document.location.protocol) ? \"https\
" : \"http\
") + {tracker_url};"
553 addn
" _paq.push([\"setTrackerUrl\
", u+\"piwik
.php\
"]);"
554 addn
" _paq.push([\"setSiteId\
", {site_id}]);"
555 addn
" var d=document, g=d.createElement(\"script\
"), s=d.getElementsByTagName(\"script\
")[0]; g.type=\"text
/javascript\
";"
556 addn
" g.defer=true; g.async=true; g.src=u+\"piwik
.js\
"; s.parentNode.insertBefore(g,s);"