easyui.hp.multipartImageUpload.js 3.76 KB
/**
 * 显示一个可以多选的上传图片插件
 * 作者:黄平
 * 日期:2016-11-25
 */
(function($) {
	$.fn.multipartImageUpload = function(options, param) {
		var self = this;
		
		if (typeof (options) == "string") {
			var method = $.fn.multipartImageUpload.methods[options];
			if (method){
				return method.call(this, param);
			}
		}
		return this.each(function() {
			var opt = $.extend({}, $.fn.multipartImageUpload.defaults, options);
			$(self).data("multipartImageUpload", opt)
			_create(self);
		});
	};
	
	/**
	 * 创建
	 * @param jq
	 * @returns
	 */
	function _create(jq) {
		var opt = jq.data("multipartImageUpload");
		var btnDiv = $("<div>").appendTo(jq);
		var contentDiv = $("<div>").appendTo(jq);
		if (opt.imgContentWidth) {
			contentDiv.css("width", opt.imgContentWidth);
		}
		if (opt.imgContentHeight) {
			contentDiv.css("height", opt.imgContentHeight);
		}
		var btn = $("<a>").linkbutton($.extend({}, opt.btnParam, {
			onClick : function() {
				var target = opt.openOnParent === true ? window.top : window.self;
				var div = $("<div>").appendTo($(target.document.body));
				$.fn.multipartImageUpload.tempParam = opt;
				
				target.$(div).myDialog($.extend({}, opt.dialogParam, {
					method : "post",
					href : opt.contextPath + "/js/jquery/easyui/myPlugins/easyui.hp.multipartImageUpload.html",
					//content : $("<div>").append(content.clone(true, true)).html(),
					modal : true,
					collapsible : true,
					cache : false,
					buttons : [{
						text : "保存图片",
						iconCls : "icon-save",
						handler : function() {
							
							var imgList = target.$("#multipartImageUploadImageList div.img-div");
							if (!imgList || imgList.length == 0) {
								target.$.messager.alert("失败", "没有上传图片", "error");
								return;
							}
							contentDiv.empty();
							contentDiv.css("width", (opt.imgWidth + 2) * opt.imgColumnNum);
							var divArr = new Array(opt.imgColumnNum);
							$(divArr).each(function(index, item) {
								divArr.push($("<div>").addClass("multipart-image-upload-waterfall-div").appendTo(contentDiv));
							});
							$(imgList).each(function(index, item) {
								//contentDiv.appdnd($(""));
								var i = index % opt.imgColumnNum;
								divArr[i].append($("<img>").attr("src", $(item).find("input[type='hidden'][role='img']").val()));
							});
							target.$(div).dialog("close");
						}
					}, {
						text : "关闭",
						iconCls : "icon-cancel",
						handler : function() {
							target.$(div).dialog("close");
						}
					}]
				}));
			}
		}));
		btnDiv.append(btn);
		
	}
	
	function _getImageUrl(jq) {
		var imgList = jq.find("img");
		var arr = [];
		$(imgList).each(function(index, item) {
			arr.push($(imgList).attr("src"));
		});
		return arr;
	}
	
	$.fn.multipartImageUpload.tempParam = {};
	
	$.fn.multipartImageUpload.methods = {
		getImageUrl : function() {
			return _getImageUrl(this);
		}
	};
	$.fn.multipartImageUpload.event = {
		
	};
	$.fn.multipartImageUpload.defaults = $.extend({}, $.fn.multipartImageUpload.event, {
		btnParam : {
			text : "批量上传",
			iconCls : "exter-icon exter-multipart-image"
		},									//按钮的属性
		dialogParam : {						//打开的对话框属性
			width : 600,
			height : 450,
			title : "批量上传"
		},
		imgContentWidth : 300,
		imgContentHeight : 500,
		openOnParent : false,				//是否在父页面打开对话框
		contextPath : "",
		multiple : false,
		accept : "image/*",
		maxSize : 1048576,					//每个文件的最大限制(byte)
		totalSize : 10 * 1048576,			//总共上传文件的最大限制(bytes)
		totalNum : 10,						//最多上传文件个数
		uploadUrl : "",						//上传文件的接口地址
		saveSuccess : function(result) {},	//图片上传成功后的回调方法
		imgColumnNum : 2,
		imgWidth : 300
	});
})(jQuery);