window.auto_close_popup = false; if(window.calendar_variable == undefined){ window.calendar_variable = {}; window.calendar_variable.is_chinese = ( I18n && I18n.locale.indexOf('zh') != -1 ); window.calendar_variable.date_type = 0; }else{ if(window.calendar_variable.date_type == 1){ window.calendar_variable.is_chinese = false; }else{ window.calendar_variable.is_chinese = ( I18n && I18n.locale.indexOf('zh') != -1 ); } } if(!(window.calendar_variable.dayNames)){ if(window.calendar_variable.is_chinese){ window.calendar_variable.dayNames = ['週日', '週一', '週二', '週三','週四', '週五', '週六']; window.calendar_variable.dayNamesShort = ['日', '一', '二', '三','四', '五', '六']; }else{ window.calendar_variable.dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday','Thursday', 'Friday', 'Saturday']; window.calendar_variable.dayNamesShort = ['Sun', 'Mon', 'Tue', 'Wed','Thu', 'Fri', 'Sat']; } } if(window.calendar_variable.is_chinese){ window.calendar_variable.months = []; for(var i=0;i<12;i++){ window.calendar_variable.months.push((i+1)+"月"); } }else{ window.calendar_variable.months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; } if(window.calendar_variable.date_type == 2){ window.calendar_variable.month_year_format = calendar_variable.is_chinese ? 'yy/MM' : 'MM/yy'; window.calendar_variable.date_format = calendar_variable.is_chinese ? 'yy/MM/dd' : 'dd/MM/yy'; window.calendar_variable.date_format_with_short_month = calendar_variable.is_chinese ? 'yy/MM/dd' : 'dd/MM/yy'; window.calendar_variable.date_format_with_week = calendar_variable.is_chinese ? 'yy/MM/dd (W)' : 'W, MM/dd/yy'; window.calendar_variable.short_date = (calendar_variable.is_chinese ? "MM/dd (W)" : "W, dd/MM"); if(window.calendar_variable.time_type == 1){ window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'yy/MM/dd H:m' : 'dd/MM/yy H:m'; window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MM/dd (W) H:m" : "W, dd/MM H:m"); }else{ window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'yy/MM/dd b h:m' : 'dd/MM/yy h:m b'; window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MM/dd (W) b h:m" : "W, dd/MM h:m b"); } }else{ window.calendar_variable.month_year_format = calendar_variable.is_chinese ? 'y MMM' : 'MMM y'; window.calendar_variable.date_format = calendar_variable.is_chinese ? 'y MMM d' : 'd MMM, y'; window.calendar_variable.date_format_with_short_month = calendar_variable.is_chinese ? 'y M d' : 'd M, y'; window.calendar_variable.date_format_with_week = calendar_variable.is_chinese ? 'y MMM d (W)' : 'W, MMM d, y'; window.calendar_variable.short_date = (calendar_variable.is_chinese ? "MMM d (W)" : "W, d MMM"); if(window.calendar_variable.time_type == 1){ window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'y MMM d H:m' : 'd MMM, y H:m'; window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MMM d (W) H:m" : "W, d MMM H:m"); }else{ window.calendar_variable.datetime_format = calendar_variable.is_chinese ? 'y MMM d b h:m' : 'd MMM, y h:m b'; window.calendar_variable.short_date_time = (calendar_variable.is_chinese ? "MMM d (W) b h:m" : "W, d MMM h:m b"); } } window.calendar_variable.date_format_numeric = 'yy/MM/dd'; window.calendar_variable.short_date_numeric = (calendar_variable.is_chinese ? "MM/dd (w)" : "w MM/dd"); if(window.calendar_variable.time_type == 1){ window.calendar_variable.time_format = "H:m"; }else{ window.calendar_variable.time_format = (calendar_variable.is_chinese ? "b h:m" : "h:m b"); } window.calendar_variable.std_date_format = 'y-MM-d'; window.calendar_variable.short_day = (calendar_variable.is_chinese ? "d (W)" : "W d"); $.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.getDateString = function(date, format, is_chinese) { var months = calendar_variable.months; var week_days = calendar_variable.dayNames; var getPaddedComp = function(comp) { return ((parseInt(comp) < 10) ? ('0' + comp) : comp) }, formattedDate = format, y = date.getFullYear(), m = date.getMonth(), d = date.getDate(), H = date.getHours(), M = date.getMinutes(), s = date.getSeconds(), S = date.getMilliseconds(), month_name = months[m], am_trans = (calendar_variable.is_chinese ? '上午' : 'AM'), pm_trans = (calendar_variable.is_chinese ? '下午' : 'PM'), o = { "yy+": y, //raw year "y+": y + (calendar_variable.is_chinese ? "年" : ""), // year "MMM+": month_name, //month "MM+": getPaddedComp(m + 1), //raw month "M+": month_name.substring(0,3), //month "dd+": getPaddedComp(d), //raw day "d+": (calendar_variable.is_chinese ? (d + "日") : getPaddedComp(d)), //day "W+": week_days[date.getDay()], //weekday "w+": (calendar_variable.is_chinese ? week_days[date.getDay()].substr(-1, 1) : week_days[date.getDay()].substr(0, 3)), //weekday "h+": getPaddedComp((H > 12) ? H % 12 : H), //hour "H+": getPaddedComp(H), //hour "m+": getPaddedComp(M), //minute "s+": getPaddedComp(s), //second "S+": getPaddedComp(S), //millisecond, "b+": (H >= 12) ? pm_trans : am_trans }; for (var k in o) { if (new RegExp("(" + k + ")").test(format)) { formattedDate = formattedDate.replace(RegExp.$1, o[k]); } } return formattedDate; }; var Calendar = function(dom){ var c = this; this.create_event_btn = $("#create_event_btn"); this.event_create_space = $("#event_create_space"); this.mousePosition = {}; 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 = "dayGridMonth"; this.navigation = $("#navigation"); this.rangeSelection = $("#range_selection"); var agendaView = new AgendaView(c); var loadeventsonviewchange = false; this.success_event = null; this.initialize = function(){ var date = new Date(); var d = date.getDate(); var m = date.getMonth(); var y = date.getFullYear(); var change_event = function(_event, delta) { _event.end = (_event.end ? _event.end : _event.start); var s = $.fullCalendar.parseDate(c.calendar_dom.find(".fc-view table tbody td:first").data("date")); var e = $.fullCalendar.parseDate(c.calendar_dom.find(".fc-view table tbody td:first").data("date")); $.ajax({ url: "/admin/calendars/"+_event.id, // data: {event:{id:_event.id,start:_event.start,end: _event.end,_s:Math.round(+s / 1000), _e:Math.round(+e / 1000)}}, data: {event:{id:_event.id,start:_event.start,end: _event.end}}, type: 'put' , datatype: 'JSON', error: function(jqXHR, textStatus, errorThrown) {}, success: function(data) { console.log('event was success updated'); } }); } var success_event = function(data,allDay,type,addbtn){ c.dialog.dismiss(); $('.bootstrap-datetimepicker-widget').remove(); c.event_create_space.html(data); var create_space_height = c.event_create_space.height(), create_space_width = c.event_create_space.width(); if((create_space_height + c.mousePosition["y"]) >= $(window).height()){ var top_pos = c.mousePosition["y"] - create_space_height if (top_pos < 0){ c.event_create_space.find('.modal-body').css('height',create_space_height+top_pos) c.event_create_space.css("top",0 + "px"); }else{ c.event_create_space.css("top",top_pos + "px"); } }else if(c.mousePosition["y"]+c.event_create_space.height()<$(window).height()){ c.event_create_space.css("top",c.mousePosition["y"] + "px"); } if((create_space_width + c.mousePosition["x"]) >= $(window).width()){ if (c.mousePosition["x"] - create_space_width < 0){ c.event_create_space.css("left",'0px') }else{ c.event_create_space.css("left",(c.mousePosition["x"] - create_space_width) + "px"); } }else{ c.event_create_space.css("left",c.mousePosition["x"] + "px"); } if(addbtn){ c.event_create_space.css({"right":"8px","bottom":"50px","left":"auto","top":"auto"}); }else{ c.event_create_space.css({"right":"","bottom":""}); } c.event_create_space.show(); var pickers = $('.default_picker input'); var checked = ($("#all_day_check").is(":checked") ? true : false); var checked_function = function(c){ var d = new Date() if(c){ for(i=0;i 9 ? start.getMonth() + 1 : "0" + (start.getMonth() + 1)) + "/" + (start.getDate() > 9 ? start.getDate() : "0" + start.getDate()), endString = end.getFullYear() + "/" + (end.getMonth() + 1 > 9 ? end.getMonth() + 1 : "0" + (end.getMonth() + 1)) + "/" + (end.getDate() > 9 ? end.getDate() : "0" + end.getDate()); if(!allDay){ startString += " " + (start.getHours() > 9 ? start.getHours() : "0" + start.getHours()) + ":" + (start.getMinutes() > 9 ? start.getMinutes() : "0" + start.getMinutes()); endString += " " + (end.getHours() > 9 ? end.getHours() : "0" + end.getHours()) + ":" + (end.getMinutes() > 9 ? end.getMinutes() : "0" + end.getMinutes()); }else{ startString += " " + start.getHours() + ":" + start.getMinutes(); endString += " " + end.getMinutes() + ":" + end.getMinutes(); } $.ajax({ type : "get", url : c.create_event_btn.attr("href"), data : {"startDate":startString,"endDate":endString,"allDay":allDay}, success : function(data){ success_event(data,allDay,"new"); } }) }, views: { timeGridDay: { titleFormat: function(date_info){ var date = date_info.date.marker; return getDateString(date, calendar_variable.date_format_with_week); }, dayHeaderFormat: function(date_info){ return window.calendar_variable.dayNames[date_info.date.marker.getDay()]; } }, timeGridWeek: { titleFormat: function(date_info){ var start = date_info.start.marker, end = date_info.end.marker; return getDateString(start, calendar_variable.date_format) + ' ~ ' + getDateString(end, calendar_variable.date_format); }, dayHeaderFormat: function(date_info){ return getDateString(date_info.date.marker, calendar_variable.short_date_numeric); } }, dayGridMonth: { dayMaxEvents: false, titleFormat: function(date_info){ var date = date_info.date.marker; return getDateString(date, calendar_variable.month_year_format); }, dayHeaderFormat: function(date_info){ return window.calendar_variable.dayNamesShort[date_info.date.marker.getDay()]; } } }, firstDay: (window.calendar_variable.sunday_first == true ? 0 : 1) }); c.create_event_btn.click(function(){ $.ajax({ type : "get", url : $(this).attr("href"), success : function(data){ success_event(data,false,"new",true); } }) return false; }); 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(); }); c.calendar_dom.mouseup(function(e){ c.mousePosition = {"x" : e.pageX, "y" : e.pageY}; }) 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; } 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.updateEvent = function(eventStick){ c.calendar_dom.fullCalendar("updateEvent",eventStick); } this.deleteEvent = function(delete_url,_id){ $.ajax({ type : "delete", url : delete_url, success : function(){ c.calendar_dom.fullCalendar("removeEvents",[_id]); c.dialog.dismiss(); } }) } this.editEvent = function(edit_url,allDay){ $.ajax({ type : "get", url : edit_url, success : function(data){ c.success_event(data,allDay,"edit",true); c.dialog.dismiss(); } }) } 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,calendar_variable.date_format); // var end_date = getDateString(_event._end,calendar_variable.date_format); _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 == undefined) _event.hiring_person_name = ""; _this_event = _event; var start_time = "", end_time = "", time_string = null; if(_event.allDay) { start_time = getDateString(_event._start,calendar_variable.date_format_with_short_month); if(_event._end) end_time = getDateString(_event._end,calendar_variable.date_format_with_short_month); time_string = (_event._start === _event._end || !_event._end ? "

" + start_time + "

" : "" + start_time + "
" + end_time + ""); } else { start_time = getDateString(_event._start,calendar_variable.date_format_with_short_month); end_time = getDateString(_event._end,calendar_variable.date_format_with_short_month); var stime = getDateString(_event._start,calendar_variable.time_format), etime = getDateString(_event._end,calendar_variable.time_format), 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 = '