META-INF.resources.asset.ajaxjs-ui.js.widgets.form.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ajaxjs-web Show documentation
Show all versions of ajaxjs-web Show documentation
AJAXJS Web aims to full-stack, not only the server-side framework, but also integrates the front-end library. It's written in HTML5 + Java, a successor to the JVM platform, efficient, secure, stable, cross-platform and many other advantages, but it abandoned the traditional enterprise architecture brought about by the large and bloated, emphasizing the lightweight, and fast, very suitable for the Internet fast application.
/**
* 表單控件
*/
// 图片验证码
Vue.component('aj-page-captcha', {
props : {
imgSrc : {
type: String, // 生成图片验证码地址
required: false,
},
fieldName : { // 提交的字段名
type: String,
required: false,
default : 'captcha'
}
},
template :
'\
\
\
\
\
',
methods : {
onClk : function(e) {
var img = e.target;
img.src = img.src.replace(/\?\d+$/, '') + '?' + new Date().valueOf();
}
}
});
// 日期选择器
Vue.component('aj-form-calendar', {
data: function() {
return {
year : 2017,
month : 2,
day : 1
};
},
props : {
},
template :
'',
// 2012-07-08
// firefox中解析 new Date('2012/12-23') 不兼容,提示invalid date 无效的日期
// Chrome下可以直接将其格式化成日期格式,且时分秒默认为零
// var arr = date.split('-'), now = new Date(arr[0], arr[1] - 1, arr[2], " ", " ", " ");
mounted : function () {
aj.apply(this, this.getDate('now'));
this.render();
},
methods : {
// 画日历
render : function () {
var el = this.$el;
var arr = this.getDateArr();// 用来保存日期列表
this.days = [];// 清空原来的日期对象列表
var frag = document.createDocumentFragment();// 插入日期
while (arr.length) {
var row = document.createElement("tr"); // 每个星期插入一个 tr
for (var i = 1; i <= 7; i++) { // 每个星期有7天
var cell = document.createElement("td");
if (arr.length) {
var d = arr.shift();
if (d) {
cell.innerHTML = d;
cell.className = 'day day_' + this.year + '-' + this.month + '-' + d;
cell.title = this.year + '-' + this.month + '-' + d;
this.days[d] = cell;
var on = new Date(this.year, this.month - 1, d);
// 判断是否今日
if (this.isSameDay(on, this.date)) {
cell.classList.add('onToday');
this.onToday && this.onToday(cell);
}
// 判断是否选择日期
this.selectDay && this.onSelectDay && this.isSameDay(on, this.selectDay) && this.onSelectDay(cell);
}
}
row.appendChild(cell);
}
frag.appendChild(row);
}
// 先清空内容再插入(ie的table不能用innerHTML)
// while (el.hasChildNodes())
// el.removeChild(el.firstChild);
var tbody = el.$("table tbody");
tbody.innerHTML = '';
tbody.appendChild(frag);
// el.querySelector(".idCalendarPre").onclick = this.PreMonth.bind(this);
// el.querySelector(".idCalendarNext").onclick = this.NextMonth.bind(this);
// el.querySelector(".idCalendarNow").onclick = this.NowMonth.bind(this);
this.onFinish && this.onFinish();
},
setYear : function(type) {
this.year = this.getDate(type).year;
},
nextYear : function() {
},
getDate: function (dateType) {
var now = new Date(), date, nowYear = now.getFullYear(), nowMonth = now.getMonth() + 1;
switch (dateType) {
case 'now':// 当前月
date = now;
break;
case 'preMonth':// 上一月
date = new Date(nowYear, nowMonth - 2, 1);
break;
case 'nextMonth':// 下一月
date = new Date(nowYear, nowMonth, 1);
break;
case 'preYear':// 上一年
date = new Date(nowYear - 1, nowMonth - 1, 1);
break;
case 'nextYear':// 下一年
date = new Date(nowYear + 1, nowMonth - 1, 1);
break;
}
return {
date : date,
year : date.getFullYear(),
month : date.getMonth() + 1,
};
},
getDateArr: function () {
var arr = [];
// 用当月第一天在一周中的日期值作为 当月离第一天的天数
for (var i = 1, firstDay = new Date(this.year, this.month - 1, 1).getDay(); i <= firstDay; i++)
arr.push(0);
// 用当月最后一天在一个月中的日期值作为当月的天数
for (var i = 1, monthDay = new Date(this.year, this.month, 0).getDate(); i <= monthDay; i++)
arr.push(i);
return arr;
},
pickDay : function(e) {
var el = e.target, date = el.title;
this.$emit('pick-date', date);
return date;
},
/**
* 判断是否同一日
*/
isSameDay: function (d1, d2) {
return (d1.getFullYear() == d2.getFullYear() && d1.getMonth() == d2.getMonth() && d1.getDate() == d2.getDate());
}
}
});
//帶有 input 輸入框的
Vue.component('aj-form-calendar-input', {
data : function(){
return {
date : this.fieldValue
}
},
props : {
fieldName : { // 表单 name,字段名
type: String,
required: true
},
fieldValue : {
type: String,// 表单值,可选的
required: false
},
positionFixed : Boolean // 是否采用固定定位
},
template :
'\
\
\
\
',
mounted : function() {
if(this.positionFixed) {
this.$el.$('.aj-form-calendar').classList.add('positionFixed');
}
},
methods : {
recEvent: function(date) {
this.date = date;
},
onMouseOver : function(e) {
if(this.positionFixed) {
var el = e.currentTarget;
var b = el.getBoundingClientRect();
var c = this.$el.$('.aj-form-calendar');
c.style.top = (b.top + el.clientHeight - 0) + 'px';
c.style.left = ((b.left - 0) + 0) + 'px';
}
}
}
});
//HTML 在綫編輯器
//注意:必须提供一个 包含有
Vue.component('aj-form-html-editor', {
template : '',
props : {
fieldName : { // 表单 name,字段名
type: String,
required: true
},
content : { // 内容
type: String,
required: false
},
basePath : { // iframe 的 路徑
type: String,
required: false,
default : ''
}
},
beforeCreate : function() {
var xhr = new XMLHttpRequest();
xhr.open("GET", this.ajResources.libraryUse + '/htmleditor-tag.htm', false);// 同步方式请求
xhr.send(null);
this.$options.template = xhr.responseText;
},
mounted : function() {
var el = this.$el;
this.iframeEl = el.$('iframe');
this.sourceEditor = el.$('textarea');
this.iframeWin = this.iframeEl.contentWindow;
this.mode = 'iframe'; // 当前可视化编辑 iframe|textarea
this.toolbarEl = el.$('.toolbar');
// 这个方法只能写在 onload 事件 不写 onload 里还不执行
this.iframeWin.onload = function() {
this.iframeDoc = this.iframeWin.document;
this.iframeDoc.designMode = 'on';
this.iframeBody = this.iframeDoc.body;
// 有内容
this.sourceEditor.value && this.setValue(this.sourceEditor.value);
}.bind(this);
},
methods: {
onToolBarClk : function(e) {
var el = e.target, clsName = el.className;
clsName = clsName.split(' ').shift();
switch(clsName) {
case 'createLink':
var result = prompt("请输入 URL 地址");
if(result)
this.format("createLink", result);
break;
case 'insertImage':
if(window.isCreate)
aj.alert.show('请保存记录后再上传图片。');
else {
var self = this;
App.$refs.uploadLayer.show(function(imgUrl) {
if(imgUrl)
self.format("insertImage", imgUrl);
});
}
break;
case 'switchMode':
this.setMode();
break;
default:
this.format(clsName);
}
},
format : function (type, para) {
this.iframeWin.focus();
if (!para) {
if (document.all)
this.iframeDoc.execCommand(type);
else
this.iframeDoc.execCommand(type, false, false);
} else
this.iframeDoc.execCommand(type, false, para);
this.iframeWin.focus();
},
insertEl : function (html) {// 重複?
this.iframeDoc.body.innerHTML = html;
},
// 設置 HTML
setValue : function(v) {
var self = this;
setTimeout(function() {
self.iframeWin.document.body.innerHTML = v;
// self.iframeBody.innerHTML = v;
}, 500);
},
// 獲取 HTML
getValue : function(cfg) {
var result = this.iframeBody.innerHTML;
if(cfg && cfg.cleanWord)
result = this.cleanPaste(result);
if(cfg && cfg.encode)
result = encodeURIComponent(result);
return result;
},
// MSWordHtmlCleaners.js https://gist.github.com/ronanguilloux/2915995
cleanPaste : function(html) {
// Remove additional MS Word content
html = html.replace(/<(\/)*(\\?xml:|meta|link|span|font|del|ins|st1:|[ovwxp]:)((.|\s)*?)>/gi, ''); // Unwanted tags
html = html.replace(/(class|style|type|start)=("(.*?)"|(\w*))/gi, ''); // Unwanted sttributes
html = html.replace(/