View Javadoc
1   /**
2    * This file Copyright (c) 2011-2014 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.model.focus;
35  
36  import info.magnolia.templating.editor.client.dom.MgnlElement;
37  import info.magnolia.templating.editor.client.jsni.JavascriptUtils;
38  import info.magnolia.templating.editor.client.model.ModelStorage;
39  import info.magnolia.templating.editor.client.widget.dnd.LegacyDragAndDrop;
40  
41  import com.google.gwt.dom.client.Element;
42  
43  /**
44   * Helper class to keep tack on selected items.
45   */
46  public class FocusModelImpl3 implements FocusModel {
47  
48      private final ModelStorage model;
49      private boolean rootSelected = false;
50  
51      public FocusModelImpl3(ModelStorage model) {
52          super();
53          this.model = model;
54      }
55  
56      @Override
57      public void onMouseUp(Element element) {
58  
59          MgnlElement mgnlElement = model.getMgnlElement(element);
60  
61          if (LegacyDragAndDrop.isMoving()) {
62              if (!LegacyDragAndDrop.sourceBar.getMgnlElement().isRelated(mgnlElement)) {
63                  LegacyDragAndDrop.moveComponentReset();
64                  reset();
65              }
66              return;
67          }
68          if (mgnlElement == null) {
69              reset();
70          }
71  
72          else if (mgnlElement.getParentArea() != null) {
73              MgnlElement area = mgnlElement.getParentArea();
74              if (area != model.getSelectedMgnlElement()) {
75                  deSelect();
76                  hideRoot();
77                  toggleSelection(area, true, true);
78                  if (area != area.getTopParentArea()) {
79                      toggleSelection(area.getTopParentArea(), true, false);
80                  }
81                  model.setSelectedMgnlElement(area);
82              }
83          }
84      }
85  
86      @Override
87      public void onLoadSelect(MgnlElement selectedMgnlElement) {
88          model.setSelectedMgnlElement(selectedMgnlElement);
89          toggleRootAreaBar(false);
90          showRootPlaceHolder();
91          toggleSelection(selectedMgnlElement, true, true);
92      }
93      @Override
94      public void reset() {
95          deSelect();
96          toggleRootAreaBar(true);
97          showRootPlaceHolder();
98      }
99  
100     private void toggleSelection(MgnlElement mgnlElement, boolean visible, boolean setCookies) {
101         if (setCookies) {
102             if (visible) {
103                 String contentId = mgnlElement.getAttribute("content");
104                 JavascriptUtils.setEditorContentIdCookie(contentId);
105             } else {
106                 JavascriptUtils.removeEditorContentIdCookie();
107             }
108         }
109 
110         if (model.getEditBar(mgnlElement) != null) {
111             model.getEditBar(mgnlElement).setVisible(visible);
112         }
113         if (model.getAreaEndBar(mgnlElement) != null) {
114             model.getAreaEndBar(mgnlElement).setVisible(visible);
115         }
116 
117         // toggle all direct child-areas place holders visibility - does this case occur (isn't this method called always on parent area)?
118         for (MgnlElement area : mgnlElement.getAreas()) {
119             if (model.getAreaPlaceHolder(area) != null) {
120                 model.getAreaPlaceHolder(area).setVisible(visible);
121             }
122         }
123 
124         for (MgnlElement component : mgnlElement.getComponents()) {
125 
126             // toggle all child-components edit bar visibility
127             if (model.getEditBar(component) != null) {
128                 model.getEditBar(component).setVisible(visible);
129             }
130             // toggle all child-components-area placeholder visibility
131             for (MgnlElement area : component.getAreas()) {
132                 // we have to make area always visible...
133                 if (model.getAreaPlaceHolder(area) != null) { // ...if it's empty we render place holder
134                     model.getAreaPlaceHolder(area).setVisible(visible);
135                 } else { // ...or we render edit bars if it has some components (otherwise the area containing only empty components wouldn't be accessible)
136                     if (model.getEditBar(area) != null) {
137                         model.getEditBar(area).setVisible(visible);
138                     }
139                     if (model.getAreaEndBar(area) != null) {
140                         model.getAreaEndBar(area).setVisible(visible);
141                     }
142                 }
143             }
144         }
145 
146         if (model.getAreaPlaceHolder(mgnlElement)!= null) {
147             if (mgnlElement.getParent() != null) {
148                 model.getAreaPlaceHolder(mgnlElement).setVisible(visible);
149             }
150             model.getAreaPlaceHolder(mgnlElement).setActive(visible);
151         }
152 
153         if (model.getComponentPlaceHolder(mgnlElement) != null) {
154             model.getComponentPlaceHolder(mgnlElement).setVisible(visible);
155         }
156     }
157 
158     private void deSelect() {
159         MgnlElement parentArea = model.getSelectedMgnlElement() != null ? model.getSelectedMgnlElement().getParentArea() : null;
160         MgnlElement topParentArea = parentArea != null ? parentArea.getTopParentArea() : null;
161 
162         if (parentArea  != null) {
163             toggleSelection(parentArea, false, true);
164             if (topParentArea != parentArea) {
165                 toggleSelection(topParentArea, false, false);
166             }
167             model.setSelectedMgnlElement(null);
168         }
169     }
170 
171     @Override
172     public void toggleRootAreaBar(boolean visible) {
173         deSelect();
174 
175         this.rootSelected = !this.rootSelected;
176         for (MgnlElement root : model.getRootElements()) {
177             if (model.getEditBar(root) != null) {
178                 model.getEditBar(root).setVisible(visible);
179             }
180             if (model.getAreaEndBar(root) != null) {
181                 model.getAreaEndBar(root).setVisible(visible);
182             }
183             if (model.getAreaPlaceHolder(root) != null) {
184                 model.getAreaPlaceHolder(root).setVisible(visible);
185             }
186         }
187     }
188 
189     private void showRootPlaceHolder() {
190         for (MgnlElement root : model.getRootElements()) {
191             if (model.getAreaPlaceHolder(root) != null) {
192                 model.getAreaPlaceHolder(root).setVisible(true);
193                 model.getAreaPlaceHolder(root).setActive(false);
194             }
195         }
196     }
197 
198     private void hideRoot() {
199         for (MgnlElement root : model.getRootElements()) {
200             if (model.getEditBar(root) != null) {
201                 model.getEditBar(root).setVisible(false);
202             }
203             if (model.getAreaEndBar(root) != null) {
204                 model.getAreaEndBar(root).setVisible(false);
205             }
206             if (model.getComponentPlaceHolder(root) != null) {
207                 model.getComponentPlaceHolder(root).setVisible(false);
208             }
209             if (model.getAreaPlaceHolder(root) != null) {
210                 model.getAreaPlaceHolder(root).setVisible(true);
211             }
212         }
213     }
214 }