加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
monk.ui.js 28.03 KB
一键复制 编辑 原始数据 按行查看 历史
百小僧 提交于 2016-10-14 20:57 . v0.1.3
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
/*!
* monk.ui.js
* version: 0.1.3
* author: 百小僧(QQ:8020292)
* site:http://www.baisoft.org
* QQ群:123049073
* https://github.com/MonkSoul/Monk.UI/
*/
; !function (factory) {
"use strict";
if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {
var target = module['exports'] || exports;
factory(target);
} else if (typeof define === 'function' && define['amd']) {
define(['exports'], factory);
} else {
factory(window['monk'] = {});
}
}(function (HExports) {
var exports = typeof HExports !== 'undefined' ? HExports : {};
exports.v = "0.1.3";
// 文本框初始化
exports.inputInit = function () {
// 文本框
var _input = $(".monk-form-input,.monk-form-textarea");
_input.each(function () {
if ($(this).siblings(".monk-iconfont.icon-monk-required").length > 0) {
$(this).siblings(".monk-iconfont.icon-monk-required").eq(0).css("right", "-40px");
}
});
_input.on({
focus: function () {
$(this).parent(".monk-form-wrap").addClass("focus");
// 选中文本
$(this).select();
},
blur: function () {
if (!$(this).parent(".monk-form-wrap").hasClass("monk-form-time-wrap")) {
$(this).parent(".monk-form-wrap").removeClass("focus");
}
},
keyup: function () {
var clearInput = $(this).siblings(".monk-clear-input");
if (clearInput) {
if ($.trim($(this).val()) !== "") {
clearInput.css({ "visibility": "visible" });
if ($(this).siblings(".monk-iconfont.icon-monk-required").length > 0) {
$(this).siblings(".monk-iconfont.icon-monk-required").eq(0).css("right", "0");
}
}
else {
clearInput.css({ "visibility": "hidden" });
if ($(this).siblings(".monk-iconfont.icon-monk-required").length > 0) {
$(this).siblings(".monk-iconfont.icon-monk-required").eq(0).css("right", "-40px");
}
}
}
}
});
$(".monk-clear-input").on({
click: function () {
$(this).css({ "visibility": "hidden" });
$(this).siblings(".monk-form-input,.monk-form-textarea").each(function () {
var readonly = $(this).attr("readonly");
var disabled = $(this).attr("disabled");
if (readonly != "readonly" && disabled != "disabled") {
$(this).val("");
}
});
if ($(this).siblings(".monk-iconfont.icon-monk-required").length > 0) {
$(this).siblings(".monk-iconfont.icon-monk-required").eq(0).css("right", "-40px");
}
}
});
};
// 复选框初始化
exports.checkboxInit = function () {
$(".monk-form-checkbox-list").each(function () {
var that = $(this);
var html = "";
$(this).children(".monk-checkbox").each(function () {
var value = $(this).val();
var text = $(this).attr("text");
var checked = $(this).attr("checked");
var readonly = $(this).attr("readonly");
var disabled = $(this).attr("disabled");
var name = $(this).attr("name");
var valueHtml = value ? 'data-value="' + value + '"' : 'data-value=""';
var nameHtml = name ? 'data-name="' + name + '" ' : '';
var textHtml = text ? text : "value";
var checkedHtml = checked ? 'checked="checked"' : '';
var readonlyHtml = readonly ? 'readonly="readonly"' : '';
var disabledHtml = disabled ? 'disabled="disabled"' : '';
html += '<div class="monk-form-wrap monk-form-checkbox-wrap" ' + nameHtml + ' ' + checkedHtml + ' ' + readonlyHtml + ' ' + disabledHtml + ' ' + valueHtml + '>';
html += '<span class="monk-form-checkbox">' + textHtml + '</span>';
html += '<span class="monk-iconfont border-left icon-monk-dagou"></span>';
html += '</div>\r\n';
});
that.prepend(html);
});
// 复选框
var _checkbox = $(".monk-form-checkbox-wrap");
_checkbox.on({
click: function () {
var readonly = $(this).attr("readonly");
var disabled = $(this).attr("disabled");
var name = $(this).attr("data-name");
var value = $(this).attr("data-value");
if (readonly != "readonly" && disabled != "disabled") {
var checkbox = $(this).siblings(".monk-checkbox[name='" + name + "'][value='" + value + "']").eq(0);
if ($(this).attr("checked") == "checked") {
$(this).removeAttr("checked");
checkbox.prop("checked", false);
}
else {
$(this).attr("checked", "checked");
checkbox.prop("checked", true);
}
}
}
});
// 绑定状态改变事件
$(".monk-checkbox").on({
change: function () {
var name = $(this).attr("name");
var value = $(this).attr("value");
var niceObj = $(this).siblings(".monk-form-checkbox-wrap[data-name='" + name + "'][data-value='" + value + "']").eq(0);
if ($(this).is(":checked")) {
niceObj.attr("checked", "checked");
}
else {
niceObj.removeAttr("checked");
}
},
click: function () {
var readonly = $(this).attr("readonly");
if (readonly) {
return false;
}
},
focus: function () {
var name = $(this).attr("name");
var value = $(this).attr("value");
var niceObj = $(this).siblings(".monk-form-checkbox-wrap[data-name='" + name + "'][data-value='" + value + "']").eq(0);
niceObj.addClass("focus");
},
blur: function () {
var name = $(this).attr("name");
var value = $(this).attr("value");
var niceObj = $(this).siblings(".monk-form-checkbox-wrap[data-name='" + name + "'][data-value='" + value + "']").eq(0);
niceObj.removeClass("focus");
}
});
};
// 切换初始化
exports.switchInit = function () {
$(".monk-form-switch-list").each(function () {
var that = $(this);
var html = "";
$(this).children(".monk-switch").each(function () {
var text = $(this).attr("text");
var value = $(this).val();
var checked = $(this).attr("checked");
var readonly = $(this).attr("readonly");
var disabled = $(this).attr("disabled");
var name = $(this).attr("name");
var valueHtml = value ? 'data-value="' + value + '"' : 'data-value=""';
var nameHtml = name ? 'data-name="' + name + '" ' : '';
var checkedHtml = checked ? 'checked="checked"' : '';
var readonlyHtml = readonly ? 'readonly="readonly"' : '';
var disabledHtml = disabled ? 'disabled="disabled"' : '';
html += '<div class="monk-form-wrap monk-form-switch-wrap" ' + nameHtml + ' ' + checkedHtml + ' ' + readonlyHtml + ' ' + disabledHtml + ' ' + valueHtml + '>';
html += '<span class="monk-switch-text">' + text + '</span>';
html += '<span class="monk-iconfont border-right icon-monk-dacha"></span>';
html += '</div>\r\n';
});
that.prepend(html);
});
// 切换框
var _switch = $(".monk-form-switch-wrap");
_switch.on({
click: function () {
var readonly = $(this).attr("readonly");
var disabled = $(this).attr("disabled");
var name = $(this).attr("data-name");
var value = $(this).attr("data-value");
if (readonly != "readonly" && disabled != "disabled") {
var switchbox = $(this).siblings(".monk-switch[name='" + name + "'][value='" + value + "']").eq(0);
if ($(this).attr("checked") == "checked") {
$(this).removeAttr("checked");
switchbox.prop("checked", false);
}
else {
$(this).attr("checked", "checked");
switchbox.prop("checked", true);
}
}
}
});
// 绑定状态改变事件
$(".monk-switch").on({
change: function () {
var name = $(this).attr("name");
var value = $(this).attr("value");
var niceObj = $(this).siblings(".monk-form-switch-wrap[data-name='" + name + "'][data-value='" + value + "']").eq(0);
if ($(this).is(":checked")) {
niceObj.attr("checked", "checked");
}
else {
niceObj.removeAttr("checked");
}
},
click: function () {
var readonly = $(this).attr("readonly");
if (readonly) {
return false;
}
},
focus: function () {
var name = $(this).attr("name");
var value = $(this).attr("value");
var niceObj = $(this).siblings(".monk-form-switch-wrap[data-name='" + name + "'][data-value='" + value + "']").eq(0);
niceObj.addClass("focus");
},
blur: function () {
var name = $(this).attr("name");
var value = $(this).attr("value");
var niceObj = $(this).siblings(".monk-form-switch-wrap[data-name='" + name + "'][data-value='" + value + "']").eq(0);
niceObj.removeClass("focus");
}
});
};
// 单选框初始化
exports.radioInit = function () {
$(".monk-form-radio-list").each(function () {
var that = $(this);
var html = "";
$(this).children(".monk-radio").each(function () {
var value = $(this).val();
var text = $(this).attr("text");
var checked = $(this).attr("checked");
var readonly = $(this).attr("readonly");
var disabled = $(this).attr("disabled");
var name = $(this).attr("name");
var valueHtml = value ? 'data-value="' + value + '"' : 'data-value=""';
var nameHtml = name ? 'data-name="' + name + '" ' : '';
var textHtml = text ? text : "value";
var checkedHtml = checked ? 'checked="checked"' : '';
var readonlyHtml = readonly ? 'readonly="readonly"' : '';
var disabledHtml = disabled ? 'disabled="disabled"' : '';
html += '<div class="monk-form-wrap monk-form-radio-wrap" ' + nameHtml + ' ' + checkedHtml + ' ' + readonlyHtml + ' ' + disabledHtml + ' ' + valueHtml + '>';
html += '<span class="monk-iconfont icon-monk-radio"></span>';
html += '<span class="monk-form-radio">' + textHtml + '</span>';
html += '</div>\r\n';
});
that.prepend(html);
});
// 单选框
var _radio = $(".monk-form-radio-wrap");
_radio.on({
click: function () {
var readonly = $(this).attr("readonly");
var disabled = $(this).attr("disabled");
var name = $(this).attr("data-name");
var value = $(this).attr("data-value");
if (readonly != "readonly" && disabled != "disabled") {
var radio = $(this).siblings(".monk-radio[name='" + name + "'][value='" + value + "']").eq(0);
$(this).attr("checked", "checked").siblings(".monk-form-radio-wrap[data-name='" + name + "']").removeAttr("checked");
radio.prop("checked", true);
}
}
});
// 绑定状态改变事件
$(".monk-radio").on({
click: function () {
var readonly = $(this).attr("readonly");
if (readonly) {
return false;
}
var name = $(this).attr("name");
var value = $(this).attr("value");
var niceObj = $(this).siblings(".monk-form-radio-wrap[data-name='" + name + "'][data-value='" + value + "']").eq(0);
if ($(this).is(":checked")) {
niceObj.attr("checked", "checked").siblings(".monk-form-radio-wrap[data-name='" + name + "']").removeAttr("checked");;
}
},
focus: function () {
var name = $(this).attr("name");
var value = $(this).attr("value");
var niceObj = $(this).siblings(".monk-form-radio-wrap[data-name='" + name + "'][data-value='" + value + "']").eq(0);
niceObj.addClass("focus");
},
blur: function () {
var name = $(this).attr("name");
var value = $(this).attr("value");
var niceObj = $(this).siblings(".monk-form-radio-wrap[data-name='" + name + "'][data-value='" + value + "']").eq(0);
niceObj.removeClass("focus");
}
});
};
// 初始化下拉
exports.selectInit = function () {
$(".monk-form-select").each(function () {
var that = $(this);
var html = "";
that.find(".monk-select").children().each(function () {
if ($(this).is("option")) {
var text = $(this).text();
var value = $(this).attr("value");
html += '<div class="monk-form-option" data-value="' + value + '">' + text + '</div>';
}
else if ($(this).is("optgroup")) {
var label = $(this).attr("label");
html += '<div class="monk-form-optgroup" data-value="' + value + '">' + label + '</div>';
}
});
that.prepend(html);
// 设置选中的option
var select = that.children(".monk-select");
var selectedOption = select.children("option:selected");
var value = select.val();
var text = selectedOption.text();
var niceOption = $(this).children(".monk-form-option[data-value='" + value + "']:contains(" + text + ")").eq(0);
niceOption.attr("selected", "selected").siblings(".monk-form-option").removeAttr("selected");
var input = select.parent().prev(".monk-form-select-wrap").children(".monk-form-input");
input.val(text);
});
// 搜索下拉
function _selectInputSearch(that) {
var value = $.trim(that.val());
var select = that.parent(".monk-form-select-wrap").next(".monk-form-select");
var option = select.children(".monk-form-option:contains(" + value + ")");
if (option && option.eq(0)) {
var scrollTop = option.eq(0).position().top;
select.animate({
scrollTop: select.scrollTop() + scrollTop
}, 0);
}
}
// 下拉框
var _selectInput = $(".monk-form-select-wrap .monk-form-input");
var _selectOption = $(".monk-form-option");
_selectInput.on({
focus: function () {
var inputWrap = $(this).parent(".monk-form-select-wrap");
var readonly = $(this).attr("readonly");
if (readonly != "readonly") {
inputWrap.next(".monk-form-select").removeClass("monk-none");
}
_selectInputSearch($(this));
},
blur: function () {
var select = $(this).parent(".monk-form-select-wrap").next(".monk-form-select");
var option = select.children(".monk-form-option[selected='selected']");
$(this).val(option.text());
$(this).parent(".monk-form-select-wrap").next(".monk-form-select").addClass("monk-none");
},
keyup: function () {
_selectInputSearch($(this));
}
});
_selectOption.on({
mousedown: function () {
var value = $(this).attr("data-value");
var text = $(this).text();
var select = $(this).parent(".monk-form-select");
var input = select.prev(".monk-form-select-wrap").children(".monk-form-input");
var scrollTop = $(this).position().top;
$(this).attr("selected", "selected").siblings(".monk-form-option").removeAttr("selected");
input.val(text);
select.animate({
scrollTop: select.scrollTop() + scrollTop
}, 0);
var realySelect = select.children(".monk-select");
realySelect.val(value);
}
});
// 点击图标
$(".monk-select-arrow").on({
mousedown: function () {
var input = $(this).siblings(".monk-form-input");
var readonly = input.attr("readonly");
var disabled = input.attr("disabled");
if (readonly != "readonly" && disabled != "disabled") {
if ($(this).parent(".monk-form-wrap").next(".monk-form-select").hasClass("monk-none")) {
setTimeout(function () {
input.focus();
}, 100);
}
else {
input.blur();
}
}
}
});
$(".monk-select").on({
change: function () {
var value = $(this).val();
var text = $(this).children("option:selected").text();
var parentSelect = $(this).parent(".monk-form-select");
var input = parentSelect.prev(".monk-form-select-wrap").children(".monk-form-input");
var option = $(this).siblings(".monk-form-option[data-value='" + value + "']:contains(" + text + ")").eq(0);
option.attr("selected", "selected").siblings(".monk-form-option").removeAttr("selected");
var scrollTop = option.position().top;
parentSelect.animate({
scrollTop: parentSelect.scrollTop() + scrollTop
}, 0);
input.val(text);
},
focus: function () {
var parentSelect = $(this).parent(".monk-form-select");
var input = parentSelect.prev(".monk-form-select-wrap").children(".monk-form-input");
input.focus();
}
});
};
// 初始化时间
exports.timeInit = function () {
function createTimeTags() {
var html = '<div class="monk-form-time monk-none">';
html += '<div class="monk-form-time-title">';
html += '<span>小时</span>';
html += '<span>分钟</span>';
html += '<span>秒钟</span>';
html += '</div>';
html += '<div class="monk-form-time-list">';
html += '<div class="monk-form-time-item">';
// 生成小时
for (var i = 0; i < 24; i++) {
if (i < 10) {
html += '<span class="monk-form-time-hour">0' + i + '</span>';
}
else {
html += '<span class="monk-form-time-hour">' + i + '</span>';
}
}
html += '</div>';
html += '<div class="monk-form-time-item">';
// 生成分钟
for (var i = 0; i < 60; i++) {
if (i < 10) {
html += '<span class="monk-form-time-minute">0' + i + '</span>';
}
else {
html += '<span class="monk-form-time-minute">' + i + '</span>';
}
}
html += '</div>';
html += '<div class="monk-form-time-item">';
// 生成秒
for (var i = 0; i < 60; i++) {
if (i < 10) {
html += '<span class="monk-form-time-second">0' + i + '</span>';
}
else {
html += '<span class="monk-form-time-second">' + i + '</span>';
}
}
html += '</div>';
html += '</div>';
html += '<div class="monk-form-time-btn"><span>当前时间</span><span>关闭</span></div>';
html += '</div>';
return html;
}
// 开始生成
$(".monk-form-time-wrap").each(function () {
var input = $(this).children(".monk-form-input");
var value = input.val();
if (!(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/.test(value))) {
input.val("00:00:00");
}
$(this).after(createTimeTags());
});
// 时间选择
var _timeInput = $(".monk-form-time-wrap .monk-form-input");
var _timeCurrentBtn = $(".monk-form-time-btn span:eq(0)");
var _timeBtn = $(".monk-form-time-btn span:eq(1)");
var _timeHour = $(".monk-form-time-hour");
var _timeMinute = $(".monk-form-time-minute");
var _timeSecond = $(".monk-form-time-second");
_timeInput.on({
focus: function () {
var timeObj = $(this).parent(".monk-form-time-wrap").next(".monk-form-time");
timeObj.removeClass("monk-none");
var value = $(this).val();
var timeArr = value.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/);
var time = timeArr[0];
var hour = timeArr[1];
var minute = timeArr[3];
var second = timeArr[4];
// 设置小时选中
var hourObj = timeObj.find(".monk-form-time-item .monk-form-time-hour:contains(" + hour + ")");
hourObj.addClass("selected").siblings(".monk-form-time-hour").removeClass("selected");
hourObj.parent().animate({
scrollTop: hourObj.parent().scrollTop() + hourObj.position().top
}, 0);
// 设置分钟选中
var minuteObj = timeObj.find(".monk-form-time-item .monk-form-time-minute:contains(" + minute + ")");
minuteObj.addClass("selected").siblings(".monk-form-time-minute").removeClass("selected");
minuteObj.parent().animate({
scrollTop: minuteObj.parent().scrollTop() + minuteObj.position().top
}, 0);
// 设置秒钟选中
var secondObj = timeObj.find(".monk-form-time-item .monk-form-time-second:contains(" + second + ")");
secondObj.addClass("selected").siblings(".monk-form-time-second").removeClass("selected");
secondObj.parent().animate({
scrollTop: secondObj.parent().scrollTop() + secondObj.position().top
}, 0);
}
});
// 当前时间
_timeCurrentBtn.on({
click: function () {
var date = new Date();
var hours = date.getHours();
if (hours < 10) {
hours = "0" + hours;
}
var minute = date.getMinutes();
if (minute < 10) {
minute = "0" + minute;
}
var second = date.getSeconds();
if (second < 10) {
second = "0" + second;
}
var value = hours + ":" + minute + ":" + second;
var time = $(this).parent(".monk-form-time-btn").parent(".monk-form-time");
var inputWrap = time.prev(".monk-form-time-wrap");
var input = inputWrap.children(".monk-form-input");
input.val(value);
_timeBtn.click();
}
});
// 关闭
_timeBtn.on({
click: function () {
var time = $(this).parent(".monk-form-time-btn").parent(".monk-form-time");
var inputWrap = time.prev(".monk-form-time-wrap");
var input = inputWrap.children(".monk-form-input");
time.addClass("monk-none");
inputWrap.removeClass("focus");
input.blur();
}
});
function selectTime(that, type) {
var index = 0;
switch (type) {
case "hour":
index = 0;
break;
case "minute":
index = 1;
break;
case "second":
index = 2;
break;
}
var timeItem = that.parent();
var value = $.trim(that.text());
var timeParent = that.parents(".monk-form-time");
var timeWrap = timeParent.prev(".monk-form-time-wrap");
var timeInput = timeWrap.children(".monk-form-input");
var inputValue = timeInput.val();
if (!(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/.test(inputValue))) {
timeInput.val("00:00:00");
}
var timeArr = inputValue.split(":");
if (timeArr.length == 0) {
timeArr = ["00", "00", "00"];
}
else if (timeArr.length == 1) {
timeArr.push("00");
timeArr.push("00");
}
else if (timeArr.length == 2) {
timeArr.push("00");
}
else {
}
that.addClass("selected").siblings(".monk-form-time-" + type).removeClass("selected");
timeArr[index] = value;
timeInput.val(timeArr.join(":"));
}
_timeHour.on({
click: function () {
selectTime($(this), "hour");
}
});
_timeMinute.on({
click: function () {
selectTime($(this), "minute");
}
});
_timeSecond.on({
click: function () {
selectTime($(this), "second");
}
});
// 点击图标
$(".icon-monk-time").on({
mousedown: function () {
var input = $(this).siblings(".monk-form-input");
var disabled = input.attr("disabled");
if (disabled != "disabled") {
if ($(this).parent(".monk-form-wrap").next(".monk-form-time").hasClass("monk-none")) {
setTimeout(function () {
input.focus();
}, 100);
}
}
}
});
};
// 文件选择
exports.fileInit = function () {
$(".monk-file").on({
change: function () {
var value = $(this).val();
var input = $(this).parent(".monk-form-image-wrap").prev(".monk-form-wrap").children(".monk-form-input");
input.val(value);
}
});
};
// 初始化
exports.init = function () {
exports.inputInit();
exports.checkboxInit();
exports.switchInit();
exports.radioInit();
exports.selectInit();
exports.timeInit();
exports.fileInit();
};
exports.init();
});
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化