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.dnd;
35  
36  import static info.magnolia.templating.editor.client.jsni.JavascriptUtils.moveComponent;
37  
38  import com.google.gwt.dom.client.Element;
39  
40  
41  import info.magnolia.templating.editor.client.PageEditor;
42  import info.magnolia.templating.editor.client.dom.MgnlElement;
43  import info.magnolia.templating.editor.client.widget.controlbar.ComponentBar;
44  import info.magnolia.templating.editor.client.widget.placeholder.ComponentPlaceHolder;
45  
46  /**
47   * DragAndDropLegacy. GWT port of legacy /magnolia-module-admininterface/src/main/resources/mgnl-resources/admin-js/inline.js
48   */
49  public class LegacyDragAndDrop {
50  
51      public static ComponentBar sourceBar;
52      private static MoveWidget moveDiv;
53  
54      public static void moveComponentStart(ComponentBar bar) {
55              toggleStyles(bar, true);
56  
57              // reset native drag and drop
58              bar.setDraggable(false);
59              MgnlElement area = bar.getMgnlElement().getParentArea();
60              if (area != null) {
61                  for (MgnlElement component : area.getComponents()) {
62                      ComponentBar componentBar = (ComponentBar) PageEditor.model.getEditBar(component);
63                      if (componentBar != null && componentBar != bar) {
64                          componentBar.setDraggable(false);
65                      }
66                  }
67              }
68              sourceBar = bar;
69              int height =bar.getOffsetHeight();
70              int width = bar.getOffsetWidth();
71              moveDiv = new MoveWidget(height, width);
72      }
73  
74      public static void moveComponentOver(ComponentBar bar) {
75          if (isMoving()) {
76              String idSource = sourceBar.getNodeName();
77  
78              if (!bar.getNodeName().equals(idSource)){
79                  bar.setStyleName("moveOver", true);
80              }
81          }
82      }
83  
84      public static void moveComponentOut(ComponentBar bar) {
85          if (isMoving()) {
86              String idSource = sourceBar.getNodeName();
87  
88              if (!bar.getNodeName().equals(idSource)){
89                  bar.setStyleName("moveOver", false);
90              }
91          }
92      }
93  
94      public static void moveComponentEnd(ComponentBar bar) {
95          if (isMoving()) {
96  
97              String idSource = sourceBar.getNodeName();
98  
99              if (!bar.getNodeName().equals(idSource)) {
100                 int xTarget = bar.getAbsoluteLeft();
101                 int yTarget = bar.getAbsoluteTop();
102                 int xOrigin = sourceBar.getAbsoluteLeft();
103                 int yOrigin = sourceBar.getAbsoluteTop();
104 
105                 boolean isDragUp = yOrigin > yTarget;
106                 boolean isDragDown = !isDragUp;
107                 boolean isDragLeft = xOrigin > xTarget;
108                 boolean isDragRight = !isDragLeft;
109 
110                 String order = null;
111 
112                 if(isDragUp || isDragLeft) {
113                     order = "before";
114                 } else if(isDragDown || isDragRight) {
115                     order = "after";
116                 }
117                 String parentPath = bar.getPath().substring(0, bar.getPath().lastIndexOf("/"));
118                 moveComponent(bar.getNodeName(), idSource, parentPath, order);
119             }
120         }
121     }
122 
123     public static void moveComponentReset() {
124         if (isMoving()) {
125             toggleStyles(sourceBar, false);
126 
127             // reset native drag and drop
128             sourceBar.setDraggable(true);
129             MgnlElement area = sourceBar.getMgnlElement().getParentArea();
130             if (area != null) {
131                 for (MgnlElement component : area.getComponents()) {
132                     ComponentBar componentBar = (ComponentBar) PageEditor.model.getEditBar(component);
133                     if (componentBar != null && componentBar != sourceBar) {
134                         componentBar.setDraggable(false);
135                     }
136                 }
137             }
138 
139             sourceBar = null;
140             moveDiv.detach();
141         }
142     }
143 
144     private static void toggleStyles(ComponentBar bar, boolean isMove) {
145         bar.toggleButtons(!isMove);
146 
147         bar.setStyleName("moveSource", isMove);
148 
149         MgnlElement area = bar.getMgnlElement().getParentArea();
150         if (area != null) {
151             for (MgnlElement component : area.getComponents()) {
152                 ComponentBar componentBar = (ComponentBar) PageEditor.model.getEditBar(component);
153                 if (componentBar != null && componentBar != bar) {
154                     componentBar.setStyleName("moveTarget", isMove);
155 
156                     componentBar.getElement().setDraggable(Element.DRAGGABLE_TRUE);
157 
158                 }
159             }
160             ComponentPlaceHolder placeholder = PageEditor.model.getComponentPlaceHolder(area);
161             if (placeholder != null) {
162                 placeholder.setStyleName("moveOngoing", isMove);
163             }
164         }
165     }
166 
167     public static boolean isMoving() {
168         return (sourceBar != null) ? true : false;
169     }
170 }