indvd00m@0: CKEDITOR.plugins.add('crossreference', {
indvd00m@0: lang : [ 'en', 'ru' ],
indvd00m@0: requires : 'dialog,notification',
indvd00m@4: icons : 'crossreference,crossreference-anchor,crossreference-link,crossreference-update',
indvd00m@0: hidpi : true,
indvd00m@0: init : function(editor) {
indvd00m@0:
indvd00m@0: // config
indvd00m@0:
indvd00m@0: var config = getConfig();
indvd00m@0: editor.config.crossreference = config;
indvd00m@0:
indvd00m@0: // plugin
indvd00m@0:
indvd00m@0: var anchorAllowedContent = 'a[!cross-reference,cross-anchor,cross-guid,cross-name,cross-number]{*}(cross-reference,cross-anchor)';
indvd00m@0: var anchorRequiredContent = 'a[cross-reference,cross-anchor]';
indvd00m@0: var linkAllowedContent = 'a[!cross-reference,cross-link,cross-guid,cross-name,cross-number]{*}(cross-reference,cross-link)';
indvd00m@0: var linkRequiredContent = 'a[cross-reference,cross-link]';
indvd00m@0: editor.addFeature({
indvd00m@0: name: 'crossreference-anchor',
indvd00m@0: allowedContent: anchorAllowedContent,
indvd00m@0: requiredContent: anchorRequiredContent
indvd00m@0: });
indvd00m@0: editor.addFeature({
indvd00m@0: name: 'crossreference-link',
indvd00m@0: allowedContent: linkAllowedContent,
indvd00m@0: requiredContent: linkRequiredContent
indvd00m@0: });
indvd00m@0: editor.ui.add('crossreference', CKEDITOR.UI_MENUBUTTON, {
indvd00m@0: label : editor.lang.crossreference.name,
indvd00m@0: modes: {
indvd00m@0: wysiwyg: 1,
indvd00m@0: source: 1
indvd00m@0: },
indvd00m@0: toolbar : 'insert',
indvd00m@0: onMenu: function() {
indvd00m@0: var selectedElement = null;
indvd00m@0:
indvd00m@0: var selection = editor.getSelection();
indvd00m@0: if (selection) {
indvd00m@0: var element = selection.getStartElement();
indvd00m@0: if (element)
indvd00m@0: element = element.getAscendant('a', true);
indvd00m@0: if (element && element.hasAttribute('cross-reference')) {
indvd00m@0: selectedElement = element;
indvd00m@0: }
indvd00m@0: }
indvd00m@0:
indvd00m@0: var state = getMenuState(selectedElement, true);
indvd00m@0: return state;
indvd00m@0: }
indvd00m@0: });
indvd00m@0:
indvd00m@0: // dialogs
indvd00m@0:
indvd00m@0: var updateCmdName = 'update-crossreferences';
indvd00m@0: var anchorDialogCmdName = 'crossreference-anchor-dialog';
indvd00m@0: var linkDialogCmdName = 'crossreference-link-dialog';
indvd00m@0:
indvd00m@0: CKEDITOR.dialog.add(anchorDialogCmdName, this.path + 'dialogs/crossreference-anchor.js');
indvd00m@0: CKEDITOR.dialog.add(linkDialogCmdName, this.path + 'dialogs/crossreference-link.js');
indvd00m@0:
indvd00m@0: editor.addCommand(anchorDialogCmdName, new CKEDITOR.dialogCommand(anchorDialogCmdName, {
indvd00m@0: allowedContent: anchorAllowedContent,
indvd00m@0: requiredContent: anchorRequiredContent
indvd00m@0: }));
indvd00m@0: editor.addCommand(linkDialogCmdName, new CKEDITOR.dialogCommand(linkDialogCmdName, {
indvd00m@0: allowedContent: linkAllowedContent,
indvd00m@0: requiredContent: linkRequiredContent
indvd00m@0: }));
indvd00m@0:
indvd00m@0: // commands
indvd00m@0:
indvd00m@0: editor.addCommand(updateCmdName, {
indvd00m@0: async: true,
indvd00m@0: contextSensitive: false,
indvd00m@0: editorFocus: false,
indvd00m@0: modes: {
indvd00m@0: wysiwyg: 1,
indvd00m@0: source: 1
indvd00m@0: },
indvd00m@0: readOnly: true,
indvd00m@0: exec: function(editor) {
indvd00m@0: editor.setReadOnly(true);
indvd00m@0: var notification = editor.showNotification(editor.lang.crossreference.updatingCrossReferences, 'progress', 0);
indvd00m@0:
indvd00m@0: var cmd = this;
indvd00m@0:
indvd00m@0: var typesCount = 0;
indvd00m@0: var processedTypesCount = 0;
indvd00m@0: for (var typeName in config.types) {
indvd00m@0: typesCount++;
indvd00m@0: }
indvd00m@0: var linksCount = 0;
indvd00m@0:
indvd00m@0: var html = null;
indvd00m@0: if (editor.mode == 'source')
indvd00m@0: html = $('
' + editor.getData() + '
');
indvd00m@0: else
indvd00m@0: html = $(editor.editable().$);
indvd00m@0:
indvd00m@0: function finishCommand() {
indvd00m@0: editor.setReadOnly(false);
indvd00m@0: editor.fire('afterCommandExec', {
indvd00m@0: name: updateCmdName,
indvd00m@0: command: cmd
indvd00m@0: });
indvd00m@0: notification.update({
indvd00m@0: type: 'success',
indvd00m@0: message: editor.lang.crossreference.updatedCrossReferences + linksCount,
indvd00m@0: important: true
indvd00m@0: });
indvd00m@0: }
indvd00m@0:
indvd00m@0: if (typesCount == 0) {
indvd00m@0: finishCommand();
indvd00m@0: return;
indvd00m@0: }
indvd00m@0:
indvd00m@0: for (var typeName in config.types) {
indvd00m@0: config.findAnchors(config, editor, config.types[typeName], function(anchors) {
indvd00m@0: notification.update({
indvd00m@0: progress: (1 / typesCount) * processedTypesCount
indvd00m@0: });
indvd00m@0: for (var i = 0; i < anchors.length; i++) {
indvd00m@0: var anchor = anchors[i];
indvd00m@0: var type = config.types[anchor.type];
indvd00m@0:
indvd00m@0: notification.update({
indvd00m@0: progress: (1 / typesCount) * processedTypesCount + (1 / typesCount / anchors.length) * i
indvd00m@0: });
indvd00m@0:
indvd00m@0: var aName = type.type + '-' + anchor.guid;
indvd00m@0:
indvd00m@0: var anchorElement = $('a[cross-reference="' + type.type + '"][cross-anchor][cross-guid="' + anchor.guid + '"]', html);
indvd00m@0: if (anchorElement.length > 0) {
indvd00m@0: anchorElement.attr('cross-reference', type.type);
indvd00m@0: anchorElement.attr('cross-anchor', '');
indvd00m@0: anchorElement.attr('cross-guid', anchor.guid);
indvd00m@0: anchorElement.attr('cross-name', anchor.name);
indvd00m@0: anchorElement.attr('cross-number', anchor.number);
indvd00m@0: anchorElement.attr('name', aName);
indvd00m@0: if (!anchorElement.hasClass('cross-reference'))
indvd00m@0: anchorElement.addClass('cross-reference');
indvd00m@0: if (!anchorElement.hasClass('cross-anchor'))
indvd00m@0: anchorElement.addClass('cross-anchor');
indvd00m@0:
indvd00m@0: anchorElement.removeAttr('cross-link');
indvd00m@0: anchorElement.removeClass('cross-link');
indvd00m@0:
indvd00m@0: anchorElement.text(anchor.text);
indvd00m@0: }
indvd00m@0:
indvd00m@0: $('a[cross-reference="' + type.type + '"][cross-link][cross-guid="' + anchor.guid + '"]', html).each(function() {
indvd00m@0: var linkElement = $(this);
indvd00m@0:
indvd00m@0: linkElement.attr('cross-reference', type.type);
indvd00m@0: linkElement.attr('cross-link', '');
indvd00m@0: linkElement.attr('cross-guid', anchor.guid);
indvd00m@0: linkElement.attr('cross-name', anchor.name);
indvd00m@0: linkElement.attr('cross-number', anchor.number);
indvd00m@0: linkElement.attr('href', '#' + aName);
indvd00m@0:
indvd00m@0: if (!linkElement.hasClass('cross-reference'))
indvd00m@0: linkElement.addClass('cross-reference');
indvd00m@0: if (!linkElement.hasClass('cross-link'))
indvd00m@0: linkElement.addClass('cross-link');
indvd00m@0:
indvd00m@0: linkElement.removeAttr('cross-anchor');
indvd00m@0: linkElement.removeClass('cross-anchor');
indvd00m@0:
indvd00m@0: var linkText = anchor.text;
indvd00m@0: if (type.linkTextTemplate)
indvd00m@0: linkText = config.formatText(type.linkTextTemplate, anchor);
indvd00m@0: linkElement.text(linkText);
indvd00m@0: linkElement.attr('title', anchor.text.replace(/ /g, ' ').trim());
indvd00m@0:
indvd00m@0: linksCount++;
indvd00m@0: });
indvd00m@0: }
indvd00m@0: processedTypesCount++;
indvd00m@0: if (processedTypesCount >= typesCount) {
indvd00m@0: // done
indvd00m@0: if (editor.mode == 'source')
indvd00m@0: editor.setData(html.html());
indvd00m@0: finishCommand();
indvd00m@0: }
indvd00m@0: });
indvd00m@0: }
indvd00m@0: }
indvd00m@0: });
indvd00m@0: editor.on('doubleclick', function(evt) {
indvd00m@0: if (evt.data.element && !evt.data.element.isReadOnly() && evt.data.element.getName() === 'a'
indvd00m@0: && evt.data.element.hasAttribute('cross-reference')) {
indvd00m@0: editor.getSelection().selectElement(evt.data.element);
indvd00m@0: if (evt.data.element.hasAttribute('cross-anchor')) {
indvd00m@0: evt.data.dialog = anchorDialogCmdName;
indvd00m@0: } else if (evt.data.element.hasAttribute('cross-link')) {
indvd00m@0: evt.data.dialog = linkDialogCmdName;
indvd00m@0: }
indvd00m@0: }
indvd00m@0: });
indvd00m@0:
indvd00m@0: // menu
indvd00m@0:
indvd00m@0: var updateMenuItemName = 'updateCrossReferences';
indvd00m@0: var setAnchorMenuItemName = 'setCrossReferenceAnchor';
indvd00m@0: var setLinkMenuItemName = 'setCrossReferenceLink';
indvd00m@0:
indvd00m@0: var getMenuState = function(element, alwaysAllowEditItems) {
indvd00m@0: var items = {};
indvd00m@0: items[updateMenuItemName] = CKEDITOR.TRISTATE_OFF;
indvd00m@0: if (alwaysAllowEditItems == true) {
indvd00m@0: items[setAnchorMenuItemName] = CKEDITOR.TRISTATE_OFF;
indvd00m@0: items[setLinkMenuItemName] = CKEDITOR.TRISTATE_OFF;
indvd00m@0: }
indvd00m@0: if (element && element.getName() === 'a' && element.hasAttribute('cross-reference')) {
indvd00m@0: items[setAnchorMenuItemName] = CKEDITOR.TRISTATE_OFF;
indvd00m@0: items[setLinkMenuItemName] = CKEDITOR.TRISTATE_OFF;
indvd00m@0: if (element.hasAttribute('cross-anchor')) {
indvd00m@0: items[setAnchorMenuItemName] = CKEDITOR.TRISTATE_ON;
indvd00m@0: items[setLinkMenuItemName] = CKEDITOR.TRISTATE_DISABLED;
indvd00m@0: }
indvd00m@0: if (element.hasAttribute('cross-link')) {
indvd00m@0: items[setAnchorMenuItemName] = CKEDITOR.TRISTATE_DISABLED;
indvd00m@0: items[setLinkMenuItemName] = CKEDITOR.TRISTATE_ON;
indvd00m@0: }
indvd00m@0: }
indvd00m@0: return items;
indvd00m@0: }
indvd00m@0: if (editor.addMenuItem) {
indvd00m@0: editor.addMenuGroup('crossreferenceGroup');
indvd00m@0: editor.addMenuItem(updateMenuItemName, {
indvd00m@0: label : editor.lang.crossreference.updateCrossReferences,
indvd00m@0: command : updateCmdName,
indvd00m@4: icon: 'crossreference-update',
indvd00m@0: group : 'crossreferenceGroup'
indvd00m@0: });
indvd00m@0: editor.addMenuItem(setAnchorMenuItemName, {
indvd00m@0: label : editor.lang.crossreference.setCrossReferenceAnchor,
indvd00m@0: command : anchorDialogCmdName,
indvd00m@4: icon: 'crossreference-anchor',
indvd00m@0: group : 'crossreferenceGroup'
indvd00m@0: });
indvd00m@0: editor.addMenuItem(setLinkMenuItemName, {
indvd00m@0: label : editor.lang.crossreference.setCrossReferenceLink,
indvd00m@0: command : linkDialogCmdName,
indvd00m@4: icon: 'crossreference-link',
indvd00m@0: group : 'crossreferenceGroup'
indvd00m@0: });
indvd00m@0: }
indvd00m@0: if (editor.contextMenu) {
indvd00m@0: editor.contextMenu.addListener(function(element, selection) {
indvd00m@0: if (element.getName() === 'a' && element.hasAttribute('cross-reference')) {
indvd00m@0: selection.selectElement(element);
indvd00m@0: }
indvd00m@0: var state = getMenuState(element, false);
indvd00m@0: return state;
indvd00m@0: });
indvd00m@0: }
indvd00m@0:
indvd00m@0: function getConfig() {
indvd00m@0: var defaultConfig = {
indvd00m@0: activeTypes: ['chapter', 'image', 'table', 'reference'],
indvd00m@0: overrideTypes: false,
indvd00m@0: types: {}
indvd00m@0: };
indvd00m@0: defaultConfig.types.chapter = {
indvd00m@0: name: editor.lang.crossreference.chapter,
indvd00m@0: anchorTextTemplate: '${number}. ${name}.',
indvd00m@0: linkTextTemplate: '${number}',
indvd00m@0: numeration: {
indvd00m@0: enabled: true,
indvd00m@0: firstNumber: '1',
indvd00m@0: increase: function(number) {
indvd00m@0: var n = parseInt(number);
indvd00m@0: return ++n;
indvd00m@0: }
indvd00m@0: },
indvd00m@0: anchorsProvider: 'default',
indvd00m@0: allowCreateAnchors: true,
indvd00m@0: groupAnchors: false
indvd00m@0: };
indvd00m@0: defaultConfig.types.image = {
indvd00m@0: name: editor.lang.crossreference.figure,
indvd00m@0: anchorTextTemplate: editor.lang.crossreference.fig + ' ${number}. ${name}.',
indvd00m@0: linkTextTemplate: '${number}',
indvd00m@0: numeration: {
indvd00m@0: enabled: true,
indvd00m@0: firstNumber: '1',
indvd00m@0: increase: function(number) {
indvd00m@0: var n = parseInt(number);
indvd00m@0: return ++n;
indvd00m@0: }
indvd00m@0: },
indvd00m@0: anchorsProvider: 'default',
indvd00m@0: allowCreateAnchors: true,
indvd00m@0: groupAnchors: false
indvd00m@0: };
indvd00m@0: defaultConfig.types.table = {
indvd00m@0: name: editor.lang.crossreference.table,
indvd00m@0: anchorTextTemplate: editor.lang.crossreference.table + ' ${number}. ${name}.',
indvd00m@0: linkTextTemplate: '${number}',
indvd00m@0: numeration: {
indvd00m@0: enabled: true,
indvd00m@0: firstNumber: '1',
indvd00m@0: increase: function(number) {
indvd00m@0: var n = parseInt(number);
indvd00m@0: return ++n;
indvd00m@0: }
indvd00m@0: },
indvd00m@0: anchorsProvider: 'default',
indvd00m@0: allowCreateAnchors: true,
indvd00m@0: groupAnchors: false
indvd00m@0: };
indvd00m@0: defaultConfig.types.reference = {
indvd00m@0: name: editor.lang.crossreference.reference,
indvd00m@0: anchorTextTemplate: '[${number}] ${name}.',
indvd00m@0: linkTextTemplate: '[${number}]',
indvd00m@0: numeration: {
indvd00m@0: enabled: true,
indvd00m@0: firstNumber: '1',
indvd00m@0: increase: function(number) {
indvd00m@0: var n = parseInt(number);
indvd00m@0: return ++n;
indvd00m@0: }
indvd00m@0: },
indvd00m@0: anchorsProvider: 'default',
indvd00m@0: allowCreateAnchors: true,
indvd00m@0: groupAnchors: false
indvd00m@0: };
indvd00m@0:
indvd00m@0: var config = CKEDITOR.tools.clone(defaultConfig);
indvd00m@0: if (editor.config.crossreference) {
indvd00m@0: config = CKEDITOR.tools.extend(config, editor.config.crossreference, true);
indvd00m@0: if (!config.overrideTypes) {
indvd00m@0: for (var typeName in defaultConfig.types) {
indvd00m@0: var type = defaultConfig.types[typeName];
indvd00m@0: if (!(typeName in config.types))
indvd00m@0: config.types[typeName] = type;
indvd00m@0: }
indvd00m@0: }
indvd00m@0: }
indvd00m@0: for (var typeName in config.types) {
indvd00m@0: var type = config.types[typeName];
indvd00m@0: type.type = typeName;
indvd00m@0: }
indvd00m@0: for (var typeName in config.types) {
indvd00m@0: if ($.inArray(typeName, config.activeTypes) == -1) {
indvd00m@0: delete config.types[typeName];
indvd00m@0: }
indvd00m@0: }
indvd00m@0:
indvd00m@0: // shared methods
indvd00m@0:
indvd00m@0: config.findAnchors = function(config, editor, type, callback) {
indvd00m@0: var anchors = [];
indvd00m@0:
indvd00m@0: if (type == null) {
indvd00m@0: callback(anchors);
indvd00m@0: return;
indvd00m@0: }
indvd00m@0:
indvd00m@0: var number = null;
indvd00m@0: if (type.numeration && type.numeration.enabled)
indvd00m@0: number = type.numeration.firstNumber + '';
indvd00m@0:
indvd00m@0: var html = null;
indvd00m@0: if (editor.mode == 'source')
indvd00m@0: html = $('' + editor.getData() + '
');
indvd00m@0: else
indvd00m@0: html = $(editor.editable().$);
indvd00m@0:
indvd00m@0: $('a[cross-reference="' + type.type + '"][cross-anchor]', html).each(function() {
indvd00m@0: var element = $(this);
indvd00m@0: var anchor = {
indvd00m@0: type: element.attr('cross-reference'),
indvd00m@0: guid: element.attr('cross-guid'),
indvd00m@0: name: element.attr('cross-name'),
indvd00m@0: number: number,
indvd00m@0: text: element.text()
indvd00m@0: }
indvd00m@0: anchors.push(anchor);
indvd00m@0: if (type.numeration && type.numeration.enabled)
indvd00m@0: number = type.numeration.increase(number);
indvd00m@0: });
indvd00m@0:
indvd00m@0: function postProcessAnchors(anchors) {
indvd00m@0: for(var i = 0; i < anchors.length; i++) {
indvd00m@0: var anchor = anchors[i];
indvd00m@0:
indvd00m@0: if (anchor.type != type.type)
indvd00m@0: throw 'Incompatible type: ' + type.type;
indvd00m@0:
indvd00m@0: var text = anchor.name;
indvd00m@0: if (type.anchorTextTemplate) {
indvd00m@0: text = config.formatText(type.anchorTextTemplate, anchor);
indvd00m@0: }
indvd00m@0: anchor.text = text;
indvd00m@0: }
indvd00m@0: callback(anchors);
indvd00m@0: }
indvd00m@0:
indvd00m@0: if (type.anchorsProvider && type.anchorsProvider !== 'default') {
indvd00m@0: type.anchorsProvider(postProcessAnchors, anchors, type, editor);
indvd00m@0: } else {
indvd00m@0: postProcessAnchors(anchors);
indvd00m@0: }
indvd00m@0: };
indvd00m@0:
indvd00m@0: config.formatText = function(template, anchor) {
indvd00m@0: var text = template;
indvd00m@0:
indvd00m@0: for (var propName in anchor) {
indvd00m@0: var propValue = anchor[propName];
indvd00m@0: var regexp = new RegExp('\\$\\{' + propName + '\\}', 'g');
indvd00m@0: if (propValue)
indvd00m@0: text = text.replace(regexp, propValue);
indvd00m@0: else
indvd00m@0: text = text.replace(regexp, '');
indvd00m@0: }
indvd00m@0:
indvd00m@0: if (anchor.level != null) {
indvd00m@0: var shift = '';
indvd00m@0: for (var i = 0; i < anchor.level; i++)
indvd00m@0: shift += ' ';
indvd00m@0:
indvd00m@0: text = text.replace(/\$\{levelShift\}/g, shift);
indvd00m@0: }
indvd00m@0:
indvd00m@2: text = text.replace(/\s+/g, ' ');
indvd00m@0: text = text.trim();
indvd00m@0:
indvd00m@0: return text;
indvd00m@0: }
indvd00m@0:
indvd00m@0: return config;
indvd00m@0: }
indvd00m@0: }
indvd00m@0: });