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

    private static Template: string = `
        <div>
            <iFrame></iFrame>
        </div>`;

    constructor(parentId: string, id: string, style: number) {
        super(parentId, id, style);
        this.$thisNode = $(IFrame.Template).attr({ id: this.id });
        let frameId = this.id;
        this.$thisNode.find("iframe").attr({ width: "100%", height: "100%" }).on("load", function () {
            // 这个其实用处不大，只有在加载本地文档时才会用得上，先注释掉吧
            // $(this.contentDocument).on("click", function (e) {
            //     let eventValue = $(e.target).attr("browserEvent");
            //     if (eventValue) {
            //         EventHub.dispatchEvent(MessageType.WIDGET, { id: frameId, browserEventValue: eventValue, evtType: AdoreEvent.BROWSER_EVENT });
            //     }
            // });
        });
    }

    public setURL(args: { url: string }) {
        let src = args.url;
        this.$thisNode.find("iframe").attr("src", src);
    }

    public setHighDpi(args: { highDpi: boolean }) {
        let viewPortMeta = $("meta[name=viewport]");
        if (args.highDpi) {
            viewPortMeta.attr("content", "user-scalable=no, initial-scale=0.5, maximum-scale=0.5, minimum-scale=0.5, width=device-width, height=device-height, target-densitydpi=device-dpi");
        } else {
            viewPortMeta.attr("content", "width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no");
        }
    }

    public execute(args: { script: string }) {
        let script: string = args.script;
        let myFrame: any = this.$thisNode.find("iframe")[0];
        if (myFrame.contentDocument) {
            let myDocument: any = myFrame.contentDocument;
            //使用myDocument全局替换document
            script = script.replace(/document/g, "myDocument");
            eval(script);
        } else {
            console.error("iframe.contentDocument not found!");
        }
    }

}