create directive for generating aliases - still needs server side validation

This commit is contained in:
Mads Rasmussen
2015-06-22 09:27:42 +02:00
parent 5e3d32c88b
commit 4aa572629f
2 changed files with 65 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
angular.module("umbraco.directives")
.directive('umbGenerateAlias', function ($timeout) {
return {
restrict: 'E',
templateUrl: 'views/components/umb-generate-alias.html',
replace: true,
scope: {
alias: '=',
aliasFrom: '=',
enableLock: '=?'
},
link: function (scope, element, attrs, ctrl) {
var unbindWatcher = function(){};
scope.locked = true;
function init() {
if(scope.alias === undefined || scope.alias === "" || scope.alias === null) {
unbindWatcher = scope.$watch('aliasFrom', function (newValue, oldValue) {
if(newValue !== undefined && newValue !== null) {
generateAlias(newValue);
}
});
}
}
function generateAlias(value) {
var str = value;
// capitalize all words
str = str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
// remove spaces
str = str.replace(/\s/g, '');
scope.alias = str;
}
scope.$watch('locked', function(newValue, oldValue){
if(newValue === false) {
unbindWatcher();
}
});
init();
}
};
});

View File

@@ -0,0 +1,4 @@
<div>
<span ng-if="!enableLock">{{ alias }}</span>
<umb-locked-field ng-if="enableLock" locked="$parent.locked" ng-model="$parent.alias"></umb-locked-field>
</div>