/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * 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.
 */
/**
 * Created by huang.dy on 2016/6/28.
 */
export class AdoreLoading {

    private static Template = `
    <div class="adore-loading">
        <div class="preloader-wrapper small active">
            <div class="spinner-layer">
                <div class="circle-clipper left">
                    <div class="circle"></div>
                </div>
                <div class="gap-patch">
                    <div class="circle"></div>
                    </div>
                    <div class="circle-clipper right">
                    <div class="circle"></div>
                </div>
            </div>
        </div>
    </div>`;

    public static show(args: { parentId: string }) {
        let parentId = args.parentId;
        let parentNode = $("#" + parentId);
        if (parentNode.length === 0) {
            return;
        }
        // 只需找到它的直接子元素即可，否则会影响非直接子元素的蒙板
        if (parentNode.children("div.adore-loading").length !== 0) {
            return;
        }
        let $templateNode = $(AdoreLoading.Template);
        $templateNode.attr("id", "AdoreLoading_" + parentId);
        $templateNode.appendTo(parentNode);
        $templateNode.height(parentNode[0].scrollHeight);
        //设置loading图标的位置
        let p = this.getPosition(parentNode[0]);
        let top = (parentNode.height() / 2 + p[0]).toFixed(0) + "px";
        let left = (parentNode.width() / 2 + p[1]).toFixed(0) + "px";
        $templateNode.find(".preloader-wrapper").css({ top: top, left: left });
        $("#AdoreLoading_" + parentId).on("click keyup keydown mouseup mousedown", function (e) {
            e.stopPropagation(); //阻止事件传递给其他节点
        });
    }

    public static destroy(attrs: { parentId: string }) {
        $("#" + attrs.parentId).children("div.adore-loading").remove();
    }

    private static getPosition(obj: HTMLElement) {
        let pos = [0, 0];
        if (obj && obj.offsetParent) {
            while (obj && obj.offsetParent) {
                pos[0] += obj.offsetTop;
                pos[1] += obj.offsetLeft;
                obj = obj.offsetParent as HTMLElement;
            }
        }
        return pos;
    }

}
