GitHubClient.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /**
  2. * Copyright (c) 2006-2017, JGraph Ltd
  3. * Copyright (c) 2006-2017, Gaudenz Alder
  4. */
  5. GitHubClient = function(editorUi)
  6. {
  7. DrawioClient.call(this, editorUi, 'ghauth');
  8. };
  9. // Extends DrawioClient
  10. mxUtils.extend(GitHubClient, DrawioClient);
  11. /**
  12. * Specifies if thumbnails should be enabled. Default is true.
  13. * LATER: If thumbnails are disabled, make sure to replace the
  14. * existing thumbnail with the placeholder only once.
  15. */
  16. GitHubClient.prototype.clientId = (window.location.hostname == 'test.draw.io') ? '23bc97120b9035515661' : '89c9e4624ca416554489';
  17. /**
  18. * OAuth scope.
  19. */
  20. GitHubClient.prototype.scope = 'repo';
  21. /**
  22. * Default extension for new files.
  23. */
  24. GitHubClient.prototype.extension = '.xml';
  25. /**
  26. * Base URL for API calls.
  27. */
  28. GitHubClient.prototype.baseUrl = 'https://api.github.com';
  29. /**
  30. * Maximum file size of the GitHub REST API.
  31. */
  32. GitHubClient.prototype.maxFileSize = 1000000 /*1MB*/;
  33. /**
  34. * Authorizes the client, gets the userId and calls <open>.
  35. */
  36. GitHubClient.prototype.updateUser = function(success, error)
  37. {
  38. var fn = mxUtils.bind(this, function()
  39. {
  40. var acceptResponse = true;
  41. var timeoutThread = window.setTimeout(mxUtils.bind(this, function()
  42. {
  43. acceptResponse = false;
  44. error({code: App.ERROR_TIMEOUT, retry: fn});
  45. }), this.ui.timeout);
  46. mxUtils.get(this.baseUrl + '/user?access_token=' + this.token, mxUtils.bind(this, function(userReq)
  47. {
  48. window.clearTimeout(timeoutThread);
  49. if (acceptResponse)
  50. {
  51. if (userReq.getStatus() === 401)
  52. {
  53. this.authorizeRequest(fn, error);
  54. }
  55. else if (userReq.getStatus() < 200 || userReq.getStatus() >= 300)
  56. {
  57. error({message: mxResources.get('accessDenied')});
  58. }
  59. else
  60. {
  61. var userInfo = JSON.parse(userReq.getText());
  62. this.setUser(new DrawioUser(userInfo.id, userInfo.email, userInfo.name));
  63. success();
  64. }
  65. }
  66. }));
  67. });
  68. fn();
  69. };
  70. /**
  71. * Authorizes the client, gets the userId and calls <open>.
  72. */
  73. GitHubClient.prototype.authorizeRequest = function(success, error)
  74. {
  75. this.ui.showAuthDialog(this, true, mxUtils.bind(this, function(remember, authSuccess)
  76. {
  77. // Initializes oauth flow
  78. window.open('https://github.com/login/oauth/authorize?client_id=' +
  79. this.clientId + '&scope=' + this.scope, 'oauth');
  80. window.onGitHubCallback = mxUtils.bind(this, function(code, authWindow)
  81. {
  82. window.onGitHubCallback = null;
  83. // Gets token for code via servlet
  84. var fn = mxUtils.bind(this, function()
  85. {
  86. var acceptResponse = true;
  87. var timeoutThread = window.setTimeout(mxUtils.bind(this, function()
  88. {
  89. acceptResponse = false;
  90. error({code: App.ERROR_TIMEOUT, retry: fn});
  91. }), this.ui.timeout);
  92. mxUtils.get('/github?client_id=' + this.clientId + '&code=' + code, mxUtils.bind(this, function(authReq)
  93. {
  94. window.clearTimeout(timeoutThread);
  95. if (acceptResponse)
  96. {
  97. try
  98. {
  99. if (authWindow != null)
  100. {
  101. authWindow.close();
  102. }
  103. if (authReq.getStatus() < 200 || authReq.getStatus() >= 300)
  104. {
  105. error({message: mxResources.get('cannotLogin')});
  106. }
  107. else
  108. {
  109. if (authSuccess != null)
  110. {
  111. authSuccess();
  112. }
  113. var res = authReq.getText();
  114. this.token = res.substring(res.indexOf('=') + 1, res.indexOf('&'));
  115. if (remember)
  116. {
  117. this.setPersistentToken(this.token);
  118. }
  119. success();
  120. }
  121. }
  122. catch (e)
  123. {
  124. error(e);
  125. }
  126. }
  127. }));
  128. });
  129. fn();
  130. });
  131. }));
  132. };
  133. /**
  134. * Authorizes the client, gets the userId and calls <open>.
  135. */
  136. GitHubClient.prototype.executeRequest = function(req, success, error)
  137. {
  138. var doExecute = mxUtils.bind(this, function()
  139. {
  140. var acceptResponse = true;
  141. var timeoutThread = window.setTimeout(mxUtils.bind(this, function()
  142. {
  143. acceptResponse = false;
  144. error({code: App.ERROR_TIMEOUT, retry: fn});
  145. }), this.ui.timeout);
  146. var temp = this.token;
  147. req.setRequestHeaders = function(request, params)
  148. {
  149. request.setRequestHeader('Authorization', 'token ' + temp);
  150. };
  151. req.send(mxUtils.bind(this, function()
  152. {
  153. window.clearTimeout(timeoutThread);
  154. if (acceptResponse)
  155. {
  156. if (req.getStatus() >= 200 && req.getStatus() <= 299)
  157. {
  158. success(req);
  159. }
  160. else if (req.getStatus() === 401)
  161. {
  162. this.authorizeRequest(fn, error);
  163. }
  164. else if (req.getStatus() === 403)
  165. {
  166. var tooLarge = false;
  167. try
  168. {
  169. var temp = JSON.parse(req.getText());
  170. if (temp != null && temp.errors != null && temp.errors.length > 0)
  171. {
  172. tooLarge = temp.errors[0].code == 'too_large';
  173. }
  174. }
  175. catch (e)
  176. {
  177. // ignore
  178. }
  179. error({message: mxResources.get((tooLarge) ? 'drawingTooLarge' : 'forbidden')});
  180. }
  181. else if (req.getStatus() === 404)
  182. {
  183. error({message: mxResources.get('fileNotFound')});
  184. }
  185. else if (req.getStatus() === 409)
  186. {
  187. // Special case: flag to the caller that there was a conflict
  188. error({status: 409});
  189. }
  190. else
  191. {
  192. error({message: mxResources.get('error') + ' ' + req.getStatus()});
  193. }
  194. }
  195. }), error);
  196. });
  197. var fn = mxUtils.bind(this, function()
  198. {
  199. if (this.user == null)
  200. {
  201. this.updateUser(doExecute, error);
  202. }
  203. else
  204. {
  205. doExecute();
  206. }
  207. });
  208. if (this.token === null)
  209. {
  210. this.authorizeRequest(fn, error);
  211. }
  212. else
  213. {
  214. fn();
  215. }
  216. };
  217. /**
  218. * Checks if the client is authorized and calls the next step.
  219. */
  220. GitHubClient.prototype.getLibrary = function(path, success, error)
  221. {
  222. this.getFile(path, success, error, true);
  223. };
  224. /**
  225. * Checks if the client is authorized and calls the next step.
  226. */
  227. GitHubClient.prototype.getFile = function(path, success, error, asLibrary)
  228. {
  229. asLibrary = (asLibrary != null) ? asLibrary : false;
  230. var tokens = path.split('/');
  231. var org = tokens[0];
  232. var repo = tokens[1];
  233. var ref = tokens[2];
  234. var path = tokens.slice(3, tokens.length).join('/');
  235. // Handles .vsdx, Gliffy and PNG+XML files by creating a temporary file
  236. if (/\.vsdx$/i.test(path) || /\.gliffy$/i.test(path) || /\.png$/i.test(path))
  237. {
  238. // Should never be null
  239. if (this.token != null)
  240. {
  241. var url = this.baseUrl + '/repos/' + org + '/' + repo +
  242. '/contents/' + path + '?ref=' + encodeURIComponent(ref) +
  243. '&token=' + this.token;
  244. var tokens = path.split('/');
  245. var name = (tokens.length > 0) ? tokens[tokens.length - 1] : path;
  246. this.ui.convertFile(url, name, null, this.extension, success, error);
  247. }
  248. else if (error != null)
  249. {
  250. error();
  251. }
  252. }
  253. else
  254. {
  255. var req = new mxXmlRequest(this.baseUrl + '/repos/' + org + '/' + repo +
  256. '/contents/' + path + '?ref=' + encodeURIComponent(ref), null, 'GET');
  257. this.executeRequest(req, mxUtils.bind(this, function(req)
  258. {
  259. try
  260. {
  261. success(this.createGitHubFile(org, repo, ref, JSON.parse(req.getText()), asLibrary));
  262. }
  263. catch (e)
  264. {
  265. if (error != null)
  266. {
  267. error(e);
  268. }
  269. }
  270. }), error);
  271. }
  272. };
  273. /**
  274. * Translates this point by the given vector.
  275. *
  276. * @param {number} dx X-coordinate of the translation.
  277. * @param {number} dy Y-coordinate of the translation.
  278. */
  279. GitHubClient.prototype.createGitHubFile = function(org, repo, ref, data, asLibrary)
  280. {
  281. var meta = {'org': org, 'repo': repo, 'ref': ref, 'name': data.name,
  282. 'path': data.path, 'sha': data.sha, 'html_url': data.html_url,
  283. 'download_url': data.download_url};
  284. var content = data.content;
  285. if (data.encoding === 'base64')
  286. {
  287. content = Base64.decode(content);
  288. }
  289. return (asLibrary) ? new GitHubLibrary(this.ui, content, meta) : new GitHubFile(this.ui, content, meta);
  290. };
  291. /**
  292. * Translates this point by the given vector.
  293. *
  294. * @param {number} dx X-coordinate of the translation.
  295. * @param {number} dy Y-coordinate of the translation.
  296. */
  297. GitHubClient.prototype.insertLibrary = function(filename, data, success, error, folderId)
  298. {
  299. this.insertFile(filename, data, success, error, true, folderId, false);
  300. };
  301. /**
  302. * Translates this point by the given vector.
  303. *
  304. * @param {number} dx X-coordinate of the translation.
  305. * @param {number} dy Y-coordinate of the translation.
  306. */
  307. GitHubClient.prototype.insertFile = function(filename, data, success, error, asLibrary, folderId, base64Encoded)
  308. {
  309. asLibrary = (asLibrary != null) ? asLibrary : false;
  310. var tokens = folderId.split('/');
  311. var org = tokens[0];
  312. var repo = tokens[1];
  313. var ref = tokens[2];
  314. var path = tokens.slice(3, tokens.length).join('/');
  315. if (path.length > 0)
  316. {
  317. path = path + '/';
  318. }
  319. path = path + filename;
  320. this.checkExists(org + '/' + repo + '/' + ref + '/' + path, true, mxUtils.bind(this, function(checked, sha)
  321. {
  322. if (checked)
  323. {
  324. // Does not insert file here as there is another writeFile implicit via fileCreated
  325. if (!asLibrary)
  326. {
  327. success(new GitHubFile(this.ui, data, {'org': org, 'repo': repo, 'ref': ref,
  328. 'name': filename, 'path': path, 'sha': sha, isNew: true}));
  329. }
  330. else
  331. {
  332. if (!base64Encoded)
  333. {
  334. data = Base64.encode(data);
  335. }
  336. this.showCommitDialog(filename, true, mxUtils.bind(this, function(message)
  337. {
  338. this.writeFile(org, repo, ref, path, message, data, sha, mxUtils.bind(this, function(req)
  339. {
  340. try
  341. {
  342. var msg = JSON.parse(req.getText());
  343. success(this.createGitHubFile(org, repo, ref, msg.content, asLibrary));
  344. }
  345. catch (e)
  346. {
  347. if (error != null)
  348. {
  349. error(e);
  350. }
  351. }
  352. }), error);
  353. }), mxUtils.bind(this, function()
  354. {
  355. if (error != null)
  356. {
  357. error();
  358. }
  359. }));
  360. }
  361. }
  362. else if (error != null)
  363. {
  364. error();
  365. }
  366. }))
  367. };
  368. /**
  369. *
  370. */
  371. GitHubClient.prototype.showCommitDialog = function(filename, isNew, success, cancel)
  372. {
  373. // Pauses spinner while commit message dialog is shown
  374. var resume = this.ui.spinner.pause();
  375. var dlg = new FilenameDialog(this.ui, mxResources.get((isNew) ? 'addedFile' : 'updateFile',
  376. [filename]), mxResources.get('ok'), mxUtils.bind(this, function(message)
  377. {
  378. resume();
  379. success(message);
  380. }), mxResources.get('commitMessage'), null, null, null, null, mxUtils.bind(this, function()
  381. {
  382. cancel();
  383. }));
  384. this.ui.showDialog(dlg.container, 300, 80, true, false);
  385. dlg.init();
  386. };
  387. /**
  388. *
  389. */
  390. GitHubClient.prototype.writeFile = function(org, repo, ref, path, message, data, sha, success, error)
  391. {
  392. if (data.length >= this.maxFileSize)
  393. {
  394. if (error != null)
  395. {
  396. error({message: mxResources.get('drawingTooLarge') + ' (' +
  397. this.ui.formatFileSize(data.length) + ' / 1 MB)'});
  398. }
  399. }
  400. else
  401. {
  402. var entity =
  403. {
  404. path: path,
  405. message: message,
  406. content: data
  407. };
  408. if (sha != null)
  409. {
  410. entity.sha = sha;
  411. }
  412. var req = new mxXmlRequest(this.baseUrl + '/repos/' + org + '/' + repo +
  413. '/contents/' + path + '?ref=' + encodeURIComponent(ref),
  414. JSON.stringify(entity), 'PUT');
  415. this.executeRequest(req, mxUtils.bind(this, function(req)
  416. {
  417. success(req);
  418. }), error);
  419. }
  420. };
  421. /**
  422. * Translates this point by the given vector.
  423. *
  424. * @param {number} dx X-coordinate of the translation.
  425. * @param {number} dy Y-coordinate of the translation.
  426. */
  427. GitHubClient.prototype.checkExists = function(path, askReplace, fn)
  428. {
  429. this.getFile(path, mxUtils.bind(this, function(file)
  430. {
  431. if (askReplace && file.meta != null)
  432. {
  433. var resume = this.ui.spinner.pause();
  434. this.ui.confirm(mxResources.get('replaceIt', [path]), function()
  435. {
  436. resume();
  437. fn(true, file.meta.sha);
  438. }, function()
  439. {
  440. resume();
  441. fn(false);
  442. });
  443. }
  444. else
  445. {
  446. this.ui.spinner.stop();
  447. this.ui.showError(mxResources.get('error'), mxResources.get('fileExists'), mxResources.get('ok'), function()
  448. {
  449. fn(false);
  450. });
  451. }
  452. }), mxUtils.bind(this, function(err)
  453. {
  454. fn(true);
  455. }));
  456. };
  457. /**
  458. * Translates this point by the given vector.
  459. *
  460. * @param {number} dx X-coordinate of the translation.
  461. * @param {number} dy Y-coordinate of the translation.
  462. */
  463. GitHubClient.prototype.saveFile = function(file, success, error)
  464. {
  465. var org = file.meta.org;
  466. var repo = file.meta.repo;
  467. var ref = file.meta.ref;
  468. var path = file.meta.path;
  469. this.showCommitDialog(file.meta.name, file.meta.sha == null || file.meta.isNew, mxUtils.bind(this, function(message)
  470. {
  471. var data = Base64.encode(file.getData());
  472. var fn = mxUtils.bind(this, function(sha)
  473. {
  474. this.writeFile(org, repo, ref, path, message, data, sha, mxUtils.bind(this, function(req)
  475. {
  476. delete file.meta.isNew;
  477. success(JSON.parse(req.getText()));
  478. }), mxUtils.bind(this, function(err)
  479. {
  480. // Handles special conflict case where overwrite needs an update of the sha
  481. if (err != null && err.status == 409)
  482. {
  483. resume = this.ui.spinner.pause();
  484. var dlg = new ErrorDialog(this.ui, mxResources.get('errorSavingFile'),
  485. mxResources.get('fileChangedOverwrite'), mxResources.get('cancel'), mxUtils.bind(this, function()
  486. {
  487. error();
  488. }), null, mxResources.get('overwrite'), mxUtils.bind(this, function()
  489. {
  490. resume();
  491. // Gets the latest sha and tries again
  492. this.getFile(org + '/' + repo + '/' + ref + '/' + path, mxUtils.bind(this, function(tempFile)
  493. {
  494. fn(tempFile.meta.sha);
  495. }));
  496. }));
  497. this.ui.showDialog(dlg.container, 340, 150, true, false);
  498. dlg.init();
  499. }
  500. else
  501. {
  502. error(err);
  503. }
  504. }));
  505. });
  506. fn(file.meta.sha);
  507. }), mxUtils.bind(this, function()
  508. {
  509. error();
  510. }));
  511. };
  512. /**
  513. * Checks if the client is authorized and calls the next step.
  514. */
  515. GitHubClient.prototype.pickLibrary = function(fn)
  516. {
  517. this.pickFile(fn);
  518. };
  519. /**
  520. * Checks if the client is authorized and calls the next step.
  521. */
  522. GitHubClient.prototype.pickFolder = function(fn)
  523. {
  524. this.showGitHubDialog(false, fn);
  525. };
  526. /**
  527. * Checks if the client is authorized and calls the next step.
  528. */
  529. GitHubClient.prototype.pickFile = function(fn)
  530. {
  531. fn = (fn != null) ? fn : mxUtils.bind(this, function(path)
  532. {
  533. this.ui.loadFile('H' + encodeURIComponent(path));
  534. });
  535. this.showGitHubDialog(true, fn);
  536. };
  537. /**
  538. *
  539. */
  540. GitHubClient.prototype.showGitHubDialog = function(showFiles, fn)
  541. {
  542. var org = null;
  543. var repo = null;
  544. var ref = null;
  545. var path = null;
  546. var content = document.createElement('div');
  547. content.style.whiteSpace = 'nowrap';
  548. content.style.overflow = 'hidden';
  549. content.style.height = '224px';
  550. var hd = document.createElement('h3');
  551. mxUtils.write(hd, mxResources.get((showFiles) ? 'selectFile' : 'selectFolder'));
  552. hd.style.cssText = 'width:100%;text-align:center;margin-top:0px;margin-bottom:12px';
  553. content.appendChild(hd);
  554. var div = document.createElement('div');
  555. div.style.whiteSpace = 'nowrap';
  556. div.style.overflow = 'auto';
  557. div.style.height = '194px';
  558. content.appendChild(div);
  559. var dlg = new CustomDialog(this.ui, content, mxUtils.bind(this, function()
  560. {
  561. fn(org + '/' + repo + '/' + ref + '/' + path);
  562. }));
  563. this.ui.showDialog(dlg.container, 340, 270, true, true);
  564. if (showFiles)
  565. {
  566. dlg.okButton.parentNode.removeChild(dlg.okButton);
  567. }
  568. var createLink = mxUtils.bind(this, function(label, fn)
  569. {
  570. var link = document.createElement('a');
  571. link.setAttribute('href', 'javascript:void(0);');
  572. mxUtils.write(link, label);
  573. mxEvent.addListener(link, 'click', fn);
  574. return link;
  575. });
  576. var updatePathInfo = mxUtils.bind(this, function(hideRef)
  577. {
  578. var pathInfo = document.createElement('div');
  579. pathInfo.style.marginBottom = '8px';
  580. pathInfo.appendChild(createLink(org + '/' + repo, mxUtils.bind(this, function()
  581. {
  582. path = null;
  583. selectRepo();
  584. })));
  585. if (!hideRef)
  586. {
  587. mxUtils.write(pathInfo, ' / ');
  588. pathInfo.appendChild(createLink(ref, mxUtils.bind(this, function()
  589. {
  590. path = null;
  591. selectRef();
  592. })));
  593. }
  594. if (path != null && path.length > 0)
  595. {
  596. var tokens = path.split('/');
  597. for (var i = 0; i < tokens.length; i++)
  598. {
  599. (function(index)
  600. {
  601. mxUtils.write(pathInfo, ' / ');
  602. pathInfo.appendChild(createLink(tokens[index], mxUtils.bind(this, function()
  603. {
  604. path = tokens.slice(0, index + 1).join('/');
  605. selectFile();
  606. })));
  607. })(i);
  608. }
  609. }
  610. div.appendChild(pathInfo);
  611. });
  612. var selectFile = mxUtils.bind(this, function()
  613. {
  614. var req = new mxXmlRequest(this.baseUrl + '/repos/' + org + '/' + repo +
  615. '/contents/' + path + '?ref=' + encodeURIComponent(ref), null, 'GET');
  616. dlg.okButton.removeAttribute('disabled');
  617. div.innerHTML = '';
  618. this.ui.spinner.spin(div, mxResources.get('loading'));
  619. this.executeRequest(req, mxUtils.bind(this, function(req)
  620. {
  621. updatePathInfo();
  622. this.ui.spinner.stop();
  623. var files = JSON.parse(req.getText());
  624. div.appendChild(createLink('../ [Up]', mxUtils.bind(this, function()
  625. {
  626. if (path == '')
  627. {
  628. path = null;
  629. selectRepo();
  630. }
  631. else
  632. {
  633. var tokens = path.split('/');
  634. path = tokens.slice(0, tokens.length - 1).join('/');
  635. selectFile();
  636. }
  637. })));
  638. mxUtils.br(div);
  639. if (files == null || files.length == 0)
  640. {
  641. mxUtils.write(div, mxResources.get('noFiles'));
  642. }
  643. else
  644. {
  645. var listFiles = mxUtils.bind(this, function(showFolders)
  646. {
  647. for (var i = 0; i < files.length; i++)
  648. {
  649. (mxUtils.bind(this, function(file)
  650. {
  651. if (showFolders == (file.type == 'dir'))
  652. {
  653. div.appendChild(createLink(file.name + ((file.type == 'dir') ? '/' : ''), mxUtils.bind(this, function()
  654. {
  655. if (file.type == 'dir')
  656. {
  657. path = file.path;
  658. selectFile();
  659. }
  660. else if (showFiles && file.type == 'file')
  661. {
  662. this.ui.hideDialog();
  663. fn(org + '/' + repo + '/' + ref + '/' + file.path);
  664. }
  665. })));
  666. mxUtils.br(div);
  667. }
  668. }))(files[i]);
  669. }
  670. });
  671. listFiles(true);
  672. if (showFiles)
  673. {
  674. listFiles(false);
  675. }
  676. }
  677. }), mxUtils.bind(this, function(err)
  678. {
  679. this.ui.spinner.stop();
  680. updatePathInfo(true);
  681. this.ui.handleError(err);
  682. }));
  683. });
  684. var selectRef = mxUtils.bind(this, function()
  685. {
  686. var req = new mxXmlRequest(this.baseUrl + '/repos/' + org + '/' + repo + '/branches', null, 'GET');
  687. dlg.okButton.setAttribute('disabled', 'disabled');
  688. div.innerHTML = '';
  689. this.ui.spinner.spin(div, mxResources.get('loading'));
  690. this.executeRequest(req, mxUtils.bind(this, function(req)
  691. {
  692. this.ui.spinner.stop();
  693. updatePathInfo(true);
  694. var branches = JSON.parse(req.getText());
  695. div.appendChild(createLink('../ [Up]', mxUtils.bind(this, function()
  696. {
  697. path = null;
  698. selectRepo();
  699. })));
  700. mxUtils.br(div);
  701. if (branches == null || branches.length == 0)
  702. {
  703. mxUtils.write(div, mxResources.get('noFiles'));
  704. }
  705. else
  706. {
  707. for (var i = 0; i < branches.length; i++)
  708. {
  709. (mxUtils.bind(this, function(branch)
  710. {
  711. div.appendChild(createLink(branch.name, mxUtils.bind(this, function()
  712. {
  713. ref = branch.name;
  714. path = '';
  715. selectFile();
  716. })));
  717. mxUtils.br(div);
  718. }))(branches[i]);
  719. }
  720. }
  721. }), mxUtils.bind(this, function(err)
  722. {
  723. this.ui.spinner.stop();
  724. updatePathInfo(true);
  725. this.ui.handleError(err);
  726. }));
  727. });
  728. var selectRepo = mxUtils.bind(this, function()
  729. {
  730. var req = new mxXmlRequest(this.baseUrl + '/user/repos', null, 'GET');
  731. dlg.okButton.setAttribute('disabled', 'disabled');
  732. div.innerHTML = '';
  733. this.ui.spinner.spin(div, mxResources.get('loading'));
  734. this.executeRequest(req, mxUtils.bind(this, function(req)
  735. {
  736. this.ui.spinner.stop();
  737. var repos = JSON.parse(req.getText());
  738. if (repos == null || repos.length == 0)
  739. {
  740. mxUtils.write(div, mxResources.get('noFiles'));
  741. }
  742. else
  743. {
  744. for (var i = 0; i < repos.length; i++)
  745. {
  746. (mxUtils.bind(this, function(repository)
  747. {
  748. div.appendChild(createLink(repository.full_name, mxUtils.bind(this, function()
  749. {
  750. org = repository.owner.login;
  751. repo = repository.name;
  752. ref = repository.default_branch;
  753. path = '';
  754. selectFile();
  755. })));
  756. mxUtils.br(div);
  757. }))(repos[i]);
  758. }
  759. }
  760. }), mxUtils.bind(this, function(err)
  761. {
  762. this.ui.spinner.stop();
  763. this.ui.handleError(err);
  764. }));
  765. });
  766. selectRepo();
  767. };
  768. /**
  769. * Checks if the client is authorized and calls the next step.
  770. */
  771. GitHubClient.prototype.logout = function()
  772. {
  773. this.setUser(null);
  774. this.clearPersistentToken();
  775. this.token = null;
  776. };