PageRenderTime 52ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/files/angular.textangular/1.2.1/textAngular.js

https://gitlab.com/Mirros/jsdelivr
JavaScript | 1071 lines | 792 code | 66 blank | 213 comment | 185 complexity | 677d6a7e2715a72594e2bb184bdbf88c MD5 | raw file
  1. /*
  2. textAngular
  3. Author : Austin Anderson
  4. License : 2013 MIT
  5. Version 1.2.1
  6. See README.md or https://github.com/fraywing/textAngular/wiki for requirements and use.
  7. */
  8. (function(){ // encapsulate all variables so they don't become global vars
  9. "Use Strict";
  10. // fix a webkit bug, see: https://gist.github.com/shimondoodkin/1081133
  11. // this is set true when a blur occurs as the blur of the ta-bind triggers before the click
  12. var globalContentEditableBlur = false;
  13. /* istanbul ignore next: Browser Un-Focus fix for webkit */
  14. if(/AppleWebKit\/([\d.]+)/.exec(navigator.userAgent)) { // detect webkit
  15. document.addEventListener("click", function(){
  16. var curelement = window.event.target;
  17. if(globalContentEditableBlur && curelement !== null){
  18. var isEditable = false;
  19. var tempEl = curelement;
  20. while(tempEl !== null && tempEl.tagName.toLowerCase() !== 'html' && !isEditable){
  21. isEditable = tempEl.contentEditable === 'true';
  22. tempEl = tempEl.parentNode;
  23. }
  24. if(!isEditable){
  25. document.getElementById('textAngular-editableFix-010203040506070809').setSelectionRange(0, 0); // set caret focus to an element that handles caret focus correctly.
  26. curelement.focus(); // focus the wanted element.
  27. }
  28. }
  29. globalContentEditableBlur = false;
  30. }, false); // add global click handler
  31. angular.element(document.body).append('<input id="textAngular-editableFix-010203040506070809" style="width:1px;height:1px;border:none;margin:0;padding:0;position:absolute; top: -10000; left: -10000;" unselectable="on" tabIndex="-1">');
  32. }
  33. // IE version detection - http://stackoverflow.com/questions/4169160/javascript-ie-detection-why-not-use-simple-conditional-comments
  34. // We need this as IE sometimes plays funny tricks with the contenteditable.
  35. // ----------------------------------------------------------
  36. // If you're not in IE (or IE version is less than 5) then:
  37. // ie === undefined
  38. // If you're in IE (>=5) then you can determine which version:
  39. // ie === 7; // IE7
  40. // Thus, to detect IE:
  41. // if (ie) {}
  42. // And to detect the version:
  43. // ie === 6 // IE6
  44. // ie > 7 // IE8, IE9, IE10 ...
  45. // ie < 9 // Anything less than IE9
  46. // ----------------------------------------------------------
  47. /* istanbul ignore next: untestable browser check */
  48. var ie = (function(){
  49. var undef,rv = -1; // Return value assumes failure.
  50. var ua = window.navigator.userAgent;
  51. var msie = ua.indexOf('MSIE ');
  52. var trident = ua.indexOf('Trident/');
  53. if (msie > 0) {
  54. // IE 10 or older => return version number
  55. rv = parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
  56. } else if (trident > 0) {
  57. // IE 11 (or newer) => return version number
  58. var rvNum = ua.indexOf('rv:');
  59. rv = parseInt(ua.substring(rvNum + 3, ua.indexOf('.', rvNum)), 10);
  60. }
  61. return ((rv > -1) ? rv : undef);
  62. }());
  63. // Thanks to answer in http://stackoverflow.com/questions/2308134/trim-in-javascript-not-working-in-ie
  64. /* istanbul ignore next: trim shim for older browsers */
  65. if(typeof String.prototype.trim !== 'function') {
  66. String.prototype.trim = function() {
  67. return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  68. };
  69. }
  70. // tests against the current jqLite/jquery implementation if this can be an element
  71. function validElementString(string){
  72. try{
  73. return angular.element(string).length !== 0;
  74. }catch(any){
  75. return false;
  76. }
  77. }
  78. /*
  79. Custom stylesheet for the placeholders rules.
  80. Credit to: http://davidwalsh.name/add-rules-stylesheets
  81. */
  82. var sheet, addCSSRule, removeCSSRule;
  83. /* istanbul ignore else: IE <8 test*/
  84. if(ie > 8 || ie === undefined){
  85. var topsheet = (function() {
  86. // Create the <style> tag
  87. var style = document.createElement("style");
  88. /* istanbul ignore else : WebKit hack :( */
  89. if(/AppleWebKit\/([\d.]+)/.exec(navigator.userAgent)) style.appendChild(document.createTextNode(""));
  90. // Add the <style> element to the page, add as first so the styles can be overridden by custom stylesheets
  91. document.head.insertBefore(style,document.head.firstChild);
  92. return style.sheet;
  93. })();
  94. // this sheet is used for the placeholders later on.
  95. sheet = (function() {
  96. // Create the <style> tag
  97. var style = document.createElement("style");
  98. /* istanbul ignore else : WebKit hack :( */
  99. if(/AppleWebKit\/([\d.]+)/.exec(navigator.userAgent)) style.appendChild(document.createTextNode(""));
  100. // Add the <style> element to the page, add as first so the styles can be overridden by custom stylesheets
  101. document.head.appendChild(style);
  102. return style.sheet;
  103. })();
  104. // use as: addCSSRule("header", "float: left");
  105. addCSSRule = function(selector, rules) {
  106. _addCSSRule(sheet, selector, rules);
  107. };
  108. _addCSSRule = function(sheet, selector, rules){
  109. var insertIndex;
  110. /* istanbul ignore else: firefox catch */
  111. if(sheet.rules) insertIndex = Math.max(sheet.rules.length - 1, 0);
  112. else if(sheet.cssRules) insertIndex = Math.max(sheet.cssRules.length - 1, 0);
  113. /* istanbul ignore else: untestable IE option */
  114. if(sheet.insertRule) {
  115. sheet.insertRule(selector + "{" + rules + "}", insertIndex);
  116. }
  117. else {
  118. sheet.addRule(selector, rules, insertIndex);
  119. }
  120. // return the index of the stylesheet rule
  121. return insertIndex;
  122. };
  123. removeCSSRule = function(index){
  124. _removeCSSRule(sheet, index);
  125. };
  126. _removeCSSRule = function(sheet, index){
  127. /* istanbul ignore else: untestable IE option */
  128. if(sheet.removeRule){
  129. sheet.removeRule(index);
  130. }else{
  131. sheet.deleteRule(index);
  132. }
  133. };
  134. // add generic styling for the editor
  135. _addCSSRule(topsheet, '.ta-scroll-window.form-control', "height: 300px; overflow: auto; font-family: inherit; font-size: 100%; position: relative; padding: 0;");
  136. _addCSSRule(topsheet, '.ta-root.focussed .ta-scroll-window.form-control', 'border-color: #66afe9; outline: 0; -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(102, 175, 233, 0.6);');
  137. _addCSSRule(topsheet, '.ta-editor.ta-html', "min-height: 300px; height: auto; overflow: auto; font-family: inherit; font-size: 100%;");
  138. _addCSSRule(topsheet, '.ta-scroll-window .ta-bind', "height: auto; min-height: 300px; padding: 6px 12px;");
  139. // add the styling for the awesomness of the resizer
  140. _addCSSRule(topsheet, '.ta-root .ta-resizer-handle-overlay', 'z-index: 100; position: absolute; display: none;');
  141. _addCSSRule(topsheet, '.ta-root .ta-resizer-handle-overlay > .ta-resizer-handle-info', 'position: absolute; bottom: 16px; right: 16px; border: 1px solid black; background-color: #FFF; padding: 0 4px; opacity: 0.7;');
  142. _addCSSRule(topsheet, '.ta-root .ta-resizer-handle-overlay > .ta-resizer-handle-background', 'position: absolute; bottom: 5px; right: 5px; left: 5px; top: 5px; border: 1px solid black; background-color: rgba(0, 0, 0, 0.2);');
  143. _addCSSRule(topsheet, '.ta-root .ta-resizer-handle-overlay > .ta-resizer-handle-corner', 'width: 10px; height: 10px; position: absolute;');
  144. _addCSSRule(topsheet, '.ta-root .ta-resizer-handle-overlay > .ta-resizer-handle-corner-tl', 'top: 0; left: 0; border-left: 1px solid black; border-top: 1px solid black;');
  145. _addCSSRule(topsheet, '.ta-root .ta-resizer-handle-overlay > .ta-resizer-handle-corner-tr', 'top: 0; right: 0; border-right: 1px solid black; border-top: 1px solid black;');
  146. _addCSSRule(topsheet, '.ta-root .ta-resizer-handle-overlay > .ta-resizer-handle-corner-bl', 'bottom: 0; left: 0; border-left: 1px solid black; border-bottom: 1px solid black;');
  147. _addCSSRule(topsheet, '.ta-root .ta-resizer-handle-overlay > .ta-resizer-handle-corner-br', 'bottom: 0; right: 0; border: 1px solid black; cursor: se-resize; background-color: white;');
  148. }
  149. // recursive function that returns an array of angular.elements that have the passed attribute set on them
  150. function getByAttribute(element, attribute){
  151. var resultingElements = [];
  152. var childNodes = element.children();
  153. if(childNodes.length){
  154. angular.forEach(childNodes, function(child){
  155. resultingElements = resultingElements.concat(getByAttribute(angular.element(child), attribute));
  156. });
  157. }
  158. if(element.attr(attribute) !== undefined) resultingElements.push(element);
  159. return resultingElements;
  160. }
  161. // this global var is used to prevent multiple fires of the drop event. Needs to be global to the textAngular file.
  162. var dropFired = false;
  163. var textAngular = angular.module("textAngular", ['ngSanitize', 'textAngularSetup']); //This makes ngSanitize required
  164. // setup the global contstant functions for setting up the toolbar
  165. // all tool definitions
  166. var taTools = {};
  167. /*
  168. A tool definition is an object with the following key/value parameters:
  169. action: [function(deferred, restoreSelection)]
  170. a function that is executed on clicking on the button - this will allways be executed using ng-click and will
  171. overwrite any ng-click value in the display attribute.
  172. The function is passed a deferred object ($q.defer()), if this is wanted to be used `return false;` from the action and
  173. manually call `deferred.resolve();` elsewhere to notify the editor that the action has finished.
  174. restoreSelection is only defined if the rangy library is included and it can be called as `restoreSelection()` to restore the users
  175. selection in the WYSIWYG editor.
  176. display: [string]?
  177. Optional, an HTML element to be displayed as the buton. The `scope` of the button is the tool definition object with some additional functions
  178. If set this will cause buttontext and iconclass to be ignored
  179. buttontext: [string]?
  180. if this is defined it will replace the contents of the element contained in the `display` element
  181. iconclass: [string]?
  182. if this is defined an icon (<i>) will be appended to the `display` element with this string as it's class
  183. activestate: [function(commonElement)]?
  184. this function is called on every caret movement, if it returns true then the class taOptions.classes.toolbarButtonActive
  185. will be applied to the `display` element, else the class will be removed
  186. disabled: [function()]?
  187. if this function returns true then the tool will have the class taOptions.classes.disabled applied to it, else it will be removed
  188. Other functions available on the scope are:
  189. name: [string]
  190. the name of the tool, this is the first parameter passed into taRegisterTool
  191. isDisabled: [function()]
  192. returns true if the tool is disabled, false if it isn't
  193. displayActiveToolClass: [function(boolean)]
  194. returns true if the tool is 'active' in the currently focussed toolbar
  195. onElementSelect: [Object]
  196. This object contains the following key/value pairs and is used to trigger the ta-element-select event
  197. element: [String]
  198. an element name, will only trigger the onElementSelect action if the tagName of the element matches this string
  199. filter: [function(element)]?
  200. an optional filter that returns a boolean, if true it will trigger the onElementSelect.
  201. action: [function(event, element, editorScope)]
  202. the action that should be executed if the onElementSelect function runs
  203. */
  204. // name and toolDefinition to add into the tools available to be added on the toolbar
  205. function registerTextAngularTool(name, toolDefinition){
  206. if(!name || name === '' || taTools.hasOwnProperty(name)) throw('textAngular Error: A unique name is required for a Tool Definition');
  207. if(
  208. (toolDefinition.display && (toolDefinition.display === '' || !validElementString(toolDefinition.display))) ||
  209. (!toolDefinition.display && !toolDefinition.buttontext && !toolDefinition.iconclass)
  210. )
  211. throw('textAngular Error: Tool Definition for "' + name + '" does not have a valid display/iconclass/buttontext value');
  212. taTools[name] = toolDefinition;
  213. }
  214. textAngular.constant('taRegisterTool', registerTextAngularTool);
  215. textAngular.value('taTools', taTools);
  216. textAngular.config([function(){
  217. // clear taTools variable. Just catches testing and any other time that this config may run multiple times...
  218. angular.forEach(taTools, function(value, key){ delete taTools[key]; });
  219. }]);
  220. textAngular.directive("textAngular", [
  221. '$compile', '$timeout', 'taOptions', 'taSanitize', 'taSelection', 'taExecCommand', 'textAngularManager', '$window', '$document', '$animate', '$log',
  222. function($compile, $timeout, taOptions, taSanitize, taSelection, taExecCommand, textAngularManager, $window, $document, $animate, $log){
  223. return {
  224. require: '?ngModel',
  225. scope: {},
  226. restrict: "EA",
  227. link: function(scope, element, attrs, ngModel){
  228. // all these vars should not be accessable outside this directive
  229. var _keydown, _keyup, _keypress, _mouseup, _focusin, _focusout,
  230. _originalContents, _toolbars,
  231. _serial = Math.floor(Math.random() * 10000000000000000),
  232. _name = (attrs.name) ? attrs.name : 'textAngularEditor' + _serial,
  233. _taExecCommand;
  234. var oneEvent = function(_element, event, action){
  235. $timeout(function(){
  236. // shim the .one till fixed
  237. var _func = function(){
  238. _element.off(event, _func);
  239. action();
  240. };
  241. _element.on(event, _func);
  242. }, 100);
  243. };
  244. _taExecCommand = taExecCommand(attrs.taDefaultWrap);
  245. // get the settings from the defaults and add our specific functions that need to be on the scope
  246. angular.extend(scope, angular.copy(taOptions), {
  247. // wraps the selection in the provided tag / execCommand function. Should only be called in WYSIWYG mode.
  248. wrapSelection: function(command, opt, isSelectableElementTool){
  249. // catch errors like FF erroring when you try to force an undo with nothing done
  250. _taExecCommand(command, false, opt);
  251. if(isSelectableElementTool){
  252. // re-apply the selectable tool events
  253. scope['reApplyOnSelectorHandlerstaTextElement' + _serial]();
  254. }
  255. // refocus on the shown display element, this fixes a display bug when using :focus styles to outline the box.
  256. // You still have focus on the text/html input it just doesn't show up
  257. scope.displayElements.text[0].focus();
  258. },
  259. showHtml: false
  260. });
  261. // setup the options from the optional attributes
  262. if(attrs.taFocussedClass) scope.classes.focussed = attrs.taFocussedClass;
  263. if(attrs.taTextEditorClass) scope.classes.textEditor = attrs.taTextEditorClass;
  264. if(attrs.taHtmlEditorClass) scope.classes.htmlEditor = attrs.taHtmlEditorClass;
  265. // optional setup functions
  266. if(attrs.taTextEditorSetup) scope.setup.textEditorSetup = scope.$parent.$eval(attrs.taTextEditorSetup);
  267. if(attrs.taHtmlEditorSetup) scope.setup.htmlEditorSetup = scope.$parent.$eval(attrs.taHtmlEditorSetup);
  268. // optional fileDropHandler function
  269. if(attrs.taFileDrop) scope.fileDropHandler = scope.$parent.$eval(attrs.taFileDrop);
  270. else scope.fileDropHandler = scope.defaultFileDropHandler;
  271. _originalContents = element[0].innerHTML;
  272. // clear the original content
  273. element[0].innerHTML = '';
  274. // Setup the HTML elements as variable references for use later
  275. scope.displayElements = {
  276. // we still need the hidden input even with a textarea as the textarea may have invalid/old input in it,
  277. // wheras the input will ALLWAYS have the correct value.
  278. forminput: angular.element("<input type='hidden' tabindex='-1' style='display: none;'>"),
  279. html: angular.element("<textarea></textarea>"),
  280. text: angular.element("<div></div>"),
  281. // other toolbased elements
  282. scrollWindow: angular.element("<div class='ta-scroll-window'></div>"),
  283. popover: angular.element('<div class="popover fade bottom" style="max-width: none; width: 305px;"><div class="arrow"></div></div>'),
  284. popoverContainer: angular.element('<div class="popover-content"></div>'),
  285. resize: {
  286. overlay: angular.element('<div class="ta-resizer-handle-overlay"></div>'),
  287. background: angular.element('<div class="ta-resizer-handle-background"></div>'),
  288. anchors: [
  289. angular.element('<div class="ta-resizer-handle-corner ta-resizer-handle-corner-tl"></div>'),
  290. angular.element('<div class="ta-resizer-handle-corner ta-resizer-handle-corner-tr"></div>'),
  291. angular.element('<div class="ta-resizer-handle-corner ta-resizer-handle-corner-bl"></div>'),
  292. angular.element('<div class="ta-resizer-handle-corner ta-resizer-handle-corner-br"></div>')
  293. ],
  294. info: angular.element('<div class="ta-resizer-handle-info"></div>')
  295. }
  296. };
  297. // Setup the popover
  298. scope.displayElements.popover.append(scope.displayElements.popoverContainer);
  299. scope.displayElements.scrollWindow.append(scope.displayElements.popover);
  300. scope.displayElements.popover.on('mousedown', function(e, eventData){
  301. /* istanbul ignore else: this is for catching the jqLite testing*/
  302. if(eventData) angular.extend(e, eventData);
  303. // this prevents focusout from firing on the editor when clicking anything in the popover
  304. e.preventDefault();
  305. return false;
  306. });
  307. // define the popover show and hide functions
  308. scope.showPopover = function(_el){
  309. scope.reflowPopover(_el);
  310. scope.displayElements.popover.css('display', 'block');
  311. $animate.addClass(scope.displayElements.popover, 'in');
  312. oneEvent(element, 'click keyup', function(){scope.hidePopover();});
  313. };
  314. scope.reflowPopover = function(_el){
  315. if(scope.displayElements.text[0].offsetHeight - 51 > _el[0].offsetTop){
  316. scope.displayElements.popover.css('top', _el[0].offsetTop + _el[0].offsetHeight + 'px');
  317. scope.displayElements.popover.removeClass('top').addClass('bottom');
  318. }else{
  319. scope.displayElements.popover.css('top', _el[0].offsetTop - 54 + 'px');
  320. scope.displayElements.popover.removeClass('bottom').addClass('top');
  321. }
  322. scope.displayElements.popover.css('left', Math.max(0,
  323. Math.min(
  324. scope.displayElements.text[0].offsetWidth - 305,
  325. _el[0].offsetLeft + (_el[0].offsetWidth / 2.0) - 152.5
  326. )
  327. ) + 'px');
  328. };
  329. scope.hidePopover = function(){
  330. $animate.removeClass(scope.displayElements.popover, 'in', /* istanbul ignore next: dosen't test with mocked animate */ function(){
  331. scope.displayElements.popover.css('display', '');
  332. scope.displayElements.popoverContainer.attr('style', '');
  333. scope.displayElements.popoverContainer.attr('class', 'popover-content');
  334. });
  335. };
  336. // setup the resize overlay
  337. scope.displayElements.resize.overlay.append(scope.displayElements.resize.background);
  338. angular.forEach(scope.displayElements.resize.anchors, function(anchor){ scope.displayElements.resize.overlay.append(anchor);});
  339. scope.displayElements.resize.overlay.append(scope.displayElements.resize.info);
  340. scope.displayElements.scrollWindow.append(scope.displayElements.resize.overlay);
  341. // define the show and hide events
  342. scope.reflowResizeOverlay = function(_el){
  343. _el = angular.element(_el)[0];
  344. scope.displayElements.resize.overlay.css({
  345. 'display': 'block',
  346. 'left': _el.offsetLeft - 5 + 'px',
  347. 'top': _el.offsetTop - 5 + 'px',
  348. 'width': _el.offsetWidth + 10 + 'px',
  349. 'height': _el.offsetHeight + 10 + 'px'
  350. });
  351. scope.displayElements.resize.info.text(_el.offsetWidth + ' x ' + _el.offsetHeight);
  352. };
  353. /* istanbul ignore next: pretty sure phantomjs won't test this */
  354. scope.showResizeOverlay = function(_el){
  355. var resizeMouseDown = function(event){
  356. var startPosition = {
  357. width: parseInt(_el.attr('width')),
  358. height: parseInt(_el.attr('height')),
  359. x: event.clientX,
  360. y: event.clientY
  361. };
  362. if(startPosition.width === undefined) startPosition.width = _el[0].offsetWidth;
  363. if(startPosition.height === undefined) startPosition.height = _el[0].offsetHeight;
  364. scope.hidePopover();
  365. var ratio = startPosition.height / startPosition.width;
  366. var mousemove = function(event){
  367. // calculate new size
  368. var pos = {
  369. x: Math.max(0, startPosition.width + (event.clientX - startPosition.x)),
  370. y: Math.max(0, startPosition.height + (event.clientY - startPosition.y))
  371. };
  372. var applyImageSafeCSS = function(_el, css){
  373. _el = angular.element(_el);
  374. if(_el[0].tagName.toLowerCase() === 'img'){
  375. if(css.height){
  376. _el.attr('height', css.height);
  377. delete css.height;
  378. }
  379. if(css.width){
  380. _el.attr('width', css.width);
  381. delete css.width;
  382. }
  383. }
  384. _el.css(css);
  385. };
  386. if(event.shiftKey){
  387. // keep ratio
  388. var newRatio = pos.y / pos.x;
  389. applyImageSafeCSS(_el, {
  390. width: ratio > newRatio ? pos.x : pos.y / ratio,
  391. height: ratio > newRatio ? pos.x * ratio : pos.y
  392. });
  393. }else{
  394. applyImageSafeCSS(_el, {
  395. width: pos.x,
  396. height: pos.y
  397. });
  398. }
  399. // reflow the popover tooltip
  400. scope.reflowResizeOverlay(_el);
  401. };
  402. $document.find('body').on('mousemove', mousemove);
  403. oneEvent(scope.displayElements.resize.overlay, 'mouseup', function(){
  404. $document.find('body').off('mousemove', mousemove);
  405. scope.showPopover(_el);
  406. });
  407. event.stopPropagation();
  408. event.preventDefault();
  409. };
  410. scope.displayElements.resize.anchors[3].on('mousedown', resizeMouseDown);
  411. scope.reflowResizeOverlay(_el);
  412. oneEvent(element, 'click', function(){scope.hideResizeOverlay();});
  413. };
  414. /* istanbul ignore next: pretty sure phantomjs won't test this */
  415. scope.hideResizeOverlay = function(){
  416. scope.displayElements.resize.overlay.css('display', '');
  417. };
  418. // allow for insertion of custom directives on the textarea and div
  419. scope.setup.htmlEditorSetup(scope.displayElements.html);
  420. scope.setup.textEditorSetup(scope.displayElements.text);
  421. scope.displayElements.html.attr({
  422. 'id': 'taHtmlElement' + _serial,
  423. 'ng-show': 'showHtml',
  424. 'ta-bind': 'ta-bind',
  425. 'ng-model': 'html'
  426. });
  427. scope.displayElements.text.attr({
  428. 'id': 'taTextElement' + _serial,
  429. 'contentEditable': 'true',
  430. 'ta-bind': 'ta-bind',
  431. 'ng-model': 'html'
  432. });
  433. scope.displayElements.scrollWindow.attr({'ng-hide': 'showHtml'});
  434. if(attrs.taDefaultWrap) scope.displayElements.text.attr('ta-default-wrap', attrs.taDefaultWrap);
  435. // add the main elements to the origional element
  436. scope.displayElements.scrollWindow.append(scope.displayElements.text);
  437. element.append(scope.displayElements.scrollWindow);
  438. element.append(scope.displayElements.html);
  439. scope.displayElements.forminput.attr('name', _name);
  440. element.append(scope.displayElements.forminput);
  441. if(attrs.tabindex){
  442. element.removeAttr('tabindex');
  443. scope.displayElements.text.attr('tabindex', attrs.tabindex);
  444. scope.displayElements.html.attr('tabindex', attrs.tabindex);
  445. }
  446. if (attrs.placeholder) {
  447. scope.displayElements.text.attr('placeholder', attrs.placeholder);
  448. scope.displayElements.html.attr('placeholder', attrs.placeholder);
  449. }
  450. if(attrs.taDisabled){
  451. scope.displayElements.text.attr('ta-readonly', 'disabled');
  452. scope.displayElements.html.attr('ta-readonly', 'disabled');
  453. scope.disabled = scope.$parent.$eval(attrs.taDisabled);
  454. scope.$parent.$watch(attrs.taDisabled, function(newVal){
  455. scope.disabled = newVal;
  456. if(scope.disabled){
  457. element.addClass(scope.classes.disabled);
  458. }else{
  459. element.removeClass(scope.classes.disabled);
  460. }
  461. });
  462. }
  463. // compile the scope with the text and html elements only - if we do this with the main element it causes a compile loop
  464. $compile(scope.displayElements.scrollWindow)(scope);
  465. $compile(scope.displayElements.html)(scope);
  466. scope.updateTaBindtaTextElement = scope['updateTaBindtaTextElement' + _serial];
  467. scope.updateTaBindtaHtmlElement = scope['updateTaBindtaHtmlElement' + _serial];
  468. // add the classes manually last
  469. element.addClass("ta-root");
  470. scope.displayElements.scrollWindow.addClass("ta-text ta-editor " + scope.classes.textEditor);
  471. scope.displayElements.html.addClass("ta-html ta-editor " + scope.classes.htmlEditor);
  472. // used in the toolbar actions
  473. scope._actionRunning = false;
  474. var _savedSelection = false;
  475. scope.startAction = function(){
  476. scope._actionRunning = true;
  477. // if rangy library is loaded return a function to reload the current selection
  478. if($window.rangy && $window.rangy.saveSelection){
  479. _savedSelection = $window.rangy.saveSelection();
  480. return function(){
  481. if(_savedSelection) $window.rangy.restoreSelection(_savedSelection);
  482. };
  483. }
  484. };
  485. scope.endAction = function(){
  486. scope._actionRunning = false;
  487. if(_savedSelection) $window.rangy.removeMarkers(_savedSelection);
  488. _savedSelection = false;
  489. scope.updateSelectedStyles();
  490. // only update if in text or WYSIWYG mode
  491. if(!scope.showHtml) scope['updateTaBindtaTextElement' + _serial]();
  492. };
  493. // note that focusout > focusin is called everytime we click a button - except bad support: http://www.quirksmode.org/dom/events/blurfocus.html
  494. // cascades to displayElements.text and displayElements.html automatically.
  495. _focusin = function(){
  496. element.addClass(scope.classes.focussed);
  497. _toolbars.focus();
  498. };
  499. scope.displayElements.html.on('focus', _focusin);
  500. scope.displayElements.text.on('focus', _focusin);
  501. _focusout = function(e){
  502. // if we are NOT runnig an action and have NOT focussed again on the text etc then fire the blur events
  503. if(!scope._actionRunning && $document[0].activeElement !== scope.displayElements.html[0] && $document[0].activeElement !== scope.displayElements.text[0]){
  504. element.removeClass(scope.classes.focussed);
  505. _toolbars.unfocus();
  506. // to prevent multiple apply error defer to next seems to work.
  507. $timeout(function(){ element.triggerHandler('blur'); }, 0);
  508. }
  509. e.preventDefault();
  510. return false;
  511. };
  512. scope.displayElements.html.on('blur', _focusout);
  513. scope.displayElements.text.on('blur', _focusout);
  514. // Setup the default toolbar tools, this way allows the user to add new tools like plugins.
  515. // This is on the editor for future proofing if we find a better way to do this.
  516. scope.queryFormatBlockState = function(command){
  517. return command.toLowerCase() === $document[0].queryCommandValue('formatBlock').toLowerCase();
  518. };
  519. scope.switchView = function(){
  520. scope.showHtml = !scope.showHtml;
  521. //Show the HTML view
  522. if(scope.showHtml){
  523. //defer until the element is visible
  524. $timeout(function(){
  525. // [0] dereferences the DOM object from the angular.element
  526. return scope.displayElements.html[0].focus();
  527. }, 100);
  528. }else{
  529. //Show the WYSIWYG view
  530. //defer until the element is visible
  531. $timeout(function(){
  532. // [0] dereferences the DOM object from the angular.element
  533. return scope.displayElements.text[0].focus();
  534. }, 100);
  535. }
  536. };
  537. // changes to the model variable from outside the html/text inputs
  538. // if no ngModel, then the only input is from inside text-angular
  539. if(attrs.ngModel){
  540. var _firstRun = true;
  541. ngModel.$render = function(){
  542. if(_firstRun){
  543. // we need this firstRun to set the originalContents otherwise it gets overrided by the setting of ngModel to undefined from NaN
  544. _firstRun = false;
  545. // if view value is null or undefined initially and there was original content, set to the original content
  546. var _initialValue = scope.$parent.$eval(attrs.ngModel);
  547. if((_initialValue === undefined || _initialValue === null) && (_originalContents && _originalContents !== '')){
  548. // on passing through to taBind it will be sanitised
  549. ngModel.$setViewValue(_originalContents);
  550. }
  551. }
  552. scope.displayElements.forminput.val(ngModel.$viewValue);
  553. // if the editors aren't focused they need to be updated, otherwise they are doing the updating
  554. /* istanbul ignore else: don't care */
  555. if(!scope._elementSelectTriggered && $document[0].activeElement !== scope.displayElements.html[0] && $document[0].activeElement !== scope.displayElements.text[0]){
  556. // catch model being null or undefined
  557. scope.html = ngModel.$viewValue || '';
  558. }
  559. };
  560. }else{
  561. // if no ngModel then update from the contents of the origional html.
  562. scope.displayElements.forminput.val(_originalContents);
  563. scope.html = _originalContents;
  564. }
  565. // changes from taBind back up to here
  566. scope.$watch('html', function(newValue, oldValue){
  567. if(newValue !== oldValue){
  568. if(attrs.ngModel && ngModel.$viewValue !== newValue) ngModel.$setViewValue(newValue);
  569. scope.displayElements.forminput.val(newValue);
  570. }
  571. });
  572. if(attrs.taTargetToolbars) _toolbars = textAngularManager.registerEditor(_name, scope, attrs.taTargetToolbars.split(','));
  573. else{
  574. var _toolbar = angular.element('<div text-angular-toolbar name="textAngularToolbar' + _serial + '">');
  575. // passthrough init of toolbar options
  576. if(attrs.taToolbar) _toolbar.attr('ta-toolbar', attrs.taToolbar);
  577. if(attrs.taToolbarClass) _toolbar.attr('ta-toolbar-class', attrs.taToolbarClass);
  578. if(attrs.taToolbarGroupClass) _toolbar.attr('ta-toolbar-group-class', attrs.taToolbarGroupClass);
  579. if(attrs.taToolbarButtonClass) _toolbar.attr('ta-toolbar-button-class', attrs.taToolbarButtonClass);
  580. if(attrs.taToolbarActiveButtonClass) _toolbar.attr('ta-toolbar-active-button-class', attrs.taToolbarActiveButtonClass);
  581. if(attrs.taFocussedClass) _toolbar.attr('ta-focussed-class', attrs.taFocussedClass);
  582. element.prepend(_toolbar);
  583. $compile(_toolbar)(scope.$parent);
  584. _toolbars = textAngularManager.registerEditor(_name, scope, ['textAngularToolbar' + _serial]);
  585. }
  586. scope.$on('$destroy', function(){
  587. textAngularManager.unregisterEditor(_name);
  588. });
  589. // catch element select event and pass to toolbar tools
  590. scope.$on('ta-element-select', function(event, element){
  591. _toolbars.triggerElementSelect(event, element);
  592. });
  593. scope.$on('ta-drop-event', function(event, element, dropEvent, dataTransfer){
  594. scope.displayElements.text[0].focus();
  595. if(dataTransfer && dataTransfer.files && dataTransfer.files.length > 0){
  596. angular.forEach(dataTransfer.files, function(file){
  597. // taking advantage of boolean execution, if the fileDropHandler returns true, nothing else after it is executed
  598. // If it is false then execute the defaultFileDropHandler if the fileDropHandler is NOT the default one
  599. try{
  600. return scope.fileDropHandler(file, scope.wrapSelection) ||
  601. (scope.fileDropHandler !== scope.defaultFileDropHandler &&
  602. scope.defaultFileDropHandler(file, scope.wrapSelection));
  603. }catch(error){
  604. $log.error(error);
  605. }
  606. });
  607. dropEvent.preventDefault();
  608. dropEvent.stopPropagation();
  609. }
  610. });
  611. // the following is for applying the active states to the tools that support it
  612. scope._bUpdateSelectedStyles = false;
  613. // loop through all the tools polling their activeState function if it exists
  614. scope.updateSelectedStyles = function(){
  615. var _selection;
  616. // test if the common element ISN'T the root ta-text node
  617. if((_selection = taSelection.getSelectionElement()) !== undefined && _selection.parentNode !== scope.displayElements.text[0]){
  618. _toolbars.updateSelectedStyles(angular.element(_selection));
  619. }else _toolbars.updateSelectedStyles();
  620. // used to update the active state when a key is held down, ie the left arrow
  621. if(scope._bUpdateSelectedStyles) $timeout(scope.updateSelectedStyles, 200);
  622. };
  623. // start updating on keydown
  624. _keydown = function(){
  625. /* istanbul ignore else: don't run if already running */
  626. if(!scope._bUpdateSelectedStyles){
  627. scope._bUpdateSelectedStyles = true;
  628. scope.$apply(function(){
  629. scope.updateSelectedStyles();
  630. });
  631. }
  632. };
  633. scope.displayElements.html.on('keydown', _keydown);
  634. scope.displayElements.text.on('keydown', _keydown);
  635. // stop updating on key up and update the display/model
  636. _keyup = function(){
  637. scope._bUpdateSelectedStyles = false;
  638. };
  639. scope.displayElements.html.on('keyup', _keyup);
  640. scope.displayElements.text.on('keyup', _keyup);
  641. // stop updating on key up and update the display/model
  642. _keypress = function(event, eventData){
  643. /* istanbul ignore else: this is for catching the jqLite testing*/
  644. if(eventData) angular.extend(event, eventData);
  645. scope.$apply(function(){
  646. if(_toolbars.sendKeyCommand(event)){
  647. /* istanbul ignore else: don't run if already running */
  648. if(!scope._bUpdateSelectedStyles){
  649. scope.updateSelectedStyles();
  650. }
  651. event.preventDefault();
  652. return false;
  653. }
  654. });
  655. };
  656. scope.displayElements.html.on('keypress', _keypress);
  657. scope.displayElements.text.on('keypress', _keypress);
  658. // update the toolbar active states when we click somewhere in the text/html boxed
  659. _mouseup = function(){
  660. // ensure only one execution of updateSelectedStyles()
  661. scope._bUpdateSelectedStyles = false;
  662. scope.$apply(function(){
  663. scope.updateSelectedStyles();
  664. });
  665. };
  666. scope.displayElements.html.on('mouseup', _mouseup);
  667. scope.displayElements.text.on('mouseup', _mouseup);
  668. }
  669. };
  670. }
  671. ]).factory('taBrowserTag', [function(){
  672. return function(tag){
  673. /* istanbul ignore next: ie specific test */
  674. if(!tag) return (ie <= 8)? 'P' : 'p';
  675. else if(tag === '') return (ie === undefined)? 'div' : (ie <= 8)? 'P' : 'p';
  676. else return (ie <= 8)? tag.toUpperCase() : tag;
  677. };
  678. }]).factory('taExecCommand', ['taSelection', 'taBrowserTag', '$document', function(taSelection, taBrowserTag, $document){
  679. var BLOCKELEMENTS = /(address|article|aside|audio|blockquote|canvas|dd|div|dl|fieldset|figcaption|figure|footer|form|h1|h2|h3|h4|h5|h6|header|hgroup|hr|noscript|ol|output|p|pre|section|table|tfoot|ul|video)/ig;
  680. var LISTELEMENTS = /(ul|li|ol)/ig;
  681. var listToDefault = function(listElement, defaultWrap){
  682. var $target;
  683. // if all selected then we should remove the list
  684. // grab all li elements and convert to taDefaultWrap tags
  685. var children = listElement.find('li');
  686. for(i = children.length - 1; i >= 0; i--){
  687. $target = angular.element('<' + defaultWrap + '>' + children[i].innerHTML + '</' + defaultWrap + '>');
  688. listElement.after($target);
  689. }
  690. listElement.remove();
  691. taSelection.setSelectionToElementEnd($target[0]);
  692. };
  693. var listToList = function(listElement, newListTag){
  694. var $target = angular.element('<' + newListTag + '>' + listElement[0].innerHTML + '</' + newListTag + '>');
  695. listElement.after($target);
  696. listElement.remove();
  697. taSelection.setSelectionToElementEnd($target.find('li')[0]);
  698. };
  699. var childElementsToList = function(elements, listElement, newListTag){
  700. var html = '';
  701. for(i = elements.length - 1; i >= 0; i--){
  702. html += '<' + taBrowserTag('li') + '>' + elements[i].innerHTML + '</' + taBrowserTag('li') + '>';
  703. }
  704. var $target = angular.element('<' + newListTag + '>' + html + '</' + newListTag + '>');
  705. listElement.after($target);
  706. listElement.remove();
  707. taSelection.setSelectionToElementEnd($target.find('li')[0]);
  708. };
  709. return function(taDefaultWrap){
  710. taDefaultWrap = taBrowserTag(taDefaultWrap);
  711. return function(command, showUI, options){
  712. var i, $target, html, _nodes, next;
  713. var defaultWrapper = angular.element('<' + taDefaultWrap + '>');
  714. var selectedElement = taSelection.getSelectionElement();
  715. var $selected = angular.element(selectedElement);
  716. if(selectedElement !== undefined){
  717. var tagName = selectedElement.tagName.toLowerCase();
  718. if(command.toLowerCase() === 'insertorderedlist' || command.toLowerCase() === 'insertunorderedlist'){
  719. var selfTag = taBrowserTag((command.toLowerCase() === 'insertorderedlist')? 'ol' : 'ul');
  720. if(tagName === selfTag){
  721. // if all selected then we should remove the list
  722. // grab all li elements and convert to taDefaultWrap tags
  723. return listToDefault($selected, taDefaultWrap);
  724. }else if(tagName === 'li' && $selected.parent()[0].tagName.toLowerCase() === selfTag && $selected.parent().children().length === 1){
  725. // catch for the previous statement if only one li exists
  726. return listToDefault($selected.parent(), taDefaultWrap);
  727. }else if(tagName === 'li' && $selected.parent()[0].tagName.toLowerCase() !== selfTag && $selected.parent().children().length === 1){
  728. // catch for the previous statement if only one li exists
  729. return listToList($selected.parent(), selfTag);
  730. }else if(tagName.match(BLOCKELEMENTS) && !$selected.hasClass('ta-bind')){
  731. // if it's one of those block elements we have to change the contents
  732. // if it's a ol/ul we are changing from one to the other
  733. if(tagName === 'ol' || tagName === 'ul'){
  734. return listToList($selected, selfTag);
  735. }else{
  736. if(selectedElement.innerHTML.match(BLOCKELEMENTS)){
  737. return childElementsToList($selected.children(), $selected, selfTag);
  738. }else{
  739. return childElementsToList(angular.element('<div>' + selectedElement.innerHTML + '</div>'), $selected, selfTag);
  740. }
  741. }
  742. }else if(tagName.match(BLOCKELEMENTS)){
  743. // if we get here then all the contents of the ta-bind are selected
  744. _nodes = taSelection.getOnlySelectedElements();
  745. if(_nodes.length === 1 && (_nodes[0].tagName.toLowerCase() === 'ol' || _nodes[0].tagName.toLowerCase() === 'ul')){
  746. if(_nodes[0].tagName.toLowerCase() === selfTag){
  747. // remove
  748. return listToDefault(angular.element(_nodes[0]), taDefaultWrap);
  749. }else{
  750. return listToList(angular.element(_nodes[0]), selfTag);
  751. }
  752. }else{
  753. html = '';
  754. var $nodes = [];
  755. for(i = 0; i < _nodes.length; i++){
  756. /* istanbul ignore else: catch for real-world can't make it occur in testing */
  757. if(_nodes[i].nodeType !== 3){
  758. var $n = angular.element(_nodes[i]);
  759. html += '<' + taBrowserTag('li') + '>' + $n[0].innerHTML + '</' + taBrowserTag('li') + '>';
  760. $nodes.unshift($n);
  761. }
  762. }
  763. $target = angular.element('<' + selfTag + '>' + html + '</' + selfTag + '>');
  764. $nodes.pop().replaceWith($target);
  765. angular.forEach($nodes, function($node){ $node.remove(); });
  766. }
  767. taSelection.setSelectionToElementEnd($target[0]);
  768. return;
  769. }
  770. }else if(command.toLowerCase() === 'formatblock' && 'blockquote' === options.toLowerCase().replace(/[<>]/ig, '')){
  771. if(tagName === 'li') $target = $selected.parent();
  772. else $target = $selected;
  773. // find the first blockElement
  774. while(!$target[0].tagName.match(BLOCKELEMENTS)){
  775. $target = $target.parent();
  776. tagName = $target[0].tagName.toLowerCase();
  777. }
  778. if(tagName === options.toLowerCase().replace(/[<>]/ig, '')){
  779. // $target is wrap element
  780. _nodes = $target.children();
  781. var hasBlock = false;
  782. for(i = 0; i < _nodes.length; i++){
  783. hasBlock = hasBlock || _nodes[i].tagName.match(BLOCKELEMENTS);
  784. }
  785. if(hasBlock){
  786. $target.after(_nodes);
  787. next = $target.next();
  788. $target.remove();
  789. $target = next;
  790. }else{
  791. defaultWrapper.append($target[0].childNodes);
  792. $target.after(defaultWrapper);
  793. $target.remove();
  794. $target = defaultWrapper;
  795. }
  796. }else if($target.parent()[0].tagName.toLowerCase() === options.toLowerCase().replace(/[<>]/ig, '') && !$target.parent().hasClass('ta-bind')){
  797. //unwrap logic for parent
  798. var blockElement = $target.parent();
  799. var contents = blockElement.contents();
  800. for(i = 0; i < contents.length; i ++){
  801. /* istanbul ignore next: can't test - some wierd thing with how phantomjs works */
  802. if(blockElement.parent().hasClass('ta-bind') && contents[i].nodeType === 3){
  803. defaultWrapper = angular.element('<' + taDefaultWrap + '>');
  804. defaultWrapper[0].innerHTML = contents[i].outerHTML;
  805. contents[i] = defaultWrapper[0];
  806. }
  807. blockElement.parent()[0].insertBefore(contents[i], blockElement[0]);
  808. }
  809. blockElement.remove();
  810. }else if(tagName.match(LISTELEMENTS)){
  811. // wrapping a list element
  812. $target.wrap(options);
  813. }else{
  814. // default wrap behaviour
  815. _nodes = taSelection.getOnlySelectedElements();
  816. if(_nodes.length === 0) _nodes = [$target[0]];
  817. html = '';
  818. if(_nodes.length === 1 && _nodes[0].nodeType === 3){
  819. var _node = _nodes[0].parentNode;
  820. while(!_node.tagName.match(BLOCKELEMENTS)){
  821. _node = _node.parentNode;
  822. }
  823. _nodes = [_node];
  824. }
  825. for(i = 0; i < _nodes.length; i++){
  826. html += _nodes[i].outerHTML;
  827. }
  828. $target = angular.element(options);
  829. $target[0].innerHTML = html;
  830. _nodes[0].parentNode.insertBefore($target[0],_nodes[0]);
  831. angular.forEach(_nodes, function(node){ node.parentNode.removeChild(node); });
  832. }
  833. taSelection.setSelectionToElementEnd($target[0]);
  834. return;
  835. }
  836. }
  837. try{
  838. $document[0].execCommand(command, showUI, options);
  839. }catch(e){}
  840. };
  841. };
  842. }]).directive('taBind', ['taSanitize', '$timeout', '$window', '$document', 'taFixChrome', 'taBrowserTag', 'taSelection', 'taSelectableElements', 'taApplyCustomRenderers',
  843. function(taSanitize, $timeout, $window, $document, taFixChrome, taBrowserTag, taSelection, taSelectableElements, taApplyCustomRenderers){
  844. // Uses for this are textarea or input with ng-model and ta-bind='text'
  845. // OR any non-form element with contenteditable="contenteditable" ta-bind="html|text" ng-model
  846. return {
  847. require: 'ngModel',
  848. scope: {},
  849. link: function(scope, element, attrs, ngModel){
  850. // the option to use taBind on an input or textarea is required as it will sanitize all input into it correctly.
  851. var _isContentEditable = element.attr('contenteditable') !== undefined && element.attr('contenteditable');
  852. var _isInputFriendly = _isContentEditable || element[0].tagName.toLowerCase() === 'textarea' || element[0].tagName.toLowerCase() === 'input';
  853. var _isReadonly = false;
  854. var _focussed = false;
  855. // defaults to the paragraph element, but we need the line-break or it doesn't allow you to type into the empty element
  856. // non IE is '<p><br/></p>', ie is '<p></p>' as for once IE gets it correct...
  857. var _defaultVal, _defaultTest;
  858. // set the default to be a paragraph value
  859. if(attrs.taDefaultWrap === undefined) attrs.taDefaultWrap = 'p';
  860. /* istanbul ignore next: ie specific test */
  861. if(attrs.taDefaultWrap === ''){
  862. _defaultVal = '';
  863. _defaultTest = (ie === undefined)? '<div><br></div>' : (ie >= 11)? '<p><br></p>' : (ie <= 8)? '<P>&nbsp;</P>' : '<p>&nbsp;</p>';
  864. }else{
  865. _defaultVal = (ie === undefined || ie >= 11)?
  866. '<' + attrs.taDefaultWrap + '><br></' + attrs.taDefaultWrap + '>' :
  867. (ie <= 8)?
  868. '<' + attrs.taDefaultWrap.toUpperCase() + '></' + attrs.taDefaultWrap.toUpperCase() + '>' :
  869. '<' + attrs.taDefaultWrap + '></' + attrs.taDefaultWrap + '>';
  870. _defaultTest = (ie === undefined || ie >= 11)?
  871. '<' + attrs.taDefaultWrap + '><br></' + attrs.taDefaultWrap + '>' :
  872. (ie <= 8)?
  873. '<' + attrs.taDefaultWrap.toUpperCase() + '>&nbsp;</' + attrs.taDefaultWrap.toUpperCase() + '>' :
  874. '<' + attrs.taDefaultWrap + '>&nbsp;</' + attrs.taDefaultWrap + '>';
  875. }
  876. element.addClass('ta-bind');
  877. // in here we are undoing the converts used elsewhere to prevent the < > and & being displayed when they shouldn't in the code.
  878. var _compileHtml = function(){
  879. if(_isContentEditable) return element[0].innerHTML;
  880. if(_isInputFriendly) return element.val();
  881. throw ('textAngular Error: attempting to update non-editable taBind');
  882. };
  883. //used for updating when inserting wrapped elements
  884. scope.$parent['updateTaBind' + (attrs.id || '')] = function(){
  885. if(!_isReadonly) ngModel.$setViewValue(_compileHtml());
  886. };
  887. //this code is used to update the models when data is entered/deleted
  888. if(_isInputFriendly){
  889. if(!_isContentEditable){
  890. element.on('paste cut', function(){
  891. // timeout to next is needed as otherwise the paste/cut event has not finished actually changing the display
  892. if(!_isReadonly) $timeout(function(){
  893. ngModel.$setViewValue(_compileHtml());
  894. }, 0);
  895. });
  896. // if a textarea or input just add in change and blur handlers, everything else is done by angulars input directive
  897. element.on('change blur', function(){
  898. if(!_isReadonly) ngModel.$setViewValue(_compileHtml());
  899. });
  900. }else{
  901. element.on('cut', function(e){
  902. // timeout to next is needed as otherwise the paste/cut event has not finished actually changing the display
  903. if(!_isReadonly)
  904. $timeout(function(){
  905. ngModel.$setViewValue(_compileHtml());
  906. }, 0);
  907. else e.preventDefault();
  908. });
  909. element.on('paste', function(e, eventData){
  910. /* istanbul ignore else: this is for catching the jqLite testing*/
  911. if(eventData) angular.extend(e, eventData);
  912. var text;
  913. // for non-ie
  914. if(e.clipboardData || (e.originalEvent && e.originalEvent.clipboardData))
  915. text = (e.originalEvent || e).clipboardData.getData('text/plain');
  916. // for ie
  917. else if($window.clipboardData)
  918. text = $window.clipboardData.getData('Text');
  919. // if theres non text data and we aren't in read-only do default
  920. if(!text && !_isReadonly) return true;
  921. // prevent the default paste command
  922. e.preventDefault();
  923. if(!_isReadonly){
  924. var _working = angular.element('<div></div>');
  925. _working[0].innerHTML = text;
  926. // this strips out all HTML tags
  927. text = _working.text();
  928. if ($document[0].selection){
  929. var range = $document[0].selection.createRange();
  930. range.pasteHTML(text);
  931. }
  932. else{
  933. $document[0].execCommand('insertText', false, text);
  934. }
  935. ngModel.$setViewValue(_compileHtml());
  936. }
  937. });
  938. // all the code specific to contenteditable divs
  939. element.on('keyup', function(event, eventData){
  940. /* istanbul ignore else: this is for catching the jqLite testing*/
  941. if(eventData) angular.extend(event, eventData);
  942. if(!_isReadonly){
  943. // if enter - insert new taDefaultWrap, if shift+enter insert <br/>
  944. if(_defaultVal !== '' && event.keyCode === 13){
  945. if(!event.shiftKey){
  946. // new paragraph, br should be caught correctly
  947. var selection = taSelection.getSelectionElement();
  948. if(selection.tagName.toLowerCase() !== attrs.taDefaultWrap && selection.tagName.toLowerCase() !== 'li' && (selection.innerHTML.trim() === '' || selection.innerHTML.trim() === '<br>')){
  949. var _new = angular.element(_defaultVal);
  950. angular.element(selection).replaceWith(_new);
  951. taSelection.setSelectionToElementStart(_new[0]);
  952. }
  953. }
  954. }
  955. var val = _compileHtml();
  956. if(_defaultVal !== '' && val.trim() === ''){
  957. element[0].innerHTML = _defaultVal;
  958. taSelection.setSelectionToElementStart(element.children()[0]);
  959. }
  960. ngModel.$setViewValue(val);
  961. }
  962. });
  963. element.on('blur', function(){
  964. _focussed = false;
  965. var val = _compileHtml();
  966. /* istanbul ignore else: if readonly don't update model */
  967. if(!_isReadonly){
  968. if(val === _defaultTest) ngModel.$setViewValue('');
  969. else ngModel.$setViewValue(_compileHtml());
  970. }
  971. ngModel.$render();
  972. });
  973. // Placeholders not supported on ie 8 and below
  974. if(attrs.placeholder && (ie > 8 || ie === undefined)){
  975. var ruleIndex;
  976. if(attrs.id) ruleIndex = addCSSRule('#' + attrs.id + '.placeholder-text:before', 'content: "' + attrs.placeholder + '"');
  977. else throw('textAngular Error: An unique ID is required for placeholders to work');
  978. scope.$on('$destroy', function(){
  979. removeCSSRule(ruleIndex);
  980. });
  981. }
  982. element.on('focus', function(){
  983. _focussed = true;
  984. ngModel.$render();
  985. });
  986. }
  987. }
  988. // catch DOM XSS via taSanitize
  989. // Sanitizing both ways is identical
  990. var _sanitize = function(unsafe){
  991. return (ngModel.$oldViewValue = taSanitize(taFixChrome(unsafe), ngModel.$oldViewValue));
  992. };
  993. // parsers trigger from the above keyup function or any other time that the viewValue is updated and parses it for storage in the ngModel
  994. ngModel.$parsers.push(_sanitize);
  995. // because textAngular is bi-directional (which is awesome) we need to also sanitize values going in from the server
  996. ngModel.$formatters.push(_sanitize);
  997. var selectorClickHandler = function(event){
  998. // emit the element-select event, pass the element
  999. scope.$emit('ta-element-select', this);
  1000. event.preventDefault();
  1001. return false;
  1002. };
  1003. var fileDropHandler = function(event, eventData){
  1004. /