(function($,window,undefined){$.fn.rangeCalendar=function(options){var defaults={lang:"en",theme:"default-theme",themeContext:this,startDate:dayjs(),endDate:dayjs().add(12,'months'),start:"+7",startRangeWidth:1,minRangeWidth:1,maxRangeWidth:14,weekends:!0,autoHideMonths:!1,visible:!0,trigger:null,changeRangeCallback:function(el,cont,dateProp){return!1}};var returnObj;var getMomentFromString=function(date){var endYear=date.substring(0,4);var endMonth=date.substring(4,6);var endDay=date.substring(6,8);if(endDay.length==1)
endDay='0'+endDay;var m=dayjs(endYear+endMonth+endDay,"YYYYMMDD");return m}
this.each(function(i,el){dayjs.extend(window.dayjs_plugin_relativeTime);dayjs.extend(window.dayjs_plugin_localeData);var obj=el,$el=$(el),settings=$.extend(!0,{},defaults,options);obj.options=settings;obj.showCalendar=function(animate){var calPos=obj.calendarObj.position();var lastItemPos=obj.calendarObj.find(".cell").last().position();var lastItemRight=(lastItemPos.left+obj.calendarObj.find(".cell").last().outerWidth());var selectedItemPos=obj.calendarObj.find(".cell").eq(0).position();$el.slideDown((animate?300:0),function(){var windowWidth=$(window).outerWidth();$(obj.calendarObj).css({left:windowWidth});$(obj.monthsObj).css({left:windowWidth});obj._placeElement(obj.calendarObj,(!selectedItemPos?0:selectedItemPos.left));obj._placeElement(obj.monthsObj)});obj.visible=!0},obj.hideCalendar=function(){$el.slideUp(50);obj.visible=!1},obj.toggleCalendar=function(){if(obj.visible)
obj.showCalendar();else obj.hideCalendar()},obj.rangeWidth=function(){var cellWidth=obj.calendarObj.find(".cell").outerWidth();var rangeWidth=parseInt(obj.calendarObj.find('.range-bar').outerWidth()/cellWidth);obj._rangeWidth=(!rangeWidth?obj._rangeWidth:rangeWidth);return obj._rangeWidth},obj.setRangeWidth=function(rangeWidth){var cellWidth=obj.calendarObj.find(".cal-cell").eq(0).outerWidth();var rangeWidth=parseInt(!rangeWidth||rangeWidth<obj.minRangeWidth?obj.minRangeWidth:rangeWidth);$el.find(".range-bar").width(cellWidth*rangeWidth);$el.find(".range-bar").trigger("resize")},obj.range=function(){var startDateIndex=obj.calendarObj.find('.cell.selected:eq(0)');var endDateIndex=obj.calendarObj.find('.cell.selected').last();var startDateTime=startDateIndex.attr('date-id');var endDateTime=endDateIndex.attr('date-id');var realStart=getMomentFromString(startDateTime);var realEnd=getMomentFromString(endDateTime);var startDateFormatted=realStart.toString();var endDateFormatted=realEnd.toString();var startDate=dayjs().add(startDateIndex.index()+obj.start,'days');var range=$.data(obj,"range",{start:startDateFormatted,end:endDateFormatted,width:obj.rangeWidth(),fromNow:startDate.fromNow()});return range},obj.setStartDate=function(startDate){var date=dayjs(startDate);var fullYear=date.locale(obj.lang).format("YYYY");var monthNumber=date.locale(obj.lang).format("MM");var dayNumber=date.locale(obj.lang).format('D');var dateId=fullYear+monthNumber+dayNumber;var monthId=fullYear+monthNumber;var dateCell=obj.calendarObj.find('.cell[date-id="'+dateId+'"]').eq(0);dateCell.trigger("click");var monthCell=obj.monthsObj.find('.cell[month-id="'+monthId+'"]').eq(0)},obj.lang=function(){return obj.lang},obj.setTheme=function(themeName){var _themeName=$(obj.themeContext).attr("theme");if(_themeName)
$(obj.themeContext).removeClass(_themeName);$(obj.themeContext).attr("theme",""+themeName+"");$(obj.themeContext).addClass(""+themeName+"");obj.theme=themeName},obj.update=function(){obj.setTheme(obj.theme);obj._generateView()},obj.didResizeBar=function(){var prevRangeWidth=obj.rangeWidth();var rangeWidth=prevRangeWidth;var resizeBarPos=obj.calendarObj.find('.range-bar').position();var resizeBarWidth=obj.calendarObj.find('.range-bar').outerWidth();var resizeBarRight=resizeBarPos.left+resizeBarWidth;var cellWidth=$(obj).find(".cell").first().outerWidth();var lastCellPos=$(obj).find(".cell").last().position();var deltaWidth=0
var objWidth=(lastCellPos.left+cellWidth);if(resizeBarRight>objWidth){deltaWidth=objWidth-resizeBarWidth;prevRangeWidth=(resizeBarWidth-deltaWidth)/cellWidth}
obj.calendarObj.find('.cell').removeClass("selected");obj.calendarObj.find('.cell').removeClass("last");obj.calendarObj.find('.cell.start').addClass("selected");obj.calendarObj.find('.cell.start').nextAll().slice(0,(!rangeWidth?obj.minRangeWidth-1:rangeWidth-1)).addClass('selected');obj.calendarObj.find('.cell.selected').last().addClass("last")},obj.didSelectMonth=function(e){if(obj.isDragging||$(obj.lastTarget).is(obj.monthsObj)){delete obj.lastTarget;return}
var currentMonthId=$(this).attr("month-id");var currentCellMonth=obj.calendarObj.find('.cell[month-id="'+currentMonthId+'"].selected').eq(0);var monthPosition=(!currentCellMonth.length?obj.calendarObj.find('.cell[month-id="'+currentMonthId+'"]').eq(0).position():currentCellMonth.position());var calendarViewWidth=$($el).outerWidth();obj.monthsObj.find('.cell').not(this).removeClass('selected');$(this).addClass('selected');obj._placeElement(obj.calendarObj,monthPosition);if(currentMonthId==obj.startDate.locale(obj.lang).format('YYYYMM'))
startday=obj.startDate.locale(obj.lang).format('DD');else startday='01';obj.setStartDate(currentMonthId+startday)},obj.didChangeRange=function(e,ui){if(obj.isDragging||$(obj.lastTarget).is(obj.calendarObj)){delete obj.lastTarget;return}
var rangeWidth=obj.rangeWidth();var currentCalItem=$(this);var lastCalItem=obj.calendarObj.find('.cell').last();var delta=lastCalItem.index()-currentCalItem.index();var rightBar=currentCalItem.index()+rangeWidth-1;if(rightBar>lastCalItem.index()){obj.calendarObj.find(' .cell').eq(currentCalItem.index()-rangeWidth+delta+1).trigger("click");return}
obj.calendarObj.find(".start").removeClass("start");currentCalItem.addClass("start");obj._updateRangeBar();obj._updateMonths();obj._dispatchEvent(obj.changeRangeCallback,obj.range(),obj)},obj._initRangeBar=function(){$(window).off("resize");var rangeWidth=obj.rangeWidth();var cellWidth=obj.calendarObj.find(".cell").eq(0).outerWidth();var cellHeight=obj.calendarObj.find(".cell").eq(0).outerHeight();var selectedCell=obj.calendarObj.find(".cell.selected:eq(0)");if(!selectedCell.length)
return;obj.calendarObj.find(".range-bar").off("resize");obj.calendarObj.find(".range-bar").remove();$(selectedCell).append('<div class="range-bar resizable"><div class="range-bar-content"></div></div>');if(obj.maxRangeWidth>1){obj.calendarObj.find(".range-bar").resizable({grid:[cellWidth,0],maxWidth:obj.maxRangeWidth*cellWidth,minWidth:cellWidth*obj.minRangeWidth,maxHeight:cellHeight,minHeight:cellHeight,handles:"e"})}
obj.setRangeWidth(rangeWidth);obj.calendarObj.find(".range-bar").on("resize",obj.didResizeBar);$(window).on("resize",obj._resize)},obj._initMonths=function(){obj.monthsObj.draggable({axis:"x",scrollSensitivity:100,scrollSpeed:100,cursor:"move",create:function(e,ui){obj._updateMonths();obj._placeElement(obj.monthsObj)},start:function(e,ui){obj.isDragging=!0;obj.monthsObj.find('.cell').off("click")},drag:function(e,ui){},stop:function(e,ui){$(this).css({top:0});obj.lastTarget=e.target;setTimeout(function(){obj.isDragging=!1;delete obj.lastTarget;obj._placeElement(obj.monthsObj);obj.monthsObj.find('.cell').on("click",obj.didSelectMonth)},10)}})},obj._initCalendar=function(){var xpos;obj.calendarObj.draggable({axis:"x",scrollSensitivity:100,scrollSpeed:100,cursor:"move",create:function(){obj.calendarObj.find('.cell').removeClass("selected");obj.calendarObj.find('.cell').removeClass("last");obj.calendarObj.find(".cell").eq(obj.start).addClass("start");obj.calendarObj.find(".cell").eq(obj.start).addClass("selected");obj.calendarObj.find('.cell.start').nextAll().slice(0,obj._rangeWidth-1).addClass('selected');obj.calendarObj.find('.cell').on("click",obj.didChangeRange);obj.calendarObj.find('.cell.selected').last().addClass("last");obj._placeElement(obj.calendarObj)},start:function(e,ui){xpos=ui.position.left;$(window).off("resize");obj.isDragging=!0;obj.calendarObj.find('.cell').off("click")},drag:function(e,ui){var xmove=ui.position.left-xpos;var direction=xmove>=0?'right':'left';var rangeCalendarWidth=$el.outerWidth();var calendarOffset=obj.calendarObj.position();var monthMaxId=parseInt(obj.monthsObj.find(".cell").last().attr("month-id"));var monthMinId=parseInt(obj.monthsObj.find(".cell").first().attr("month-id"));var currentMonthId=parseInt(obj.monthsObj.find(".cell.selected").attr("month-id"));var nextMonthId=parseInt(obj.monthsObj.find(".cell.selected").next().attr("month-id"));var prevMonthId=parseInt(obj.monthsObj.find(".cell.selected").prev().attr("month-id"));if(nextMonthId&&currentMonthId&&nextMonthId<=monthMaxId&&direction=="left"){var nextMonthsCell=obj.monthsObj.find('.cell[month-id="'+nextMonthId+'"]');var nextMonthCalendarCell=obj.calendarObj.find('.cell[month-id="'+nextMonthId+'"]').first();var nextMonthCalendarCellPos=nextMonthCalendarCell.position();var nextMonthLeftCenter=(rangeCalendarWidth/2-(nextMonthCalendarCellPos.left));if(nextMonthLeftCenter>=calendarOffset.left&&calendarOffset.left!=0){obj.monthsObj.find(".cell").removeClass("selected");$(nextMonthsCell).addClass("selected");obj._placeElement(obj.monthsObj,nextMonthsCell.position())}}else if(prevMonthId&&currentMonthId&&prevMonthId>=monthMinId&&direction=="right"){var prevMonthCell=obj.monthsObj.find('.cell[month-id="'+prevMonthId+'"]');var prevMonthCalendarCell=obj.calendarObj.find('.cell[month-id="'+prevMonthId+'"]').last();var prevMonthCalendarCellPos=prevMonthCalendarCell.position();var prevMonthLeftCenter=(rangeCalendarWidth/2-(prevMonthCalendarCellPos.left));if(prevMonthLeftCenter<=calendarOffset.left+prevMonthCalendarCell.outerWidth()){obj.monthsObj.find(".cell").removeClass("selected");$(prevMonthCell).addClass("selected");obj._placeElement(obj.monthsObj,prevMonthCell.position())}}},stop:function(e,ui){obj.lastTarget=e.target;obj._placeElement(obj.calendarObj);setTimeout(function(){obj.isDragging=!1;delete obj.lastTarget;obj.calendarObj.find('.cell').on("click",obj.didChangeRange);$(window).on("resize",obj._resize);obj._placeElement(obj.monthsObj)},100)}})},obj._getCalendarHTML=function(startDate,endDate){var calendarHtml='';var cell;var date=dayjs(startDate).add(obj.start,'days');var endDate=dayjs(endDate).add(obj.start,'days');var rangeWidth=obj.rangeWidth();for(var index=1;(date.isBefore(endDate)||date.isSame(endDate));index++){var fullYear=date.locale(obj.lang).format("YYYY");var month=date.locale(obj.lang).format("MM");var monthNumber=date.locale(obj.lang).format("MM");var day=date.locale(obj.lang).format('dd');var dayNumber=date.locale(obj.lang).format('D');var isWeekend=date.day()%6==0;if(isWeekend&&!obj.weekends){date=date.add(1,'days');continue}
cell='<div class="cal-cell cell" date-id="'+fullYear+monthNumber+dayNumber+'" month-id="'+fullYear+''+monthNumber+'" month="'+monthNumber+'">';cell+='<div class="cell-content">';cell+='<div class="day-number">'+dayNumber+'</div>';cell+='</div>';cell+='</div>';calendarHtml+=cell;date=date.add(1,'days')}
return calendarHtml},obj._getMonthsHTML=function(startDate,endDate){var monthsHtml='';var cell;var date=dayjs(startDate).add(obj.start,'days');var endDate=dayjs(endDate).add(obj.start,'days');for(var index=1;(date.isBefore(endDate)||date.isSame(endDate));index++){var year=date.locale(obj.lang).format("YY");var fullYear=date.locale(obj.lang).format("YYYY");var month=date.locale(obj.lang).format("MMM");var monthNumber=date.locale(obj.lang).format("MM");cell='<div class="month-cell cell" month-id="'+fullYear+''+monthNumber+'" month="'+monthNumber+'" tabindex="0">';cell+='<i class="bullet"></i>';cell+='<div class="date-formatted"><span class="month-name">'+month+'</span> '+fullYear+'</span></div>';cell+='</div>';monthsHtml+=cell;date=date.add(1,'month')}
return monthsHtml},obj._updateMonths=function(){var currentMonth=obj.calendarObj.find('.cell.selected:eq(0)').attr("month-id");obj.monthsObj.find('.cell').removeClass('selected');obj.monthsObj.find('.cell').removeClass('current');obj.monthsObj.find('.cell[month-id="'+currentMonth+'"]').addClass('selected');obj.monthsObj.find('.cell[month-id="'+currentMonth+'"]').addClass('current')},obj._updateRangeBar=function(){obj.didResizeBar();obj._initRangeBar()},obj._dispatchEvent=function(callback,options,el){if(!callback)
return!1;callback(el,options)},obj._placeElement=function(el){obj._placeElement(el,null)},obj._placeElement=function(el,position){var calendarViewWidth=$el.outerWidth();var cellWidth=$(el).find(".cell").first().outerWidth();var objChildrens=$(el).children().length;var objWidth=(objChildrens*cellWidth);var elPos=$(el).position();left=(!position?parseInt(elPos.left):-position.left);if(calendarViewWidth>objWidth)
left=(calendarViewWidth-objWidth)/2;else if(calendarViewWidth<objWidth&&left>=0)
left=0;else if(left<calendarViewWidth-objWidth)
left=-objWidth+calendarViewWidth;$(el).stop().animate({left:left},300,'easeOutCirc')},obj._timedResize=function(){clearTimeout(obj.resizeTimer);obj.resizeTimer=setTimeout(obj._resize,obj.timeoutTime)},obj._bindEvents=function(){if(obj.trigger){$(obj.trigger).off("click");$(obj.trigger).on("click",obj.toggleCalendar)}
obj.timeout=!1;obj.timeoutTime=100;obj.calendarObj.find(".range-bar").on("resize",obj.didResizeBar);obj.monthsObj.find('.cell').on("click",obj.didSelectMonth);$(window).on('resize',obj._timedResize);$el.on('mouseenter',function(){if(obj.autoHideMonths)
obj.monthsObj.slideDown(100,'easeOutCirc');});$el.on('mouseleave',function(){if(obj.autoHideMonths)
obj.monthsObj.slideUp(0,'easeOutCirc');});$(obj).on('mousedown','.range-bar',function(e){var topElement=document.elementFromPoint(e.clientX,e.clientY);if($(topElement).hasClass('ui-resizable-handle')){e.stopPropagation();return!1}});$(obj).on('mouseup','.range-bar',function(e){if($(obj).find('.range-bar').hasClass('ui-resizable-resizing'))
return!0;$(this).hide();var BottomElement=document.elementFromPoint(e.clientX,e.clientY);$(this).show();$(BottomElement).trigger('click');return!0})},obj._resize=function(){obj._placeElement(obj.calendarObj);obj._placeElement(obj.monthsObj);obj.timeout=!1},obj._updateView=function(startDate,endDate){obj.calendarObj.append(obj._getCalendarHTML(startDate,endDate));obj.monthsObj.append(obj._getMonthsHTML(startDate,endDate));if(obj.visible){$el.css({display:"block"});obj.showCalendar()}else $el.css({display:"none"});obj._initCalendar();obj._initMonths();obj._initRangeBar()},obj._generateView=function(){var mainClass="range-calendar";$el.removeClass(mainClass)
$el.addClass(mainClass);$el.empty();obj.monthsObj=$('<div class="wrapper"><div class="months"></div></div>');$el.append(obj.monthsObj);obj.monthsObj=obj.monthsObj.find(".months");(obj.autoHideMonths?obj.monthsObj.addClass("auto-hide-months"):'');obj.calendarObj=$('<div class="wrapper"><div class="calendar"></div></div>');$el.append(obj.calendarObj);obj.calendarObj=obj.calendarObj.find(".calendar");obj._updateView(obj.startDate,obj.endDate);obj._bindEvents()}
obj._init=function(element,options){obj.themeContext=options.themeContext;obj.lang=options.lang;obj.isDragging=!1;obj.minRangeWidth=options.minRangeWidth;obj.maxRangeWidth=options.maxRangeWidth;obj.weekends=options.weekends;obj.startDate=options.startDate;obj.endDate=options.endDate;obj.start=(!options.start?"+7":parseInt(options.start));obj.startRangeWidth=(options.startRangeWidth>options.maxRangeWidth?options.maxRangeWidth:options.startRangeWidth);obj._rangeWidth=obj.startRangeWidth;obj.trigger=options.trigger;obj.visible=options.visible;obj.changeRangeCallback=options.changeRangeCallback;obj.autoHideMonths=options.autoHideMonths;obj.theme=(options.theme?options.theme:defaults.theme);obj.update()},obj._init(el,obj.options);$(obj).data('rangeCalendar',obj)});return this.data("rangeCalendar")}})(jQuery,window);$(document).on("keyup",function(event){var keycode=(event.keyCode?event.keyCode:event.which);if(keycode=='13'){$(':focus').trigger("click")}})