jquey+bootstrap实现表单拖拽并设置属性生成表单json数据代码

代码语言:html

所属分类:表单美化

代码描述:jquey+bootstrap实现表单拖拽并设置属性生成表单json数据代码

代码标签: 拖拽 设置 属性 生成 表单 json 数据

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en">

<head>
    <title>拖拽表单</title>
    <!-- Required meta tags -->
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/jquery.1.11.min.js"></script>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://at.alicdn.com/t/font_2716841_5q133u7t0gp.css" />

    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link type="text/css" rel="stylesheet" href="//repo.bfw.wiki/bfwrepo/css/bootstrap.3.3.4.css">

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/bootstrap-3.3.4.js"></script>
    <style>
        .controlRealName i {
    	margin-right: 3px;
    }
    .controlName {
    	width: 48%;
    	float: left;
    	padding: 5px;
    	border: 1px solid #ededed;
    	margin: 0 1% 20px 1%;
    	border-radius: 2px;
    	color: #777;
    }
    .interval {
    	width: 96%;
    	margin: 0 2%;
    	padding: 0 2%;
    	background: #ffffff;
    	float: left;
    	/* overflow-y: auto;
        overflow-x: hidden; */
    }
    .controlImgName {
    	width: 20px;
    	float: left;
    	height: 20px;
    	overflow: hidden;
    	position: relative;
    }
    .controlImgName img {
    	width: 100%;
    	position: absolute;
    	top: 0;
    	left: 0;
    }
    .controlRealName {
    	float: left;
    	font-size: 14px;
    }
    .controlName:hover {
    	color: #0377ed;
    	cursor: move;
    	border: 1px solid #0377ed;
    }
    .LdgFormTitle {
    	text-align: center;
    	font-size: 18px;
    }
    .LdgFormSubject {
    	width: 100%;
    	padding: 0px 10px;
    	float: left;
    	position: relative;
    	height: 100%;
    	overflow: auto;
    	background-color: #fff;
    }
    .LdgFormItemHead {
    	padding: 7px 6px;
    	width: 100%;
    }
    .LdgFormItemContent .checkbox,
    .LdgFormItemContent .checkbox-inline,
    .LdgFormItemContent .radio,
    .LdgFormItemContent .radio-inline {
    	padding-top: 7px;
    	margin-top: 0;
    	margin-bottom: 0;
    }
    .LdgFormItemName {
    	float: left;
    }
    .LdgFormItemGroupButton {
    	position: absolute;
    	right: 0;
    	bottom: 0;
    }
    .LdgFormItemGroupButton i {
    	margin-left: 2px;
    	background-color: #22aaff;
    	padding: 5px;
    	color: white;
    	cursor: pointer;
    }
    .LdgFormItemGroupButton i.controlMove {
    	cursor: move;
    }
    .LdgFormItemID {
    	background-color: #22aaff;
    	color: white;
    	padding: 2px;
    	margin-right: 5px;
    }
    .LdgFormItemContent {
    	width: 100%;
    }
    .LdgFormItemContent::before,
    .LdgFormItemContent::after {
    	display: table;
    	content: '';
    }
    .checkMargin {
    	margin-top: 7px;
    }
    .selectInputDiv {
    	display: none;
    	border: 1px solid #ccc;
    	width: 92%;
    	padding: 4px 6px;
    	box-shadow: 1px 1px 0px 0px #ccc;
    	position: absolute;
    	z-index: 10000;
    	background: #fff;
    	overflow: auto;
    }
    .selectInputDiv p {
    	margin-bottom: 0px;
    	padding: 0.5rem 0;
    }
    .selectInputDiv p:hover {
    	background: #0377ed;
    	color: #fff;
    }
    .full {
    	width: 100%;
    	float: left;
    }
    .z_photo {
    	padding: 5px;
    	border: 1px dashed #e7e6e6;
    }
    .z_file {
    	width: 120px;
    	height: 120px;
    	margin: 0 auto;
    	position: relative;
    }
    .add-img {
    	display: block;
    	width: 120px;
    	height: 120px;
    }
    .imgFile {
    	width: 100%;
    	height: 100%;
    	opacity: 0;
    	position: absolute;
    	top: 0px;
    	left: 0px;
    	z-index: 100;
    }
    .imgExplain {
    	font-size: 12px;
    	color: #888;
    	float: left;
    }
    .errerClass {
    	display: none;
    	float: left;
    	font-size: 14px;
    	color: #ff0000;
    }
    .message {
    	float: left;
    	width: 100%;
    	overflow: hidden;
    	position: relative;
    }
    .message .input {
    	width: 70%;
    	float: left;
    }
    .message .liulan {
    	width: 30%;
    	height: 38px;
    	padding: 4px 6px;
    	background: url(images/file.png) left center no-repeat;
    	text-indent: 10px;
    	cursor: pointer;
    	border: 1px solid #ccc;
    	border-radius: 3px;
    }
    .message .files {
    	position: absolute;
    	left: 0px;
    	top: 0px;
    	cursor: pointer;
    	filter: Alpha(opacity=0);
    	opacity: 0;
    	display: none;
    }
    .fileTips {
    	float: left;
    }
    .fileTips p {
    	color: #888 !important;
    	font-size: 12px !important;
    	margin: 0px !important;
    }
    .LdgFormButton {
    	border-bottom: 1px solid #e7e6e6;
    	padding: 5px;
    	margin-bottom: 5px;
    }
    .btn:focus,
    .btn:active:focus,
    .btn.active:focus,
    .btn.focus,
    .btn:active.focus,
    .btn.active.focus {
    	outline: none;
    	box-shadow: none;
    }
    
    .LdgFormAttribute,
    .LdgFormConfigs,
    .LdgFormControls {
    	width: 96%;
    	margin: 0 2%;
    	padding: 0 2%;
    	float: left;
    	position: relative;
    	overflow-y: auto;
    	overflow-x: hidden;
    }
    .textRegularCustom {
    	display: none;
    }
    .placeLocationTop {
    	border-top: 3px solid #22aaff;
    }
    .placeLocationBottom {
    	border-bottom: 3px solid #22aaff;
    }
    .LdgFormItem {
    	width: 100%;
    }
    .required {
    	float: left;
    	color: #f00;
    	margin: 0 2px;
    }
    .requiredHiddle {
    	display: none;
    }
    .previewMain {
    	position: absolute;
    	width: 800px;
    	height: 600px;
    	display: none;
    	margin: 0 auto;
    	border: 1px solid #000;
    	background: #fff;
    	top: 0;
    	left: 0;
    	right: 0;
    	bottom: 0;
    }
    .errorMain {
    	line-height: 40px;
    	height: 40px;
    	overflow: hidden;
    	color: #f00;
    	width: 100%;
    	display: none;
    }
    .hidMargin {
    	margin-bottom: 0px !important;
    }
    .z_fileImgDel {
    	position: absolute;
    	width: 100%;
    	text-align: center;
    	background: #000;
    	opacity: 0.7;
    	color: #fff;
    	bottom: 0px;
    	z-index: 101;
    }
    .messagedel {
    	position: absolute;
    	z-index: 101;
    	width: 120px;
    	height: 100%;
    	text-align: center;
    	line-height: 38px;
    	background: #000;
    	opacity: 0.5;
    	color: #fff;
    	display: none;
    	right: 30%;
    }
    .LdgFormTitle {
    	border-bottom: 1px solid #e7e6e6;
    	padding: 5px;
    	margin-bottom: 5px;
    }
    .LdgDiyForm {
    	border: 2px solid #e7e6e6;
    }
    .LdgDiyForm,
    .LdgDiyForm > div {
    	height: 100%;
    	position: relative;
    }
    .LdgDiyForm > div:nth-child(1) {
    	background-color: #fff;
    	width: 250px;
    	float: left;
    	border-right: 1px solid #e7e6e6;
    }
    .LdgDiyForm > div:nth-child(2) {
    	width: calc(100% - 254px);
    	margin-left: 250px;
    	padding: 4px;
    	position: absolute;
    }
    .LdgDiyForm > div:nth-child(3),
    .LdgDiyForm > div:nth-child(4) {
    	display: none;
    	border-left: 1px solid #e7e6e6;
    }
    
    .hasProperties.LdgDiyForm > div:nth-child(2) {
    	width: calc(100% - 504px);
    	position: absolute;
    }
    .hasProperties.LdgDiyForm > .showAttr {
    	/*div:nth-child(3)*/
    	background-color: #fff;
    	width: 250px;
    	float: right;
    	display: flex;
    	flex-direction: column;
    }
    
    .LdgFormSubjectForm .row {
    	margin: 3px auto;
    }
    .ldgPrew .LdgFormItem {
    	border: rgb(25, 178, 238) 1px dashed;
    	background-color: rgba(236, 245, 255, 0.3);
    	padding: 3px;
    	position: relative;
    }
    .ldgLeft .LdgFormItemHead,
    .ldgRight .LdgFormItemHead {
    	width: 100px;
    }
    .ldgLeft .LdgFormItem,
    .ldgRight .LdgFormItem {
    	width: 100%;
    	display: flex;
    }
    
    .ldgLeft .LdgFormItemContent {
    	float: left;
    }
    .ldgRight .LdgFormItemName {
    	float: right;
    }
    .ldgRight .required {
    	float: right;
    }
    </style>
</head>

<body style="height: 100%; position: fixed; width: 100%">
  
    <div id="app"></div>
    <script>
        //-----------------------------------------全局变量-------------------------------------------
    var mouseX //鼠标的位置
    var mouseY //鼠标的位置
    var controlHtml //被选中的控件
    var isCanBePlaced = false //是否可以放置
    var isDownControl = false //是否按下了控件
    var formLeft = 0 //form表单的坐标
    var formTop = 0
    var formRight = 0
    var formBottom = 0
    var object //中部控件
    var objectControl //中部控件点击后记录控件信息
    var objectIndex //中部控件正在操作的id
    var numberRegExp = '^[0-9]+$' //纯数字表达式
    var letterRegExp = '^[a-zA-Z]+$' //纯字母
    var chineseRegExp = '^[\u4e00-\u9fa5]{0,}$' //中文
    var emailRegExp = '^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$' //邮箱
    var mobileRegExp = '^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$' //手机号码
    var idRegExp = '^((d{18})|([0-9x]{18})|([0-9X]{18}))$' //身份证
    var ldgDefaultBtns = {
    	//默认按钮  移动和删除
    	move: { class: 'icon-yidong controlMove' },
    	delete: { class: 'icon-shanchu controlDelete' },
    }
    var contorlCache = []
    
    //-----------------------------------------初始化方法及页面加载方法-------------------------------------------
    function Initialization(Parameters) {
    	//先清除对应div中的html代码
    	let el = '.DiyForm'
    	if (Parameters['el']) {
    		el = Parameters['el']
    	}
    	$(el).empty()
    	let height = '100%'
    	if (Parameters['height']) {
    		height = Parameters['height']
    	}
    	$(el).height(height)
    	var main = '<div class="LdgDiyForm" style="background:#edededa1;">'
    	main += leftToolbar()
    	main += centerMain()
    	main += rightAttribute()
    	main += '</div><div class="dragControl"></div>'
    	main += '<div class="previewMain"></div>'
    
    	//添加基本的界面布局
    	$(el).append(main)
    
    	$('.interval').css('height', document.body.offsetHeight + 'px') //初始化高度,跟浏览器可视高度一致
    }
    function GetBtns(btns, id) {
    	let str = '<!--表单控件的功能按钮-->'
    	str += '<div class="LdgFormItemGroupButton">'
    	str += '<span class="LdgFormItemID">' + id + '</span>'
    	if (btns) {
    		for (let key in btns) {
    			str += '<i  class="iconfont ' + btns[key].class + '"></i>'
    		}
    	} else {
    		for (let key in ldgDefaultBtns) {
    			str += '<i  class="iconfont ' + ldgDefaultBtns[key].class + '"></i>'
    		}
    	}
    	str += '</div>'
    	return str
    }
    function GetRequired(required) {
    	if (required) {
    		return '<div class="required">*</div>'
    	}
    	return ''
    }
    
    function GetCheck(required, check = false) {
    	if (required || check) {
    		return '<div class="errorMain"></div>'
    	}
    	return ''
    }
    
    function getRadioByTextAreaValue(TextAreaValue, optionvalue, id) {
    	let spilts = TextAreaValue.split('\n')
    	let str = ''
    	spilts.forEach(function (item) {
    		if (item != '') {
    			let checked = ''
    			if (item == optionvalue) {
    				checked = " checked=''"
    			}
    			str += `<div class="radio"><label><input type="radio" name="${id}" value="${item}"${checked}>${item}</label></div>`
    		}
    	})
    	return str
    }
    
    function getCheckBoxByTextAreaValue(TextAreaValue, optionvalue, id) {
    	let spilts = TextAreaValue.split('\n')
    	let spiltso = optionvalue.split('\n')
    	let str = ''
    	spilts.forEach(function (item) {
    		if (item != '') {
    			let checked = ''
    			spiltso.forEach(function (item2) {
    				if (item == item2) {
    					checked = " checked=''"
    				}
    			})
    			str += `<div class="checkbox"><label><input  name="${id}" type="checkbox" value="${item}"${checked}>${item}</label></div>`
    		}
    	})
    	return str
    }
    
    function getSelectByTextAreaValue(TextAreaValue, optionvalue, id) {
    	let spilts = TextAreaValue.split('\n')
    	if (!optionvalue || optionvalue.indexOf('请选择') == 0) {
    		optionvalue = ''
    	}
    	let str = '<select name="${id}" class="form-control">'
    	spilts.forEach(function (item) {
    		let itemvalue = item
    		if (itemvalue.indexOf('请选择') == 0) {
    			itemvalue = ''
    		}
    		if (item != '') {
    			let selected = ''
    			if (itemvalue == optionvalue) {
    				selected = " selected=''"
    			}
    			str += `<option value="${itemvalue}"${selected}>${item}</option>`
    		}
    	})
    	str += '</select>'
    	return str
    }
    
    function getTextSelectByTextAreaValue(TextAreaValue, id) {
    	let spilts = TextAreaValue.split('\n')
    	let str = '<input type="text" name="${id}" class="form-control selectInput formVal"><div class="selectInputDiv" title="下拉菜单">'
    	spilts.forEach(function (item) {
    		if (item != '') {
    			str += '<p class="selectInputDivP">' + item + '</p>'
    		}
    	})
    	str += '</div>'
    	return str
    }
    let ldgFormConfig={
    	width: { name: '组件标签宽度', value: '100', type: 'number' },
    	align: { name: '标签对齐方式', value: 0, options: { 右侧对齐: 0, 顶部对齐: 1, 左侧对齐: 2 }, type: 'radio' },
    }
    let controlList = {
    	//控件英文名
    	text: {
    		name: '文本框', //控件名称
    		icon: 'icon-duohangshurukuang', //控件图标
    		datas: {
    			//控件属性
    			//对象名:{name:"属性名称",value:"默认值",type:"text(文本类型)|number(数字类型)|date(日期型)|select(下拉选择)|radio(单选框)",options(仅支持下拉和单选框):{ 选项1: "值1", 选项2: "值1",选项3:"other(如果值为other,下拉选择支持自定义输入)" }}
    			name: { name: '控件名称', value: '文本框', type: 'text' },
    			placeholder: { name: '提示语句', value: '', type: 'text' },
    			value: { name: '默认内容', value: '', type: 'text' },
    			check: { name: '校验规则', value: '', options: { 无需校验: '', 数字: numberRegExp, 字母: letterRegExp, 中文: chineseRegExp, 邮箱: emailRegExp, 手机号: mobileRegExp, 身份证: idRegExp, 自定义: 'other' }, type: 'select' },
    			required: { name: '是否必填', value: 0, options: { 必填: 0, 选填: 1 }, type: 'radio' },
    		},
    		btns: {
    			move: { class: 'icon-yidong controlMove' },
    			delete: { class: 'icon-shanchu controlDelete' },
    		},
    		getHtml: function () {
    			return '<input name="'+this.datas.id.value+'" type="text" class="form-control formVal">'
    		},
    	}, //文本框
    	multiple: {
    		name: '多行文本框',
    		icon: 'icon-danhangshurukuang',
    		datas: {
    			name: { name: '控件名称', value: '多行文本框', type: 'text' },
    			placeholder: { name: '提示语句', value: '', type: 'text' },
    			value: { name: '默认内容', value: '', type: 'textarea' },
    			height: { name: '控件高度', value: 100, type: 'number' },
    			required: { name: '是否必填', value: 0, options: { 必填: 0, 选填: 1 }, type: 'radio' },
    		},
    		getHtml: function () {
    			return '<textarea name="'+this.datas.id.value+'" class="form-control formVal" cols="30" rows="5" style="resize: none;"></textarea>'
    		},
    	}, //多行文本框
    	date: {
    		name: '时间选择框',
    		icon: 'icon-shijianxuanzeqi',
    		datas: {
    			name: { name: '控件名称', value: '时间框', type: 'text' },
    			value: { name: '默认内容', value: '', type: 'date' },
    			required: { name: '是否必填', value: 0, options: { 必填: 0, 选填: 1 }, type: 'radio' },
    		},
    		getHtml: function () {
    			return '<input name="'+this.datas.id.value+'" type="date" class="form-control formVal">'
    		},
    	}, //时间选择框
    	radio: {
    		name: '单选框',
    		icon: 'icon-danxuan',
    		datas: {
    			name: { name: '控件名称', value: '单选框', type: 'text' },
    			value: { name: '默认勾选', value: '选项一', type: 'text' },
    			options: { name: '单选框选项(一行一个选项)', value: '选项一\n选项二', type: 'textarea' },
    			required: { name: '是否必填', value: 0, options: { 必填: 0, 选填: 1 }, type: 'radio' },
    		},
    		getHtml: function () {
    			return getRadioByTextAreaValue(this.datas.options.value, this.datas.value.value, this.datas.id.value)
    		},
    	}, //单选框
    	checkbox: {
    		name: '多选框',
    		icon: 'icon-duoxuan',
    		datas: {
    			name: { name: '控件名称', value: '多选框', type: 'text' },
    			value: { name: '默认勾选', value: '多选一\n多选二', type: 'textarea' },
    			options: { name: '多选框选项(一行一个选项)', value: '多选一\n多选二\n多选三', type: 'textarea' },
    			required: { name: '是否必填', value: 0, options: { 必填: 0, 选填: 1 }, type: 'radio' },
    		},
    		getHtml: function () {
    			return getCheckBoxByTextAreaValue(this.datas.options.value, this.datas.value.value, this.datas.id.value)
    		},
    	}, //多选框
    	dropdown: {
    		name: '下拉选择框',
    		icon: 'icon-choose1',
    		datas: {
    			name: { name: '控件名称', value: '下拉选择框', type: 'text' },
    			value: { name: '默认选择', value: '请选择省份', type: 'text' },
    			options: { name: '下拉框选项(一行一个选项)', value: '请选择省份\n河南\n河北\n山东', type: 'textarea' },
    			required: { name: '是否必填', value: 0, options: { 必填: 0, 选填: 1 }, type: 'radio' },
    		},
    		getHtml: function () {
    			return getSelectByTextAreaValue(this.datas.options.value, this.datas.value.value, this.datas.id.value)
    		},
    	}, //下拉选择框
    	textdropdown: {
    		name: '文本下拉框',
    		icon: 'icon-xialaxuanze',
    		datas: {
    			name: { name: '控件名称', value: '文本下拉框', type: 'text' },
    			value: { name: '默认内容', value: '', type: 'text' },
    			placeholder: { name: '提示语句', value: '', type: 'text' },
    			options: { name: '下拉框选项(一行一个选项)', value: '操作证一级\n操作证二级\n电工操作证', type: 'textarea' },
    			required: { name: '是否必填', value: 0, options: { 必填: 0, 选填: 1 }, type: 'radio' },
    		},
    		getHtml: function () {
    			return getTextSelectByTextAreaValue(this.datas.options.value, this.datas.value.value, this.datas.id.value)
    		},
    	}, //文本下拉框
    	//img: { name: '图片上传', icon: 'icon-ic_image_upload' }, //图片上传
    	//file: { name: '文件上传', icon: 'icon-wenjianshangchuan' }, //文件上传
    }
    //左侧工具栏
    function leftToolbar() {
    	//<!--左侧工具栏-->
    	var str = '<div class="ldgFormControl">'
    	str += '<p class="LdgFormTitle">工具栏</p>'
    	str += '<div class="LdgFormControls">'
    	for (var key in controlList) {
    		str += '<div class="controlName" data-type="' + key + '">'
    		str += '<div class="controlRealName"><i  class="iconfont ' + controlList[key].icon + '"></i>' + controlList[key].name + '</div>'
    		str += '</div>'
    	}
    	str += '</div></div>'
    	return str
    }
    
    //中部控件主体
    function centerMain() {
    	//<!--中间内容区域-->
    	var str = '<div class="ldgFormDesigner">'
    	//<!--主体部分-->
    	str += '<div class="LdgFormSubject ldgPrew ldgRight">'
    	str += `<div class="LdgFormButton">
    	<button class="btn btn-danger btn-xs ldgFormClearAll"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span>清空</button>
    	<span class="pull-right">
    	<button class="btn btn-info btn-xs ldgFormImport"><span class="glyphicon glyphicon-import" aria-hidden="true"></span>导入JSON</button>
    	<button class="btn btn-info btn-xs ldgFormExport"><span class="glyphicon glyphicon-export" aria-hidden="true"></span>导出JSON</button>
    	<button class="btn btn-success btn-xs ldgFormPreview"><span class="glyphicon glyphicon-sunglasses" aria-hidden="true"></span>预览表单</button>
    	<button class="btn btn-default btn-xs ldgFormConfigShow"><span class="glyphicon glyphicon-sunglasses" aria-hidden="true"></span>表单属性</button>
    	</span>
    	</div>`
    	str += '<form class="LdgFormSubjectForm"></form>'
    	str += '</div></div>'
    	return str
    }
    
    //右侧属性栏
    function rightAttribute() {
    	//<!--右侧属性框-->
    	var str = '<div class="ldgFormConfig">'
    	str += '<p class="LdgFormTitle">表单属性  <button class="btn btn-danger btn-xs closeAttr"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span>关闭</button></p>'
    	str += '<div class="LdgFormConfigs"></div></div>'
    	str += '<div class="ldgFormProperties">'
    	str += '<p class="LdgFormTitle">组件设置  <button class="btn btn-danger btn-xs closeAttr"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span>关闭</button></p>'
    	str += '<div class="LdgFormAttribute" data-type=""></div>'
    	/*
        //<!--图片框属性-->
        str +=
            '<div class="LdgFormAttribute" data-type="img">                     <div class="form-group">                         <label>控件名称</label>                         <input type="text" class="form-control controlAttributeName">                     </div>                     <div class="form-group">                         <label>上传数量</label>                         <input type="number" class="form-control uploadNum">                     </div>                     <div class="form-group">                         <label>此题</label>                         <div class="row">                             <div class="col-lg-6">                                 <div class="form-check">                                     <label class="form-check-label">                                         <input type="radio" class="form-check-input" name="ChoiceRadio"                                             value="0" >                                         必填                                     </label>                                 </div>                             </div>                             <div class="col-lg-6">                                 <div class="form-check">                                     <label class="form-check-label">                                         <input type="radio" class="form-check-input" name="ChoiceRadio"                                             value="1">                                         选填                                     </label>                                 </div>                             </div>                         </div>                     </div>                 </div> '
        //<!--文件框属性-->
        str +=
            '<div class="LdgFormAttribute" data-type="file">                     <div class="form-group">                         <label>控件名称</label>                         <input type="text" class="form-control controlAttributeName">                     </div>                     <div class="form-group">                         <label>上传数量</label>                         <input type="number" class="form-control uploadNum">                     </div>                     <div class="form-group">                         <label>此题</label>                         <div class="row">                             <div class="col-lg-6">                                 <div class="form-check">                                     <label class="form-check-label">                                         <input type="radio" class="form-check-input" name="ChoiceRadio"                                             value="0" >                                         必填                                     </label>                                 </div>                             </div>                             <div class="col-lg-6">                                 <div class="form-check">                                     <label class="form-check-label">                                         <input type="radio" class="form-check-input" name="ChoiceRadio"                                             value="1">                                         选填                                     </label>                                 </div>                             </div>                         </div>                     </div>                 </div>'
        */
    
    	str += '</div>'
    	return str
    }
    
    //-----------------------------------------中部控件的显示方法-------------------------------------------
    
    //图片
    function getImg() {
    	//<!--具体的表单控件 图片-->
    	var str = '<div class="row"><div class="LdgFormItem" data-type="img" data-choice="true" data-num="1" >'
    	//<!--表单控件的头部-->
    	str += '<div class="LdgFormItemHead">'
    	//<!--表单控件的名称-->
    	str += '<div class="required">*</div><div class="LdgFormItemName">上传图片</div>'
    	//<!--表单控件的功能按钮-->
    	str += '<div class="LdgFormItemGroupButton">'
    	str += '<button type="button" class="btn btn-primary controlMove">移动</button>'
    	str += '<button type="button" class="btn btn-primary controlDelete">删除</button>'
    	str += '</div></div>'
    	//<!--控件-->
    	str += '<div class="LdgFormItemContent">'
    	str += '<div class="form-group hidMargin">'
    	str += '<div class="img-box full">'
    	str += '<section class="img-section">'
    	str += '<div class="z_photo upimg-div clear">'
    	str += '<section class="z_file fl">'
    	str += '<div class="z_fileImgDel">删除</div>'
    	str += '<img src="../images/a11.png" class="add-img" />'
    	str += '<input type="file" name="file" class="file imgFile" value="" accept="image/jpg,image/jpeg,image/png,image/bmp" leipiplugins="imgupload" title="图片上传" />'
    	str += '</section></div></section>'
    	str += '<div class="imgExplain">支持jpg、jpeg、png、bmp等图片格式</div>'
    	str += '<div class="errerClass imgErrer"></div>'
    	str += '</div>'
    	str += '</div><div class="errorMain"></div></div></div></div>'
    	return str
    }
    
    //文件
    function getFile() {
    	//<!--具体的表单控件 图片-->
    	var str = '<div class="row"><div class="LdgFormItem" data-type="file" data-choice="true" data-num="1" >'
    	//<!--表单控件的头部-->
    	str += '<div class="LdgFormItemHead">'
    	//<!--表单控件的名称-->
    	str += '<div class="required">*</div><div class="LdgFormItemName">上传文件</div>'
    	//<!--表单控件的功能按钮-->
    	str += '<div class="LdgFormItemGroupButton">'
    	str += '<button type="button" class="btn btn-primary controlMove">移动</button>'
    	str += '<button type="button" class="btn btn-primary controlDelete">删除</button>'
    	str += '</div></div>'
    	//<!--控件-->
    	str += '<div class="LdgFormItemContent">'
    	str += '<div class="form-group hidMargin">'
    	str += '<div class="message">'
    	str += '<div class="messagedel">删除</div>'
    	str += '<input type="text" name="txt" class="input fileUpload form-control" value="" disabled="disabled" />'
    	str += '<input type="button" value="上传文件" size="30" class="liulan fileUploadBtn">'
    	str += '<input type="file" name="f" style="height:26px;" class="files leipiplugins fileUploadReal" size="1" leipiplugins="uploadfile" title="文件上传" hidefocus></div>'
    	str += '<div class="fileTips">'
    	str += '<p>1.上传文档不超过100MB</p>'
    	str += '<p>2.为了保证文档能正常使用,我们支持以下格式的文档上传:</p>'
    	str += '<p>MS Office文档: doc,docx,ppt,pptx,xls,xlsx,vsd,pot,pps,rtf</p>'
    	str += '<p>压缩文档: rar,zip,tar,gz,tgz</p>'
    	str += '<p>图片: jpg,jpeg,gif,bmp,png</p>'
    	str += '<p>WPS office系列: wps,et,dps</p>'
    	str += '<p>PDF: pdf</p>'
    	str += '<p>纯文本: txt</p>'
    	str += '<p>EPUB: epub</p></div>'
    	str += '<div class="errerClass FileErrer"></div>'
    	str += '</div><div class="errorMain"></div></div></div></div>'
    	return str
    }
    
    function switchType(typeinfo, type,isdiy) {
    	if ('undefined' != typeof typeinfo.getDiyHtml) {
    		return typeinfo.getDiyHtml(isdiy)
    	}
    	if ('undefined' != typeof typeinfo.getHtml) {
    		let html='<div class="row"><div class="LdgFormItem" data-id="'+typeinfo.datas.id.value+'" data-type="' + type + '">' + '<div class="LdgFormItemHead"><div class="LdgFormItemName">' + GetRequired(typeinfo.datas.required) + '<span>' + typeinfo.datas.name.value + '</span>' + '</div></div>';
    		if(isdiy)
    		{
    			html += GetBtns(typeinfo.btns, typeinfo.datas.id.value) 
    		}
    		html+='<div class="LdgFormItemContent"><div class="form-group hidMargin">' + typeinfo.getHtml(isdiy) + '</div>' + GetCheck(typeinfo.datas.required, typeinfo.datas.check) + '</div>'
    		return html
    	}
    	var str
    	switch (type) {
    		case 'img':
    			str = getImg()
    			break
    		case 'file':
    			str = getFile()
    			break
    		default:
    			str = ''
    			break
    	}
    	return str
    }
    
    //-----------------------------------------左侧工具栏事件-------------------------------------------
    //左侧工具栏鼠标按下控件事件
    $('body').on('mousedown', '.controlName', function (e) {
    	e.preventDefault()
    	mouseX = e.clientX //获取鼠标所在的x坐标
    	mouseY = e.clientY //获取鼠标所在的y坐标
    	controlHtml = $(this).prop('outerHTML')
    	var style = 'class="controlName controlNameMove" '
    	style += 'style="width:' + $(this).width() + 'px;'
    	style += 'position: absolute;'
    	style += 'background: #fff;'
    	style += 'z-index: 100;'
    	style += 'top: ' + new Number(mouseY - $(this).height() / 2) + 'px;'
    	style += 'left: ' + new Number(mouseX - $(this).width() / 2) + 'px;'
    	style += '"'
    	controlHtml = controlHtml.replace('class="controlName"', style)
    	$('.dragControl').append(controlHtml)
    	//获取form的坐标
    	var LdgFormSubjectForm = $('.LdgFormSubject')
    	formLeft = $(LdgFormSubjectForm).offset().left
    	formTop = $(LdgFormSubjectForm).offset().top
    	formRight = formLeft + LdgFormSubjectForm.width()
    	formBottom = formTop + LdgFormSubjectForm.height()
    	//按下了控件,触发滑动事件里的效果
    	isDownControl = true
    	$('.LdgDiyForm').removeClass('hasProperties')
    })
    
    //左侧工具栏鼠标松开控件事件
    $('body').on('mouseup', '.controlName', function (e) {
    	e.preventDefault()
    	//松开了控件,不触发滑动事件里的效果
    	isDownControl = false
    	var type = $(this).data('type')
    	//处于放置的位置才能在中部区域增加控件
    	if (isCanBePlaced) {
    		isCanBePlaced = false
    		addControl(type)
    		ApplyConfig()
    	}
    	$('.LdgFormSubjectForm').removeClass('placeLocationBottom')
    	$('.LdgFormSubjectForm').removeClass('placeLocationTop')
    	$('.dragControl').empty()
    })
    
    //-----------------------------------------中部区域事件-------------------------------------------
    //中部控件删除事件
    $('body').on('click', '.controlDelete', function (e) {
    	e.stopPropagation() //阻止事件冒泡
    	let type = $(this).parents('.LdgFormItem').data('type') //点击的元素类型
    	var index = $(this).index('.controlDelete')
    	$('.LdgFormSubjectForm').children().eq(index).remove()
    	$('.LdgDiyForm').removeClass('hasProperties')
    	contorlCache.splice(index,1)
    })
    
    //中部控件按下事件
    $('body').on('mousedown', '.controlMove', function (e) {
    	e.preventDefault()
    	mouseX = e.clientX //获取鼠标所在的x坐标
    	mouseY = e.clientY //获取鼠标所在的y坐标
    	controlHtml = $(this).parents('.row').html()
    	var style = 'class="LdgFormItem LdgFormItemMove" '
    	style += 'style="width:' + $(this).parents('.row').width() + 'px;'
    	style += 'position: absolute;'
    	style += 'background: #fff;'
    	style += 'z-index: 100;'
    	style += 'top: ' + new Number(mouseY - $(this).parents('.row').height()) + 'px;' // / 2是中间
    	style += 'left: ' + new Number(mouseX - $(this).parents('.row').width()) + 'px;'
    	style += '"'
    	controlHtml = controlHtml.replace('class="LdgFormItem"', style)
    	controlHtml = '<div class="row">' + controlHtml + '</div>'
    	$('.dragControl').append(controlHtml)
    	//获取form的坐标
    	var LdgFormSubjectForm = $('.LdgFormSubject')
    	formLeft = getRealLocationLeft(LdgFormSubjectForm, 0)
    	formTop = getRealLocationTop(LdgFormSubjectForm, 0)
    	formRight = formLeft + LdgFormSubjectForm.width()
    	formBottom = formTop + LdgFormSubjectForm.height()
    	//按下了控件,触发滑动事件里的效果
    	isDownControl = true
    
    	var index = $(this).index('.controlMove')
    	$('.LdgFormSubjectForm').children().eq(index).remove()
    })
    
    //中部控件松开事件
    $('body').on('mouseup', '.LdgFormItemMove', function (e) {
    	e.preventDefault()
    	$(this).removeAttr('style')
    	var str = $('.LdgFormItemMove').parents('.dragControl').html()
    	str = str.replace(' LdgFormItemMove', '')
    
    	//获取当前中部控件,删除放置效果
    	var item = $('.LdgFormSubjectForm').find('.row')
    
    	//处于放置的位置才能在中部区域增加控件
    	if (isCanBePlaced && isDownControl) {
    		if (item.length > 0) {
    			//判断是否位置放好了,有时候没有放置在控件上,就放置到最底层
    			var isSelectPosition = false
    			for (var i = 0; i < item.length; i++) {
    				if (item.eq(i).hasClass('placeLocationBottom')) {
    					item.eq(i).after(str)
    					item.eq(i).removeClass('placeLocationBottom')
    					isSelectPosition = true
    				} else if (item.eq(i).hasClass('placeLocationTop')) {
    					item.eq(i).before(str)
    					item.eq(i).removeClass('placeLocationTop')
    					isSelectPosition = true
    				}
    			}
    			if (!isSelectPosition) {
    				$('.LdgFormSubjectForm').append(str)
    			}
    		} else {
    			$('.LdgFormSubjectForm').append(str)
    		}
    	} else {
    		//放置在其他地区时,直接删除样式,添加到最后一个位置
    		if (item.length > 0) {
    			//删除样式
    			for (var i = 0; i < item.length; i++) {
    				item.eq(i).removeClass('placeLocationBottom')
    				item.eq(i).removeClass('placeLocationTop')
    			}
    		}
    		$('.LdgFormSubjectForm').append(str)
    	}
    
    	$('.LdgFormSubjectForm').removeClass('placeLocationBottom')
    	$('.LdgFormSubjectForm').removeClass('placeLocationTop')
    	//松开了控件,不触发滑动事件里的效果
    	isDownControl = false
    	$('.dragControl').empty()
    })
    
    //中间控件点击事件(显示右侧属性栏)
    $('body').on('click', '.ldgPrew .LdgFormItem', function () {
    	$('.LdgDiyForm').addClass('hasProperties')
    	$(".ldgFormProperties").addClass("showAttr")
    	$(".ldgFormConfig").removeClass("showAttr")
    	object = $(this)
    	objectIndex = $('.ldgPrew .LdgFormItem').index($(this))
    	let datas = contorlCache[objectIndex].datas
    	AttributeShow('.LdgFormAttribute',datas)
    })
    
    //-----------------------------------------右侧属性栏事件-------------------------------------------
    //显示属性框的方法
    function AttributeShow(showel,datas) {
    	var attribute = $(showel)
    	//if (attribute.data('type') != object.data('type')) {
    	//attribute.data('type', object.data('type'))
    	//objectControl = controlList[object.data('type')]
    	//let datas = objectControl.datas
    	
    	if (datas) {
    		//$('.LdgFormAttribute').html(controlList[object.data('type')].attr)
    		let html = ''
    		for (let key in datas) {
    			//let cvalue = object.data(key)
    			let cvalue = datas[key].value
    			// if (!cvalue) {
    			// 	cvalue = datas[key].value
    			// }
    			if (datas[key].type == 'text' || datas[key].type == 'number' || datas[key].type == 'date') {
    				//字符串输入框属性
    				html += `<div class="form-group" attrname="${key}"><label>${datas[key].name}</label><input type="${datas[key].type}" value="${cvalue}" class="form-control"></div>`
    			} else if (datas[key].type == 'textarea') {
    				//多行字符串输入框属性
    				html += `<div class="form-group" attrname="${key}"><label>${datas[key].name}</label> <textarea class="form-control" rows="5">${cvalue}</textarea></div>`
    			} else if (datas[key].type == 'select') {
    				//下拉框属性
    				html += `<div class="form-group"  attrname="${key}"><label>${datas[key].name}</label><select class="form-control">`
    				let othervalue = 'other'
    				for (let key2 in datas[key].options) {
    					if (datas[key].options[key2] == cvalue) {
    						othervalue = ''
    					}
    				}
    				for (let key2 in datas[key].options) {
    					let tempselected = ''
    					if (datas[key].options[key2] == cvalue || othervalue == datas[key].options[key2]) {
    						tempselected = 'selected'
    					}
    					html += `<option ${tempselected} value="${datas[key].options[key2]}">${key2}</option>`
    				}
    				html += '</select>'
    				html += '<input type="text" class="form-control other" placeholder="请输入自定义内容" style="display: ' + (othervalue != '' ? 'inline-block' : 'none') + ';" value="' + cvalue + '"></input>'
    				html += '</div>'
    			} else if (datas[key].type == 'radio') {
    				//单选框属性
    				html += `<div class="form-group"  attrname="${key}"><label>${datas[key].name}</label><div class="row">`
    				let radioclass="col-lg-6";
    				if(Object.keys(datas[key].options).length!=2)
    				{
    					radioclass="col-lg-12";
    				}
    				for (let key2 in datas[key].options) {
    					let tempchecked = ''
    					if (datas[key].options[key2] == cvalue) {
    						tempchecked = 'checked'
    					}
    					html += `<div class="${radioclass}"><div clas.........完整代码请登录后点击上方下载按钮下载查看

网友评论0