mxODPicker.js 22 KB

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