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;
35  
36  
37  import static info.magnolia.templating.editor.client.jsni.LegacyJavascript.getI18nMessage;
38  import static info.magnolia.templating.editor.client.jsni.LegacyJavascript.isPreviewMode;
39  import info.magnolia.templating.editor.client.PreviewChannelWidget.Orientation;
40  import info.magnolia.templating.editor.client.button.PreviewButtonWidget;
41  import info.magnolia.templating.editor.client.dom.CMSComment;
42  import info.magnolia.templating.editor.client.model.ModelStorage;
43  
44  import java.util.ArrayList;
45  import java.util.List;
46  
47  import com.google.gwt.dom.client.Document;
48  import com.google.gwt.dom.client.Style.Float;
49  import com.google.gwt.event.dom.client.ClickEvent;
50  import com.google.gwt.event.dom.client.ClickHandler;
51  import com.google.gwt.event.dom.client.MouseDownEvent;
52  import com.google.gwt.event.dom.client.MouseDownHandler;
53  import com.google.gwt.user.client.Command;
54  import com.google.gwt.user.client.ui.Button;
55  import com.google.gwt.user.client.ui.InlineLabel;
56  import com.google.gwt.user.client.ui.MenuItem;
57  
58  /**
59   * 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
60   * 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>.
61   * <p>I.e., assuming usage of jQuery, a module's own javascript could do something like this
62   * <p>
63   * {@code
64   *  jQuery('#mgnlEditorMainbarPlaceholder').append('<p>Blah</p>')
65   * }
66   * <p>The placeholder is styled to be automatically centered in the main bar. See this module's editor.css file (id selector #mgnlEditorMainbarPlaceholder).
67   *
68   * TODO: review and clean up, especially the private Command classes.
69   */
70  public class PageBarWidget extends AbstractBarWidget {
71  
72      private PageEditor pageEditor;
73  
74      private String workspace;
75      private String path;
76      private String dialog;
77      private boolean previewState = false;
78  
79      public PageBarWidget(final PageEditor pageEditor, final CMSComment comment) {
80          super(null);
81          this.pageEditor = pageEditor;
82  
83          String content = comment.getAttribute("content");
84          int i = content.indexOf(':');
85          this.workspace = content.substring(0, i);
86          this.path = content.substring(i + 1);
87          this.dialog = comment.getAttribute("dialog");
88  
89          if(isPreviewMode()){
90              createPreviewModeBar();
91          } else {
92              createAuthoringModeBar();
93          }
94  
95          addDomHandler(new MouseDownHandler() {
96              @Override
97              public void onMouseDown(MouseDownEvent event) {
98                  ModelStorage.getInstance().getFocusModel().reset();
99              }
100         }, MouseDownEvent.getType());
101 
102     }
103 
104     private void createAuthoringModeBar() {
105         InlineLabel mainbarPlaceholder = new InlineLabel();
106         mainbarPlaceholder.getElement().setId("mgnlEditorMainbarPlaceholder");
107         mainbarPlaceholder.setStylePrimaryName("mgnlMainbarPlaceholder");
108         //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.
109         getElement().insertFirst(mainbarPlaceholder.getElement());
110 
111         Button properties = new Button(getI18nMessage("buttons.properties.js"));
112         properties.addClickHandler(new ClickHandler() {
113             @Override
114             public void onClick(ClickEvent event) {
115                 pageEditor.openDialog(dialog, workspace, path, null, null);
116             }
117         });
118         addButton(properties, Float.RIGHT);
119 
120         MenuItem desktop = new MenuItem(getI18nMessage("buttons.preview.desktop.js"), true, new DesktopPreviewCommand());
121         MenuItem smartphone = new MenuItem(getI18nMessage("buttons.preview.smartphone.js"), true, new MobilePreviewCommand("smartphone", Orientation.PORTRAIT));
122         MenuItem tablet = new MenuItem(getI18nMessage("buttons.preview.tablet.js"), true, new MobilePreviewCommand("tablet", Orientation.LANDSCAPE));
123 
124         List<MenuItem> options = new ArrayList<MenuItem>();
125         options.add(desktop);
126         options.add(smartphone);
127         options.add(tablet);
128 
129         PreviewButtonWidget preview = new PreviewButtonWidget(getI18nMessage("buttons.preview.js"), new DesktopPreviewCommand(), options);
130         addButton(preview, Float.LEFT, "mgnlEditorPreviewButton");
131 
132         Button adminCentral = new Button(getI18nMessage("buttons.admincentral.js"));
133         adminCentral.addClickHandler(new ClickHandler() {
134             @Override
135             public void onClick(ClickEvent event) {
136                 pageEditor.showTree(workspace, path);
137             }
138         });
139         addButton(adminCentral, Float.LEFT);
140 
141         setClassName("mgnlEditorMainbar mgnlEditorBar");
142 
143         previewState = false;
144     }
145 
146     private void createPreviewModeBar() {
147         Button preview = new Button(getI18nMessage("buttons.preview.hidden.js"));
148         preview.addClickHandler(new ClickHandler() {
149             @Override
150             public void onClick(ClickEvent event) {
151                 pageEditor.preview(false);
152             }
153         });
154         addButton(preview, Float.LEFT);
155         setClassName("mgnlEditorMainbarPreview");
156 
157         previewState = true;
158     }
159 
160     public final boolean isPreviewState() {
161         return previewState;
162     }
163 
164     private class MobilePreviewCommand implements Command {
165 
166         private String deviceType;
167         private Orientation orientation;
168 
169         public MobilePreviewCommand(final String deviceType, final Orientation orientation) {
170            this.deviceType = deviceType;
171            this.orientation = orientation;
172         }
173 
174         @Override
175         public void execute() {
176             pageEditor.createChannelPreview("mobile", deviceType, orientation);
177         }
178     }
179 
180     private class DesktopPreviewCommand implements Command {
181 
182         @Override
183         public void execute() {
184             pageEditor.preview(true);
185         }
186     }
187 
188     public void attach() {
189         Document.get().getBody().insertFirst(getElement());
190         onAttach();
191     }
192 }