View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.internal.renderkit.renderer;
21  
22  import org.apache.myfaces.tobago.context.Markup;
23  import org.apache.myfaces.tobago.internal.component.AbstractUIPopup;
24  import org.apache.myfaces.tobago.internal.util.HtmlRendererUtils;
25  import org.apache.myfaces.tobago.model.CollapseMode;
26  import org.apache.myfaces.tobago.renderkit.css.BootstrapClass;
27  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
28  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
29  import org.apache.myfaces.tobago.renderkit.html.HtmlRoleValues;
30  import org.apache.myfaces.tobago.util.ComponentUtils;
31  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
32  
33  import javax.faces.context.FacesContext;
34  import java.io.IOException;
35  
36  public class PopupRenderer<T extends AbstractUIPopup> extends CollapsiblePanelRendererBase<T> {
37  
38    @Override
39    public void encodeBeginInternal(final FacesContext facesContext, final T component) throws IOException {
40  
41      final TobagoResponseWriter writer = getResponseWriter(facesContext);
42      final String clientId = component.getClientId(facesContext);
43      final boolean collapsed = component.isCollapsed();
44      final Markup markup = component.getMarkup();
45  
46      // this makes the popup NOT closable with a click to the background
47      ComponentUtils.putDataAttribute(component, "backdrop", "static");
48  
49      writer.startElement(HtmlElements.TOBAGO_POPUP);
50      writer.writeIdAttribute(clientId);
51      writer.writeClassAttribute(
52          BootstrapClass.MODAL,
53          BootstrapClass.FADE,
54          component.getCustomClass());
55      writer.writeAttribute(HtmlAttributes.TABINDEX, -1);
56      writer.writeAttribute(HtmlAttributes.ROLE, HtmlRoleValues.DIALOG.toString(), false);
57      HtmlRendererUtils.writeDataAttributes(facesContext, writer, component);
58      // todo: aria-labelledby
59      writer.startElement(HtmlElements.DIV);
60      writer.writeClassAttribute(
61          BootstrapClass.MODAL_DIALOG,
62          markup != null && markup.contains(Markup.LARGE) ? BootstrapClass.MODAL_LG : null,
63          markup != null && markup.contains(Markup.SMALL) ? BootstrapClass.MODAL_SM : null);
64      writer.writeAttribute(HtmlAttributes.ROLE, HtmlRoleValues.DOCUMENT.toString(), false);
65      writer.startElement(HtmlElements.DIV);
66      writer.writeClassAttribute(BootstrapClass.MODAL_CONTENT);
67  
68      if (component.getCollapsedMode() != CollapseMode.none) {
69        encodeHidden(writer, clientId, collapsed);
70      }
71    }
72  
73    @Override
74    public void encodeEndInternal(final FacesContext facesContext, final T component) throws IOException {
75  
76      final TobagoResponseWriter writer = getResponseWriter(facesContext);
77      writer.endElement(HtmlElements.DIV);
78      writer.endElement(HtmlElements.DIV);
79      writer.endElement(HtmlElements.TOBAGO_POPUP);
80  
81    }
82  }