TrelloClient.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. Trello.authorize({
  158. type: 'popup',
  159. name: 'draw.io',
  160. scope: {
  161. read: 'true',
  162. write: 'true'
  163. },
  164. expiration: 'never',
  165. success: mxUtils.bind(this, function()
  166. {
  167. var acceptResponse = true;
  168. var timeoutThread = window.setTimeout(mxUtils.bind(this, function()
  169. {
  170. acceptResponse = false;
  171. error({code: App.ERROR_TIMEOUT, retry: fn});
  172. }), this.ui.timeout);
  173. var formData = new FormData();
  174. formData.append("key", Trello.key());
  175. formData.append("token", Trello.token());
  176. formData.append("file", data);
  177. formData.append("name", filename);
  178. var request = new XMLHttpRequest();
  179. request.responseType = "json";
  180. request.onreadystatechange = mxUtils.bind(this, function()
  181. {
  182. if (request.readyState === 4)
  183. {
  184. window.clearTimeout(timeoutThread);
  185. if (acceptResponse)
  186. {
  187. if (request.status == 200)
  188. {
  189. var fileMeta = request.response;
  190. fileMeta.compoundId = cardId + this.SEPARATOR + fileMeta.id
  191. success(fileMeta);
  192. }
  193. else
  194. {
  195. error();
  196. }
  197. }
  198. }
  199. });
  200. request.open("POST", this.baseUrl + 'cards/' + cardId + '/attachments');
  201. request.send(formData);
  202. }),
  203. error: error
  204. });
  205. }
  206. else
  207. {
  208. error({message: mxResources.get('unknownError')});
  209. }
  210. };
  211. /**
  212. * Checks if the client is authorized and calls the next step.
  213. */
  214. TrelloClient.prototype.pickLibrary = function(fn)
  215. {
  216. this.pickFile(fn);
  217. };
  218. /**
  219. *
  220. */
  221. TrelloClient.prototype.pickFolder = function(fn)
  222. {
  223. Trello.authorize({
  224. type: 'popup',
  225. name: 'draw.io',
  226. scope: {
  227. read: 'true',
  228. write: 'true' },
  229. expiration: 'never',
  230. success: mxUtils.bind(this, function() {
  231. //show file select
  232. this.showTrelloDialog(false, fn);
  233. }),
  234. error: mxUtils.bind(this, function(e)
  235. {
  236. this.ui.showError(mxResources.get('error'), e);
  237. })
  238. });
  239. };
  240. /**
  241. * Checks if the client is authorized and calls the next step.
  242. */
  243. TrelloClient.prototype.pickFile = function(fn, returnObject)
  244. {
  245. fn = (fn != null) ? fn : mxUtils.bind(this, function(id)
  246. {
  247. this.ui.loadFile('T' + encodeURIComponent(id));
  248. });
  249. Trello.authorize({
  250. type: 'popup',
  251. name: 'draw.io',
  252. scope: {
  253. read: 'true',
  254. write: 'true' },
  255. expiration: 'never',
  256. success: mxUtils.bind(this, function() {
  257. //show file select
  258. this.showTrelloDialog(true, fn);
  259. }),
  260. error: mxUtils.bind(this, function(e)
  261. {
  262. this.ui.showError(mxResources.get('error'), e);
  263. })
  264. });
  265. };
  266. /**
  267. *
  268. */
  269. TrelloClient.prototype.showTrelloDialog = function(showFiles, fn)
  270. {
  271. var cardId = null;
  272. var filter = null;
  273. var linkCounter = 0;
  274. var content = document.createElement('div');
  275. content.style.whiteSpace = 'nowrap';
  276. content.style.overflow = 'hidden';
  277. content.style.height = '224px';
  278. var hd = document.createElement('h3');
  279. mxUtils.write(hd, showFiles? mxResources.get('selectFile') : mxResources.get('selectCard'));
  280. hd.style.cssText = 'width:100%;text-align:center;margin-top:0px;margin-bottom:12px';
  281. content.appendChild(hd);
  282. var div = document.createElement('div');
  283. div.style.whiteSpace = 'nowrap';
  284. div.style.overflow = 'auto';
  285. div.style.height = '194px';
  286. content.appendChild(div);
  287. var dlg = new CustomDialog(this.ui, content);
  288. this.ui.showDialog(dlg.container, 340, 270, true, true);
  289. dlg.okButton.parentNode.removeChild(dlg.okButton);
  290. var createLink = mxUtils.bind(this, function(label, fn, preview)
  291. {
  292. linkCounter++;
  293. var div = document.createElement('div');
  294. div.style = "width:100%;vertical-align:middle;background:" + (linkCounter % 2 == 0? "#eee" : "#fff");
  295. var link = document.createElement('a');
  296. link.setAttribute('href', 'javascript:void(0);');
  297. if (preview != null)
  298. {
  299. var img = document.createElement('img');
  300. img.src = preview.url;
  301. img.width = preview.width;
  302. img.height= preview.height;
  303. img.style= "border: 1px solid black;margin:5px;vertical-align:middle"
  304. link.appendChild(img);
  305. }
  306. mxUtils.write(link, label);
  307. mxEvent.addListener(link, 'click', fn);
  308. div.appendChild(link);
  309. return div;
  310. });
  311. var error = mxUtils.bind(this, function(err)
  312. {
  313. this.ui.handleError(err, null, mxUtils.bind(this, function()
  314. {
  315. this.ui.spinner.stop();
  316. this.ui.hideDialog();
  317. }));
  318. });
  319. var selectAtt = mxUtils.bind(this, function()
  320. {
  321. linkCounter = 0;
  322. div.innerHTML = '';
  323. this.ui.spinner.spin(div, mxResources.get('loading'));
  324. Trello.cards.get(cardId + '/attachments', {fields: 'id,name,previews'}, mxUtils.bind(this, function(data)
  325. {
  326. this.ui.spinner.stop();
  327. var files = data;
  328. div.appendChild(createLink('../ [Up]', mxUtils.bind(this, function()
  329. {
  330. selectCard();
  331. })));
  332. mxUtils.br(div);
  333. if (files == null || files.length == 0)
  334. {
  335. mxUtils.write(div, mxResources.get('noFiles'));
  336. }
  337. else
  338. {
  339. var listFiles = mxUtils.bind(this, function()
  340. {
  341. for (var i = 0; i < files.length; i++)
  342. {
  343. (mxUtils.bind(this, function(file)
  344. {
  345. div.appendChild(createLink(file.name, mxUtils.bind(this, function()
  346. {
  347. this.ui.hideDialog();
  348. fn(cardId + this.SEPARATOR + file.id);
  349. }), file.previews != null? file.previews[0] : null));
  350. }))(files[i]);
  351. }
  352. });
  353. listFiles();
  354. }
  355. }), error);
  356. });
  357. // Adds paging for cards (files limited to 1000 by API)
  358. var pageSize = 100;
  359. var nextPageDiv = null;
  360. var scrollFn = null;
  361. var selectCard = mxUtils.bind(this, function(page)
  362. {
  363. if (page == null)
  364. {
  365. linkCounter = 0;
  366. div.innerHTML = '';
  367. page = 1;
  368. }
  369. this.ui.spinner.spin(div, mxResources.get('loading'));
  370. if (nextPageDiv != null && nextPageDiv.parentNode != null)
  371. {
  372. nextPageDiv.parentNode.removeChild(nextPageDiv);
  373. }
  374. nextPageDiv = document.createElement('a');
  375. nextPageDiv.style.display = 'block';
  376. nextPageDiv.setAttribute('href', 'javascript:void(0);');
  377. mxUtils.write(nextPageDiv, mxResources.get('more') + '...');
  378. var nextPage = mxUtils.bind(this, function()
  379. {
  380. mxEvent.removeListener(div, 'scroll', scrollFn);
  381. selectCard(page + 1);
  382. });
  383. mxEvent.addListener(nextPageDiv, 'click', nextPage);
  384. Trello.get('search', {
  385. 'query': (filter != null ? filter : 'is:open'),
  386. 'cards_limit': pageSize,
  387. 'cards_page': page-1
  388. },
  389. mxUtils.bind(this, function(data)
  390. {
  391. this.ui.spinner.stop();
  392. var cards = (data != null) ? data.cards : null;
  393. if (cards == null || cards.length == 0)
  394. {
  395. if (filter != null)
  396. {
  397. div.appendChild(createLink(mxResources.get('clearFilter'), mxUtils.bind(this, function()
  398. {
  399. filter = null;
  400. selectCard();
  401. })));
  402. mxUtils.br(div);
  403. }
  404. mxUtils.write(div, mxResources.get('noFiles'));
  405. }
  406. else
  407. {
  408. if (page == 1)
  409. {
  410. if (filter != null)
  411. {
  412. div.appendChild(createLink(mxResources.get('clearFilter'), mxUtils.bind(this, function()
  413. {
  414. filter = null;
  415. selectCard();
  416. })));
  417. }
  418. else
  419. {
  420. div.appendChild(createLink(mxResources.get('filterCards') + '...', mxUtils.bind(this, function()
  421. {
  422. var dlg = new FilenameDialog(this.ui, 'is:open', mxResources.get('ok'), mxUtils.bind(this, function(value)
  423. {
  424. if (value != null)
  425. {
  426. filter = value;
  427. selectCard();
  428. }
  429. }), mxResources.get('cardName'), null, null, 'http://help.trello.com/article/808-searching-for-cards-all-boards');
  430. this.ui.showDialog(dlg.container, 300, 80, true, false);
  431. dlg.init();
  432. })));
  433. }
  434. mxUtils.br(div);
  435. }
  436. for (var i = 0; i < cards.length; i++)
  437. {
  438. (mxUtils.bind(this, function(card)
  439. {
  440. div.appendChild(createLink(card.name, mxUtils.bind(this, function()
  441. {
  442. if (showFiles)
  443. {
  444. cardId = card.id;
  445. selectAtt();
  446. }
  447. else
  448. {
  449. this.ui.hideDialog();
  450. fn(card.id);
  451. }
  452. })));
  453. }))(cards[i]);
  454. }
  455. }
  456. if (cards.length == pageSize)
  457. {
  458. div.appendChild(nextPageDiv);
  459. scrollFn = function()
  460. {
  461. if (div.scrollTop >= div.scrollHeight - div.offsetHeight)
  462. {
  463. nextPage();
  464. }
  465. };
  466. mxEvent.addListener(div, 'scroll', scrollFn);
  467. }
  468. }),
  469. error);
  470. });
  471. selectCard();
  472. };
  473. /**
  474. * Checks if the client is authorized
  475. */
  476. TrelloClient.prototype.isAuthorized = function()
  477. {
  478. //TODO this may break if Trello client.js is changed
  479. return localStorage["trello_token"] != null; //Trello.authorized(); doesn't work unless authorize is called first
  480. };
  481. /**
  482. * Logout and deauthorize the user.
  483. */
  484. TrelloClient.prototype.logout = function()
  485. {
  486. Trello.deauthorize();
  487. };