Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create multiple cards for RT (RequestTracker) search results. #21

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 89 additions & 25 deletions trello_bookmarklet.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
(function(window){
var $;

var notify = function(card) {
// Display a little notification in the upper-left corner with a link to the card
// that was just created
var container = $('#cardcontainer');
if (!container.length) {
$('<div id="cardcontainer"></div>').css({
position: "absolute",
left: 0,
"z-index": 1e4,
top: 0}).appendTo("body");
var container = $('#cardcontainer');
}
var $cardLink = $("<a>")
.attr({
href: card.url,
target: "card"
})
.text("Created trello card '" + card.name + "'")
.css({
padding: "4px",
border: "1px solid #000",
background: "#fff",
"z-index": 1e4
})
.appendTo("#cardcontainer")

setTimeout(function(){
$cardLink.fadeOut(3000);
}, 5000)
}

/* This is run after we've connected to Trello and selected a list */
var run = function(Trello, idList) {
var name;
var tickets;
// Default description is the URL of the page we're looking at
var desc = location.href;

Expand Down Expand Up @@ -36,7 +68,37 @@

// We're looking at a redmine issue
name = $("#content h2:first").text().trim() + ": " + $("#content h3:first").text().trim();

} else if ($('a#page-edit_search').length) {
// We're looking at a RequestTracker (RT) search result
base_url = window.location.href.split('/').splice(0,3).join('/');
tickets = {};
all_tickets = $('td.collection-as-table b a');
// we need to find out the column where the owner is
$('th.collection-as-table a').each(function(index, header) {
header_link = $(header).attr('href');
if (header_link.indexOf('OrderBy=Owner') != -1) {
owner_index = index;
} else if (header_link.indexOf('OrderBy=Status') != -1) {
status_index = index;
}
});
// we now have all links twice, from column 1 and 2.
$.each(all_tickets, function(ind, ticket) {
ticket = $(ticket);
if (ticket.attr('href').indexOf(ticket.text()) == -1) {
cells = ticket.parents('tr').find('td');
owner = $(cells[owner_index]).text();
stat = $(cells[status_index]).text();
if (owner != 'Nobody' & ((stat == 'open') | (stat == 'offen') | (stat == 'neu') | (stat == 'new'))) {
if (!(owner in tickets)) {
tickets[owner] = {user: owner, tickets: []};
}
tickets[owner].tickets.push(
{url: base_url + ticket.attr('href'),
descr: ticket.text()});
}
}
});
} else if ($('#header h1').length) {

// We're looking at a RequestTracker (RT) ticket
Expand Down Expand Up @@ -75,34 +137,36 @@

name = name || 'Unknown page';

// Create the card
if(name) {
if (tickets) {
// Create a bunch of cards
created = 0;
$.each(tickets, function(t_index, usertickets) {
if (usertickets.tickets.length > 0) {
Trello.post("lists/" + idList + "/cards", {
name: 'RT Tickets for ' + usertickets.user
}, function(card) {
// Add checklist
Trello.post("cards/" + card.id + "/checklists", {
name: 'TODO'
}, function(checklist) {
// Add items
notify(card);
$.each(usertickets.tickets, function(idx, tick) {
Trello.post("checklists/" + checklist.id + '/checkItems', {
name: tick.descr + ' (' + tick.url + ')'
});
});
});
});
}
});
} else if(name) {
//create a single card
Trello.post("lists/" + idList + "/cards", {
name: name,
desc: desc
}, function(card){
// Display a little notification in the upper-left corner with a link to the card
// that was just created
var $cardLink = $("<a>")
.attr({
href: card.url,
target: "card"
})
.text("Created a Trello Card")
.css({
position: "absolute",
left: 0,
top: 0,
padding: "4px",
border: "1px solid #000",
background: "#fff",
"z-index": 1e3
})
.appendTo("body")

setTimeout(function(){
$cardLink.fadeOut(3000);
}, 5000)
notify(card);
})
}
}
Expand Down