001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.gwt.client.util;
029
030import java.util.function.Consumer;
031
032import com.google.gwt.core.client.JavaScriptObject;
033
034/**
035 * GWT wrapper for  programmatic media query checking.
036 *
037 *<p>An instance of this class can be created from the media query rule text usign the parse() method.
038 * Checking if the media query applies can be either done using the matches() method, or by adding a listener
039 * with the addListener method if you need to keep track of changes.
040 */
041public final class CmsMediaQuery extends JavaScriptObject {
042
043    /**
044     * Hidden constructor.
045     */
046    protected CmsMediaQuery() {}
047
048    /**
049     * Creates a new instance using the given media query rule.
050     *
051     * @param text the rule text
052     * @return the new media query
053     */
054    public static native CmsMediaQuery parse(String text) /*-{
055        return $wnd.matchMedia(text);
056    }-*/;
057
058    /**
059     * Adds a listener to detect when the matching state of the media query changes.
060     *
061     * @param callback the callback to call with the matching state as a parameter
062     */
063    public final native void addListener(Consumer<Boolean> callback) /*-{
064        var jsCallback = function(state) {
065            var match;
066            if (state.matches) {
067                match = @java.lang.Boolean::TRUE;
068            } else {
069                match = @java.lang.Boolean::FALSE;
070            }
071            callback.@java.util.function.Consumer::accept(Ljava/lang/Object;)(match);
072        };
073
074        try {
075            this.addEventListener('change', jsCallback);
076        } catch (e) {
077            // for older browsers which don't have the addEventListener method
078            this.addListener(jsCallback);
079        }
080    }-*/;
081
082    /**
083     * Checks if the media query matches.
084     *
085     * @return true if the media query matches
086     */
087    public final native boolean matches() /*-{
088        return this.matches;
089    }-*/;
090
091}