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.lightswitch;
23  
24  import java.util.Collection;
25  import java.util.Map;
26  
27  import javax.faces.application.ResourceDependency;
28  import javax.faces.component.UIComponentBase;
29  import javax.faces.component.behavior.ClientBehaviorHolder;
30  import javax.faces.context.FacesContext;
31  import javax.faces.event.AjaxBehaviorEvent;
32  import javax.faces.event.FacesEvent;
33  
34  import org.primefaces.component.api.MixedClientBehaviorHolder;
35  import org.primefaces.component.api.Widget;
36  import org.primefaces.event.SelectEvent;
37  import org.primefaces.util.Constants;
38  import org.primefaces.util.LangUtils;
39  
40  /**
41   * <code>LightSwitch</code> component. Automatically switches to defined dark mode theme based on OS settings.
42   *
43   * @author Jasper de Vries &lt;jepsar@gmail.com&gt;
44   * @since 10.0
45   */
46  @ResourceDependency(library = "primefaces", name = "jquery/jquery.js")
47  @ResourceDependency(library = "primefaces", name = "jquery/jquery-plugins.js")
48  @ResourceDependency(library = "primefaces", name = "core.js")
49  @ResourceDependency(library = "primefaces-extensions", name = "primefaces-extensions.js")
50  @ResourceDependency(library = "primefaces-extensions", name = "lightswitch/lightswitch.js")
51  public class LightSwitch extends UIComponentBase implements Widget, ClientBehaviorHolder, MixedClientBehaviorHolder {
52  
53      public static final String COMPONENT_TYPE = "org.primefaces.extensions.component.LightSwitch";
54      public static final String COMPONENT_FAMILY = "org.primefaces.extensions.component";
55      public static final String EVENT_SWITCH = "switch";
56  
57      private static final String DEFAULT_RENDERER = "org.primefaces.extensions.component.LightSwitchRenderer";
58      private static final Collection<String> EVENT_NAMES = LangUtils.unmodifiableList(EVENT_SWITCH);
59  
60      // @formatter:off
61      @SuppressWarnings("java:S115")
62      public enum PropertyKeys {
63          widgetVar,
64          selected,
65          light,
66          dark,
67          automatic
68      }
69      // @formatter:on
70  
71      /**
72       * Default constructor
73       */
74      public LightSwitch() {
75          setRendererType(DEFAULT_RENDERER);
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public String getFamily() {
83          return COMPONENT_FAMILY;
84      }
85  
86      @Override
87      public String getDefaultEventName() {
88          return EVENT_SWITCH;
89      }
90  
91      public String getWidgetVar() {
92          return (String) getStateHelper().eval(PropertyKeys.widgetVar, null);
93      }
94  
95      public void setWidgetVar(final String widgetVar) {
96          getStateHelper().put(PropertyKeys.widgetVar, widgetVar);
97      }
98  
99      public String getSelected() {
100         return (String) getStateHelper().eval(PropertyKeys.selected, null);
101     }
102 
103     public void setSelected(final String selected) {
104         getStateHelper().put(PropertyKeys.selected, selected);
105     }
106 
107     public void setSelectedByValueExpression(final FacesContext context, final String selected) {
108         this.getValueExpression(PropertyKeys.selected.name()).setValue(context.getELContext(), selected);
109     }
110 
111     public String getLight() {
112         return (String) getStateHelper().eval(PropertyKeys.light, "saga");
113     }
114 
115     public void setLight(final String light) {
116         getStateHelper().put(PropertyKeys.light, light);
117     }
118 
119     public String getDark() {
120         return (String) getStateHelper().eval(PropertyKeys.dark, "arya");
121     }
122 
123     public void setDark(final String dark) {
124         getStateHelper().put(PropertyKeys.dark, dark);
125     }
126 
127     public boolean isAutomatic() {
128         return (boolean) getStateHelper().eval(PropertyKeys.automatic, true);
129     }
130 
131     public void setAutomatic(final boolean automatic) {
132         getStateHelper().put(PropertyKeys.automatic, automatic);
133     }
134 
135     @Override
136     public Collection<String> getEventNames() {
137         return EVENT_NAMES;
138     }
139 
140     @Override
141     public Collection<String> getUnobstrusiveEventNames() {
142         return getEventNames();
143     }
144 
145     @Override
146     public void queueEvent(final FacesEvent event) {
147         final FacesContext context = getFacesContext();
148         final Map<String, String> params = context.getExternalContext().getRequestParameterMap();
149         final String eventName = params.get(Constants.RequestParams.PARTIAL_BEHAVIOR_EVENT_PARAM);
150 
151         if (eventName != null && event instanceof AjaxBehaviorEvent) {
152             final AjaxBehaviorEvent ajaxBehaviorEvent = (AjaxBehaviorEvent) event;
153 
154             if (EVENT_SWITCH.equals(eventName)) {
155                 final String theme = params.get(getClientId(context) + "_theme");
156                 final SelectEvent<String> selectEvent = new SelectEvent<>(this, ajaxBehaviorEvent.getBehavior(), theme);
157                 selectEvent.setPhaseId(ajaxBehaviorEvent.getPhaseId());
158                 setSelectedByValueExpression(context, theme);
159                 super.queueEvent(selectEvent);
160             }
161         }
162     }
163 
164 }