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.impl;
029
030import org.opencms.gwt.client.util.CmsDomUtil;
031
032import com.google.gwt.dom.client.Element;
033
034/**
035 * Helper class to retrieve the computed style of an element.<p>
036 *
037 * This implementation is used for all none MSIE browsers.<p>
038 *
039 * @since 8.0.0
040 */
041public class DocumentStyleImpl {
042
043    /**
044     * Transforms a CSS property name to its javascript property name (font-size >> fontSize).<p>
045     *
046     * @param s the property name
047     *
048     * @return the javascript property name
049     */
050    protected static native String camelize(String s)/*-{
051                                                     return s.replace(/\-(\w)/g, function(all, letter) {
052                                                     return letter.toUpperCase();
053                                                     });
054                                                     }-*/;
055
056    /**
057     * Removes the opacity attribute from the element's inline-style.<p>
058     *
059     * @param element the DOM element to manipulate
060     */
061    public native void clearOpacity(Element element) /*-{
062                                                     element.style.removeProperty("opacity");
063                                                     }-*/;
064
065    /**
066     * Returns the computed style of the given element.<p>
067     *
068     * @param elem the element
069     * @param name the name of the CSS property
070     *
071     * @return the currently computed style
072     */
073    public String getCurrentStyle(Element elem, String name) {
074
075        name = hyphenize(name);
076        String propVal = getComputedStyle(elem, name);
077        if (CmsDomUtil.Style.opacity.name().equals(name) && ((propVal == null) || (propVal.trim().length() == 0))) {
078            propVal = "1";
079        }
080        return propVal;
081    }
082
083    /**
084     * Transforms the CSS style name to the name of the javascript style property.<p>
085     *
086     * @param name the name of the CSS property
087     *
088     * @return the javascript property name
089     */
090    public String getPropertyName(String name) {
091
092        if ("float".equals(name)) {
093            return "cssFloat";
094        } else if ("class".equals(name)) {
095            return "className";
096        } else if ("for".equals(name)) {
097            return "htmlFor";
098        }
099        return camelize(name);
100    }
101
102    /**
103     * Hyphenizes the given string.<p>
104     *
105     * @param name the string to hyphenize
106     *
107     * @return the result
108     */
109    protected native String hyphenize(String name) /*-{
110                                                   return name.replace(/([A-Z])/g, "-$1").toLowerCase();
111                                                   }-*/;
112
113    /**
114     * Returns the computed style from the DOM object.<p>
115     *
116     * @param elem the element object
117     * @param name name of the CSS property
118     *
119     * @return the property value
120     */
121    protected native String getComputedStyle(Element elem, String name) /*-{
122                                                                        var cStyle = $doc.defaultView.getComputedStyle(elem, null);
123                                                                        if (cStyle == null) {
124                                                                        return null;
125                                                                        }
126                                                                        var value = cStyle.getPropertyValue(name);
127                                                                        if (value == "auto" && (name == "width" || name == "height")) {
128                                                                        var which = name === "width" ? [ "Left", "Right" ] : [ "Top",
129                                                                        "Bottom" ];
130                                                                        function getWH() {
131                                                                        var val;
132                                                                        val = name === "width" ? elem.offsetWidth : elem.offsetHeight;
133                                                                        for ( var i = 0; i < which.length; i++) {
134                                                                        val -= parseFloat(getComputedStyle(elem, "padding"
135                                                                        + which[i])) || 0;
136                                                                        val -= parseFloat(getComputedStyle(elem, "border"
137                                                                        + which[i] + "Width")) || 0;
138
139                                                                        }
140                                                                        return Math.max(0, Math.round(val));
141                                                                        }
142                                                                        value = getWH() + "px";
143                                                                        }
144
145                                                                        return value;
146                                                                        }-*/;
147}