View Javadoc
1   /*
2    * Copyright (c) 2011-2024 PrimeFaces Extensions
3    *
4    *  Permission is hereby granted, free of charge, to any person obtaining a copy
5    *  of this software and associated documentation files (the "Software"), to deal
6    *  in the Software without restriction, including without limitation the rights
7    *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    *  copies of the Software, and to permit persons to whom the Software is
9    *  furnished to do so, subject to the following conditions:
10   *
11   *  The above copyright notice and this permission notice shall be included in
12   *  all copies or substantial portions of the Software.
13   *
14   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   *  THE SOFTWARE.
21   */
22  package org.primefaces.extensions.component.echarts;
23  
24  import java.util.Collection;
25  import java.util.Map;
26  
27  import javax.faces.application.ResourceDependency;
28  import javax.faces.context.ExternalContext;
29  import javax.faces.context.FacesContext;
30  import javax.faces.event.AjaxBehaviorEvent;
31  import javax.faces.event.BehaviorEvent;
32  import javax.faces.event.FacesEvent;
33  
34  import org.primefaces.event.ItemSelectEvent;
35  import org.primefaces.extensions.event.EChartEvent;
36  import org.primefaces.util.Constants;
37  import org.primefaces.util.MapBuilder;
38  
39  @ResourceDependency(library = "primefaces", name = "jquery/jquery.js")
40  @ResourceDependency(library = "primefaces", name = "jquery/jquery-plugins.js")
41  @ResourceDependency(library = "primefaces", name = "core.js")
42  @ResourceDependency(library = "primefaces", name = "components.js")
43  @ResourceDependency(library = "primefaces-extensions", name = "echarts/echarts.js")
44  public class EChart extends EChartBase {
45      private static final String DEFAULT_EVENT = "itemSelect";
46  
47      private static final Map<String, Class<? extends BehaviorEvent>> BEHAVIOR_EVENT_MAPPING = MapBuilder.<String, Class<? extends BehaviorEvent>> builder()
48                  .put("itemSelect", ItemSelectEvent.class)
49                  .build();
50  
51      private static final Collection<String> EVENT_NAMES = BEHAVIOR_EVENT_MAPPING.keySet();
52  
53      @Override
54      public Map<String, Class<? extends BehaviorEvent>> getBehaviorEventMapping() {
55          return BEHAVIOR_EVENT_MAPPING;
56      }
57  
58      @Override
59      public Collection<String> getEventNames() {
60          return EVENT_NAMES;
61      }
62  
63      @Override
64      public String getDefaultEventName() {
65          return DEFAULT_EVENT;
66      }
67  
68      @Override
69      public void processDecodes(final FacesContext fc) {
70          if (isSelfRequest(fc)) {
71              decode(fc);
72          }
73          else {
74              super.processDecodes(fc);
75          }
76      }
77  
78      @Override
79      public void processValidators(final FacesContext fc) {
80          if (!isSelfRequest(fc)) {
81              super.processValidators(fc);
82          }
83      }
84  
85      @Override
86      public void processUpdates(final FacesContext fc) {
87          if (!isSelfRequest(fc)) {
88              super.processUpdates(fc);
89          }
90      }
91  
92      private boolean isSelfRequest(FacesContext facesContext) {
93          String clientId = getClientId(facesContext);
94          ExternalContext externalContext = facesContext.getExternalContext();
95          Map<String, String> requestParameterMap = externalContext.getRequestParameterMap();
96          String partialSourceParam = requestParameterMap.get(Constants.RequestParams.PARTIAL_SOURCE_PARAM);
97          return clientId.equals(partialSourceParam);
98      }
99  
100     @Override
101     public void queueEvent(FacesEvent event) {
102         if (isSelfRequest(event.getFacesContext()) && event instanceof AjaxBehaviorEvent) {
103             BehaviorEvent behaviorEvent = (AjaxBehaviorEvent) event;
104             Map<String, String> map = getFacesContext().getExternalContext().getRequestParameterMap();
105             String name = map.get("name");
106             String componentType = map.get("componentType");
107             String componentSubType = map.get("componentSubType");
108             String seriesType = map.get("seriesType");
109             String seriesName = map.get("seriesName");
110             String data = map.get("value");
111             int componentIndex = Integer.parseInt(map.get("componentIndex"));
112             int dataIndex = Integer.parseInt(map.get("dataIndex"));
113             int seriesIndex = Integer.parseInt(map.get("seriesIndex"));
114 
115             EChartEvent eChartEvent = new EChartEvent(this, behaviorEvent.getBehavior(), name, dataIndex, seriesIndex, componentType, componentSubType,
116                         seriesType, seriesName, data, componentIndex);
117 
118             super.queueEvent(eChartEvent);
119         }
120         else {
121             super.queueEvent(event);
122         }
123     }
124 }