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 # A component that display tabbed data.
42 autoinit
(html_id
, drop_text
, css_classes
)
44 # HTML id of this component.
47 # Text displayed on the tabs dropdown button.
50 # Panels to display in this tab group.
51 var panels
= new Array[DocTabPanel]
53 # Droplist containing links to panels.
55 # Can also be used to add external links.
56 var drop_list
: DocTabsDrop is lazy
do return new DocTabsDrop(html_id
, drop_text
)
58 # Adds a new `panel` to that tab.
60 # You should always use this instead of `panels.add` because it also set the
62 fun add_panel
(panel
: DocTabPanel) do
63 drop_list
.add_li panel
.render_tab
67 redef fun rendering
do
68 if panels
.is_empty
then return
69 panels
.first
.is_active
= true
70 add
"<div role=\"tabpanel\
">"
71 if drop_list
.items
.length
> 1 then add drop_list
72 add
" <div class=\"tab-content\
">"
73 for panel
in panels
do
81 # A list of tab regrouped in a dropdown
84 autoinit
(html_id
, html_title
, items
, css_classes
)
86 # HTML id used by the tabs group.
89 # Title to display in the tab item.
90 var html_title
: String
92 redef fun rendering
do
93 add
"""<ul id="{{{html_id}}}-tabs" class="nav pull-right" role="tablist">"""
94 add
""" <li role="presentation" class="dropdown pull-right">"""
95 add
""" <a href="#" id="{{{html_id}}}-drop" class="dropdown-toggle"
96 data-toggle="dropdown" aria-controls="{{{html_id}}}-contents"
97 aria-expanded="false">"""
99 add
""" <span class="glyphicon glyphicon-menu-hamburger"></span>"""
101 add
""" <ul class="dropdown-menu" role="menu"
102 aria-labelledby="{{{html_id}}}-drop" id="{{{html_id}}}-contents">"""
103 for item
in items
do add item
110 # A panel that goes in a DocTabs.
113 autoinit
(html_id
, tab_title
, html_content
, is_active
, css_classes
)
115 # HTML id of this panel.
118 # Title of this panel as displayed in the tab label.
119 var tab_title
: String
121 # HTML content of this panel.
122 var html_content
: Writable is writable
124 # Is this panel visible by default?
125 var is_active
= false is optional
127 redef fun rendering
do
129 if is_active
then active
= "active in"
130 add
"<div role=\"tabpanel\
" class=\"tab-pane fade
{active}\
""
131 add
" id=\"{html_id}\
" aria-labelledby=\"{html_id}-tab\
">"
136 private fun render_tab
: DocTabItem do return new DocTabItem(tab_title
, html_id
)
139 # A ListItem that goes in a DocTabsDrop.
140 private class DocTabItem
142 autoinit
(text
, target_id
, css_classes
)
144 # Panel id to trigger when the link is clicked.
145 var target_id
: String
147 redef fun rendering
do
148 add
"<li{render_css_classes}>"
149 add
" <a role=\"tab\
" data-toggle=\"tab\
" aria-expanded=\"false\
" tabindex=\"-1\
""
150 add
" id=\"{target_id}-tab\
" href=\"#{target_id}\" aria-controls=\"{target_id}\">"
157 # A HTML tag attribute
158 # `<tag attr="value">`
161 # var attr: TagAttribute
163 # attr = new TagAttribute("foo", null)
164 # assert attr.write_to_string == " foo=\"\""
166 # attr = new TagAttribute("foo", "bar<>")
167 # assert attr.write_to_string == " foo=\"bar<>\""
173 var value
: nullable String
175 redef fun rendering
do
176 var value
= self.value
177 if value
== null then
178 # SEE: http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes
179 add
" {name.html_escape}=\"\
""
181 add
" {name.html_escape}=\"{value.html_escape}\
""
186 # Javacript template that can be added into a DocPage.
190 # HTML attributes to add in this tag.
191 var attrs
= new Array[TagAttribute]
193 # Text content of this script tag.
194 var content
: nullable Writable = null is writable
197 attrs
.add
(new TagAttribute("type", "text/javascript"))
200 # Render the content of this script.
201 protected fun render_content
do
202 if content
!= null then add content
.as(not null)
205 redef fun rendering
do
207 for attr
in attrs
do add attr
214 # JS script for Piwik Tracker
218 # Piwik URL to use for this tracker.
219 var tracker_url
: String
221 # Site ID used on Piwik system.
224 redef fun render_content
do
225 var site_id
= self.site_id
.to_json
226 var tracker_url
= self.tracker_url
.trim
227 if tracker_url
.chars
.last
!= '/' then tracker_url
+= "/"
228 tracker_url
= "://{tracker_url}".to_json
230 addn
"<!-- Piwik -->"
231 addn
"var _paq = _paq || [];"
232 addn
" _paq.push([\"trackPageView\
"]);"
233 addn
" _paq.push([\"enableLinkTracking\
"]);"
234 addn
"(function() \{"
235 addn
" var u=((\"https
:\
" == document.location.protocol) ? \"https\
" : \"http\
") + {tracker_url};"
236 addn
" _paq.push([\"setTrackerUrl\
", u+\"piwik
.php\
"]);"
237 addn
" _paq.push([\"setSiteId\
", {site_id}]);"
238 addn
" var d=document, g=d.createElement(\"script\
"), s=d.getElementsByTagName(\"script\
")[0]; g.type=\"text
/javascript\
";"
239 addn
" g.defer=true; g.async=true; g.src=u+\"piwik
.js\
"; s.parentNode.insertBefore(g,s);"