我的menu

function Menu() {
    this.tbarH = 28;
    this.bbarH = 28;
    this.barH = 25;
    this.container;
    if (!arguments[0]) {
        alert("\u8bf7\u5728\u521b\u5efa\u83dc\u5355\u65f6\u6307\u5b9a\u83dc\u5355\u7684\u5bb9\u5668");
        return;
    }
    if (typeof (arguments[0]) == "string") {
        this.container = document.getElementById(arguments[0]);
    } else {
        this.container = arguments[0];
    }
    var tbar = document.createElement("div");
    tbar.style.cssText = "border:1px solid #eee;background:#3586BD url(images/bar.png) repeat-x;color:#fff;line-height:2;height:" + this.tbarH + "px";
    tbar.innerHTML = "\u7cfb\u7edf\u83dc\u5355";
    var bbar = document.createElement("div");
    bbar.style.cssText = "border:1px solid #eee;background:#3586BD url(images/bar.png) repeat-x;height:" + this.bbarH + "px";
    var c = document.createElement("div");
    c.style.height = this.container.offsetHeight ? this.container.offsetHeight : "100%";
    c.style.border = "1px solid #999";
    c.appendChild(tbar);
    var body = document.createElement("div");
    body.style.borderTop = "1px solid #999";
    body.style.borderBottom = "1px solid #999";
    body.style.backgroundColor = "lightyellow";
    body.style.height = parseInt(this.container.offsetHeight) - this.tbarH - this.bbarH - 6 + "px";
    c.appendChild(body);
    c.appendChild(bbar);
    this.container.appendChild(c);
    this.body = body;
    this.ibids = [];
}
Menu.prototype.add = function (id, pid, name, href, icon) {
    if (this.container) {
        if (pid == 0) {
            var item = document.createElement("div");
            item.setAttribute("id", "item" + id);
            var itemhead = document.createElement("div");
            itemhead.setAttribute("id", "itemhead" + id);
            itemhead.style.cssText = "border-style:solid;border-color:#eee;border-width:0 1px 1px 1px;background:#3586BD url(images/bar.png) repeat-x;line-height:25px;text-align:center;color:#fff;height:" + this.barH + "px";
            itemhead.innerHTML = name;
            var ibids = this.ibids;
            var bd = this.body;
            var bh = this.barH;
            itemhead.onclick = function (e) {
                var el = e ? e.target : event.srcElement;
                var temp = el.id.substring(8);
                for (var k = 0; k < ibids.length; k++) {
                    if (ibids[k] == temp) {
                        var ib = document.getElementById("itembody" + ibids[k]);
                        ib.style.display = "block";
                        ib.style.height = parseInt(bd.style.height) - ibids.length * (bh + 1);
                    } else {
                        document.getElementById("itembody" + ibids[k]).style.display = "none";
                    }
                }
            };
            item.appendChild(itemhead);
            var itembody = document.createElement("div");
            itembody.setAttribute("id", "itembody" + id);
            itembody.style.cssText = "border-style:solid;border-color:#eee;border-width:0 1px 1px 1px;text-align:center;display:none";
            item.appendChild(itembody);
            this.ibids.push(id);
            this.body.appendChild(item);
        } else {
            var item = document.createElement("div");
            item.setAttribute("id", "item" + id);
            item.setAttribute("pid", pid);
            item.setAttribute("text", name);
            item.setAttribute("href", href);
            item.style.cssText = "border-style:solid;border-color:#999;border-width:0;text-align:center;line-height:25px;cursor:pointer;";
            item.innerHTML = name;
            var menu = this;
            item.onclick = function (e) {
                var el = e ? e.target : event.srcElement;
                if(menu.callback){
                    menu.callback(el);
                }
            };
            var ib = document.getElementById("itembody" + pid);
            ib.appendChild(item);
        }
    }
};
Menu.prototype.init = function (fn) {
    var index = 0;
    var firstib = document.getElementById("itembody" + this.ibids[index]);
    firstib.style.display = "block";
    firstib.style.height = parseInt(this.body.style.height) - this.ibids.length * (this.barH + 1);
    this.callback = fn;
};
Menu.prototype.load = function (url, params, fn) {
    var menu = this;
    function kk(res) {
        var ret = eval("(" + res.responseText + ")");
        for (var k in ret) {
            menu.add(ret[k].id, ret[k].pid, ret[k].name, ret[k].href, ret[k].icon);
        }
        var index = 0;
        var firstib = document.getElementById("itembody" + menu.ibids[index]);
        firstib.style.display = "block";
        firstib.style.height = parseInt(menu.body.style.height) - menu.ibids.length * (menu.barH + 1);
        menu.callback = fn;
    };
    Ajax(url, params, kk);
};
var Ajax = function (url, params, callback) {
    var reqError = "\u54cd\u5e94\u5f02\u5e38\uff01\uff01\uff01";
    var sendData = null;
    var createXHR = function () {
        var XHR;
        if (window.XMLHttpRequest) {
            XHR = new XMLHttpRequest();
        } else {
            if (window.ActiveXObject) {
                try {
                    XHR = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch (e) {
                    try {
                        XHR = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch (e) {
                    }
                }
            }
        }
        return XHR;
    };
    var processParams = function () {
        var ret = "";
        for (var p in params) {
            ret += "&";
            ret += p + "=" + params[p];
        }
        ret = ret.substring(1);
        return ret;
    };
    var method = (url.indexOf("?") == -1) ? "POST" : "GET";
    if (params && typeof (params) == "object") {
        if (typeof (params) == "object") {
            if (method == "GET") {
                url += "&" + processParams();
            } else {
                sendData = processParams();
            }
        }
        if (typeof (params) == "string") {
            if (method == "GET") {
                url += "&" + params;
            } else {
                sendData = params;
            }
        }
    }
    var xhr = createXHR();
    xhr.open(method, url, true);
    if (method == "POST") {
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    }
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                if (callback) {
                    callback(xhr);
                }
            } else {
                window.alert(reqError);
            }
        }
    };
    xhr.send(sendData);
};

 

你可能感兴趣的:(menu)