This repository has been archived by the owner on Jan 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #201 from bryan-gilbert/EPIC-1136-DocFilName
EPIC-1136 Document filename
- Loading branch information
Showing
5 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
``` | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}) | ||
; | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters