/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * Special Declaration: These technical material reserved as the technical secrets by AGREE 
 * TECHNOLOGY have been protected by the "Copyright Law" "ordinances on Protection of Computer 
 * Software" and other relevant administrative regulations and international treaties. Without 
 * the written permission of the Company, no person may use (including but not limited to the 
 * illegal copy, distribute, display, image, upload, and download) and disclose the above 
 * technical documents to any third party. Otherwise, any infringer shall afford the legal 
 * liability to the company.
 */
import {WidgetManager} from "util/WidgetManager";
import {Widget} from "widget/Widget";
/**
 * Created by huang.dy on 2016/5/29.
 */
export class WidgetUtil {
    //刷新并设置z-index，修正存在多个对话框或者多个AdoreShell时对话框被遮挡的问题
    public static refreshDialogZIndex(htmlElement: HTMLElement) {
        let thisZIndex: number =  parseInt(htmlElement.style.zIndex);
        let maxZIndex: number = -1;
        let dialogNodes: JQuery = $("div.adore_dialog");
        let i: number, tempZIndex: number;
        for (i = 0; i < dialogNodes.length; i++) {
            if (dialogNodes[i].id === htmlElement.id) {
                continue;
            }
            tempZIndex = parseInt(dialogNodes[i].style.zIndex);
            maxZIndex = tempZIndex > maxZIndex ? tempZIndex : maxZIndex;
        }
        let adoreShellNodes: JQuery = $("div.adore_shell");
        for (i = 0; i < adoreShellNodes.length; i++) {
            if (adoreShellNodes[i].id === htmlElement.id) {
                continue;
            }
            tempZIndex = parseInt(adoreShellNodes[i].style.zIndex);
            maxZIndex = tempZIndex > maxZIndex ? tempZIndex : maxZIndex;
        }
        if (maxZIndex >= thisZIndex) {
            $(htmlElement).css("z-index", maxZIndex + 1);
        }
    }

    public static findWidgetByTarget(target: HTMLElement): Widget {
        if (!target ) {
            return null;
        }
        let widget = WidgetManager.byId(target.id);
        if (widget) {
            return widget;
        }else {
            return WidgetUtil.findWidgetByTarget(target.parentNode as HTMLElement);
        }
    }

    public static elementExists(id: string): boolean {
        if (id) {
            let $node = $("#" + id);
            if ($node.length !== 0 ) {
                return true;
            }
        }
        return false;
    }
}