window.auto_close_popup = false; $.fn.fullCalendar = function(args){ var self = this[0] if(!self.calendar_args) self.calendar_args = args; else args = Object.assign(self.calendar_args, args); var calendar = new FullCalendar.Calendar(self,args); calendar.render(); $(window).on("load",function(){ calendar.render(); }) this.calendar = calendar; self.calendar = calendar; $.fullCalendar = calendar; return calendar; }; function correct_date(date){ var new_date = new Date(); new_date.setTime(date.getTime() + date.getTimezoneOffset() * 60 * 1000); return new_date; } FullCalendar.Calendar.prototype.get_all_events = function(){ this.currentData.all_events = []; var all_events = this.currentData.all_events; if(this.currentData.eventStore && this.currentData.eventStore.instances){ var instances = this.currentData.eventStore.instances; Object.keys(instances).forEach(function(k){ var instance = instances[k]; var range = Object.assign({},instance.range); range.start = correct_date(range.start); range.end = correct_date(range.end); all_events.push(range); }) } return this.currentData.all_events; } FullCalendar.Calendar.prototype.isAnOverlapEvent = function(eventStartDay, eventEndDay){ eventStartDay = eventStartDay || eventEndDay; eventEndDay = eventEndDay || eventStartDay; if((typeof(eventStartDay)).toLowerCase() == "string") eventStartDay = new Date(eventStartDay); if((typeof(eventEndDay)).toLowerCase() == "string") eventEndDay = new Date(eventEndDay); var events = this.get_all_events(); for (var i = 0; i < events.length; i++) { var eventA = events[i]; // start-time in between any of the events if (eventStartDay >= eventA.start && eventStartDay <= eventA.end) { return true; } //end-time in between any of the events if (eventEndDay >= eventA.start && eventEndDay <= eventA.end) { return true; } //any of the events in between/on the start-time and end-time if (eventStartDay <= eventA.start && eventEndDay >= eventA.end) { return true; } } return false; } window.is_chinese = ( I18n && I18n.locale.indexOf('zh') != -1 ); window.datetime_format = is_chinese ? 'y M d h:m b' : 'd M, y h:m b'; window.date_format = is_chinese ? 'y M d' : 'd M, y'; window.time_format = "h:m b"; window.date_time_str_format = 'y/MM/d H:m'; window.std_date_format = 'y-MM-d'; window.short_day = (is_chinese ? "d (w)" : "w d"); window.short_date = (is_chinese ? "M d (w)" : "w d, M"); window.short_date_time = (is_chinese ? "M d (w) h:m b" : "w d, M h:m b"); window.getDateString = function(date, format,is_chinese) { var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; var week_days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat']; if(is_chinese){ months = []; for(var i=0;i<12;i++){ months.push((i+1)+"月"); } week_days = ["週日","週一","週二","週三","週四","週五","週六"] } var getPaddedComp = function(comp) { return ((parseInt(comp) < 10) ? ('0' + comp) : comp) }, formattedDate = format, o = { "y+": date.getFullYear() + (is_chinese ? "年" : ""), // year "MM+": getPaddedComp(date.getMonth() + 1), //raw month "M+": months[date.getMonth()], //month "d+": (is_chinese ? (date.getDate() + "日") : getPaddedComp(date.getDate())), //day "w+": week_days[date.getDay()], //weekday "h+": getPaddedComp((date.getHours() > 12) ? date.getHours() % 12 : date.getHours()), //hour "H+": getPaddedComp(date.getHours()), //hour "m+": getPaddedComp(date.getMinutes()), //minute "s+": getPaddedComp(date.getSeconds()), //second "S+": getPaddedComp(date.getMilliseconds()), //millisecond, "b+": (date.getHours() >= 12) ? 'PM' : 'AM' }; for (var k in o) { if (new RegExp("(" + k + ")").test(format)) { formattedDate = formattedDate.replace(RegExp.$1, o[k]); } } return formattedDate; }; var Calendar = function(dom, property_id, valid_range, currentView, display_hire_event){ var c = this; display_hire_event = (display_hire_event == undefined ? true : display_hire_event); this.title = $("#current_title"); this.calendar_dom = $(dom); this.nextBtn = $("#next_month_btn"); this.prevBtn = $("#prev_month_btn"); this.todayBtn = $("#today_btn"); this.modeBtns = $(".calendar_mode button"); this.refreshBtn = $("#refresh_btn"); this.dialog = new EventDialog(c); this.loading = $('#calendar-loading'); this.agenda_space = $("#calendar_agenda"); this.currentView = currentView || "dayGridMonth"; this.property_id = property_id; this.navigation = $("#navigation"); this.rangeSelection = $("#range_selection"); var agendaView = new AgendaView(c); var loadeventsonviewchange = false; this.initialize = function(){ var date = new Date(); var d = date.getDate(); var m = date.getMonth(); var y = date.getFullYear(); var dview = (c.currentView == "agenda" ? "dayGridMonth" : c.currentView); c.calendar_dom.css("overflow","visible"); c.calendar_dom.fullCalendar({ themeSystem: 'bootstrap', editable: false, selectable: true, width: "100%", validRange: valid_range, events: function(args, success_callback, fail_callback) { var start = args.start; var end = args.end; $.ajax({ url: "/xhr/property_hires/get_bookings?property_id="+c.property_id, dataType: 'json', type: 'GET', data: { start: Math.round(start.getTime() / 1000), end: Math.round(end.getTime() / 1000), _: Date.now(), display_hire_event: display_hire_event }, success: function(json) { // json = json.map(function(obj){ // obj.start = new Date(obj.start).toJSON(); // obj.end = new Date(obj.end).toJSON(); // return obj; // }) success_callback(json); } }); }, // events: 'https://fullcalendar.io/demo-events.json', headerToolbar: false, fixedWeekCount: false, initialView: dview, loading: function(bool) { if (bool) c.loading.show(); else c.loading.hide(); if(this.currentData) $('#current_title').html(this.currentData.viewTitle); }, windowResize : function(view){ c.calendar_dom.calendar.refetchEvents(); }, eventTimeFormat: { hour12: true, hour: '2-digit', minute: '2-digit', omitZeroMinute: true, meridiem: 'narrow' }, eventClick: function(eventClickInfo) { var calEvent = {"event": eventClickInfo.event}, originalEvent = eventClickInfo.jsEvent, view = eventClickInfo.view, el = $(eventClickInfo.el); if(el.hasClass("reserve_btn")){ window.calEvent = calEvent; var start_time = calEvent.event.start; var date_str = window.getDateString(start_time,std_date_format); c.dialog.hide(); var allow_times = calEvent.event._def.extendedProps.allow_times; window.pick_hire_date(date_str,allow_times); }else{ c.dialog.dismiss(); c.dialog.inflate(calEvent); c.dialog.show({"x": originalEvent.clientX,"y": originalEvent.clientY}); } }, dateClick: function(ev) { var calendar = this; var calendar_dom = $(this.el); if(c.calendar_dom.hasClass("active_picker")){ var date = ev.date, date_str = getDateString(date,date_time_str_format), day_element = ev.dayEl, jsEvent = ev.jsEvent; var time_str = date_str.split(" ")[1]; var date_str = date_str.split(" ")[0]; calendar_dom.trigger("init_time",[time_str]); calendar_dom.trigger("select_time",[date_str]); } }, views: { dayGridMonth: { dayMaxEvents: true } } }); c.nextBtn.click(function(){ c.dialog.dismiss(); c.calendar_dom.calendar.next(); c.title.text(c.calendar_dom.calendar.currentData.viewTitle); }); c.prevBtn.click(function(){ c.dialog.dismiss(); c.calendar_dom.calendar.prev(); c.title.text(c.calendar_dom.calendar.currentData.viewTitle); }); c.todayBtn.click(function(){ c.dialog.dismiss(); c.calendar_dom.calendar.today(); c.title.text(c.calendar_dom.calendar.currentData.viewTitle); }); c.modeBtns.click(function(){ c.dialog.dismiss(); toggleViews($(this).data("mode")); }); c.refreshBtn.click(function(){ c.dialog.dismiss(); if(c.currentView == "agenda") agendaView.refresh(); else c.calendar_dom.calendar.refetchEvents(); }); var toggleViews = function(view){ c.modeBtns.removeClass("active"); c.modeBtns.each(function(){ if ($(this).data("mode") == view) $(this).addClass("active"); }); if(view != "agenda"){ if(c.currentView == "agenda"){ // $("#sec1").addClass("span3").removeClass("span7"); $("#sec2").show(); // $("#sec3").addClass("span4").removeClass("span5"); agendaView.hide(); } c.calendar_dom.calendar.changeView(view); }else{ // $("#sec1").addClass("span7").removeClass("span3"); $("#sec2").hide(); // $("#sec3").addClass("span5").removeClass("span4"); agendaView.inflate(); } c.currentView = view; if(loadeventsonviewchange){ c.calendar_dom.calendar.refetchEvents(); loadeventsonviewchange = false; } if(c.calendar_dom.calendar.currentData){ var viewTitle = c.calendar_dom.calendar.currentData.viewTitle; if(view == "timeGridDay" && $('.fc-col-header-cell-cushion ').text() != "") viewTitle = $('.fc-col-header-cell-cushion ').text() + ', ' + viewTitle; $('#current_title').html(viewTitle); } c.title.text(c.calendar_dom.calendar.currentData.viewTitle); // c.calendar_dom.calendar.rerenderEvents(); //Rerender to fix layout }; if(c.currentView == "agenda"){toggleViews("agenda");loadeventsonviewchange = true;} }; this.destroy = function () { c.calendar_dom.fullCalendar("destroy"); } this.renderEvent = function(eventStick){ if(eventStick.recurring === true) c.calendar_dom.calendar.refetchEvents(); else c.calendar_dom.calendar.addEvent(eventStick); }; $(document).ready(function() { c.initialize(); }); }; var EventDialog = function(calendar,event){ _t = this; var event_quick_view = null; var template = ""; var _this_event = null; this.inflate = function(_event){ if(!_event) throw new UserException("EventStick can't be null!"); _event.allDay = _event.event.allDay; _event._start = _event.event.start; _event._end = (_event.event.end ? _event.event.end : _event.event.start); // var start_date = getDateString(_event._start,date_format); // var end_date = getDateString(_event._end,date_format); if(_event._end - _event._start > 86400 * 1000){ _event.allDay = true; } _event.title = _event.event.title; var extendedProps = _event.event.extendedProps; Object.keys(extendedProps).forEach(function(k){ _event[k] = extendedProps[k]; }) if(!_event.hiring_person_name) _event.hiring_person_name = ""; _this_event = _event; var start_time = "", end_time = "", time_string = null; if(_event.allDay) { start_time = getDateString(_event._start,datetime_format, is_chinese); if(_event._end) end_time = getDateString(_event._end,datetime_format, is_chinese); time_string = (_event._start === _event._end || !_event._end ? "

" + start_time + "

" : "" + start_time + "
" + end_time + ""); } else { start_time = getDateString(_event._start,date_format, is_chinese); end_time = getDateString(_event._end,date_format, is_chinese); var stime = getDateString(_event._start,time_format, is_chinese), etime = getDateString(_event._end,time_format, is_chinese), same = (start_time == end_time); if( same ){ time_string = "

" + start_time + "

" + stime + " " + etime ; }else{ time_string = "" + start_time + " " + stime + "
" + end_time + " " + etime + "" } // time_string = (same ? "

" + start_time + "

" + stime + " " + etime : "

" + start_time + "" + stime + "

" + end_time + "" + etime + "

"); } event_quick_view = $(''); template = '