mxODPicker.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. function mxODPicker(container, previewFn, getODFilesList, getODFileInfo, getRecentList, addToRecent, pickedFileCallback,
  2. errorFn, foldersOnly, backFn, withSubmitBtn, withThumbnail, initFolderPath, acceptAllFiles)
  3. {
  4. var previewHtml = '';
  5. if (previewFn == null)
  6. {
  7. previewFn = renderFile;
  8. previewHtml = '<div style="text-align: center;" class="odPreview"></div>';
  9. }
  10. if (getRecentList == null)
  11. {
  12. getRecentList = function()
  13. {
  14. var list = null;
  15. try
  16. {
  17. list = JSON.parse(localStorage.getItem('mxODPickerRecentList'));
  18. }
  19. catch(e){}
  20. return list;
  21. }
  22. }
  23. if (addToRecent == null)
  24. {
  25. addToRecent = function(file)
  26. {
  27. if (file == null)
  28. {
  29. return;
  30. }
  31. var recentList = getRecentList() || {};
  32. delete file['@microsoft.graph.downloadUrl'];
  33. recentList[file.id] = file;
  34. localStorage.setItem('mxODPickerRecentList', JSON.stringify(recentList));
  35. }
  36. }
  37. function _$(selector, elem)
  38. {
  39. elem = elem || document;
  40. return elem.querySelector(selector);
  41. };
  42. function _$$(selector, elem)
  43. {
  44. elem = elem || document;
  45. return elem.querySelectorAll(selector);
  46. };
  47. var html =
  48. '<div class="odCatsList">' +
  49. '<div class="odCatsListLbl">OneDrive</div>' +
  50. '<div id="odFiles" class="odCatListTitle odCatSelected">' + mxUtils.htmlEntities(mxResources.get('files')) + '</div>' +
  51. '<div id="odRecent" class="odCatListTitle">' + mxUtils.htmlEntities(mxResources.get('recent')) + '</div>' +
  52. '<div id="odShared" class="odCatListTitle">' + mxUtils.htmlEntities(mxResources.get('shared')) + '</div>' +
  53. '<div id="odSharepoint" class="odCatListTitle">' + mxUtils.htmlEntities(mxResources.get('sharepoint')) + '</div>' +
  54. '</div>' +
  55. '<div class="odFilesSec">' +
  56. '<div class="searchBar" style="display:none"><input type="search" id="odSearchBox" placeholder="' + mxUtils.htmlEntities(mxResources.get('search')) + '"></div>' +
  57. '<div class="odFilesBreadcrumb"></div>' +
  58. '<div id="refreshOD" class="odRefreshButton">' +
  59. '<img class="geAdaptiveAsset" src="/images/update32.png" width="16" height="16" title="' + mxUtils.htmlEntities(mxResources.get('refresh')) + 'Refresh" border="0"/>' +
  60. '</div>' +
  61. '<div class="odFilesList"></div>' +
  62. '</div>' +
  63. previewHtml +
  64. (backFn? '<div id="odBackBtn" class="odLinkBtn">&lt; ' + mxUtils.htmlEntities(mxResources.get('back')) + '</div>' : '') +
  65. (withSubmitBtn? '<button id="odSubmitBtn" class="odSubmitBtn">' + mxUtils.htmlEntities(mxResources.get(foldersOnly? 'select' : 'open')) + '</button>' : '');
  66. var isDarkMode = window.Editor != null && Editor.isDarkMode != null && Editor.isDarkMode();
  67. var css =
  68. '.odCatsList *, .odFilesSec * { user-select: none; }' +
  69. '.odCatsList {' +
  70. ' box-sizing: border-box;' +
  71. ' position:absolute;' +
  72. ' top:0px;' +
  73. ' bottom:50%;' +
  74. ' width:30%;' +
  75. ' border: 1px solid #CCCCCC;' +
  76. ' border-bottom:none;' +
  77. ' display: inline-block;' +
  78. ' overflow-x: hidden;' +
  79. ' overflow-y: auto;' +
  80. '}' +
  81. '.odCatsListLbl {' +
  82. ' height: 17px;' +
  83. ' color: #6D6D6D;' +
  84. ' font-size: 14px;' +
  85. ' font-weight: bold;' +
  86. ' line-height: 17px;' +
  87. ' margin: 10px 0 3px 5px;' +
  88. '}' +
  89. '.odFilesSec {' +
  90. ' box-sizing: border-box;' +
  91. ' position:absolute;' +
  92. ' left:30%;' +
  93. ' top:0px;' +
  94. ' bottom:50%;' +
  95. ' width: 70%;' +
  96. ' border: 1px solid #CCCCCC;' +
  97. ' border-left:none;' +
  98. ' border-bottom:none;' +
  99. ' display: inline-block;' +
  100. ' overflow: hidden;' +
  101. '}' +
  102. '.odFilesBreadcrumb {' +
  103. ' box-sizing: border-box;' +
  104. ' position:absolute;' +
  105. ' min-height: 32px;' +
  106. ' left:0px;' +
  107. ' right:20px;' +
  108. ' text-overflow:ellipsis;' +
  109. ' overflow:hidden;' +
  110. ' font-size: 13px;' +
  111. ' color: #6D6D6D;' +
  112. ' padding: 5px;' +
  113. '}' +
  114. '.odRefreshButton {' +
  115. ' box-sizing: border-box;' +
  116. ' position:absolute;' +
  117. ' right:0px;' +
  118. ' top:0px;' +
  119. ' padding: 4px;' +
  120. ' margin: 1px;' +
  121. ' height:24px;' +
  122. ' cursor:default;' +
  123. '}' +
  124. '.odRefreshButton>img {' +
  125. ' opacity:0.5;' +
  126. '}' +
  127. '.odRefreshButton:hover {' +
  128. ' background-color:#ddd;' +
  129. ' border-radius:50%;' +
  130. '}' +
  131. // '.odRefreshButton:hover {' +
  132. // ' background-color:#ddd;' +
  133. // ' border-radius:50%;' +
  134. // '}' +
  135. '.odRefreshButton:active {' +
  136. ' opacity:0.7;' +
  137. '}' +
  138. '.odFilesList {' +
  139. ' box-sizing: border-box;' +
  140. ' position:absolute;' +
  141. ' top:32px;' +
  142. ' bottom:0px;' +
  143. ' width: 100%;' +
  144. ' overflow-x: hidden;' +
  145. ' overflow-y: auto;' +
  146. '}' +
  147. '.odFileImg {' +
  148. ' width: 24px;' +
  149. ' padding-left: 5px;' +
  150. ' padding-right: 5px;' +
  151. '}' +
  152. '.odFileTitle {' +
  153. ' cursor: default;' +
  154. ' font-weight: normal;' +
  155. ' color: #666666 !important;' +
  156. ' width: calc(100% - 20px);' +
  157. ' white-space: nowrap;' +
  158. ' overflow: hidden;' +
  159. ' text-overflow: ellipsis;' +
  160. '}' +
  161. '.odFileListGrid {' +
  162. ' width: 100%;' +
  163. ' white-space: nowrap;' +
  164. ' font-size: 13px;' +
  165. ' box-sizing: border-box;' +
  166. ' border-spacing: 0;' +
  167. '}' +
  168. '.odOddRow {' +
  169. (isDarkMode ? '' : ' background-color: #eeeeee;') +
  170. '}' +
  171. '.odEvenRow {' +
  172. (isDarkMode ? '' : ' background-color: #FFFFFF;') +
  173. '}' +
  174. '.odRowSelected {' +
  175. ' background-color: #cadfff;' +
  176. '}' +
  177. '.odCatListTitle {' +
  178. ' box-sizing: border-box;' +
  179. ' height: 17px;' +
  180. ' cursor: default;' +
  181. ' color: #666666;' +
  182. ' font-size: 14px;' +
  183. ' line-height: 17px;' +
  184. ' margin: 5px 0 5px 0px;' +
  185. ' padding-left: 10px;' +
  186. '}' +
  187. '.odCatSelected {' +
  188. ' font-weight: bold;' +
  189. ' background-color: #cadfff;' +
  190. '}' +
  191. '.odEmptyFolder {' +
  192. ' height: 17px;' +
  193. ' color: #6D6D6D;' +
  194. ' font-size: 14px;' +
  195. ' font-weight: bold;' +
  196. ' line-height: 17px;' +
  197. ' margin: 10px 0 3px 5px;' +
  198. ' width: 100%;' +
  199. ' text-align: center;' +
  200. '}' +
  201. '.odBCFolder {' +
  202. ' cursor: pointer;' +
  203. ' color: #0432ff;' +
  204. '}' +
  205. '.odPreviewStatus {' +
  206. ' position:absolute;' +
  207. ' text-align:center;' +
  208. ' width:100%;' +
  209. ' top:50%;' +
  210. ' transform: translateY(-50%);' +
  211. ' font-size:13px;' +
  212. ' opacity:0.5;' +
  213. '}' +
  214. '.odPreview {' +
  215. ' position:absolute;' +
  216. ' overflow:hidden;' +
  217. ' border: 1px solid #CCCCCC;' +
  218. ' bottom:0px;' +
  219. ' top: 50%;' +
  220. ' left:0px;' +
  221. ' right:0px;' +
  222. '}' +
  223. '.odLinkBtn {' +
  224. ' position: absolute;' +
  225. ' font-size: 12px;' +
  226. ' cursor: pointer;' +
  227. ' color: #6D6D6D;' +
  228. ' left: 5px;' +
  229. ' bottom: 3px;' +
  230. '}' +
  231. '.odSubmitBtn {' +
  232. ' position: absolute;' +
  233. ' color: #333;' +
  234. ' right: 5px;' +
  235. ' bottom: 5px;' +
  236. '}';
  237. var opts =
  238. {
  239. left: '50%',
  240. lines: 12, // The number of lines to draw
  241. length: 8, // The length of each line
  242. width: 3, // The line thickness
  243. radius: 5, // The radius of the inner circle
  244. rotate: 0, // The rotation offset
  245. color: '#000', // #rgb or #rrggbb
  246. speed: 1, // Rounds per second
  247. trail: 60, // Afterglow percentage
  248. shadow: false, // Whether to render a shadow
  249. hwaccel: false, // Whether to use hardware acceleration
  250. className: 'spinner', // The CSS class to assign to the spinner
  251. zIndex: 2e9 // The z-index (defaults to 2000000000)
  252. };
  253. var spinner = new Spinner(opts);
  254. var editor = new Editor();
  255. var curViewer = null;
  256. var selectedFile = null;
  257. var selectedDriveId = null;
  258. var selectedSiteId = null;
  259. var requestInProgress = false;
  260. var breadcrumb = [];
  261. var lastFolderArgs = null;
  262. var loadingPreviewFile = null;
  263. function getDrawioFileDoc(file, success, error)
  264. {
  265. if (file['@microsoft.graph.downloadUrl'] == null)
  266. {
  267. if (file.parentReference == null)
  268. {
  269. error();
  270. }
  271. else
  272. {
  273. getODFileInfo(file.id, file.parentReference.driveId, function(completeFile)
  274. {
  275. getDrawioFileDoc(completeFile, success, error);
  276. }, error);
  277. return;
  278. }
  279. }
  280. var req = new XMLHttpRequest();
  281. //TODO find another way to disable caching (adding a parameter breaks the url)
  282. req.open('GET', file['@microsoft.graph.downloadUrl']);
  283. var isPng = file.file? (file.file.mimeType == 'image/png') : false;
  284. req.onreadystatechange = function()
  285. {
  286. if (this.readyState == 4)
  287. {
  288. if (this.status >= 200 && this.status <= 299)
  289. {
  290. try
  291. {
  292. var cnt = req.responseText;
  293. if (isPng)
  294. {
  295. cnt = 'data:image/png;base64,' + Editor.base64Encode(cnt);
  296. cnt = Editor.extractGraphModelFromPng(cnt);
  297. }
  298. else if (/\.pdf$/.test(file.name))
  299. {
  300. cnt = 'data:application/pdf;base64,' + Editor.base64Encode(cnt);
  301. cnt = Editor.extractGraphModelFromPdf(cnt);
  302. }
  303. var doc = mxUtils.parseXml(cnt);
  304. var node = (doc.documentElement.nodeName == 'mxlibrary') ?
  305. doc.documentElement : Editor.extractGraphModel(doc.documentElement);
  306. if (node != null)
  307. {
  308. success(node.ownerDocument);
  309. return;
  310. }
  311. }
  312. catch(e) {} //on error and if the doc is null, the following line will call the error
  313. }
  314. error();
  315. }
  316. };
  317. if ((isPng || /\.pdf$/.test(file.name)) && req.overrideMimeType)
  318. {
  319. req.overrideMimeType('text/plain; charset=x-user-defined');
  320. }
  321. req.send();
  322. };
  323. function doSubmit()
  324. {
  325. function submit(img)
  326. {
  327. pickedFileCallback(selectedFile, img);
  328. addToRecent(selectedFile);
  329. }
  330. if (withThumbnail && curViewer != null)
  331. {
  332. editor.exportToCanvas(function(canvas)
  333. {
  334. submit(EditorUi.prototype.createImageDataUri(canvas, null, 'png'));
  335. }, 400, null, null, function(err)
  336. {
  337. //TODO handle errors
  338. console.log(err);
  339. }, 600, null, null, null, null, null, curViewer);
  340. }
  341. else
  342. {
  343. submit();
  344. }
  345. };
  346. function renderFile(file)
  347. {
  348. if (prevDiv == null)
  349. {
  350. return;
  351. }
  352. prevDiv.style.background = 'transparent';
  353. prevDiv.innerText = '';
  354. function showRenderMsg(msg)
  355. {
  356. prevDiv.style.background = 'transparent';
  357. prevDiv.innerText = '';
  358. var status = document.createElement('div');
  359. status.className = 'odPreviewStatus';
  360. mxUtils.write(status, msg);
  361. prevDiv.appendChild(status);
  362. spinner.stop();
  363. };
  364. if (file == null || file.folder || /\.drawiolib$/.test(file.name))
  365. {
  366. showRenderMsg(mxResources.get('noPreview'));
  367. return;
  368. }
  369. try
  370. {
  371. // Workaround for parentReference access
  372. if (file.remoteItem != null)
  373. {
  374. file = file.remoteItem;
  375. }
  376. loadingPreviewFile = file;
  377. spinner.spin(prevDiv);
  378. getDrawioFileDoc(file, function(doc)
  379. {
  380. spinner.stop();
  381. if (loadingPreviewFile != file)
  382. {
  383. return;
  384. }
  385. else if (doc.documentElement.nodeName == 'mxlibrary')
  386. {
  387. showRenderMsg(mxResources.get('noPreview'));
  388. }
  389. else
  390. {
  391. var diagrams = doc.getElementsByTagName('diagram');
  392. curViewer = AspectDialog.prototype.createViewer(prevDiv,
  393. diagrams.length == 0? doc.documentElement : diagrams[0],
  394. null, 'transparent');
  395. }
  396. },
  397. function() //If the file is not a draw.io diagram
  398. {
  399. selectedFile = null;
  400. showRenderMsg(mxResources.get('notADiagramFile'));
  401. });
  402. }
  403. catch (e)
  404. {
  405. selectedFile = null;
  406. showRenderMsg(mxResources.get('notADiagramFile'));
  407. }
  408. };
  409. function renderBreadcrumb()
  410. {
  411. var bcDiv = _$('.odFilesBreadcrumb');
  412. if (bcDiv == null) return;
  413. bcDiv.innerText = '';
  414. for (var i = 0; i < breadcrumb.length - 1; i++)
  415. {
  416. var folder = document.createElement('span');
  417. folder.className = 'odBCFolder';
  418. folder.innerHTML = mxUtils.htmlEntities(breadcrumb[i].name || mxResources.get('home'));
  419. bcDiv.appendChild(folder);
  420. (function(bcItem, index)
  421. {
  422. folder.addEventListener('click', function()
  423. {
  424. previewFn(null);
  425. breadcrumb = breadcrumb.slice(0, index);
  426. fillFolderFiles(bcItem.driveId, bcItem.folderId, bcItem.siteId, bcItem.name);
  427. });
  428. })(breadcrumb[i], i);
  429. var sep = document.createElement('span');
  430. sep.innerHTML = ' &gt; ';
  431. bcDiv.appendChild(sep);
  432. }
  433. if (breadcrumb[breadcrumb.length - 1] != null)
  434. {
  435. var curr = document.createElement('span');
  436. curr.innerHTML = mxUtils.htmlEntities((breadcrumb.length == 1) ?
  437. mxResources.get(foldersOnly ? 'selectFolder' : 'officeSelDiag') :
  438. (breadcrumb[breadcrumb.length - 1].name || mxResources.get('home')));
  439. bcDiv.appendChild(curr);
  440. }
  441. };
  442. function openFile()
  443. {
  444. if (selectedFile == null || requestInProgress) return;
  445. if (selectedDriveId == 'sharepoint')
  446. {
  447. fillFolderFiles('site', null, selectedFile.id, selectedFile.displayName);
  448. }
  449. else if (selectedDriveId == 'site')
  450. {
  451. fillFolderFiles('subsite', null, selectedFile.id, selectedFile.name);
  452. }
  453. else
  454. {
  455. var isFolder = selectedFile.folder;
  456. selectedFile = selectedFile.remoteItem? selectedFile.remoteItem : selectedFile; //handle remote items which is accessed indirectly
  457. var folderDI = (selectedFile.parentReference? selectedFile.parentReference.driveId : null) || selectedDriveId;
  458. var id = selectedFile.id;
  459. if (isFolder)
  460. {
  461. fillFolderFiles(folderDI, id, null, selectedFile.name);
  462. }
  463. else
  464. {
  465. doSubmit();
  466. }
  467. }
  468. };
  469. function fillFolderFiles(driveId, folderId, siteId, folderName, searchTxt)
  470. {
  471. if (requestInProgress) return;
  472. _$('.odCatsList').style.display = 'block';
  473. _$('.odFilesSec').style.display = 'block';
  474. // _$('#signOutLnk').style.display = '';
  475. if (prevDiv != null)
  476. {
  477. prevDiv.innerText = '';
  478. prevDiv.style.top = '50%';
  479. }
  480. requestInProgress = true;
  481. var acceptRequest = true;
  482. var isSharepointSites = 0;
  483. lastFolderArgs = arguments;
  484. function renderList(potentialDrawioFiles)
  485. {
  486. spinner.stop();
  487. var grid = document.createElement('table');
  488. grid.className = 'odFileListGrid';
  489. var currentItem = null;
  490. var count = 0;
  491. //TODO support paging
  492. for (var i = 0; potentialDrawioFiles!= null && i < potentialDrawioFiles.length; i++)
  493. {
  494. var item = potentialDrawioFiles[i];
  495. if (isSharepointSites == 1 && item.webUrl && !(item.webUrl.indexOf('sharepoint.com/sites/') > 0 || item.webUrl.indexOf('sharepoint.com/') < 0))
  496. {
  497. continue;
  498. }
  499. var title = item.displayName || item.name;
  500. var tooltip = mxUtils.htmlEntities(item.description || title);
  501. if (isSharepointSites)
  502. {
  503. item.folder = isSharepointSites == 2? {isRoot: true} : true;
  504. }
  505. var isFolder = item.folder != null;
  506. if (foldersOnly && !isFolder)
  507. {
  508. continue;
  509. }
  510. var row = document.createElement('tr');
  511. row.className = (count++) % 2? 'odOddRow' : 'odEvenRow';
  512. var td = document.createElement('td');
  513. td.style.width = '36px';
  514. var typeImg = document.createElement('img');
  515. typeImg.src = '/images/' + (isFolder? 'folder.png' : 'file.png');
  516. typeImg.className = 'odFileImg';
  517. td.appendChild(typeImg);
  518. row.appendChild(td);
  519. td = document.createElement('td');
  520. var titleDiv = document.createElement('div');
  521. titleDiv.className = "odFileTitle";
  522. titleDiv.innerHTML = mxUtils.htmlEntities(title);
  523. titleDiv.setAttribute('title', tooltip);
  524. td.appendChild(titleDiv);
  525. row.appendChild(td);
  526. grid.appendChild(row);
  527. if (currentItem == null)
  528. {
  529. currentItem = row;
  530. currentItem.className += ' odRowSelected';
  531. selectedFile = item;
  532. selectedDriveId = driveId;
  533. if (!acceptAllFiles)
  534. {
  535. previewFn(selectedFile);
  536. }
  537. }
  538. (function(item2, row2)
  539. {
  540. row.addEventListener('dblclick', openFile);
  541. row.addEventListener('click', function()
  542. {
  543. if (currentItem != row2)
  544. {
  545. currentItem.className = currentItem.className.replace('odRowSelected', '');
  546. currentItem = row2;
  547. currentItem.className += ' odRowSelected';
  548. selectedFile = item2;
  549. selectedDriveId = driveId;
  550. if (!acceptAllFiles)
  551. {
  552. previewFn(selectedFile);
  553. }
  554. }
  555. });
  556. })(item, row);
  557. }
  558. if (count == 0)
  559. {
  560. var emptyMsg = document.createElement('div');
  561. emptyMsg.className = 'odEmptyFolder';
  562. emptyMsg.innerHTML = mxUtils.htmlEntities(mxResources.get('folderEmpty', null, 'Folder is empty!'));
  563. filesList.appendChild(emptyMsg);
  564. }
  565. else
  566. {
  567. filesList.appendChild(grid);
  568. }
  569. renderBreadcrumb();
  570. requestInProgress = false;
  571. };
  572. var timeoutThread = setTimeout(function()
  573. {
  574. acceptRequest = false;
  575. requestInProgress = false;
  576. spinner.stop();
  577. errorFn(mxResources.get('timeout'));
  578. }, 20000); //20 sec timeout
  579. var filesList = _$('.odFilesList');
  580. filesList.innerText = '';
  581. spinner.spin(filesList);
  582. var url;
  583. switch(driveId)
  584. {
  585. case 'recent':
  586. breadcrumb = [{name: mxResources.get('recent', null, 'Recent'), driveId: driveId}];
  587. var recentList = getRecentList() || {};
  588. var list = [];
  589. for (var id in recentList)
  590. {
  591. list.push(recentList[id]);
  592. }
  593. clearTimeout(timeoutThread);
  594. renderList(list);
  595. return;
  596. case 'shared':
  597. url = '/me/drive/sharedWithMe';
  598. breadcrumb = [{name: mxResources.get('sharedWithMe', null, 'Shared With Me'), driveId: driveId}];
  599. break;
  600. case 'sharepoint':
  601. url = '/sites?search=';
  602. breadcrumb = [{name: mxResources.get('sharepointSites', null, 'Sharepoint Sites'), driveId: driveId}];
  603. isSharepointSites = 1;
  604. break;
  605. case 'site':
  606. breadcrumb.push({name: folderName, driveId: driveId, folderId: folderId, siteId: siteId});
  607. url = '/sites/' + siteId + '/drives';
  608. isSharepointSites = 2;
  609. break;
  610. case 'subsite':
  611. breadcrumb.push({name: folderName, driveId: driveId, folderId: folderId, siteId: siteId});
  612. url = '/drives/' + siteId + (folderId? '/items/' + folderId : '/root') + '/children';
  613. break;
  614. case 'search': //TODO search doesn't return any results, find out why then remove display: none from the searchBox
  615. driveId = selectedDriveId;
  616. breadcrumb = [{driveId: driveId, name: mxResources.get('back', null, 'Back')}];
  617. searchTxt = encodeURIComponent(searchTxt.replace(/\'/g, '\\\''));
  618. url = selectedSiteId? '/sites/' + selectedSiteId + '/drive/root/search(q=\'' + searchTxt + '\')' : (driveId? '/drives/' + driveId + '/root/search(q=\'' + searchTxt + '\')' : '/me/drive/root/search(q=\'' + searchTxt + '\')');
  619. break;
  620. default:
  621. if (folderId == null)
  622. {
  623. breadcrumb = [{driveId: driveId}];
  624. }
  625. else
  626. {
  627. breadcrumb.push({name: folderName, driveId: driveId, folderId: folderId});
  628. }
  629. url = (driveId? '/drives/' + driveId : '/me/drive') + (folderId? '/items/' + folderId : '/root') + '/children';
  630. }
  631. if (!isSharepointSites)
  632. {
  633. url += (url.indexOf('?') > 0 ? '&' : '?') + 'select=id,name,description,parentReference,file,createdBy,lastModifiedBy,lastModifiedDateTime,size,folder,remoteItem,@microsoft.graph.downloadUrl';
  634. }
  635. var potentialDrawioFiles = [];
  636. function getChunk(nextUrl)
  637. {
  638. getODFilesList(nextUrl? nextUrl : url, function(resp)
  639. {
  640. if (!acceptRequest) return;
  641. var list = resp.value || [];
  642. if (acceptAllFiles || isSharepointSites)
  643. {
  644. Array.prototype.push.apply(potentialDrawioFiles, list);
  645. }
  646. else
  647. {
  648. for (var i = 0; i < list.length; i++)
  649. {
  650. var file = list[i];
  651. var mimeType = file.file? file.file.mimeType : null;
  652. if (file.folder || mimeType == 'text/html' || mimeType == 'text/xml' || mimeType == 'application/xml' || mimeType == 'image/png'
  653. || /\.svg$/.test(file.name) || /\.html$/.test(file.name) || /\.xml$/.test(file.name) || /\.png$/.test(file.name)
  654. || /\.drawio$/.test(file.name) || /\.drawiolib$/.test(file.name) || /\.pdf$/.test(file.name))
  655. {
  656. potentialDrawioFiles.push(file);
  657. }
  658. }
  659. // Sorts entries by type and name
  660. potentialDrawioFiles.sort(function(a, b)
  661. {
  662. var nameA = a.name.toLowerCase();
  663. var nameB = b.name.toLowerCase();
  664. if (a.folder && !b.folder)
  665. {
  666. return -1;
  667. }
  668. else if (!a.folder && b.folder)
  669. {
  670. return 1;
  671. }
  672. else
  673. {
  674. if (nameA < nameB)
  675. {
  676. return -1;
  677. }
  678. else if (nameA > nameB)
  679. {
  680. return 1;
  681. }
  682. else
  683. {
  684. return 0;
  685. }
  686. }
  687. });
  688. }
  689. if (resp['@odata.nextLink'] && potentialDrawioFiles.length < 1000) // TODO Support dynamic paging instead of 1000 limit
  690. {
  691. getChunk(resp['@odata.nextLink']);
  692. }
  693. else
  694. {
  695. clearTimeout(timeoutThread);
  696. renderList(potentialDrawioFiles);
  697. }
  698. },
  699. function(err)
  700. {
  701. if (!acceptRequest) return;
  702. clearTimeout(timeoutThread);
  703. var errMsg = null;
  704. try
  705. {
  706. errMsg = JSON.parse(err.responseText).error.message;
  707. }
  708. catch(e){} //ignore errors
  709. errorFn(mxResources.get('errorFetchingFolder', null, 'Error fetching folder items') +
  710. (errMsg != null? ' (' + errMsg + ')' : ''));
  711. requestInProgress = false;
  712. spinner.stop();
  713. }, nextUrl != null);
  714. };
  715. getChunk();
  716. };
  717. this.getSelectedItem = function()
  718. {
  719. if (selectedFile != null)
  720. {
  721. addToRecent(selectedFile);
  722. }
  723. return selectedFile;
  724. }
  725. //Code execution starts here
  726. if (_$('#mxODPickerCss') == null)
  727. {
  728. var head = document.head || document.getElementsByTagName('head')[0],
  729. style = document.createElement('style');
  730. head.appendChild(style);
  731. style.type = 'text/css';
  732. style.id = 'mxODPickerCss';
  733. style.appendChild(document.createTextNode(css));
  734. }
  735. container.innerHTML = html;
  736. var prevDiv = _$('.odPreview');
  737. var selectedCat = _$('#odFiles');
  738. var cats = _$$('.odCatListTitle');
  739. function setSelectedCat(cat)
  740. {
  741. selectedCat.className = selectedCat.className.replace('odCatSelected', '');
  742. selectedCat = cat;
  743. selectedCat.className += ' odCatSelected';
  744. };
  745. for (var i = 0; i < cats.length; i++)
  746. {
  747. cats[i].addEventListener('click', function()
  748. {
  749. loadingPreviewFile = null;
  750. selectedFile = null;
  751. if (requestInProgress) return;
  752. setSelectedCat(this);
  753. switch(this.id)
  754. {
  755. case 'odFiles':
  756. fillFolderFiles();
  757. break;
  758. case 'odRecent':
  759. fillFolderFiles('recent');
  760. break;
  761. case 'odShared':
  762. fillFolderFiles('shared');
  763. break;
  764. case 'odSharepoint':
  765. fillFolderFiles('sharepoint');
  766. break;
  767. }
  768. });
  769. }
  770. //Search (Currently API doesn't work)
  771. var delayTimer = null;
  772. function doSearch(searchStr)
  773. {
  774. if (requestInProgress) return;
  775. delayTimer = null;
  776. fillFolderFiles('search', null, null, null, searchStr)
  777. };
  778. //Use keyup to detect delete and backspace
  779. _$('#odSearchBox').addEventListener('keyup', function(evt)
  780. {
  781. var searchInput = this;
  782. if (delayTimer != null)
  783. {
  784. clearTimeout(delayTimer);
  785. }
  786. if (evt.keyCode == 13)
  787. {
  788. doSearch(searchInput.value);
  789. }
  790. else
  791. {
  792. delayTimer = setTimeout(function()
  793. {
  794. doSearch(searchInput.value);
  795. }, 500);
  796. }
  797. });
  798. function refreshFolder()
  799. {
  800. if (lastFolderArgs != null)
  801. {
  802. previewFn(null);
  803. var temp = breadcrumb.slice();
  804. fillFolderFiles.apply(this, lastFolderArgs);
  805. breadcrumb = temp;
  806. }
  807. };
  808. _$('#refreshOD').addEventListener('click', refreshFolder);
  809. if (backFn)
  810. {
  811. _$('#odBackBtn').addEventListener('click', backFn);
  812. }
  813. if (withSubmitBtn)
  814. {
  815. _$('#odSubmitBtn').addEventListener('click', doSubmit);
  816. }
  817. if (initFolderPath != null)
  818. {
  819. var folderInfo = initFolderPath.pop();
  820. if (initFolderPath[0].driveId == 'sharepoint')
  821. {
  822. setSelectedCat(_$('#odSharepoint'));
  823. }
  824. breadcrumb = initFolderPath;
  825. fillFolderFiles(folderInfo.driveId, folderInfo.folderId, folderInfo.siteId, folderInfo.name);
  826. }
  827. else
  828. {
  829. fillFolderFiles();
  830. }
  831. };