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  import info.magnolia.templating.editor.client.PageEditor;
38  import info.magnolia.templating.editor.client.dom.MgnlElement;
39  import info.magnolia.templating.editor.client.widget.controlbar.ComponentBar;
40  import info.magnolia.templating.editor.client.widget.placeholder.ComponentPlaceHolder;
41  
42  import com.google.gwt.dom.client.Element;
43  import com.google.gwt.event.dom.client.DragEndEvent;
44  import com.google.gwt.event.dom.client.DragEndHandler;
45  import com.google.gwt.event.dom.client.DragLeaveEvent;
46  import com.google.gwt.event.dom.client.DragLeaveHandler;
47  import com.google.gwt.event.dom.client.DragOverEvent;
48  import com.google.gwt.event.dom.client.DragOverHandler;
49  import com.google.gwt.event.dom.client.DragStartEvent;
50  import com.google.gwt.event.dom.client.DragStartHandler;
51  import com.google.gwt.event.dom.client.DropEvent;
52  import com.google.gwt.event.dom.client.DropHandler;
53  
54  /**
55   * DragAndDropImpl.
56   */
57  public class DragAndDropImpl {
58  
59      public void dragAndDrop (final ComponentBar bar) {
60          bar.addDomHandler(new DragStartHandler() {
61              @Override
62              public void onDragStart(DragStartEvent event) {
63                  bar.toggleButtons(false);
64  
65                  bar.setStyleName("moveSource", true);
66  
67                  MgnlElement area = bar.getMgnlElement().getParentArea();
68                  if (area != null) {
69                      for (MgnlElement component : area.getComponents()) {
70                          ComponentBar componentBar = (ComponentBar) PageEditor.model.getEditBar(component);
71                          if (componentBar != null && componentBar != bar) {
72                              componentBar.setStyleName("moveTarget", true);
73                          }
74                      }
75                      ComponentPlaceHolder placeholder = (ComponentPlaceHolder) PageEditor.model.getComponentPlaceHolder(area);
76                      if (placeholder != null) {
77                          placeholder.setStyleName("moveOngoing", true);
78                      }
79                  }
80  
81                  int x = bar.getAbsoluteLeft();
82                  int y = bar.getAbsoluteTop();
83                  event.setData("text", bar.getNodeName() + "," + x +","+y);
84                  event.getDataTransfer().setDragImage(bar.getElement(), 10, 10);
85  
86              }
87          }, DragStartEvent.getType());
88  
89          bar.addDomHandler(new DragEndHandler() {
90              @Override
91              public void onDragEnd(DragEndEvent event) {
92                  bar.toggleButtons(true);
93  
94                  bar.setStyleName("moveSource", false);
95  
96                  MgnlElement area = bar.getMgnlElement().getParentArea();
97                  if (area != null) {
98                      for (MgnlElement component : area.getComponents()) {
99                          ComponentBar componentBar = (ComponentBar) PageEditor.model.getEditBar(component);
100                         if (componentBar != null && componentBar != bar) {
101                             componentBar.setStyleName("moveTarget", false);
102                         }
103                     }
104                     ComponentPlaceHolder placeholder = (ComponentPlaceHolder) PageEditor.model.getComponentPlaceHolder(area);
105                     if (placeholder != null) {
106                         placeholder.setStyleName("moveOngoing", false);
107                     }
108                 }
109             }
110         }, DragEndEvent.getType());
111 
112         bar.addDomHandler(new DragOverHandler() {
113             @Override
114             public void onDragOver(DragOverEvent event) {
115                 String data = event.getData("text");
116                 String[] tokens = data.split(",");
117                 String idSource = tokens[0];
118 
119                 if (!bar.getNodeName().equals(idSource)){
120                     bar.setStyleName("moveOver", true);
121                 }
122                 event.stopPropagation();
123             }
124         }, DragOverEvent.getType());
125 
126         bar.addDomHandler(new DragLeaveHandler() {
127 
128             @Override
129             public void onDragLeave(DragLeaveEvent event) {
130                 bar.setStyleName("moveOver", false);
131                 event.stopPropagation();
132             }
133         }, DragLeaveEvent.getType());
134 
135         bar.addDomHandler(new DropHandler() {
136             @Override
137             public void onDrop(DropEvent event) {
138                 String data = event.getData("text");
139                 String[] tokens = data.split(",");
140                 String idSource = tokens[0];
141 
142                 if (!bar.getNodeName().equals(idSource)) {
143                     int xTarget = bar.getAbsoluteLeft();
144                     int yTarget = bar.getAbsoluteTop();
145                     int xOrigin = Integer.valueOf(tokens[1]);
146                     int yOrigin = Integer.valueOf(tokens[2]);
147 
148                     boolean isDragUp = yOrigin > yTarget;
149                     boolean isDragDown = !isDragUp;
150                     boolean isDragLeft = xOrigin > xTarget;
151                     boolean isDragRight = !isDragLeft;
152 
153                     String order = null;
154 
155                     if(isDragUp || isDragLeft) {
156                         order = "before";
157                     } else if(isDragDown || isDragRight) {
158                         order = "after";
159                     }
160                     String parentPath = bar.getPath().substring(0, bar.getPath().lastIndexOf("/"));
161                     moveComponent(bar.getNodeName(), idSource, parentPath, order);
162                 }
163 
164                 event.preventDefault();
165             }
166         }, DropEvent.getType());
167 
168         bar.getElement().setDraggable(Element.DRAGGABLE_TRUE);
169     }
170 }