refund.js 3.08 KB
(function($) {    
	// 插件的定义    
	$.fn.Refund = function(options) {
		var defaults = {
			'boxId':'',
			'showBoxEvent' : [],
			'tpl' : '',
			'imgInputTpl':'imgInputTpl',
			'imageUploadButton':'upload',
			'imageProject' : 'evidenceImages',
			'imgTpl' : 'imgTpl',
			'imageSize' : '100x100',
			'maxImageNum' : 4
		};
  
		var params = $.extend(defaults, options);
		var imageNum = new Array();
		// iterate and reformat each matched element    
		return this.each(function() {
			$(this).change(function(){
				params['textid'] = $(this).attr('textid');
				var boxKey = params.boxId + '_' + params.textid;
				var showStatus = false;
				if(typeof(params.showBoxEvent) != undefined || params.showBoxEvent != {}){
					var id = $(this).val();
					for (k in params.showBoxEvent)
					{
						if(params.showBoxEvent[k] == id)
						{
							showStatus = true;
						}
					}
				}else{
					showStatus = true;
				}
				$('#' + boxKey).html('').parent().hide();
				if(showStatus == false)
				{
					return false;
				}
				if(typeof(params.boxId) == undefined || params.boxId == ''){
					alert('请设置容器ID');
					return false;
				}
				if(typeof(params.tpl) == undefined || params.tpl == '')
				{
					alert('请设置内容模版');
					return false;
				}
				if(typeof(params.imageUploadButton) == undefined || params.imageUploadButton == '')
				{
					alert('请设置上传图片的按钮ID');
					return false;
				}
				$('#' + boxKey).html('').html($('#' + params.tpl).html().replace(/{{textid}}/g,params.textid)).parent().show();
				imageNum[params.textid] = new Array();
				for(var t = 0; t < params.maxImageNum; t++)
				{
					imageNum[params.textid][t] = 0;
				}
				$('#' + params.imageUploadButton + '_' + params.textid).QFSUpload({
					'formData' : {'project':params.imageProject},
					'height' : 33,
					'width' : 116,
					'multi' : true,
					'file_queue_limit' : params.maxImageNum,
					'buttonImage' : 'http://static.yohobuy.com/images/v2/common/up_pic_btn.png',
					'onSuccess' : function(imageUrl, selector){
						idArr = selector.split('_');
						var textid = 0;
						if(idArr.length == 2)
						{
							textid = idArr[1];
						}
						for(var i = 0; i < imageNum[textid].length; i++){
							if(imageNum[textid][i] == 0){
								var _image = QGlobal.Common.QFSFImages(imageUrl,params.imageProject,params.imageSize,2);
								//设置隐藏域
								var html = $('#' + params.imgInputTpl).html().replace(/{{textid}}/g,textid).replace('{{num}}', i).replace('{{image}}', imageUrl);
								$('#' + params.boxId + '_' + textid).append(html);
								//显示效果
								var template = $('#' + params.imgTpl).html().replace('{{imageSrc}}', _image);
								$('#' + params.boxId + '_' + textid + ' li:eq('+i+')').html(template);
								//删除上传图片
								$('#' + params.boxId + '_' + textid + ' a:eq('+i+')').click(function(){
									$('#img_' + textid + '_' + i).remove();
									$(this).parent().html('');
									imageNum[textid][i] = 0;
								});
								imageNum[textid][i] = 1;
								return false;
							}
						}
					}
				});
			});
		});
	};
})(jQuery);