commonsApp.directive('ensureUrl', [ 'commonsAppServices', function(commonsAppServices) { return { require : 'ngModel', link : function(scope, ele, attrs, c) { scope.$watch(attrs.ngModel, function() { var result = commonsAppServices .validateUrl(commonsAppServices.refForDotNotation(scope,attrs.ngModel)); if (result == true) { c.$setValidity('isUrl', true); } else { c.$setValidity('isUrl', false); } }); } }; } ]); commonsApp.directive('autofocusIf', [ '$timeout', function($timeout) { return function(scope, element, attrs) { scope.$watch(attrs.autofocusIf, function(newValue) { if (newValue == true) { $timeout(function() { element.focus(); }); } }, true); }; } ]); commonsApp.directive('contenteditable',function() { return { require: 'ngModel', scope : true, link: function(scope, elm, attrs, ctrl) { var oldValue= ""; max = 17; min = 1; // view -> model elm.bind('blur', function() { check_mincount(min, event); scope.$apply(function() { var addToArray=true; for(var i=0;i view ctrl.render = function(value) { elm.html(value); }; // load init value from DOM ctrl.$setViewValue(elm.html()); elm.bind('focus', function(event) { oldValue = elm.html(); }); elm.bind('focusout', function(event) { check_mincount(min, event); }); elm.bind('keydown', function(event) { // console.log("keydown " + event.which); var esc = event.which == 27, el = event.target; check_maxcount(max, event); if (esc) { console.log("esc"); ctrl.$setViewValue(elm.html()); el.blur(); event.preventDefault(); } }); elm.bind('keyup', function(event){ check_maxcount(max, event);}); function check_maxcount(max, e) { if(e.which != 8 && e.target.textContent.length > max) { scope.addGroup.groupName.$error.maxlength = true; el = e.target; ctrl.$setViewValue(elm.html()); el.blur(); e.preventDefault(); } else { scope.addGroup.groupName.$error.maxlength = false; } } function check_mincount(min, e) { if(e.target.textContent.length < min) { el = e.target; elm.html(oldValue); el.blur(); e.preventDefault(); } } } }; }); commonsApp.directive('owlerCdnSrc',['commonsAppServices',function(commonsAppServices){ return { restrict : 'A', link : function(scope,element,attrs){ attrs.$observe('owlerCdnSrc', function(value) { if (!value) return; attrs.$set('src', commonsAppServices.getCDNImageUrl(value)); }); } }; }])