Skip to content
This repository has been archived by the owner on Jan 7, 2020. It is now read-only.

Commit

Permalink
Merge pull request #201 from bryan-gilbert/EPIC-1136-DocFilName
Browse files Browse the repository at this point in the history
EPIC-1136 Document filename
  • Loading branch information
marklise authored Jul 18, 2017
2 parents d365eb7 + 7b1e9fd commit 5974d6d
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 1 deletion.
35 changes: 35 additions & 0 deletions modules/core/client/fn-filename/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Filename Validator

Provides a filename validation along with preserving the original file's extension

## Usage

Add `fn-filename` attribute to a text input. Provide the file extension in `fn-extension`. Optionally, provide the ID of a DOM element to display error messages.
The `fn-filename` directive requires the input has a ng-model.

Sample HTML
```
<form name="editFileForm" novalidate>
...
<div class="form-group file-type" x-show-errors>
<label for="documentFileName" class="control-label">File Name</label>
<span id="fileNameError" ng-show="editFileForm.documentFileName.$invalid" class="help-block"></span>
<input class="form-control" id="documentFileName" name="documentFileName" type="text" ng-model="documentFileName"
fn-filename fn-extension="{{extension}}" fn-error="fileNameError">
</div>
...
</form>
```

Sample controller
```
// ng-model
$scope.documentFileName = documentFileName;
// fn-extension
$scope.extension = documentFileName.split('.').pop();
```





77 changes: 77 additions & 0 deletions modules/core/client/fn-filename/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict';

(function() {
var NOT_ALLOWED = 'These characters are not allowed: /?<>:*|":';
var NO_CONTROL = 'Control characters are not allowed';
var NO_PERIOD_OR_SPACE = "Names shouldn't begin with a period or spaces";
var RESERVED = 'Can not use reserved file names like COM0, PRN, etc';
var NO_ALL_PERIOD = 'Names can not be just periods';
var REQUIRED = 'Required';

angular.module('core')
// fn-filename
.directive('fnFilename', function () {
return {
require: 'ngModel',
restrict: 'A',
link: function (scope, element, attributes, ngModel) {
var extension = attributes.fnExtension; // fn-extension
var $error;
if (attributes.fnError) { // fn-error
var errorId = "#" + attributes.fnError;
$error = angular.element(document.querySelector(errorId));
}
element.on('focus', function () {
if (extension) {
var m = element.val();
m = m.substring(0, m.indexOf(extension) - 1);
element.val(m);
}
});
element.on('blur', function () {
if (extension) {
var m = element.val() + '.' + extension;
// set both the display and the model values
element.val(m);
ngModel.$setViewValue( m );
}
});
ngModel.$validators.filename = function (modelValue, viewValue) {
var input = modelValue;
var errMsg = '';
if (extension && input === '.'+ extension) {
errMsg = REQUIRED;
} else if (input) {
errMsg = validateFilename(input, extension);
}
if ($error) {
$error.text(errMsg);
}
return errMsg === '';
};
}
};

function validateFilename(input, extension) {
var illegalRe = /[\/\?<>\\:\*\|":]/g; // basic illegal filename characters
var controlRe = /[\x00-\x1f\x80-\x9f]/g; // no control characters
var windowsReservedRe = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i;
var leadingRe = /^[\.\s]+/; // no names beginning with . or whitespace
var reservedRe = /^\.+$/; // no names like ".", ".."
var result = '';
if (input.match(illegalRe)) {
result = NOT_ALLOWED;
} else if (input.match(controlRe)) {
result = NO_CONTROL;
} else if (input.match(leadingRe)) {
result = NO_PERIOD_OR_SPACE;
} else if (!extension && input.match(windowsReservedRe)) {
result = RESERVED;
} else if (input.match(reservedRe)) {
result = NO_ALL_PERIOD;
}
return result;
}
})
;
})();
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ angular.module('documents')

$scope.originalName = obj.displayName || obj.documentFileName || obj.internalOriginalName;
$scope.doc = obj;
// Only set the filename extension attribute (fn-extension) if there is an extension.
var parts = $scope.doc.documentFileName.split('.');
$scope.extension = parts.length > 1 ? parts.pop() : undefined;
// any dates going to the datepicker need to be javascript Date objects...
$scope.doc.documentDate = _.isEmpty(obj.documentDate) ? null : moment(obj.documentDate).toDate();
$scope.datePicker = {
Expand Down
8 changes: 7 additions & 1 deletion modules/documents/client/views/document-manager-edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ <h3 ng-if="doc._schemaName === 'Folder'" class="modal-title">Edit Folder &nbsp;
<label for="displayName" class="control-label">Display Name<em class="required">(Required)</em></label>
<input class="form-control" id="displayName" name="displayName" type="text" ng-model="doc.displayName" required ng-blur="validate()">
</div>

<div ng-if="doc._schemaName === 'Document'" class="form-group file-type" x-show-errors>
<label for="documentFileName" class="control-label">File Name
<span class="text-danger " id="fileNameExtError" ng-show="editFileForm.documentFileName.$invalid"></span>
</label>
<input class="form-control" id="documentFileName" name="documentFileName" type="text" ng-model="doc.documentFileName"
fn-filename fn-extension="{{extension}}" fn-error="fileNameExtError">
</div>
<div ng-if="doc._schemaName === 'Document'" class="form-group" x-show-errors>
<label class="control-label" for="documentType">Document Type</label>
<select class="form-control" title="Document Type" id="documentType" name="documentType"
Expand Down
4 changes: 4 additions & 0 deletions modules/documents/client/views/document-manager.html
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,10 @@ <h3>Properties</h3>
<span class="name">Follow up:</span>
<span class="value">{{ documentMgr.infoPanel.data.inspectionReport.followup }}</span>
</li>
<li ng-if="authentication.user">
<span class="name">File Name:</span>
<span class="value">{{ documentMgr.infoPanel.data.documentFileName }}</span>
</li>
</ul>
</section>

Expand Down

0 comments on commit 5974d6d

Please sign in to comment.