View Javadoc
1   /**
2    * This file Copyright (c) 2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.vaadin.gwt.client.layout.lazylayout.widget;
35  
36  import com.google.gwt.dom.client.DivElement;
37  import com.google.gwt.dom.client.Element;
38  import com.google.gwt.dom.client.ImageElement;
39  import com.google.gwt.dom.client.SpanElement;
40  import com.google.gwt.dom.client.Style;
41  import com.google.gwt.user.client.DOM;
42  
43  /**
44   * Sub implementaton of {@link EscalatorPanel} for cards, aka the 6.0 UI.
45   *
46   * TODO: should be merged back with {@link EscalatorPanel} once 5.x UI is dropped.
47   */
48  public class EscalatorCardsPanel extends EscalatorPanel {
49  
50      private static Flyweight flyweight = new Flyweight() {
51  
52          static final String THUMBNAIL_STYLE_NAME = "thumbnail";
53          static final String THUMBNAIL_IMAGE_STYLE_NAME = "thumbnail-image";
54          public static final String CLEARED_STYLE_NAME = "cleared";
55  
56          @Override
57          Element createThumbnail() {
58              final DivElement thumbnail = DivElement.as(DOM.createDiv());
59              thumbnail.addClassName(THUMBNAIL_STYLE_NAME);
60              thumbnail.getStyle().setDisplay(Style.Display.INLINE_BLOCK);
61  
62              final SpanElement caption = SpanElement.as(DOM.createSpan());
63              caption.getStyle().setDisplay(Style.Display.NONE);
64  
65              final ImageElement image = ImageElement.as(DOM.createImg());
66              image.addClassName(THUMBNAIL_IMAGE_STYLE_NAME);
67              image.getStyle().setDisplay(Style.Display.NONE);
68  
69              thumbnail.appendChild(image);
70              thumbnail.appendChild(caption);
71  
72              return thumbnail;
73          }
74  
75          @Override
76          void setImageSrc(String src, Element thumbnail) {
77              final Element img = getImage(thumbnail);
78              final Element caption = getCaption(thumbnail);
79  
80              img.getStyle().setDisplay(Style.Display.BLOCK);
81              caption.getStyle().setDisplay(Style.Display.BLOCK);
82  
83              img.setAttribute("src", src);
84              thumbnail.removeClassName(CLEARED_STYLE_NAME);
85          }
86  
87          @Override
88          void setIconFontStyle(String style, Element thumbnail) {
89              // no-op
90          }
91  
92          private Element getCaption(Element thumbnail) {
93              return Element.as(thumbnail.getChild(1));
94          }
95  
96          @Override
97          Element getImage(Element thumbnail) {
98              return Element.as(thumbnail.getChild(0));
99          }
100 
101         @Override
102         public void clear(Element thumbnail) {
103             Element caption = getCaption(thumbnail);
104             caption.setInnerText("");
105             caption.getStyle().setDisplay(Style.Display.NONE);
106 
107             Element image = getImage(thumbnail);
108             image.removeAttribute("src");
109             image.getStyle().setDisplay(Style.Display.NONE);
110 
111             thumbnail.addClassName(CLEARED_STYLE_NAME);
112         }
113 
114         @Override
115         void setCaption(String newCaption, Element thumbnail) {
116             final Element caption = getCaption(thumbnail);
117             caption.setInnerHTML(newCaption);
118             caption.getStyle().setDisplay(Style.Display.BLOCK); // ?
119 
120             final Element img = getImage(thumbnail);
121             img.getStyle().setDisplay(Style.Display.BLOCK); // ?
122 
123             thumbnail.removeClassName(CLEARED_STYLE_NAME);
124         }
125     };
126 
127     public EscalatorCardsPanel(Listener listener) {
128         super(listener, flyweight);
129     }
130 
131     public void updateImageCaption(String caption, int index) {
132         flyweight.setCaption(caption, Element.as(super.getImageContainer().getChild(super.relativeIndex(index))));
133     }
134 }