View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.rice.krad.uif.layout;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
20  import org.kuali.rice.krad.uif.CssConstants;
21  import org.kuali.rice.krad.uif.component.Component;
22  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleRestriction;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  /**
28   * Css Grid Layout managers are a layout managers which creates div "rows" and "cells" to replicate a
29   * table look by using div elements for its items.
30   *
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  @BeanTag(name = "cssGridLayoutBase", parent = "Uif-CssGridLayoutBase")
34  public abstract class CssGridLayoutManagerBase extends LayoutManagerBase {
35      private static final long serialVersionUID = 1830635073147703757L;
36  
37      protected static final int NUMBER_OF_COLUMNS = 12;
38      protected static final String BOOTSTRAP_SPAN_PREFIX = "col-md-";
39  
40      // Cannot be set by bean
41      protected List<Component> cellItems;
42      protected List<String> cellCssClassAttributes;
43  
44      // Internal local variables
45      protected int xsTotalSize = 0;
46      protected int smTotalSize = 0;
47      protected int mdTotalSize = 0;
48      protected int lgTotalSize = 0;
49  
50      public CssGridLayoutManagerBase() {
51          cellCssClassAttributes = new ArrayList<String>();
52          cellItems = new ArrayList<Component>();
53      }
54  
55      /**
56       * Determines the css class(es) and based on what settings the item, defaultSizes and basicSize have
57       *
58       * <p>
59       * Priority of what sizes to apply are as follows:
60       * 1. cssGridSizes on the item itself
61       * 2. Sizes in the defaultSizes object
62       * 3. basicSize passed in the if the above two contain no settings, defaults to md (medium) col size
63       * </p>
64       *
65       * @param item the item to process classes for
66       * @param cellCssClasses the list of classes to add the new class string to
67       * @param defaultSizes the default fallback sizes to use if items have none
68       * @param basicSize the fallback md size to use if both item and default size have none
69       */
70      protected void calculateCssClassAndSize(Component item, List<String> cellCssClasses, CssGridSizes defaultSizes,
71              int basicSize) {
72  
73          if (StringUtils.isNotBlank(item.getCssGridSizes().getCssClassString())) {
74              cellCssClasses.add(0, item.getCssGridSizes().getCssClassString());
75  
76              xsTotalSize += item.getCssGridSizes().getXsSize();
77              smTotalSize += item.getCssGridSizes().getTotalSmSize();
78              mdTotalSize += item.getCssGridSizes().getTotalMdSize();
79              lgTotalSize += item.getCssGridSizes().getTotalLgSize();
80          } else if (StringUtils.isNotBlank(defaultSizes.getCssClassString())) {
81              cellCssClasses.add(0, defaultSizes.getCssClassString());
82  
83              xsTotalSize += defaultSizes.getXsSize();
84              smTotalSize += defaultSizes.getTotalSmSize();
85              mdTotalSize += defaultSizes.getTotalMdSize();
86              lgTotalSize += defaultSizes.getTotalLgSize();
87          } else {
88              cellCssClasses.add(0, BOOTSTRAP_SPAN_PREFIX + basicSize);
89  
90              mdTotalSize += basicSize;
91          }
92      }
93  
94      /**
95       * Adds a class (or classeees) which will clear the left float for wrapped content at each screen size, which
96       * will prevent natural float from taking available space instead of wrapping to a new "row".
97       *
98       * @param cellCssClasses the set of css classes to add the left clear class to
99       */
100     protected void addLeftClearCssClass(List<String> cellCssClasses) {
101         String classString = getCellStyleClassesAsString(cellCssClasses);
102 
103         // We explicitly check for the col prefix to avoid unnecessary class additions since the clear will be
104         // inherited from a smaller size screen if no size/offset has been specified for this size specifically
105         // see KRAD css grid css
106         if (lgTotalSize > 12) {
107             if (classString.contains(CssConstants.CssGrid.LG_COL_PREFIX)) {
108                 cellCssClasses.add(0, CssConstants.CssGrid.LG_CLEAR_LEFT);
109             }
110             lgTotalSize = lgTotalSize - 12;
111         }
112 
113         if (mdTotalSize > 12) {
114             if (classString.contains(CssConstants.CssGrid.MD_COL_PREFIX)) {
115                 cellCssClasses.add(0, CssConstants.CssGrid.MD_CLEAR_LEFT);
116             }
117             mdTotalSize = mdTotalSize - 12;
118         }
119 
120         if (smTotalSize > 12) {
121             if (classString.contains(CssConstants.CssGrid.SM_COL_PREFIX)) {
122                 cellCssClasses.add(0, CssConstants.CssGrid.SM_CLEAR_LEFT);
123             }
124             smTotalSize = smTotalSize - 12;
125         }
126 
127         if (xsTotalSize > 12) {
128             cellCssClasses.add(0, CssConstants.CssGrid.XS_CLEAR_LEFT);
129             xsTotalSize = xsTotalSize - 12;
130         }
131     }
132 
133     /**
134      * Builds the HTML class attribute string by combining the cellStyleClasses list with a space
135      * delimiter
136      *
137      * @param cellCssClasses list of cell CSS classes
138      * @return class attribute string
139      */
140     protected String getCellStyleClassesAsString(List<String> cellCssClasses) {
141         if (cellCssClasses != null) {
142             return StringUtils.join(cellCssClasses, " ").trim();
143         }
144 
145         return "";
146     }
147 
148     /**
149      * Get the items which will make up each "cell" divs of this css grid layout, these divs will have appropriate
150      * css class applied to them based on the values stored in cellCssClassAttributes
151      *
152      * @return the items of this cssGrid
153      */
154     @ViewLifecycleRestriction
155     public List<Component> getCellItems() {
156         return cellItems;
157     }
158 
159     /**
160      * List of css class HTML attribute values ordered by the order in which the cell appears
161      *
162      * @return the list of css class HTML attributes for cells
163      */
164     public List<String> getCellCssClassAttributes() {
165         return cellCssClassAttributes;
166     }
167 
168 }