View Javadoc

1   /**
2    * This file Copyright (c) 2011 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.templating.editor.client.widget.controlbar;
35  
36  
37  import static info.magnolia.templating.editor.client.jsni.JavascriptUtils.getI18nMessage;
38  import info.magnolia.channel.ChannelResolver;
39  import info.magnolia.templating.editor.client.PageEditor;
40  import info.magnolia.templating.editor.client.dom.CMSComment;
41  import info.magnolia.templating.editor.client.jsni.JavascriptUtils;
42  import info.magnolia.templating.editor.client.model.ModelStorage;
43  import info.magnolia.templating.editor.client.widget.PreviewChannel.Orientation;
44  import info.magnolia.templating.editor.client.widget.button.LocaleSelector;
45  import info.magnolia.templating.editor.client.widget.button.PreviewButton;
46  
47  import java.util.ArrayList;
48  import java.util.HashMap;
49  import java.util.List;
50  import java.util.Map;
51  
52  import com.google.gwt.core.client.GWT;
53  import com.google.gwt.dom.client.Document;
54  import com.google.gwt.dom.client.Style.Float;
55  import com.google.gwt.event.dom.client.ClickEvent;
56  import com.google.gwt.event.dom.client.ClickHandler;
57  import com.google.gwt.event.dom.client.MouseDownEvent;
58  import com.google.gwt.event.dom.client.MouseDownHandler;
59  import com.google.gwt.event.dom.client.MouseUpEvent;
60  import com.google.gwt.event.dom.client.MouseUpHandler;
61  import com.google.gwt.user.client.Command;
62  import com.google.gwt.user.client.ui.Button;
63  import com.google.gwt.user.client.ui.InlineLabel;
64  import com.google.gwt.user.client.ui.MenuItem;
65  
66  /**
67   * Page bar. The HTML output by this widget contains an empty <code>span</code> element with an id called <code>mgnlEditorMainbarPlaceholder</code> as a convenience which can be used by other modules to inject
68   * their own DOM elements into the main bar, <strong>once the page editor is loaded (see {@link PageEditor} and <code>mgnl.PageEditor.onReady(..)</code>)</strong>.
69   * <p>I.e., assuming usage of jQuery, a module's own javascript could do something like this
70   * <p>
71   * {@code
72   *  jQuery('#mgnlEditorMainbarPlaceholder').append('<p>Blah</p>')
73   * }
74   * <p>The placeholder is styled to be automatically centered in the main bar. See this module's editor.css file (id selector #mgnlEditorMainbarPlaceholder).
75   *
76   * TODO: review and clean up, especially the private Command classes.
77   */
78  public class PageBar extends AbstractBar {
79  
80  
81      private String workspace;
82      private String path;
83      private String dialog;
84      private String currentURI;
85      private Map<String,String> availableLocales = new HashMap<String, String>();
86  
87      public PageBar(final CMSComment comment) {
88          super(null);
89  
90          String content = comment.getAttribute("content");
91          int i = content.indexOf(':');
92          workspace = content.substring(0, i);
93          path = content.substring(i + 1);
94          dialog = comment.getAttribute("dialog");
95  
96          currentURI = comment.getAttribute("currentURI");
97  
98          boolean isPreview = Boolean.parseBoolean(comment.getAttribute("preview"));
99          PageEditor.setPreview(isPreview);
100 
101         if(PageEditor.isPreview()){
102             createPreviewModeBar();
103         } else {
104 
105             String availableLocalesAttribute = comment.getAttribute("availableLocales");
106 
107             if(JavascriptUtils.isNotEmpty(availableLocalesAttribute)) {
108                 String[] localeAndUris = availableLocalesAttribute.split(",");
109 
110                 for(String localeAndUri: localeAndUris) {
111                     String[] tmp = localeAndUri.split(":");
112                     if(tmp.length != 2) {
113                         GWT.log("Could not split string [" + tmp + "] while getting locales and uris");
114                         continue;
115                     }
116                     GWT.log("Found available locale [" + tmp[0] + "," + tmp[1] + "]");
117                     availableLocales.put(tmp[0],tmp[1]);
118                 }
119             }
120             createAuthoringModeBar();
121         }
122 
123         addDomHandler(new MouseDownHandler() {
124             @Override
125             public void onMouseDown(MouseDownEvent event) {
126                 ModelStorage.getInstance().getFocusModel().toggleRootAreaBar(true);
127                 event.stopPropagation();
128             }
129         }, MouseDownEvent.getType());
130 
131         addDomHandler(new MouseUpHandler() {
132             @Override
133             public void onMouseUp(MouseUpEvent event) {
134                 event.stopPropagation();
135             }
136         }, MouseUpEvent.getType());
137 
138     }
139 
140     private void createAuthoringModeBar() {
141         InlineLabel mainbarPlaceholder = new InlineLabel();
142         mainbarPlaceholder.getElement().setId("mgnlEditorMainbarPlaceholder");
143         mainbarPlaceholder.setStylePrimaryName("mgnlMainbarPlaceholder");
144         //the placeholder must be added as the first child of the bar element (before the buttons wrapper) so that the style applied to it centers it correctly.
145         getElement().insertFirst(mainbarPlaceholder.getElement());
146 
147 
148         Button properties = new Button(getI18nMessage("buttons.properties.js"));
149         properties.addClickHandler(new ClickHandler() {
150             @Override
151             public void onClick(ClickEvent event) {
152                 PageEditor.openDialog(dialog, workspace, path, null, null);
153             }
154         });
155         addButton(properties, Float.RIGHT);
156 
157         if(!availableLocales.isEmpty()) {
158             LocaleSelector localeSelector = new LocaleSelector(availableLocales, currentURI);
159             addButton(localeSelector, Float.RIGHT, "mgnlEditorLocaleSelector");
160         }
161 
162         MenuItem desktop = new MenuItem(getI18nMessage("buttons.preview.desktop.js"), true, new DesktopPreviewCommand());
163         MenuItem smartphone = new MenuItem(getI18nMessage("buttons.preview.smartphone.js"), true, new MobilePreviewCommand("smartphone", Orientation.PORTRAIT));
164         MenuItem tablet = new MenuItem(getI18nMessage("buttons.preview.tablet.js"), true, new TabletPreviewCommand("tablet", Orientation.LANDSCAPE));
165 
166         List<MenuItem> options = new ArrayList<MenuItem>();
167         options.add(desktop);
168         options.add(smartphone);
169         options.add(tablet);
170 
171         PreviewButton preview = new PreviewButton(getI18nMessage("buttons.preview.js"), new DesktopPreviewCommand(), options);
172         addButton(preview, Float.LEFT, "mgnlEditorPreviewButton");
173 
174         Button adminCentral = new Button(getI18nMessage("buttons.admincentral.js"));
175         adminCentral.addClickHandler(new ClickHandler() {
176             @Override
177             public void onClick(ClickEvent event) {
178                 PageEditor.showTree(workspace, path);
179             }
180         });
181         addButton(adminCentral, Float.LEFT);
182 
183         setClassName("mgnlEditorMainbar mgnlEditorBar");
184 
185     }
186 
187     private void createPreviewModeBar() {
188         Button preview = new Button(getI18nMessage("buttons.preview.hidden.js"));
189         preview.addClickHandler(new ClickHandler() {
190             @Override
191             public void onClick(ClickEvent event) {
192                 PageEditor.enablePreview(false);
193             }
194         });
195         addButton(preview, Float.LEFT);
196         setClassName("mgnlEditorMainbarPreview");
197     }
198 
199     private class MobilePreviewCommand implements Command {
200 
201         private String deviceType;
202         private Orientation orientation;
203 
204         public MobilePreviewCommand(final String deviceType, final Orientation orientation) {
205            this.deviceType = deviceType;
206            this.orientation = orientation;
207         }
208 
209         @Override
210         public void execute() {
211             PageEditor.createChannelPreview("smartphone", deviceType, orientation);
212         }
213     }
214 
215     private class DesktopPreviewCommand implements Command {
216 
217         @Override
218         public void execute() {
219             PageEditor.enablePreview(true);
220         }
221     }
222 
223     @Override
224     public void attach() {
225         Document.get().getBody().insertFirst(getElement());
226         onAttach();
227     }
228 
229     private class TabletPreviewCommand implements Command {
230 
231         private String deviceType;
232         private Orientation orientation;
233 
234         public TabletPreviewCommand(final String deviceType, final Orientation orientation) {
235            this.deviceType = deviceType;
236            this.orientation = orientation;
237         }
238 
239         @Override
240         public void execute() {
241             PageEditor.createChannelPreview(ChannelResolver.ALL, deviceType, orientation);
242         }
243     }
244 }