star.js 6.71 KB
/**
 * @fileOverview 视频模块
 * @author:Hbomb(zhouqq@yoho.cn)
 * @date:2013-05-27
 */
define('star',function(require, exports)
{
    var box = require('box'); // 提示框
    var $ = require("jquery");
    var common = require("common");
    var upload = require('admin/upload');
    var form_html = $("#form_pan").html()+'';
    var json = require("lib/util/json");
    $("#form_pan").remove();
    
    function init_upload()
    {
        //绑定上传背景按钮
        upload.init(
        {
            button_placeholder_id:"upload_pics",
            button_width : "58",
            button_height : "30",
            post_params: {'key':$("#upload_pics").attr('key'), 'format':'json'},
            custom_settings:
            {
                saveCallback:function(response,swf)
                {
                    if (response.code == 12)
                    {
                        var thumbPath = common.getImages(response.data.file.relaPath, "0200x0150", "blogimg", "primary");
                        var realPath = response.data.file.relaPath;
                        var val = $("#pics_val").val();
                        
                        if(val=="")
                        {
                            $("#pics_val").val('[{"url":"'+realPath+'","type":"pic"}]');
                        }
                        else
                        {
                            var arr = json.parse(val);
                            if(typeof arr == "string")
                            {
                                arr = json.parse(arr);
                            }
                            arr.push({"url":realPath,"type":"pic"});
                            var str = json.stringify(arr);
                            $("#pics_val").val(str);
                        }
                        $("#pics_val").after("<img class='thumb_but' width='40' height='30' val='"+realPath+"' src='"+thumbPath+"'>");
                    }
                    else
                    {
                        box.alert(response.message);
                    }
                }  
            }
        });
    }
    
    $("#add_star").click(function()
    {
        box.confirm
        (
            form_html, submit,
            {
                title: '添加主理人', 
                width: '750px', 
                autoClose: false
            }
        );
        init_upload();
    });
    
    
    //查看
    $(".look").click(function()
    {
        box.confirm
        (
            form_html, submit,
            {
                title: '查看主理人', 
                width: '750px', 
                autoClose: false
            }
        );
        
        init_upload();
        
        var obj = $(this).parents("tr");
        var name = obj.find("td:eq(1)").text();
        var pics = obj.find("td:eq(2)").attr("title");
        var description = obj.find("td:eq(3)").text();
        var brand_id = obj.find("td:eq(4)").text();
        var news_ids = obj.find("td:eq(5)").text();
        var seq = obj.find("td:eq(7)").text();
        var say = obj.find("td:eq(6)").text();
        var id = obj.find("td:eq(0)").text();
        
        var arr = json.parse(pics);
        if(typeof arr == "string")
        {
            arr = json.parse(arr);
        }
        
        var pic_arr = [];
        var video = '';
        $.each(arr,function(k,v)
        {
            if(v.type=="pic")
            {
                pic_arr.push(v);
                $("#pics_val").after("<img class='thumb_but' width='40' height='30' val='"
                        +v.url+"' src='"+common.getImages(v.url, "0200x0150", "blogimg", "primary")+"'>");
            }
            else
            {
                video = v.url;
            }
        });
        
        $("#id").val(id);
        $("#name").val(name);
        $("#pics_val").val(json.stringify(pics));
        $("#pics").val(video);
        $("#description").val(description);
        $("#brand_id").val(brand_id);
        $("#news_ids").val(news_ids);
        $("#seq").val(seq);
        $("#say").val(say);
        $("#state").val($("#state_from").val())
    });
    
    //提交主理人
    function submit()
    {
        if($("#name").val()=="")
        {
            alert("名字不能为空!");
            return;
        }

        if($("#description").val()=="")
        {
            alert("描述不能为空!");
            return;
        }
        if($("#brand_id").val()=="")
        {
            alert("品牌ID不能为空!");
            return;
        }
        if($("#pics_val").val()=="")
        {
            alert("图片不能为空!");
            return;
        }
        
        var arr = json.parse($("#pics_val").val());
        
        if(typeof arr == "string")
        {
            arr = json.parse(arr);
        }
        if($("#pics").val()!='')
        {
            var hasVideo = false;
            $.each(arr,function(k,v)
            {
                if(v&&v.type=="video")
                {
                    arr[k].url = $("#pics").val();
                    hasVideo = true;
                }
            });
            
            if(!hasVideo)
            {
                arr.push({type:"video",url:$("#pics").val()});
            }
        }
        
        var data = 
        {
           id:$("#id").val(),
           name:$("#name").val(),
           description:$("#description").val(),
           brand_id:$("#brand_id").val(),
           news_ids:$("#news_ids").val(),
           seq:$("#seq").val(),
           pics:json.stringify(arr),
           state:$("#state").val(),
           say:$("#say").val()
        };

        $.ajax
        ({
            type: "post",
            url: '/admin/leader/submit',
            data: data,
            success: function(data)
            {
                if (data.code == 200)
                {
                    window.location.reload();
                }
                else
                {
                    box.alert(data.message);
                }
            }
        });
    };
    
    $(".thumb_but").live("click",function()
    {
        var val = $(this).attr("val");
        var arr = [];
        if(val!="")
        {
            arr = json.parse($("#pics_val").val());
            if(typeof arr == "string")
            {
                arr = json.parse(arr);
            }
        }
        $.each(arr,function(k,v)
        {
           if(v&&v.url==val)
           {
               arr.splice(k,1);
           }
        });
        
        $("#pics_val").val(json.stringify(arr));
        $(this).remove();
    });
    
    $(".look_video").click(function()
    {
       $(this).next().toggle(); 
    });

    $("#state_from").change(function()
    {
        $(".form-search").submit();
    });
});