View Javadoc
1   package gboat2.base.view.components;
2   
3   import gboat2.base.bridge.util.json.JsonUtil;
4   import gboat2.base.view.GboatViewException;
5   
6   import javax.servlet.http.HttpServletRequest;
7   import javax.servlet.http.HttpServletResponse;
8   
9   import org.apache.commons.lang3.StringUtils;
10  import org.apache.struts2.components.UIBean;
11  import org.apache.struts2.views.annotations.StrutsTag;
12  import org.apache.struts2.views.annotations.StrutsTagAttribute;
13  
14  import com.fasterxml.jackson.databind.node.ObjectNode;
15  import com.opensymphony.xwork2.util.ValueStack;
16  
17  /**
18   * <p>
19   * 创建拖动元素的目标,使所选元素可拖放(这意味着 {@link Draggable} 拖动可以被拖放接受)。详见
20   * <a href="http://www.jqueryui.com/droppable/" target="_blank">jQuery UI Droppable plugin</a>。
21   * </p>
22   * 
23   * @author <a href="mailto:[email protected]">何明旺</a>
24   * @since 3.0
25   * @date 2014年5月4日
26   */
27  @StrutsTag(
28          name="droppable",
29          tldTagClass="gboat2.base.view.jsp.DroppableTag",
30          description="创建拖动元素的目标,详见 http://www.jqueryui.com/droppable/")
31  public class Droppable extends UIBean{
32      
33      public static final String TEMPLATE = "droppable.ftl";
34  
35      protected String accept;
36      protected String activeClass;
37      protected String addClasses;
38      protected String disabled; //xxx
39      protected String greedy;
40      protected String hoverClass;
41      protected String scope;
42      protected String tolerance;
43  
44      protected String onActivateTopics;
45      protected String onDeactivateTopics;
46      protected String onDropTopics;
47      protected String onOutTopics;
48      protected String onOverTopics;
49  
50      public Droppable(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
51          super(stack,req, res);
52      }
53  
54      @Override
55      protected String getDefaultTemplate() {
56          return TEMPLATE;
57      }
58      
59      @Override
60      public void evaluateExtraParams() {
61          UIBean component = (UIBean) findAncestor(UIBean.class);
62          component.addParameter("droppable", Boolean.TRUE);
63          
64          Object componentId = component.getParameters().get("id");
65          if(componentId == null || StringUtils.isBlank(componentId.toString()))
66              throw new GboatViewException("必须为可以通过拖拽调整尺寸的元素设置 id 属性");
67          addParameter("componentId", componentId);
68          
69          ObjectNode options = JsonUtil.generateMapper().createObjectNode();
70          if (accept != null)
71              options.put("accept", findString(accept));
72          
73          if (activeClass != null)
74              options.put("activeClass", findString(activeClass));
75          
76          if (addClasses != null)
77              options.put("addClasses", (Boolean) findValue(addClasses, Boolean.class));
78          
79          if (disabled != null)
80              options.put("disabled", (Boolean) findValue(disabled, Boolean.class));
81          
82          if (greedy != null)
83              options.put("greedy", (Boolean) findValue(greedy, Boolean.class));
84  
85          if (hoverClass != null)
86              options.put("hoverClass", findString(hoverClass));
87  
88          if (scope != null)
89              options.put("scope", findString(scope));
90          
91          if (tolerance != null)
92              options.put("tolerance", findString(tolerance));
93          
94          addParameter("options", options);
95  
96          if (onActivateTopics != null)
97              addParameter("onActivateTopics", findString(onActivateTopics));
98          
99          if (onDeactivateTopics != null)
100             addParameter("onDeactivateTopics", findString(onDeactivateTopics));
101         
102         if (onOverTopics != null)
103             addParameter("onOverTopics", findString(onOverTopics));
104         
105         if (onOutTopics != null)
106             addParameter("onOutTopics", findString(onOutTopics));
107         
108         if (onDropTopics != null)
109             addParameter("onDropTopics", findString(onDropTopics));
110     }
111 
112     @StrutsTagAttribute(description = "符合该选择器的拖拽元素可以被接受,如: #myid 或 .myclass,详见 http://api.jqueryui.com/droppable/#option-accept", defaultValue="*")
113     public void setAccept(String accept) {
114         this.accept = accept;
115     }
116 
117     @StrutsTagAttribute(description = "如果指定了该参数,在被允许的 Draggable 对象填充时,droppable 会被添加上指定的样式,详见 http://api.jqueryui.com/droppable/#option-activeClass")
118     public void setActiveClass(String activeClass) {
119         this.activeClass = activeClass;
120     }
121 
122     @StrutsTagAttribute(description = "如果设置为false, 可以防止 ui-droppable 类在进行时添加. 这可能会使在初始化数百个元素执行.droppable()时使性能得到理想的优化。详见 http://api.jqueryui.com/droppable/#option-addClasses", type="Boolean", defaultValue = "true")
123     public void setAddClasses(String addClasses) {
124         this.addClasses = addClasses;
125     }
126 
127     @StrutsTagAttribute(description = "如果设置为 true将禁止拖放,详见 http://api.jqueryui.com/droppable/#option-disabled", type="Boolean", defaultValue = "false")
128     public void setDisabled(String disabled) {
129         this.disabled = disabled;
130     }
131 
132     @StrutsTagAttribute(description = "默认情况下,当一个元素被放到嵌套的放置(droppable)对象时,每个放置(droppable)对象都将接收到这个元素。 然而,当设置这个选项为 true 时,任何父级的放置(droppable)对象不会接收元素。详见 http://api.jqueryui.com/droppable/#option-greedy", type="Boolean", defaultValue = "false")
133     public void setGreedy(String greedy) {
134         this.greedy = greedy;
135     }
136 
137     @StrutsTagAttribute(description = "如果设置了该参数,将在一个被允许的拖动元素悬停在放置(droppable)对象上时,向放置(droppable)对象添加一个指定的样式,详见 http://api.jqueryui.com/droppable/#option-hoverClass")
138     public void setHoverClass(String hoverClass) {
139         this.hoverClass = hoverClass;
140     }
141 
142     @StrutsTagAttribute(description = "用来设置拖动(draggle)元素和放置(droppable)对象的集合, 除了droppable中的accept属性指定的元素外, 和放置(droppable)对象相同集合的放置(droppable)对象也被允许添加到放置(droppable)对象中,详见 http://api.jqueryui.com/droppable/#option-scope", defaultValue="default")
143     public void setScope(String scope) {
144         this.scope = scope;
145     }
146 
147     @StrutsTagAttribute(description = "指定使用那种模式来测试一个拖动(draggable)元素 over 一个放置(droppable)对象。可选值:fit、intersect、pointer、 touch,详见 http://api.jqueryui.com/droppable/#option-tolerance", defaultValue="intersect")
148     public void setTolerance(String tolerance) {
149         this.tolerance = tolerance;
150     }
151 
152     @StrutsTagAttribute(description = "这个事件会在任何允许的draggable对象开始拖动时触发。它可以用来在你想让droppable对象在可以被填充的时'亮起来'的时候,详见 http://api.jqueryui.com/droppable/#event-activate")
153     public void setOnActivateTopics(String onActivateTopics) {
154         this.onActivateTopics = onActivateTopics;
155     }
156 
157     @StrutsTagAttribute(description = "此事件会在任何允许的draggable对象停止拖动时触发,详见 http://api.jqueryui.com/droppable/#event-deactivate")
158     public void setOnDeactivateTopics(String onDeactivateTopics) {
159         this.onDeactivateTopics = onDeactivateTopics;
160     }
161 
162     @StrutsTagAttribute(description = "这个事件会在一个允许的拖动(draggable)元素填充进这个放置(droppable)对象时触发(基于tolerance选项)。详见 http://api.jqueryui.com/droppable/#event-drop")
163     public void setOnDropTopics(String onDropTopics) {
164         this.onDropTopics = onDropTopics;
165     }
166 
167     @StrutsTagAttribute(description = "此事件会在一个允许的拖动(draggable)元素离开这个放置(droppable)对象时触发(基于tolerance选项)。详见 http://api.jqueryui.com/droppable/#event-out")
168     public void setOnOutTopics(String onOutTopics) {
169         this.onOutTopics = onOutTopics;
170     }
171 
172     @StrutsTagAttribute(description = "此事件会在一个允许的拖动(draggable)元素经过这个放置(droppable)对象时触发(基于tolerance选项)。详见 http://api.jqueryui.com/droppable/#event-over")
173     public void setOnOverTopics(String onOverTopics) {
174         this.onOverTopics = onOverTopics;
175     }
176 }