Skip to content

Instantly share code, notes, and snippets.

@rlogiacco
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlogiacco/ed3a407b1e97a0eb7941 to your computer and use it in GitHub Desktop.
Save rlogiacco/ed3a407b1e97a0eb7941 to your computer and use it in GitHub Desktop.
AngularJS UI Utils
angular.module("ui-utils.focus", ['ng'])
.directive('focusMe', function($log) {
return {
link: function(scope, element, attrs) {
scope.$watch(attrs.focusMe, function(value) {
if(value === true) {
$log.debug("value=" + element[0].value);
element[0].focus();
scope[attrs.focusMe] = false;
}
});
}
};
});
angular.module('ui-utils.truncate', ['ng'])
.filter('chars', function () {
return function (input, chars, breakOnWord) {
if (isNaN(chars)) return input;
if (chars <= 0) return '';
if (input && input.length > chars) {
input = input.substring(0, chars);
if (!breakOnWord) {
var lastspace = input.lastIndexOf(' ');
//get last space
if (lastspace !== -1) {
input = input.substr(0, lastspace);
}
} else {
while(input.charAt(input.length-1) === ' '){
input = input.substr(0, input.length -1);
}
}
return input + '...';
}
return input;
};
})
.filter('words', function () {
return function (input, words) {
if (isNaN(words)) return input;
if (words <= 0) return '';
if (input) {
var inputWords = input.split(/\s+/);
if (inputWords.length > words) {
input = inputWords.slice(0, words).join(' ') + '...';
}
}
return input;
};
});
angular.module("ui-utils", ["ui-utils.focus","ui-utils.truncate"]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment