1 CKEDITOR.dialog.add('crossreference-link-dialog', function(editor) {
3 var config = editor.config.crossreference;
5 var updateAnchors = function(dialog) {
6 dialog.setState(CKEDITOR.DIALOG_STATE_BUSY);
9 dialog.anchorsByGuid = {};
10 dialog.anchorsGroupNameByGuid = {};
13 var typeName = dialog.getValueOf('tab-main', 'type');
15 type = config.types[typeName];
17 var anchorSelect = dialog.getContentElement('tab-main', 'anchor');
19 anchorSelect.add('', '');
21 var anchorGroupSelect = dialog.getContentElement('tab-main', 'anchorGroup');
22 anchorGroupSelect.clear();
23 anchorGroupSelect.add('', '');
24 if (type && type.groupAnchors == true)
25 anchorGroupSelect.getElement().show();
27 anchorGroupSelect.getElement().hide();
29 config.findAnchors(config, editor, type, function(anchors) {
30 dialog.anchors = anchors;
31 dialog.anchorsByGuid = {};
32 dialog.anchorsGroupNameByGuid = {};
34 for (var i = 0; i < anchors.length; i++) {
35 var anchor = anchors[i];
36 var guid = anchor.guid;
37 var label = anchor.text;
38 anchorSelect.add(label, guid);
40 if (type.groupAnchors == true && anchor.groupGuid) {
41 dialog.anchorsGroupNameByGuid[anchor.groupGuid] = anchor.groupName;
43 dialog.anchorsByGuid[guid] = anchor;
46 if (type && type.groupAnchors == true) {
47 for (var groupGuid in dialog.anchorsGroupNameByGuid) {
48 var groupName = dialog.anchorsGroupNameByGuid[groupGuid];
49 anchorGroupSelect.add(groupName, groupGuid);
53 // fix and others
54 $('option', anchorSelect.getInputElement().$).each(function() {
56 var text = option.text();
60 if (dialog.insertMode)
61 filterAnchors(dialog);
63 anchorSelect.setup(dialog.element);
65 dialog.setState(CKEDITOR.DIALOG_STATE_IDLE);
69 var filterAnchors = function(dialog) {
70 var filter = dialog.getValueOf('tab-main', 'filter');
72 filter = filter.trim().toLowerCase();
77 var typeName = dialog.getValueOf('tab-main', 'type');
79 type = config.types[typeName];
82 if (type && type.groupAnchors == true)
83 groupGuid = dialog.getValueOf('tab-main', 'anchorGroup');
85 var anchorSelect = dialog.getContentElement('tab-main', 'anchor');
86 var prevValue = anchorSelect.getValue();
88 $('option', anchorSelect.getInputElement().$).each(function() {
90 var guid = option.val();
91 var anchor = dialog.anchorsByGuid[guid];
92 var text = option.text();
94 var matches = filter.length == 0 || text.toLowerCase().indexOf(filter) != -1;
96 matches = matches && anchor && anchor.groupGuid == groupGuid;
99 option.removeAttr('disabled', 'disabled');
100 if (selected == null)
101 selected = option.val();
102 else if (guid == prevValue)
103 selected = option.val();
105 option.attr('disabled', 'disabled');
108 anchorSelect.setValue(selected);
112 title : editor.lang.crossreference.link,
119 label : editor.lang.crossreference.link,
128 html : '<div style="white-space: normal; text-align: justify;">' + editor.lang.crossreference.linkDescription + '</div>',
134 label : editor.lang.crossreference.anchorType,
138 this.getInputElement().setStyle('width', '100%');
139 for (var typeName in config.types) {
140 var type = config.types[typeName];
141 var label = type.name;
142 this.add(label, type.type);
145 onChange: function() {
146 updateAnchors(this.getDialog());
148 setup: function(element) {
149 this.setValue(element.getAttribute('cross-reference'));
151 commit: function(element) {
152 element.setAttribute('cross-reference', this.getValue());
153 element.setAttribute('cross-link', '');
160 label : editor.lang.crossreference.anchorGroup,
164 this.getInputElement().setStyle('width', '100%');
166 onChange: function() {
167 filterAnchors(this.getDialog());
174 label : editor.lang.crossreference.linkFilter,
175 onKeyUp : function() {
176 filterAnchors(this.getDialog());
183 label : editor.lang.crossreference.anchor,
187 this.getInputElement().setStyle('width', '100%');
189 setup: function(element) {
190 this.setValue(element.getAttribute('cross-guid'));
192 commit: function(element) {
193 element.setAttribute('cross-guid', this.getValue());
202 onLoad : function() {
206 onShow : function() {
207 var selection = editor.getSelection();
208 this.element = selection.getStartElement();
210 this.element = this.element.getAscendant('a', true);
211 if (!this.element || this.element.getName() != 'a' || !this.element.hasAttribute('cross-link')) {
212 this.element = editor.document.createElement('a');
213 this.element.setAttribute('cross-link', '');
214 this.insertMode = true;
216 this.insertMode = false;
219 if (this.insertMode) {
220 this.setValueOf('tab-main', 'filter', selection.getSelectedText().trim());
223 this.setupContent(this.element);
227 if (!this.getValueOf('tab-main', 'type'))
229 if (!this.getValueOf('tab-main', 'anchor'))
232 this.commitContent(this.element);
235 editor.insertElement(this.element);
237 editor.execCommand('update-crossreferences');