UNPKG

jquery-timepicker

Version:

Enhances standard form input fields helping users to select (or type) times.

27 lines 13.4 kB
/* jQuery Form Styler v1.4.8 | (c) Dimox | https://github.com/Dimox/jQueryFormStyler */ (function(c){c.fn.styler=function(f){f=c.extend({wrapper:"form",idSuffix:"-styler",filePlaceholder:"\u0424\u0430\u0439\u043b \u043d\u0435 \u0432\u044b\u0431\u0440\u0430\u043d",fileBrowse:"\u041e\u0431\u0437\u043e\u0440...",selectSearch:!0,selectSearchLimit:10,selectSearchNotFound:"\u0421\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u0439 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e",selectSearchPlaceholder:"\u041f\u043e\u0438\u0441\u043a...",selectVisibleOptions:0,singleSelectzIndex:"100", selectSmartPositioning:!0},f);return this.each(function(){function s(){var c="",q="",b="",v="";void 0!==a.attr("id")&&""!=a.attr("id")&&(c=' id="'+a.attr("id")+f.idSuffix+'"');void 0!==a.attr("title")&&""!=a.attr("title")&&(q=' title="'+a.attr("title")+'"');void 0!==a.attr("class")&&""!=a.attr("class")&&(b=" "+a.attr("class"));var s=a.data(),h;for(h in s)""!=s[h]&&(v+=" data-"+h+'="'+s[h]+'"');this.id=c+v;this.title=q;this.classes=b}var a=c(this);a.is(":checkbox")?a.each(function(){if(1>a.parent("div.jq-checkbox").length){var f= function(){var f=new s,b=c("<div"+f.id+' class="jq-checkbox'+f.classes+'"'+f.title+'><div class="jq-checkbox__div"></div></div>');a.css({position:"absolute",zIndex:"-1",opacity:0,margin:0,padding:0}).after(b).prependTo(b);b.attr("unselectable","on").css({"-webkit-user-select":"none","-moz-user-select":"none","-ms-user-select":"none","-o-user-select":"none","user-select":"none",display:"inline-block",position:"relative",overflow:"hidden"});a.is(":checked")&&b.addClass("checked");a.is(":disabled")&& b.addClass("disabled");b.click(function(){b.is(".disabled")||(a.is(":checked")?(a.prop("checked",!1),b.removeClass("checked")):(a.prop("checked",!0),b.addClass("checked")),a.change());return!1});a.closest("label").add('label[for="'+a.attr("id")+'"]').click(function(a){b.click();a.preventDefault()});a.change(function(){a.is(":checked")?b.addClass("checked"):b.removeClass("checked")}).keydown(function(a){13!=a.which&&32!=a.which||b.click()}).focus(function(){b.is(".disabled")||b.addClass("focused")}).blur(function(){b.removeClass("focused")})}; f();a.on("refresh",function(){a.parent().before(a).remove();f()})}}):a.is(":radio")?a.each(function(){if(1>a.parent("div.jq-radio").length){var p=function(){var q=new s,b=c("<div"+q.id+' class="jq-radio'+q.classes+'"'+q.title+'><div class="jq-radio__div"></div></div>');a.css({position:"absolute",zIndex:"-1",opacity:0,margin:0,padding:0}).after(b).prependTo(b);b.attr("unselectable","on").css({"-webkit-user-select":"none","-moz-user-select":"none","-ms-user-select":"none","-o-user-select":"none","user-select":"none", display:"inline-block",position:"relative"});a.is(":checked")&&b.addClass("checked");a.is(":disabled")&&b.addClass("disabled");b.click(function(){b.is(".disabled")||(b.closest(f.wrapper).find('input[name="'+a.attr("name")+'"]').prop("checked",!1).parent().removeClass("checked"),a.prop("checked",!0).parent().addClass("checked"),a.change());return!1});a.closest("label").add('label[for="'+a.attr("id")+'"]').click(function(a){b.click();a.preventDefault()});a.change(function(){a.parent().addClass("checked")}).focus(function(){b.is(".disabled")|| b.addClass("focused")}).blur(function(){b.removeClass("focused")})};p();a.on("refresh",function(){a.parent().before(a).remove();p()})}}):a.is(":file")?a.css({position:"absolute",top:0,right:0,width:"100%",height:"100%",opacity:0,margin:0,padding:0}).each(function(){if(1>a.parent("div.jq-file").length){var p=function(){var q=new s,b=c("<div"+q.id+' class="jq-file'+q.classes+'"'+q.title+' style="display: inline-block; position: relative; overflow: hidden"></div>'),p=c('<div class="jq-file__name">'+ f.filePlaceholder+"</div>").appendTo(b);c('<div class="jq-file__browse">'+f.fileBrowse+"</div>").appendTo(b);a.after(b);b.append(a);a.is(":disabled")&&b.addClass("disabled");a.change(function(){p.text(a.val().replace(/.+[\\\/]/,""));""==a.val()&&p.text(f.filePlaceholder)}).focus(function(){b.addClass("focused")}).blur(function(){b.removeClass("focused")}).click(function(){b.removeClass("focused")})};p();a.on("refresh",function(){a.parent().before(a).remove();p()})}}):a.is("select")?a.each(function(){if(1> a.parent("div.jqselect").length){var p=function(){function q(a){a.unbind("mousewheel DOMMouseScroll").bind("mousewheel DOMMouseScroll",function(a){var b=null;"mousewheel"==a.type?b=-1*a.originalEvent.wheelDelta:"DOMMouseScroll"==a.type&&(b=40*a.originalEvent.detail);b&&(a.stopPropagation(),a.preventDefault(),c(this).scrollTop(b+c(this).scrollTop()))})}function b(){i=0;for(len=h.length;i<len;i++){var a="",c="",b=a="",f="",k="";h.eq(i).prop("selected")&&(c="selected sel");h.eq(i).is(":disabled")&&(c= "disabled");h.eq(i).is(":selected:disabled")&&(c="selected sel disabled");void 0!==h.eq(i).attr("class")&&(b=" "+h.eq(i).attr("class"),k=' data-jqfs-class="'+h.eq(i).attr("class")+'"');var e=h.eq(i).data(),n;for(n in e)""!=e[n]&&(a+=" data-"+n+'="'+e[n]+'"');a="<li"+k+a+' class="'+c+b+'">'+h.eq(i).text()+"</li>";h.eq(i).parent().is("optgroup")&&(void 0!==h.eq(i).parent().attr("class")&&(f=" "+h.eq(i).parent().attr("class")),a="<li"+k+' class="'+c+b+" option"+f+'">'+h.eq(i).text()+"</li>",h.eq(i).is(":first-child")&& (a='<li class="optgroup'+f+'">'+h.eq(i).parent().attr("label")+"</li>"+a));w+=a}}function p(){var r=new s,d=c("<div"+r.id+' class="jq-selectbox jqselect'+r.classes+'" style="display: inline-block; position: relative; z-index:'+f.singleSelectzIndex+'"><div class="jq-selectbox__select"'+r.title+' style="position: relative"><div class="jq-selectbox__select-text"></div><div class="jq-selectbox__trigger"><div class="jq-selectbox__trigger-arrow"></div></div></div></div>');a.css({margin:0,padding:0}).after(d).prependTo(d); var r=c("div.jq-selectbox__select",d),m=c("div.jq-selectbox__select-text",d),l=h.filter(":selected");l.length?m.text(l.text()):m.text(h.first().text());b();var k="";f.selectSearch&&(k='<div class="jq-selectbox__search"><input type="search" autocomplete="off" placeholder="'+f.selectSearchPlaceholder+'"></div><div class="jq-selectbox__not-found">'+f.selectSearchNotFound+"</div>");var e=c('<div class="jq-selectbox__dropdown" style="position: absolute">'+k+'<ul style="position: relative; list-style: none; overflow: auto; overflow-x: hidden">'+ w+"</ul></div>");d.append(e);var n=c("ul",e),g=c("li",e),t=c("input",e),x=c("div.jq-selectbox__not-found",e).hide();g.length<f.selectSearchLimit&&t.parent().hide();var u=0,A=0;g.each(function(){var a=c(this);a.css({display:"inline-block","white-space":"nowrap"});a.width()>u&&(u=a.innerWidth(),A=a.width());a.css({display:"block"})});var k=d.clone().appendTo("body").width("auto"),v=k.width();k.remove();v==d.width()&&(m.width(A),u+=d.find("div.jq-selectbox__trigger").width());u>d.width()&&e.width(u); a.css({position:"absolute",left:0,top:0,width:"100%",height:"100%",opacity:0});var B=d.outerHeight(),z=t.outerHeight(),y=n.css("max-height"),k=g.filter(".selected");1>k.length&&g.first().addClass("selected sel");void 0===g.data("li-height")&&g.data("li-height",g.outerHeight());var C=e.css("top");"auto"==e.css("left")&&e.css({left:0});"auto"==e.css("top")&&e.css({top:B});e.hide();k.length&&(h.first().text()!=l.text()&&d.addClass("changed"),d.data("jqfs-class",k.data("jqfs-class")),d.addClass(k.data("jqfs-class"))); if(a.is(":disabled"))return d.addClass("disabled"),!1;r.click(function(){a.focus();if(!navigator.userAgent.match(/(iPad|iPhone|iPod)/g)){if(f.selectSmartPositioning){var b=c(window),h=d.offset().top,m=b.height()-B-(h-b.scrollTop()),k=f.selectVisibleOptions,l=g.data("li-height"),p=5*l,r=l*k;0<k&&6>k&&(p=r);0==k&&(r="auto");m>p+z+20?(e.height("auto").css({bottom:"auto",top:C}),k=function(){n.css("max-height",Math.floor((m-20-z)/l)*l)},k(),n.css("max-height",r),"none"!=y&&n.css("max-height",y),m<e.outerHeight()+ 20&&k()):(e.height("auto").css({top:"auto",bottom:C}),k=function(){n.css("max-height",Math.floor((h-b.scrollTop()-20-z)/l)*l)},k(),n.css("max-height",r),"none"!=y&&n.css("max-height",y),h-b.scrollTop()-20<e.outerHeight()+20&&k())}c("div.jqselect").css({zIndex:f.singleSelectzIndex-1}).removeClass("opened focused");d.css({zIndex:f.singleSelectzIndex});e.is(":hidden")?(c("div.jq-selectbox__dropdown:visible").hide(),e.show(),d.addClass("opened")):(e.hide(),d.removeClass("opened"));g.filter(".selected").length&& (0!=n.innerHeight()/l%2&&(l/=2),n.scrollTop(n.scrollTop()+g.filter(".selected").position().top-n.innerHeight()/2+l));t.length&&(t.val("").keyup(),x.hide(),t.focus().keyup(function(){var a=c(this).val();g.each(function(){c(this).html().match(RegExp(".*?"+a+".*?","i"))?c(this).show():c(this).hide()});1>g.filter(":visible").length?x.show():x.hide()}));q(n);return!1}});g.hover(function(){c(this).siblings().removeClass("selected")});var D=g.filter(".selected").text();g.filter(".selected").text();g.filter(":not(.disabled):not(.optgroup)").click(function(){var b= c(this),n=b.text();if(D!=n){var g=b.index();b.is(".option")&&(g-=b.prevAll(".optgroup").length);b.addClass("selected sel").siblings().removeClass("selected sel");h.prop("selected",!1).eq(g).prop("selected",!0);D=n;m.text(n);h.first().text()!=n?d.addClass("changed"):d.removeClass("changed");d.data("jqfs-class")&&d.removeClass(d.data("jqfs-class"));d.data("jqfs-class",b.data("jqfs-class"));d.addClass(b.data("jqfs-class"));a.change()}t.length&&(t.val("").keyup(),x.hide());e.hide();d.removeClass("opened")}); e.mouseout(function(){c("li.sel",e).addClass("selected")});a.change(function(){m.text(h.filter(":selected").text());g.removeClass("selected sel").not(".optgroup").eq(a[0].selectedIndex).addClass("selected sel")}).focus(function(){d.addClass("focused")}).blur(function(){d.removeClass("focused")}).bind("keydown keyup",function(b){m.text(h.filter(":selected").text());g.removeClass("selected sel").not(".optgroup").eq(a[0].selectedIndex).addClass("selected sel");38!=b.which&&37!=b.which&&33!=b.which|| e.scrollTop(e.scrollTop()+g.filter(".selected").position().top);40!=b.which&&39!=b.which&&34!=b.which||e.scrollTop(e.scrollTop()+g.filter(".selected").position().top-e.innerHeight()+liHeight);13==b.which&&e.hide()});c(document).on("click",function(a){c(a.target).parents().hasClass("jq-selectbox")||"OPTION"==a.target.nodeName||(t.length&&t.val("").keyup(),e.hide().find("li.sel").addClass("selected"),d.removeClass("focused opened"))})}function E(){var f=new s,d=c("<div"+f.id+' class="jq-select-multiple jqselect'+ f.classes+'"'+f.title+' style="display: inline-block; position: relative"></div>');a.css({margin:0,padding:0}).after(d);b();d.append("<ul>"+w+"</ul>");var m=c("ul",d).css({position:"relative","overflow-x":"hidden","-webkit-overflow-scrolling":"touch"}),l=c("li",d).attr("unselectable","on").css({"-webkit-user-select":"none","-moz-user-select":"none","-ms-user-select":"none","-o-user-select":"none","user-select":"none","white-space":"nowrap"}),f=a.attr("size"),k=m.outerHeight(),e=l.outerHeight();void 0!== f&&0<f?m.css({height:e*f}):m.css({height:4*e});k>d.height()&&(m.css("overflowY","scroll"),q(m),l.filter(".selected").length&&m.scrollTop(m.scrollTop()+l.filter(".selected").position().top));a.prependTo(d).css({position:"absolute",left:0,top:0,width:"100%",height:"100%",opacity:0});a.is(":disabled")?(d.addClass("disabled"),h.each(function(){c(this).is(":selected")&&l.eq(c(this).index()).addClass("selected")})):(l.filter(":not(.disabled):not(.optgroup)").click(function(b){a.focus();d.removeClass("focused"); var g=c(this);b.ctrlKey||b.metaKey||g.addClass("selected");b.shiftKey||g.addClass("first");b.ctrlKey||(b.metaKey||b.shiftKey)||g.siblings().removeClass("selected first");if(b.ctrlKey||b.metaKey)g.is(".selected")?g.removeClass("selected first"):g.addClass("selected first"),g.siblings().removeClass("first");if(b.shiftKey){var e=!1,f=!1;g.siblings().removeClass("selected").siblings(".first").addClass("selected");g.prevAll().each(function(){c(this).is(".first")&&(e=!0)});g.nextAll().each(function(){c(this).is(".first")&& (f=!0)});e&&g.prevAll().each(function(){if(c(this).is(".selected"))return!1;c(this).not(".disabled, .optgroup").addClass("selected")});f&&g.nextAll().each(function(){if(c(this).is(".selected"))return!1;c(this).not(".disabled, .optgroup").addClass("selected")});1==l.filter(".selected").length&&g.addClass("first")}h.prop("selected",!1);l.filter(".selected").each(function(){var a=c(this),b=a.index();a.is(".option")&&(b-=a.prevAll(".optgroup").length);h.eq(b).prop("selected",!0)});a.change()}),h.each(function(a){c(this).data("optionIndex", a)}),a.change(function(){l.removeClass("selected");var a=[];h.filter(":selected").each(function(){a.push(c(this).data("optionIndex"))});l.not(".optgroup").filter(function(b){return-1<c.inArray(b,a)}).addClass("selected")}).focus(function(){d.addClass("focused")}).blur(function(){d.removeClass("focused")}),k>d.height()&&a.keydown(function(a){38!=a.which&&37!=a.which&&33!=a.which||m.scrollTop(m.scrollTop()+l.filter(".selected").position().top-e);40!=a.which&&39!=a.which&&34!=a.which||m.scrollTop(m.scrollTop()+ l.filter(".selected:last").position().top-m.innerHeight()+2*e)}))}var h=c("option",a),w="";a.is("[multiple]")?E():p()};p();a.on("refresh",function(){a.parent().before(a).remove();p()})}}):a.is(":reset")&&a.click(function(){setTimeout(function(){a.closest(f.wrapper).find("input, select").trigger("refresh")},1)})})}})(jQuery);