加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
monk.ui.min.js 13.60 KB
一键复制 编辑 原始数据 按行查看 历史
百小僧 提交于 2016-10-14 20:57 . v0.1.3
/*!
* monk.ui.js
* version: 0.1.3
* author: 百小僧(QQ:8020292)
* site:http://www.baisoft.org
* QQ群:123049073
* https://github.com/MonkSoul/Monk.UI/
*/
!function(n){"use strict";if(typeof require=="function"&&typeof exports=="object"&&typeof module=="object"){var t=module.exports||exports;n(t)}else typeof define=="function"&&define.amd?define(["exports"],n):n(window.monk={})}(function(n){var t=typeof n!="undefined"?n:{};t.v="0.1.3";t.inputInit=function(){var n=$(".monk-form-input,.monk-form-textarea");n.each(function(){$(this).siblings(".monk-iconfont.icon-monk-required").length>0&&$(this).siblings(".monk-iconfont.icon-monk-required").eq(0).css("right","-40px")});n.on({focus:function(){$(this).parent(".monk-form-wrap").addClass("focus");$(this).select()},blur:function(){$(this).parent(".monk-form-wrap").hasClass("monk-form-time-wrap")||$(this).parent(".monk-form-wrap").removeClass("focus")},keyup:function(){var n=$(this).siblings(".monk-clear-input");n&&($.trim($(this).val())!==""?(n.css({visibility:"visible"}),$(this).siblings(".monk-iconfont.icon-monk-required").length>0&&$(this).siblings(".monk-iconfont.icon-monk-required").eq(0).css("right","0")):(n.css({visibility:"hidden"}),$(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 n=$(this).attr("readonly"),t=$(this).attr("disabled");n!="readonly"&&t!="disabled"&&$(this).val("")});$(this).siblings(".monk-iconfont.icon-monk-required").length>0&&$(this).siblings(".monk-iconfont.icon-monk-required").eq(0).css("right","-40px")}})};t.checkboxInit=function(){$(".monk-form-checkbox-list").each(function(){var t=$(this),n="";$(this).children(".monk-checkbox").each(function(){var t=$(this).val(),i=$(this).attr("text"),u=$(this).attr("checked"),f=$(this).attr("readonly"),e=$(this).attr("disabled"),r=$(this).attr("name"),o=t?'data-value="'+t+'"':'data-value=""',s=r?'data-name="'+r+'" ':"",h=i?i:"value",c=u?'checked="checked"':"",l=f?'readonly="readonly"':"",a=e?'disabled="disabled"':"";n+='<div class="monk-form-wrap monk-form-checkbox-wrap" '+s+" "+c+" "+l+" "+a+" "+o+">";n+='<span class="monk-form-checkbox">'+h+"<\/span>";n+='<span class="monk-iconfont border-left icon-monk-dagou"><\/span>';n+="<\/div>\r\n"});t.prepend(n)});var n=$(".monk-form-checkbox-wrap");n.on({click:function(){var t=$(this).attr("readonly"),i=$(this).attr("disabled"),r=$(this).attr("data-name"),u=$(this).attr("data-value"),n;t!="readonly"&&i!="disabled"&&(n=$(this).siblings(".monk-checkbox[name='"+r+"'][value='"+u+"']").eq(0),$(this).attr("checked")=="checked"?($(this).removeAttr("checked"),n.prop("checked",!1)):($(this).attr("checked","checked"),n.prop("checked",!0)))}});$(".monk-checkbox").on({change:function(){var t=$(this).attr("name"),i=$(this).attr("value"),n=$(this).siblings(".monk-form-checkbox-wrap[data-name='"+t+"'][data-value='"+i+"']").eq(0);$(this).is(":checked")?n.attr("checked","checked"):n.removeAttr("checked")},click:function(){var n=$(this).attr("readonly");if(n)return!1},focus:function(){var n=$(this).attr("name"),t=$(this).attr("value"),i=$(this).siblings(".monk-form-checkbox-wrap[data-name='"+n+"'][data-value='"+t+"']").eq(0);i.addClass("focus")},blur:function(){var n=$(this).attr("name"),t=$(this).attr("value"),i=$(this).siblings(".monk-form-checkbox-wrap[data-name='"+n+"'][data-value='"+t+"']").eq(0);i.removeClass("focus")}})};t.switchInit=function(){$(".monk-form-switch-list").each(function(){var t=$(this),n="";$(this).children(".monk-switch").each(function(){var r=$(this).attr("text"),t=$(this).val(),u=$(this).attr("checked"),f=$(this).attr("readonly"),e=$(this).attr("disabled"),i=$(this).attr("name"),o=t?'data-value="'+t+'"':'data-value=""',s=i?'data-name="'+i+'" ':"",h=u?'checked="checked"':"",c=f?'readonly="readonly"':"",l=e?'disabled="disabled"':"";n+='<div class="monk-form-wrap monk-form-switch-wrap" '+s+" "+h+" "+c+" "+l+" "+o+">";n+='<span class="monk-switch-text">'+r+"<\/span>";n+='<span class="monk-iconfont border-right icon-monk-dacha"><\/span>';n+="<\/div>\r\n"});t.prepend(n)});var n=$(".monk-form-switch-wrap");n.on({click:function(){var t=$(this).attr("readonly"),i=$(this).attr("disabled"),r=$(this).attr("data-name"),u=$(this).attr("data-value"),n;t!="readonly"&&i!="disabled"&&(n=$(this).siblings(".monk-switch[name='"+r+"'][value='"+u+"']").eq(0),$(this).attr("checked")=="checked"?($(this).removeAttr("checked"),n.prop("checked",!1)):($(this).attr("checked","checked"),n.prop("checked",!0)))}});$(".monk-switch").on({change:function(){var t=$(this).attr("name"),i=$(this).attr("value"),n=$(this).siblings(".monk-form-switch-wrap[data-name='"+t+"'][data-value='"+i+"']").eq(0);$(this).is(":checked")?n.attr("checked","checked"):n.removeAttr("checked")},click:function(){var n=$(this).attr("readonly");if(n)return!1},focus:function(){var n=$(this).attr("name"),t=$(this).attr("value"),i=$(this).siblings(".monk-form-switch-wrap[data-name='"+n+"'][data-value='"+t+"']").eq(0);i.addClass("focus")},blur:function(){var n=$(this).attr("name"),t=$(this).attr("value"),i=$(this).siblings(".monk-form-switch-wrap[data-name='"+n+"'][data-value='"+t+"']").eq(0);i.removeClass("focus")}})};t.radioInit=function(){$(".monk-form-radio-list").each(function(){var t=$(this),n="";$(this).children(".monk-radio").each(function(){var t=$(this).val(),i=$(this).attr("text"),u=$(this).attr("checked"),f=$(this).attr("readonly"),e=$(this).attr("disabled"),r=$(this).attr("name"),o=t?'data-value="'+t+'"':'data-value=""',s=r?'data-name="'+r+'" ':"",h=i?i:"value",c=u?'checked="checked"':"",l=f?'readonly="readonly"':"",a=e?'disabled="disabled"':"";n+='<div class="monk-form-wrap monk-form-radio-wrap" '+s+" "+c+" "+l+" "+a+" "+o+">";n+='<span class="monk-iconfont icon-monk-radio"><\/span>';n+='<span class="monk-form-radio">'+h+"<\/span>";n+="<\/div>\r\n"});t.prepend(n)});var n=$(".monk-form-radio-wrap");n.on({click:function(){var i=$(this).attr("readonly"),r=$(this).attr("disabled"),n=$(this).attr("data-name"),u=$(this).attr("data-value"),t;i!="readonly"&&r!="disabled"&&(t=$(this).siblings(".monk-radio[name='"+n+"'][value='"+u+"']").eq(0),$(this).attr("checked","checked").siblings(".monk-form-radio-wrap[data-name='"+n+"']").removeAttr("checked"),t.prop("checked",!0))}});$(".monk-radio").on({click:function(){var t=$(this).attr("readonly");if(t)return!1;var n=$(this).attr("name"),i=$(this).attr("value"),r=$(this).siblings(".monk-form-radio-wrap[data-name='"+n+"'][data-value='"+i+"']").eq(0);$(this).is(":checked")&&r.attr("checked","checked").siblings(".monk-form-radio-wrap[data-name='"+n+"']").removeAttr("checked")},focus:function(){var n=$(this).attr("name"),t=$(this).attr("value"),i=$(this).siblings(".monk-form-radio-wrap[data-name='"+n+"'][data-value='"+t+"']").eq(0);i.addClass("focus")},blur:function(){var n=$(this).attr("name"),t=$(this).attr("value"),i=$(this).siblings(".monk-form-radio-wrap[data-name='"+n+"'][data-value='"+t+"']").eq(0);i.removeClass("focus")}})};t.selectInit=function(){function n(n){var u=$.trim(n.val()),t=n.parent(".monk-form-select-wrap").next(".monk-form-select"),i=t.children(".monk-form-option:contains("+u+")"),r;i&&i.eq(0)&&(r=i.eq(0).position().top,t.animate({scrollTop:t.scrollTop()+r},0))}$(".monk-form-select").each(function(){var n=$(this),t="",u;n.find(".monk-select").children().each(function(){var i,n,r;$(this).is("option")?(i=$(this).text(),n=$(this).attr("value"),t+='<div class="monk-form-option" data-value="'+n+'">'+i+"<\/div>"):$(this).is("optgroup")&&(r=$(this).attr("label"),t+='<div class="monk-form-optgroup" data-value="'+n+'">'+r+"<\/div>")});n.prepend(t);var i=n.children(".monk-select"),f=i.children("option:selected"),e=i.val(),r=f.text(),o=$(this).children(".monk-form-option[data-value='"+e+"']:contains("+r+")").eq(0);o.attr("selected","selected").siblings(".monk-form-option").removeAttr("selected");u=i.parent().prev(".monk-form-select-wrap").children(".monk-form-input");u.val(r)});var t=$(".monk-form-select-wrap .monk-form-input"),i=$(".monk-form-option");t.on({focus:function(){var t=$(this).parent(".monk-form-select-wrap"),i=$(this).attr("readonly");i!="readonly"&&t.next(".monk-form-select").removeClass("monk-none");n($(this))},blur:function(){var n=$(this).parent(".monk-form-select-wrap").next(".monk-form-select"),t=n.children(".monk-form-option[selected='selected']");$(this).val(t.text());$(this).parent(".monk-form-select-wrap").next(".monk-form-select").addClass("monk-none")},keyup:function(){n($(this))}});i.on({mousedown:function(){var i=$(this).attr("data-value"),r=$(this).text(),n=$(this).parent(".monk-form-select"),u=n.prev(".monk-form-select-wrap").children(".monk-form-input"),f=$(this).position().top,t;$(this).attr("selected","selected").siblings(".monk-form-option").removeAttr("selected");u.val(r);n.animate({scrollTop:n.scrollTop()+f},0);t=n.children(".monk-select");t.val(i)}});$(".monk-select-arrow").on({mousedown:function(){var n=$(this).siblings(".monk-form-input"),t=n.attr("readonly"),i=n.attr("disabled");t!="readonly"&&i!="disabled"&&($(this).parent(".monk-form-wrap").next(".monk-form-select").hasClass("monk-none")?setTimeout(function(){n.focus()},100):n.blur())}});$(".monk-select").on({change:function(){var u=$(this).val(),t=$(this).children("option:selected").text(),n=$(this).parent(".monk-form-select"),f=n.prev(".monk-form-select-wrap").children(".monk-form-input"),i=$(this).siblings(".monk-form-option[data-value='"+u+"']:contains("+t+")").eq(0),r;i.attr("selected","selected").siblings(".monk-form-option").removeAttr("selected");r=i.position().top;n.animate({scrollTop:n.scrollTop()+r},0);f.val(t)},focus:function(){var n=$(this).parent(".monk-form-select"),t=n.prev(".monk-form-select-wrap").children(".monk-form-input");t.focus()}})};t.timeInit=function(){function i(){var n='<div class="monk-form-time monk-none">',t;for(n+='<div class="monk-form-time-title">',n+="<span>小时<\/span>",n+="<span>分钟<\/span>",n+="<span>秒钟<\/span>",n+="<\/div>",n+='<div class="monk-form-time-list">',n+='<div class="monk-form-time-item">',t=0;t<24;t++)n+=t<10?'<span class="monk-form-time-hour">0'+t+"<\/span>":'<span class="monk-form-time-hour">'+t+"<\/span>";for(n+="<\/div>",n+='<div class="monk-form-time-item">',t=0;t<60;t++)n+=t<10?'<span class="monk-form-time-minute">0'+t+"<\/span>":'<span class="monk-form-time-minute">'+t+"<\/span>";for(n+="<\/div>",n+='<div class="monk-form-time-item">',t=0;t<60;t++)n+=t<10?'<span class="monk-form-time-second">0'+t+"<\/span>":'<span class="monk-form-time-second">'+t+"<\/span>";return n+="<\/div>",n+="<\/div>",n+='<div class="monk-form-time-btn"><span>当前时间<\/span><span>关闭<\/span><\/div>',n+"<\/div>"}function n(n,t){var r=0,i;switch(t){case"hour":r=0;break;case"minute":r=1;break;case"second":r=2}var h=n.parent(),e=$.trim(n.text()),o=n.parents(".monk-form-time"),s=o.prev(".monk-form-time-wrap"),u=s.children(".monk-form-input"),f=u.val();/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/.test(f)||u.val("00:00:00");i=f.split(":");i.length==0?i=["00","00","00"]:i.length==1?(i.push("00"),i.push("00")):i.length==2&&i.push("00");n.addClass("selected").siblings(".monk-form-time-"+t).removeClass("selected");i[r]=e;u.val(i.join(":"))}$(".monk-form-time-wrap").each(function(){var n=$(this).children(".monk-form-input"),t=n.val();/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/.test(t)||n.val("00:00:00");$(this).after(i())});var r=$(".monk-form-time-wrap .monk-form-input"),u=$(".monk-form-time-btn span:eq(0)"),t=$(".monk-form-time-btn span:eq(1)"),f=$(".monk-form-time-hour"),e=$(".monk-form-time-minute"),o=$(".monk-form-time-second");r.on({focus:function(){var i=$(this).parent(".monk-form-time-wrap").next(".monk-form-time"),n,t;i.removeClass("monk-none");var f=$(this).val(),r=f.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/),h=r[0],e=r[1],o=r[3],s=r[4],u=i.find(".monk-form-time-item .monk-form-time-hour:contains("+e+")");u.addClass("selected").siblings(".monk-form-time-hour").removeClass("selected");u.parent().animate({scrollTop:u.parent().scrollTop()+u.position().top},0);n=i.find(".monk-form-time-item .monk-form-time-minute:contains("+o+")");n.addClass("selected").siblings(".monk-form-time-minute").removeClass("selected");n.parent().animate({scrollTop:n.parent().scrollTop()+n.position().top},0);t=i.find(".monk-form-time-item .monk-form-time-second:contains("+s+")");t.addClass("selected").siblings(".monk-form-time-second").removeClass("selected");t.parent().animate({scrollTop:t.parent().scrollTop()+t.position().top},0)}});u.on({click:function(){var u=new Date,r=u.getHours(),n,i;r<10&&(r="0"+r);n=u.getMinutes();n<10&&(n="0"+n);i=u.getSeconds();i<10&&(i="0"+i);var f=r+":"+n+":"+i,e=$(this).parent(".monk-form-time-btn").parent(".monk-form-time"),o=e.prev(".monk-form-time-wrap"),s=o.children(".monk-form-input");s.val(f);t.click()}});t.on({click:function(){var n=$(this).parent(".monk-form-time-btn").parent(".monk-form-time"),t=n.prev(".monk-form-time-wrap"),i=t.children(".monk-form-input");n.addClass("monk-none");t.removeClass("focus");i.blur()}});f.on({click:function(){n($(this),"hour")}});e.on({click:function(){n($(this),"minute")}});o.on({click:function(){n($(this),"second")}});$(".icon-monk-time").on({mousedown:function(){var n=$(this).siblings(".monk-form-input"),t=n.attr("disabled");t!="disabled"&&$(this).parent(".monk-form-wrap").next(".monk-form-time").hasClass("monk-none")&&setTimeout(function(){n.focus()},100)}})};t.fileInit=function(){$(".monk-file").on({change:function(){var n=$(this).val(),t=$(this).parent(".monk-form-image-wrap").prev(".monk-form-wrap").children(".monk-form-input");t.val(n)}})};t.init=function(){t.inputInit();t.checkboxInit();t.switchInit();t.radioInit();t.selectInit();t.timeInit();t.fileInit()};t.init()});
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化