From 15a9cf5616bb229308152f7c3dc6ee26d1e7629b Mon Sep 17 00:00:00 2001 From: Chuck Brazie Date: Tue, 14 Feb 2017 14:30:08 -0500 Subject: [PATCH] Add in new files.js Change-Id: I1e81519fe006f9f78d327dd03be054d52dd3f599 --- xCAT-UI/js/configure/files.js | 333 ++++++++++++++++++++++++++++++++++ 1 file changed, 333 insertions(+) create mode 100644 xCAT-UI/js/configure/files.js diff --git a/xCAT-UI/js/configure/files.js b/xCAT-UI/js/configure/files.js new file mode 100644 index 000000000..a75eed4a6 --- /dev/null +++ b/xCAT-UI/js/configure/files.js @@ -0,0 +1,333 @@ +/** + * Load the files page + */ +function loadFilesPage() { + var tabId = 'filesTab'; + $('#' + tabId).empty(); + + // Set padding for page + $('#' + tabId).css('padding', '10px 30px'); + + // Create info bar + var info = $('#' + tabId).find('.ui-state-highlight'); + // If there is no info bar + if (!info.length) { + var infoBar = createInfoBar('Below is a listing of the xCAT repository. ' + + 'Upload any file or package into the repository using the Upload button. ' + + 'Go into any subdirectories by specifying the directory path and clicking on Go.'); + + var directoryFS = $('
'); + var dirLegend = $('Directory'); + directoryFS.append(dirLegend); + + // Division to hold directory actions + var actions = $('
'); + directoryFS.append(actions); + + // Create button to create a directory + var folderBtn = createButton('New folder'); + folderBtn.click(function() { + var deleteFolderBtn = $(''); + var createFolderBtn = createButton('Create'); + + // Create a new directory + var newFolder = $('
  • '); + newFolder.prepend(deleteFolderBtn); + newFolder.append(createFolderBtn); + $('#repo_content ul').append(newFolder); + + // Delete new folder on-click + deleteFolderBtn.click(function() { + $(this).parents('li').remove(); + }); + + // Create folder on-click + createFolderBtn.click(function() { + var directory = $('#' + tabId + ' input[name="repo_directory"]'); + var newFolderPath = $('#' + tabId + ' input[name="new_folder"]').val(); + if (newFolderPath) { + $.ajax({ + url : 'lib/cmd.php', + dataType : 'json', + data : { + cmd : 'webrun', + tgt : '', + args : 'createfolder;' + directory.val() + '/' + newFolderPath, + msg : '' + }, + + success:function(data) { + openDialog('info', data.rsp[0]); + } + }); + + $(this).parents('li').remove(); + } else { + openDialog('warn', 'You must specify the folder name'); + } + }); + }); + + // Create button to upload files + var uploadBtn = createButton('Upload'); + uploadBtn.click(function() { + var directory = $('#' + tabId + ' input[name="repo_directory"]'); + openUploadDialog(directory.val()); + }); + + // Create button to go into a directory path + var dirPath = $(''); + var goBtn = createButton('Go'); + goBtn.click(function() { + var directory = $('#' + tabId + ' input[name="repo_directory"]'); + loadPath(directory.val()); + }); + goBtn.attr('id', 'go_to_path'); + + var space = $('
    '); + var content = $('
    '); + actions.append(folderBtn, uploadBtn, dirPath, goBtn); + directoryFS.append(space, content); + + $('#' + tabId).append(infoBar, directoryFS); + } + + // Retrieve repository space + getRepositorySpace(); + + // Retrieve files from /install + loadPath('/install'); +} + +/** + * Get the repository space + */ +function getRepositorySpace() { + // Grab repository space + $.ajax({ + url : 'lib/cmd.php', + dataType : 'json', + async: false, + data : { + cmd : 'webrun', + tgt : '', + args : 'getrepospace', + msg : '' + }, + success: function(data) { + $('#repo_space').children().remove(); + + // Data returned is: size, used, available, used %, mount + // Data could be in a different format in CMO, where it puts the directory on the line + // "rsp":["\/data\/xcat\/install 28G 6.0G 20G 24% \/install"],"msg":null} + var space = data.rsp[0].split(' '); + if (space.length == 6) { + space.splice(0,1); + } + var spaceLabel = $(''); + $('#repo_space').append(spaceLabel); + } + }); +} + +/** + * Open a dialog to upload files into the repository + * + * @param destDirectory The destination directory + */ +function openUploadDialog(destDirectory) { + // Create info bar + var info = createInfoBar('Select a file to upload onto ' + destDirectory + '.'); + var dialog = $('
    '); + dialog.append(info); + + // Upload file + var upload = $('
    '); + var label = $(''); + var file = $(''); + var subBtn = createButton('Upload'); + upload.append(label, file, subBtn); + dialog.append(upload); + + upload.submit(function() { + // Create status bar, hide on load + var statBarId = 'uploadStatusBar'; + var statBar = createStatusBar(statBarId); + var loader = createLoader(''); + statBar.find('div').append('Do not close this dialog while the file is being uploaded '); + statBar.find('div').append(loader); + statBar.prependTo($('#upload_file_dg')); + + var data = new FormData($('#upload_file')[0]); + $.ajax({ + type: 'POST', + url : 'lib/uploadfile.php?destination=' + destDirectory, + data: data, + success: function(data) { + $('#uploadStatusBar').find('img').hide(); + $('#uploadStatusBar').find('div').empty(); + $('#uploadStatusBar').find('div').append(data); + + // Refresh directory contents + $('#go_to_path').click(); + getRepositorySpace(); + }, + cache: false, + contentType: false, + processData: false + }); + + return false; + }); + + // Create dialog + dialog.dialog({ + modal: true, + title: 'Upload', + width: 500, + close: function() {$(this).remove();} + }); +} + +/** + * Load the directory path structure + * + * @path The directory path + */ +function loadPath(path) { + // Limit access to only /install + if (path.substring(0, 9).indexOf("install") == -1) { + openDialog('warn', 'You are not authorized to browse outside the repository'); + return; + } + + var tabId = 'filesTab'; + var directory = $('#' + tabId + ' input[name="repo_directory"]'); + directory.val(path); + + // Un-ordered list containing directories and files + var contentId = 'repo_content'; + $('#' + contentId).empty(); + var itemsList = $(''); + $('#' + contentId).append(itemsList); + + // Back button to go up a directory + var item = $('
  • ..
  • '); + itemsList.append(item); + item.dblclick(function() { + if (path.lastIndexOf('/') > 1) + path = path.substring(0, path.lastIndexOf('/')); + loadPath(path); + }); + + $.ajax({ + type: 'POST', + url : 'lib/getpath.php', + dataType : 'json', + data: { + action: 'browse', + path: path, + time: new Date().getTime() + }, + beforeSend: function() { + // Show loading image + }, + success: function(files) { + $.each(files, function(index, file) { + if (!file.path || file.path.indexOf("undefined")) + file.path = ""; + + var fullPath = file.path + "/" + file.name; + + // Create a list showing the directories and files + var item; + if (file.isFolder) { + var deleteFolderBtn = $(''); + + item = $('
  • ' + file.name + '
  • '); + item.prepend(deleteFolderBtn); + itemsList.append(item); + item.dblclick(function() { + loadPath(directory.val() + fullPath); + }); + + // Delete file on click + deleteFolderBtn.click(function() { + deleteFile($(this).parents('li'), directory.val() + fullPath); + }); + } else { + var icon = $(''); + var deleteFileBtn = $(''); + + item = $('
  • ' + file.name + '
  • '); + item.append(deleteFileBtn, icon); + + // Delete file on click + deleteFileBtn.click(function() { + deleteFile($(this).parents('li'), directory.val() + fullPath); + }); + + itemsList.append(item); + } + }); + } + }); +} + +/** + * Prompt user to confirm deletion of file + * + * @param container The element container + * @param file The file name to delete + */ +function deleteFile(container, file) { + // Open dialog to confirm + var confirmDialog = $('
    '); + var warn = createWarnBar('Are you sure you want to delete ' + file + '?'); + confirmDialog.append(warn); + confirmDialog.dialog({ + title: "Confirm", + modal: true, + width: 400, + close: function() {$(this).remove();}, + buttons: { + "Ok": function() { + var loader = createLoader('').css({'margin': '5px'}); + $(this).append(loader); + + // Change dialog buttons + $(this).dialog('option', 'buttons', { + 'Close':function() { + $(this).dialog('destroy').remove(); + } + }); + + $.ajax({ + url : 'lib/cmd.php', + dataType : 'json', + async: false, + data : { + cmd : 'webrun', + tgt : '', + args : 'deletefile;' + file, + msg : '' + }, + success: function(data) { + $('#confirm_delete').children().remove(); + var info = createInfoBar(data.rsp[0]); + $('#confirm_delete').append(info); + getRepositorySpace(); + } + }); + + // Delete folder from the list + container.remove(); + }, + "Cancel": function() { + $(this).dialog('destroy').remove(); + } + } + }); +} \ No newline at end of file