TrelloClient.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /**
  2. * Copyright (c) 2006-2017, JGraph Ltd
  3. * Copyright (c) 2006-2017, Gaudenz Alder
  4. */
  5. TrelloClient = function(editorUi)
  6. {
  7. DrawioClient.call(this, editorUi, 'tauth');
  8. Trello.setKey(this.key);
  9. };
  10. // Extends DrawioClient
  11. mxUtils.extend(TrelloClient, DrawioClient);
  12. TrelloClient.prototype.key = (window.location.hostname == 'test.draw.io') ?
  13. 'e73615c79cf7e381aef91c85936e9553' : 'e73615c79cf7e381aef91c85936e9553';
  14. TrelloClient.prototype.baseUrl = 'https://api.trello.com/1/';
  15. TrelloClient.prototype.SEPARATOR = '|$|';
  16. /**
  17. * Maximum attachment size of Trello.
  18. */
  19. TrelloClient.prototype.maxFileSize = 10000000 /*10MB*/;
  20. /**
  21. * Default extension for new files.
  22. */
  23. TrelloClient.prototype.extension = '.xml'; //TODO export to png
  24. /**
  25. *
  26. */
  27. TrelloClient.prototype.getLibrary = function(id, success, error)
  28. {
  29. this.getFile(id, success, error, false, true);
  30. };
  31. /**
  32. *
  33. */
  34. TrelloClient.prototype.getFile = function(id, success, error, denyConvert, asLibrary)
  35. {
  36. asLibrary = (asLibrary != null) ? asLibrary : false;
  37. Trello.authorize({
  38. type: 'popup',
  39. name: 'draw.io',
  40. scope: {
  41. read: 'true',
  42. write: 'true' },
  43. expiration: 'never',
  44. success: mxUtils.bind(this, function() {
  45. var ids = id.split(this.SEPARATOR);
  46. Trello.cards.get(ids[0] + '/attachments/' + ids[1], mxUtils.bind(this, function(meta)
  47. {
  48. //TODO Trello doesn't allow CORS requests to load attachments. Confirm that and make sure that only a proxy technique can work!
  49. // Handles .vsdx, Gliffy and PNG+XML files by creating a temporary file
  50. if ((/\.vsdx$/i.test(meta.name) || /\.gliffy$/i.test(meta.name) || /\.png$/i.test(meta.name)))
  51. {
  52. this.ui.convertFile(PROXY_URL + '?url=' + encodeURIComponent(meta.url), meta.name, meta.mimeType,
  53. this.extension, success, error);
  54. }
  55. else
  56. {
  57. var acceptResponse = true;
  58. var timeoutThread = window.setTimeout(mxUtils.bind(this, function()
  59. {
  60. acceptResponse = false;
  61. error({code: App.ERROR_TIMEOUT})
  62. }), this.ui.timeout);
  63. this.ui.loadUrl(PROXY_URL + '?url=' + encodeURIComponent(meta.url), mxUtils.bind(this, function(data)
  64. {
  65. window.clearTimeout(timeoutThread);
  66. if (acceptResponse)
  67. {
  68. //keep our id which includes the cardId
  69. meta.compoundId = id;
  70. if (asLibrary)
  71. {
  72. success(new TrelloLibrary(this.ui, data, meta));
  73. }
  74. else
  75. {
  76. success(new TrelloFile(this.ui, data, meta));
  77. }
  78. }
  79. }), mxUtils.bind(this, function(req)
  80. {
  81. window.clearTimeout(timeoutThread);
  82. if (acceptResponse)
  83. {
  84. error();
  85. }
  86. }), meta.mimeType != null &&
  87. meta.mimeType.substring(0, 6) == 'image/');
  88. }
  89. }));
  90. }),
  91. error: error
  92. });
  93. };
  94. /**
  95. *
  96. */
  97. TrelloClient.prototype.insertLibrary = function(filename, data, success, error, cardId)
  98. {
  99. this.insertFile(filename, data, success, error, true, cardId);
  100. };
  101. /**
  102. *
  103. */
  104. TrelloClient.prototype.insertFile = function(filename, data, success, error, asLibrary, cardId)
  105. {
  106. asLibrary = (asLibrary != null) ? asLibrary : false;
  107. this.writeFile(filename, data, cardId, mxUtils.bind(this, function(meta)
  108. {
  109. if (asLibrary)
  110. {
  111. success(new TrelloLibrary(this.ui, data, meta));
  112. }
  113. else
  114. {
  115. success(new TrelloFile(this.ui, data, meta));
  116. }
  117. }), error);
  118. };
  119. /**
  120. *
  121. */
  122. TrelloClient.prototype.saveFile = function(file, success, error)
  123. {
  124. //delete file first, then write it again
  125. var ids = file.meta.compoundId.split(this.SEPARATOR);
  126. Trello.authorize({
  127. type: 'popup',
  128. name: 'draw.io',
  129. scope: {
  130. read: 'true',
  131. write: 'true'
  132. },
  133. expiration: 'never',
  134. success: mxUtils.bind(this, function()
  135. {
  136. Trello.del('cards/' + ids[0] + '/attachments/' + ids[1], mxUtils.bind(this, function()
  137. {
  138. this.writeFile(file.meta.name, file.getData(), ids[0], success, error);
  139. }), error);
  140. }),
  141. error: error
  142. });
  143. };
  144. /**
  145. *
  146. */
  147. TrelloClient.prototype.writeFile = function(filename, data, cardId, success, error)
  148. {
  149. if (filename != null && data != null)
  150. {
  151. if (data.length >= this.maxFileSize)
  152. {
  153. error({message: mxResources.get('drawingTooLarge') + ' (' +
  154. this.ui.formatFileSize(data.length) + ' / 10 MB)'});
  155. return;
  156. }
  157. var fn = mxUtils.bind(this, function()
  158. {
  159. Trello.authorize({
  160. type: 'popup',
  161. name: 'draw.io',
  162. scope: {
  163. read: 'true',
  164. write: 'true'
  165. },
  166. expiration: 'never',
  167. success: mxUtils.bind(this, function()
  168. {
  169. var acceptResponse = true;
  170. var timeoutThread = window.setTimeout(mxUtils.bind(this, function()
  171. {
  172. acceptResponse = false;
  173. error({code: App.ERROR_TIMEOUT, retry: fn});
  174. }), this.ui.timeout);
  175. var formData = new FormData();
  176. formData.append("key", Trello.key());
  177. formData.append("token", Trello.token());
  178. formData.append("file", data);
  179. formData.append("name", filename);
  180. var request = new XMLHttpRequest();
  181. request.responseType = "json";
  182. request.onreadystatechange = mxUtils.bind(this, function()
  183. {
  184. if (request.readyState === 4)
  185. {
  186. window.clearTimeout(timeoutThread);
  187. if (acceptResponse)
  188. {
  189. if (request.status == 200)
  190. {
  191. var fileMeta = request.response;
  192. fileMeta.compoundId = cardId + this.SEPARATOR + fileMeta.id
  193. success(fileMeta);
  194. }
  195. else
  196. {
  197. error();
  198. }
  199. }
  200. }
  201. });
  202. request.open("POST", this.baseUrl + 'cards/' + cardId + '/attachments');
  203. request.send(formData);
  204. }),
  205. error: error
  206. });
  207. });
  208. fn();
  209. }
  210. else
  211. {
  212. error({message: mxResources.get('unknownError')});
  213. }
  214. };
  215. /**
  216. * Checks if the client is authorized and calls the next step.
  217. */
  218. TrelloClient.prototype.pickLibrary = function(fn)
  219. {
  220. this.pickFile(fn);
  221. };
  222. /**
  223. *
  224. */
  225. TrelloClient.prototype.pickFolder = function(fn)
  226. {
  227. Trello.authorize({
  228. type: 'popup',
  229. name: 'draw.io',
  230. scope: {
  231. read: 'true',
  232. write: 'true' },
  233. expiration: 'never',
  234. success: mxUtils.bind(this, function() {
  235. //show file select
  236. this.showTrelloDialog(false, fn);
  237. }),
  238. error: mxUtils.bind(this, function(e)
  239. {
  240. this.ui.showError(mxResources.get('error'), e);
  241. })
  242. });
  243. };
  244. /**
  245. * Checks if the client is authorized and calls the next step.
  246. */
  247. TrelloClient.prototype.pickFile = function(fn, returnObject)
  248. {
  249. fn = (fn != null) ? fn : mxUtils.bind(this, function(id)
  250. {
  251. this.ui.loadFile('T' + encodeURIComponent(id));
  252. });
  253. Trello.authorize({
  254. type: 'popup',
  255. name: 'draw.io',
  256. scope: {
  257. read: 'true',
  258. write: 'true' },
  259. expiration: 'never',
  260. success: mxUtils.bind(this, function() {
  261. //show file select
  262. this.showTrelloDialog(true, fn);
  263. }),
  264. error: mxUtils.bind(this, function(e)
  265. {
  266. this.ui.showError(mxResources.get('error'), e);
  267. })
  268. });
  269. };
  270. /**
  271. *
  272. */
  273. TrelloClient.prototype.showTrelloDialog = function(showFiles, fn)
  274. {
  275. var cardId = null;
  276. var filter = null;
  277. var linkCounter = 0;
  278. var content = document.createElement('div');
  279. content.style.whiteSpace = 'nowrap';
  280. content.style.overflow = 'hidden';
  281. content.style.height = '224px';
  282. var hd = document.createElement('h3');
  283. mxUtils.write(hd, showFiles? mxResources.get('selectFile') : mxResources.get('selectCard'));
  284. hd.style.cssText = 'width:100%;text-align:center;margin-top:0px;margin-bottom:12px';
  285. content.appendChild(hd);
  286. var div = document.createElement('div');
  287. div.style.whiteSpace = 'nowrap';
  288. div.style.overflow = 'auto';
  289. div.style.height = '194px';
  290. content.appendChild(div);
  291. var dlg = new CustomDialog(this.ui, content);
  292. this.ui.showDialog(dlg.container, 340, 270, true, true);
  293. dlg.okButton.parentNode.removeChild(dlg.okButton);
  294. var createLink = mxUtils.bind(this, function(label, fn, preview)
  295. {
  296. linkCounter++;
  297. var div = document.createElement('div');
  298. div.style = "width:100%;vertical-align:middle;background:" + (linkCounter % 2 == 0? "#eee" : "#fff");
  299. var link = document.createElement('a');
  300. link.setAttribute('href', 'javascript:void(0);');
  301. if (preview != null)
  302. {
  303. var img = document.createElement('img');
  304. img.src = preview.url;
  305. img.width = preview.width;
  306. img.height= preview.height;
  307. img.style= "border: 1px solid black;margin:5px;vertical-align:middle"
  308. link.appendChild(img);
  309. }
  310. mxUtils.write(link, label);
  311. mxEvent.addListener(link, 'click', fn);
  312. div.appendChild(link);
  313. return div;
  314. });
  315. var error = mxUtils.bind(this, function(err)
  316. {
  317. this.ui.handleError(err, null, mxUtils.bind(this, function()
  318. {
  319. this.ui.spinner.stop();
  320. this.ui.hideDialog();
  321. }));
  322. });
  323. var selectAtt = mxUtils.bind(this, function()
  324. {
  325. linkCounter = 0;
  326. div.innerHTML = '';
  327. this.ui.spinner.spin(div, mxResources.get('loading'));
  328. Trello.cards.get(cardId + '/attachments', {fields: 'id,name,previews'}, mxUtils.bind(this, function(data)
  329. {
  330. this.ui.spinner.stop();
  331. var files = data;
  332. div.appendChild(createLink('../ [Up]', mxUtils.bind(this, function()
  333. {
  334. selectCard();
  335. })));
  336. mxUtils.br(div);
  337. if (files == null || files.length == 0)
  338. {
  339. mxUtils.write(div, mxResources.get('noFiles'));
  340. }
  341. else
  342. {
  343. var listFiles = mxUtils.bind(this, function()
  344. {
  345. for (var i = 0; i < files.length; i++)
  346. {
  347. (mxUtils.bind(this, function(file)
  348. {
  349. div.appendChild(createLink(file.name, mxUtils.bind(this, function()
  350. {
  351. this.ui.hideDialog();
  352. fn(cardId + this.SEPARATOR + file.id);
  353. }), file.previews != null? file.previews[0] : null));
  354. }))(files[i]);
  355. }
  356. });
  357. listFiles();
  358. }
  359. }), error);
  360. });
  361. // Adds paging for cards (files limited to 1000 by API)
  362. var pageSize = 100;
  363. var nextPageDiv = null;
  364. var scrollFn = null;
  365. var selectCard = mxUtils.bind(this, function(page)
  366. {
  367. if (page == null)
  368. {
  369. linkCounter = 0;
  370. div.innerHTML = '';
  371. page = 1;
  372. }
  373. this.ui.spinner.spin(div, mxResources.get('loading'));
  374. if (nextPageDiv != null && nextPageDiv.parentNode != null)
  375. {
  376. nextPageDiv.parentNode.removeChild(nextPageDiv);
  377. }
  378. nextPageDiv = document.createElement('a');
  379. nextPageDiv.style.display = 'block';
  380. nextPageDiv.setAttribute('href', 'javascript:void(0);');
  381. mxUtils.write(nextPageDiv, mxResources.get('more') + '...');
  382. var nextPage = mxUtils.bind(this, function()
  383. {
  384. mxEvent.removeListener(div, 'scroll', scrollFn);
  385. selectCard(page + 1);
  386. });
  387. mxEvent.addListener(nextPageDiv, 'click', nextPage);
  388. Trello.get('search', {
  389. 'query': (filter != null ? filter : 'is:open'),
  390. 'cards_limit': pageSize,
  391. 'cards_page': page-1
  392. },
  393. mxUtils.bind(this, function(data)
  394. {
  395. this.ui.spinner.stop();
  396. var cards = (data != null) ? data.cards : null;
  397. if (cards == null || cards.length == 0)
  398. {
  399. if (filter != null)
  400. {
  401. div.appendChild(createLink(mxResources.get('clearFilter'), mxUtils.bind(this, function()
  402. {
  403. filter = null;
  404. selectCard();
  405. })));
  406. mxUtils.br(div);
  407. }
  408. mxUtils.write(div, mxResources.get('noFiles'));
  409. }
  410. else
  411. {
  412. if (page == 1)
  413. {
  414. if (filter != null)
  415. {
  416. div.appendChild(createLink(mxResources.get('clearFilter'), mxUtils.bind(this, function()
  417. {
  418. filter = null;
  419. selectCard();
  420. })));
  421. }
  422. else
  423. {
  424. div.appendChild(createLink(mxResources.get('filterCards') + '...', mxUtils.bind(this, function()
  425. {
  426. var dlg = new FilenameDialog(this.ui, 'is:open', mxResources.get('ok'), mxUtils.bind(this, function(value)
  427. {
  428. if (value != null)
  429. {
  430. filter = value;
  431. selectCard();
  432. }
  433. }), mxResources.get('cardName'), null, null, 'http://help.trello.com/article/808-searching-for-cards-all-boards');
  434. this.ui.showDialog(dlg.container, 300, 80, true, false);
  435. dlg.init();
  436. })));
  437. }
  438. mxUtils.br(div);
  439. }
  440. for (var i = 0; i < cards.length; i++)
  441. {
  442. (mxUtils.bind(this, function(card)
  443. {
  444. div.appendChild(createLink(card.name, mxUtils.bind(this, function()
  445. {
  446. if (showFiles)
  447. {
  448. cardId = card.id;
  449. selectAtt();
  450. }
  451. else
  452. {
  453. this.ui.hideDialog();
  454. fn(card.id);
  455. }
  456. })));
  457. }))(cards[i]);
  458. }
  459. }
  460. if (cards.length == pageSize)
  461. {
  462. div.appendChild(nextPageDiv);
  463. scrollFn = function()
  464. {
  465. if (div.scrollTop >= div.scrollHeight - div.offsetHeight)
  466. {
  467. nextPage();
  468. }
  469. };
  470. mxEvent.addListener(div, 'scroll', scrollFn);
  471. }
  472. }),
  473. error);
  474. });
  475. selectCard();
  476. };
  477. /**
  478. * Checks if the client is authorized
  479. */
  480. TrelloClient.prototype.isAuthorized = function()
  481. {
  482. //TODO this may break if Trello client.js is changed
  483. return localStorage["trello_token"] != null; //Trello.authorized(); doesn't work unless authorize is called first
  484. };
  485. /**
  486. * Logout and deauthorize the user.
  487. */
  488. TrelloClient.prototype.logout = function()
  489. {
  490. Trello.deauthorize();
  491. };