|
| 1 | +(function () { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + /** |
| 5 | + * usage: <textarea ng-model="content" redactor></textarea> |
| 6 | + * |
| 7 | + * additional options: |
| 8 | + * redactor: hash (pass in a redactor options hash) |
| 9 | + * |
| 10 | + */ |
| 11 | + |
| 12 | + var redactorOptions = {}; |
| 13 | + |
| 14 | + angular.module('angular-redactor', []) |
| 15 | + .constant('redactorOptions', redactorOptions) |
| 16 | + .directive('redactor', ['$timeout', function ($timeout) { |
| 17 | + return { |
| 18 | + restrict: 'A', |
| 19 | + require: 'ngModel', |
| 20 | + link: function (scope, element, attrs, ngModel) { |
| 21 | + |
| 22 | + // Expose scope var with loaded state of Redactor |
| 23 | + scope.redactorLoaded = false; |
| 24 | + |
| 25 | + var updateModel = function updateModel(value) { |
| 26 | + // $timeout to avoid $digest collision |
| 27 | + $timeout(function () { |
| 28 | + scope.$apply(function () { |
| 29 | + ngModel.$setViewValue(value); |
| 30 | + }); |
| 31 | + }); |
| 32 | + }, |
| 33 | + options = { |
| 34 | + changeCallback: updateModel |
| 35 | + }, |
| 36 | + additionalOptions = attrs.redactor ? |
| 37 | + scope.$eval(attrs.redactor) : {}, |
| 38 | + editor, |
| 39 | + $_element = angular.element(element); |
| 40 | + |
| 41 | + angular.extend(options, redactorOptions, additionalOptions); |
| 42 | + |
| 43 | + // put in timeout to avoid $digest collision. call render() to |
| 44 | + // set the initial value. |
| 45 | + $timeout(function () { |
| 46 | + editor = $_element.redactor(options); |
| 47 | + ngModel.$render(); |
| 48 | + }); |
| 49 | + |
| 50 | + ngModel.$render = function () { |
| 51 | + if (angular.isDefined(editor)) { |
| 52 | + $timeout(function() { |
| 53 | + $_element.redactor('set', ngModel.$viewValue || ''); |
| 54 | + scope.redactorLoaded = true; |
| 55 | + }); |
| 56 | + } |
| 57 | + }; |
| 58 | + } |
| 59 | + }; |
| 60 | + }]); |
| 61 | +})(); |
| 62 | + |
0 commit comments