-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbootstrap-ckeditor.js
201 lines (183 loc) · 7.12 KB
/
bootstrap-ckeditor.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
angular.module("schemaForm").run([
"$templateCache",
function ($templateCache) {
$templateCache.put(
"directives/decorators/bootstrap/ckeditor/ckeditor.html",
'<div class="form-group" ng-class="{\'has-error\': hasError()}">\r\n <label class="control-label" ng-show="showTitle()">{{form.title}}</label>\r\n\r\n <textarea ng-show="form.key"\r\n style="background-color: white"\r\n type="text"\r\n class="form-control"\r\n schema-validate="form"\r\n ng-model="$$value$$"\r\n ckeditor="form.ckeditor"></textarea>\r\n\r\n <span class="help-block">{{ (hasError() && errorMessage(schemaError())) || form.description}}</span>\r\n</div>'
);
},
]);
angular.module("schemaForm").config([
"schemaFormProvider",
"schemaFormDecoratorsProvider",
"sfPathProvider",
function (schemaFormProvider, schemaFormDecoratorsProvider, sfPathProvider) {
var ckeditor = function (name, schema, options) {
if (schema.type === "string" && schema.format === "ckeditor") {
var f = schemaFormProvider.stdFormObj(name, schema, options);
f.key = options.path;
f.type = "ckeditor";
options.lookup[sfPathProvider.stringify(options.path)] = f;
return f;
}
};
schemaFormProvider.defaults.string.unshift(ckeditor);
//Add to the bootstrap directive
schemaFormDecoratorsProvider.addMapping(
"bootstrapDecorator",
"ckeditor",
"directives/decorators/bootstrap/ckeditor/ckeditor.html"
);
schemaFormDecoratorsProvider.createDirective(
"ckeditor",
"directives/decorators/bootstrap/ckeditor/ckeditor.html"
);
},
]);
(function (angular, factory) {
if (typeof define === "function" && define.amd) {
define(["angular", "ckeditor"], function (angular) {
return factory(angular);
});
} else {
return factory(angular);
}
})(angular || null, function (angular) {
var app = angular.module("schemaForm");
var $defer,
loaded = false;
app.run([
"$q",
"$timeout",
function ($q, $timeout) {
$defer = $q.defer();
if (angular.isUndefined(CKEDITOR)) {
throw new Error("CKEDITOR not found");
}
CKEDITOR.disableAutoInline = true;
function checkLoaded() {
if (CKEDITOR.status == "loaded") {
loaded = true;
$defer.resolve();
} else {
checkLoaded();
}
}
CKEDITOR.on("loaded", checkLoaded);
$timeout(checkLoaded, 100);
},
]);
app.directive("ckeditor", [
"$timeout",
"$q",
function ($timeout, $q) {
"use strict";
return {
restrict: "AC",
require: ["ngModel", "^?form"],
scope: false,
link: function (scope, element, attrs, ctrls) {
var ngModel = ctrls[0];
var form = ctrls[1] || null;
var EMPTY_HTML = "<p></p>",
isTextarea = element[0].tagName.toLowerCase() == "textarea",
data = [],
isReady = false;
if (!isTextarea) {
element.attr("contenteditable", true);
}
var onLoad = function () {
var options = {
toolbar: "full",
// toolbar_full: [
// { name: 'basicstyles',
// items: [ 'Bold', 'Italic', 'Strike', 'Underline' ] },
// { name: 'paragraph', items: [ 'BulletedList', 'NumberedList', 'Blockquote' ] },
// { name: 'editing', items: ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock' ] },
// { name: 'links', items: [ 'Link', 'Unlink', 'Anchor' ] },
// { name: 'tools', items: [ 'SpellChecker', 'Maximize' ] },
// '/',
// { name: 'styles', items: [ 'Format', 'FontSize', 'TextColor', 'PasteText', 'PasteFromWord', 'RemoveFormat' ] },
// { name: 'insert', items: [ 'Image', 'Table', 'SpecialChar' ] },
// { name: 'forms', items: [ 'Outdent', 'Indent' ] },
// { name: 'clipboard', items: [ 'Undo', 'Redo' ] },
// { name: 'document', items: [ 'PageBreak', 'Source' ] }
// ],
disableNativeSpellChecker: false,
uiColor: "#FAFAFA",
height: "400px",
width: "100%",
};
if (attrs.ckeditor) {
var customOptions = scope.$eval(attrs.ckeditor);
options = angular.extend(options, customOptions);
}
var instance = CKEDITOR.dom.element.get(element[0]).getEditor()
? CKEDITOR.dom.element.get(element[0]).getEditor()
: isTextarea
? CKEDITOR.replace(element[0], options)
: CKEDITOR.inline(element[0], options),
configLoaderDef = $q.defer();
element.bind("$destroy", function () {
instance.destroy(
false //If the instance is replacing a DOM element, this parameter indicates whether or not to update the element with the instance contents.
);
});
var setModelData = function (setPristine) {
var data = instance.getData();
if (data == "") {
data = null;
}
$timeout(function () {
// for key up event
(setPristine !== true || data != ngModel.$viewValue) &&
ngModel.$setViewValue(data);
setPristine === true && form && form.$setPristine();
}, 0);
},
onUpdateModelData = function (setPristine) {
if (!data.length) {
return;
}
var item = data.pop() || EMPTY_HTML;
isReady = false;
instance.setData(item, function () {
setModelData(setPristine);
isReady = true;
});
};
//instance.on('pasteState', setModelData);
instance.on("change", setModelData);
instance.on("blur", setModelData);
//instance.on('key', setModelData); // for source view
instance.on("instanceReady", function () {
scope.$broadcast("ckeditor.ready");
scope.$apply(function () {
onUpdateModelData(true);
});
instance.document.on("keyup", setModelData);
});
instance.on("customConfigLoaded", function () {
configLoaderDef.resolve();
});
ngModel.$render = function () {
data.push(ngModel.$viewValue);
if (isReady) {
onUpdateModelData();
}
};
};
if (CKEDITOR.status == "loaded") {
loaded = true;
}
if (loaded) {
onLoad();
} else {
$defer.promise.then(onLoad);
}
},
};
},
]);
return app;
});