File size: 9,905 Bytes
eb67da4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
import namedavatar from 'namedavatar'
import { QuillDeltaToHtmlConverter } from 'quill-delta-to-html'
import './projectchart.html'
import Projects, { ProjectStats } from '../../api/projects/projects.js'
import projectUsers from '../../api/users/users.js'
import { getUserSetting, getUserTimeUnitVerbose } from '../../utils/frontend_helpers'
Template.projectchart.onCreated(function projectchartCreated() {
this.topTasks = new ReactiveVar()
this.projectDescAsHtml = new ReactiveVar()
this.isVisible = new ReactiveVar(false)
})
Template.projectchart.helpers({
totalHours() {
const precision = getUserSetting('precision')
return ProjectStats.findOne({ _id: Template.instance().data.projectId })
? Number(ProjectStats.findOne({
_id: Template.instance().data.projectId,
}).totalHours ? ProjectStats.findOne({
_id: Template.instance().data.projectId,
}).totalHours : 0).toFixed(precision)
: false
},
hourIndicator() {
const stats = ProjectStats.findOne({ _id: Template.instance().data.projectId })
if (stats.previousMonthHours > stats.currentMonthHours) {
return '<i class="d-md-none fa fa-arrow-circle-up"></i>'
}
if (stats.previousMonthHours < stats.currentMonthHours) {
return '<i class="d-md-none fa fa-arrow-circle-down"></i>'
}
return '<i class="d-md-none fa fa-minus-square"></i>'
},
allTeamMembers() {
return projectUsers.findOne({ _id: Template.instance().data.projectId })
? projectUsers.findOne({ _id: Template.instance().data.projectId }).users : false
},
avatarImg(avatar, name, avatarColor) {
if (avatar) {
return `<img src="${avatar}" alt="${name}" style="height:25px; cursor:pointer;" class="rounded js-avatar-tooltip" data-bs-placement="top" title="${name}"/>`
}
namedavatar.config({
nameType: 'initials',
backgroundColors: [avatarColor || '#455A64'],
minFontSize: 2,
})
const rawSVG = namedavatar.getSVG(name)
rawSVG.classList = 'rounded js-avatar-tooltip'
rawSVG.style.width = '25px'
rawSVG.style.height = '25px'
rawSVG.style.cursor = 'pointer'
rawSVG.setAttribute('title', name)
return rawSVG.outerHTML
},
topTasks() {
return Template.instance().topTasks.get()
},
turnOver() {
const precision = getUserSetting('precision')
const project = Projects.findOne({ _id: Template.instance().data.projectId })
return project && project.rate && project.totalHours
? Number(project.rate * project.totalHours).toFixed(precision) : false
},
target() {
return Number(Projects.findOne({ _id: Template.instance().data.projectId }).target) > 0
? Projects.findOne({ _id: Template.instance().data.projectId }).target : false
},
projectDescAsHtml: () => encodeURI(Template.instance().projectDescAsHtml.get()),
truncatedProjectDescAsHtml: () => (Template.instance().projectDescAsHtml.get()
? Template.instance().projectDescAsHtml.get().replace('<p>', '<p style="max-height:1.9em;pointer-events:none;" class="text-truncate p-0 m-0">') : ''),
componentIsReady() {
return Template.instance().isVisible.get() && Template.instance().subscriptionsReady()
},
})
Template.projectchart.onRendered(() => {
const templateInstance = Template.instance()
const precision = getUserSetting('precision')
templateInstance.observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
templateInstance.isVisible.set(true)
templateInstance.observer.unobserve(templateInstance.firstNode)
}
})
})
templateInstance.observer.observe(templateInstance.firstNode)
templateInstance.autorun(() => {
if (templateInstance.subscriptionsReady() && templateInstance.projectDescAsHtml.get()) {
import('bootstrap').then((bs) => {
const tooltip = new bs.Tooltip(templateInstance.$('.js-tooltip').get(0), {
title: templateInstance.projectDescAsHtml.get(),
html: true,
placement: 'right',
trigger: 'hover focus',
})
})
}
})
templateInstance.autorun(() => {
if (templateInstance.isVisible.get()) {
if (!this.singleProjectSub) {
templateInstance.singleProjectSub = templateInstance.subscribe('singleProject', templateInstance.data.projectId)
}
if (!templateInstance.projectStatsSub) {
templateInstance.projectStatsSub = templateInstance.subscribe('projectStats', templateInstance.data.projectId)
}
if (!templateInstance.projectUsersSub) {
templateInstance.projectUsersSub = templateInstance.subscribe('projectUsers', { projectId: templateInstance.data.projectId })
}
Meteor.call('getTopTasks', { projectId: templateInstance.data.projectId }, (error, result) => {
if (error) {
console.error(error)
} else {
templateInstance.topTasks.set(result)
}
})
}
})
templateInstance.autorun(() => {
if (templateInstance.subscriptionsReady()) {
const converter = new QuillDeltaToHtmlConverter(Projects
.findOne({ _id: Template.instance().data.projectId })?.desc?.ops,
{ multiLineParagraph: true })
templateInstance.projectDescAsHtml.set(converter.convert())
}
})
templateInstance.autorun(() => {
if (templateInstance.subscriptionsReady() && templateInstance.isVisible.get()) {
const stats = ProjectStats.findOne({ _id: templateInstance.data.projectId })
if (stats) {
import('frappe-charts').then((chartModule) => {
const { Chart } = chartModule
if (getUserSetting('timeunit') === 'd') {
stats.beforePreviousMonthHours
/= getUserSetting('hoursToDays')
stats.beforePreviousMonthHours = Number(stats.beforePreviousMonthHours)
stats.previousMonthHours
/= getUserSetting('hoursToDays')
stats.previousMonthHours = Number(stats.previousMonthHours)
stats.currentMonthHours
/= getUserSetting('hoursToDays')
}
if (getUserSetting('timeunit') === 'm') {
stats.beforePreviousMonthHours *= 60
stats.beforePreviousMonthHours = Number(stats.beforePreviousMonthHours)
stats.previousMonthHours *= 60
stats.previousMonthHours = Number(stats.previousMonthHours)
stats.currentMonthHours *= 60
stats.currentMonthHours = Number(stats.currentMonthHours)
}
stats.currentMonthHours = Number(stats.currentMonthHours)
if (templateInstance.chart) {
templateInstance.chart.destroy()
}
stats.beforePreviousMonthHours = stats.beforePreviousMonthHours.toFixed(precision)
stats.previousMonthHours = stats.previousMonthHours.toFixed(precision)
stats.currentMonthHours = stats.currentMonthHours.toFixed(precision)
window.requestAnimationFrame(() => {
if (templateInstance.$('.js-hours-chart-container')[0] && templateInstance.$('.js-hours-chart-container').is(':visible')) {
templateInstance.chart = new Chart(templateInstance.$('.js-hours-chart-container')[0], {
type: 'line',
height: 160,
colors: [Projects.findOne({ _id: templateInstance.data.projectId }).color || '#009688'],
lineOptions: {
regionFill: 1,
},
data: {
labels:
[stats.beforePreviousMonthName, stats.previousMonthName, stats.currentMonthName],
datasets: [{
values:
[stats.beforePreviousMonthHours,
stats.previousMonthHours,
stats.currentMonthHours],
}],
},
tooltipOptions: {
formatTooltipY: (value) => `${value} ${getUserTimeUnitVerbose()}`,
},
})
}
})
})
}
}
})
templateInstance.autorun(() => {
if (templateInstance.subscriptionsReady() && templateInstance.isVisible.get()) {
if (templateInstance.topTasks.get()) {
import('frappe-charts').then((chartModule) => {
window.requestAnimationFrame(() => {
const { Chart } = chartModule
if (templateInstance.piechart) {
templateInstance.piechart.destroy()
}
if (templateInstance.$('.js-pie-chart-container')[0] && templateInstance.$('.js-pie-chart-container').is(':visible')) {
templateInstance.piechart = new Chart(templateInstance.$('.js-pie-chart-container')[0], {
type: 'pie',
colors: [Projects.findOne({ _id: templateInstance.data.projectId }).color || '#009688', '#66c0b8', '#e4e4e4'],
height: 230,
data: {
// BUG: CWE-79 Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')
// labels: templateInstance.topTasks.get().map((task) => task._id),
// FIXED:
labels: templateInstance.topTasks.get().map((task) => $('<span>').text(task._id).html()),
datasets: [{
values: templateInstance.topTasks.get().map((task) => task.count),
}],
},
tooltipOptions: {
},
})
}
})
})
}
}
})
})
Template.projectchart.onDestroyed(() => {
Template.instance().$('.js-tooltip').tooltip('dispose')
const templateInstance = Template.instance()
if (templateInstance.chart) {
templateInstance.chart.destroy()
}
if (templateInstance.piechart) {
templateInstance.piechart.destroy()
}
templateInstance.observer.disconnect()
})
|