mirror of
https://github.com/renbaoshuo/S2OJ.git
synced 2024-11-25 04:28:45 +00:00
11 lines
8.5 KiB
JavaScript
11 lines
8.5 KiB
JavaScript
/*
|
|
* jquery-calendar-heatmap - v1.3.0
|
|
* A simple Calendar Heatmap for jQuery.
|
|
* https://github.com/SeBassTian23/CalendarHeatmap
|
|
*
|
|
* Made by Sebastian Kuhlgert
|
|
* Under MIT License
|
|
*/
|
|
|
|
!function(b){"use strict";var n="CalendarHeatmap",a={title:null,months:12,weekStartDay:1,lastMonth:(new Date).getMonth()+1,lastYear:(new Date).getFullYear(),coloring:null,labels:{days:!1,months:!0,custom:{weekDayLabels:null,monthLabels:null}},tiles:{shape:"square"},legend:{show:!0,align:"right",minLabel:"Less",maxLabel:"More",divider:" to "},tooltips:{show:!1,options:{}}};function i(t,e,s){this.element=t,this.data=e,this.settings=b.extend(!0,{},a,s),this._defaults=a,this._name=n,this.init()}b.extend(i.prototype,{init:function(){this.calendarHeatmap()},_parse:function(t){var e=[];if(!Array.isArray(t)||"object"!=typeof t)return console.log("Invalid data source"),null;if(Array.isArray(t)&&0<t.length){var s=typeof t[0];if("object"!=typeof t[0]||Array.isArray(t[0])){if(-1<["string","date","number"].indexOf(s)){if(this._dateValid(t[0])){var a,n,i={};for(a in t){var l=this._dateFormat(t[a]);i[l]?i[l]+=1:i[l]=1}for(n in e=[],i)e.push({count:parseInt(i[n]),date:n});return e}return console.log("Invalid Date format."),null}return console.log("Invalid format."),null}if(t[0].date&&t[0].count){for(var r in e=[],t){var h=t[r].date;this._isNumeric(t[r].date)&&(h=parseInt(t[r].date)),e.push({count:parseInt(t[r].count),date:this._dateFormat(h)})}return e}return console.log("Invalid Object format."),null}if(Array.isArray(t)&&0===t.length)return[];if("object"!=typeof t||Object.empty(t))return null;s=Object.keys(t);if(!this._dateValid(s[0]))return console.log("Invalid Date format."),null;if(this._isNumeric(t[s[0]])){var d,o=[];for(d in t)o.push({count:parseInt(t[d]),date:this._dateFormat(d)});return o}},_pad:function(t,e){return(t=String(t)).length<e?this._pad("0"+t,e):t},_calculateBins:function(t){var e,s=this.settings.steps||4,a=["0"],n=[[0,0]],i=t.map(function(t){return parseInt(t.count)}),l=Math.min.apply(Math,i),i=Math.max.apply(Math,i),r=Math.ceil(i/s);for(0===r&&(r=i/s)<1&&(r=1),n=[[0,0]],a=Number.isFinite(l)?["0"]:[""],e=0;e<s;e++){var h=r*e+1,d=r*(e+1);n.push([h,d]),!Number.isFinite(l)||Number.isNaN(h)||!Number.isFinite(h)?a.push(""):h==d?a.push(String(h)):a.push(String(h)+(this.settings.legend.divider||" to ")+String(d))}for(e in t)t[e].level=this._matchBin(n,t[e].count);return{events:t,bins:a}},_matchBin:function(t,e){for(var s in t)if(e>=t[s][0]&&e<=t[s][1])return parseInt(s);return 0},_matchDate:function(t,e){return t.find(function(t){return t.date===e})||null},_matchDateIdx:function(t,e){return t.findIndex(function(t){return t.date===e})},_futureDate:function(t){var e=this._dateFormat().split("-"),t=t.split("-");return parseInt(e[0])<parseInt(t[0])||(parseInt(e[0])===parseInt(t[0])&&parseInt(e[1])<parseInt(t[1])||parseInt(e[0])===parseInt(t[0])&&parseInt(e[1])===parseInt(t[1])&&parseInt(e[2])<parseInt(t[2]))},_isNumeric:function(t){return!isNaN(parseFloat(t))&&isFinite(t)},_dateValid:function(t){return!!(String(t).match(/^(\d{4}-\d{2}-\d{2})$/)||String(t).match(/^(\d{1,13})$/)&&"number"==typeof t)},_dateWords:{MMM:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],MMMM:["January","February","March","April","May","June","July","August","September","October","November","December"],dd:["Su","Mo","Tu","We","Th","Fr","Sa"],ddd:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],dddd:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]},_dateFormat:function(e,s){void 0===e?e=new Date:"string"==typeof e&&(e=e.split("-"),e=new Date(parseInt(e[0]),parseInt(e[1])-1,parseInt(e[2]))),void 0===s&&(s="YYYY-MM-DD");var a=this._dateWords;return s.replace(/(Y{2,4})|(M{1,4})|(d{1,4})|(D{1,2})/g,function(t){return"YY"===t?parseInt(e.getFullYear().toString().slice(2,4)):"YYYY"===t?e.getFullYear():"M"===t?e.getMonth()+1:"MM"===t?(s=String(e.getMonth()+1)).length<2?"0"+s:s:"MMM"===t||"MMMM"===t?a[t][e.getMonth()]:"D"===t?e.getDate():"DD"===t?(s=String(e.getDate())).length<2?"0"+s:s:"d"===t?e.getDay()+1:"dd"===t||"ddd"===t||"dddd"===t?a[t][e.getDay()]:t})},_addWeekColumn:function(){if(this.settings.labels.days){b(".ch-year",this.element).append('<div class="ch-week-labels"></div>'),b(".ch-week-labels",this.element).append('<div class="ch-week-label-col"></div>'),b(".ch-week-label-col",this.element).append('<div class="ch-day-labels"></div>'),this.settings.labels.months&&b(".ch-week-labels",this.element).append('<div class="ch-month-label"> </div>');for(var t=this.settings.weekStartDay,e=0;e<7;e++){var s,a=e+t<7?e+t:e+t-7,n=this._dateWords.ddd[a];(e-1)%2?(s=this.settings.labels.custom.weekDayLabels,Array.isArray(s)?n=s[a]||"":"string"==typeof s?n=this._dateWords[s][a]||"":"function"==typeof s&&(n=s(a))):n=" ",b("<div>",{class:"ch-day-label",html:n}).appendTo(b(".ch-day-labels",this.element))}}},calendarHeatmap:function(){var t=this._parse(this.data);if(Array.isArray(t)){this.data=t;var e=this._calculateBins(t),s=e.events,a=e.bins,t=this.settings.lastMonth,e=this.settings.lastYear,n=this.settings.months,i=this.settings.weekStartDay||1;b(this.element).empty(),this.settings.title&&b("<h3>",{class:"ch-title",html:this.settings.title}).appendTo(b(this.element)),b(this.element).addClass("ch").append('<div class="ch-year"></div>'),this._addWeekColumn(),this.settings.tiles.shape&&"square"!==this.settings.tiles.shape&&b(this.element).addClass(" ch-"+this.settings.tiles.shape);for(var l=t,r=e,h=[],d=0;d<n;d++)(l+=-1)<0&&(--r,l+=12),h.push([l,r]);for(h.reverse(),d=0;d<h.length;d++){var l=h[d][0],r=h[d][1],o=this._dateFormat(r+"-"+(l+1)+"-1","MMM"),c=this.settings.labels.custom.monthLabels;c&&(o=Array.isArray(c)?c[l]||"":"function"==typeof c?c(r,l+1):this._dateFormat(r+"-"+(l+1)+"-1",c)),b(".ch-year",this.element).append('<div class="ch-month"></div>'),b(".ch-month:last",this.element).append('<div class="ch-weeks"></div>'),this.settings.labels.months&&b(".ch-month:last",this.element).append('<div class="ch-month-label">'+o+"</div>");var p=new Date(r,l+1,0).getDate();b(".ch-month:last .ch-weeks",this.element).append('<div class="ch-week"></div>');for(var u=0,g=0;g<p;g++){var m=r+"-"+this._pad(l+1,2);m+="-"+this._pad(g+1,2);var f,v=this._matchDate(s,m),y="";this._futureDate(m)&&(y=" is-after-today"),v?(f=v.count+" on ",f+=this._dateFormat(v.date,"MMM D, YYYY"),m="",this.settings.coloring&&(m=" "+this.settings.coloring+"-"+v.level),b("<div/>",{class:"ch-day lvl-"+v.level+m,title:f,"data-toggle":"tooltip"}).appendTo(b(".ch-month:last .ch-weeks .ch-week:last",this.element))):b("<div/>",{class:"ch-day"+y}).appendTo(b(".ch-month:last .ch-weeks .ch-week:last",this.element));y=new Date(r,l,g+2).getDay();u++,(y=0===y?7:y)===i&&g<p-1&&(b(".ch-month:last .ch-weeks",this.element).append('<div class="ch-week">'+g+"</div>"),u=0)}for(;u<7;u++)b(".ch-month:last .ch-weeks .ch-week:last",this.element).append('<div class="ch-day is-outside-month"></div>')}if(this.settings.legend.show)for(b("<div>",{class:"ch-legend"}).appendTo(this.element).append("<small>"+(this.settings.legend.minLabel||"")+"</small>").append('<ul class="ch-lvls"></ul>').append("<small>"+(this.settings.legend.maxLabel||"")+"</small>"),"left"===this.settings.legend.align&&b(".ch-legend",this.element).addClass("ch-legend-left"),"center"===this.settings.legend.align&&b(".ch-legend",this.element).addClass("ch-legend-center"),d=0;d<a.length;d++)b("<li>",{class:"ch-lvl lvl-"+d,title:a[d],"data-toggle":"tooltip"}).appendTo(b(".ch-lvls",this.element)),this.settings.coloring&&b(".ch-lvls li:last",this.element).addClass(this.settings.coloring+"-"+d);this.settings.tooltips.show&&"function"==typeof b.fn.tooltip&&b('[data-toggle="tooltip"]',this.element).tooltip(this.settings.tooltips.options)}},updateDates:function(t){this.data=t,this.calendarHeatmap()},appendDates:function(t){var e=this._parse(t);if(Array.isArray(e)&&Array.isArray(this.data))for(var s in e){var a=this._matchDateIdx(this.data,e[s].date);-1<a?this.data[a].count+=e[s].count:this.data.push(e[s])}this.calendarHeatmap()},updateOptions:function(t){this.settings=b.extend(!0,{},this.settings,t),this.calendarHeatmap()},getDates:function(){return this.data},getOptions:function(){return this.settings}}),b.fn[n]=function(e,t){var s,a=arguments;return Array.isArray(e)?this.each(function(){b.data(this,"plugin_"+n)||b.data(this,"plugin_"+n,new i(this,e,t))}):"string"==typeof e&&"_"!==e[0]&&"init"!==e?(this.each(function(){var t=b.data(this,"plugin_"+n);t instanceof i&&"function"==typeof t[e]&&(s=t[e].apply(t,Array.prototype.slice.call(a,1))),"destroy"===e&&b(this).removeData()}),void 0!==s?s:this):void 0}}(jQuery);
|