View Javadoc
1   package gboat2.base.view.components;
2   
3   import gboat2.base.bridge.util.json.JsonUtil;
4   import gboat2.base.view.GboatViewException;
5   import gboat2.base.view.GboatViewUtil;
6   
7   import javax.servlet.http.HttpServletRequest;
8   import javax.servlet.http.HttpServletResponse;
9   
10  import org.apache.commons.lang3.StringUtils;
11  import org.apache.struts2.components.UIBean;
12  import org.apache.struts2.views.annotations.StrutsTag;
13  import org.apache.struts2.views.annotations.StrutsTagAttribute;
14  
15  import com.fasterxml.jackson.databind.ObjectMapper;
16  import com.fasterxml.jackson.databind.node.ObjectNode;
17  import com.opensymphony.xwork2.util.ValueStack;
18  
19  /**
20   * <p>
21   * 让被选择元素可以被鼠标拖动,详见 <a href="http://www.jqueryui.com/draggable/"
22   * target="_blank">jQuery UI Draggable plugin</a>。
23   * </p>
24   * <p>
25   * 如果你想把元素拖放到另一个元素内部,请参考 {@link Droppable},该组件为被拖动元素提供了一个目标容器。
26   * </p>
27   * 
28   * @author <a href="mailto:[email protected]">何明旺</a>
29   * @since 3.0
30   * @date 2014年5月4日
31   */
32  @StrutsTag(
33          name="draggable",
34          tldTagClass="gboat2.base.view.jsp.DraggableTag",
35          description="让被选择元素可以被鼠标拖动,详见 http://www.jqueryui.com/draggable/")
36  public class Draggable extends UIBean{
37      
38      public static final String TEMPLATE = "draggable.ftl";
39  
40      protected String addClasses;
41      protected String appendTo;
42      protected String axis;
43      protected String cancel;
44      protected String connectToSortable;
45      protected String containment;
46      protected String cursor;
47      protected String cursorAt;
48      protected String delay;
49      protected String disabled;
50      protected String distance;
51      protected String grid;
52      protected String handle;
53      protected String helper;
54      protected String iframeFix;
55      protected String opacity;
56      protected String refreshPositions;
57      protected String revert;
58      protected String revertDuration;
59      protected String scope;
60      protected String scroll;
61      protected String scrollSensitivity;
62      protected String scrollSpeed;
63      protected String snap;
64      protected String snapMode;
65      protected String snapTolerance;
66      protected String stack;
67      protected String zIndex;
68  
69      protected String onDragTopics;
70      protected String onStartTopics;
71      protected String onStopTopics;
72      
73      public Draggable(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
74          super(stack,req, res);
75      }
76  
77      @Override
78      protected String getDefaultTemplate() {
79          return TEMPLATE;
80      }
81      
82      @Override
83      public void evaluateExtraParams() {
84          UIBean component = (UIBean) findAncestor(UIBean.class);
85          component.addParameter("draggable", Boolean.TRUE);
86          
87          Object componentId = component.getParameters().get("id");
88          if(componentId == null || StringUtils.isBlank(componentId.toString()))
89              throw new GboatViewException("必须为可以通过拖拽调整尺寸的元素设置 id 属性");
90          addParameter("componentId", componentId);
91          
92          ObjectMapper om = JsonUtil.generateMapper();
93          ObjectNode options = om.createObjectNode();
94          
95          if (addClasses != null)
96              options.put("addClasses", (Boolean) findValue(addClasses, Boolean.class));
97          
98          if (appendTo != null)
99              options.put("appendTo", findString(appendTo));
100         
101         if (axis != null)
102             options.put("axis", findString(axis));
103         
104         if (cancel != null)
105             options.put("cancel", findString(cancel));
106         
107         if (connectToSortable != null)
108             options.put("connectToSortable", findString(connectToSortable));
109 
110         if (containment != null)
111             options.put("containment", findString(containment));
112         
113         if (cursor != null)
114             options.put("cursor", findString(cursor));
115 
116         if (cursorAt != null)
117             options.put("cursorAt", findString(cursorAt));
118         
119         if (delay != null)
120             options.put("delay", (Integer) findValue(delay, Integer.class));
121         
122         if (disabled != null)
123             options.put("disabled", (Boolean) findValue(disabled, Boolean.class));
124         
125         if (distance != null)
126             options.put("distance", (Integer) findValue(distance, Integer.class));
127 
128         if (grid != null)
129             options.put("grid", GboatViewUtil.string2ArrayNode(findString("grid"), om));
130         
131         if (handle != null)
132             options.put("handle", findString(handle));
133         
134         if (helper != null)
135             options.put("helper", findString(helper));
136         
137         if (iframeFix != null)
138             options.put("iframeFix", (Boolean) findValue(iframeFix, Boolean.class));
139         
140         if (opacity != null)
141             options.put("opacity", (Float) findValue(opacity, Float.class));
142         
143         if (refreshPositions != null)
144             options.put("refreshPositions", (Boolean) findValue(refreshPositions, Boolean.class));
145 
146         if (revert != null)
147             options.put("revert", (Boolean) findValue(revert, Boolean.class));
148         
149         if (revertDuration != null)
150             options.put("revertDuration", (Integer) findValue(revertDuration, Integer.class));
151         
152         if (scope != null)
153             options.put("scope", findString(scope));
154         
155         if (scroll != null)
156             options.put("scroll", (Boolean) findValue(scroll, Boolean.class));
157         
158         if (scrollSensitivity != null)
159             options.put("scrollSensitivity", (Integer) findValue(scrollSensitivity, Integer.class));
160 
161         if (scrollSpeed != null)
162             options.put("scrollSpeed", (Integer) findValue(scrollSpeed, Integer.class));
163         
164         if (snap != null)
165             options.put("snap", (Boolean) findValue(snap, Boolean.class));
166         
167         if (snapMode != null)
168             options.put("snapMode", findString(snapMode));
169         
170         if (snapTolerance != null)
171             options.put("snapTolerance", (Integer) findValue(snapTolerance, Integer.class));
172         
173         if (stack != null)
174             options.put("stack", findString(stack));
175         
176         if (zIndex != null) {
177             options.put("zIndex", (Integer) findValue(zIndex, Integer.class));
178         }
179 
180         addParameter("options", options);
181 
182         if (onDragTopics != null)
183             addParameter("onDragTopics", findString(onDragTopics));
184         
185         if (onStartTopics != null)
186             addParameter("onStartTopics", findString(onStartTopics));
187         
188         if (onStopTopics != null)
189             addParameter("onStopTopics", findString(onStopTopics));
190     }
191 
192     @StrutsTagAttribute(description = "如果值设置为false, ui-draggable样式类将不能被添加引用。当在大量元素上调用.draggable()时,你可能会想要这样设置,作为一个性能优化。详见 http://api.jqueryui.com/draggable/#option-addClasses", type = "Boolean", defaultValue="true")
193     public void setAddClasses(String addClasses) {
194         this.addClasses = addClasses;
195     }
196 
197     @StrutsTagAttribute(description = "当进行拖动时,拖动组件助手应该被添加到的元素。可选值为 parent 或 jQuery 选择器。详见 http://api.jqueryui.com/draggable/#option-appendTo", defaultValue="parent")
198     public void setAppendTo(String appendTo) {
199         this.appendTo = appendTo;
200     }
201 
202     @StrutsTagAttribute(description = "约束拖动的动作只能在水平(x轴)或垂直(y轴)上执行。可选值: x, y。详见 http://api.jqueryui.com/draggable/#option-axis")
203     public void setAxis(String axis) {
204         this.axis = axis;
205     }
206 
207     @StrutsTagAttribute(description = "防止在指定元素上开始拖动。详见 http://api.jqueryui.com/draggable/#option-cancel", defaultValue="input,textarea,button,select,option")
208     public void setCancel(String cancel) {
209         this.cancel = cancel;
210     }
211 
212     @StrutsTagAttribute(description = "允许draggable被拖拽到指定的sortables中。如果使用了该选项,被拖动的元素可以被放置于一个应用了排序组件的元素列表中并成为该列表的一部分。注意: 为了完美的使用该特性,helper选项必须被设置为 clone。详见 http://api.jqueryui.com/draggable/#option-connectToSortable")
213     public void setConnectToSortable(String connectToSortable) {
214         this.connectToSortable = connectToSortable;
215     }
216 
217     @StrutsTagAttribute(description = "可以限制draggable只能在指定的元素或区域的边界以内进行拖动。可以是一个 jquery 选择器或  parent, document, window,详见 http://api.jqueryui.com/draggable/#option-containment")
218     public void setContainment(String containment) {
219         this.containment = containment;
220     }
221 
222     @StrutsTagAttribute(description = "指定在做拖拽动作时,鼠标的CSS样式。详见 http://api.jqueryui.com/draggable/#option-cursor", defaultValue="auto")
223     public void setCursor(String cursor) {
224         this.cursor = cursor;
225     }
226 
227     @StrutsTagAttribute(description = "设置拖动帮手相对于鼠标光标的偏移量。坐标可被指定为一个散列 使用的组合中的一个或两个键:{ top, left, right, bottom } 。详见 http://api.jqueryui.com/draggable/#option-cursorAt")
228     public void setCursorAt(String cursorAt) {
229         this.cursorAt = cursorAt;
230     }
231 
232     @StrutsTagAttribute(description = "当鼠标按下后,指定时延迟间后才开始激活拖拽动作(单位:毫秒),此选项可用来阻止当点击一个元素时可能发生的非期望拖动行为。详见 http://api.jqueryui.com/draggable/#option-delay", type="Integer", defaultValue="0")
233     public void setDelay(String delay) {
234         this.delay = delay;
235     }
236 
237     @StrutsTagAttribute(description = "如果该值设置为true,拖动特效将被禁用。。详见 http://api.jqueryui.com/draggable/#option-disabled", type="Boolean", defaultValue="false")
238     public void setDisabled(String disabled) {
239         this.disabled = disabled;
240     }
241 
242     @StrutsTagAttribute(description = "当鼠标点下后,只有移动指定像素后才开始激活拖拽动作,单位为像素,此选项可用来阻止当点击一个元素时可能发生的非期望拖动行为。详见 http://api.jqueryui.com/draggable/#option-distance", type="Integer", defaultValue="1")
243     public void setDistance(String distance) {
244         this.distance = distance;
245     }
246 
247     @StrutsTagAttribute(description = "拖拽元素时,只能以指定大小的方格进行拖动。数组形式为[ x, y ]。详见 http://api.jqueryui.com/draggable/#option-grid", type="Integer[]")
248     public void setGrid(String grid) {
249         this.grid = grid;
250     }
251 
252     @StrutsTagAttribute(description = "只有在特定的元素上触发鼠标按下事件时,才可以拖动。 只有被拖到元素的子元素才可以应用该参数。详见 http://api.jqueryui.com/draggable/#option-handle")
253     public void setHandle(String handle) {
254         this.handle = handle;
255     }
256 
257     @StrutsTagAttribute(description = "允许一个元素被用来进行拖动。如果值设置为 clone, 那么该元素将会被复制,并且被复制的元素将被拖动。详见 http://api.jqueryui.com/draggable/#option-helper")
258     public void setHelper(String helper) {
259         this.helper = helper;
260     }
261 
262     @StrutsTagAttribute(description = "在拖动期间阻止 iframe 捕捉鼠标移过事件。当鼠标指针可能不在拖动助手元素之上时,该参数非常有用。详见 http://api.jqueryui.com/draggable/#option-iframeFix", type = "Boolean", defaultValue="false")
263     public void setIframeFix(String iframeFix) {
264         this.iframeFix = iframeFix;
265     }
266 
267     @StrutsTagAttribute(description = "当拖动时,拖动助手元素的不透明度。值为 0.01 到 1 之间,详见 http://api.jqueryui.com/draggable/#option-opacity", type="Float")
268     public void setOpacity(String opacity) {
269         this.opacity = opacity;
270     }
271 
272     @StrutsTagAttribute(description = "如果设置为true, 所有的可拖动位置在每次鼠标移动时都会被计算。注意: 这解决了具有高度动态内容页面的问题,但是却降低了性能。详见 http://api.jqueryui.com/draggable/#option-refreshPositions ", type = "Boolean", defaultValue="false")
273     public void setRefreshPositions(String refreshPositions) {
274         this.refreshPositions = refreshPositions;
275     }
276 
277     @StrutsTagAttribute(description = "当拖动停止时,元素是否要被重置到它的初始位置。详见 http://api.jqueryui.com/draggable/#option-revert", type = "Boolean", defaultValue="false")
278     public void setRevert(String revert) {
279         this.revert = revert;
280     }
281 
282     @StrutsTagAttribute(description = "动画重置时间, 单位为微秒。如果revert选项设置为false,则忽略(即被拖到元素不会被重置)。详见 http://api.jqueryui.com/draggable/#option-revertDuration", type="Integer", defaultValue="500")
283     public void setRevertDuration(String revertDuration) {
284         this.revertDuration = revertDuration;
285     }
286 
287     @StrutsTagAttribute(description = "被用来将拖动组件和拖动至容器组件的参数进行分组, 除了拖动至容器组件的 accept 选项。 如果一个一般拖动组件的scope参数值和一个拖动至容器组件的scope参数值一样,那么这个一般拖动组件将被接受为拖动至容器组件。详见 http://api.jqueryui.com/draggable/#option-scope", defaultValue="default")
288     public void setScope(String scope) {
289         this.scope = scope;
290     }
291 
292     @StrutsTagAttribute(description = "如果设置为true, 当拖动时,div盒模型将自动翻滚。详见 http://api.jqueryui.com/draggable/#option-scroll", type = "Boolean", defaultValue="true")
293     public void setScroll(String scroll) {
294         this.scroll = scroll;
295     }
296 
297     @StrutsTagAttribute(description = "离开可视区域边缘多少距离开始滚动。距离是相对指针进行计算的,而不是被拖到元素本身。如果 scroll 选项设置为false,则不滚动。详见 http://api.jqueryui.com/draggable/#option-scrollSensitivity", type="Integer", defaultValue="20")
298     public void setScrollSensitivity(String scrollSensitivity) {
299         this.scrollSensitivity = scrollSensitivity;
300     }
301 
302     @StrutsTagAttribute(description = "当鼠标指针的值小于scrollSensitivity的值时,窗口滚动的速度。如果scroll选项设置为false,则该参数无效。详见 http://api.jqueryui.com/draggable/#option-scrollSpeed", type="Integer", defaultValue="20")
303     public void setScrollSpeed(String scrollSpeed) {
304         this.scrollSpeed = scrollSpeed;
305     }
306 
307     @StrutsTagAttribute(description = "决定一个元素是否应该吸附到其它元素上。当设置为 true时, 元素将可以吸附到所有其它可拖动元素上。详见 http://api.jqueryui.com/draggable/#option-snap", type="Boolean", defaultValue = "false")
308     public void setSnap(String snap) {
309         this.snap = snap;
310     }
311 
312     @StrutsTagAttribute(description = "决定可拖动元素将要吸附到哪个元素的边缘。如果snap选项设置为 false,则忽略该参数。 可选值: inner, outer, both,详见 http://api.jqueryui.com/draggable/#option-snapMode", defaultValue="both")
313     public void setSnapMode(String snapMode) {
314         this.snapMode = snapMode;
315     }
316 
317     @StrutsTagAttribute(description = "当距离可吸附元素多远时,触发吸附事件。如果snap选项设置为false,则忽略该参数。详见 http://api.jqueryui.com/draggable/#option-snapTolerance", type="Integer", defaultValue="20")
318     public void setSnapTolerance(String snapTolerance) {
319         this.snapTolerance = snapTolerance;
320     }
321 
322     @StrutsTagAttribute(description = "控制由选择器所触发的一系列元素的z-index值, 总是将当前被拖动对象至于最前。对于像窗口管理这样的应用来说,非常有用。详见 http://api.jqueryui.com/draggable/#option-stack")
323     public void setStack(String stack) {
324         this.stack = stack;
325     }
326 
327     @StrutsTagAttribute(description = "当被拖动时,拖动助手的Z-index值。详见 http://api.jqueryui.com/draggable/#option-zIndex", type="Integer")
328     public void setZIndex(String zIndex) {
329         this.zIndex = zIndex;
330     }
331 
332     @StrutsTagAttribute(description = "当鼠标在拖动过程中移动时触发,详见 http://api.jqueryui.com/draggable/#event-drag")
333     public void setOnDragTopics(String onDragTopics) {
334         this.onDragTopics = onDragTopics;
335     }
336 
337     @StrutsTagAttribute(description = "当拖动开始时触发,详见 http://api.jqueryui.com/draggable/#event-start")
338     public void setOnStartTopics(String onStartTopics) {
339         this.onStartTopics = onStartTopics;
340     }
341 
342     @StrutsTagAttribute(description = "当拖动停止时触发,详见 http://api.jqueryui.com/draggable/#event-stop")
343     public void setOnStopTopics(String onStopTopics) {
344         this.onStopTopics = onStopTopics;
345     }
346 
347 }