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 dialog.setValueOf('tab-main', 'filter', '');
19 var anchorSelect = dialog.getContentElement('tab-main', 'anchor');
21 anchorSelect.add('', '');
23 var anchorGroupSelect = dialog.getContentElement('tab-main', 'anchorGroup');
24 anchorGroupSelect.clear();
25 anchorGroupSelect.add('', '');
26 if (type && type.groupAnchors == true)
27 anchorGroupSelect.getElement().show();
29 anchorGroupSelect.getElement().hide();
31 config.findAnchors(config, editor, type, function(anchors) {
32 dialog.anchors = anchors;
33 dialog.anchorsByGuid = {};
34 dialog.anchorsGroupNameByGuid = {};
36 for (var i = 0; i < anchors.length; i++) {
37 var anchor = anchors[i];
38 var guid = anchor.guid;
39 var label = anchor.text;
40 anchorSelect.add(label, guid);
42 if (type.groupAnchors == true && anchor.groupGuid) {
43 dialog.anchorsGroupNameByGuid[anchor.groupGuid] = anchor.groupName;
45 dialog.anchorsByGuid[guid] = anchor;
48 if (type && type.groupAnchors == true) {
49 for (var groupGuid in dialog.anchorsGroupNameByGuid) {
50 var groupName = dialog.anchorsGroupNameByGuid[groupGuid];
51 anchorGroupSelect.add(groupName, groupGuid);
55 // fix and others
56 $('option', anchorSelect.getInputElement().$).each(function() {
58 var text = option.text();
62 if (!dialog.insertMode)
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') {
212 this.element = editor.document.createElement('a');
213 this.element.setAttribute('cross-link', '');
214 this.insertMode = true;
216 this.insertMode = false;
222 this.setupContent(this.element);
226 if (!this.getValueOf('tab-main', 'type'))
228 if (!this.getValueOf('tab-main', 'anchor'))
231 this.commitContent(this.element);
234 editor.insertElement(this.element);
236 editor.execCommand('update-crossreferences');