|
1 CKEDITOR.dialog.add('crossreference-link-dialog', function(editor) { |
|
2 |
|
3 var config = editor.config.crossreference; |
|
4 |
|
5 var updateAnchors = function(dialog) { |
|
6 dialog.setState(CKEDITOR.DIALOG_STATE_BUSY); |
|
7 |
|
8 dialog.anchors = []; |
|
9 dialog.anchorsByGuid = {}; |
|
10 dialog.anchorsGroupNameByGuid = {}; |
|
11 |
|
12 var type = null; |
|
13 var typeName = dialog.getValueOf('tab-main', 'type'); |
|
14 if (typeName) |
|
15 type = config.types[typeName]; |
|
16 |
|
17 dialog.setValueOf('tab-main', 'filter', ''); |
|
18 |
|
19 var anchorSelect = dialog.getContentElement('tab-main', 'anchor'); |
|
20 anchorSelect.clear(); |
|
21 anchorSelect.add('', ''); |
|
22 |
|
23 var anchorGroupSelect = dialog.getContentElement('tab-main', 'anchorGroup'); |
|
24 anchorGroupSelect.clear(); |
|
25 anchorGroupSelect.add('', ''); |
|
26 if (type && type.groupAnchors == true) |
|
27 anchorGroupSelect.getElement().show(); |
|
28 else |
|
29 anchorGroupSelect.getElement().hide(); |
|
30 |
|
31 config.findAnchors(config, editor, type, function(anchors) { |
|
32 dialog.anchors = anchors; |
|
33 dialog.anchorsByGuid = {}; |
|
34 dialog.anchorsGroupNameByGuid = {}; |
|
35 |
|
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); |
|
41 |
|
42 if (type.groupAnchors == true && anchor.groupGuid) { |
|
43 dialog.anchorsGroupNameByGuid[anchor.groupGuid] = anchor.groupName; |
|
44 } |
|
45 dialog.anchorsByGuid[guid] = anchor; |
|
46 } |
|
47 |
|
48 if (type && type.groupAnchors == true) { |
|
49 for (var groupGuid in dialog.anchorsGroupNameByGuid) { |
|
50 var groupName = dialog.anchorsGroupNameByGuid[groupGuid]; |
|
51 anchorGroupSelect.add(groupName, groupGuid); |
|
52 } |
|
53 } |
|
54 |
|
55 // fix and others |
|
56 $('option', anchorSelect.getInputElement().$).each(function() { |
|
57 var option = $(this); |
|
58 var text = option.text(); |
|
59 option.html(text); |
|
60 }); |
|
61 |
|
62 if (!dialog.insertMode) |
|
63 anchorSelect.setup(dialog.element); |
|
64 |
|
65 dialog.setState(CKEDITOR.DIALOG_STATE_IDLE); |
|
66 }); |
|
67 }; |
|
68 |
|
69 var filterAnchors = function(dialog) { |
|
70 var filter = dialog.getValueOf('tab-main', 'filter'); |
|
71 if (filter) |
|
72 filter = filter.trim().toLowerCase(); |
|
73 else |
|
74 filter = ''; |
|
75 |
|
76 var type = null; |
|
77 var typeName = dialog.getValueOf('tab-main', 'type'); |
|
78 if (typeName) |
|
79 type = config.types[typeName]; |
|
80 |
|
81 var groupGuid = null; |
|
82 if (type && type.groupAnchors == true) |
|
83 groupGuid = dialog.getValueOf('tab-main', 'anchorGroup'); |
|
84 |
|
85 var anchorSelect = dialog.getContentElement('tab-main', 'anchor'); |
|
86 var prevValue = anchorSelect.getValue(); |
|
87 var selected = null; |
|
88 $('option', anchorSelect.getInputElement().$).each(function() { |
|
89 var option = $(this); |
|
90 var guid = option.val(); |
|
91 var anchor = dialog.anchorsByGuid[guid]; |
|
92 var text = option.text(); |
|
93 |
|
94 var matches = filter.length == 0 || text.toLowerCase().indexOf(filter) != -1; |
|
95 if (groupGuid) |
|
96 matches = matches && anchor && anchor.groupGuid == groupGuid; |
|
97 |
|
98 if (matches) { |
|
99 option.removeAttr('disabled', 'disabled'); |
|
100 if (selected == null) |
|
101 selected = option.val(); |
|
102 else if (guid == prevValue) |
|
103 selected = option.val(); |
|
104 } else { |
|
105 option.attr('disabled', 'disabled'); |
|
106 } |
|
107 }); |
|
108 anchorSelect.setValue(selected); |
|
109 }; |
|
110 |
|
111 return { |
|
112 title : editor.lang.crossreference.link, |
|
113 minWidth : 400, |
|
114 minHeight : 150, |
|
115 |
|
116 contents : [ |
|
117 { |
|
118 id : 'tab-main', |
|
119 label : editor.lang.crossreference.link, |
|
120 elements : [ |
|
121 { |
|
122 type : 'vbox', |
|
123 widths : [ '100%' ], |
|
124 children : [ |
|
125 { |
|
126 type : 'html', |
|
127 id : 'description', |
|
128 html : '<div style="white-space: normal; text-align: justify;">' + editor.lang.crossreference.linkDescription + '</div>', |
|
129 }, |
|
130 { |
|
131 type : 'select', |
|
132 id : 'type', |
|
133 width: '100%', |
|
134 label : editor.lang.crossreference.anchorType, |
|
135 required: true, |
|
136 items : [['']], |
|
137 onLoad: function() { |
|
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); |
|
143 } |
|
144 }, |
|
145 onChange: function() { |
|
146 updateAnchors(this.getDialog()); |
|
147 }, |
|
148 setup: function(element) { |
|
149 this.setValue(element.getAttribute('cross-reference')); |
|
150 }, |
|
151 commit: function(element) { |
|
152 element.setAttribute('cross-reference', this.getValue()); |
|
153 element.setAttribute('cross-link', ''); |
|
154 } |
|
155 }, |
|
156 { |
|
157 type : 'select', |
|
158 id : 'anchorGroup', |
|
159 width: '100%', |
|
160 label : editor.lang.crossreference.anchorGroup, |
|
161 required: false, |
|
162 items : [['']], |
|
163 onLoad: function() { |
|
164 this.getInputElement().setStyle('width', '100%'); |
|
165 }, |
|
166 onChange: function() { |
|
167 filterAnchors(this.getDialog()); |
|
168 } |
|
169 }, |
|
170 { |
|
171 type : 'text', |
|
172 id : 'filter', |
|
173 width: '100%', |
|
174 label : editor.lang.crossreference.linkFilter, |
|
175 onKeyUp : function() { |
|
176 filterAnchors(this.getDialog()); |
|
177 } |
|
178 }, |
|
179 { |
|
180 type : 'select', |
|
181 id : 'anchor', |
|
182 width: '100%', |
|
183 label : editor.lang.crossreference.anchor, |
|
184 required: true, |
|
185 items : [['']], |
|
186 onLoad: function() { |
|
187 this.getInputElement().setStyle('width', '100%'); |
|
188 }, |
|
189 setup: function(element) { |
|
190 this.setValue(element.getAttribute('cross-guid')); |
|
191 }, |
|
192 commit: function(element) { |
|
193 element.setAttribute('cross-guid', this.getValue()); |
|
194 } |
|
195 } |
|
196 ] |
|
197 } |
|
198 ] |
|
199 } |
|
200 ], |
|
201 |
|
202 onLoad : function() { |
|
203 |
|
204 }, |
|
205 |
|
206 onShow : function() { |
|
207 var selection = editor.getSelection(); |
|
208 this.element = selection.getStartElement(); |
|
209 if (this.element) |
|
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; |
|
215 } else { |
|
216 this.insertMode = false; |
|
217 } |
|
218 |
|
219 if (this.insertMode) |
|
220 updateAnchors(this); |
|
221 else |
|
222 this.setupContent(this.element); |
|
223 }, |
|
224 |
|
225 onOk : function() { |
|
226 if (!this.getValueOf('tab-main', 'type')) |
|
227 return; |
|
228 if (!this.getValueOf('tab-main', 'anchor')) |
|
229 return; |
|
230 |
|
231 this.commitContent(this.element); |
|
232 |
|
233 if (this.insertMode) |
|
234 editor.insertElement(this.element); |
|
235 |
|
236 editor.execCommand('update-crossreferences'); |
|
237 } |
|
238 |
|
239 }; |
|
240 }); |