/* -- DO NOT REMOVE -- * jQuery DCalendar 1.1 and DCalendar Picker 1.3 plugin * * Author: Dionlee Uy * Email: dionleeuy@gmail.com * * Date: Sat Mar 2 2013 * * @requires jQuery * -- DO NOT REMOVE -- */ if (typeof jQuery === 'undefined') { throw new Error('DCalendar.Picker: This plugin requires jQuery'); } +function ($) { Date.prototype.getDays = function() { return new Date(this.getFullYear(), this.getMonth() + 1, 0).getDate(); }; var months = ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'], short_months = ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'], daysofweek = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六']; var DCalendar = function(elem, options) { this.calendar = $(elem); this.today = new Date(); //system date //current selected date, default is today if no value given if(this.calendar.prev().val() === '') { this.date = new Date(); } else { var dateObj = this.reformatDate(this.calendar.prev().val()); this.date = isNaN(parseInt(dateObj.m)) ? new Date(dateObj.m + " " + dateObj.d + ", " + dateObj.y) : new Date(dateObj.y, dateObj.m - 1, dateObj.d); } this.viewMode = 'days'; this.options = options; this.selected = (this.date.getMonth() + 1) + "/" + this.date.getDate() + "/" + this.date.getFullYear(); this.minDate = this.calendar.prev().data('mindate'); this.maxDate = this.calendar.prev().data('maxdate'); if(options.mode === 'calendar') this.tHead = $('‹›一二三四五六日'); else if (options.mode === 'datepicker') this.tHead = $('‹›一二三四五六日'); this.tHead.find('#currM').text(months[this.today.getMonth()] +" " + this.today.getFullYear()); this.calendar.prepend(this.tHead); var that = this; this.calendar.on('click', '#next', function() { initCreate('next'); }) .on('click', '#prev', function() { initCreate('prev'); }) .on('click', '#today', function() { that.viewMode = 'days'; var curr = new Date(that.date), sys = new Date(that.today); if(curr.toString() != sys.toString()) { that.date = sys; } that.create('days'); }).on('click', '.date, .pMDate, .nMDate', function() { var isPrev = $(this).hasClass('pMDate'), isNext = $(this).hasClass('nMDate'), sdate = $(this).text(), cmonth = that.date.getMonth(), cyear = that.date.getFullYear(), min = that.minDate === "today" ? new Date(that.today.getFullYear(), that.today.getMonth(), that.today.getDate()) : new Date(that.minDate), max = that.maxDate === "today" ? new Date(that.today.getFullYear(), that.today.getMonth(), that.today.getDate()) : new Date(that.maxDate); /* Calculate year */ if(isPrev) { cyear = (cmonth === 0 ? cyear - 1 : cyear); } else if(isNext) { cyear = (cmonth + 2 === 13 ? cyear + 1 : cyear); } /* Calculate month */ if(isPrev) { cmonth = (cmonth === 0 ? '12' : cmonth); } else if (isNext) { cmonth = (cmonth + 2 === 13 ? '1' : cmonth + 2); } else { cmonth = cmonth + 1; } // Selected date var selected = new Date(cyear, cmonth - 1, sdate); if ((that.minDate && selected < min) || (that.maxDate && selected > max)) return; that.selected = cmonth + '/' + sdate + '/' + cyear; if(that.options.mode === 'datepicker') { that.calendar.find('td').removeClass('selected'); $(this).addClass('selected'); } that.selectDate(); return true; }).on('click', '#currM', function(){ that.viewMode = 'months'; that.create(that.viewMode); }).on('click', '.month', function(e){ that.viewMode = 'days'; var curr = new Date(that.date), y = that.calendar.find('#currM').text(); curr.setMonth($(e.currentTarget).attr('num')); that.date = curr; that.create(that.viewMode); }); function initCreate(o){ var curr = new Date(that.date), currMonth = curr.getMonth(), currYear = curr.getFullYear(); curr.setDate(1); if(that.viewMode === 'days') { curr.setMonth(currMonth + (o === 'next' ? 1 : -1)); } else { curr.setFullYear(currYear + (o === 'next' ? 1 : - 1)); } that.date = curr; that.create(that.viewMode); } this.create(this.viewMode); } DCalendar.prototype = { constructor : DCalendar, setDate : function() { var that = this, dateObj = that.reformatDate(that.calendar.prev().val()), value = isNaN(parseInt(dateObj.m)) ? new Date(dateObj.m + " " + dateObj.d + ", " + dateObj.y) : new Date(dateObj.y, dateObj.m - 1, dateObj.d); that.selected = (value.getMonth() + 1) + "/" + value.getDate() + "/" + value.getFullYear(); that.selectDate(); that.date = value; that.create(that.viewMode); }, selectDate : function () { var that = this, newDate = that.formatDate(that.options.format), e = $.Event('selectdate', {date: newDate}); that.showCurInfo(newDate); that.calendar.trigger(e); }, //展示current info showCurInfo : function (newDate) { var that = this; var curday_info; if (that.options.infoList) { for (var i = 0; i < that.options.infoList.length; i++) { if (newDate == that.options.infoList[i].meetdate) { curday_info = that.options.infoList[i].list; break; } } } }, reformatDate : function (date) { var that = this, format = that.options.format; return { m: date.substring(format.indexOf('m'), format.lastIndexOf('m') + 1), d: date.substring(format.indexOf('d'), format.lastIndexOf('d') + 1), y: date.substring(format.indexOf('y'), format.lastIndexOf('y') + 1) }; }, formatDate : function (format) { var that = this; var d = new Date(that.selected), day = d.getDate(), m = d.getMonth(), y = d.getFullYear(); return format.replace(/(yyyy|yy|mmmm|mmm|mm|m|dd|d)/gi, function (e) { switch(e.toLowerCase()){ case 'd': return day; case 'dd': return (day < 10 ? "0"+day: day); case 'm': return m+1; case 'mm': return (m+1 < 10 ? "0"+(m+1): (m+1)); case 'mmm': return short_months[m]; case 'mmmm': return months[m]; case 'yy': return y.toString().substr(2,2); case 'yyyy': return y; } }); }, create : function(mode){ var that = this, cal = [], tBody = $(''), d = new Date(that.date.getFullYear(), that.date.getMonth(), that.date.getDate()), days = that.date.getDays(), day = 1, nStartDate = 1, selDate = that.selected.split('/'), selected = new Date(selDate[2], selDate[0] -1, selDate[1]), today = new Date(that.today.getFullYear(), that.today.getMonth(), that.today.getDate()), min = that.minDate === "today" ? today : new Date(that.minDate), max = that.maxDate === "today" ? today : new Date(that.maxDate); that.getStuKqInfo(d, function() { that.calendar.empty(); if (mode === "days") { if (that.options.mode === 'calendar') { that.tHead = $('‹›一二三四五六日'); } else if (that.options.mode === 'datepicker') { that.tHead = $('‹›一二三四五六日'); } that.tHead.find('#currM').text(months[that.date.getMonth()] + " " + that.date.getFullYear()); that.calendar.append(that.tHead); /*//隐藏当月之后 if (d.getFullYear() == today.getFullYear() && (d.getMonth()+1) == (today.getMonth()+1)) { $("#next").hide(); }else{ $("#next").show(); }*/ var InfoListArr = that.options.infoList; for (var i = 1; i <= 6; i++) { var temp = [$(''), $(''), $(''), $(''), $(''), $(''), $('')]; var curday_info = []; //考勤增加 考勤数据 while (day <= days) { d.setDate(day); var dayOfWeek = (d.getDay() == 0 ? 7 : d.getDay()) - 1;//星期从周一开始 if (d.getTime() == today.getTime()) { temp[dayOfWeek].attr('id', 'currDay'); } curday_info = []; //更新日期 添加额外信息 if (InfoListArr != undefined) { if (i === 1 && dayOfWeek === 0) { } else { var syf_current_date = that.dateToString(d); for (var info_index = 0; info_index < InfoListArr.length; info_index++) { if (InfoListArr[info_index].meetdate == syf_current_date) { curday_info = InfoListArr[info_index].list; break; } } if (curday_info && curday_info.length > 0) { temp[dayOfWeek].addClass("hasData"); } else { temp[dayOfWeek].removeClass("hasData"); } } } if ((that.minDate && d < min) || (that.maxDate && d > max)) temp[dayOfWeek].addClass('disabled'); if (that.options.mode === 'datepicker' && d.getTime() == selected.getTime()) temp[dayOfWeek].addClass('selected'); if (i === 1 && dayOfWeek === 0) { break; } else if (dayOfWeek < 6) { temp[dayOfWeek].html('' + (day++) + '').addClass('date'); if (d.getTime() == today.getTime()) { temp[dayOfWeek].html('').addClass('date'); }else{ temp = that.addJobListToTable(curday_info,temp,dayOfWeek); } } else { if (d.getTime() == today.getTime()) { temp[dayOfWeek].html('').addClass('date'); var listStr = ""; console.info(curday_info.length) if (curday_info.length >= 2) { listStr += "
  • "; listStr += "

    " + curday_info[0]["companyName"] + "

    "; listStr += "
  • "; listStr += "

    " + curday_info[1]["companyName"] + "

    "; }else if(curday_info.length > 0){ listStr += "
  • "; listStr += "

    " + curday_info[0]["companyName"] + "

    "; }else{ listStr += "
  • 暂无信息

  • "; } $(".today_list ul").empty().append(listStr); day++; break; } else { temp[dayOfWeek].html('' + (day++) + '').addClass('date'); temp = that.addJobListToTable(curday_info,temp,dayOfWeek); break; } } } /* For days of previous and next month */ if (i === 1 || i > 4) { // First week if (i === 1) { var p = new Date(that.date.getFullYear(), that.date.getMonth() - 1, 1), pMonth = p.getMonth(), pDays = p.getDays(); for (var a = 6; a >= 0; a--) { if (temp[a].text() === '') { p.setDate(pDays); temp[a].html('' + (pDays--) + '').addClass('pMDate'); if ((that.minDate && p < min) || (that.maxDate && p > max)) temp[a].addClass('disabled'); if (that.options.mode === 'datepicker' && p.getTime() == selected.getTime()) temp[a].addClass('selected'); } } } // Last week else if (i > 4) { var nextMonth = new Date(that.date.getFullYear(), that.date.getMonth() + 1, 1); for (var a = 0; a <= 6; a++) { if (temp[a].text() === '') { nextMonth.setDate(nStartDate); temp[a].html('' + (nStartDate++) + '').addClass('nMDate'); if ((that.minDate && nextMonth < min) || (that.maxDate && nextMonth > max)) temp[a].addClass('disabled'); if (that.options.mode === 'datepicker' && nextMonth.getTime() == selected.getTime()) temp[a].addClass('selected'); } } } } cal.push(temp); } that.selected = that.date.getFullYear() + "-" + (that.date.getMonth() + 1) + "-01"; that.selectDate(); $.each(cal, function (i, v) { var row = $(''), l = v.length; for (var i = 0; i < l; i++) { row.append(v[i]); } tBody.append(row); }); var sysDate = "今天: " + that.today.getFullYear() + "年" + months[that.today.getMonth()] + "" + that.today.getDate() + "日 " + daysofweek[that.today.getDay()]; // tBody.append('' + sysDate + '').appendTo(that.calendar); tBody.append('').appendTo(that.calendar); } else { this.tHead = $('‹›'); that.tHead.find('#currM').text(that.date.getFullYear()); that.tHead.appendTo(that.calendar); var currI = 0; for (var i = 0; i < 3; i++) { var row = $(''); for (var x = 0; x < 4; x++) { var col = $(''); var m = $('' + short_months[currI] + ''); col.append(m).appendTo(row); currI++; } tBody.append(row); } var sysDate = "Today: " + daysofweek[that.today.getDay()] + ", " + months[that.today.getMonth()] + " " + that.today.getDate() + ", " + that.today.getFullYear(); // tBody.append('' + sysDate + '').appendTo(that.calendar); tBody.append('').appendTo(that.calendar); } }); }, //考勤增加 将日期转换成字符串 dateToString: function (d){ var syf_month = (d.getMonth()+1) < 10 ? ("0"+(d.getMonth()+1)) : (d.getMonth()+1); var syf_day = d.getDate() < 10 ? ("0"+d.getDate()) : d.getDate(); return d.getFullYear() + "-" + syf_month + "-" + syf_day; }, addJobListToTable: function(curday_info,temp,dayOfWeek){ if(curday_info.length > 0){ var htmlStr = "
    "; for (var j = 0; j < curday_info.length; j++) { if(curday_info[j]['sourceType'] == 1){ var href = "https://51uns.com/#/public/xbxjh_detail?id=" + curday_info[j]['id'] + "&code="+schoolCode + "&type=" + curday_info[j]['typeId']; }else{ var href = "https://51uns.com/#/public/xjh_detail?id=" + curday_info[j]['id'] + "&code="+schoolCode + "&type=" + curday_info[j]['typeId']; } htmlStr += "

    " + curday_info[j]["companyName"] +"

    "; } htmlStr += "
    "; temp[dayOfWeek].append(htmlStr); } return temp; }, //增加 获取招聘信息 getStuKqInfo: function (date, callback) { var that = this; //专场招聘会 var zcListDate = []; var zcList = []; $(".calendar .loadingImg").show(); $.ajax({ url: requestHead + "/api/anonymous/getLectureDatas", type: 'POST', data: getParam(300, 1), success: function (AjaxResult) { zcListDate = []; zcList = []; if (AjaxResult) { AjaxResult = JSON.parse(AjaxResult.data); if(AjaxResult.rows.length > 0){ $.each(AjaxResult.rows, function(index, obj) { var meetdate = obj.startTime.replace(/-/ig, '/'); d = fixDate(meetdate); if (zcListDate.indexOf(d) < 0) { zcListDate.push(d); zcList.push({ meetdate: d, list: [{ id: obj.id, companyName: obj.companyName, meetDate: d,sourceType:obj.sourceType, typeId:obj.typeId}]}); } else { for (var j = 0; j < zcList.length; j++) { if (zcList[j]["meetdate"] == d + "") { zcList[j].list.push({ id: obj.id, companyName: obj.companyName, meetDate: d, sourceType:obj.sourceType, typeId:obj.typeId }) break; } } } }); } } that.options.infoList = zcList; $(".calendar .loadingImg").hide(); callback(); }, error: function () { $(".calendar .loadingImg").hide(); } }); } } /* DEFINITION FOR DCALENDAR */ $.fn.dcalendar = function(opts){ return $(this).each(function(index, elem){ var that = this; var $this = $(that), data = $(that).data('dcalendar'), options = $.extend({}, $.fn.dcalendar.defaults, $this.data(), typeof opts === 'object' && opts); if(!data){ $this.data('dcalendar', (data = new DCalendar(this, options))); } if(typeof opts === 'string') data[opts](); }); } $.fn.dcalendar.defaults = { mode: 'calendar', format: 'yyyy-mm-dd', }; $.fn.dcalendar.Constructor = DCalendar; /* DEFINITION FOR DCALENDAR PICKER */ $.fn.dcalendarpicker = function(opts){ return $(this).each(function(){ var that = $(this), hovered = false, selectedDate = false, cal = null; if(typeof opts === 'string') { var data = that.next('.calendar').data('dcalendar'); data[opts](); } else { cal = $('
    '); that.wrap($('
    ')); cal.css({ position:'absolute', left:0, display:'none', 'box-shadow':'0 4px 6px 1px rgba(0, 0, 0, 0.14)', width:'230px', }).appendTo(that.parent()); if(opts){ opts.mode = 'datepicker'; cal.dcalendar(opts); } else{ cal.dcalendar({mode: 'datepicker'}); } cal.hover(function(){ hovered = true; }, function(){ hovered = false; }).on('click', function(){ if(!selectedDate) that.focus(); else { selectedDate = false; $(this).hide(); } }).on('selectdate', function(e){ that.val(e.date).trigger('onchange'); that.trigger($.Event('dateselected', {date: e.date, elem: that})); selectedDate = true; }); that.on('keydown', function(e){ if(e.which) return false; }) .on('focus', function(){ $('.datepicker').find('.calendar').not(cal).hide(); cal.show(); }).on('blur', function(){ if(!hovered) cal.hide(); }); } }); } }(jQuery);