From 80066a82632653e143e20ffe05edc6e14cdcf721 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Pi=C4=85tek?= Date: Tue, 31 Mar 2015 12:44:02 +0200 Subject: [PATCH 1/3] Added UMD support --- js/jquery.pickmeup.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/js/jquery.pickmeup.js b/js/jquery.pickmeup.js index dd89653..8c833be 100644 --- a/js/jquery.pickmeup.js +++ b/js/jquery.pickmeup.js @@ -6,6 +6,7 @@ * @copyright Copyright (c) 2008-2009, Stefan Petre * @license MIT License, see license.txt */ + (function (d) { function getMaxDays () { var tmpDate = new Date(this.toString()), @@ -39,7 +40,19 @@ return Math.floor(time / 24*60*60*1000); }; })(Date.prototype); -(function ($) { + +(function (factory) { + if (typeof define === 'function' && define.amd) { + // AMD + define(['jquery'], factory); + } else if (typeof exports === 'object') { + // CommonJS + factory(require('jquery')); + } else { + // Browser globals + factory(jQuery); + } +}(function ($) { var instances_count = 0; $.pickmeup = $.extend($.pickmeup || {}, { date : new Date, @@ -1119,4 +1132,4 @@ } }); }; -})(jQuery); +})); From ed9573b54adb53b2d2055a12e501effdcab4b72d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Pi=C4=85tek?= Date: Tue, 31 Mar 2015 13:00:23 +0200 Subject: [PATCH 2/3] Added UMD support info --- readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/readme.md b/readme.md index a4289a4..4055e35 100644 --- a/readme.md +++ b/readme.md @@ -16,6 +16,8 @@ Browser support: ## Getting started You need only 2 files: `js/jquery.pickmeup.min.js` and `css/pickmeup.min.css`. +The plugin can also be loaded as AMD or CommonJS module. + Then you can apply datepicker to any element: ```javascript $('.date').pickmeup(); From 764da6166109a6419a8803644cc22859dbba577a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Pi=C4=85tek?= Date: Tue, 31 Mar 2015 13:05:16 +0200 Subject: [PATCH 3/3] Added UMD-supporting minified distribution version --- js/jquery.pickmeup.min.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/jquery.pickmeup.min.js b/js/jquery.pickmeup.min.js index 66de98c..e7f615d 100644 --- a/js/jquery.pickmeup.min.js +++ b/js/jquery.pickmeup.min.js @@ -6,4 +6,4 @@ * @copyright Copyright (c) 2008-2009, Stefan Petre * @license MIT License, see license.txt */ -(function(d){function getMaxDays(){var tmpDate=new Date(this.toString()),d=28,m=tmpDate.getMonth();while(tmpDate.getMonth()==m){++d;tmpDate.setDate(d);}return d-1;}d.addDays=function(n){this.setDate(this.getDate()+n);};d.addMonths=function(n){var day=this.getDate();this.setDate(1);this.setMonth(this.getMonth()+n);this.setDate(Math.min(day,getMaxDays.apply(this)));};d.addYears=function(n){var day=this.getDate();this.setDate(1);this.setFullYear(this.getFullYear()+n);this.setDate(Math.min(day,getMaxDays.apply(this)));};d.getDayOfYear=function(){var now=new Date(this.getFullYear(),this.getMonth(),this.getDate(),0,0,0);var then=new Date(this.getFullYear(),0,0,0,0,0);var time=now-then;return Math.floor(time/24*60*60*1000);};})(Date.prototype);(function($){var instances_count=0;$.pickmeup=$.extend($.pickmeup||{},{date:new Date,default_date:new Date,flat:false,first_day:1,prev:"◀",next:"▶",mode:"single",select_year:true,select_month:true,select_day:true,view:"days",calendars:1,format:"d-m-Y",position:"bottom",trigger_event:"click touchstart",class_name:"",separator:" - ",hide_on_select:false,min:null,max:null,render:function(){},change:function(){return true;},before_show:function(){return true;},show:function(){return true;},hide:function(){return true;},fill:function(){return true;},locale:{days:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],daysShort:["Sun","Mon","Tue","Wed","Thu","Fri","Sat","Sun"],daysMin:["Su","Mo","Tu","We","Th","Fr","Sa","Su"],months:["January","February","March","April","May","June","July","August","September","October","November","December"],monthsShort:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]}});var views={years:"pmu-view-years",months:"pmu-view-months",days:"pmu-view-days"},tpl={wrapper:'
',head:function(d){var result="";for(var i=0;i<7;++i){result+="
"+d.day[i]+"
";}return'
";},body:function(elements,container_class_name){var result="";for(var i=0;i'+elements[i].text+"
";}return'
'+result+"
";}};function fill(){var options=$(this).data("pickmeup-options"),pickmeup=this.pickmeup,current_cal=Math.floor(options.calendars/2),actual_date=options.date,current_date=options.current,min_date=options.min?new Date(options.min):null,max_date=options.max?new Date(options.max):null,local_date,header,html,instance,today=(new Date).setHours(0,0,0,0).valueOf(),shown_date_from,shown_date_to,tmp_date;if(min_date){min_date.setDate(1);min_date.addMonths(1);min_date.addDays(-1);}if(max_date){max_date.setDate(1);max_date.addMonths(1);max_date.addDays(-1);}pickmeup.find(".pmu-instance > :not(nav)").remove();for(var i=0;imax_date){--i;current_date.addMonths(-1);shown_date_to=undefined;continue;}}}shown_date_to=new Date(local_date);if(!shown_date_from){shown_date_from=new Date(local_date);shown_date_from.setDate(1);shown_date_from.addMonths(1);shown_date_from.addDays(-1);if(min_date&&min_date>shown_date_from){--i;current_date.addMonths(1);shown_date_from=undefined;continue;}}instance.find(".pmu-month").text(header);html="";var is_year_selected=function(year){return(options.mode=="range"&&year>=new Date(actual_date[0]).getFullYear()&&year<=new Date(actual_date[1]).getFullYear())||(options.mode=="multiple"&&actual_date.reduce(function(prev,current){prev.push(new Date(current).getFullYear());return prev;},[]).indexOf(year)!==-1)||new Date(actual_date).getFullYear()==year;};var is_months_selected=function(year,month){var first_year=new Date(actual_date[0]).getFullYear(),lastyear=new Date(actual_date[1]).getFullYear(),first_month=new Date(actual_date[0]).getMonth(),last_month=new Date(actual_date[1]).getMonth();return(options.mode=="range"&&year>first_year&&year=first_month)||(options.mode=="range"&&year>first_year&&year==lastyear&&month<=last_month)||(options.mode=="range"&&year==first_year&&year==lastyear&&month>=first_month&&month<=last_month)||(options.mode=="multiple"&&actual_date.reduce(function(prev,current){current=new Date(current);prev.push(current.getFullYear()+"-"+current.getMonth());return prev;},[]).indexOf(year+"-"+month)!==-1)||(new Date(actual_date).getFullYear()==year&&new Date(actual_date).getMonth()==month);};(function(){var years=[],start_from_year=local_date.getFullYear()-6,min_year=new Date(options.min).getFullYear(),max_year=new Date(options.max).getFullYear(),year;for(var j=0;j<12;++j){year={text:start_from_year+j,class_name:[]};if((options.min&&year.textmax_year)){year.class_name.push("pmu-disabled");}else{if(is_year_selected(year.text)){year.class_name.push("pmu-selected");}}year.class_name=year.class_name.join(" ");years.push(year);}html+=tpl.body(years,"pmu-years");})();(function(){var months=[],current_year=local_date.getFullYear(),min_year=new Date(options.min).getFullYear(),min_month=new Date(options.min).getMonth(),max_year=new Date(options.max).getFullYear(),max_month=new Date(options.max).getMonth(),month;for(var j=0;j<12;++j){month={text:options.locale.monthsShort[j],class_name:[]};if((options.min&&(current_yearmax_year||(j>max_month&¤t_year>=max_year)))){month.class_name.push("pmu-disabled");}else{if(is_months_selected(current_year,j)){month.class_name.push("pmu-selected");}}month.class_name=month.class_name.join(" ");months.push(month);}html+=tpl.body(months,"pmu-months");})();(function(){var days=[],current_month=local_date.getMonth(),day;(function(){local_date.setDate(1);var day=(local_date.getDay()-options.first_day)%7;local_date.addDays(-(day+(day<0?7:0)));})();for(var j=0;j<42;++j){day={text:local_date.getDate(),class_name:[]};if(current_month!=local_date.getMonth()){day.class_name.push("pmu-not-in-month");}if(local_date.getDay()==0){day.class_name.push("pmu-sunday");}else{if(local_date.getDay()==6){day.class_name.push("pmu-saturday");}}var from_user=options.render(new Date(local_date))||{},val=local_date.valueOf(),disabled=(options.min&&options.min>local_date)||(options.max&&options.max=options.date[0]&&val<=options.date[1])){day.class_name.push("pmu-selected");}}if(val==today){day.class_name.push("pmu-today");}if(from_user.class_name){day.class_name.push(from_user.class_name);}day.class_name=day.class_name.join(" ");days.push(day);local_date.addDays(1);}html+=tpl.body(days,"pmu-days");})();instance.append(html);}shown_date_from.setDate(1);shown_date_to.setDate(1);shown_date_to.addMonths(1);shown_date_to.addDays(-1);pickmeup.find(".pmu-prev").css("visibility",options.min&&options.min>=shown_date_from?"hidden":"visible");pickmeup.find(".pmu-next").css("visibility",options.max&&options.max<=shown_date_to?"hidden":"visible");options.fill.apply(this);}function parseDate(date,format,separator,locale){if(date.constructor==Date){return date;}else{if(!date){return new Date;}}var splitted_date=date.split(separator);if(splitted_date.length>1){splitted_date.forEach(function(element,index,array){array[index]=parseDate($.trim(element),format,separator,locale);});return splitted_date;}var months_text=locale.monthsShort.join(")(")+")("+locale.months.join(")("),separator=new RegExp("[^0-9a-zA-Z("+months_text+")]+"),parts=date.split(separator),against=format.split(separator),d,m,y,h,min,now=new Date();for(var i=0;i100?0:(y<29?2000:1900);break;case"H":case"I":case"k":case"l":h=parseInt(parts[i],10);break;case"P":case"p":if(/pm/i.test(parts[i])&&h<12){h+=12;}else{if(/am/i.test(parts[i])&&h>=12){h-=12;}}break;case"M":min=parseInt(parts[i],10);break;}}var parsed_date=new Date(y===undefined?now.getFullYear():y,m===undefined?now.getMonth():m,d===undefined?now.getDate():d,h===undefined?now.getHours():h,min===undefined?now.getMinutes():min,0);if(isNaN(parsed_date*1)){parsed_date=new Date;}return parsed_date;}function formatDate(date,format,locale){var m=date.getMonth();var d=date.getDate();var y=date.getFullYear();var w=date.getDay();var s={};var hr=date.getHours();var pm=(hr>=12);var ir=(pm)?(hr-12):hr;var dy=date.getDayOfYear();if(ir==0){ir=12;}var min=date.getMinutes();var sec=date.getSeconds();var parts=format.split(""),part;for(var i=0;i15?-1:1);}options.current.setDate(val);options.binded.update_date();}}}}options.binded.fill();}return false;}function prepareDate(options){var result;if(options.mode=="single"){result=new Date(options.date);return[formatDate(result,options.format,options.locale),result];}else{result=[[],[]];$.each(options.date,function(nr,val){var date=new Date(val);result[0].push(formatDate(date,options.format,options.locale));result[1].push(date);});return result;}}function show(force){var pickmeup=this.pickmeup;if(force||!pickmeup.is(":visible")){var $this=$(this),options=$this.data("pickmeup-options"),pos=$this.offset(),viewport={l:document.documentElement.scrollLeft,t:document.documentElement.scrollTop,w:document.documentElement.clientWidth,h:document.documentElement.clientHeight},top=pos.top,left=pos.left;options.binded.fill();if($this.is("input")){$this.pickmeup("set_date",parseDate($this.val()?$this.val():options.default_date,options.format,options.separator,options.locale)).keydown(function(e){if(e.which==9){$this.pickmeup("hide");}});options.lastSel=false;}options.before_show();if(options.show()==false){return;}if(!options.flat){switch(options.position){case"top":top-=pickmeup.outerHeight();break;case"left":left-=pickmeup.outerWidth();break;case"right":left+=this.offsetWidth;break;case"bottom":top+=this.offsetHeight;break;}if(top+pickmeup.offsetHeight>viewport.t+viewport.h){top=pos.top-pickmeup.offsetHeight;}if(topviewport.l+viewport.w){left=pos.left-pickmeup.offsetWidth;}if(left',head:function(d){var result="";for(var i=0;i<7;++i){result+="
"+d.day[i]+"
";}return'
";},body:function(elements,container_class_name){var result="";for(var i=0;i'+elements[i].text+"";}return'
'+result+"
";}};function fill(){var options=$(this).data("pickmeup-options"),pickmeup=this.pickmeup,current_cal=Math.floor(options.calendars/2),actual_date=options.date,current_date=options.current,min_date=options.min?new Date(options.min):null,max_date=options.max?new Date(options.max):null,local_date,header,html,instance,today=(new Date).setHours(0,0,0,0).valueOf(),shown_date_from,shown_date_to,tmp_date;if(min_date){min_date.setDate(1);min_date.addMonths(1);min_date.addDays(-1);}if(max_date){max_date.setDate(1);max_date.addMonths(1);max_date.addDays(-1);}pickmeup.find(".pmu-instance > :not(nav)").remove();for(var i=0;imax_date){--i;current_date.addMonths(-1);shown_date_to=undefined;continue;}}}shown_date_to=new Date(local_date);if(!shown_date_from){shown_date_from=new Date(local_date);shown_date_from.setDate(1);shown_date_from.addMonths(1);shown_date_from.addDays(-1);if(min_date&&min_date>shown_date_from){--i;current_date.addMonths(1);shown_date_from=undefined;continue;}}instance.find(".pmu-month").text(header);html="";var is_year_selected=function(year){return(options.mode=="range"&&year>=new Date(actual_date[0]).getFullYear()&&year<=new Date(actual_date[1]).getFullYear())||(options.mode=="multiple"&&actual_date.reduce(function(prev,current){prev.push(new Date(current).getFullYear());return prev;},[]).indexOf(year)!==-1)||new Date(actual_date).getFullYear()==year;};var is_months_selected=function(year,month){var first_year=new Date(actual_date[0]).getFullYear(),lastyear=new Date(actual_date[1]).getFullYear(),first_month=new Date(actual_date[0]).getMonth(),last_month=new Date(actual_date[1]).getMonth();return(options.mode=="range"&&year>first_year&&year=first_month)||(options.mode=="range"&&year>first_year&&year==lastyear&&month<=last_month)||(options.mode=="range"&&year==first_year&&year==lastyear&&month>=first_month&&month<=last_month)||(options.mode=="multiple"&&actual_date.reduce(function(prev,current){current=new Date(current);prev.push(current.getFullYear()+"-"+current.getMonth());return prev;},[]).indexOf(year+"-"+month)!==-1)||(new Date(actual_date).getFullYear()==year&&new Date(actual_date).getMonth()==month);};(function(){var years=[],start_from_year=local_date.getFullYear()-6,min_year=new Date(options.min).getFullYear(),max_year=new Date(options.max).getFullYear(),year;for(var j=0;j<12;++j){year={text:start_from_year+j,class_name:[]};if((options.min&&year.textmax_year)){year.class_name.push("pmu-disabled");}else{if(is_year_selected(year.text)){year.class_name.push("pmu-selected");}}year.class_name=year.class_name.join(" ");years.push(year);}html+=tpl.body(years,"pmu-years");})();(function(){var months=[],current_year=local_date.getFullYear(),min_year=new Date(options.min).getFullYear(),min_month=new Date(options.min).getMonth(),max_year=new Date(options.max).getFullYear(),max_month=new Date(options.max).getMonth(),month;for(var j=0;j<12;++j){month={text:options.locale.monthsShort[j],class_name:[]};if((options.min&&(current_yearmax_year||(j>max_month&¤t_year>=max_year)))){month.class_name.push("pmu-disabled");}else{if(is_months_selected(current_year,j)){month.class_name.push("pmu-selected");}}month.class_name=month.class_name.join(" ");months.push(month);}html+=tpl.body(months,"pmu-months");})();(function(){var days=[],current_month=local_date.getMonth(),day;(function(){local_date.setDate(1);var day=(local_date.getDay()-options.first_day)%7;local_date.addDays(-(day+(day<0?7:0)));})();for(var j=0;j<42;++j){day={text:local_date.getDate(),class_name:[]};if(current_month!=local_date.getMonth()){day.class_name.push("pmu-not-in-month");}if(local_date.getDay()==0){day.class_name.push("pmu-sunday");}else{if(local_date.getDay()==6){day.class_name.push("pmu-saturday");}}var from_user=options.render(new Date(local_date))||{},val=local_date.valueOf(),disabled=(options.min&&options.min>local_date)||(options.max&&options.max=options.date[0]&&val<=options.date[1])){day.class_name.push("pmu-selected");}}if(val==today){day.class_name.push("pmu-today");}if(from_user.class_name){day.class_name.push(from_user.class_name);}day.class_name=day.class_name.join(" ");days.push(day);local_date.addDays(1);}html+=tpl.body(days,"pmu-days");})();instance.append(html);}shown_date_from.setDate(1);shown_date_to.setDate(1);shown_date_to.addMonths(1);shown_date_to.addDays(-1);pickmeup.find(".pmu-prev").css("visibility",options.min&&options.min>=shown_date_from?"hidden":"visible");pickmeup.find(".pmu-next").css("visibility",options.max&&options.max<=shown_date_to?"hidden":"visible");options.fill.apply(this);}function parseDate(date,format,separator,locale){if(date.constructor==Date){return date;}else{if(!date){return new Date;}}var splitted_date=date.split(separator);if(splitted_date.length>1){splitted_date.forEach(function(element,index,array){array[index]=parseDate($.trim(element),format,separator,locale);});return splitted_date;}var months_text=locale.monthsShort.join(")(")+")("+locale.months.join(")("),separator=new RegExp("[^0-9a-zA-Z("+months_text+")]+"),parts=date.split(separator),against=format.split(separator),d,m,y,h,min,now=new Date();for(var i=0;i100?0:(y<29?2000:1900);break;case"H":case"I":case"k":case"l":h=parseInt(parts[i],10);break;case"P":case"p":if(/pm/i.test(parts[i])&&h<12){h+=12;}else{if(/am/i.test(parts[i])&&h>=12){h-=12;}}break;case"M":min=parseInt(parts[i],10);break;}}var parsed_date=new Date(y===undefined?now.getFullYear():y,m===undefined?now.getMonth():m,d===undefined?now.getDate():d,h===undefined?now.getHours():h,min===undefined?now.getMinutes():min,0);if(isNaN(parsed_date*1)){parsed_date=new Date;}return parsed_date;}function formatDate(date,format,locale){var m=date.getMonth();var d=date.getDate();var y=date.getFullYear();var w=date.getDay();var s={};var hr=date.getHours();var pm=(hr>=12);var ir=(pm)?(hr-12):hr;var dy=date.getDayOfYear();if(ir==0){ir=12;}var min=date.getMinutes();var sec=date.getSeconds();var parts=format.split(""),part;for(var i=0;i15?-1:1);}options.current.setDate(val);options.binded.update_date();}}}}options.binded.fill();}return false;}function prepareDate(options){var result;if(options.mode=="single"){result=new Date(options.date);return[formatDate(result,options.format,options.locale),result];}else{result=[[],[]];$.each(options.date,function(nr,val){var date=new Date(val);result[0].push(formatDate(date,options.format,options.locale));result[1].push(date);});return result;}}function show(force){var pickmeup=this.pickmeup;if(force||!pickmeup.is(":visible")){var $this=$(this),options=$this.data("pickmeup-options"),pos=$this.offset(),viewport={l:document.documentElement.scrollLeft,t:document.documentElement.scrollTop,w:document.documentElement.clientWidth,h:document.documentElement.clientHeight},top=pos.top,left=pos.left;options.binded.fill();if($this.is("input")){$this.pickmeup("set_date",parseDate($this.val()?$this.val():options.default_date,options.format,options.separator,options.locale)).keydown(function(e){if(e.which==9){$this.pickmeup("hide");}});options.lastSel=false;}options.before_show();if(options.show()==false){return;}if(!options.flat){switch(options.position){case"top":top-=pickmeup.outerHeight();break;case"left":left-=pickmeup.outerWidth();break;case"right":left+=this.offsetWidth;break;case"bottom":top+=this.offsetHeight;break;}if(top+pickmeup.offsetHeight>viewport.t+viewport.h){top=pos.top-pickmeup.offsetHeight;}if(topviewport.l+viewport.w){left=pos.left-pickmeup.offsetWidth;}if(left