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