upload.js 5.94 KB
/**
 * @fileOverview 上传组件
 * @author:Hbomb(zhouqq@yoho.cn)
 * @date:2012-09-26
 */
define(
        'admin/upload',
        function(require, exports)
        {
            var $ = require("jquery");// jquery
            require('lib/ui/bootstrap');
            var json = require("lib/util/json");
            window.SWFUpload = require("lib/ui/swfupload/swfupload");
            
            var settings = {
                flash_url :  YohoConfig.resUrl+"/js/lib/ui/swfupload/swfupload.swf?rnd="+Math.random(),
                upload_url : YohoConfig.uploadUrl,
                post_params : {
                    "key" : "",
                    "format":"json"
                },
                file_post_name: "file",
                file_size_limit : "10 MB",
                file_types : "*.jpg;*.gif;*.png",
                file_types_description : "图片文件",
                file_upload_limit : 100,
                file_queue_limit : 1,
                debug : false,
                custom_settings:
                {
                    saveCallback:function()
                    {
                        
                    }
                },
                // Button settings
                button_placeholder_id : "swfupload",
                button_width : "65",
                button_height : "29",
                button_window_mode : SWFUpload.WINDOW_MODE.TRANSPARENT,
                button_cursor : SWFUpload.CURSOR.HAND,
                
                // The event handler functions are defined in handlers.js
                file_queued_handler : fileQueued,
                file_queue_error_handler : fileQueueError,
                file_dialog_complete_handler : fileDialogComplete,
                upload_start_handler : uploadStart,
                upload_progress_handler : uploadProgress,
                upload_error_handler : uploadError,
                upload_success_handler : uploadSuccess,
                upload_complete_handler : uploadComplete,
                queue_complete_handler : queueComplete
            };
            
            exports.init = function(opt)
            {
                $.extend(settings, opt);
                $.extend(settings.post_params, {"tag":"flash"});
                new SWFUpload(settings);
            };

            function fileQueued(file)
            {
                try
                {
                    
                }
                catch (ex)
                {
                    this.debug(ex);
                }
                
            }
            
            function fileQueueError(file, errorCode, message)
            {
                try
                {
                    if(errorCode==-110)
                    {
                        alert("上传图片不能超过10M")
                    }
                    else if(errorCode==-100)
                    {
                        alert("一次上传只能"+message+"个");
                    }
                    else
                    {
                        alert(message);
                    }
                }
                catch (ex)
                {
                    this.debug(ex);
                }
            }
            
            function fileDialogComplete(numFilesSelected, numFilesQueued)
            {
                try
                {
                    if (numFilesSelected < 1)
                    {
                        return;
                    }
                    
                    this.startUpload();
                }
                catch (ex)
                {
                    this.debug(ex);
                }
            }
            
            function uploadStart(file)
            {
                try
                {
                    var bar = '<div class="progress progress-striped active" style="height:5px;width:130px;margin-bottom:5px">'+
                    '<div class="bar" style="width: 0%;"></div>'+
                    '</div>';
                    $("#"+this.movieName).prev(".progress").remove();
                    $("#"+this.movieName).before(bar);
                }
                catch (ex)
                {
                }
                
                return true;
            }
            
            function uploadProgress(file, bytesLoaded, bytesTotal)
            {
                try
                {
                    var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
                    $("#"+this.movieName).prev(".progress").find(".bar").css(
                    {
                       "width":percent+"%" 
                    });
                }
                catch (ex)
                {
                    this.debug(ex);
                }
            }
            
            function uploadSuccess(file, serverData)
            {
                try
                {
                    $("#"+this.movieName).prev(".progress").remove();
                    var data = json.parse(serverData);
                    this.customSettings.saveCallback(data,this);                    
                }
                catch (ex)
                {
                    this.debug(ex);
                }
            }
            
            function uploadError(file, errorCode, message)
            {
                try
                {
                    if(errorCode==-110)
                    {
                        alert("上传图片不能超过10M")
                    }
                    else
                    {
                        alert(message);
                    }
                }
                catch (ex)
                {
                    this.debug(ex);
                }
            }
            
            function uploadComplete(file)
            {
                if (this.getStats().files_queued === 0)
                {
                    
                }
            }
            
            function queueComplete(numFilesUploaded)
            {
                
            }
        });