UNPKG

ng-content-editable

Version:

Use ngModel and validations with contenteditable HTML5 attribute

1 lines 1.86 kB
!function(){"use strict";angular.module("content-editable",[]).directive("contenteditable",["$timeout",function(u){return{restrict:"A",require:["^?ngModel","^?form"],link:function(n,r,t,e){var l=e[0];if(null===l)return null;var i=function(){if(void 0===t.ngModel)return null;var e=t.ngModel.split(".");return e[e.length-1]}(),o={onlyText:!1,convertNewLines:!1,noLf:!1,onlyNum:!1,noTrim:!1};angular.forEach(["onlyText","convertNewLines","noLf","onlyNum","noTrim"],function(e){t.hasOwnProperty(e)&&t[e]&&"false"!==t[e]&&(o[e]=!0)}),u(function(){return o.onlyText&&o.noLf||o.onlyNum?r.text(l.$modelValue):r.html(l.$modelValue)});var a=function(){var e,n="";o.onlyText&&o.noLf||o.onlyNum?n=r.text():(n=r.html())&&(n=function(e){if(e=e.replace(/&nbsp;/g," "),o.convertNewLines||o.noLf){var n="\r\n",r=/\r\n$/;o.noLf&&(n=" ",r=/ $/),e=(e=(e=(e=(e=(e=e.replace(/<br(\s*)\/*>/gi,n)).replace(/<[div>]+>/gi,n)).replace(/<\/[div>]+>/gm,"")).replace(/<[p>]+>/gi,n)).replace(/<\/[p>]+>/gm,"")).replace(r,"")}o.onlyText&&(e=e.replace(/<\S[^><]*>/g,""));return e}(n)),!1===o.noTrim&&""!==n&&(n=(n=n.replace(/&nbsp;/g," ")).trim()),l.$setViewValue(n),(e=n.length)>t.ngMaxlength||e<t.ngMinlength?(l.$setValidity(i,!1),r.addClass("-error")):r.hasClass("-error")&&(l.$setValidity(i,!0),r.removeClass("-error"))};l.$render=function(){o.onlyText&&o.noLf||o.onlyNum?r.text(l.$viewValue||""):r.html(l.$viewValue||"")},r.bind("blur keyup change",function(e){n.$apply(a),"blur"===e.type&&n.$apply(l.$render)}),r.bind("keydown",function(e){var n=[8,37,38,39,40,46];if(o.onlyNum&&-1===n.indexOf(e.which)&&188!==e.which&&190!==e.which&&!(48<=e.which&&e.which<=57||96<=e.which&&e.which<=105))return e.preventDefault(),!1;if(o.noLf){if(13===e.which)return e.preventDefault(),!1;if(t.ngMaxlength&&r.text().length>=t.ngMaxlength&&-1===n.indexOf(e.which))return e.preventDefault(),!1}})}}}])}();