001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ade.containerpage.client;
029
030import org.opencms.ade.containerpage.client.ui.CmsAddToFavoritesButton;
031import org.opencms.ade.containerpage.client.ui.CmsToolbarAllGalleriesMenu;
032import org.opencms.ade.containerpage.client.ui.CmsToolbarClipboardMenu;
033import org.opencms.ade.containerpage.client.ui.CmsToolbarEditButton;
034import org.opencms.ade.containerpage.client.ui.CmsToolbarElementInfoButton;
035import org.opencms.ade.containerpage.client.ui.CmsToolbarFavLocationButton;
036import org.opencms.ade.containerpage.client.ui.CmsToolbarGalleryMenu;
037import org.opencms.ade.containerpage.client.ui.CmsToolbarInfoButton;
038import org.opencms.ade.containerpage.client.ui.CmsToolbarListAddButton;
039import org.opencms.ade.containerpage.client.ui.CmsToolbarListManagerButton;
040import org.opencms.ade.containerpage.client.ui.CmsToolbarMoveButton;
041import org.opencms.ade.containerpage.client.ui.CmsToolbarPublishButton;
042import org.opencms.ade.containerpage.client.ui.CmsToolbarRemoveButton;
043import org.opencms.ade.containerpage.client.ui.CmsToolbarSelectionButton;
044import org.opencms.ade.containerpage.client.ui.CmsToolbarSettingsButton;
045import org.opencms.ade.containerpage.client.ui.css.I_CmsLayoutBundle;
046import org.opencms.gwt.client.A_CmsEntryPoint;
047import org.opencms.gwt.client.CmsBroadcastTimer;
048import org.opencms.gwt.client.CmsCoreProvider;
049import org.opencms.gwt.client.CmsRpcContext;
050import org.opencms.gwt.client.dnd.CmsCompositeDNDController;
051import org.opencms.gwt.client.dnd.CmsDNDHandler;
052import org.opencms.gwt.client.dnd.CmsDNDHandler.AnimationType;
053import org.opencms.gwt.client.ui.CmsErrorDialog;
054import org.opencms.gwt.client.ui.CmsPushButton;
055import org.opencms.gwt.client.ui.CmsQuickLauncher.A_QuickLaunchHandler;
056import org.opencms.gwt.client.ui.CmsToolbar;
057import org.opencms.gwt.client.ui.CmsToolbarContextButton;
058import org.opencms.gwt.client.ui.I_CmsToolbarButton;
059import org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuCommand;
060import org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuCommandInitializer;
061import org.opencms.gwt.client.util.CmsDomUtil;
062import org.opencms.gwt.client.util.CmsEmbeddedDialogFrame;
063import org.opencms.gwt.client.util.CmsStyleVariable;
064import org.opencms.gwt.shared.CmsGwtConstants;
065import org.opencms.gwt.shared.CmsGwtConstants.QuickLaunch;
066import org.opencms.gwt.shared.CmsQuickLaunchParams;
067import org.opencms.util.CmsStringUtil;
068
069import java.util.Map;
070
071import com.google.gwt.core.client.GWT;
072import com.google.gwt.core.client.JavaScriptObject;
073import com.google.gwt.event.dom.client.ClickEvent;
074import com.google.gwt.event.dom.client.ClickHandler;
075import com.google.gwt.user.client.ui.RootPanel;
076import com.google.gwt.user.client.ui.Widget;
077
078import elemental2.dom.DomGlobal;
079
080/**
081 * The container page editor.<p>
082 *
083 * @since 8.0.0
084 */
085public class CmsContainerpageEditor extends A_CmsEntryPoint {
086
087    /** Margin-top added to the document body element when the tool-bar is shown. */
088    //    private int m_bodyMarginTop;
089
090    /**
091     * Quick launch handler for the page editor.
092     */
093    public static class PageEditorQuickLaunchHandler extends A_QuickLaunchHandler {
094
095        /**
096         * @see org.opencms.gwt.client.ui.CmsQuickLauncher.I_QuickLaunchHandler#getParameters()
097         */
098        public CmsQuickLaunchParams getParameters() {
099
100            return new CmsQuickLaunchParams(
101                QuickLaunch.CONTEXT_PAGE,
102                CmsCoreProvider.get().getStructureId(),
103                CmsContainerpageController.get().getData().getDetailId(),
104                null,
105                CmsCoreProvider.get().getUri(),
106                CmsCoreProvider.get().getLastPageId());
107        }
108
109    }
110
111    /** Add menu. */
112    private CmsToolbarGalleryMenu m_add;
113
114    /** Add to favorites button. */
115    private CmsAddToFavoritesButton m_addToFavorites;
116
117    /** The button for the 'complete galleries' dialog. */
118    private CmsToolbarAllGalleriesMenu m_allGalleries;
119
120    /** Clip-board menu. */
121    private CmsToolbarClipboardMenu m_clipboard;
122
123    /** The Button for the context menu. */
124    private CmsToolbarContextButton m_context;
125
126    /** The available context menu commands. */
127    private Map<String, I_CmsContextMenuCommand> m_contextMenuCommands;
128
129    /** Edit button. */
130    private CmsToolbarEditButton m_edit;
131
132    /** Button for the elements information. */
133    private CmsToolbarElementInfoButton m_elementsInfo;
134
135    /** Info button. */
136    private CmsToolbarInfoButton m_info;
137
138    /** Move button. */
139    private CmsToolbarMoveButton m_move;
140
141    /** Properties button. */
142    private CmsToolbarSettingsButton m_properties;
143
144    /** Publish button. */
145    private CmsToolbarPublishButton m_publish;
146
147    /** Remove button. */
148    private CmsToolbarRemoveButton m_remove;
149
150    /** Selection button. */
151    private CmsToolbarSelectionButton m_selection;
152
153    /** The style variable for the display mode for small elements. */
154    private CmsStyleVariable m_smallElementsStyle;
155
156    /** The tool-bar. */
157    private CmsToolbar m_toolbar;
158
159    /**
160     * Opens a message dialog with the given content.<p>
161     *
162     * @param title the dialog title
163     * @param displayHtmlContent the dialog content
164     */
165    private static void openMessageDialog(String title, String displayHtmlContent) {
166
167        new CmsErrorDialog(title, displayHtmlContent).center();
168    }
169
170    /**
171     * Disables the edit functionality.<p>
172     *
173     * @param reason the text stating the reason why the edit functionality was disabled
174     */
175    public void disableEditing(String reason) {
176
177        CmsContainerpageController.get().reinitializeButtons();
178        m_add.disable(reason);
179        m_clipboard.disable(reason);
180    }
181
182    /**
183     * Deactivates all toolbar buttons.<p>
184     */
185    public void disableToolbarButtons() {
186
187        for (Widget button : m_toolbar.getAll()) {
188            if (button instanceof I_CmsToolbarButton) {
189                ((I_CmsToolbarButton)button).setEnabled(false);
190            }
191        }
192        m_toolbar.setVisible(false);
193    }
194
195    /**
196     * Enables the toolbar buttons.<p>
197     *
198     * @param hasChanges if the page has changes
199     * @param noEditReason the no edit reason
200     */
201    public void enableToolbarButtons(boolean hasChanges, String noEditReason) {
202
203        for (Widget button : m_toolbar.getAll()) {
204            // enable all buttons that are not equal save or reset or the page has changes
205            if (button instanceof I_CmsToolbarButton) {
206                ((I_CmsToolbarButton)button).setEnabled(true);
207            }
208        }
209        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(noEditReason)) {
210            m_add.disable(noEditReason);
211            m_clipboard.disable(noEditReason);
212        }
213
214        m_toolbar.setVisible(true);
215    }
216
217    /**
218     * Returns the add gallery menu.<p>
219     *
220     * @return the add gallery menu
221     */
222    public CmsToolbarGalleryMenu getAdd() {
223
224        return m_add;
225    }
226
227    /**
228     * Returns the clip-board menu.<p>
229     *
230     * @return the clip-board menu
231     */
232    public CmsToolbarClipboardMenu getClipboard() {
233
234        return m_clipboard;
235    }
236
237    /**
238     * Returns the context menu.<p>
239     *
240     * @return the context menu
241     */
242    public CmsToolbarContextButton getContext() {
243
244        return m_context;
245    }
246
247    /**
248     * Returns the available context menu commands as a map by class name.<p>
249     *
250     * @return the available context menu commands as a map by class name
251     */
252    public Map<String, I_CmsContextMenuCommand> getContextMenuCommands() {
253
254        if (m_contextMenuCommands == null) {
255            I_CmsContextMenuCommandInitializer initializer = GWT.create(I_CmsContextMenuCommandInitializer.class);
256            m_contextMenuCommands = initializer.initCommands();
257        }
258        return m_contextMenuCommands;
259    }
260
261    /**
262     * Returns the publish.<p>
263     *
264     * @return the publish
265     */
266    public CmsToolbarPublishButton getPublish() {
267
268        return m_publish;
269    }
270
271    /**
272     * Returns the selection button.<p>
273     *
274     * @return the selection button
275     */
276    public CmsToolbarSelectionButton getSelection() {
277
278        return m_selection;
279    }
280
281    /**
282     * Returns the tool-bar widget.<p>
283     *
284     * @return the tool-bar widget
285     */
286    public CmsToolbar getToolbar() {
287
288        return m_toolbar;
289    }
290
291    /**
292     * @see com.google.gwt.core.client.EntryPoint#onModuleLoad()
293     */
294    @Override
295    public void onModuleLoad() {
296
297        super.onModuleLoad();
298        CmsRpcContext.get().put(CmsGwtConstants.RpcContext.PAGE_ID, "" + CmsCoreProvider.get().getStructureId());
299        CmsBroadcastTimer.start();
300        JavaScriptObject window = CmsDomUtil.getWindow();
301        CmsDomUtil.setAttribute(window, "__hideEditorCloseButton", "true");
302
303        I_CmsLayoutBundle.INSTANCE.containerpageCss().ensureInjected();
304        I_CmsLayoutBundle.INSTANCE.dragdropCss().ensureInjected();
305
306        I_CmsLayoutBundle.INSTANCE.groupcontainerCss().ensureInjected();
307        org.opencms.gwt.client.ui.css.I_CmsLayoutBundle.INSTANCE.elementSettingsDialogCss().ensureInjected();
308        org.opencms.gwt.client.ui.css.I_CmsLayoutBundle.INSTANCE.listAddCss().ensureInjected();
309
310        final CmsContainerpageController controller = new CmsContainerpageController();
311        final CmsContainerpageHandler containerpageHandler = new CmsContainerpageHandler(controller, this);
312        CmsContentEditorHandler contentEditorHandler = new CmsContentEditorHandler(containerpageHandler);
313        CmsCompositeDNDController dndController = new CmsCompositeDNDController();
314        dndController.addController(new CmsContainerpageDNDController(controller));
315        //dndController.addController(new CmsImageDndController(controller));
316        controller.setDndController(dndController);
317        CmsDNDHandler dndHandler = new CmsDNDHandler(dndController);
318        dndHandler.setAnimationType(AnimationType.SPECIAL);
319        ClickHandler clickHandler = new ClickHandler() {
320
321            /**
322             * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent)
323             */
324            public void onClick(ClickEvent event) {
325
326                I_CmsToolbarButton source = (I_CmsToolbarButton)event.getSource();
327                source.onToolbarClick();
328                if (source instanceof CmsPushButton) {
329                    ((CmsPushButton)source).clearHoverState();
330                }
331            }
332        };
333
334        //        m_bodyMarginTop = CmsDomUtil.getCurrentStyleInt(Document.get().getBody(), Style.marginTop);
335        m_toolbar = new CmsToolbar();
336        m_toolbar.setQuickLaunchHandler(new PageEditorQuickLaunchHandler());
337        m_toolbar.getUserInfo().setHandler(containerpageHandler);
338        m_toolbar.getQuickLauncher().setHandler(containerpageHandler);
339        String title = controller.getData().getAppTitle();
340        if (title == null) {
341            title = Messages.get().key(Messages.GUI_PAGE_EDITOR_TITLE_0);
342        }
343        m_toolbar.setAppTitle(title);
344
345        m_publish = new CmsToolbarPublishButton(containerpageHandler);
346        m_publish.addClickHandler(clickHandler);
347        m_toolbar.addLeft(m_publish);
348
349        m_move = new CmsToolbarMoveButton(containerpageHandler, dndHandler);
350
351        m_edit = new CmsToolbarEditButton(containerpageHandler);
352
353        m_addToFavorites = new CmsAddToFavoritesButton(containerpageHandler);
354
355        m_remove = new CmsToolbarRemoveButton(containerpageHandler);
356
357        m_properties = new CmsToolbarSettingsButton(containerpageHandler);
358        m_info = new CmsToolbarInfoButton(containerpageHandler);
359
360        m_clipboard = new CmsToolbarClipboardMenu(containerpageHandler);
361        m_clipboard.addClickHandler(clickHandler);
362        m_toolbar.addLeft(m_clipboard);
363
364        m_add = new CmsToolbarGalleryMenu(containerpageHandler, dndHandler);
365        m_add.addClickHandler(clickHandler);
366        m_toolbar.addLeft(m_add);
367
368        m_allGalleries = new CmsToolbarAllGalleriesMenu(containerpageHandler, dndHandler);
369        m_allGalleries.addClickHandler(clickHandler);
370        m_toolbar.addLeft(m_allGalleries);
371
372        m_elementsInfo = new CmsToolbarElementInfoButton(containerpageHandler, controller);
373        m_elementsInfo.addClickHandler(clickHandler);
374        m_toolbar.addLeft(m_elementsInfo);
375
376        m_selection = new CmsToolbarSelectionButton(containerpageHandler);
377        m_selection.addClickHandler(clickHandler);
378        m_toolbar.addLeft(m_selection);
379
380        m_context = new CmsToolbarContextButton(containerpageHandler);
381        m_context.addClickHandler(clickHandler);
382        m_toolbar.insertRight(m_context, 0);
383
384
385
386        CmsToolbarFavLocationButton favLocButton = new CmsToolbarFavLocationButton(containerpageHandler);
387        favLocButton.addClickHandler(clickHandler);
388        m_toolbar.insertRight(favLocButton, 1);
389
390        DomGlobal.window.addEventListener("pagehide", event -> {
391            controller.onWindowClose();
392        });
393
394        containerpageHandler.activateSelection();
395
396        RootPanel root = RootPanel.get();
397        root.add(m_toolbar);
398        CmsContainerpageUtil containerpageUtil = new CmsContainerpageUtil(
399            controller,
400            m_edit,
401            m_move,
402            new CmsToolbarListAddButton(containerpageHandler),
403            new CmsToolbarListManagerButton(containerpageHandler),
404            m_info,
405            m_properties,
406            m_addToFavorites,
407            m_remove);
408        CmsEmbeddedDialogFrame.get().preload();
409        controller.init(containerpageHandler, dndHandler, contentEditorHandler, containerpageUtil);
410
411        // export open stack trace dialog function
412        exportMethods(controller);
413    }
414
415    /**
416     * Exports the __openMessageDialog and the __reinitializeEditButtons method to the page context.<p>
417     *
418     * @param controller the controller
419     */
420    private native void exportMethods(CmsContainerpageController controller) /*-{
421                var contr = controller;
422                $wnd.opencms = {
423                        openStacktraceDialog : function(event) {
424                                event = (event) ? event : ((window.event) ? window.event : "");
425                                var elem = (event.target) ? event.target : event.srcElement;
426                                if (elem != null) {
427                                        var children = elem.getElementsByTagName("span");
428                                        if (children.length > 0) {
429                                                var title = children[0].getAttribute("title");
430                                                var content = children[0].innerHTML;
431                                                @org.opencms.ade.containerpage.client.CmsContainerpageEditor::openMessageDialog(Ljava/lang/String;Ljava/lang/String;)(title,content);
432                                        }
433                                }
434                        },
435                        reinitializeEditButtons : function() {
436                                contr.@org.opencms.ade.containerpage.client.CmsContainerpageController::reinitializeButtons()();
437                        }
438                }
439    }-*/;
440}