mirror of
https://github.com/renbaoshuo/S2OJ.git
synced 2024-11-22 18:48:41 +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)
|