View Javadoc
1   /*
2   * Copyright 2012-2025 Christophe Friederich
3   *
4   * Licensed under the Apache License, Version 2.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7   *
8   * http://www.apache.org/licenses/LICENSE-2.0
9   *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16  package org.devacfr.maven.skins.reflow.model;
17  
18  import javax.annotation.Nonnull;
19  import org.devacfr.maven.skins.reflow.ISkinConfig;
20  
21  /**
22   * @author devacfr
23   * @since 2.0
24   */
25  public class TocSidebar extends Toc<TocSidebar> {
26  
27    /** fixed position by default */
28    private boolean fixed = true;
29  
30    /** expanded by default. */
31    private boolean expanded = true;
32  
33    /** auto expandable by default. */
34    private boolean autoExpandable = true;
35  
36    /** heading level limit to display. all by default */
37    private int level = 0;
38  
39    /**
40     * Default constructor.
41     *
42     * @param config
43     *          a config (can <b>not</b> be {@code null}).
44     */
45    public TocSidebar(final @Nonnull ISkinConfig config) {
46      super(config, "sidebar", "sidebar");
47      final String position = config.getAttributeValue("toc", "position", String.class, "fixed").toLowerCase();
48      this.withEnabled(true)
49          .withExpanded(config.getAttributeValue("toc", "expanded", Boolean.class, true))
50          .withAutoExpandable(config.getAttributeValue("toc", "autoExpandable", Boolean.class, true))
51          .withFixed("fixed".equals(position))
52          .withLevel(config.getAttributeValue("toc", "level", Integer.class, 0));
53      if (this.isEnabled()) {
54        this.addCssOptions("toc-sidebar-enabled");
55      }
56      if (isExpanded()) {
57        this.addCssOptions("toc-sidebar-expanded");
58      }
59      if (isAutoExpandable()) {
60        this.addCssOptions("toc-sidebar-autoexpandable");
61      }
62      if (isFixed()) {
63        this.addCssOptions("toc-sidebar-fixed");
64      } else {
65        this.addCssOptions("toc-sidebar-relative");
66      }
67    }
68  
69    /**
70     * Gets the indicating whether is fixed.
71     *
72     * @return Returns {@code true} if is fixed, otherwise {@code false}.
73     */
74    public boolean isFixed() {
75      return fixed;
76    }
77  
78    /**
79     * Sets the indicating whether is fixed.
80     *
81     * @param fixed
82     *          {@code true} is fixed.
83     * @return Returns the fluent instance.
84     */
85    protected TocSidebar withFixed(final boolean fixed) {
86      this.fixed = fixed;
87      return self();
88    }
89  
90    /**
91     * Gets the indicating whether is expanded.
92     *
93     * @return Returns {@code true} if is expanded, otherwise {@code false}.
94     */
95    public boolean isExpanded() {
96      return expanded;
97    }
98  
99    /**
100    * Sets the indicating whether is expanded.
101    *
102    * @param expanded
103    *          {@code true} is expanded.
104    * @return Returns the fluent instance.
105    */
106   protected TocSidebar withExpanded(final boolean expanded) {
107     this.expanded = expanded;
108     return self();
109   }
110 
111   /**
112    * Gets the indicating whether is auto-expanded.
113    *
114    * @return Returns {@code true} if is expanded, otherwise {@code false}.
115    */
116   public boolean isAutoExpandable() {
117     return autoExpandable;
118   }
119 
120   /**
121    * Sets the indicating whether is auto-expanded.
122    *
123    * @param autoExpandable
124    *          {@code true} is auto-expanded.
125    * @return Returns the fluent instance.
126    */
127   protected TocSidebar withAutoExpandable(final boolean autoExpandable) {
128     this.autoExpandable = autoExpandable;
129     return self();
130   }
131 
132   /**
133    * @return Returns the level limit to display.
134    */
135   public int getLevel() {
136     return level;
137   }
138 
139   /**
140    * Sets the level limit to display
141    *
142    * @param level
143    *          the level to use.
144    * @return Returns the fluent instance.
145    */
146   protected TocSidebar withLevel(final int level) {
147     if (level < 1) {
148       this.level = Integer.MAX_VALUE;
149     } else {
150       this.level = level;
151     }
152     return self();
153   }
154 }