FileManagerWindow.js 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. SC.loadPackage({ 'FileManagerWindow': {
  2. comment: 'I am a FileManagerWindow.',
  3. traits: ['Window'],
  4. properties: {
  5. fileManager: { comment: 'I hold the fileManager to which I am the window.' },
  6. context: {
  7. comment: 'I hold the context from which I was called ("","saveAs","chooseFile", "newPage")',
  8. transform: function(context) {
  9. if ( context == 'saveAs' || context == 'newPage' ) {
  10. this.get('content').querySelector('.sg-filemanager-directory-container').classList.add('nameInput');
  11. var self = this;
  12. var modalNameInput = document.createElement('input');
  13. modalNameInput.setAttribute('type', 'text');
  14. modalNameInput.classList.add('sg-modal-name-input');
  15. if ( context == 'saveAs' ) {
  16. modalNameInput.setAttribute('value', self.get('originalFileName') );
  17. } else if ( context == 'newPage' ) {
  18. modalNameInput.setAttribute('value', 'NewPage.html' );
  19. }
  20. modalNameInput.addEventListener('keyup', function(evt) {
  21. if ( evt.target.value.length >= 1 ) {
  22. self.set({ confirmPath: self.get('basePath') +'/'+ evt.target.value });
  23. self.get('content').querySelector('button.confirm').classList.add('active');
  24. } else {
  25. self.get('content').querySelector('button.confirm').classList.remove('active');
  26. }
  27. });
  28. modalNameInput.addEventListener('paste', function(evt) {
  29. if ( evt.target.value.length >= 1 ) {
  30. self.set({ confirmPath: self.get('basePath') +'/'+ evt.target.value });
  31. self.get('content').querySelector('button.confirm').classList.add('active');
  32. } else {
  33. self.get('content').querySelector('button.confirm').classList.remove('active');
  34. }
  35. });
  36. this.get('content').querySelector('.sg-filemanager-directory-container').appendChild(modalNameInput);
  37. modalNameInput.focus();
  38. } else if ( context == 'chooseFile' ) {
  39. this.get('content').querySelector('.sg-filemanager-directory-container').classList.remove('nameInput');
  40. if ( this.get('content').querySelector('.sg-modal-name-input') ) {
  41. this.get('content').querySelector('.sg-modal-name-input').remove();
  42. }
  43. } else {
  44. this.get('content').querySelector('.sg-filemanager-directory-container').classList.remove('nameInput');
  45. if ( this.get('content').querySelector('.sg-modal-name-input') ) {
  46. this.get('content').querySelector('.sg-modal-name-input').remove();
  47. }
  48. }
  49. return context;
  50. }
  51. },
  52. currentPath: {
  53. comment: 'I hold the current file path.',
  54. transform: function(path){
  55. var cleanPath;
  56. if ( path == '/' ) {
  57. cleanPath = '';
  58. } else {
  59. cleanPath = path;
  60. }
  61. this.set({ basePath: cleanPath });
  62. this.get('directoryControlContainer').querySelector('.sg-filemanager-current-path').innerHTML = path;
  63. return path;
  64. }
  65. },
  66. originalFileName: { comment: 'I hold the original file name when the context is "saveAs".' },
  67. callback: { comment: 'I hold the callback when the context is "saveAs" or "chooseFile".' },
  68. basePath: { comment: 'I hold the current base path which strips of "/" when currentPath is the root directory.' },
  69. selectPath: {
  70. comment: 'I hold the path that was given to me to auto-select a file / directory.',
  71. transform: function(path) {
  72. if ( path && path.indexOf('/resources/img/') == -1 ) {
  73. var fullPath = path;
  74. var fullPathArray = fullPath.split('/');
  75. fullPathArray.pop();
  76. var parentDirectory = fullPathArray.join('/');
  77. this.do('listDirectory', { path: parentDirectory, selectPath: path });
  78. } else {
  79. this.do('listDirectory', { path: '/' });
  80. }
  81. return path;
  82. }
  83. },
  84. confirmPath: { comment: 'I hold the path that is given to the callback on confirmation.' },
  85. copiedFilePath: {
  86. comment: 'I hold the copied file path to use when pasteToAvailableLocation is called.',
  87. transform: function(path) {
  88. if ( path ) {
  89. this.get('optionContainer').querySelector('.sg-filemanager-operation.paste').classList.add('active');
  90. this.set({ copiedFileName: this.get('selectedName') });
  91. this.set({ copiedFileType: this.get('selectedType') });
  92. } else {
  93. this.get('optionContainer').querySelector('.sg-filemanager-operation.paste').classList.remove('active');
  94. }
  95. return path;
  96. }
  97. },
  98. copiedFileName: { comment: 'I hold the copied file name to use when pasteToAvailableLocation is called.' },
  99. copiedFileType: { comment: 'I hold the copied file type (directory/file) to use when pasteToAvailableLocation is called.' },
  100. selectedName: { comment: 'I hold the current file / directory name. Empty string if nothing is selected.' },
  101. selectedType: { comment: 'I hold the type (file or directory) of the selection.' },
  102. isFileSelected: {
  103. comment: 'Wether a File or Directory is selected.',
  104. transform: function(aBoolean){
  105. this.get('previewContainer').classList.remove('active');
  106. this.get('previewContainer').innerHTML = '';
  107. if (aBoolean) {
  108. var selectedFile = this.get('directoryListing').querySelector('[data-path="'+ this.get('basePath') +'/'+ this.get('selectedName') +'"]');
  109. this.get('optionContainer').querySelector('.delete').classList.add('active');
  110. this.get('optionContainer').querySelector('.rename').classList.add('active');
  111. this.get('optionContainer').querySelector('.copy').classList.add('active');
  112. if ( this.get('selectedType') == 'file' ) {
  113. if ( this.get('hasOKandCancelButton') && this.get('context') == 'chooseFile' ) {
  114. this.get('content').querySelector('button.confirm').classList.add('active');
  115. this.set({ confirmPath: this.get('basePath') +'/'+ this.get('selectedName') });
  116. }
  117. if ( selectedFile && selectedFile.classList.contains('image') ) {
  118. var preview = document.createElement('img');
  119. preview.setAttribute('src', selectedFile.getAttribute('data-path'));
  120. this.get('previewContainer').classList.add('active');
  121. this.get('previewContainer').appendChild(preview);
  122. }
  123. } else {
  124. if ( this.get('hasOKandCancelButton') && this.get('context') == 'chooseFile' ) {
  125. this.get('content').querySelector('button.confirm').classList.remove('active');
  126. }
  127. }
  128. } else {
  129. this.get('optionContainer').querySelector('.delete').classList.remove('active');
  130. this.get('optionContainer').querySelector('.rename').classList.remove('active');
  131. this.get('optionContainer').querySelector('.copy').classList.remove('active');
  132. var selectedFiles = this.get('directoryListing').querySelectorAll('.active');
  133. for (var i = 0; i < selectedFiles.length; i++) {
  134. selectedFiles[i].classList.remove('active');
  135. }
  136. if ( this.get('selectedType') == 'file' ) {
  137. if ( this.get('hasOKandCancelButton') && this.get('context') == 'chooseFile' ) {
  138. this.get('content').querySelector('button.confirm').classList.remove('active');
  139. }
  140. }
  141. }
  142. return aBoolean;
  143. }
  144. },
  145. isWorking: {
  146. comment: 'Wether a request is currently done.',
  147. transform: function(aBoolean){
  148. if ( aBoolean ) {
  149. // working
  150. if ( !this.get('content').querySelector('.sg-filemanager-working') ) {
  151. var workingIndicator = document.createElement('div');
  152. workingIndicator.setAttribute('class', 'sg-filemanager-working');
  153. this.get('content').appendChild(workingIndicator);
  154. }
  155. if ( !this.get('content').querySelector('.sg-filemanager-blocked') ) {
  156. var blockingIndicator = document.createElement('div');
  157. blockingIndicator.setAttribute('class', 'sg-filemanager-blocked');
  158. this.get('content').querySelector('.sg-filemanager-directory-container').appendChild(blockingIndicator);
  159. }
  160. var activeButtons = this.get('content').querySelectorAll('.sg-filemanager-operation.active');
  161. for (var i = 0; i<activeButtons.length; i++) {
  162. activeButtons[i].classList.remove('active');
  163. }
  164. if ( this.get('hasOKandCancelButton') && this.get('context') != 'saveAs' && this.get('context') != 'newPage' ) {
  165. this.get('content').querySelector('button.confirm').classList.remove('active');
  166. }
  167. } else {
  168. // finished
  169. if ( this.get('content').querySelector('.sg-filemanager-working') ) {
  170. this.get('content').querySelector('.sg-filemanager-working').remove();
  171. }
  172. if ( this.get('content').querySelector('.sg-filemanager-blocked') ) {
  173. this.get('content').querySelector('.sg-filemanager-blocked').remove();
  174. }
  175. this.get('optionContainer').querySelector('.file-upload').classList.add('active');
  176. this.get('optionContainer').querySelector('.directory-new').classList.add('active');
  177. }
  178. return aBoolean;
  179. }
  180. },
  181. hasOKandCancelButton: {
  182. comment: 'Wether I have a OK and Cancel button.',
  183. transform: function(aBoolean){
  184. if (aBoolean) {
  185. if ( !this.get('content').querySelector('.sg-editing-filemanager-modal-container') ) {
  186. var self = this;
  187. var modalButtonContainer = document.createElement('div');
  188. modalButtonContainer.classList.add('sg-editing-filemanager-modal-container');
  189. var modalButtonConfirm = document.createElement('button');
  190. modalButtonConfirm.classList.add('confirm');
  191. modalButtonConfirm.addEventListener('click', function() {
  192. if ( this.classList.contains('active') ) {
  193. if ( self.get('context') == 'newPage' ) {
  194. self.do('createNewPageAtAvailableLocation', { name: self.get('content').querySelector('.sg-modal-name-input').value });
  195. //SuperGlue.get('windowManager').do('closeWindow', self);
  196. } else {
  197. // Send Callback Path and Close
  198. // TODO: Close only if not opened before
  199. self.get('callback').call({ path: self.get('confirmPath') });
  200. SuperGlue.get('windowManager').do('closeWindow', self);
  201. }
  202. }
  203. });
  204. var modalButtonCancel = document.createElement('button');
  205. modalButtonCancel.classList.add('cancel', 'active');
  206. modalButtonCancel.addEventListener('click', function() {
  207. // Close
  208. // TODO: Close only if not opened before
  209. SuperGlue.get('windowManager').do('closeWindow', self);
  210. });
  211. modalButtonContainer.appendChild(modalButtonConfirm);
  212. modalButtonContainer.appendChild(modalButtonCancel);
  213. this.get('content').appendChild(modalButtonContainer);
  214. this.get('content').parentNode.querySelector('.sg-editing-window-closebutton').style.display = 'none';
  215. }
  216. } else {
  217. if ( this.get('content').querySelector('.sg-editing-filemanager-modal-container') ) {
  218. this.get('content').querySelector('.sg-editing-filemanager-modal-container').remove();
  219. this.get('content').parentNode.querySelector('.sg-editing-window-closebutton').style.display = 'block';
  220. }
  221. }
  222. return aBoolean;
  223. }
  224. },
  225. optionContainer: { comment: 'I hold a reference to the file operations panel.' },
  226. directoryListing: { comment: 'I hold a reference to the directory container.' },
  227. directoryControlContainer: { comment: 'I hold a reference to the directory controls.' },
  228. previewContainer: { comment: 'I hold a reference to the image preview container.' }
  229. },
  230. methods: {
  231. init: {
  232. comment: 'I start a new FileManagerWindow. My argument is '+
  233. '{ top: anInt, left: anInt, width: anInt, height: anInt }.',
  234. code: function(startConfig){
  235. var self = this;
  236. SuperGlue.get('fileManager').set({ activeFileManagerWindow: self });
  237. self.set({ onClose: function() {
  238. SuperGlue.get('fileManager').set({ activeFileManagerWindow: null })
  239. } });
  240. self.delegate('Window', 'init', startConfig);
  241. // Directory Listing
  242. var directoryContainerElement = document.createElement('div');
  243. directoryContainerElement.setAttribute('class', 'sg-filemanager-directory-container');
  244. var directoryListingElement = document.createElement('ul');
  245. directoryListingElement.setAttribute('class', 'sg-filemanager-directory-listing');
  246. directoryListingElement.addEventListener('mouseup', function(evt) {
  247. self.set({ isFileSelected: false });
  248. evt.stopPropagation();
  249. });
  250. self.set({ directoryListing: directoryListingElement });
  251. directoryContainerElement.appendChild(directoryListingElement);
  252. self.get('content').appendChild(directoryContainerElement);
  253. // Directory Controls
  254. var directoryControls = document.createElement('div');
  255. directoryControls.setAttribute('class', 'sg-filemanager-directory-controls');
  256. self.get('content').appendChild(directoryControls);
  257. self.set({ directoryControlContainer: directoryControls });
  258. // Go UP one directory button
  259. var optionDirectoryUp = document.createElement('div');
  260. optionDirectoryUp.classList.add('sg-filemanager-operation', 'directory-up');
  261. optionDirectoryUp.addEventListener('click', function() {
  262. if ( self.get('currentPath') == '/' ) {
  263. return false;
  264. }
  265. var path = self.get('currentPath');
  266. var pathArray = path.split('/');
  267. pathArray.pop();
  268. var newPath;
  269. if (pathArray.length > 1 ) {
  270. newPath = pathArray.join('/');
  271. } else if (pathArray.length == 1) {
  272. newPath = '/';
  273. }
  274. self.do('listDirectory', { path: newPath });
  275. });
  276. directoryControls.appendChild(optionDirectoryUp);
  277. // Current path display
  278. var currentPathContainer = document.createElement('div');
  279. currentPathContainer.classList.add('sg-filemanager-current-path');
  280. directoryControls.appendChild(currentPathContainer);
  281. // File Operations
  282. var optionContainerElement = document.createElement('div');
  283. optionContainerElement.setAttribute('class', 'sg-filemanager-operations');
  284. self.set({ optionContainer: optionContainerElement });
  285. self.get('content').appendChild(self.get('optionContainer'));
  286. // Upload file button
  287. var optionUploadFile = document.createElement('div');
  288. optionUploadFile.classList.add('sg-filemanager-operation', 'file-upload', 'active');
  289. optionUploadFile.addEventListener('click', function(evt) {
  290. self.set({ isFileSelected: false });
  291. var fileInput = document.createElement('input');
  292. fileInput.setAttribute('type', 'file');
  293. fileInput.style.visibility = 'hidden';
  294. self.get('optionContainer').appendChild(fileInput);
  295. fileInput.addEventListener('change', function(){
  296. self.do('uploadWithAvailableFilename', { name: fileInput.files[0].name, data: fileInput.files[0] });
  297. });
  298. fileInput.click();
  299. });
  300. // Add new directory button (in current directory)
  301. var optionNewDirectory = document.createElement('div');
  302. optionNewDirectory.classList.add('sg-filemanager-operation', 'directory-new', 'active');
  303. optionNewDirectory.addEventListener('click', function() {
  304. self.get('directoryListing').scrollTop = 0;
  305. var selectedFiles = self.get('directoryListing').querySelectorAll('.active');
  306. for (var i = 0; i < selectedFiles.length; i++) {
  307. selectedFiles[i].classList.remove('active');
  308. }
  309. var directoryNameInputContainer = document.createElement('li');
  310. directoryNameInputContainer.classList.add('sg-resource-directory', 'new');
  311. var alreadyFired;
  312. var directoryNameInput = document.createElement('input');
  313. directoryNameInput.setAttribute('type', 'text');
  314. directoryNameInput.setAttribute('value', 'NewFolder');
  315. directoryNameInput.addEventListener('blur', function(evt) {
  316. setDirectoryName(evt);
  317. alreadyFired = true;
  318. });
  319. directoryNameInput.addEventListener('keydown', function(evt) {
  320. if ( evt.keyCode == 13 ) {
  321. setDirectoryName(evt);
  322. alreadyFired = true;
  323. }
  324. });
  325. directoryNameInputContainer.appendChild(directoryNameInput);
  326. self.get('directoryListing').insertBefore(directoryNameInputContainer, self.get('directoryListing').firstChild);
  327. directoryNameInput.focus();
  328. directoryNameInput.select();
  329. var setDirectoryName = function(evt) {
  330. if (evt.target.value != '' && !alreadyFired) {
  331. self.set({ isWorking: true });
  332. var path;
  333. if (self.get('currentPath') == '/' ) {
  334. path = '';
  335. } else {
  336. path = self.get('currentPath');
  337. }
  338. self.do('createAvailableDirectory', { name: evt.target.value });
  339. }
  340. }
  341. });
  342. // Rename button (file or folder)
  343. var optionRename = document.createElement('div');
  344. optionRename.classList.add('sg-filemanager-operation', 'rename');
  345. optionRename.addEventListener('click', function(evt) {
  346. if ( !this.classList.contains('active') ) {
  347. return false;
  348. }
  349. // TODO: Scroll to resource position
  350. //self.get('directoryListing').scrollTop = 0;
  351. var selectedFiles = self.get('directoryListing').querySelectorAll('.active');
  352. for (var i = 0; i < selectedFiles.length; i++) {
  353. selectedFiles[i].classList.remove('active');
  354. }
  355. var oldName = self.get('selectedName');
  356. var selectedElement = self.get('directoryListing').querySelector( '[data-path="'+ self.get('basePath') +'/'+ oldName +'"]' );
  357. selectedElement.classList.add('edit');
  358. var alreadyFired;
  359. var renameInput = document.createElement('input');
  360. renameInput.setAttribute('type', 'text');
  361. renameInput.setAttribute('value', oldName);
  362. renameInput.addEventListener('blur', function(evt) {
  363. setNewName(evt);
  364. alreadyFired = true;
  365. });
  366. renameInput.addEventListener('keydown', function(evt) {
  367. if ( evt.keyCode == 13 ) {
  368. setNewName(evt);
  369. alreadyFired = true;
  370. }
  371. });
  372. selectedElement.appendChild(renameInput);
  373. renameInput.focus();
  374. renameInput.select();
  375. var setNewName = function(evt) {
  376. if (evt.target.value != '' && !alreadyFired) {
  377. self.set({ isWorking: true });
  378. self.do('renameToAvailableFilename', { name: evt.target.value, origin: self.get('basePath') +'/'+ oldName, type: self.get('selectedType') });
  379. }
  380. }
  381. });
  382. // Copy button (file or folder)
  383. var optionCopy = document.createElement('div');
  384. optionCopy.classList.add('sg-filemanager-operation', 'copy');
  385. optionCopy.addEventListener('click', function(evt) {
  386. if ( !this.classList.contains('active') ) {
  387. return false;
  388. }
  389. self.set({ copiedFilePath: self.get('basePath') +'/'+ self.get('selectedName') });
  390. });
  391. // Paste button (file or folder)
  392. var optionPaste = document.createElement('div');
  393. optionPaste.classList.add('sg-filemanager-operation', 'paste');
  394. optionPaste.addEventListener('click', function(evt) {
  395. if ( !this.classList.contains('active') || !self.get('copiedFilePath') ) {
  396. return false;
  397. }
  398. var selectedFiles = self.get('directoryListing').querySelectorAll('.active');
  399. for (var i = 0; i < selectedFiles.length; i++) {
  400. selectedFiles[i].classList.remove('active');
  401. }
  402. self.set({ isWorking: true });
  403. self.do('pasteToAvailableLocation', { originPath: self.get('copiedFilePath') });
  404. });
  405. // Delete button (file or folder)
  406. var optionDelete = document.createElement('div');
  407. optionDelete.classList.add('sg-filemanager-operation', 'delete');
  408. optionDelete.addEventListener('click', function(evt) {
  409. if ( !this.classList.contains('active') ) {
  410. return false;
  411. }
  412. var fullPath = self.get('basePath') +'/'+ self.get('selectedName');
  413. if ( self.get('selectedType') == 'file' ) {
  414. var deleteFileConfirmation = confirm('Are you sure you want to delete the file:\n\n ' + fullPath + ' ?\n\nThis action is irreversible!');
  415. if ( deleteFileConfirmation == true ) {
  416. self.set({ isWorking: true });
  417. SuperGlue.get('server').do('removeFile', {
  418. path: fullPath,
  419. onerror: function() {
  420. alert('File could not be removed.\nError Message:\n\n' + arguments);
  421. },
  422. onsuccess: function() {
  423. self.set({ isWorking: false });
  424. self.do('listDirectory', { path: self.get('currentPath') });
  425. }
  426. });
  427. }
  428. } else {
  429. var deleteDirectoryConfirmation = confirm('Are you sure you want to delete the folder:\n\n ' + fullPath + ' \n\nand all of its contents?\nThis action is irreversible!');
  430. if ( deleteDirectoryConfirmation == true ) {
  431. self.set({ isWorking: true });
  432. SuperGlue.get('server').do('removeDirectory', {
  433. path: fullPath,
  434. onerror: function() {
  435. alert('Directory could not be removed.\nError Message:\n\n' + arguments);
  436. },
  437. onsuccess: function() {
  438. self.set({ isWorking: false });
  439. self.do('listDirectory', { path: self.get('currentPath') });
  440. }
  441. });
  442. }
  443. }
  444. });
  445. optionContainerElement.appendChild(optionUploadFile);
  446. optionContainerElement.appendChild(optionNewDirectory);
  447. optionContainerElement.appendChild(optionRename);
  448. optionContainerElement.appendChild(optionCopy);
  449. optionContainerElement.appendChild(optionPaste);
  450. optionContainerElement.appendChild(optionDelete);
  451. var previewContainer = document.createElement('div');
  452. previewContainer.classList.add('sg-filemanager-preview');
  453. self.get('content').appendChild(previewContainer);
  454. self.set({ previewContainer: previewContainer });
  455. self.set({ hasOKandCancelButton: startConfig.hasOKandCancelButton });
  456. if ( startConfig.context == 'saveAs' ) {
  457. self.set({ originalFileName: startConfig.originalFileName });
  458. }
  459. if ( startConfig.callback ) {
  460. self.set({ callback: startConfig.callback });
  461. }
  462. if ( startConfig.selectPath ) {
  463. self.set({ selectPath: startConfig.selectPath });
  464. } else {
  465. self.do('listDirectory', { path: '/' });
  466. }
  467. self.set({ context: startConfig.context });
  468. }
  469. },
  470. listDirectory: {
  471. comment: 'I list a directory by a given path.',
  472. code: function(arg) {
  473. var self = this;
  474. self.set({ isWorking: true });
  475. SuperGlue.get('server').do('directoryListing', {
  476. path: arg.path,
  477. onerror: function() {
  478. alert('Directory Listing failed.\nError Message:\n\n' + this);
  479. },
  480. onsuccess: function() {
  481. self.get('directoryListing').innerHTML = '';
  482. var result = this;
  483. /*
  484. result.sort(function(a, b){
  485. var keyA = a.isDirectory,
  486. keyB = b.isDirectory;
  487. if(keyA < keyB) return 1;
  488. if(keyA > keyB) return -1;
  489. return 0;
  490. });
  491. */
  492. var directories = [],
  493. files = [];
  494. for (var r=0; r < result.length; r++) {
  495. if ( result[r].isDirectory ) {
  496. directories.push(result[r]);
  497. } else {
  498. files.push(result[r]);
  499. }
  500. }
  501. for (var d=0; d < directories.length; d++) {
  502. var pathArray = directories[d].name.split('/');
  503. var name = pathArray[pathArray.length-1];
  504. var type = 'directory';
  505. var resultElem = document.createElement('li');
  506. resultElem.setAttribute('class', 'sg-resource-'+ type);
  507. resultElem.setAttribute('data-path', directories[d].name);
  508. resultElem.setAttribute('data-name', name);
  509. resultElem.setAttribute('data-type', type);
  510. resultElem.innerHTML = name;
  511. resultElem.addEventListener('click', function(evt) {
  512. var selectedFiles = self.get('directoryListing').querySelectorAll('.active');
  513. for (var i = 0; i < selectedFiles.length; i++) {
  514. selectedFiles[i].classList.remove('active');
  515. }
  516. self.get('previewContainer').classList.remove('active');
  517. self.get('previewContainer').innerHTML = '';
  518. this.classList.add('active');
  519. self.set({ selectedName: this.getAttribute('data-name') });
  520. self.set({ selectedType: this.getAttribute('data-type') });
  521. self.set({ isFileSelected: true });
  522. });
  523. resultElem.addEventListener('dblclick', function(evt) {
  524. self.do('listDirectory', { path: evt.target.getAttribute('data-path') });
  525. });
  526. self.get('directoryListing').appendChild(resultElem);
  527. }
  528. function bytesToSize(bytes) {
  529. if(bytes == 0) return '0 Byte';
  530. var k = 1000;
  531. var sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  532. var i = Math.floor(Math.log(bytes) / Math.log(k));
  533. return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
  534. }
  535. for (var f=0; f < files.length; f++) {
  536. var pathArray = files[f].name.split('/');
  537. var name = pathArray[pathArray.length-1];
  538. var type = 'file';
  539. var size = bytesToSize(files[f].size);
  540. var fileType;
  541. if ( (/\.(gif|jpg|jpeg|tiff|png)$/i).test(files[f].name) ) {
  542. fileType = 'image';
  543. } else if ( (/\.(html)$/i).test(files[f].name) ) {
  544. fileType = 'html';
  545. } else {
  546. fileType = undefined;
  547. }
  548. var resultElem = document.createElement('li');
  549. resultElem.classList.add('sg-resource-'+ type);
  550. if (fileType) {
  551. resultElem.classList.add(fileType);
  552. }
  553. resultElem.setAttribute('data-path', files[f].name);
  554. resultElem.setAttribute('data-name', name);
  555. resultElem.setAttribute('data-type', type);
  556. if ( fileType == 'html' ) {
  557. resultElem.innerHTML = name + '<a href="'+ files[f].name +'" target="_blank"></a><span class="size">'+ size +'</span>';
  558. } else {
  559. resultElem.innerHTML = name + '<span class="size">'+ size +'</span>';
  560. }
  561. resultElem.addEventListener('click', function(evt) {
  562. var selectedFiles = self.get('directoryListing').querySelectorAll('.active');
  563. for (var i = 0; i < selectedFiles.length; i++) {
  564. selectedFiles[i].classList.remove('active');
  565. }
  566. this.classList.add('active');
  567. self.set({ selectedName: this.getAttribute('data-name') });
  568. self.set({ selectedType: this.getAttribute('data-type') });
  569. self.set({ isFileSelected: true });
  570. });
  571. self.get('directoryListing').appendChild(resultElem);
  572. }
  573. if ( arg.path == '/' ) {
  574. self.get('directoryControlContainer').querySelector('.directory-up').classList.remove('active');
  575. self.get('optionContainer').querySelector('.delete').classList.remove('active');
  576. } else {
  577. self.get('directoryControlContainer').querySelector('.directory-up').classList.add('active');
  578. }
  579. self.set({ currentPath: arg.path });
  580. if ( self.get('context') == 'saveAs' && self.get('content').querySelector('.sg-modal-name-input') ) {
  581. self.set({ confirmPath: self.get('basePath') +'/'+ self.get('content').querySelector('.sg-modal-name-input').value });
  582. }
  583. if ( arg.selectPath && self.get('directoryListing').querySelector('[data-path="'+ arg.selectPath +'"]') ) {
  584. var selectedFiles = self.get('directoryListing').querySelectorAll('.active');
  585. for (var i = 0; i < selectedFiles.length; i++) {
  586. selectedFiles[i].classList.remove('active');
  587. }
  588. self.get('directoryListing').querySelector('[data-path="'+ arg.selectPath +'"]').classList.add('active');
  589. self.get('directoryListing').querySelector('[data-path="'+ arg.selectPath +'"]').scrollIntoView(false);
  590. self.set({ selectedType: self.get('directoryListing').querySelector('[data-path="'+ arg.selectPath +'"]').getAttribute('data-type') });
  591. self.set({ selectedName: self.get('directoryListing').querySelector('[data-path="'+ arg.selectPath +'"]').getAttribute('data-name') });
  592. self.set({ isFileSelected: true });
  593. } else {
  594. self.set({ isFileSelected: false });
  595. }
  596. self.set({ isWorking: false });
  597. if ( self.get('copiedFilePath') ) {
  598. self.get('optionContainer').querySelector('.paste').classList.add('active');
  599. }
  600. }
  601. });
  602. }
  603. },
  604. checkName: {
  605. comment: 'I check if the name is valid and if not return a valid name. Params: name',
  606. code: function(arg) {
  607. // Replace all illegal characters with '-'
  608. var cleanString = arg.name.replace(/ |\\|%|"|'|<|>|\/|\.\.|\$|&|\{|\}|\[|\]|#|\?|,/gi, '-');
  609. // Replace Umlauts
  610. cleanString.replace(/ä/gi, 'ae').replace(/ö/gi, 'oe').replace(/ü/gi, 'ue');
  611. return cleanString;
  612. }
  613. },
  614. createAvailableDirectory: {
  615. comment: 'I create a directrory with a valid and available name. Params: name, (increment)',
  616. code: function(arg) {
  617. var self = this;
  618. var increment;
  619. if (!arg.increment) {
  620. increment = 1;
  621. } else {
  622. increment = parseInt(arg.increment)+1;
  623. }
  624. var cleanedName = self.do('checkName', { name: arg.name });
  625. var destination;
  626. if (increment == 1) {
  627. destination = self.get('basePath') +'/'+ cleanedName;
  628. } else {
  629. destination = self.get('basePath') +'/'+ cleanedName + '-' + increment;
  630. }
  631. SuperGlue.get('server').do('doesDirectoryExist', {
  632. path: destination,
  633. onerror: function() {
  634. alert('Checking if directory exists threw an error.\nError Message:\n\n' + this);
  635. },
  636. onsuccess: function(aBoolean) {
  637. if (aBoolean) {
  638. var newIncrement = increment++;
  639. self.do('createAvailableDirectory', { name: cleanedName, increment: newIncrement });
  640. } else {
  641. var destination;
  642. if (increment == 1) {
  643. destination = self.get('basePath') +'/'+ cleanedName;
  644. } else {
  645. destination = self.get('basePath') +'/'+ cleanedName + '-' + increment;
  646. }
  647. SuperGlue.get('server').do('makeDirectory', {
  648. path: destination,
  649. onerror: function() {
  650. alert('Directory could not be created.\nError Message:\n\n' + this);
  651. },
  652. onsuccess: function() {
  653. self.set({ isWorking: false });
  654. self.do('listDirectory', { path: self.get('currentPath'), selectPath: destination });
  655. }
  656. });
  657. }
  658. }
  659. });
  660. }
  661. },
  662. uploadWithAvailableFilename: {
  663. comment: 'I upload a file to an available location. Params: name, data, (increment)',
  664. code: function(arg) {
  665. var self = this;
  666. self.set({ isWorking: true });
  667. var increment;
  668. if (!arg.increment) {
  669. increment = 1;
  670. } else {
  671. increment = parseInt(arg.increment)+1;
  672. }
  673. var cleanedName = self.do('checkName', { name: arg.name });
  674. var destination;
  675. if (increment == 1) {
  676. destination = self.get('basePath') +'/'+ cleanedName;
  677. } else {
  678. destination = self.get('basePath') + '/' + cleanedName.substr(0, (cleanedName.lastIndexOf('.')) || cleanedName) + '-' + increment + cleanedName.substring(cleanedName.lastIndexOf('.'))
  679. }
  680. SuperGlue.get('server').do('doesFileExist', {
  681. path: destination,
  682. onerror: function() {
  683. alert('Checking if file exists threw an error.\nError Message:\n\n' + this);
  684. },
  685. onsuccess: function(aBoolean) {
  686. if (aBoolean) {
  687. var newIncrement = increment++;
  688. self.do('uploadWithAvailableFilename', { name: cleanedName, data: arg.data, increment: newIncrement });
  689. } else {
  690. var destination;
  691. if (increment == 1) {
  692. destination = self.get('basePath') +'/'+ cleanedName;
  693. } else {
  694. destination = self.get('basePath') +'/'+ cleanedName.substr(0, (cleanedName.lastIndexOf('.')) || cleanedName) + '-' + increment + cleanedName.substring(cleanedName.lastIndexOf('.'));
  695. }
  696. var progressBar = document.createElement('div');
  697. progressBar.setAttribute('class', 'sg-filemanager-progress-bar');
  698. var uploadProgress = document.createElement('div');
  699. uploadProgress.setAttribute('class', 'sg-filemanager-progress');
  700. uploadProgress.style.opacity = 1;
  701. progressBar.appendChild(uploadProgress);
  702. self.get('content').appendChild(progressBar);
  703. var file = arg.data;
  704. var uploadForm = new FormData();
  705. uploadForm.append('userimage', file);
  706. SuperGlue.get('server').do('upload', {
  707. data: uploadForm,
  708. path: destination,
  709. onerror: function() {
  710. alert('Upload failed.\nError Message:\n\n' + this);
  711. },
  712. onprogress: function(evt){
  713. if(evt.lengthComputable){
  714. var percentComplete = evt.loaded / evt.total * 100;
  715. uploadProgress.style.width = percentComplete + '%';
  716. } else {
  717. uploadProgress.style.width = 40 + '%';
  718. }
  719. },
  720. onresponse: function(){
  721. self.get('optionContainer').querySelector('input[type="file"]').remove();
  722. self.do('listDirectory', { path: self.get('currentPath'), selectPath: destination });
  723. uploadProgress.style.width = 100 + '%';
  724. setTimeout(function() {
  725. uploadProgress.style.opacity = 0;
  726. self.set({ isWorking: false });
  727. setTimeout(function() {
  728. self.get('content').querySelector('.sg-filemanager-progress-bar').remove();
  729. }, 500);
  730. }, 1200);
  731. }
  732. });
  733. }
  734. }
  735. });
  736. }
  737. },
  738. renameToAvailableFilename: {
  739. comment: 'I rename a file to an available and valid name. Params: name, origin, type, (increment)',
  740. code: function(arg) {
  741. var self = this;
  742. self.set({ isWorking: true });
  743. var increment;
  744. if (!arg.increment) {
  745. increment = 1;
  746. } else {
  747. increment = parseInt(arg.increment)+1;
  748. }
  749. var cleanedName = self.do('checkName', { name: arg.name });
  750. var destination;
  751. if (increment == 1) {
  752. destination = self.get('basePath') +'/'+ cleanedName;
  753. } else {
  754. if ( arg.type == 'directory' ) {
  755. destination = self.get('basePath') +'/'+ cleanedName + '-' + increment;
  756. } else {
  757. destination = self.get('basePath') +'/'+ cleanedName.substr(0, (cleanedName.lastIndexOf('.')) || cleanedName) + '-' + increment + cleanedName.substring(cleanedName.lastIndexOf('.'));
  758. }
  759. }
  760. var checkFunctionName;
  761. if (arg.type == 'directory') {
  762. checkFunctionName = 'doesDirectoryExist';
  763. } else {
  764. checkFunctionName = 'doesFileExist';
  765. }
  766. SuperGlue.get('server').do(checkFunctionName, {
  767. path: destination,
  768. onerror: function() {
  769. alert('Checking if path already exists threw an error.\nError Message:\n\n' + this);
  770. },
  771. onsuccess: function(aBoolean) {
  772. if (aBoolean /*&& checkPath +'/'+ cleanedName != arg.origin*/) {
  773. var newIncrement = increment++;
  774. self.do('renameToAvailableFilename', { name: cleanedName, origin: arg.origin, type: arg.type, increment: newIncrement });
  775. } else {
  776. var destination;
  777. if (increment == 1) {
  778. destination = self.get('basePath') +'/'+ cleanedName;
  779. } else {
  780. if ( arg.type == 'directory' ) {
  781. destination = self.get('basePath') +'/'+ cleanedName + '-' + increment;
  782. } else {
  783. destination = self.get('basePath') +'/'+ cleanedName.substr(0, (cleanedName.lastIndexOf('.')) || cleanedName) + '-' + increment + cleanedName.substring(cleanedName.lastIndexOf('.'));
  784. }
  785. }
  786. SuperGlue.get('server').do('moveFile', {
  787. sourcePath: arg.origin,
  788. targetPath: destination,
  789. onerror: function() {
  790. alert('File could not be copied to new location.\nError Message:\n\n' + this);
  791. },
  792. onprogress: function(evt){
  793. //
  794. },
  795. onsuccess: function(){
  796. self.do('listDirectory', { path: self.get('currentPath'), selectPath: destination });
  797. self.set({ isWorking: false });
  798. }
  799. });
  800. }
  801. }
  802. });
  803. }
  804. },
  805. pasteToAvailableLocation: {
  806. comment: 'I paste a file to an available and valid location in the current directory. Params: originPath, (increment)',
  807. code: function(arg) {
  808. var self = this;
  809. self.set({ isWorking: true });
  810. var increment;
  811. if (!arg.increment) {
  812. increment = 1;
  813. } else {
  814. increment = parseInt(arg.increment)+1;
  815. }
  816. var cleanedName = self.get('copiedFileName');
  817. var destination;
  818. if (increment == 1) {
  819. destination = self.get('basePath') +'/'+ cleanedName;
  820. } else {
  821. if ( arg.type == 'directory' ) {
  822. destination = self.get('basePath') +'/'+ cleanedName + '-' + increment;
  823. } else {
  824. destination = self.get('basePath') +'/'+ cleanedName.substr(0, (cleanedName.lastIndexOf('.')) || cleanedName) + '-' + increment + cleanedName.substring(cleanedName.lastIndexOf('.'));
  825. }
  826. }
  827. var checkFunctionName;
  828. if (self.get('copiedFileType') == 'directory') {
  829. checkFunctionName = 'doesDirectoryExist';
  830. } else {
  831. checkFunctionName = 'doesFileExist';
  832. }
  833. SuperGlue.get('server').do(checkFunctionName, {
  834. path: destination,
  835. onerror: function() {
  836. alert('Checking if path already exists threw an error.\nError Message:\n\n' + this);
  837. },
  838. onsuccess: function(aBoolean) {
  839. if (aBoolean /*&& checkPath +'/'+ cleanedName != arg.origin*/) {
  840. var newIncrement = increment++;
  841. self.do('pasteToAvailableLocation', { originPath: arg.originPath, increment: newIncrement });
  842. } else {
  843. var destination;
  844. if (increment == 1) {
  845. destination = self.get('basePath') +'/'+ cleanedName;
  846. } else {
  847. if ( arg.type == 'directory' ) {
  848. destination = self.get('basePath') +'/'+ cleanedName + '-' + increment;
  849. } else {
  850. destination = self.get('basePath') +'/'+ cleanedName.substr(0, (cleanedName.lastIndexOf('.')) || cleanedName) + '-' + increment + cleanedName.substring(cleanedName.lastIndexOf('.'));
  851. }
  852. }
  853. var copyFunctionName;
  854. if (self.get('copiedFileType') == 'directory') {
  855. copyFunctionName = 'copyDirectory';
  856. } else {
  857. copyFunctionName = 'copyFile';
  858. }
  859. SuperGlue.get('server').do(copyFunctionName, {
  860. sourcePath: arg.originPath,
  861. targetPath: destination,
  862. onerror: function() {
  863. alert('File / Directory could not be copied.\nError Message:\n\n' + this);
  864. },
  865. onprogress: function(evt){
  866. //
  867. },
  868. onsuccess: function(){
  869. self.do('listDirectory', { path: self.get('currentPath'), selectPath: destination });
  870. self.set({ isWorking: false });
  871. self.set({ copiedFilePath: undefined });
  872. }
  873. });
  874. }
  875. }
  876. });
  877. }
  878. },
  879. createNewPageAtAvailableLocation: {
  880. comment: 'I create a new file at an available and valid location in the current directory. Params: name, (increment)',
  881. code: function(arg) {
  882. var self = this;
  883. self.set({ isWorking: true });
  884. var increment;
  885. if (!arg.increment) {
  886. increment = 1;
  887. } else {
  888. increment = parseInt(arg.increment)+1;
  889. }
  890. var cleanedName = self.do('checkName', { name: arg.name });
  891. var destination;
  892. if (increment == 1) {
  893. destination = self.get('basePath') +'/'+ cleanedName;
  894. } else {
  895. destination = self.get('basePath') +'/'+ cleanedName.substr(0, (cleanedName.lastIndexOf('.')) || cleanedName) + '-' + increment + cleanedName.substring(cleanedName.lastIndexOf('.'));
  896. }
  897. SuperGlue.get('server').do('doesFileExist', {
  898. path: destination,
  899. onerror: function() {
  900. alert('The file does already exist.');
  901. console.log(this)
  902. },
  903. onsuccess: function(aBoolean) {
  904. if (aBoolean) {
  905. var newIncrement = increment++;
  906. self.do('createNewPageAtAvailableLocation', { name: arg.name, increment: newIncrement });
  907. } else {
  908. var destination;
  909. if (increment == 1) {
  910. destination = self.get('basePath') +'/'+ cleanedName;
  911. } else {
  912. destination = self.get('basePath') +'/'+ cleanedName.substr(0, (cleanedName.lastIndexOf('.')) || cleanedName) + '-' + increment + cleanedName.substring(cleanedName.lastIndexOf('.'));
  913. }
  914. SuperGlue.get('server').do('newPage', {
  915. newPath: destination,
  916. onerror: function() {
  917. alert('There was a critical error.\nSee console for more details');
  918. console.log(this);
  919. },
  920. onprogress: function(evt){
  921. //
  922. },
  923. onsuccess: function(){
  924. self.do('listDirectory', { path: self.get('currentPath'), selectPath: destination });
  925. self.set({ isWorking: false });
  926. self.set({ context: undefined });
  927. self.set({ hasOKandCancelButton: false });
  928. }
  929. });
  930. }
  931. }
  932. });
  933. }
  934. }
  935. }
  936. }});