acha-framework
Version:
is a modular framework on both client (angular.js) and server (node.js) side, it provides security, orm, ioc, obfuscation and ...
110 lines • 4.04 kB
JavaScript
(function ($, angular, underscore, window, document, undefined) {
'use strict';
angular.module('frontend.directives').factory('editorService', [
'$q',
function ($q) {
return function () {
return $q(function (resolve, reject) {
if (window.CKEDITOR) {
resolve();
return;
}
$script('/framework/vendor/ckeditor/ckeditor.js', function () {
resolve();
});
});
};
}
]).directive('editor', [
'editorService',
'commonService',
'modalService',
'fileService',
function (editorService, commonService, modalService, fileService) {
var SETUP_DONE = false;
return {
restrict: 'E',
replace: true,
scope: {
tag: '<?',
disabled: '=?',
visible: '=?',
cssClass: '=?',
instance: '=?',
model: '=?'
},
templateUrl: '/templates/framework/directives/editor/template.html',
link: function (scope, element, attr) {
scope.vm = { uuid: commonService.uuid() };
scope.vm.init = function () {
if (angular.isUndefined(scope.disabled)) {
scope.disabled = false;
}
if (angular.isUndefined(scope.visible)) {
scope.visible = true;
}
if (angular.isUndefined(scope.cssClass)) {
scope.cssClass = '';
}
scope.vm.bind();
};
scope.vm.setup = function () {
if (SETUP_DONE)
return;
window.CKEDITOR.on('dialogDefinition', function (ev) {
var dialogName = ev.data.name;
var dialogDefinition = ev.data.definition;
if (dialogName === 'image') {
for (var i = 0; i < dialogDefinition.contents.length; i++) {
var browseButton = dialogDefinition.contents[i].get('browse');
if (browseButton !== null) {
browseButton.hidden = false;
browseButton.onClick = function (dialog, i) {
modalService.dialog({
titleTranslate: 'editor.fileManager',
template: '<file-manager multiple-selection="false" model="vm.selectedFiles"></file-manager>',
data: {},
onAccept: function (modal_scope) {
return commonService.$q(function (resolve, reject) {
if (!modal_scope.vm.selectedFiles || !modal_scope.vm.selectedFiles.length) {
reject();
return;
}
if (!fileService.isImage(modal_scope.vm.selectedFiles[0].path)) {
reject();
return;
}
dialog.data.dialog.getContentElement('info', 'txtUrl').setValue(modal_scope.vm.selectedFiles[0].path);
resolve();
});
}
});
};
}
}
}
});
SETUP_DONE = true;
};
scope.vm.bind = function () {
editorService().then(function () {
scope.vm.setup();
scope.instance = window.CKEDITOR.replace(scope.vm.uuid, { language: commonService.culture.lang });
scope.instance.on('change', function () {
scope.$apply(function () {
scope.model = scope.instance.getData();
});
});
});
scope.$on('$destroy', function () {
if (scope.instance) {
scope.instance.destroy();
}
});
};
scope.vm.init();
}
};
}
]);
}(jQuery, angular, _, window, document));