Skip to content

Commit

Permalink
#394 first spike of checking for existing files when creating a new file
Browse files Browse the repository at this point in the history
  • Loading branch information
jstrachan committed Jul 18, 2013
1 parent a5454a4 commit 4cc4119
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 40 deletions.
13 changes: 10 additions & 3 deletions hawtio-web/src/main/webapp/app/wiki/html/viewPage.html
Expand Up @@ -87,6 +87,12 @@
activateNodes="createDocumentTreeActivations"></div>
</div>

<div class="control-group">
<div class="well">
{{selectedCreateDocumentTemplate.tooltip}}
</div>
</div>

<form class="form-inline" ng-submit="addAndCloseDialog()">
<label class="control-label" for="fileName">Name: </label>

Expand All @@ -95,14 +101,15 @@
</form>

<div class="control-group">
<div class="well">
{{selectedCreateDocumentTemplate.tooltip}}
<div ng-show="fileExists.exists" class="alert">
Please choose a different name as <b>{{fileExists.name}}</b> already exists
</div>
</div>

</div>
<div class="modal-footer">
<input id="submit" class="btn btn-primary add" type="submit" ng-click="addAndCloseDialog()"
ng-disabled="!selectedCreateDocumentTemplate.exemplar"
ng-disabled="!selectedCreateDocumentTemplate.exemplar || fileExists.exists"
value="Create">
<button class="btn btn-warning cancel" type="button" ng-click="addDialog.close()">Cancel</button>
</div>
Expand Down
25 changes: 25 additions & 0 deletions hawtio-web/src/main/webapp/app/wiki/js/helpers.ts
Expand Up @@ -170,6 +170,31 @@ module Wiki {
return answer;
}

/**
* Returns the file name of the given path; stripping off any directories
*/
export function fileName(path: string) {
if (path) {
var idx = path.lastIndexOf("/");
if (idx > 0) {
return path.substring(idx + 1);
}
}
return path;
}

/**
* Returns the folder of the given path (everything but the last path name)
*/
export function fileParent(path: string) {
if (path) {
var idx = path.lastIndexOf("/");
if (idx > 0) {
return path.substring(0, idx);
}
}
return path;
}

export function fileIconHtml(row) {
var css = null;
Expand Down
120 changes: 83 additions & 37 deletions hawtio-web/src/main/webapp/app/wiki/js/view.ts
Expand Up @@ -18,6 +18,10 @@ module Wiki {
$scope.isFile = false;
$scope.createDocumentTree = Wiki.createWizardTree();
$scope.createDocumentTreeActivations = ["camel-spring.xml", "ReadMe.md"];
$scope.fileExists = {
exists: false,
name: ""
};

$scope.gridOptions = {
data: 'children',
Expand Down Expand Up @@ -80,7 +84,7 @@ module Wiki {

$scope.fileIconHtml = (entity) => {
return Wiki.fileIconHtml(entity);
}
};


$scope.format = Wiki.fileFormat($scope.pageId, fileExtensionTypeRegistry);
Expand Down Expand Up @@ -130,54 +134,32 @@ module Wiki {

$scope.onCreateDocumentSelect = (node) => {
$scope.selectedCreateDocumentTemplate = node ? node.entity : null;
checkFileExists(getNewDocumentPath());
};

$scope.$watch("newDocumentName", () => {
checkFileExists(getNewDocumentPath());
});

$scope.openAddDialog = () => {
$scope.newDocumentName = null;
$scope.addDialog.open();
};

$scope.addAndCloseDialog = () => {
var template = $scope.selectedCreateDocumentTemplate;
if (!template) {
console.log("No template selected");
var path = getNewDocumentPath();
if (!template || !path) {
return;
}
var name = Wiki.fileName(path);
var fileName = name;
var folder = Wiki.fileParent(path);
var exemplar = template.exemplar;
var name = $scope.newDocumentName || exemplar;

if (name.indexOf('.') < 0) {
// lets add the file extension from the exemplar
var idx = exemplar.lastIndexOf(".");
if (idx > 0) {
name += exemplar.substring(idx);
}
}

var commitMessage = "Created " + template.label;
var exemplarUri = url("/app/wiki/exemplar/" + exemplar);

// TODO detect if we are a folder or not!

// lets deal with directories in the name
var folder = $scope.pageId;
if ($scope.isFile) {
// if we are a file lets discard the last part of the path
var idx = folder.lastIndexOf("/");
if (idx <= 0) {
folder = "";
} else {
folder = folder.substring(0, idx);
}
}
var fileName = name;
var idx = name.lastIndexOf("/");
if (idx > 0) {
folder += "/" + name.substring(0, idx);
name = name.substring(idx + 1);
}
var path = folder + "/" + name;

if (template.folder) {
notification("success", "Creating new folder " + name);

Expand Down Expand Up @@ -255,6 +237,16 @@ module Wiki {
$scope.deleteDialog = false;
};

$scope.$watch("fileName", () => {
// ignore errors if the file is the same as the rename file!
var path = getRenameFilePath();
if ($scope.originalRenameFilePath === path) {
$scope.fileExists = { exsits: false, name: null };
} else {
checkFileExists(path);
}
});

$scope.openRenameDialog = () => {
var name = null;
if ($scope.gridOptions.selectedItems.length) {
Expand All @@ -263,9 +255,10 @@ module Wiki {
}
if (name) {
$scope.fileName = name;
$scope.originalRenameFilePath = getRenameFilePath();
$scope.renameDialog.open();
$timeout(() => {
$('#renameFileName').focus();
$('#renameFileName').focus();
}, 50);
} else {
console.log("No items selected right now! " + $scope.gridOptions.selectedItems);
Expand All @@ -275,11 +268,11 @@ module Wiki {
$scope.renameAndCloseDialog = () => {
if ($scope.gridOptions.selectedItems.length) {
var selected = $scope.gridOptions.selectedItems[0];
var newName = $scope.fileName;
var newPath = getRenameFilePath();
if (selected && newName) {
var oldName = selected.name;
var newName = Wiki.fileName(newPath);
var oldPath = $scope.pageId + "/" + oldName;
var newPath = $scope.pageId + "/" + newName;
console.log("About to rename file " + oldPath + " to " + newPath);
$scope.git = wikiRepository.rename($scope.branch, oldPath, newPath, null, (result) => {
notification("success", "Renamed file to " + newName);
Expand All @@ -297,7 +290,7 @@ module Wiki {
$scope.moveFolder = $scope.pageId;
$scope.moveDialog.open();
$timeout(() => {
$('#moveFolder').focus();
$('#moveFolder').focus();
}, 50);
} else {
console.log("No items selected right now! " + $scope.gridOptions.selectedItems);
Expand Down Expand Up @@ -431,5 +424,58 @@ module Wiki {
}
Core.$apply($scope);
}

function checkFileExists(path) {
if (path) {
//console.log("Checking if the file " + path + " exists");
wikiRepository.exists($scope.branch, path, (result) => {
console.log("for path " + path + " got result " + result);
$scope.fileExists.exists = result ? true : false;
$scope.fileExists.name = result ? result.name : null;
Core.$apply($scope);
});
}
}

function getNewDocumentPath() {
var template = $scope.selectedCreateDocumentTemplate;
if (!template) {
console.log("No template selected");
return null;
}
var exemplar = template.exemplar;
var name = $scope.newDocumentName || exemplar;

if (name.indexOf('.') < 0) {
// lets add the file extension from the exemplar
var idx = exemplar.lastIndexOf(".");
if (idx > 0) {
name += exemplar.substring(idx);
}
}

// lets deal with directories in the name
var folder = $scope.pageId;
if ($scope.isFile) {
// if we are a file lets discard the last part of the path
var idx = folder.lastIndexOf("/");
if (idx <= 0) {
folder = "";
} else {
folder = folder.substring(0, idx);
}
}
var fileName = name;
var idx = name.lastIndexOf("/");
if (idx > 0) {
folder += "/" + name.substring(0, idx);
name = name.substring(idx + 1);
}
return folder + "/" + name;
}

function getRenameFilePath() {
return ($scope.pageId && $scope.fileName) ? $scope.pageId + "/" + $scope.fileName : null;
}
}
}
1 change: 1 addition & 0 deletions hawtio-web/src/test/specs/SpecRunner.html
Expand Up @@ -106,6 +106,7 @@
<script type="text/javascript" src="spec/CoreSpec.js"></script>
<script type="text/javascript" src="spec/LogsSpec.js"></script>
<script type="text/javascript" src="spec/OSGiSpec.js"></script>
<script type="text/javascript" src="spec/WikiSpec.js"></script>

<script type="text/javascript">
(function() {
Expand Down
37 changes: 37 additions & 0 deletions hawtio-web/src/test/specs/spec/WikiSpec.js
@@ -0,0 +1,37 @@
describe("Wiki", function () {

var fileNameParentData = [
{
path: "foo.xml",
expectedName: "foo.xml",
expectedPath: ""
},
{
path: "/foo.xml",
expectedName: "foo.xml",
expectedPath: "/"
},
{
path: "/foo/foo.xml",
expectedName: "foo.xml",
expectedPath: "/foo"
},
{
path: "/foo/bar/foo.xml",
expectedName: "foo.xml",
expectedPath: "/foo/bar"
}
];

angular.forEach(fileNameParentData, function (sample) {
var path = sample.path;
var expectedName = sample.expectedName;
var expectedParent = sample.expectedParent;

it("file name and parent on path " + path), function () {
expect(expectedName).toEqual(Wiki.fileName(path));
expect(expectedParent).toEqual(Wiki.fileParent(path));
};
});

});

0 comments on commit 4cc4119

Please sign in to comment.