project.js 11.4 KB
jQuery(document).ready(function () {
    getProjects();
});

/**
 * 操作变更
 */
function operateChange() {
    getProjects();
    var operate = $("input[name='operate']:checked").val();
    if ("Rollback" == operate) {
        //隐藏Branch输入框
        document.getElementById("branch-div").style.display = "none";
        //展示 回滚选择框
        document.getElementById("rollback-div").style.display = "";
    } else {
        //展示Branch输入框
        document.getElementById("branch-div").style.display = "";
        //隐藏 回滚选择框
        document.getElementById("rollback-div").style.display = "none";

    }
}

/**
 * 根据操作获取当前的项目列表
 */
function getProjects() {
    var operate = $("input[name='operate']:checked").val();

    $.ajax({
        url: 'getProjects',
        type: 'POST',
        dataType: 'json',
        contentType: "application/json",
        success: function (data) {
            var obj = eval("(" + data + ")");
            if ("Deploy" == operate) {//发布多选框
                var order1HTML = "<div class='ckbox ckbox-primary'>";
                order1HTML += " <input name='project1all' type='checkbox' id='project1all' value='project1all' onclick='projectcheckall(this,\"project1\");'/>";
                order1HTML += "<label for='project1all' style='width: 200px'>yoho30  全选</label></div>";

                var order2HTML = "<div class='ckbox ckbox-warning'>";
                order2HTML += " <input name='project2all' type='checkbox' id='project2all' value='project2all' onclick='projectcheckall(this,\"project2\");'/>";
                order2HTML += "<label for='project2all' style='width: 200px'>yoho-search  全选</label></div>";

                var order3HTML = "<div class='ckbox ckbox-success'>";
                order3HTML += " <input name='project3all' type='checkbox' id='project3all' value='project3all' onclick='projectcheckall(this,\"project3\");'/>";
                order3HTML += "<label for='project3all' style='width: 200px'>platform  全选</label></div>";

                var order1num = 1;
                var order2num = 1;
                var order3num = 1;

                for (var i = 0; i < obj.length; i++) {
                    var order = obj[i].order;
                    var name = obj[i].name;
                    if ("1" == order) {
                        order1HTML += "<div class='ckbox ckbox-primary' style='display: inline'>";
                        order1HTML += " <input name='project1' type='checkbox' id='" + name + "' value='" + name + "'/>";
                        order1HTML += "<label for='" + name + "' style='width: 200px'>" + name + "</label></div>";
                        if (order1num % 4 == 0) {
                            order1HTML += "</br>";
                        }
                        order1num += 1;

                    }
                    if ("2" == order) {
                        order2HTML += "<div class='ckbox ckbox-warning' style='display: inline'>";
                        order2HTML += " <input name='project2' type='checkbox' id='" + name + "' value='" + name + "'/>";
                        order2HTML += "<label for='" + name + "' style='width: 200px'>" + name + "</label></div>";
                        if (order2num % 4 == 0) {
                            order2HTML += "</br>";
                        }
                        order2num += 1;

                    }
                    if ("3" == order) {
                        order3HTML += "<div class='ckbox ckbox-success' style='display: inline'>";
                        order3HTML += " <input name='project3' type='checkbox' id='" + name + "' value='" + name + "'/>";
                        order3HTML += "<label for='" + name + "' style='width: 200px'>" + name + "</label></div>";
                        if (order3num % 4 == 0) {
                            order3HTML += "</br>";
                        }
                        order3num += 1;

                    }

                }
                document.getElementById("project-div").innerHTML = order1HTML + "<hr>" + order2HTML + "<hr>" + order3HTML;
                branchdefault();
            } else {//回滚单选框
                var order1HTML = "";
                var order2HTML = "";
                var order3HTML = "";

                var order1num = 1;
                var order2num = 1;
                var order3num = 1;

                for (var i = 0; i < obj.length; i++) {
                    var order = obj[i].order;
                    var name = obj[i].name;
                    if ("1" == order) {
                        order1HTML += "<div class='rdio rdio-primary' style='display: inline'>";
                        if (name == "yoho-gateway") {
                            order1HTML += " <input name='project' type='radio' id='" + name + "' value='" + name + "' onclick='getRollbackList()' checked='checked'/>";
                        } else {
                            order1HTML += " <input name='project' type='radio' id='" + name + "' value='" + name + "' onclick='getRollbackList()'/>";
                        }
                        order1HTML += "<label for='" + name + "' style='width: 200px'>" + name + "</label></div>";
                        if (order1num % 4 == 0) {
                            order1HTML += "</br>";
                        }
                        order1num += 1;
                    }
                    if ("2" == order) {
                        order2HTML += "<div class='rdio rdio-warning' style='display: inline'>";
                        order2HTML += " <input name='project' type='radio' id='" + name + "' value='" + name + "' onclick='getRollbackList()'/>";
                        order2HTML += "<label for='" + name + "' style='width: 200px'>" + name + "</label></div>";
                        if (order2num % 4 == 0) {
                            order2HTML += "</br>";
                        }
                        order2num += 1;

                    }
                    if ("3" == order) {
                        order3HTML += "<div class='rdio rdio-success' style='display: inline'>";
                        order3HTML += " <input name='project' type='radio' id='" + name + "' value='" + name + "' onclick='getRollbackList()'/>";
                        order3HTML += "<label for='" + name + "' style='width: 200px'>" + name + "</label></div>";
                        if (order3num % 4 == 0) {
                            order3HTML += "</br>";
                        }
                        order3num += 1;

                    }
                }
                document.getElementById("project-div").innerHTML = order1HTML + "<hr>" + order2HTML + "<hr>" + order3HTML;
                getRollbackList();
            }
        },
        error: function (e) {
            alert("从后台获取数据出错");
        }
    });
}

/**
 * check box全选
 * @param obj
 * @param cName
 */
function projectcheckall(obj, cName) {
    var checkboxs = document.getElementsByName(cName);
    for (var i = 0; i < checkboxs.length; i += 1) {
        checkboxs[i].checked = obj.checked;
    }

}

//确认提交
function comfirmSubmit() {
    var operate = $("input[name='operate']:checked").val();
    var environment = $("input[name='environments']:checked").val();
    var projects = new Array();
    if ("Deploy" == operate) {//发布
        $('input[name="project1"]:checked').each(function () {
            projects.push($(this).val());//向数组中添加元素
        });
        $('input[name="project2"]:checked').each(function () {
            projects.push($(this).val());//向数组中添加元素
        });
        $('input[name="project3"]:checked').each(function () {
            projects.push($(this).val());//向数组中添加元素
        });
        if (projects.length == 0) {
            alert("请选择项目");
            return;
        }
        var branch = $("input[name='branch']").val();
        if (branch == "") {
            alert("请填写分支名称");
            return;
        }
        $.ajax({
            url: 'checkBranch?projects=' + projects + '&branch=' + branch+ '&environment=' + environment,
            type: 'POST',
            success: function (data) {
                if (data != "1") {
                    alert(data);
                    return;
                } else {
                    $("input[name='operate_name']").val(operate);
                    $("input[name='project_name']").val(projects);
                    $("input[name='environment_name']").val(environment);
                    $("input[name='branch_name']").val(branch);
                    $('#confirmSubmitDivId').modal('show');
                }
            },
            error: function (e) {
                alert("从后台获取数据出错");
            }
        });
    } else {//回滚
        var project = $("input[name='project']:checked").val();
        var rollbackfile = $("#rollbackList option:selected").val();
        if (project == "") {
            alert("请选择回滚项目");
            return;
        }
        if (typeof(rollbackfile) == "undefined" || rollbackfile == "") {
            alert("请选择回滚版本");
            return;
        }
        $("input[name='operate_name']").val(operate);
        $("input[name='project_name']").val(project);
        $("input[name='environment_name']").val(environment);
        $("input[name='rollbackfile_name']").val(rollbackfile);
        $('#confirmSubmitDivId').modal('show');

    }
}

function getRollbackList() {
    var project = $("input[name='project']:checked").val();
    var environment = $("input[name='environments']:checked").val();
    $.ajax({
        url: 'rollbackList?project=' + project + "&environment=" + environment,
        type: 'POST',
        dataType: 'json',
        success: function (data) {
            if ("failed" == data) {
                document.getElementById("select-rollbackList-div").innerHTML = "";
                return;
            }

            var list = eval("(" + data + ")");
            //动态生成rollbackList的select组件
            var innerHTMLContext = "<select id='rollbackList' data-placeholder='Choose One' style='width:350px;'>";
            for (var i = 0; i < list.length; i++) {
                innerHTMLContext += "<option value='" + list[i] + "'>" + list[i] + "</option>";
            }
            innerHTMLContext += "</select>";
            document.getElementById("select-rollbackList-div").innerHTML = innerHTMLContext;

            jQuery('#rollbackList').select2({
                minimumResultsForSearch: -1
            });
        },
        error: function (e) {
            alert("该项目暂无备份,无法回滚");
        }
    });
}
function changeservers() {
    var operate = $("input[name='operate']:checked").val();
    if (operate == "Rollback") {
        getRollbackList();
    }
    if(operate == "Deploy"){
        branchdefault();
    }
}

function branchdefault() {
    var operate = $("input[name='operate']:checked").val();
    if (operate == "Deploy") {
        var environment = $("input[name='environments']:checked").val();
        if(environment == "aws" || environment == "qcloud" || environment == "qcloud_gray"){
            $("input[name='branch']").val("master");
        }
        if(environment == "test"){
            $("input[name='branch']").val("test");
        }
        if(environment == "dev"){
            $("input[name='branch']").val("dev");
        }
    }
}