001package gwt.material.design.client.base.helper;
002
003/*
004 * #%L
005 * GwtMaterial
006 * %%
007 * Copyright (C) 2015 - 2017 GwtMaterialDesign
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023
024import com.google.gwt.dom.client.Element;
025import com.google.gwt.dom.client.Node;
026import com.google.gwt.dom.client.NodeList;
027import com.google.gwt.user.client.ui.HasWidgets;
028import com.google.gwt.user.client.ui.RootPanel;
029import com.google.gwt.user.client.ui.Widget;
030
031// TODO: Replace with jQuery JSInterop
032public class DOMHelper {
033
034    public static Element getChildElementByClass(Element parent, String className) {
035        if (parent != null) {
036            for (int i = 0; i < parent.getChildCount(); i++) {
037                Node childNode = parent.getChild(i);
038                if (Element.is(childNode)) {
039                    Element child = Element.as(childNode);
040                    if (child.getClassName().contains(className)) {
041                        return child;
042                    }
043
044                    if (child.getChildCount() > 0) {
045                        return getChildElementByClass(child, className);
046                    }
047                }
048            }
049        }
050        return null;
051    }
052
053    public static Element getChildElementById(Element parent, String id) {
054        if (parent != null) {
055            for (int i = 0; i < parent.getChildCount(); i++) {
056                Node childNode = parent.getChild(i);
057                if (Element.is(childNode)) {
058                    Element child = Element.as(childNode);
059                    if (child.getId().equals(id)) {
060                        return child;
061                    }
062
063                    if (child.getChildCount() > 0) {
064                        return getChildElementById(child, id);
065                    }
066                }
067            }
068        }
069        return null;
070    }
071
072    public static Widget getChildWidgetById(HasWidgets parent, String id) {
073        if (parent != null) {
074            for (Widget child : parent) {
075                if (child.getElement().getId().equals(id)) {
076                    return child;
077                }
078            }
079        }
080        return null;
081    }
082
083    public static Element getElementByAttribute(String attr, String value) {
084        return getElementByAttribute(RootPanel.getBodyElement().getElementsByTagName("*"), attr, value);
085    }
086
087    public static Element getElementByAttribute(NodeList<Element> elems, String attr, String value) {
088        if (elems != null) {
089            for (int i = 0; i < elems.getLength(); i++) {
090                Element child = elems.getItem(i);
091                if (child.getAttribute(attr).equals(value)) {
092                    return child;
093                }
094            }
095        }
096        return null;
097    }
098}