fredshare / blog

护卫银河
https://fredshare.github.com/blog/
112 stars 23 forks source link

php打造可视化源码发布系统 #23

Open fredshare opened 8 years ago

fredshare commented 8 years ago

php打造可视化源码发布系统

image

* 任务发布

当点击文件的时候,将文件展示出来,并添加查看源码和文件对比的功能。
$('#tree')
    .jstree({
        'core' : {
            'data' : {
                'url' : '/release/getNodeInfo?path='+path+'&operation=get_node',
                'data' : function (node) {
                    return { 'id' : node.id };
                }
            },
            'check_callback' : function(o, n, p, i, m) {
                if(m && m.dnd && m.pos !== 'i') { return false; }
                if(o === "move_node" || o === "copy_node") {
                    if(this.get_node(n).parent === this.get_node(p).id) { return false; }
                }
                return true;
            },
            'themes' : {
                'responsive' : false,
                'variant' : 'small',
                'stripes' : true
            }
        },
        'sort' : function(a, b) {
            return this.get_type(a) === this.get_type(b) ? (this.get_text(a) > this.get_text(b) ? 1 : -1) : (this.get_type(a) >= this.get_type(b) ? 1 : -1);
        },
        'contextmenu' : {
            'items' : function(node) {
                var tmp = $.jstree.defaults.contextmenu.items();
                delete tmp.create.action;
                tmp.create.label = "New";
                tmp.create.submenu = {
                    "create_folder" : {
                        "separator_after"   : true,
                        "label"             : "Folder",
                        "action"            : function (data) {
                            var inst = $.jstree.reference(data.reference),
                                obj = inst.get_node(data.reference);
                            inst.create_node(obj, { type : "default" }, "last", function (new_node) {
                                setTimeout(function () { inst.edit(new_node); },0);
                            });
                        }
                    },
                    "create_file" : {
                        "label"             : "File",
                        "action"            : function (data) {
                            var inst = $.jstree.reference(data.reference),
                                obj = inst.get_node(data.reference);
                            inst.create_node(obj, { type : "file" }, "last", function (new_node) {
                                setTimeout(function () { inst.edit(new_node); },0);
                            });
                        }
                    }
                };
                if(this.get_type(node) === "file") {
                    delete tmp.create;
                }
                return tmp;
            }
        },
        'types' : {
            'default' : { 'icon' : 'folder' },
            'file' : { 'valid_children' : [], 'icon' : 'file' }
        },
        'unique' : {
            'duplicate' : function (name, counter) {
                return name + ' ' + counter;
            }
        },
        'plugins' : ['state','dnd','sort','types','contextmenu','unique']
    })
    .on('delete_node.jstree', function (e, data) {
        $.get('/release/getNodeInfo?path='+path+'&operation=delete_node', { 'id' : data.node.id })
            .fail(function () {
                data.instance.refresh();
            });
    })
    .on('create_node.jstree', function (e, data) {
        $.get('/release/getNodeInfo?path='+path+'&operation=create_node', { 'type' : data.node.type, 'id' : data.node.parent, 'text' : data.node.text })
            .done(function (d) {
                data.instance.set_id(data.node, d.id);
            })
            .fail(function () {
                data.instance.refresh();
            });
    })
    .on('rename_node.jstree', function (e, data) {
        $.get('/release/getNodeInfo?path='+path+'&operation=rename_node', { 'id' : data.node.id, 'text' : data.text })
            .done(function (d) {
                data.instance.set_id(data.node, d.id);
            })
            .fail(function () {
                data.instance.refresh();
            });
    })
    .on('move_node.jstree', function (e, data) {
        $.get('/release/getNodeInfo?path='+path+'&operation=move_node', { 'id' : data.node.id, 'parent' : data.parent })
            .done(function (d) {
                //data.instance.load_node(data.parent);
                data.instance.refresh();
            })
            .fail(function () {
                data.instance.refresh();
            });
    })
    .on('copy_node.jstree', function (e, data) {
        $.get('/release/getNodeInfo?path='+path+'&operation=copy_node', { 'id' : data.original.id, 'parent' : data.parent })
            .done(function (d) {
                //data.instance.load_node(data.parent);
                data.instance.refresh();
            })
            .fail(function () {
                data.instance.refresh();
            });
    })
    .on('changed.jstree', function (e, data) {
        if(data && data.selected && data.selected.length) {
            var id = data.selected.join(':');
            if(id.split('.').length>1){
                //保存id信息
                if(!isIdExist(id)){
                    saveId(id);
                    $('#js_file_list').append('<li class="jstree-node  jstree-no"><span class="js_path">'+ id +'</span>&nbsp;&nbsp;<a href="#del" class="js_del">删除</a>&nbsp;&nbsp;<a href="#query" class="js_query">查看文件</a>&nbsp;&nbsp;<a href="#query" class="js_diff">对比文件</a></li>');
                }
            }

        }
        else {
            //$('#data .content').hide();
            $('#data .default').html('Select a file from the tree.').show();
        }
    });
    function isIdExist(id){
        for (var i = 0; i < fileList.length; i++) {
            if(fileList[i] == id){
                return true;
            }
        };
        return false;
    }
    function saveId(id){
        fileList.push(id);
    }
    function delId(id){
        for (var i = 0; i < fileList.length; i++) {
            if(fileList[i] == id){
                fileList.splice(i,1)
            }
        };
    }
    $('body').delegate('.js_del','click',function(e){
        var target = $(e.target).closest('li'),
            id = target.find('.js_path').text();
        console.log(target);
        target.remove();
        delId(id);
    })
    $('body').delegate('.js_query','click',function(e){
        var target = $(e.target).closest('li'),
            id = target.find('.js_path').text();
        var loading_id = layer.load(2);
        $.get('/release/getNodeInfo?path='+path+'&operation=get_content&id=' + id, function (d) {
            if(d && typeof d.type !== 'undefined') {
                switch(d.type) {
                    case 'text':
                    case 'txt':
                    case 'md':
                    case 'htaccess':
                    case 'log':
                    case 'sql':
                    case 'php':
                    case 'js':
                    case 'json':
                    case 'css':
                    case 'html':
                        //$('#data .code').show();
                        //$('#code').val(d.content);
                        //break;
                    case 'png':
                    case 'jpg':
                    case 'jpeg':
                    case 'bmp':
                    case 'gif':
                        /*$('#data .image img').one('load', function () { $(this).css({'marginTop':'-' + $(this).height()/2 + 'px','marginLeft':'-' + $(this).width()/2 + 'px'}); }).attr('src',d.content);
                        $('#data .image').show();*/
                        layer.close(loading_id);
                        layer.open({
                            type: 2,
                            title: false,
                            closeBtn: 0, //不显示关闭按钮
                            shade: [0],
                            area: ['340px', '215px'],
                            offset: 'rb', //右下角弹出
                            time: 1000, //1秒后自动关闭
                            shift: 2,
                            content: ['/release/fileInfo?path='+path+'&operation=get_content&id='+id, 'no'], //iframe的url,no代表不显示滚动条
                            end: function(){ //此处用于演示
                                layer.open({
                                    type: 2,
                                    title: '源码查看-'+id,
                                    shadeClose: true,
                                    shade: false,
                                    maxmin: true, //开启最大化最小化按钮
                                    area: ['893px', '600px'],
                                    content: '/release/fileInfo?path='+path+'&operation=get_content&id='+id
                                });
                            }
                        }); 
                        break;
                    default:
                        //$('#data .default').html(d.content).show();
                        //$('#js_file_list').append('<li class="jstree-node  jstree-no">'+data.selected.join(':')+'&nbsp;&nbsp;<a href="#del">删除</a>&nbsp;&nbsp;<a href="#query">查看文件</a></li>');
                        break;
                }
            }
        });
    })
    $('body').delegate('#js_release','click',function(e){
        console.log(fileList);
    })
    $('body').delegate('.js_diff','click',function(e){
        var target = $(e.target).closest('li'),
            id = target.find('.js_path').text();
        var loading_id = layer.load(2);
        layer.close(loading_id);
        layer.open({
            type: 2,
            title: false,
            closeBtn: 0, //不显示关闭按钮
            shade: [0],
            area: ['340px', '215px'],
            offset: 'rb', //右下角弹出
            time: 1000, //1秒后自动关闭
            shift: 2,
            content: ['/release/diffFile?path='+path+'&id='+id, 'no'], //iframe的url,no代表不显示滚动条
            end: function(){ //此处用于演示
                layer.open({
                    type: 2,
                    title: '源码对比-'+id,
                    shadeClose: true,
                    shade: false,
                    maxmin: true, //开启最大化最小化按钮
                    area: ['893px', '600px'],
                    content: '/release/diffFile?path='+path+'&id='+id
                });
            }
        }); 
    })
});

效果如下图:

image

image

文件对比就比较麻烦了,你得分别读开发环境下的文件的线上的文件,再一行一行的对比,还得渲染出来,不过网上还蛮多这样的开源项目,拿来主义,哈哈。我采用了[php-diff](https://github.com/chrisboulton/php-diff)项目。效果如下:

image