Code source wiki de MacroService
Version 6.1 par Xavier Richard le 19/05/2020 à 11:43
Afficher les derniers auteurs
author | version | line-number | content |
---|---|---|---|
1 | {{include reference="CKEditor.VelocityMacros" /}} | ||
2 | |||
3 | {{velocity output="false"}} | ||
4 | ## ================================================================ | ||
5 | ## Returned JSON format: | ||
6 | ## | ||
7 | ## { | ||
8 | ## 'options': { | ||
9 | ## 'allMacrosExcludedCategories': [ | ||
10 | ## (translated category name), ... | ||
11 | ## ] | ||
12 | ## }, | ||
13 | ## 'list': [ | ||
14 | ## { | ||
15 | ## 'id': (macro id), | ||
16 | ## 'name': (translated macro name), | ||
17 | ## 'description': (translated macro description), | ||
18 | ## 'defaultCategory': (translated macro category), | ||
19 | ## }, | ||
20 | ## ... | ||
21 | ## ] | ||
22 | ## } | ||
23 | ## ================================================================ | ||
24 | #macro (getMacroList $syntaxId) | ||
25 | #set ($syntax = $services.rendering.resolveSyntax($syntaxId)) | ||
26 | #set ($macroDescriptors = $services.rendering.getMacroDescriptors($syntax)) | ||
27 | #if (!$macroDescriptors) | ||
28 | ## Before XWiki 9.7RC1 we had to use APIs that require programming rights. | ||
29 | #set ($macroDescriptors = []) | ||
30 | #try() | ||
31 | #set ($macroManager = $services.component.getInstance('org.xwiki.rendering.macro.MacroManager')) | ||
32 | #foreach ($macroId in $macroManager.getMacroIds($syntax)) | ||
33 | #set ($macroDescriptor = $macroManager.getMacro($macroId).descriptor) | ||
34 | #set ($discard = $macroDescriptors.add($macroDescriptor)) | ||
35 | #end | ||
36 | #end | ||
37 | #end | ||
38 | #set ($data = {}) | ||
39 | #set ($allMacrosExcludedCategories = []) | ||
40 | #set ($discard = $allMacrosExcludedCategories.add("#maybeTranslate('rendering.macroCategory.Internal' 'Internal')")) | ||
41 | #set ($discard = $allMacrosExcludedCategories.add("#maybeTranslate('rendering.macroCategory.Deprecated' 'Deprecated')")) | ||
42 | #set ($discard = $data.put('options', { 'allMacrosExcludedCategories' : $allMacrosExcludedCategories })) | ||
43 | #set ($macroList = []) | ||
44 | #foreach ($macroDescriptor in $macroDescriptors) | ||
45 | #set ($macroTranslationKey = "rendering.macro.$macroDescriptor.id") | ||
46 | #set ($macroCategoryTranslationKey = "rendering.macroCategory.$macroDescriptor.defaultCategory") | ||
47 | #set ($discard = $macroList.add({ | ||
48 | 'id': $macroDescriptor.id, | ||
49 | 'name': "#maybeTranslate(""${macroTranslationKey}.name"" $macroDescriptor.name)", | ||
50 | 'description': "#maybeTranslate(""${macroTranslationKey}.description"" $macroDescriptor.description)", | ||
51 | 'defaultCategory': "#maybeTranslate($macroCategoryTranslationKey $macroDescriptor.defaultCategory)" | ||
52 | })) | ||
53 | #end | ||
54 | #set ($macroList = $resolvedSortTool.sort($macroList, 'name')) | ||
55 | #set ($discard = $data.put('list', $macroList)) | ||
56 | #end | ||
57 | |||
58 | #macro (maybeGetMacroDescriptor $macroIdAsString) | ||
59 | #set ($xmacro = $NULL) | ||
60 | #set ($macroDescriptor = $NULL) | ||
61 | #set ($macroId = $services.rendering.resolveMacroId($macroIdAsString)) | ||
62 | #if ($macroId) | ||
63 | #set ($macroDescriptor = $services.rendering.getMacroDescriptor($macroId)) | ||
64 | #if (!$macroDescriptor && $macroId.syntax) | ||
65 | ## Try the macro id without the syntax. | ||
66 | #set ($macroId = $services.rendering.resolveMacroId($macroId.id)) | ||
67 | #set ($macroDescriptor = $services.rendering.getMacroDescriptor($macroId)) | ||
68 | #end | ||
69 | #else | ||
70 | ## Either the macro id could not be resolved (unlikely) or we are on an older XWiki instance (before 10.10RC1) where | ||
71 | ## we had to use APIs that require programming rights. | ||
72 | #getMacroWithPR($macroIdAsString) | ||
73 | #if ($xmacro) | ||
74 | #set ($macroDescriptor = $xmacro.descriptor) | ||
75 | #end | ||
76 | #end | ||
77 | #if ($macroDescriptor) | ||
78 | #getMacroDescriptor($macroDescriptor) | ||
79 | #if ($xmacro) | ||
80 | ## supportsInlineMode was not exposed on the macro descriptor before XWiki 10.10RC1. | ||
81 | #set ($data.supportsInlineMode = $xmacro.supportsInlineMode()) | ||
82 | #end | ||
83 | #end | ||
84 | #end | ||
85 | |||
86 | #macro (getMacroWithPR $macroIdAsString) | ||
87 | #set ($xmacro = $NULL) | ||
88 | #try() | ||
89 | #set ($macroIdFactory = $services.component.getInstance('org.xwiki.rendering.macro.MacroIdFactory')) | ||
90 | #set ($macroId = $macroIdFactory.createMacroId($macroIdAsString)) | ||
91 | #set ($macroManager = $services.component.getInstance('org.xwiki.rendering.macro.MacroManager')) | ||
92 | #if ($macroManager.exists($macroId)) | ||
93 | #set ($xmacro = $macroManager.getMacro($macroId)) | ||
94 | #elseif ($macroId.syntax) | ||
95 | ## Try the macro id without the syntax. | ||
96 | #set ($macroId = $macroIdFactory.createMacroId($macroId.id)) | ||
97 | #if ($macroManager.exists($macroId)) | ||
98 | #set ($xmacro = $macroManager.getMacro($macroId)) | ||
99 | #end | ||
100 | #end | ||
101 | #end | ||
102 | #end | ||
103 | |||
104 | #macro (getMacroDescriptor $macroDescriptor) | ||
105 | ## Translate the macro name and description. | ||
106 | #set ($macroTranslationKey = "rendering.macro.$macroDescriptor.id") | ||
107 | #initRequiredSkinExtensions() | ||
108 | #set ($data = { | ||
109 | 'id': $macroDescriptor.id, | ||
110 | 'name': "#maybeTranslate(""${macroTranslationKey}.name"" $macroDescriptor.name)", | ||
111 | 'description': "#maybeTranslate(""${macroTranslationKey}.description"" $macroDescriptor.description)", | ||
112 | 'defaultCategory': $macroDescriptor.defaultCategory, | ||
113 | 'supportsInlineMode': $macroDescriptor.supportsInlineMode(), | ||
114 | 'parameterDescriptorMap': {} | ||
115 | }) | ||
116 | #if ($macroDescriptor.contentDescriptor) | ||
117 | ## Translate the content label and description. | ||
118 | ## Treat the macro content as if it is the last macro parameter. | ||
119 | #set ($data.contentDescriptor = { | ||
120 | 'name': "#maybeTranslate('rendering.macroContent' 'Content')", | ||
121 | 'description': "#maybeTranslate(""${macroTranslationKey}.content.description"" | ||
122 | $macroDescriptor.contentDescriptor.description)", | ||
123 | 'mandatory': $macroDescriptor.contentDescriptor.mandatory, | ||
124 | 'deprecated': $macroDescriptor.contentDescriptor.deprecated, | ||
125 | 'advanced': $macroDescriptor.contentDescriptor.advanced, | ||
126 | 'defaultValue': $macroDescriptor.contentDescriptor.defaultValue, | ||
127 | 'type': $macroDescriptor.contentDescriptor.type, | ||
128 | 'editTemplate': '<textarea name="$content" rows="7"></textarea>', | ||
129 | 'index': $macroDescriptor.parameterDescriptorMap.size() | ||
130 | }) | ||
131 | #fixDescriptorType($data.contentDescriptor) | ||
132 | #end | ||
133 | #set ($groupDescriptorTree = {}) | ||
134 | #foreach ($entry in $macroDescriptor.parameterDescriptorMap.entrySet()) | ||
135 | #set ($parameterDescriptor = $entry.value) | ||
136 | ## Translate the parameter name and description. | ||
137 | #set ($parameterTranslationKey = "${macroTranslationKey}.parameter.$parameterDescriptor.id") | ||
138 | ## Note: The displayHidden parameter is new in XWiki 12.4RC1 so make sure we set 'hidden' to false if it doesn't | ||
139 | ## exist | ||
140 | #if ("$!parameterDescriptor.displayHidden" != '') | ||
141 | #set ($parameterHidden = $parameterDescriptor.displayHidden) | ||
142 | #else | ||
143 | #set ($parameterHidden = false) | ||
144 | #end | ||
145 | #set ($translatedParameterDescriptor = { | ||
146 | 'id': $parameterDescriptor.id, | ||
147 | 'name': "#maybeTranslate(""${parameterTranslationKey}.name"" $parameterDescriptor.name)", | ||
148 | 'description': "#maybeTranslate(""${parameterTranslationKey}.description"" $parameterDescriptor.description)", | ||
149 | 'mandatory': $parameterDescriptor.mandatory, | ||
150 | 'deprecated': $parameterDescriptor.deprecated, | ||
151 | 'advanced': $parameterDescriptor.advanced, | ||
152 | 'defaultValue': $parameterDescriptor.defaultValue, | ||
153 | 'type': $parameterDescriptor.displayType, | ||
154 | 'hidden' : $parameterHidden, | ||
155 | 'index': $foreach.index | ||
156 | }) | ||
157 | #if ("$!translatedParameterDescriptor.type" == '') | ||
158 | ## displayType is not available before XWiki 11.0 so we need to fall back on parameterType. | ||
159 | #set ($translatedParameterDescriptor.type = $parameterDescriptor.parameterType) | ||
160 | #end | ||
161 | #set ($translatedParameterDescriptor.caseInsensitive = $translatedParameterDescriptor.type.isEnum()) | ||
162 | #set ($groupDescriptor = $parameterDescriptor.groupDescriptor) | ||
163 | #if ($groupDescriptor) | ||
164 | #handleMacroParameterGroup($groupDescriptor $groupDescriptorTree $translatedParameterDescriptor) | ||
165 | #end | ||
166 | #if ($translatedParameterDescriptor.type.getName() == 'java.lang.String' | ||
167 | && ($parameterDescriptor.defaultValue == 'false' || $parameterDescriptor.defaultValue == 'true') | ||
168 | && $macroDescriptor.parametersBeanClass.getSimpleName() == 'WikiMacroParameters') | ||
169 | #set ($translatedParameterDescriptor.defaultValue = $parameterDescriptor.defaultValue == 'true') | ||
170 | #set ($translatedParameterDescriptor.type = $translatedParameterDescriptor.defaultValue.getClass()) | ||
171 | #end | ||
172 | #set ($htmlDisplayerParameters = {'name': $parameterDescriptor.id}) | ||
173 | #if ($translatedParameterDescriptor.group) | ||
174 | #set ($discard = $htmlDisplayerParameters.put('data-property-group', | ||
175 | $stringtool.join($translatedParameterDescriptor.group, '/'))) | ||
176 | #end | ||
177 | #set ($translatedParameterDescriptor.editTemplate = $services.display.html.display( | ||
178 | $translatedParameterDescriptor.type, $translatedParameterDescriptor.defaultValue, $htmlDisplayerParameters, 'edit' | ||
179 | )) | ||
180 | #if ("$!translatedParameterDescriptor.editTemplate" == '') | ||
181 | #set ($translatedParameterDescriptor.editTemplate = "#getMacroParameterEditTemplate( | ||
182 | $translatedParameterDescriptor)") | ||
183 | #end | ||
184 | #set ($translatedParameterDescriptor.editTemplate = $translatedParameterDescriptor.editTemplate.trim()) | ||
185 | #fixDescriptorType($translatedParameterDescriptor) | ||
186 | ## Make sure the key is lowercase (for XWiki <9.0). | ||
187 | ## See XWIKI-13990: Inconsistency between Java-based and Wiki-based rendering macros regarding the parameter | ||
188 | ## descriptor map keys | ||
189 | #set ($discard = $data.parameterDescriptorMap.put($entry.key.toLowerCase(), $translatedParameterDescriptor)) | ||
190 | #end | ||
191 | #if ($groupDescriptorTree.groups) | ||
192 | #set ($data.groupDescriptorTree = $groupDescriptorTree.groups) | ||
193 | #end | ||
194 | #set ($data.requiredSkinExtensions = "#getRequiredSkinExtensions()") | ||
195 | #end | ||
196 | |||
197 | #macro (fixDescriptorType $descriptor) | ||
198 | ## The goal of this code is to obtain a normalized string representation of the type specified in the descriptor. | ||
199 | ## See XCOMMONS-1583: Define a stable way to serialize types | ||
200 | ## | ||
201 | ## The type specified in the descriptor can be any implementation of java.lang.reflect.Type, not necessarily a | ||
202 | ## java.lang.Class. We can't use toString() because the return of Class#toString() is different than Class#getName(). | ||
203 | ## We can't use Type#getTypeName() either because the access to this method is restricted from Velocity. The only | ||
204 | ## option for now is to try #getName() first and fall back on #toString() for types that are not instances of | ||
205 | ## java.lang.Class. | ||
206 | #set ($typeName = $descriptor.type.getName()) | ||
207 | #if ("$!typeName" == '') | ||
208 | ## Probably not a java.lang.Class. Fall back on #toString(). | ||
209 | #set ($typeName = "$!descriptor.type") | ||
210 | #end | ||
211 | ## Remove whitespace from the type name in order to have a single string representation. | ||
212 | #set ($descriptor.type = $typeName.replaceAll('\s+', '')) | ||
213 | #end | ||
214 | |||
215 | ## Builds the group tree with the following structure: | ||
216 | ## | ||
217 | ## { | ||
218 | ## 'parentGroupId': { | ||
219 | ## 'id': 'parentGroupId', | ||
220 | ## 'name': 'Parent Group', | ||
221 | ## 'feature': 'someFeature', | ||
222 | ## 'groups': { | ||
223 | ## 'childGroupId': {...}, | ||
224 | ## ... | ||
225 | ## } | ||
226 | ## }, | ||
227 | ## ... | ||
228 | ## } | ||
229 | #macro (handleMacroParameterGroup $groupDescriptor $groupDescriptorTree $translatedParameterDescriptor) | ||
230 | #if ($groupDescriptor.group && $groupDescriptor.group.size() > 0) | ||
231 | #set ($translatedParameterDescriptor.group = $groupDescriptor.group) | ||
232 | #set ($parentGroup = $groupDescriptorTree) | ||
233 | #foreach ($groupId in $groupDescriptor.group) | ||
234 | #if (!$parentGroup.groups) | ||
235 | #set ($parentGroup.groups = {}) | ||
236 | #end | ||
237 | #set ($childGroup = $parentGroup.groups.get($groupId)) | ||
238 | #if (!$childGroup) | ||
239 | #if ($groupId == $translatedParameterDescriptor.id) | ||
240 | #set ($groupName = $translatedParameterDescriptor.name) | ||
241 | #else | ||
242 | #set ($groupTranslationKey = "${macroTranslationKey}.group.$groupId") | ||
243 | #set ($groupName = "#maybeTranslate(""${groupTranslationKey}.name"" $groupId)") | ||
244 | #end | ||
245 | #set ($childGroup = { | ||
246 | 'id': $groupId, | ||
247 | 'name': $groupName | ||
248 | }) | ||
249 | #set ($discard = $parentGroup.groups.put($groupId, $childGroup)) | ||
250 | #end | ||
251 | #set ($parentGroup = $childGroup) | ||
252 | #end | ||
253 | #if ("$!groupDescriptor.feature" != '') | ||
254 | #set ($parentGroup.feature = $groupDescriptor.feature) | ||
255 | #end | ||
256 | #elseif ($groupDescriptor.feature) | ||
257 | ## This group is made of a single parameter. The feature then refers to this parameter. | ||
258 | #set ($translatedParameterDescriptor.feature = $groupDescriptor.feature) | ||
259 | #end | ||
260 | #end | ||
261 | |||
262 | #macro (getMacroParameterEditTemplate $translatedParameterDescriptor) | ||
263 | #if ($translatedParameterDescriptor.type.getName() == 'boolean' | ||
264 | || $translatedParameterDescriptor.type.getName() == 'java.lang.Boolean') | ||
265 | <input type="checkbox" name="$escapetool.xml($translatedParameterDescriptor.id)" value="true"/>## | ||
266 | ## We need to submit something in case the checkbox is not checked. | ||
267 | <input type="hidden" name="$escapetool.xml($translatedParameterDescriptor.id)" value="false"/> | ||
268 | #elseif ($translatedParameterDescriptor.type.isEnum()) | ||
269 | #if ($translatedParameterDescriptor.defaultValue) | ||
270 | #set ($enumValues = $translatedParameterDescriptor.defaultValue.values()) | ||
271 | #else | ||
272 | ## A parameter of type enum that doesn't have a default value is very unlikely. We attempt to read the list of | ||
273 | ## possible values from the enum type in this case, which is currently forbidden, but at least it will generate | ||
274 | ## a warning in the logs that will help us investigate the problem. | ||
275 | #set ($enumValues = $translatedParameterDescriptor.type.getEnumConstants()) | ||
276 | #end | ||
277 | <select name="$escapetool.xml($translatedParameterDescriptor.id)">## | ||
278 | #foreach ($enumValue in $enumValues) | ||
279 | #set ($value = $enumValue.name()) | ||
280 | #set ($label = "#maybeTranslate(""${parameterTranslationKey}.value.$value"" $enumValue)") | ||
281 | <option value="$escapetool.xml($value)">$escapetool.xml($label)</option>## | ||
282 | #end | ||
283 | </select> | ||
284 | #else | ||
285 | <input type="text" name="$escapetool.xml($translatedParameterDescriptor.id)"/> | ||
286 | #end | ||
287 | #end | ||
288 | |||
289 | #macro (maybeTranslate $key $defaultValue) | ||
290 | #if ($services.localization.get($key)) | ||
291 | $services.localization.render($key)## | ||
292 | #else | ||
293 | $!defaultValue## | ||
294 | #end | ||
295 | #end | ||
296 | {{/velocity}} | ||
297 | |||
298 | {{velocity wiki="false"}} | ||
299 | #if ("$!request.data" != '') | ||
300 | #set ($data = $NULL) | ||
301 | #if ($request.data == 'list') | ||
302 | #getMacroList($request.syntaxId) | ||
303 | #elseif ($request.data == 'descriptor') | ||
304 | #maybeGetMacroDescriptor($request.macroId) | ||
305 | #end | ||
306 | #if ($data) | ||
307 | #set ($discard = $response.setContentType('application/json')) | ||
308 | $jsontool.serialize($data) | ||
309 | #else | ||
310 | $response.sendError(404) | ||
311 | #end | ||
312 | #end | ||
313 | {{/velocity}} |