forked from TylerGarlick/angular-redactor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathangular-redactor.js
85 lines (73 loc) · 3.36 KB
/
angular-redactor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
(function() {
'use strict';
/**
* usage: <textarea ng-model="content" redactor></textarea>
*
* additional options:
* redactor: hash (pass in a redactor options hash)
*
*/
var redactorOptions = {};
angular.module('angular-redactor', [])
.constant('redactorOptions', redactorOptions)
.directive('redactor', ['$timeout', function($timeout) {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attrs, ngModel) {
// Expose scope var with loaded state of Redactor
scope.redactorLoaded = false;
var updateModel = function updateModel(value) {
// $timeout to avoid $digest collision
// for firefox
var tempvalue = value.replace( /\s/g, '' );
if ( tempvalue === '<p><br></p>' || value.trim() === '' ) {
value = '';
element.closest( '.redactor-box' ).css( 'border' , '1px solid red' );
} else {
element.closest( '.redactor-box' ).css( 'border' , 'none' );
}
$timeout(function() {
scope.$apply(function() {
ngModel.$setViewValue(value);
});
});
},
options = {
changeCallback: updateModel
},
additionalOptions = attrs.redactor ?
scope.$eval(attrs.redactor) : {},
editor;
angular.extend(options, redactorOptions, additionalOptions);
// prevent collision with the constant values on ChangeCallback
var changeCallback = additionalOptions.changeCallback || redactorOptions.changeCallback;
if (changeCallback) {
options.changeCallback = function(value) {
updateModel.call(this, value);
changeCallback.call(this, value);
}
}
// put in timeout to avoid $digest collision. call render() to
// set the initial value.
$timeout(function() {
editor = element.redactor(options);
ngModel.$render();
element.on('remove',function(){
element.off('remove');
element.redactor('core.destroy');
});
});
ngModel.$render = function() {
if(angular.isDefined(editor)) {
$timeout(function() {
element.redactor('code.set', ngModel.$viewValue || '');
element.redactor('placeholder.toggle');
scope.redactorLoaded = true;
});
}
};
}
};
}]);
})();