UNPKG

trello-sprinter

Version:

Provides summarizations of sprint activity in a Trello project

233 lines (196 loc) 5.04 kB
/* * Representation of a Trello Board */ import {Member} from './Member'; import {List} from './List'; import {Card} from './Card'; import {Update} from './Update'; import {Comment} from './Comment'; export const Board = function(data, options) { this.data = data; this.id = data.id; this.name = data.name; this.lists = []; this.members = {}; this.options = options; this.find_card = function(id) { for(var i=0; i<this.lists.length; i++) { var card = this.lists[i].find_card(id); if(card !== null) { return card; } } return null; } this.add_member = function(member) { this.members[member.id] = member; } } Board.fetch = function(trello, name, options, msg, success, failure) { msg.innerText = 'Fetching board ' + name; /// Keeps track of how many open async calls there are right now let active = 0; // Will be set to point to a new Board object let board = null; async_start(); trello.get("/member/me/boards?fields=all", (data) => { for(var i=0; i<data.length; i++) { var boardData = data[i]; if(boardData.name.toLowerCase() === name.toLowerCase()) { // We have found the board (if not closed) if(!boardData.closed) { // Create the board object board = new Board(boardData, options); // Fetch the membership fetch_membership(board); // Fetch the lists for the board fetch_lists(board); break; } } } if(board === null) { failure("Unable to find Trello board " + name); return; } async_end(); }, (data) => { failure("Unable to fetch Trello boards") }, msg ) /** * Fetch the membership of the board. * @param board Board to fetch for */ function fetch_membership(board) { async_start(); trello.get("/board/" + board.id + "/members?fields=all", function(data) { data.forEach(function(memberData) { var member = new Member(board, memberData); board.add_member(member); }); async_end(); }, function(data) { failure("Unable to fetch Trello membership") }, msg ) } function fetch_lists(board) { msg.innerText = "Fetching lists for " + name; async_start(); trello.get("/boards/" + board.id + "/lists?filter=open", function(data) { data.forEach(function(listData) { if(!listData.closed) { var list = new List(board, listData); board.lists.push(list); } }); fetch_cards(board); async_end(); }, function(data) { failure("Unable to fetch Trello lists") }, msg ) } /** * Fetch all cards as a single batch operation. * @param board Board we are fetching for */ function fetch_cards(board) { msg.innerText = "Fetching cards"; // Collect up all URLs for all of the lists var urls = ''; for(var i=0; i<board.lists.length; i++) { var list = board.lists[i]; if(urls.length > 0) { urls += ','; } urls += "/lists/" + list.id + "/cards/open"; } // Get get it async_start(); trello.get("/batch?urls=" + urls, function(data) { // Loop over the result for each provided URL for(let i=0; i<data.length && i<board.lists.length; i++) { const list = board.lists[i]; const cardsData = data[i][200]; cardsData.forEach(function(cardData) { if(!cardData.closed) { const card = new Card(board, list, cardData); if(options.after !== null) { if(card.created.getTime() / 1000 > options.after) { list.add_card(card); } } else { list.add_card(card); } } }); } fetch_actions(board); async_end(); }, function(data) { failure("Unable to fetch Trello cards") }, msg ) } function fetch_actions(board) { async_start(); trello.get("/board/" + board.id + "/actions?filter=commentCard,updateCard:idList&limit=1000", function(data) { data.forEach(function(action) { if(!action.closed) { if(action.type === "commentCard") { let card = board.find_card(action.data.card.id); if(card !== null) { var comment = new Comment(board, card, action); card.comments.push(comment); } //console.log(action); } else if(action.type === 'updateCard') { let card = board.find_card(action.data.card.id); if(card !== null) { var update = new Update(board, card, action); card.updates.push(update); } //console.log(action); } } }); board.lists.forEach(function(list) { list.cards.forEach(function(card) { card.sort_updates(); }) }); async_end(); }, function(data) { failure("Unable to fetch Trello card information") }, msg ) } function async_start() { active++; } function async_end() { active--; if(active === 0) { success(board); } } } Board.prototype.find_list = function(name) { name = name.toLowerCase(); for(var i=0; i<this.lists.length; i++) { var list = this.lists[i]; if(list.name.toLowerCase() === name) { return list; } } return null; }