Created
July 21, 2014 13:02
-
-
Save A-gambit/c7190fe8b5e1fe535ae6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $(document).ready(function() { | |
| var firstTime = true, | |
| USHoliday = [], | |
| UAHoliday = []; | |
| var accountNum = 0; | |
| var myJSON = [], | |
| email = ""; | |
| $(".now-1").html((new Date()).getFullYear()); | |
| $(".now-2").html((new Date()).getFullYear()); | |
| $(".last-year").html((new Date()).getFullYear()-1); | |
| $("#login").click(function () { | |
| login(); | |
| $('#login').css('display','none'); | |
| $('.load-spiner').css('display','block'); | |
| $('.login-h1').css('display','none'); | |
| }); | |
| function endWork(){ | |
| $(".content").css('display' , 'block'); | |
| $(".load-spiner").css('display' ,'none'); | |
| $(".login").css('display','none'); | |
| } | |
| $('#login').css('display','none'); | |
| $('.load-spiner').css('display','block'); | |
| $('.login-h1').css('display','none'); | |
| setTimeout( function (){ | |
| var config = { | |
| 'client_id': '365560126958-l83mn3tkkgaqrtkbskvocb3mvrn28i9d.apps.googleusercontent.com', | |
| 'scope': 'https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.profile.emails.read', | |
| 'authuser': accountNum, | |
| 'immediate':true | |
| }; | |
| gapi.auth.authorize(config, getMail); | |
| }, 200); | |
| function login(){ | |
| if(!firstTime) return | |
| var config = { | |
| 'client_id': '365560126958-l83mn3tkkgaqrtkbskvocb3mvrn28i9d.apps.googleusercontent.com', | |
| 'scope': 'https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.profile.emails.read', | |
| //'authuser': accountNum, | |
| 'authuser': -1 | |
| }; | |
| gapi.auth.authorize(config, getMail); | |
| } | |
| function getMail(){ | |
| gapi.client.load('plus', 'v1', getProfile); | |
| function getProfile(){ | |
| var request = gapi.client.plus.people.get({ | |
| 'userId': 'me' | |
| }); | |
| request.execute(profile); | |
| } | |
| function profile(resp){ | |
| console.log(resp); | |
| if(resp['emails']){ | |
| findMail(resp); | |
| } | |
| else{ | |
| choose(); | |
| return | |
| } | |
| } | |
| function findMail(resp){ | |
| for(i = 0; i < resp['emails'].length; i++){ | |
| var isEmail = resp['emails'][i]['type'] == 'account'; | |
| if(isEmail) email = resp['emails'][i]['value']; | |
| } | |
| console.log(email); | |
| calendar(); | |
| } | |
| } | |
| function choose(){ | |
| $("#login").css('display','block'); | |
| $('.load-spiner').css('display','none'); | |
| $('.login-h1').css('display','block'); | |
| login(); | |
| } | |
| function calendar(resp){ | |
| gapi.client.setApiKey('AIzaSyB4qUardN0ac-p4xYKWQKk_PxdSjPYBtY8'); | |
| gapi.client.load('calendar', 'v3', makeRequest); | |
| } | |
| function makeRequest() { | |
| req(gapi.client.calendar.calendarList.list()) | |
| } | |
| function req(request) { | |
| request.execute(function(response) { | |
| console.log(response); | |
| if(firstTime) | |
| knowCalendar(response); | |
| else | |
| workWithCalendar(response); | |
| }); | |
| } | |
| function knowCalendar(response){ | |
| var isEmail = false; | |
| if(email) | |
| isEmail=email.indexOf("@grammarly.com") >= 0; | |
| if(isEmail) | |
| trueEmail(response.items); | |
| else | |
| repeatEmail(); | |
| function repeatEmail(){ | |
| accountNum++; | |
| var config = { | |
| 'client_id': '365560126958-l83mn3tkkgaqrtkbskvocb3mvrn28i9d.apps.googleusercontent.com', | |
| 'scope': 'https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.profile.emails.read', | |
| 'authuser': accountNum, | |
| 'immediate':true | |
| }; | |
| gapi.auth.authorize(config, getMail); | |
| } | |
| function trueEmail(list){ | |
| getHoliday(); | |
| otherСalendars(list); | |
| firstTime = false; | |
| var lastTime = new Date(), | |
| startTime = new Date(lastTime.getFullYear(), 0, 1); | |
| lastTime.setFullYear(lastTime.getFullYear()+1); | |
| var config = { | |
| calendarId: 'grammarly.com_471831rd40r114bmjnf7r49uh8@group.calendar.google.com', | |
| maxResults: 2500, | |
| timeMin: startTime, | |
| timeMax: lastTime | |
| }; | |
| var list = gapi.client.calendar.events.list(config); | |
| req(list); | |
| } | |
| } | |
| function getHoliday(){ | |
| startTime = new Date((new Date()).getFullYear(), 0, 1); | |
| lastTime = new Date((new Date()).getFullYear(), 11, 31, 23, 59, 59); | |
| function reqHoliday(request) { | |
| request.execute(getHolidayJSON); | |
| } | |
| function getHolidayJSON(response){ | |
| console.log(response); | |
| sortHoliday(response.items); | |
| } | |
| function sortHoliday(items){ | |
| for (var i = 0; i < items.length; i++) { | |
| var summary = items[i].summary; | |
| var isUA = summary.indexOf("Ukraine") >= 0, | |
| isUS = summary.indexOf("US") >= 0; | |
| if(isUA) | |
| UAHoliday.push(items[i]); | |
| if(isUS) | |
| USHoliday.push(items[i]); | |
| }; | |
| } | |
| var config = { | |
| calendarId: 'grammarly.com_471831rd40r114bmjnf7r49uh8@group.calendar.google.com', | |
| maxResults: 2500, | |
| timeMin: startTime, | |
| timeMax: lastTime, | |
| q: 'Public' | |
| }; | |
| var holiday = gapi.client.calendar.events.list(config); | |
| reqHoliday(holiday); | |
| } | |
| function otherСalendars(list){ | |
| var startTime = new Date((new Date()).getFullYear(), (new Date()).getMonth(),(new Date()).getDate(), 0, 0), | |
| lastTime = new Date((new Date()).getFullYear(), (new Date()).getMonth(),(new Date()).getDate(), 23, 59, 59); | |
| for (var i = 1; i < list.length; i++) { | |
| var id = list[i].id, | |
| isVac = list[i].id!='grammarly.com_471831rd40r114bmjnf7r49uh8@group.calendar.google.com'; | |
| if(isVac) toList(id); | |
| }; | |
| function toList(id){ | |
| var config = { | |
| calendarId: id, | |
| maxResults: 2500, | |
| timeMin: startTime, | |
| timeMax: lastTime | |
| } | |
| var list = gapi.client.calendar.events.list(config); | |
| reqToday(list); | |
| } | |
| function reqToday(request) { | |
| request.execute(printNews); | |
| } | |
| function printNews(response){ | |
| console.log(response.summary,response); | |
| var items = response.items, | |
| elNews = $(".content__bottom__news"); | |
| if(items) printItems(items, elNews); | |
| } | |
| function printItems(items, elNews){ | |
| for(var i = 0; i < items.length; i++){ | |
| var summary = items[i].summary; | |
| elNews.html(elNews.html()+"<h2>"+summary+"</h2>"); | |
| }; | |
| } | |
| } | |
| function workWithCalendar(response){ | |
| var myInf = []; | |
| myInf = getDataAboutMe(response); | |
| console.log(myInf); | |
| getMyJson(myInf); | |
| getInf(myInf); | |
| todayList = getTodaytNews(response); | |
| console.log(todayList); | |
| printList(todayList); | |
| function getMyJson(myInf){ | |
| var userName = email; | |
| for (var i = 0; i < members.length; i++){ | |
| var isName = members[i].mail == userName; | |
| myJSON = isName ? members[i] : []; | |
| if(isName) break; | |
| }; | |
| } | |
| } | |
| function printList(list){ | |
| var elNews = $(".content__bottom__news"), | |
| elAbsent = $(".content__bottom__absent"), | |
| elWFH = $(".content__bottom__wft"); | |
| var startNews = elNews.html(), | |
| startAbsent = elAbsent.html(), | |
| startWFH = elWFH.html(); | |
| for (var i = 0; i < list.length; i++) { | |
| var summary = list[i].summary; | |
| if(summary){ | |
| elNews.html(elNews.html()+ "<h2>"+summary+"</h2>"); | |
| var strVacation = "Vacation", | |
| strSick = "Sick", | |
| strWFH = "WFH"; | |
| var conditionVacation = summary.indexOf(strVacation.toLowerCase()) >= 0 || summary.indexOf(strVacation) >= 0, | |
| conditionSick = summary.indexOf(strSick.toLowerCase()) >= 0 || summary.indexOf(strSick) >= 0 , | |
| conditionWFH = summary.indexOf(strWFH.toLowerCase()) >=0 || summary.indexOf(strWFH) >= 0; | |
| var name = list[i].creator.displayName, | |
| mail = list[i].creator.email; | |
| var isSrc = false, | |
| isVacationOrSick = conditionVacation || conditionSick; | |
| for (var j = 0; j < members.length; j++) { | |
| var isMail = mail == members[j].mail, | |
| isVOS = isVacationOrSick && isMail, | |
| isWFH = isMail && conditionWFH; | |
| if(isWFH) | |
| elWFH.html(elWFH.html()+"<h2><img src='/pic/team/"+members[j].photo+"' class='content__bottom-img'>"+name+"</h2>"); | |
| if(isVOS) | |
| elAbsent.html(elAbsent.html()+ "<h2><img src='/pic/team/"+members[j].photo+"' class='content__bottom-img'></span>"+name+"</h2>"); | |
| isSrc = isMail ? true : isSrc; | |
| j = isMail ? members.length : isSrc; | |
| } | |
| if(!isSrc){ | |
| if(conditionWFH) | |
| elWFH.html(elWFH.html()+"<h2><img src='/pic/team/undefined.png' class='content__bottom-img'>"+name+"</h2>"); | |
| else if(isVacationOrSick) | |
| elAbsent.html(elAbsent.html()+ "<h2><img src='/pic/team/undefined.png' class='content__bottom-img'></span>"+name+"</h2>"); | |
| } | |
| } | |
| } | |
| if(startAbsent==elAbsent.html()){ | |
| elAbsent.html(elAbsent.html()+"<h2>Nobody is Absent</h2>"); | |
| } | |
| else{ | |
| var el = $(".absent-h1"); | |
| el.css("margin-bottom","-4px"); | |
| } | |
| if(startWFH==elWFH.html()){ | |
| elWFH.html(elWFH.html()+"<h2>Nobody is Work from Home</h2>"); | |
| } | |
| else{ | |
| var el = $(".wfh-h1"); | |
| el.css("margin-bottom","-4px"); | |
| } | |
| if(startNews==elNews.html()){ | |
| elNews.html(elNews.html()+"<h2>No News</h2>"); | |
| } | |
| } | |
| function getTodaytNews(response){ | |
| var events = response.items, | |
| todayList = [], | |
| stopFind = false; | |
| for(var i = events.length-1; i>0 && !stopFind; i--) | |
| if(events[i].creator) addList(i); | |
| return todayList; | |
| function addList(i){ | |
| var start = events[i].start.date ? events[i].start.date : events[i].start.dateTime, | |
| end = events[i].end.date ? events[i].end.date : events[i].end.dateTime, | |
| curDate = new Date(), | |
| eventStart = new Date(start), | |
| eventEnd = new Date(end), | |
| isToday = curDate > eventStart && curDate < eventEnd; | |
| if(isToday) | |
| todayList.push(events[i]); | |
| stopFind = curDate > eventEnd+1; | |
| } | |
| } | |
| function getInf(myInf){ | |
| myJSON.vacation = 0; | |
| myJSON.sick = 0; | |
| myJSON.sick_wfh = 0; | |
| myJSON.wfh = 0; | |
| for(var i=0; i<myInf.length; i++){ | |
| var summary = myInf[i].summary, | |
| start = myInf[i].start.date ? myInf[i].start.date : myInf[i].start.dateTime, | |
| end = myInf[i].end.date ? myInf[i].end.date : myInf[i].end.dateTime; | |
| if(summary){ | |
| var strVacation = "Vacation", | |
| strSick = "Sick", | |
| strWFH = "WFH"; | |
| var conditionVacation = summary.indexOf(strVacation.toLowerCase()) >= 0 || summary.indexOf(strVacation) >= 0, | |
| conditionSick = summary.indexOf(strSick.toLowerCase()) >= 0 || summary.indexOf(strSick) >= 0 , | |
| conditionWTH = summary.indexOf(strWFH.toLowerCase()) >=0 || summary.indexOf(strWFH) >= 0; | |
| var startDate = new Date(start), | |
| endDate = new Date(end); | |
| var minusDay=0; | |
| minusDay = dayOff(startDate, endDate, new Date()); | |
| //console.log(minusDay); | |
| startDate = new Date(start), | |
| endDate = new Date(end); | |
| if(conditionVacation) | |
| myJSON.vacation+=countDays(startDate, endDate)-minusDay; | |
| else if(conditionWTH && conditionSick) | |
| myJSON.sick_wfh+=countDays(startDate, endDate)-minusDay; | |
| else if(conditionWTH) | |
| myJSON.wfh+=countDays(startDate, endDate); | |
| else if(conditionSick) | |
| myJSON.sick+=countDays(startDate, endDate)-minusDay; | |
| } | |
| } | |
| addDate(); | |
| function addDate(){ | |
| var userName = email, | |
| name = myJSON.name; | |
| $(".name").html(name); | |
| var curDay = new Date(), | |
| endYear = new Date((new Date).getFullYear(), 11, 31), | |
| startYear = new Date((new Date).getFullYear(), 0, 1), | |
| maxVacation = 0, | |
| vacationDays = 0; | |
| var daysInYear = countDays(startYear, endYear); | |
| var startWorkDate = new Date(myJSON.started); | |
| startYear = startYear.getFullYear() > startWorkDate.getFullYear() ? startYear : startWorkDate; | |
| var isUA = myJSON.location == "Kyiv"; | |
| if(isUA) | |
| maxVacation = 20; | |
| else | |
| maxVacation = 15; | |
| curDay = countDays(startYear ,curDay); | |
| vacationDays = (maxVacation*curDay/daysInYear).toFixed(); | |
| var vacation = myJSON.vacation, | |
| sick = myJSON.sick, | |
| wfh = myJSON.wfh, | |
| sick_wfh = myJSON.sick_wfh; | |
| console.log(vacationDays); | |
| vacation = isNaN(vacation) || vacation == undefined ? 0 : vacation; | |
| vacationDays = isNaN(vacationDays) || vacationDays == undefined ? 0 : vacationDays; | |
| var elVacTack = $(".vacation-tack"), | |
| elVacAccrued = $(".vacation-accrued"), | |
| elLastYear = $(".vacation-2013"), | |
| elToday = $(".vacation-today"), | |
| elSick = $(".sick"), | |
| elWFH = $(".wfh"), | |
| elSickWFH = $(".sick-wfh"); | |
| var maxVacation = 10, | |
| rolled = myJSON.rolledVacation; | |
| rolled = rolled && rolled>0 ? rolled : 0; | |
| rolled = rolled > maxVacation ? maxVacation : rolled; | |
| var total = parseInt(vacationDays) + rolled - parseInt(vacation); | |
| total = total > 0 ? total : 0; | |
| elLastYear.html(elLastYear.html()+ "<span class='vacation-day'> "+rolled+"</span>"); | |
| elToday.html(elToday.html()+"<span class='vacation-day'> "+total+"</span>"); | |
| elVacTack.html(elVacTack.html() + "<span class='vacation-day'> "+vacation+"</span>"); | |
| elVacAccrued.html(elVacAccrued.html()+"<span class='vacation-day'> "+vacationDays+"</span>"); | |
| wfh = isNaN(wfh) || wfh == undefined ? 0 : wfh; | |
| sick = isNaN(sick) || sick == undefined ? 0 : sick; | |
| sick_wfh = isNaN(sick_wfh) || sick_wfh == undefined ? 0 : sick_wfh; | |
| elWFH.html(elWFH.html()+" "+wfh); | |
| elSick.html(elSick.html()+" "+sick); | |
| elSickWFH.html(elSickWFH.html()+" "+sick_wfh); | |
| var isSrc = false; | |
| for (var i = 0; i < members.length; i++) { | |
| var isProfilImg = userName == members[i].mail; | |
| if(isProfilImg){ | |
| var elImg = $('.img'); | |
| elImg.each( function(){ this.src = "/pic/team/"+members[i].photo; } ); | |
| isSrc = true; | |
| i = members.length; | |
| } | |
| } | |
| if(!isSrc) | |
| $('.img').each( function(){ this.src = "/pic/team/undefined.png" } ); | |
| endWork(); | |
| } | |
| } | |
| function countDays(start, end){ | |
| var days = 0; | |
| var timeDiff = Math.abs(end.getTime() - start.getTime()); | |
| days = Math.ceil(timeDiff / (1000 * 3600 * 24)); | |
| return days; | |
| } | |
| function dayOff(start, end, nowTime){ | |
| var day=0, | |
| isDate = start.getFullYear() == end.getFullYear() && start.getMonth() == end.getMonth() && start.getDate() == end.getDate(); | |
| while(!isDate){ | |
| var curTime = start.getFullYear() != nowTime.getFullYear() || start > nowTime, | |
| dayOff = start.getDay() == 0 || start.getDay() == 6, | |
| dayHoliday = isHoliday(start), | |
| isMinusDay = curTime || dayOff || dayHoliday; | |
| //console.log("DAY: "+start+" Status: "+dayHoliday); | |
| if(isMinusDay) day++; | |
| start.setDate(start.getDate()+1); | |
| isDate = start.getFullYear() == end.getFullYear() && start.getMonth() == end.getMonth() && start.getDate() == end.getDate(); | |
| } | |
| return day; | |
| } | |
| function isHoliday(day){ | |
| var loc = myJSON.location == "Kyiv", | |
| isToday = false; | |
| if(loc) | |
| isToday = findHoliday(UAHoliday, day); | |
| else | |
| isToday = findHoliday(USHoliday, day); | |
| return isToday; | |
| function findHoliday(holiday, day){ | |
| var flag=false; | |
| for (var i = 0; i < holiday.length; i++) { | |
| var start = holiday[i].start.date ? holiday[i].start.date : holiday[i].start.dateTime, | |
| end = holiday[i].end.date ? holiday[i].end.date : holiday[i].end.dateTime, | |
| startDate = new Date(start), | |
| endDate = new Date(start); | |
| startDate.setFullYear(day.getFullYear()); | |
| endDate.setFullYear(day.getFullYear()); | |
| var isToday = day.getTime() >= startDate.getTime() && day.getTime() <= endDate.getTime(); | |
| if(isToday){ | |
| flag = true; | |
| return flag; | |
| } | |
| } | |
| return flag; | |
| } | |
| } | |
| function getDataAboutMe(response){ | |
| var myInf=[]; | |
| //email = 'eugene.chechurin@grammarly.com'; | |
| //email = 'valentin.chubukin@grammarly.com'; | |
| for( var i =0 ; i < response.items.length; i++){ | |
| var user = response.items[i].creator ? response.items[i].creator.email : ""; | |
| if (user != email) continue | |
| myInf.push(response.items[i]); | |
| } | |
| return myInf; | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment