2015-03-26 01:22:10 -04:00
|
|
|
class @Calendar
|
2016-05-16 07:51:49 -04:00
|
|
|
constructor: (timestamps, @calendar_activities_path) ->
|
|
|
|
@currentSelectedDate = ''
|
|
|
|
@daySpace = 1
|
|
|
|
@daySize = 15
|
|
|
|
@daySizeWithSpace = @daySize + (@daySpace * 2)
|
|
|
|
@monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
|
|
|
|
@months = []
|
|
|
|
@highestValue = 0
|
2016-04-28 06:03:14 -04:00
|
|
|
|
2016-04-26 08:21:48 -04:00
|
|
|
# Get the highest value from the timestampes
|
2016-05-16 07:51:49 -04:00
|
|
|
_.each timestamps, (count) =>
|
|
|
|
if count > @highestValue
|
|
|
|
@highestValue = count
|
2016-04-26 08:21:48 -04:00
|
|
|
|
2016-04-27 06:07:46 -04:00
|
|
|
# Loop through the timestamps to create a group of objects
|
|
|
|
# The group of objects will be grouped based on the day of the week they are
|
2016-05-16 07:51:49 -04:00
|
|
|
@timestampsTmp = []
|
2016-04-27 06:07:46 -04:00
|
|
|
i = 0
|
|
|
|
group = 0
|
2016-05-16 07:51:49 -04:00
|
|
|
_.each timestamps, (count, date) =>
|
2016-04-27 06:07:46 -04:00
|
|
|
newDate = new Date parseInt(date) * 1000
|
|
|
|
day = newDate.getDay()
|
|
|
|
|
|
|
|
# Create a new group array if this is the first day of the week
|
|
|
|
# or if is first object
|
|
|
|
if (day is 0 and i isnt 0) or i is 0
|
2016-05-16 07:51:49 -04:00
|
|
|
@timestampsTmp.push []
|
2016-04-27 06:07:46 -04:00
|
|
|
group++
|
|
|
|
|
2016-05-16 07:51:49 -04:00
|
|
|
innerArray = @timestampsTmp[group-1]
|
2016-04-27 06:07:46 -04:00
|
|
|
|
|
|
|
# Push to the inner array the values that will be used to render map
|
|
|
|
innerArray.push
|
|
|
|
count: count
|
|
|
|
date: newDate
|
|
|
|
day: day
|
|
|
|
|
|
|
|
i++
|
|
|
|
|
2016-05-16 07:51:49 -04:00
|
|
|
# Init color functions
|
|
|
|
@color = @initColor()
|
|
|
|
@colorKey = @initColorKey()
|
2016-04-27 06:07:46 -04:00
|
|
|
|
2016-05-16 07:51:49 -04:00
|
|
|
# Init the svg element
|
|
|
|
@renderSvg(group)
|
|
|
|
@renderDays()
|
|
|
|
@renderMonths()
|
|
|
|
@renderDayTitles()
|
|
|
|
@renderKey()
|
|
|
|
|
|
|
|
@initTooltips()
|
2016-04-26 08:21:48 -04:00
|
|
|
|
2016-05-16 07:51:49 -04:00
|
|
|
renderSvg: (group) ->
|
|
|
|
@svg = d3.select '.js-contrib-calendar'
|
2016-04-26 08:21:48 -04:00
|
|
|
.append 'svg'
|
2016-05-16 07:51:49 -04:00
|
|
|
.attr 'width', (group + 1) * @daySizeWithSpace
|
2016-04-27 06:07:46 -04:00
|
|
|
.attr 'height', 167
|
|
|
|
.attr 'class', 'contrib-calendar'
|
2016-04-26 08:21:48 -04:00
|
|
|
|
2016-05-16 07:51:49 -04:00
|
|
|
renderDays: ->
|
|
|
|
@svg.selectAll 'g'
|
|
|
|
.data @timestampsTmp
|
2016-04-26 08:21:48 -04:00
|
|
|
.enter()
|
|
|
|
.append 'g'
|
2016-05-16 07:51:49 -04:00
|
|
|
.attr 'transform', (group, i) =>
|
|
|
|
_.each group, (stamp, a) =>
|
2016-04-27 06:07:46 -04:00
|
|
|
if a is 0 and stamp.day is 0
|
|
|
|
month = stamp.date.getMonth()
|
2016-05-16 07:51:49 -04:00
|
|
|
x = (@daySizeWithSpace * i + 1) + @daySizeWithSpace
|
|
|
|
lastMonth = _.last(@months)
|
2016-04-27 06:07:46 -04:00
|
|
|
if lastMonth?
|
|
|
|
lastMonthX = lastMonth.x
|
2016-04-26 08:21:48 -04:00
|
|
|
|
2016-04-27 06:07:46 -04:00
|
|
|
if !lastMonth?
|
2016-05-16 07:51:49 -04:00
|
|
|
@months.push
|
2016-04-26 08:21:48 -04:00
|
|
|
month: month
|
|
|
|
x: x
|
2016-05-16 07:51:49 -04:00
|
|
|
else if month isnt lastMonth.month and x - @daySizeWithSpace isnt lastMonthX
|
|
|
|
@months.push
|
2016-04-27 06:07:46 -04:00
|
|
|
month: month
|
|
|
|
x: x
|
|
|
|
|
2016-05-16 07:51:49 -04:00
|
|
|
"translate(#{(@daySizeWithSpace * i + 1) + @daySizeWithSpace}, 18)"
|
2016-04-26 08:21:48 -04:00
|
|
|
.selectAll 'rect'
|
|
|
|
.data (stamp) ->
|
|
|
|
stamp
|
|
|
|
.enter()
|
|
|
|
.append 'rect'
|
|
|
|
.attr 'x', '0'
|
2016-05-16 07:51:49 -04:00
|
|
|
.attr 'y', (stamp, i) =>
|
|
|
|
(@daySizeWithSpace * stamp.day)
|
|
|
|
.attr 'width', @daySize
|
|
|
|
.attr 'height', @daySize
|
|
|
|
.attr 'title', (stamp) =>
|
2016-04-27 12:38:22 -04:00
|
|
|
contribText = 'No contributions'
|
|
|
|
|
|
|
|
if stamp.count > 0
|
|
|
|
contribText = "#{stamp.count} contribution#{if stamp.count > 1 then 's' else ''}"
|
|
|
|
|
|
|
|
date = dateFormat(stamp.date, 'mmm d, yyyy')
|
|
|
|
|
|
|
|
"#{contribText}<br />#{date}"
|
2016-04-27 06:07:46 -04:00
|
|
|
.attr 'class', 'user-contrib-cell js-tooltip'
|
2016-05-16 07:51:49 -04:00
|
|
|
.attr 'fill', (stamp) =>
|
2016-04-26 08:21:48 -04:00
|
|
|
if stamp.count isnt 0
|
2016-05-16 07:51:49 -04:00
|
|
|
@color(stamp.count)
|
2016-04-27 06:07:46 -04:00
|
|
|
else
|
|
|
|
'#ededed'
|
2016-04-26 08:21:48 -04:00
|
|
|
.attr 'data-container', 'body'
|
2016-05-16 07:51:49 -04:00
|
|
|
.on 'click', @clickDay
|
2016-04-26 08:21:48 -04:00
|
|
|
|
2016-05-16 07:51:49 -04:00
|
|
|
renderDayTitles: ->
|
2016-04-27 06:07:46 -04:00
|
|
|
days = [{
|
|
|
|
text: 'M'
|
2016-05-16 07:51:49 -04:00
|
|
|
y: 29 + (@daySizeWithSpace * 1)
|
2016-04-27 06:07:46 -04:00
|
|
|
}, {
|
|
|
|
text: 'W'
|
2016-05-16 07:51:49 -04:00
|
|
|
y: 29 + (@daySizeWithSpace * 3)
|
2016-04-27 06:07:46 -04:00
|
|
|
}, {
|
|
|
|
text: 'F'
|
2016-05-16 07:51:49 -04:00
|
|
|
y: 29 + (@daySizeWithSpace * 5)
|
2016-04-27 06:07:46 -04:00
|
|
|
}]
|
2016-05-16 07:51:49 -04:00
|
|
|
@svg.append 'g'
|
2016-04-27 06:07:46 -04:00
|
|
|
.selectAll 'text'
|
|
|
|
.data days
|
|
|
|
.enter()
|
|
|
|
.append 'text'
|
|
|
|
.attr 'text-anchor', 'middle'
|
|
|
|
.attr 'x', 8
|
|
|
|
.attr 'y', (day) ->
|
|
|
|
day.y
|
|
|
|
.text (day) ->
|
|
|
|
day.text
|
|
|
|
.attr 'class', 'user-contrib-text'
|
|
|
|
|
2016-05-16 07:51:49 -04:00
|
|
|
renderMonths: ->
|
|
|
|
@svg.append 'g'
|
|
|
|
.selectAll 'text'
|
|
|
|
.data @months
|
|
|
|
.enter()
|
|
|
|
.append 'text'
|
|
|
|
.attr 'x', (date) ->
|
|
|
|
date.x
|
|
|
|
.attr 'y', 10
|
|
|
|
.attr 'class', 'user-contrib-text'
|
|
|
|
.text (date) =>
|
|
|
|
@monthNames[date.month]
|
|
|
|
|
|
|
|
renderKey: ->
|
|
|
|
keyColors = ['#ededed', @colorKey(0), @colorKey(1), @colorKey(2), @colorKey(3)]
|
|
|
|
@svg.append 'g'
|
|
|
|
.attr 'transform', "translate(18, #{@daySizeWithSpace * 8 + 16})"
|
2016-04-27 06:07:46 -04:00
|
|
|
.selectAll 'rect'
|
|
|
|
.data keyColors
|
|
|
|
.enter()
|
|
|
|
.append 'rect'
|
2016-05-16 07:51:49 -04:00
|
|
|
.attr 'width', @daySize
|
|
|
|
.attr 'height', @daySize
|
|
|
|
.attr 'x', (color, i) =>
|
|
|
|
@daySizeWithSpace * i
|
2016-04-27 06:07:46 -04:00
|
|
|
.attr 'y', 0
|
|
|
|
.attr 'fill', (color) ->
|
|
|
|
color
|
|
|
|
|
2016-05-16 07:51:49 -04:00
|
|
|
initColor: ->
|
|
|
|
d3.scale
|
|
|
|
.linear()
|
|
|
|
.range(['#acd5f2', '#254e77'])
|
|
|
|
.domain([0, @highestValue])
|
|
|
|
|
|
|
|
initColorKey: ->
|
|
|
|
d3.scale
|
|
|
|
.linear()
|
|
|
|
.range(['#acd5f2', '#254e77'])
|
|
|
|
.domain([0, 3])
|
|
|
|
|
2016-05-27 11:35:20 -04:00
|
|
|
clickDay: (stamp) =>
|
2016-05-16 07:51:49 -04:00
|
|
|
if @currentSelectedDate isnt stamp.date
|
|
|
|
@currentSelectedDate = stamp.date
|
|
|
|
formatted_date = @currentSelectedDate.getFullYear() + "-" + (@currentSelectedDate.getMonth()+1) + "-" + @currentSelectedDate.getDate()
|
|
|
|
|
|
|
|
$.ajax
|
|
|
|
url: @calendar_activities_path
|
|
|
|
data:
|
|
|
|
date: formatted_date
|
|
|
|
cache: false
|
|
|
|
dataType: 'html'
|
|
|
|
beforeSend: ->
|
|
|
|
$('.user-calendar-activities').html '<div class="text-center"><i class="fa fa-spinner fa-spin user-calendar-activities-loading"></i></div>'
|
|
|
|
success: (data) ->
|
|
|
|
$('.user-calendar-activities').html data
|
|
|
|
else
|
|
|
|
$('.user-calendar-activities').html ''
|
|
|
|
|
|
|
|
initTooltips: ->
|
2016-04-27 06:07:46 -04:00
|
|
|
$('.js-contrib-calendar .js-tooltip').tooltip
|
2016-04-26 08:21:48 -04:00
|
|
|
html: true
|