forked from mfrye/textAngular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
textAngular.min.js
1 lines (1 loc) · 20.4 KB
/
textAngular.min.js
1
!function(){"Use Strict";function a(a,b){if(!a||""===a||c.hasOwnProperty(a))throw"textAngular Error: A unique name is required for a Tool Definition";if(b.display&&(""===b.display||0===angular.element(b.display).length)||!b.display&&!b.buttontext&&!b.iconclass)throw'textAngular Error: Tool Definition for "'+a+'" does not have a valid display/iconclass/buttontext value';c[a]=b}var b=angular.module("textAngular",["ngSanitize"]);b.value("taOptions",{toolbar:[["h1","h2","h3","h4","h5","h6","p","pre","quote"],["bold","italics","underline","ul","ol","redo","undo","clear"],["justifyLeft","justifyCenter","justifyRight"],["html","insertImage","insertLink","unlink"]],classes:{focussed:"focussed",toolbar:"btn-toolbar",toolbarGroup:"btn-group",toolbarButton:"btn btn-default",toolbarButtonActive:"active",disabled:"disabled",textEditor:"form-control",htmlEditor:"form-control"},setup:{textEditorSetup:function(){},htmlEditorSetup:function(){}}});var c={};b.constant("taRegisterTool",a),b.value("taTools",c),b.config(["taRegisterTool",function(a){angular.forEach(c,function(a,b){delete c[b]}),a("html",{buttontext:"Toggle HTML",action:function(){this.$editor().switchView()},activeState:function(){return this.$editor().showHtml}});var b=function(a){return function(){return this.$editor().queryFormatBlockState(a)}},d=function(){return this.$editor().wrapSelection("formatBlock","<"+this.name.toUpperCase()+">")};angular.forEach(["h1","h2","h3","h4","h5","h6"],function(c){a(c.toLowerCase(),{buttontext:c.toUpperCase(),action:d,activeState:b(c.toLowerCase())})}),a("p",{buttontext:"P",action:function(){return this.$editor().wrapSelection("formatBlock","<P>")},activeState:function(){return this.$editor().queryFormatBlockState("p")}}),a("pre",{buttontext:"pre",action:function(){return this.$editor().wrapSelection("formatBlock","<PRE>")},activeState:function(){return this.$editor().queryFormatBlockState("pre")}}),a("ul",{iconclass:"fa fa-list-ul",action:function(){return this.$editor().wrapSelection("insertUnorderedList",null)},activeState:function(){return document.queryCommandState("insertUnorderedList")}}),a("ol",{iconclass:"fa fa-list-ol",action:function(){return this.$editor().wrapSelection("insertOrderedList",null)},activeState:function(){return document.queryCommandState("insertOrderedList")}}),a("quote",{iconclass:"fa fa-quote-right",action:function(){return this.$editor().wrapSelection("formatBlock","<BLOCKQUOTE>")},activeState:function(){return this.$editor().queryFormatBlockState("blockquote")}}),a("undo",{iconclass:"fa fa-undo",action:function(){return this.$editor().wrapSelection("undo",null)}}),a("redo",{iconclass:"fa fa-repeat",action:function(){return this.$editor().wrapSelection("redo",null)}}),a("bold",{iconclass:"fa fa-bold",action:function(){return this.$editor().wrapSelection("bold",null)},activeState:function(){return document.queryCommandState("bold")},commandKeyCode:98}),a("justifyLeft",{iconclass:"fa fa-align-left",action:function(){return this.$editor().wrapSelection("justifyLeft",null)},activeState:function(a){var b=!1;return a&&(b="left"===a.css("text-align")||"left"===a.attr("align")||"right"!==a.css("text-align")&&"center"!==a.css("text-align")&&!document.queryCommandState("justifyRight")&&!document.queryCommandState("justifyCenter")),b=b||document.queryCommandState("justifyLeft")}}),a("justifyRight",{iconclass:"fa fa-align-right",action:function(){return this.$editor().wrapSelection("justifyRight",null)},activeState:function(a){var b=!1;return a&&(b="right"===a.css("text-align")),b=b||document.queryCommandState("justifyRight")}}),a("justifyCenter",{iconclass:"fa fa-align-center",action:function(){return this.$editor().wrapSelection("justifyCenter",null)},activeState:function(a){var b=!1;return a&&(b="center"===a.css("text-align")),b=b||document.queryCommandState("justifyCenter")}}),a("italics",{iconclass:"fa fa-italic",action:function(){return this.$editor().wrapSelection("italic",null)},activeState:function(){return document.queryCommandState("italic")},commandKeyCode:105}),a("underline",{iconclass:"fa fa-underline",action:function(){return this.$editor().wrapSelection("underline",null)},activeState:function(){return document.queryCommandState("underline")},commandKeyCode:117}),a("clear",{iconclass:"fa fa-ban",action:function(a,b){this.$editor().wrapSelection("removeFormat",null);var c=[];if(this.$window.rangy&&this.$window.rangy.getSelection&&1===(c=this.$window.rangy.getSelection().getAllRanges()).length){var d=angular.element(c[0].commonAncestorContainer),e=function(a){a=angular.element(a);var b=a;angular.forEach(a.children(),function(a){var c=angular.element("<p></p>");c.html(angular.element(a).html()),b.after(c),b=c}),a.remove()};angular.forEach(d.find("ul"),e),angular.forEach(d.find("ol"),e);var f=this.$editor(),g=function(a){a=angular.element(a),a[0]!==f.displayElements.text[0]&&a.removeAttr("class"),angular.forEach(a.children(),g)};angular.forEach(d,g),"li"!==d[0].tagName.toLowerCase()&&"ol"!==d[0].tagName.toLowerCase()&&"ul"!==d[0].tagName.toLowerCase()&&this.$editor().wrapSelection("formatBlock","<p>")}else this.$editor().wrapSelection("formatBlock","<p>");b()}}),a("insertImage",{iconclass:"fa fa-picture-o",action:function(){var a;return a=prompt("Please enter an image URL to insert","http://"),""!==a&&"http://"!==a?this.$editor().wrapSelection("insertImage",a):void 0}}),a("insertLink",{iconclass:"fa fa-link",action:function(){var a;return a=prompt("Please enter an URL to insert","http://"),""!==a&&"http://"!==a?this.$editor().wrapSelection("createLink",a):void 0},activeState:function(a){return a?"A"===a[0].tagName:!1}}),a("unlink",{iconclass:"fa fa-unlink",action:function(){return this.$editor().wrapSelection("unlink",null)},activeState:function(a){return a?"A"===a[0].tagName:!1}})}]),b.directive("textAngular",["$compile","$timeout","taOptions","taSanitize","textAngularManager","$window",function(a,b,c,d,e,f){return{require:"?ngModel",scope:{},restrict:"EA",link:function(d,g,h,i){var j,k,l,m,n,o,p,q,r=Math.floor(1e16*Math.random()),s=h.name?h.name:"textAngularEditor"+r;angular.extend(d,angular.copy(c),{wrapSelection:function(a,b){try{document.execCommand(a,!1,b)}catch(c){}d.displayElements.text[0].focus()},showHtml:!1}),h.taFocussedClass&&(d.classes.focussed=h.taFocussedClass),h.taTextEditorClass&&(d.classes.textEditor=h.taTextEditorClass),h.taHtmlEditorClass&&(d.classes.htmlEditor=h.taHtmlEditorClass),h.taTextEditorSetup&&(d.setup.textEditorSetup=d.$parent.$eval(h.taTextEditorSetup)),h.taHtmlEditorSetup&&(d.setup.htmlEditorSetup=d.$parent.$eval(h.taHtmlEditorSetup)),p=g[0].innerHTML,g[0].innerHTML="",d.displayElements={forminput:angular.element("<input type='hidden' tabindex='-1' style='display: none;'>"),html:angular.element("<textarea></textarea>"),text:angular.element("<div></div>")},d.setup.htmlEditorSetup(d.displayElements.html),d.setup.textEditorSetup(d.displayElements.text),d.displayElements.html.attr({id:"taHtmlElement","ng-show":"showHtml","ta-bind":"ta-bind","ng-model":"html"}),d.displayElements.text.attr({id:"taTextElement",contentEditable:"true","ng-hide":"showHtml","ta-bind":"ta-bind","ng-model":"html"}),g.append(d.displayElements.text),g.append(d.displayElements.html),d.displayElements.forminput.attr("name",s),g.append(d.displayElements.forminput),h.tabindex&&(g.removeAttr("tabindex"),d.displayElements.text.attr("tabindex",h.tabindex),d.displayElements.html.attr("tabindex",h.tabindex)),h.placeholder&&(d.displayElements.text.attr("placeholder",h.placeholder),d.displayElements.html.attr("placeholder",h.placeholder)),h.taDisabled&&(d.displayElements.text.attr("ta-readonly","disabled"),d.displayElements.html.attr("ta-readonly","disabled"),d.disabled=d.$parent.$eval(h.taDisabled),d.$parent.$watch(h.taDisabled,function(a){d.disabled=a,d.disabled?g.addClass(d.classes.disabled):g.removeClass(d.classes.disabled)})),a(d.displayElements.text)(d),a(d.displayElements.html)(d),g.addClass("ta-root"),d.displayElements.text.addClass("ta-text ta-editor "+d.classes.textEditor),d.displayElements.html.addClass("ta-html ta-editor "+d.classes.textEditor),d._actionRunning=!1;var t=!1;if(d.startAction=function(){return d._actionRunning=!0,f.rangy&&f.rangy.saveSelection?(t=f.rangy.saveSelection(),function(){t&&f.rangy.restoreSelection(t)}):void 0},d.endAction=function(){d._actionRunning=!1,t&&f.rangy.removeMarkers(t),t=!1,d.updateSelectedStyles(),d.showHtml||d.updateTaBindtaTextElement()},n=function(){g.addClass(d.classes.focussed),q.focus()},d.displayElements.html.on("focus",n),d.displayElements.text.on("focus",n),o=function(a){return d._actionRunning||document.activeElement===d.displayElements.html[0]||document.activeElement===d.displayElements.text[0]||(g.removeClass(d.classes.focussed),q.unfocus(),b(function(){g.triggerHandler("blur")},0)),a.preventDefault(),!1},d.displayElements.html.on("blur",o),d.displayElements.text.on("blur",o),d.queryFormatBlockState=function(a){return a.toLowerCase()===document.queryCommandValue("formatBlock").toLowerCase()},d.switchView=function(){d.showHtml=!d.showHtml,d.showHtml?b(function(){return d.displayElements.html[0].focus()},100):b(function(){return d.displayElements.text[0].focus()},100)},h.ngModel){var u=!0;i.$render=function(){if(u){u=!1;var a=d.$parent.$eval(h.ngModel);void 0!==a&&null!==a||!p||""===p||i.$setViewValue(p)}d.displayElements.forminput.val(i.$viewValue),document.activeElement!==d.displayElements.html[0]&&document.activeElement!==d.displayElements.text[0]&&(d.html=i.$viewValue||"")}}else d.displayElements.forminput.val(p),d.html=p;if(d.$watch("html",function(a,b){a!==b&&(h.ngModel&&i.$setViewValue(a),d.displayElements.forminput.val(a))}),h.taTargetToolbars)q=e.registerEditor(s,d,h.taTargetToolbars.split(","));else{var v=angular.element('<div text-angular-toolbar name="textAngularToolbar'+r+'">');h.taToolbar&&v.attr("ta-toolbar",h.taToolbar),h.taToolbarClass&&v.attr("ta-toolbar-class",h.taToolbarClass),h.taToolbarGroupClass&&v.attr("ta-toolbar-group-class",h.taToolbarGroupClass),h.taToolbarButtonClass&&v.attr("ta-toolbar-button-class",h.taToolbarButtonClass),h.taToolbarActiveButtonClass&&v.attr("ta-toolbar-active-button-class",h.taToolbarActiveButtonClass),h.taFocussedClass&&v.attr("ta-focussed-class",h.taFocussedClass),g.prepend(v),a(v)(d.$parent),q=e.registerEditor(s,d,["textAngularToolbar"+r])}d.$on("$destroy",function(){e.unregisterEditor(s)}),d._bUpdateSelectedStyles=!1,d.updateSelectedStyles=function(){var a;f.rangy&&f.rangy.getSelection&&1===(a=f.rangy.getSelection().getAllRanges()).length&&a[0].commonAncestorContainer.parentNode!==d.displayElements.text[0]?q.updateSelectedStyles(angular.element(a[0].commonAncestorContainer.parentNode)):q.updateSelectedStyles(),d._bUpdateSelectedStyles&&b(d.updateSelectedStyles,200)},j=function(){d._bUpdateSelectedStyles||(d._bUpdateSelectedStyles=!0,d.$apply(function(){d.updateSelectedStyles()}))},d.displayElements.html.on("keydown",j),d.displayElements.text.on("keydown",j),k=function(){d._bUpdateSelectedStyles=!1},d.displayElements.html.on("keyup",k),d.displayElements.text.on("keyup",k),l=function(a){d.$apply(function(){return q.sendKeyCommand(a)?(d._bUpdateSelectedStyles||d.updateSelectedStyles(),a.preventDefault(),!1):void 0})},d.displayElements.html.on("keypress",l),d.displayElements.text.on("keypress",l),m=function(){d._bUpdateSelectedStyles=!1,d.$apply(function(){d.updateSelectedStyles()})},d.displayElements.html.on("mouseup",m),d.displayElements.text.on("mouseup",m)}}}]).directive("taBind",["taSanitize","$timeout","taFixChrome",function(a,b,c){return{require:"ngModel",scope:{},link:function(d,e,f,g){var h=void 0!==e.attr("contenteditable")&&e.attr("contenteditable"),i=h||"textarea"===e[0].tagName.toLowerCase()||"input"===e[0].tagName.toLowerCase(),j=!1,k=function(){if(h)return e[0].innerHTML;if(i)return e.val();throw"textAngular Error: attempting to update non-editable taBind"};d.$parent["updateTaBind"+(f.id||"")]=function(){j||g.$setViewValue(k())},i&&(e.on("paste cut",function(){j||b(function(){g.$setViewValue(k())},0)}),h?(e.on("keyup",function(){j||g.$setViewValue(k())}),e.on("blur",function(){var a=k();""===a&&e.attr("placeholder")&&e.addClass("placeholder-text"),j||g.$setViewValue(k()),g.$render()}),e.attr("placeholder")&&(e.addClass("placeholder-text"),e.on("focus",function(){e.removeClass("placeholder-text"),g.$render()}))):e.on("change blur",function(){j||g.$setViewValue(k())}));var l=function(b){return g.$oldViewValue=a(c(b),g.$oldViewValue)};g.$parsers.push(l),g.$formatters.push(l),g.$render=function(){if(document.activeElement!==e[0]){var a=g.$viewValue||"";h?(e[0].innerHTML=""===a&&e.attr("placeholder")&&e.hasClass("placeholder-text")?e.attr("placeholder"):a,j||e.find("a").on("click",function(a){return a.preventDefault(),!1})):"textarea"!==e[0].tagName.toLowerCase()&&"input"!==e[0].tagName.toLowerCase()?e[0].innerHTML=a:e.val(a)}},f.taReadonly&&(j=d.$parent.$eval(f.taReadonly),j?(("textarea"===e[0].tagName.toLowerCase()||"input"===e[0].tagName.toLowerCase())&&e.attr("disabled","disabled"),void 0!==e.attr("contenteditable")&&e.attr("contenteditable")&&e.removeAttr("contenteditable")):"textarea"===e[0].tagName.toLowerCase()||"input"===e[0].tagName.toLowerCase()?e.removeAttr("disabled"):h&&e.attr("contenteditable","true"),d.$parent.$watch(f.taReadonly,function(a,b){b!==a&&(a?(("textarea"===e[0].tagName.toLowerCase()||"input"===e[0].tagName.toLowerCase())&&e.attr("disabled","disabled"),void 0!==e.attr("contenteditable")&&e.attr("contenteditable")&&e.removeAttr("contenteditable")):"textarea"===e[0].tagName.toLowerCase()||"input"===e[0].tagName.toLowerCase()?e.removeAttr("disabled"):h&&e.attr("contenteditable","true"),j=a)}))}}}]).factory("taFixChrome",function(){var a=function(a){for(var b=angular.element("<div>"+a+"</div>"),c=angular.element(b).find("span"),d=0;d<c.length;d++){var e=angular.element(c[d]);e.attr("style")&&e.attr("style").match(/line-height: 1.428571429;|color: inherit; line-height: 1.1;/i)&&(e.attr("style",e.attr("style").replace(/( |)font-family: inherit;|( |)line-height: 1.428571429;|( |)line-height:1.1;|( |)color: inherit;/gi,"")),e.attr("style")&&""!==e.attr("style")||(e.next().length>0&&"BR"===e.next()[0].tagName&&e.next().remove(),e.replaceWith(e[0].innerHTML)))}var f=b[0].innerHTML.replace(/style="[^"]*?(line-height: 1.428571429;|color: inherit; line-height: 1.1;)[^"]*"/gi,"");return f!==b[0].innerHTML&&(b[0].innerHTML=f),b[0].innerHTML};return a}).factory("taSanitize",["$sanitize",function(a){function b(a,c){var d=[],e=a.children();return e.length&&angular.forEach(e,function(a){d=d.concat(b(angular.element(a),c))}),a.attr(c)&&d.push(a),d}return function(c,d){var e=angular.element("<div>"+c+"</div>");angular.forEach(b(e,"align"),function(a){a.css("text-align",a.attr("align")),a.removeAttr("align")}),c=e[0].innerHTML;var f;try{f=a(c)}catch(g){f=d||""}return f}}]).directive("textAngularToolbar",["$compile","textAngularManager","taOptions","taTools","taToolExecuteAction","$window",function(a,b,c,d,e,f){return{scope:{name:"@"},restrict:"EA",link:function(g,h,i){if(!g.name||""===g.name)throw"textAngular Error: A toolbar requires a name";angular.extend(g,angular.copy(c)),i.taToolbar&&(g.toolbar=g.$parent.$eval(i.taToolbar)),i.taToolbarClass&&(g.classes.toolbar=i.taToolbarClass),i.taToolbarGroupClass&&(g.classes.toolbarGroup=i.taToolbarGroupClass),i.taToolbarButtonClass&&(g.classes.toolbarButton=i.taToolbarButtonClass),i.taToolbarActiveButtonClass&&(g.classes.toolbarButtonActive=i.taToolbarActiveButtonClass),i.taFocussedClass&&(g.classes.focussed=i.taFocussedClass),g.disabled=!0,g.focussed=!1,h[0].innerHTML="",h.addClass("ta-toolbar "+g.classes.toolbar),g.$watch("focussed",function(){g.focussed?h.addClass(g.classes.focussed):h.removeClass(g.classes.focussed)}),setupToolElement=function(b,c){var d;if(d=angular.element(b&&b.display?b.display:"<button type='button'>"),d.addClass(g.classes.toolbarButton),d.attr("name",c.name),d.attr("unselectable","on"),d.attr("ng-disabled","isDisabled()"),d.attr("tabindex","-1"),d.attr("ng-click","executeAction()"),d.attr("ng-class","displayActiveToolClass(active)"),d.on("mousedown",function(a){return a.preventDefault(),!1}),b&&!b.display&&!c._display&&(d[0].innerHTML="",b.buttontext&&(d[0].innerHTML=b.buttontext),b.iconclass)){var e=angular.element("<i>"),f=d[0].innerHTML;e.addClass(b.iconclass),d[0].innerHTML="",d.append(e),f&&""!==f&&d.append(" "+f)}return c._lastToolDefinition=angular.copy(b),a(d)(c)},g.tools={},g._parent={disabled:!0,showHtml:!1,queryFormatBlockState:function(){return!1}};var j={$window:f,$editor:function(){return g._parent},isDisabled:function(){return this.$eval("disabled")||this.$eval("disabled()")||"html"!==this.name&&this.$editor().showHtml||this.$parent.disabled||this.$editor().disabled},displayActiveToolClass:function(a){return a?g.classes.toolbarButtonActive:""},executeAction:e};angular.forEach(g.toolbar,function(a){groupElement=angular.element("<div>"),groupElement.addClass(g.classes.toolbarGroup),angular.forEach(a,function(a){g.tools[a]=angular.extend(g.$new(!0),d[a],j,{name:a}),g.tools[a].$element=setupToolElement(d[a],g.tools[a]),groupElement.append(g.tools[a].$element)}),h.append(groupElement)}),g.updateToolDisplay=function(a,b,c){var d=g.tools[a];if(d){if(d._lastToolDefinition&&!c&&(b=angular.extend({},d._lastToolDefinition,b)),null===b.buttontext&&null===b.iconclass&&null===b.display)throw'textAngular Error: Tool Definition for updating "'+a+'" does not have a valid display/iconclass/buttontext value';null===b.buttontext&&delete b.buttontext,null===b.iconclass&&delete b.iconclass,null===b.display&&delete b.display,toolElement=setupToolElement(b,d),d.$element.replaceWith(toolElement),d.$element=toolElement}},b.registerToolbar(g),g.$on("$destroy",function(){b.unregisterToolbar(g.name)})}}}]).service("taToolExecuteAction",["$q",function(a){return function(b){void 0!==b&&(this.$editor=function(){return b});var c=a.defer(),d=c.promise,e=this.$editor();d["finally"](function(){e.endAction.call(e)});var f;try{f=this.action(c,e.startAction())}catch(g){}(f||void 0===f)&&c.resolve()}}]).service("textAngularManager",["taToolExecuteAction",function(a){var b={},d={};return{registerEditor:function(e,f,g){if(!e||""===e)throw"textAngular Error: An editor requires a name";if(!f)throw"textAngular Error: An editor requires a scope";if(d[e])throw'textAngular Error: An Editor with name "'+e+'" already exists';var h=[];return angular.forEach(g,function(a){b[a]&&h.push(b[a])}),d[e]={scope:f,toolbars:g,_registerToolbar:function(a){this.toolbars.indexOf(a.name)>=0&&h.push(a)},editorFunctions:{disable:function(){angular.forEach(h,function(a){a.disabled=!0})},enable:function(){angular.forEach(h,function(a){a.disabled=!1})},focus:function(){angular.forEach(h,function(a){a._parent=f,a.disabled=!1,a.focussed=!0})},unfocus:function(){angular.forEach(h,function(a){a.disabled=!0,a.focussed=!1})},updateSelectedStyles:function(a){angular.forEach(h,function(b){angular.forEach(b.tools,function(b){b.activeState&&(b.active=b.activeState(a))})})},sendKeyCommand:function(b){var d=!1;return(b.ctrlKey||b.metaKey)&&angular.forEach(c,function(c,e){if(c.commandKeyCode&&c.commandKeyCode===b.which)for(var g=0;g<h.length;g++)if(void 0!==h[g].tools[e]){a.call(h[g].tools[e],f),d=!0;break}}),d}}},d[e].editorFunctions},retrieveEditor:function(a){return d[a]},unregisterEditor:function(a){delete d[a]},registerToolbar:function(a){if(!a)throw"textAngular Error: A toolbar requires a scope";if(!a.name||""===a.name)throw"textAngular Error: A toolbar requires a name";if(b[a.name])throw'textAngular Error: A toolbar with name "'+a.name+'" already exists';b[a.name]=a,angular.forEach(d,function(b){b._registerToolbar(a)})},retrieveToolbar:function(a){return b[a]},retrieveToolbarsViaEditor:function(a){var b=[],c=this;return angular.forEach(this.retrieveEditor(a).toolbars,function(a){b.push(c.retrieveToolbar(a))}),b},unregisterToolbar:function(a){delete b[a]},updateToolsDisplay:function(a){var b=this;angular.forEach(a,function(a,c){b.updateToolDisplay(c,a)})},resetToolsDisplay:function(){var a=this;angular.forEach(c,function(b,c){a.resetToolDisplay(c)})},updateToolDisplay:function(a,c){var d=this;angular.forEach(b,function(b,e){d.updateToolbarToolDisplay(e,a,c)})},resetToolDisplay:function(a){var c=this;angular.forEach(b,function(b,d){c.resetToolbarToolDisplay(d,a)})},updateToolbarToolDisplay:function(a,c,d){if(!b[a])throw'textAngular Error: No Toolbar with name "'+a+'" exists';b[a].updateToolDisplay(c,d)},resetToolbarToolDisplay:function(a,d){if(!b[a])throw'textAngular Error: No Toolbar with name "'+a+'" exists';b[a].updateToolDisplay(d,c[d],!0)},refreshEditor:function(a){if(!d[a])throw'textAngular Error: No Editor with name "'+a+'" exists';d[a].scope.updateTaBindtaTextElement(),d[a].scope.$$phase||d[a].scope.$digest()}}}])}();