Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function(){
- function openEffortEntry(taskId, cb) {
- getFrame('Submit').location.href = '/usr/submit/Effort.asp?cmd=submit&tid=' + taskId;
- setTimeout(cb, 1000);
- }
- function fillEffort(taskId, date, log, cb) {
- openEffortEntry(taskId, function() {
- var DRDate = toDRDate(date);
- var docAll = getFrame('Submit').document.all;
- var startEl = docAll.Start;
- startEl.previousElementSibling.checked = true;
- docAll.End.value = DRDate;
- startEl.value = DRDate;
- docAll.W1004.value = log.hours;
- docAll.A1004.value = log.desc;
- cb();
- });
- }
- function submitEffort(taskId, date, log, cb) {
- fillEffort(taskId, date, log, function() {
- getFrame('Submit').document.all.SubmitForm.submit();
- setTimeout(cb, 1000);
- });
- }
- function submitNextEffort(dateEntries) {
- var first = dateEntries[0];
- if (first) {
- var log = first.entries.shift();
- var next = function() {
- submitNextEffort(dateEntries);
- };
- if (log) {
- submitEffort(getTaskId(), first.date, log, next);
- } else {
- dateEntries.shift();
- next();
- }
- } else {
- console.log('finished');
- }
- }
- function getLogEntries(log, options) {
- if (options.groupIssues) {
- return [{
- desc: reduceLogDesc(log, options.showParent),
- hours: reduceLogHours(log)
- }];
- } else {
- return log.map(function(entry) {
- return {
- desc: getLogEntryIssue(entry, options.showParent),
- hours: entry.hours
- }
- });
- }
- }
- function displayTimesheet(data, options) {
- var html = '<label title="Group by date"><input type=checkbox name=group> Group</label> \u00A0 <label title="Show story instead of a sub-task"><input type=checkbox name=parent>Show parent</label>';
- html += '<table border=1 style="margin:0 auto 10px auto">';
- html += '<tr><th><input type=checkbox name=toggleAll checked></th><th>Date</th><th>Tasks</th><th>Hours</th></tr>';
- var dates = Object.keys(data);
- dates.sort();
- var allEntries = dates.map(function(date) {
- return {
- date: date,
- entries: getLogEntries(data[date], options)
- };
- });
- allEntries.forEach(function(dateEntries) {
- var date = dateEntries.date;
- dateEntries.entries.forEach(function(entry, i) {
- html += '<tr><td><input type=checkbox checked data-date="' + date + '" data-entrynum=' + i + '></td><td>' + toDRDate(date) + '</td><td>' +
- entry.desc + '</td><td>' + entry.hours + '</td></tr>\n';
- });
- });
- html += '</table><span class="dr_close" style="text-decoration:underline;margin-right:10px">close</span><input type="button" value=Submit>';
- var doc = getFrame('Submit').document;
- var body = doc.body;
- var timeSheetCt = doc.createElement('div');
- timeSheetCt.style.margin = '0 0 20px';
- timeSheetCt.style.textAlign = 'center';
- timeSheetCt.innerHTML = html;
- // toggle checkboxes
- var toggleCheckbox = timeSheetCt.querySelector('th input[name=toggleAll]');
- var groupCheckbox = timeSheetCt.querySelector('input[name=group]');
- var parentCheckbox = timeSheetCt.querySelector('input[name=parent]');
- toggleCheckbox.onchange = function() {
- toArr(timeSheetCt.querySelectorAll('td input')).forEach(function(node) {
- node.checked = toggleCheckbox.checked;
- });
- };
- function close() {
- timeSheetCt.parentNode.removeChild(timeSheetCt);
- }
- function syncOptions(node, key) {
- node.checked = options[key];
- node.onchange = function() {
- options[key] = node.checked;
- close();
- displayTimesheet(data, options);
- };
- }
- syncOptions(groupCheckbox, 'groupIssues');
- syncOptions(parentCheckbox, 'showParent');
- // remove button
- timeSheetCt.querySelector('.dr_close').onclick = close;
- // submit button
- timeSheetCt.querySelector('input[value="Submit"]').onclick = function() {
- var toRemove = {};
- toArr(timeSheetCt.querySelectorAll('input[data-date]:not(:checked)')).forEach(function(node) {
- var date = node.dataset.date;
- toRemove[date] = toRemove[date] || [];
- toRemove[date].push(node.dataset.entrynum);
- });
- allEntries.forEach(function(dateEntries) {
- var remove = toRemove[dateEntries.date];
- if (remove) {
- dateEntries.entries = dateEntries.entries.filter(function(entry, i) {
- return remove.indexOf('' + i) === -1;
- });
- }
- })
- submitNextEffort(allEntries);
- };
- body.insertBefore(timeSheetCt, body.firstChild);
- }
- var getFrame;
- var getTaskId;
- function main(global) {
- var frames = {};
- (function getFrames(window) {
- toArr(window.frames).forEach(function(win) {
- if (win.name) {
- frames[win.name] = win;
- }
- getFrames(win);
- });
- })(global);
- getFrame = function(name) {
- return frames[name];
- };
- var DRTaskId = localStorage.DRTaskId;
- var pageTaskId = getFrame('Submit').location.href.match(/Effort\.asp\?cmd=submit&tid=(\d+)/);
- pageTaskId = pageTaskId && pageTaskId[1];
- if (DRTaskId && pageTaskId && DRTaskId !== pageTaskId) {
- alert('Task id mismatch, using ' + pageTaskId);
- DRTaskId = pageTaskId;
- } else if (!DRTaskId) {
- if (pageTaskId) {
- DRTaskId = pageTaskId;
- } else {
- DRTaskId = prompt('Enter DR task id');
- }
- if (!DRTaskId) {
- alert('failed');
- return;
- }
- }
- localStorage.DRTaskId = DRTaskId;
- getTaskId = function() {
- return DRTaskId;
- };
- var data = prompt('Paste data from JIRA timesheet');
- if (data) {
- try {
- displayTimesheet(JSON.parse(data), {
- groupIssues: false,
- showParent: true
- });
- } catch(e) {
- console.error(e);
- }
- }
- }
- function toArr(obj) {
- return Array.prototype.slice.call(obj);
- }
- function toDRDate(date) {
- return date.slice(0, 2) + '.' + date.slice(2, 4) + '.' + date.slice(4);
- }
- function reduceLogHours(log) {
- return log.reduce(function(total, log) {
- return total + log.hours;
- }, 0);
- }
- function reduceLogDesc(log, parent) {
- return log.reduce(function(desc, log) {
- return (desc ? desc + ' ' : '') + getLogEntryIssue(log, parent);
- }, '');
- }
- function getLogEntryIssue(entry, parent) {
- return parent && entry.parentIssue || entry.issueId;
- }
- main(this);
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement