tabpane.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. {{/* Check parameter types */ -}}
  2. {{ $tpPersistAttrName := "data-td-tp-persist" -}}
  3. {{ with .Get "langEqualsHeader" -}}
  4. {{ if ne ( printf "%T" . ) "bool" -}}
  5. {{ errorf "Shortcode %q: boolean value expected for parameter %q, but got %s. Error position: %s" $.Name "langEqualsHeader" (printf "%T" .) $.Position -}}
  6. {{ end -}}
  7. {{ end -}}
  8. {{ with .Get "text" -}}
  9. {{ if ne ( printf "%T" . ) "bool" -}}
  10. {{ errorf "Shortcode %q: boolean value expected for parameter %q, but got %s. Error position: %s" $.Name "text" (printf "%T" .) $.Position -}}
  11. {{ end -}}
  12. {{ end -}}
  13. {{ $_persistLang := .Get "persistLang" -}}
  14. {{ if and (ne $_persistLang nil) (ne $_persistLang "") -}}
  15. {{ if ne ( printf "%T" $_persistLang ) "bool" -}}
  16. {{ errorf "Shortcode %q: boolean value expected for parameter %q, but got %s. Error position: %s" $.Name "persistLang" (printf "%T" $_persistLang) $.Position -}}
  17. {{ else -}}
  18. {{ warnf "Shortcode %q parameter `persistLang` is deprecated, use `persist` instead: %s" $.Name $.Position -}}
  19. {{ end -}}
  20. {{ end -}}
  21. {{ $_persist := .Get "persist" -}}
  22. {{ with $_persist -}}
  23. {{ $matched := findRE "^(header|lang|disabled)$" . -}}
  24. {{ if not $matched -}}
  25. {{ errorf "Shortcode %q: parameter %q should be one of 'header', 'lang', or 'disabled'; but got %s. Error position: %s" $.Name "persist" $_persist $.Position -}}
  26. {{ end -}}
  27. {{ end -}}
  28. {{ with .Get "right" -}}
  29. {{ if ne ( printf "%T" . ) "bool" -}}
  30. {{ errorf "Shortcode %q: boolean value expected for parameter %q, but got %s. Error position: %s" $.Name "right" (printf "%T" .) $.Position -}}
  31. {{ end -}}
  32. {{ end -}}
  33. {{/* Set values given defined within tabpane */ -}}
  34. {{ $langPane := default "" ($.Get "lang") -}}
  35. {{ $hloptionsPane := default "" ($.Get "highlight") -}}
  36. {{ $textPane := default false ($.Get "text") -}}
  37. {{ $langEqualsHeader := default false ($.Get "langEqualsHeader") -}}
  38. {{ $deprecatedPersistLang := $_persistLang | default true -}}
  39. {{ $persistKeyKind := $_persist
  40. | default (cond (or $textPane (ne "" $langPane)) "header" "lang") -}}
  41. {{ $persistTabpane := and $deprecatedPersistLang (ne $persistKeyKind "disabled") -}}
  42. {{ $rightPane := default false ($.Get "right") -}}
  43. {{ $activeSet := false -}}
  44. {{/* Scratchpad gets populated through call to .Inner */ -}}
  45. {{ .Inner -}}
  46. {{ $duplicate := false -}}
  47. {{ $duplicateKey := "" -}}
  48. {{ $persistKeyList := slice -}}
  49. {{ $tabPaneOrdinal := .Ordinal -}}
  50. {{/* Nav tabs */ -}}
  51. <ul class="nav nav-tabs{{ if $rightPane }} justify-content-end{{ end }}" id="tabs-{{- $tabPaneOrdinal -}}" role="tablist">
  52. {{ range $index, $element := $.Scratch.Get "tabs" -}}
  53. {{ $enabled := not $element.disabled -}}
  54. {{ $lang := $langPane -}}
  55. {{ if $langEqualsHeader -}}
  56. {{ $lang = $element.header -}}
  57. {{ end -}}
  58. {{ with $element.language -}}
  59. {{ $lang = . -}}
  60. {{ end -}}
  61. {{ $persistKey := "" -}}
  62. {{ if eq $persistKeyKind "lang" -}}
  63. {{ with $lang -}}
  64. {{ $persistKey = . -}}
  65. {{ else -}}
  66. {{ with findRE "```(\\S+)" $element.content 1 -}}
  67. {{ $persistKey = index . 1 -}}
  68. {{ end -}}
  69. {{ end -}}
  70. {{ else if eq $persistKeyKind "header" -}}
  71. {{ $persistKey = $element.header -}}
  72. {{ end -}}
  73. {{ $persistKey = $persistKey | lower -}}
  74. {{ $persistTab := and $persistTabpane $persistKey $enabled -}}
  75. {{/* Check for duplicate tab-persistence keys */ -}}
  76. {{ if $persistTab -}}
  77. {{ if in $persistKeyList $persistKey -}}
  78. {{ $duplicate = true -}}
  79. {{ $duplicateKey = $persistKey -}}
  80. {{ $persistTab = false -}}
  81. {{ else -}}
  82. {{ $persistKeyList = $persistKeyList | append $persistKey -}}
  83. {{ end -}}
  84. {{ end -}}
  85. {{ $rightpush := false -}}
  86. {{ with $element.rightpush -}}
  87. {{ $rightpush = . -}}
  88. {{ end -}}
  89. <li class="nav-item{{ if $rightpush }} ms-auto{{ end -}}">
  90. {{/* Generate the IDs for the <a> and the <div> elements */ -}}
  91. {{ $tabid := printf "tabs-%02v-%v-tab" $tabPaneOrdinal $index | anchorize -}}
  92. {{ $entryid := printf "tabs-%02v-%v" $tabPaneOrdinal $index | anchorize -}}
  93. {{ $isActive := and $enabled (not $activeSet) -}}
  94. <button class="nav-link
  95. {{- if $isActive -}}
  96. {{ $activeSet = true }} active
  97. {{- end -}}
  98. {{ if not $enabled }} disabled{{ end -}}"
  99. id="{{ $tabid }}" data-bs-toggle="tab" data-bs-target="#{{ $entryid }}" role="tab"
  100. {{ if $persistTab -}}
  101. {{ printf "%s=%q " $tpPersistAttrName $persistKey | safeHTMLAttr -}}
  102. {{ end -}}
  103. aria-controls="{{- $entryid -}}" aria-selected="{{ $isActive }}">
  104. {{ index . "header" | markdownify }}
  105. </button>
  106. </li>
  107. {{- end }}
  108. </ul>
  109. {{ if $duplicate -}}
  110. {{ warnf "Shortcode %q: duplicate tab-persistence key %q detected, disabling persistence to avoid multiple tab display. Position: %s" $.Name $duplicateKey $.Position -}}
  111. {{ end -}}
  112. {{ $activeSet = false -}}
  113. {{/* Tab panes */ -}}
  114. <div class="tab-content" id="tabs-{{ $tabPaneOrdinal }}-content">
  115. {{- range $index, $element := $.Scratch.Get "tabs" -}}
  116. {{ $lang := $langPane -}}
  117. {{ if $langEqualsHeader -}}
  118. {{ $lang = $element.header -}}
  119. {{ end -}}
  120. {{ with $element.language -}}
  121. {{ $lang = . -}}
  122. {{ end -}}
  123. {{ $enabled := not $element.disabled -}}
  124. {{ $hloptions := $hloptionsPane -}}
  125. {{ with $element.highlight -}}
  126. {{ $hloptions = . -}}
  127. {{ end -}}
  128. {{ $text := $textPane -}}
  129. {{ with $element.text -}}
  130. {{ $text = . }}
  131. {{ end -}}
  132. {{ $tabid := printf "tabs-%02v-%v-tab" $tabPaneOrdinal $index | anchorize -}}
  133. {{ $entryid := printf "tabs-%02v-%v" $tabPaneOrdinal $index | anchorize }}
  134. {{ $isActive := and $enabled (not $activeSet) -}}
  135. <div class="{{ if $text }}tab-body {{end}}tab-pane fade
  136. {{- if $isActive -}}
  137. {{ $activeSet = true }} show active
  138. {{- end }}"
  139. id="{{ $entryid }}" role="tabpanel" aria-labelled-by="{{ $tabid }}" tabindex="{{ $tabPaneOrdinal }}">
  140. {{ if $text -}}
  141. {{ index . "content" -}}
  142. {{ else -}}
  143. {{ highlight (trim (index . "content") "\r\n") $lang $hloptions -}}
  144. {{ end }}
  145. </div>
  146. {{- end }}
  147. </div>