yhbLibrary.js
3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* 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);
}
}