const express = require('express'); const router = express.Router(); const moment = require('moment'); const { mongo, Mongoose } = require('mongoose'); let Work=require('../models/works'); let Employee = require('../models/employee'); var utils = require('../js/utils'); const { default: mongoose } = require('mongoose'); // Add Devices ToRoute router.get('/', utils.ensureAuthenticated,async function (req,res) { const employees=await Employee.find({'konyvelesre':'on','isSubcontractor':{$ne:'on'}},null,{sort: {name: 1}} ); await employees.forEach(element => { const color = generateColorHsl(element.name, saturationRange, lightnessRange); const c=HSLToRGB(color.h,color.s,color.l); element.color=c; }) res.render('gantt_holiday', { title: 'Szabadságok', employees:employees }); }); router.get('/getcolors', utils.ensureAuthenticated,async function (req,res) { const employees=await Employee.find({'konyvelesre':'on'},null,{sort: {name: 1}} ); var colors=[]; await employees.forEach(element => { const color = generateColorHsl(element.name, saturationRange, lightnessRange); const c=HSLToRGB(color.h,color.s,color.l); colors.push(c); }) res.send(colors); }); const getHashOfString = (str) => { let hash = 0; for (let i = 0; i < str.length; i++) { // tslint:disable-next-line: no-bitwise hash = str.charCodeAt(i) + ((hash << 5) - hash); } hash = Math.abs(hash); return hash; }; const normalizeHash = (hash, min, max) => { return Math.floor((hash % (max - min)) + min); }; const generateHSL = (name, saturationRange, lightnessRange) => { const hash = getHashOfString(name); const h = normalizeHash(hash, 0, 360); const s = normalizeHash(hash, saturationRange[0], saturationRange[1]); const l = normalizeHash(hash, lightnessRange[0], lightnessRange[1]); return [h, s, l]; }; const HSLtoString = (hsl) => { // return `hsl(${hsl[0]}, ${hsl[1]}%, ${hsl[2]}%)`; return {'h':hsl[0],'s':hsl[1],'l':hsl[2]} }; const generateColorHsl = (id, saturationRange, lightnessRange) => { return HSLtoString(generateHSL(id, saturationRange, lightnessRange)); }; const getInitials = (user) => { return `${user.name.first[0]}${user.name.last[0]}` } const setValue = (functionFor) => { return (e) => { const value = parseInt(e.target.value); functionFor(value); } }; const getRange = (value, range) => { return [Math.max(0, value-range), Math.min(value+range, 100)]; } const saturation = 50; const lightness = 50; const range = 10; const saturationRange = getRange(saturation, range); const lightnessRange = getRange(lightness, range); function HSLToRGB(h,s,l) { // Must be fractions of 1 s /= 100; l /= 100; let c = (1 - Math.abs(2 * l - 1)) * s, x = c * (1 - Math.abs((h / 60) % 2 - 1)), m = l - c/2, r = 0, g = 0, b = 0; if (0 <= h && h < 60) { r = c; g = x; b = 0; } else if (60 <= h && h < 120) { r = x; g = c; b = 0; } else if (120 <= h && h < 180) { r = 0; g = c; b = x; } else if (180 <= h && h < 240) { r = 0; g = x; b = c; } else if (240 <= h && h < 300) { r = x; g = 0; b = c; } else if (300 <= h && h < 360) { r = c; g = 0; b = x; } r = Math.round((r + m) * 255); g = Math.round((g + m) * 255); b = Math.round((b + m) * 255); //return {'r':r,'g':g,'b':b}; return "rgb(" + r + "," + g + "," + b + ")"; } router.get("/data/:display/:year", async function(req, res){ console.log('GANTT2'); var data=[]; moment.locale('HU'); const display=req.params.display const year=req.params.year const employees=await Employee.find({'konyvelesre':'on','isSubcontractor':{$ne:'on'}},null,{sort: {name: 1}} ); await employees.forEach(element => { const color = generateColorHsl(element.name, saturationRange, lightnessRange); const c=HSLToRGB(color.h,color.s,color.l); if (element.konyvelesre=='on') { element.holidayData.forEach(hd=>{ if (moment(hd.start_date,'DD.MM.YYYY').format('YYYY')==year || moment(hd.end_date,'DD.MM.YYYY').format('YYYY')==year) { var task={ id:hd._id, start:moment(hd.start_date,'DD.MM.YYYY').startOf('day').format('YYYY-MM-DD HH:mm:SS'), end:moment(hd.end_date,'DD.MM.YYYY').add(1, 'days').toDate(),//.endOf('day').toDate(), title:element.name, allDay:true, eventBackgroundColor: '#378006',//rgb(c.r,c.g,c.b), color: c,//'#378006', display:display, extendedProps: { employeeId:element._id, }, } data.push(task); } }) } }); res.send(data); }); module.exports = router;