﻿
/* ---------------------------- Class KmwDialog ------------------------- */

/*
 *  [Summary]
 *      页面对话框
 *  [Parameters]
 *      vdid    - (required)对话框ID，在一个页面中唯一
 */
function KmwDialog(vdid) {

    this.id = vdid;         // 对话框ID
    this.opener = null;
    this.disabled = false;

    this.width = 320;       // 对话框宽度
    this.height = 240;      // 对话框高度
    this.left = 0;          // 对话框左坐标
    this.top = 0;           // 对话框上坐标

    this.title = "";        // 对话框标题
    this.body_html = "";    // 对话框主体
    this.foot_html = "";    // 对话框脚

    
    /*
     *  [Summary]
     *      创建对话框
     */
    this.create = function() {
        var dialog = document.getElementById(this.id);

        if (dialog == null) {
            dialog = document.createElement("div");
            dialog.setAttribute("id", this.id);
            dialog.style.visibility = "hidden";
            dialog.className = "dialog";

            dialog.innerHTML = 
            "<div class='head'>" +
                "<span class='title' id='"+this.id+"_title'>x</span>" +
                "<span class='closebox' onclick='close_dialog(\""+this.id+"\");'>&nbsp;</span>&nbsp;" +
            "</div>" +
            "<div class='body' id='"+this.id+"_body'>&nbsp;</div>" + 
            "<div class='foot' id='"+this.id+"_foot'>&nbsp;</div>";

            document.body.appendChild(dialog);
            
            if (typeof(this.onCreate) == "function") {
                this.onCreate();
            }
        }
    }
    
    /*
     *  [Summary]
     *      显示对话框
     *  [Parameters]
     *      center    - (optional)是否居中显示
     *  [Exception]
     *      对话框实体不存在
     */
    this.show = function(vcenter) {
        var dialog = document.getElementById(this.id);
        if (!dialog) {
            throw "Error: dialog(" + this.id + ") has been destroyed.";
        }
        
        if (typeof(this.onShow) == "function") {
            this.onShow();
        }

        dialog.style.width = this.width + "px";
        dialog.style.height = this.height + "px";
        
        if (vcenter) {
            dialog.style.left = (document.body.clientWidth - this.width) / 2;
            dialog.style.top = (document.body.clientHeight - this.height) / 2;
        } else {
            dialog.style.left = this.left;
            dialog.style.top = this.top;
        }
        
        var obj = document.getElementById(this.id + "_title");
        obj.innerHTML = this.title;
        
        obj = document.getElementById(this.id + "_body");
        obj.innerHTML = this.body_html;
        
        obj = document.getElementById(this.id + "_foot");
        obj.innerHTML = this.foot_html;
        
        dialog.style.visibility = "visible";
    }
    
    /*
     *  [Summary]
     *      隐藏对话框
     *  [Exception]
     *      对话框实体不存在
     */
    this.hide = function() {
        var dialog = document.getElementById(this.id);
        if (!dialog) {
            throw "Error: dialog(" + this.id + ") has been destroyed.";
        }
        
        dialog.style.visibility = "hidden";

        if (typeof(this.onHide) == "function") {
            this.onHide();
        }
    }
    
    /*
     *  [Summary]
     *      关闭并销毁对话框
     */
    this.close = function() {
        if (this.disabled) {
            return;
        }
        var dialog = document.getElementById(this.id);
        if (dialog) {
            if (typeof(this.onClose) == "function") {
                this.onClose();
            }
            dialog.style.visibility = "hidden";
            document.body.removeChild(dialog);
        }
        delete KmwDialog.__items[this.id];
    }
    
    /*
     *  [Summary]
     *      触发对话框事件
     *  [Parameters]
     *      veid    - (required)事件ID
     */
    this.event = function(veid) {
        if (this.disabled) {
            return;
        }
        var event_handler = eval("this.on" + veid);
        if (typeof(event_handler) == "function") {
            var argc = arguments.length;
            var args = new Array();
            if (argc > 1) {
                for (var i = 1; i < argc; i ++) {
                    args[i - 1] = arguments[i];
                }
            }
            event_handler.apply(this, args);
        }
    }
    
    this.disable = function(vFlag) {
        this.disabled = (vFlag) ? true : false;
        var dialog = document.getElementById(this.id);
        if (dialog) {
            dialog.disabled = this.disabled;
        }
    }

    /*
     *  [Summary]
     *      获得对话框摘要
     *  [Returns]
     *      对话框摘要字符串
     */
    this.toString = function() {
        var msg = "Dialog(id=" + this.id + ") {title=" + this.title + "}";
        return msg;
    }
}

/*
 *  [Summary]
 *      所有对话框的集合
 *  [Remarks]
 *      由于无法从<div>元素反向获得其KmwDialog对象，所以利用此静态成员
 *      保存所有已被创建的KmwDialog对象。通过对话框ID可以迅速找到其控制
 *      对象，从而进一步地进行各种操作。
 */
KmwDialog.__items = new Object();


/*
 *  [Summary]
 *      打开对话框
 *  [Parameters]
 *      vdid    - (required)对话框ID
 *  [Returns]
 *      对话框的控制对象
 *  [Remarks]
 *      首先检查此对话框是否已经被创建，如果尚未被创建则立即创建之；
 *      然后返回对话框的控制对象。
 */
function open_dialog(vdid, vclass, vopener) {
    if (typeof(vclass) == "string") {
        var dialog = KmwDialog.__items[vdid];
        if (!dialog) {
            dialog = eval("new " + vclass + "('" + vdid + "')");
            dialog.create();
            KmwDialog.__items[vdid] = dialog;
        }
    } else {
        var dialog = KmwDialog.__items[vdid];
        if (!dialog) {
            dialog = new KmwDialog(vdid);
            dialog.create();
            KmwDialog.__items[vdid] = dialog;
        }
    }
    if (vopener) {
        dialog.opener = vopener;
    }
    return dialog;
}

/*
 *  [Summary]
 *      关闭并销毁对话框及其控制对象
 *  [Parameters]
 *      vdid - (optional)对话框ID。当指定此参数时，将关闭指定的对话框；
 *              当此参数未指定时，将关闭所有的对话框。
 */
function close_dialog(vdid) {
    if (typeof(vdid) == "undefined") {
        for (vdid in KmwDialog.__items) {
            var dialog = KmwDialog.__items[vdid];
            dialog.close();
        }
        KmwDialog.__items = new Object();
    } else {
        var dialog = KmwDialog.__items[vdid];
        if (dialog) {
            dialog.close();
        }
    }
}

/*
 *  [Summary]
 *      触发对话框事件
 *  [Parameters]
 *      vdid	- (required)对话框ID。
 *		veid	- (required)事件ID
 */
function event_dialog(vdid, veid) {
    var dialog = KmwDialog.__items[vdid];
    if (dialog) {
        var argc = arguments.length;
        var args = new Array();
        if (argc > 1) {
            for (var i = 1; i < argc; i ++) {
                args[i-1] = arguments[i];
            }
        }
        dialog.event.apply(dialog, args);
    }
}


/* ---------------------------- Class MessageDialog ------------------------- */

/*
 *  [Summary]
 *      消息提示框
 *  [Parameters]
 *      vdid    - (required)对话框ID，在一个页面中唯一
 */
/*
function MessageBox(vdid) {
    // 从KmwDialog派生
    KmwDialog.apply(this, new Array(vdid));
    
    this.message = "";
    this.mode = "";
}

MessageBox.BODY_HTML = "<table width='100%' cellpadding='0' cellspacing='1'>"
        + "<tr><td valign='top' align='center'>{MESSAGE}</td>"
        + "</tr></table>";

MessageBox.FOOT_HTML = "<table width='100%'>"
        + "<tr><td align='right'>"
        +     "<input type='button' class='button' value='关闭' onclick='close_dialog(\"{DLG_ID}\");' />&nbsp;"
        +  "</td></tr></table>";

MessageBox.prototype.onShow = function() {
    var htmlstr = MessageDialog.BODY_HTML;
    htmlstr = htmlstr.replace(/{MESSAGE}/g, this.message);
    this.body_html = htmlstr;
    
    htmlstr = MessageDialog.FOOT_HTML;
    htmlstr = htmlstr.replace(/{DLG_ID}/g, this.id);
    this.foot_html = htmlstr;

    if (this.opener) {
        this.opener.disable(true);
    }
}

MessageBox.prototype.onClose = function() {
    if (this.opener) {
        this.opener.disable(false);
    }
}

MessageBox.show = function(vopener, vmessage, vmode) {
    
}
*/