yhbLibrary.js 3.75 KB
/**
 * Created with JetBrains PhpStorm.
 * User: liuziyang
 * Date: 13-6-4
 * Time: 上午11:31
 * To change this template use File | Settings | File Templates.
 */
if (typeof YHBLibrary == undefined || !YHBLibrary) {
    var YHBLibrary = {};
}
YHBLibrary.debug = {
    logs: function (message) {
        window.console && console.log(message, this);
    }
}

YHBLibrary.uploadKey = '';

$.fn.extend({
    YHBLibraryUpload: function (options, uploadOptions) {
        _YHBLibraryUpload($(this).attr('id'), options, uploadOptions);
    }
});


function YHBLibraryUploadLimit(uploadBoxID, uploadLimit) {
    return $("#" + uploadBoxID).uploadify('settings', 'uploadLimit', uploadLimit);
}

function YHBLibrarySettings(uploadBoxID, name, value) {
    return $("#" + uploadBoxID).uploadify('settings', name, value);
}

function _YHBLibraryUpload(uploadBoxID, options, uploadOptions) {
    var _options = {
        dialogID: 'progressDialog',
        dialogClass: 'uploadDialog',
        debug: false,
        uploadBox: uploadBoxID,
        dialogTxt: '图片上传中……<span id="uploadProgress">0%</span>',
        progressBar: true,
        dialog: function () {
        },
        resultData: function (data) {
        }
    };

    var _uploadOptions = {
        swf: 'http://static.yohobuy.com/js/uploadify/uploadify.swf',
        uploader: 'http://upload.yohobuy.com',
        buttonImage: 'http://static.yohobuy.com/images/btn_ok_4.png',
        onUploadProgress: function (file, bytesUploaded, bytesTotal, totalBytesUploaded, totalBytesTotal) {
            var progressBar = Math.ceil(bytesUploaded / bytesTotal * 100);
            $('#uploadProgress').html(progressBar + '%');
            $('#uploadProgressBarBox').css('width', progressBar + "%").html(progressBar + "%");
            YHBLibrary.debug.logs(progressBar);
        },
        onUploadComplete: function (file) {
            YHBLibrary.debug.logs('ok');
        },
        onUploadSuccess: function (file, data, response) {
            var dataJson = $.parseJSON(data);
            var _resultData = new Array();
            if (dataJson.data != null) {
                for (row in dataJson.data.hit) {
                    _resultData.push(dataJson.data.hit[row].return_file_path);
                }
            }
            if (typeof ( _options.resultData ) == 'function') {
                _options.resultData(_resultData);
            }
            $('#' + _options.dialogID).dialog('close');
        },
        onInit: function (instance) {
            $("#" + _options.uploadBox + "-queue").hide();
        },
        onUploadStart: function (file) {
            _options.dialog();
        },
        onUploadError: function (file, errorCode, errorMsg, errorString) {
            YHBLibrary.debug.logs('The file ' + file.name + ' could not be uploaded: ' + errorString);
            $('#dialogBox').html('上传失败');
        },
        formData: {
            _key: YHBLibrary.uploadKey
        }
    };

    $.extend(_options, options);
    $.extend(_uploadOptions, uploadOptions);
    _dialog();
    $("#" + _options.uploadBox).uploadify(_uploadOptions);

    function _dialog() {
        if ($("#" + _options.dialogID).length > 0) {
            return;
        }
        var html = '<div id="' + _options.dialogID + '" class="' + _options.dialogClass + '"><span id="dialogBox">' + _options.dialogTxt + '</span>';
        if (_options.progressBar == true) {
            html += '<div class="uploadProgressBarbackground"><div id="uploadProgressBarBox" class="uploadProgressBarBox"></div></div>';
        }
        html += '</div>';
        $('body').append(html);
    }

    function _debug(messageKey, messageVal) {
        if (_options.debug == false) {
            return;
        }
        window.console && console.log(messageKey, messageVal);
    }
}