1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
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
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 }