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   * 使用鼠标选择一个或一组元素。允许一个元素被鼠标划出的选择区域选中。
19   * 同样,元素也可以被点击选中或者同时按住 Ctrl/Meta 键,允许多个(非连续)的选择。
20   * 详见 <a href="http://api.jqueryui.com/selectable/" target="_blank">jQuery UI Selectable Widget</a>。
21   * 
22   * @author <a href="mailto:[email protected]">何明旺</a>
23   * @since 3.0
24   * @date 2014年5月5日
25   */
26  @StrutsTag(
27          name="selectable",
28          tldTagClass="gboat2.base.view.jsp.SelectableTag",
29          description="使用鼠标选择一个或一组元素,详见 http://www.jqueryui.com/selectable/")
30  public class Selectable extends UIBean{
31      
32      public static final String TEMPLATE = "selectable.ftl";
33  
34      protected String appendTo;
35      protected String autoRefresh;
36      protected String cancel;
37      protected String delay;
38      protected String disabled;
39      protected String distance;
40      protected String filter;
41      protected String tolerance;
42  
43      protected String onSelectedTopics;
44      protected String onSelectingTopics;
45      protected String onStartTopics;
46      protected String onStopTopics;
47      protected String onUnselectedTopics;
48      protected String onUnselectingTopics;
49  
50      public Selectable(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("selectable", 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  
71          if (appendTo != null)
72              options.put("appendTo", findString(appendTo));
73  
74          if (autoRefresh != null)
75              options.put("autoRefresh", (Boolean) findValue(autoRefresh, Boolean.class));
76  
77          if (cancel != null)
78              options.put("cancel", findString(cancel));
79  
80          if (delay != null)
81              options.put("delay", (Integer) findValue(delay, Integer.class));
82  
83          if (disabled != null)
84              options.put("disabled", (Boolean) findValue(disabled, Boolean.class));
85  
86          if (distance != null)
87              options.put("distance", (Integer) findValue(distance, Integer.class));
88  
89          if (filter != null)
90              options.put("filter", findString(filter));
91  
92          if (tolerance != null)
93              options.put("tolerance", findString(tolerance));
94  
95          addParameter("options", options);
96  
97          if (onSelectedTopics != null)
98              addParameter("onSelectedTopics", findString(onSelectedTopics));
99          
100         if (onSelectingTopics != null)
101             addParameter("onSelectingTopics", findString(onSelectingTopics));
102         
103         if (onStartTopics != null)
104             addParameter("onStartTopics", findString(onStartTopics));
105         
106         if (onStopTopics != null)
107             addParameter("onStopTopics", findString(onStopTopics));
108         
109         if (onUnselectedTopics != null)
110             addParameter("onUnselectedTopics", findString(onUnselectedTopics));
111         
112         if (onUnselectingTopics != null)
113             addParameter("onUnselectingTopics", findString(onUnselectingTopics));
114     }
115 
116     @StrutsTagAttribute(description="选择帮手(套索) 应追加到哪个元素,值为 jQuery 选择器,详见 http://api.jqueryui.com/selectable/#option-appendTo", defaultValue="body")
117     public void setAppendTo(String appendTo) {
118         this.appendTo = appendTo;
119     }
120 
121     @StrutsTagAttribute(description="确定每个选择操作开始时如何刷新(重新计算)每个选择项的位置和大小。如果你有很多很多选择项,你应当设置此项为 false,详见 http://api.jqueryui.com/selectable/#option-autoRefresh", type="Boolean", defaultValue="true")
122     public void setAutoRefresh(String autoRefresh) {
123         this.autoRefresh = autoRefresh;
124     }
125 
126     @StrutsTagAttribute(description="如果你使用了匹配选择器,符合匹配的元素将被禁止可选,详见 http://api.jqueryui.com/selectable/#option-cancel", defaultValue="input,textarea,button,select,option")
127     public void setCancel(String cancel) {
128         this.cancel = cancel;
129     }
130 
131     @StrutsTagAttribute(description="定义需要经过多少毫秒后选择才会开始. 这可以预防意外的点击造成元素被选择,详见 http://api.jqueryui.com/selectable/#option-delay", type="Integer", defaultValue="0")
132     public void setDelay(String delay) {
133         this.delay = delay;
134     }
135 
136     @StrutsTagAttribute(description="如果设置为 true 将禁止selectable,详见 http://api.jqueryui.com/selectable/#option-disabled", type="Boolean", defaultValue="false")
137     public void setDisabled(String disabled) {
138         this.disabled = disabled;
139     }
140 
141     @StrutsTagAttribute(description="定义需要移动多少个像素选择才会开始. 如果指定了该项, 选择不会马上开始,而是会在鼠标移动了指定像素的距离之后才会开始,详见 http://api.jqueryui.com/selectable/#option-distance", type="Integer", defaultValue="0")
142     public void setDistance(String distance) {
143         this.distance = distance;
144     }
145 
146     @StrutsTagAttribute(description="匹配子元素中那些符合条件的元素才可以被选择,详见 http://api.jqueryui.com/selectable/#option-filter", defaultValue="*")
147     public void setFilter(String filter) {
148         this.filter = filter;
149     }
150 
151     @StrutsTagAttribute(description="指定那种模式,用来测试套索是否应该选择一个项目。可选值:fit、touch,详见 http://api.jqueryui.com/selectable/#option-tolerance", defaultValue="touch")
152     public void setTolerance(String tolerance) {
153         this.tolerance = tolerance;
154     }
155 
156     @StrutsTagAttribute(description="此事件会在选择操作结束时,在添加到选择的每个元素上触发,详见 http://api.jqueryui.com/selectable/#event-selected")
157     public void setOnSelectedTopics(String onSelectedTopics) {
158         this.onSelectedTopics = onSelectedTopics;
159     }
160 
161     @StrutsTagAttribute(description="此事件会在选择操作过程中,在添加到选择的每个元素上触发,详见 http://api.jqueryui.com/selectable/#event-selecting")
162     public void setOnSelectingTopics(String onSelectingTopics) {
163         this.onSelectingTopics = onSelectingTopics;
164     }
165 
166     @StrutsTagAttribute(description="这个事件将在选择操作开始时触发,详见 http://api.jqueryui.com/selectable/#event-start")
167     public void setOnStartTopics(String onStartTopics) {
168         this.onStartTopics = onStartTopics;
169     }
170 
171     @StrutsTagAttribute(description="这个事件将在选择操作结束后触发,详见 http://api.jqueryui.com/selectable/#event-stop")
172     public void setOnStopTopics(String onStopTopics) {
173         this.onStopTopics = onStopTopics;
174     }
175 
176     @StrutsTagAttribute(description="此事件会在选择操作结束时,在从选择元素集合中移除的每个元素上触发,详见 http://api.jqueryui.com/selectable/#event-unselected")
177     public void setOnUnselectedTopics(String onUnselectedTopics) {
178         this.onUnselectedTopics = onUnselectedTopics;
179     }
180 
181     @StrutsTagAttribute(description="此事件会在选择操作过程中,在从选择元素集合中移除的每个元素上触发,详见 http://api.jqueryui.com/selectable/#event-unselecting")
182     public void setOnUnselectingTopics(String onUnselectingTopics) {
183         this.onUnselectingTopics = onUnselectingTopics;
184     }
185 }