Commit 6810cab4 authored by Peter Walter's avatar Peter Walter
Browse files

version 1.4

parent 90c02158
// ==UserScript==
// @name RT Helpers
// @namespace https://rt.ebi.ac.uk/
// @description Add some shortcut links to RT
// @match https://rt.ebi.ac.uk/*
// @include https://rt.ebi.ac.uk/extserv/*
// @version 1.4
// @updateURL http://www.ebi.ac.uk/~pwalter/rthelper/rthelper.user.js
// @run-at document-end
// ==/UserScript==
/*jshint scripturl:true, strict:false, jquery:true */
// add trim to string prototype
if (!String.prototype.trim) {
String.prototype.trim=function(){return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');};
}
/**
* Get value from html element
* @param String selector
* @param String type
* @return String
*/
function getElementText(selector, type) {
var val = '';
var el;
selector = selector || '';
type = type || 'innerHTML';
el = document.querySelector(selector);
if (!el) {
return '';
}
switch(type) {
case 'href':
val = el.href.trim();
break;
default:
case 'innerHTML':
val = el.innerHTML.trim();
break;
}
return val;
}
/**
* Get id of current user
* @return String
*/
function getUserId() {
return getElementText('div#main-navigation span.current-user');
}
/**
* Get id for current ticket
* @return String
*/
function getTicketId() {
return getElementText('div.ticket-info-basics tr.id td.value').split('\n',1)[0];
}
/**
* Get first name of current user
* @param Bool full - true to return fullname, default false to return firstname
* @return String
*/
function getName(full) {
if (full) {
return getElementText('#NewReminder-Owner option[selected=selected]');
}
else {
return getElementText('#NewReminder-Owner option[selected=selected]').split(' ',1)[0];
}
}
/**
* Get queue for current ticket
* @return String
*/
function getQueue() {
return getElementText('div.ticket-info-basics tr.queue td.value').split('\n')[0];
}
/**
* Get owner for current ticket
* @return String
*/
function getOwner() {
return getElementText('div.ticket-info-people tr td.value').split('\n')[0];
}
/**
* Get custom field for current ticket
* @return String
*/
function getCustomField(id) {
return getElementText('div.ticket-info-cfs tr#CF-' + id + '-ShowRow td.value').split('\n',1)[0];
}
/**
* Get status for current ticket
* @return String
*/
function getStatus() {
return getElementText('div.ticket-info-basics tr.status td.value').split('\n',1)[0];
}
/**
* Get modify link for current ticket
* @param Object commands - object of argument key value pairs to append to modify link
* @return String
*/
function getModifyLink(commands) {
var arg = '';
if (typeof commands === 'object') {
for(var key in commands) {
if (key && commands[key]) {
arg += "&" + key.toString().trim() + "=" + commands[key].toString().trim();
}
}
}
return getElementText('div#page-navigation a#page-basics', 'href') + arg;
}
/**
* Get modify people link for current ticket
* @param Object commands - object of argument key value pairs to append to modify people link
* @return String
*/
function getModifyPeopleLink(commands) {
var arg = '';
if (typeof commands === 'object') {
for(var key in commands) {
if (key && commands[key]) {
arg += "&" + key.toString().trim() + "=" + commands[key].toString().trim();
}
}
}
return getElementText('div#page-navigation a#page-people', 'href') + arg;
}
/**
* Get most recent reply link for current ticket
* @param Object commands - array of argument key value pairs to append to reply link
* @return String
*/
function getReplyLink(commands) {
var arg = '';
if (typeof commands === 'object') {
for(var key in commands) {
if (key && commands[key]) {
arg += "&" + key.toString().trim() + "=" + commands[key].toString().trim();
}
}
}
return getElementText('#ticket-history .message .reply-link', 'href') + arg;
}
/**
* Insert node into DOM
* @param DocumentObject container
* @param String text - link text
* @param String link - link href, null for text only
* @param String prefix - text before link
* @param String suffix - text after link
* @param Bool breakBefore - true to place on new line
* @return DocumentObject
*/
function insertLink(container, text, link, prefix, suffix, breakBefore) {
var newlink = null;
if (!container) {
return null;
}
if (breakBefore) {
container.appendChild(document.createElement('br'));
}
if (prefix) {
container.appendChild(document.createTextNode(prefix));
}
if (link) {
newlink = document.createElement('a');
newlink.innerHTML = text;
newlink.href = link;
}
else {
newlink = document.createTextNode(text);
}
container.appendChild(newlink);
if (suffix) {
container.appendChild(document.createTextNode(suffix));
}
return newlink;
}
/**
* Build javascript link
* @param String url - url of action
* @param String done - function to call when complete
* @return String
*/
function buildLink(url, done) {
return (
'javascript:(' +
' function(){' +
' jQuery.ajax({' +
' url:\'' + url + '\',' +
' cache:false,' +
' complete:' + done +
' });' +
' }' +
')();'
);
}
/**
* Refresh entire page
* @return String
*/
function refreshElement() {
return (
'function(){' +
' location.reload();' +
'}'
);
}
/**
* Initialize
* @return void
*/
function init() {
var first;
var container;
var supercontainer;
// console.log('in init');
// add some links to queue
(function queueSwitch() {
var currentQueue = getQueue();
var queues = ['General', 'Web Development', 'EBI website improvements', 'Confluence-Jira'];
var queue;
container = document.querySelector('div.ticket-info-basics tr.queue td.value');
if (!container) {
return;
}
insertLink(container, ' (Move to: ', null , null, null, true);
first=true;
for (queue in queues) {
if (queues[queue]) {
insertLink(
container,
queues[queue],
buildLink(
getModifyLink({'Queue':queues[queue]}),
refreshElement('#body')
),
(true || first) ? '' : ' | '
).className = 'rthelper-button' + (queues[queue] === currentQueue ? ' rthelper-active' : '' );
first = false;
}
}
insertLink(container, ')');
})();
// add some links to status
(function statusSwitch() {
var currentStatus = getStatus();
var statuses = ['new', 'open', 'stalled', 'resolved'];
var status;
container = document.querySelector('div.ticket-info-basics tr.status td.value');
if (!container) {
return;
}
insertLink(container, ' (Set to: ', null , null, null, true);
first=true;
for (status in statuses) {
if (statuses[status]) { // !== currentStatus) {
insertLink(
container,
statuses[status],
buildLink(
getModifyLink({'Status':statuses[status]}),
refreshElement('#body')
),
(true || first) ? '' : ' | '
).className = 'rthelper-button' + (statuses[status] === currentStatus ? ' rthelper-active' : '' );
first = false;
}
}
insertLink(container, ')');
})();
// add some links to owner
(function ownerSwitch() {
var currentOwner = getOwner();
var owners = { // text and id must match Reminders select box
'Nobody in particular': 10,
'Bren Vaughan': 241,
'Francis Rowland': 7625,
'Jenny Cham': 15120,
'Joseph Rosetto': 50768,
'Liang Shen': 51610,
'Peter Walter': 19200,
'Thomas Laurent': 563
};
var owner;
container = document.querySelector('div.ticket-info-people table tr td.value') || document.querySelector('div.ticket-info-basics table tr.owner td.value');
if (!container) {
return;
}
insertLink(container, ' (Assign to: ', null , null, null, true);
first=true;
for (owner in owners) {
if (owners[owner]) {
insertLink(
container,
owner.split(' ',1)[0], // shorten name
buildLink(
getModifyLink({'Owner':owners[owner],'ForceOwnerChange':1}),
refreshElement('#body')
),
(true || first) ? '' : ' | '
).className = 'rthelper-button' + (owner === currentOwner ? ' rthelper-active' : '' );
first = false;
}
}
insertLink(container, ')');
})();
// add some links to project
(function projectSwitch() {
var cf = 21;
var currentProject = getCustomField(cf);
var params = {};
var projects = ['EBI website', 'Biocuration 2013', 'DGVa', 'EGA', 'ENA', 'External seminars', 'Intranet', 'Staff database', 'Train Online', 'Training'];
var project;
container = document.querySelector('div.ticket-info-cfs tr#CF-' + cf + '-ShowRow td.value') || document.querySelector('div.edit-custom-field div.entry select.CF-' + cf + '-Edit');
if (!container) {
return;
}
if (container.tagName === 'SELECT') {
subcontainer = document.createElement('div');
subcontainer.className = 'value';
subcontainer.style['marginLeft'] = '12em';
container = container.parentNode.appendChild(subcontainer);
}
insertLink(container, ' (Set to: ', null , null, null, true);
first=true;
for (project in projects) {
if (projects[project]) { // !== currentProject) {
params['Object-RT::Ticket-' + getTicketId() + '-CustomField-' + cf + '-Values'] = projects[project]; // build dynamic object key
insertLink(
container,
projects[project],
buildLink(
getModifyLink(params),
refreshElement('#body')
),
(true || first) ? '' : ' | '
).className = 'rthelper-button' + (projects[project] === currentProject ? ' rthelper-active' : '' );
first = false;
}
}
insertLink(container, ')');
projects = ['1000 Genomes', 'BioMedBridges', 'BiotNet', 'DREAM', 'ENFIN', 'Elixir', 'HGNC', 'IMEX', 'INSDC', 'LRG', 'PSIMEx', 'ProteomeXchange', 'Quantomics', 'Staff Association'];
insertLink(container, ' (Set to: ', null , null, null, true);
first=true;
for (project in projects) {
if (projects[project]) { // !== currentProject) {
params['Object-RT::Ticket-' + getTicketId() + '-CustomField-' + cf + '-Values'] = projects[project]; // build dynamic object key
insertLink(
container,
projects[project],
buildLink(
getModifyLink(params),
refreshElement('#body')
),
(true || first) ? '' : ' | '
).className = 'rthelper-button' + (projects[project] === currentProject ? ' rthelper-active' : '' );
first = false;
}
}
insertLink(container, ')');
})();
// add quick link to add cc
(function addccLink() {
container = document.querySelector('div.ticket-info-people .titlebox-title .right');
if (!container) {
supercontainer = document.querySelector('div.ticket-info-people .titlebox-title');
if (!supercontainer) {
return;
}
container = document.createElement('span');
container.className = 'right';
supercontainer.appendChild(container);
}
else {
insertLink(container, ' | ');
}
insertLink(
container,
'CC me',
buildLink(
getModifyPeopleLink({'WatcherTypeEmail1':'Cc','WatcherAddressEmail1':getUserId()}),
refreshElement('#body')
)
);
})();
// add quick link to reply
(function quickreplyLink() {
container = document.querySelector('div.ticket-info-basics .titlebox-title .right');
if (!container) {
supercontainer = document.querySelector('div.ticket-info-basics .titlebox-title');
if (!supercontainer) {
return;
}
container = document.createElement('span');
container.className = 'right';
supercontainer.appendChild(container);
}
else {
insertLink(container, ' | ');
}
insertLink(
container,
'Quick reply/resolve',
"javascript:(" +
" function(){" +
" var replytext = prompt('Reply message','All done.');" +
" if (!replytext) {" +
" return;" +
" }" +
" var workedtime=prompt('Time worked (mins)','5'); " +
" if (!workedtime) {" +
" return;" +
" }" +
" var replylink = '" + getReplyLink() + "&Status=resolved&UpdateTimeWorked=' + workedtime;" +
" var firstname = '" + getName() + "';" +
" var replywindow = window.open(replylink); " +
" replywindow.onload = function(){ " +
" replywindow.jQuery('textarea.messagebox')[0].value=replytext + '\\n' + firstname + '.\\n\\n' + replywindow.jQuery('textarea.messagebox')[0].value; " +
" replywindow.jQuery('input[name=SubmitTicket]').click();" +
" }; " +
" var replyinterval = setInterval(function(){ " +
" var response = replywindow.jQuery('.ticket-info-basics .status .value')[0].innerHTML; " +
" if (response === 'resolved') { " +
" self.clearInterval(replyinterval); " +
" replywindow.close(); " +
" location.reload(); " +
" } " +
" }, 500);" +
" }" +
")();"
);
})();
// add quick link to SPAM
(function spamLink() {
container = document.querySelector('div.ticket-info-basics .titlebox-title .right');
if (!container) {
supercontainer = document.querySelector('div.ticket-info-basics .titlebox-title');
if (!supercontainer) {
return;
}
container = document.createElement('span');
container.className = 'right';
supercontainer.appendChild(container);
}
else {
insertLink(container, ' | ');
}
insertLink(
container,
'SPAM',
buildLink(
getModifyLink({'Queue':'spam','Status':'deleted'}),
refreshElement('#body')
)
);
})();
// add style
(function createStyles() {
document.getElementById('logo').innerHTML = '<a href="http://www.ebi.ac.uk"><img src="//www.ebi.ac.uk/web_guidelines/images/logos/EMBL-EBI/EMBL_EBI_Logo_white.png" /></a> <span class="rtname">RT for extserv</span>';
var head = document.head || document.getElementsByTagName('head')[0];
var style = document.createElement('style');
style.type = 'text/css';
var css = '' +
'.browser-firefox input[type="reset"], input[type="submit"], input[class="button"], button { border-radius: 0.5em; }' + // fix button styling
'.browser-chrome .titlebox .titlebox-title .left { line-height: 1.6em; }' + // fix height of titlebox
'.rthelper-button { color: #222 !important; background: #eee; padding: 0.25em; padding-left: 0.5em; padding-right: 0.5em; border-radius: 0.5em; -moz-border-radius: 0.5em; -webkit-border-radius: 0.5em; display: inline-block; margin: 0.125em; border: 1px solid #ddd; }' +
'.rthelper-active { background: #369; color: #fff !important; }' +
'.ticket-info-basics .label, .ticket-info-cfs .label, .ticket-info-people .label { vertical-align: top; padding-top: 0.375em}' + // fix position of labels
'div#body { margin-left: 0; border: none; border-radius: 0; }' +
'';
if (style.styleSheet){
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
})();
(function browserIdentify() {
if (navigator.userAgent.indexOf("Firefox") !== -1) {
document.body.parentNode.className += ' browser-firefox';
}
if (navigator.userAgent.indexOf("Chrome") !== -1) {
document.body.parentNode.className += ' browser-chrome';
}
})();
}
try {
init();
}
catch(err) {
console.log(err.message);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment