Wiki-Quellcode von Macros
Zuletzt geändert von Daniel Herrmann am 2025/07/19 16:47
Zeige letzte Bearbeiter
| author | version | line-number | content |
|---|---|---|---|
| 1 | {{velocity filter="none" output="false"}} | ||
| 2 | ## Determine the document that is being annotated. | ||
| 3 | #if ("$!request.reference" != '') | ||
| 4 | #set ($annotationReference = $services.model.resolveDocument($request.reference)) | ||
| 5 | #elseif ("$!request.wiki" != '' && "$!request.space" != '' && "$!request.page" != '') | ||
| 6 | #set ($annotationReference = $services.model.createDocumentReference($request.wiki, $request.space, $request.page)) | ||
| 7 | #elseif ("$!request.sheet" != '') | ||
| 8 | ## When the annotation create / edit / display form is loaded as a sheet for the annotated document. | ||
| 9 | #set ($annotationReference = $doc.documentReference) | ||
| 10 | #end | ||
| 11 | ## | ||
| 12 | ## Not used here but kept for retro-compatibility | ||
| 13 | #set($restURL = "${request.contextPath}/rest") | ||
| 14 | ## Retrieve the annotation class from the configuration | ||
| 15 | #set($config = 'AnnotationCode.AnnotationConfig') | ||
| 16 | #set($annotationClassDocName = $xwiki.getDocument($config).getObject($config).getProperty('annotationClass').value) | ||
| 17 | #set($annotationClass = $xwiki.getDocument("$!{annotationClassDocName}").getxWikiClass()) | ||
| 18 | ## | ||
| 19 | ## | ||
| 20 | ## Do not display these 'internal' properties in the form: | ||
| 21 | #set($hiddenProperties = ['selection', 'selectionLeftContext', 'selectionRightContext', 'originalSelection', 'target', 'date', 'author', 'state', 'replyto', 'highlight'])## TODO: 'color' | ||
| 22 | ## | ||
| 23 | ## | ||
| 24 | #* | ||
| 25 | * Display a box for creating an annotation | ||
| 26 | * | ||
| 27 | * @param $docWiki the wiki of the annotated document | ||
| 28 | * @param $docSpace the space of the annotated document | ||
| 29 | * @param $docPage the page of the annotated document | ||
| 30 | * @param $annData a map containing the mandatory selection-related fields | ||
| 31 | * @deprecated since 7.2M2, use #displayCreateBoxFromReference instead | ||
| 32 | *# | ||
| 33 | #macro(displayCreateBox $docWiki $docSpace $docPage $annData) | ||
| 34 | #displayCreateBoxFromReference($services.model.createDocumentReference($docWiki, $docSpace, $docPage)) | ||
| 35 | #end | ||
| 36 | #* | ||
| 37 | * Display a box for creating an annotation | ||
| 38 | * | ||
| 39 | * @param $docRef the reference of the annotated document | ||
| 40 | * @param $annData a map containing the mandatory selection-related fields | ||
| 41 | *# | ||
| 42 | #macro(displayCreateBoxFromReference $docRef $annData) | ||
| 43 | <div class="annotation-box annotation-box-create"> | ||
| 44 | #set ($fullRestURL = "${services.rest.url($docRef)}/annotations?media=json") | ||
| 45 | <form method="POST" action="$escapetool.xml($response.encodeURL($fullRestURL))"> | ||
| 46 | <div class="hidden"> | ||
| 47 | #foreach($field in $annData.keySet()) | ||
| 48 | <input type="hidden" name="${field}" value="$!{escapetool.xml($annData.get(${field}))}"/> | ||
| 49 | #end | ||
| 50 | </div> | ||
| 51 | #displayAnnotationFromReference($annData, 'create', $docRef) | ||
| 52 | #displayAnnotationBoxButtons('create') | ||
| 53 | </form> | ||
| 54 | </div> | ||
| 55 | #end | ||
| 56 | #* | ||
| 57 | * Display a box for editing an annotation | ||
| 58 | * | ||
| 59 | * @param $id the annotation identifier | ||
| 60 | * @param $docWiki the wiki of the annotated document | ||
| 61 | * @param $docSpace the space of the annotated document | ||
| 62 | * @param $docPage the page of the annotated document | ||
| 63 | * @deprecated, starting with 7.2M3, use #displayEditBoxFromReference instead | ||
| 64 | *# | ||
| 65 | #macro(displayEditBox $id $docWiki $docSpace $docPage) | ||
| 66 | #displayEditBoxFromReference($id, $services.model.createDocumentReference($docWiki, $docSpace, $docPage)) | ||
| 67 | #end | ||
| 68 | #* | ||
| 69 | * Display a box for editing an annotation | ||
| 70 | * | ||
| 71 | * @param $id the annotation identifier | ||
| 72 | * @param $docRef the reference of the annotated document | ||
| 73 | *# | ||
| 74 | #macro(displayEditBoxFromReference $id $docRef) | ||
| 75 | #set($ann = $services.annotations.getAnnotation($docRef, $id)) | ||
| 76 | <div class="annotation-box annotation-box-edit"> | ||
| 77 | #if($ann) | ||
| 78 | #set($editURL = "${services.rest.url($docRef)}/annotation/${escapetool.url($ann.id)}?method=PUT&media=json") | ||
| 79 | <form method='POST' action="${editURL}"> | ||
| 80 | #displayAnnotationFromReference($ann, 'edit', $docRef) | ||
| 81 | #displayAnnotationBoxButtons('edit') | ||
| 82 | </form> | ||
| 83 | #else | ||
| 84 | #info($services.localization.render('annotations.action.edit.error.notfound')) | ||
| 85 | #end | ||
| 86 | </div> | ||
| 87 | #end | ||
| 88 | #* | ||
| 89 | * Display a box containing the annotation | ||
| 90 | * | ||
| 91 | * @param $id the annotation identifier | ||
| 92 | * @param $docWiki the wiki of the annotated document | ||
| 93 | * @param $docSpace the space of the annotated document | ||
| 94 | * @param $docPage the page of the annotated document | ||
| 95 | * @deprecated since 7.2M3, use #displayViewBoxFromReference instead | ||
| 96 | *# | ||
| 97 | #macro(displayViewBox $id $docWiki $docSpace $docPage) | ||
| 98 | #displayViewBoxFromReference($id, $services.model.createDocumentReference($docWiki, $docSpace, $docPage)) | ||
| 99 | #end | ||
| 100 | #* | ||
| 101 | * Display a box containing the annotation | ||
| 102 | * | ||
| 103 | * @param $target the annotated entity identifier | ||
| 104 | * @param $id the annotation identifier | ||
| 105 | * @param $docRef the reference of the annotated document | ||
| 106 | *# | ||
| 107 | #macro(displayViewBoxFromReference $id $docRef) | ||
| 108 | #set($ann = $services.annotations.getAnnotation($docRef, $id)) | ||
| 109 | <div class="annotation-box annotation-box-view"> | ||
| 110 | #if ($ann) | ||
| 111 | #*<div class="links"> | ||
| 112 | <a href="#" class='annotation-view-hide'>$services.localization.render('annotations.action.view.hide.text')</a> | ||
| 113 | </div>*# | ||
| 114 | ##<div class="clearfloats"></div> | ||
| 115 | #displayAnnotationFromReference($ann $mode $docRef) | ||
| 116 | #else | ||
| 117 | #info($services.localization.render('annotations.action.view.error.notfound')) | ||
| 118 | #end | ||
| 119 | </div> | ||
| 120 | #end | ||
| 121 | ## | ||
| 122 | ## | ||
| 123 | ## | ||
| 124 | #* | ||
| 125 | * Display form buttons | ||
| 126 | * | ||
| 127 | * @param $mode 'create' or 'edit' | ||
| 128 | *# | ||
| 129 | #macro(displayAnnotationBoxButtons $mode) | ||
| 130 | <div class="buttons"> | ||
| 131 | <span class='buttonwrapper'><input type="submit" class='button' value='$!{escapetool.xml($services.localization.render("annotations.action.${mode}.submit.text"))}'/></span> | ||
| 132 | <span class='buttonwrapper'><input type="reset" class='button secondary' value='$!{escapetool.xml($services.localization.render("annotations.action.${mode}.cancel.text"))}'/></span> | ||
| 133 | </div> | ||
| 134 | #end | ||
| 135 | ## | ||
| 136 | ## | ||
| 137 | #** | ||
| 138 | * Display an annotation | ||
| 139 | * | ||
| 140 | * @param $ann the annotation object | ||
| 141 | * @param $mode view (default), list, create or edit | ||
| 142 | * @param $docWiki the wiki of the annotated document | ||
| 143 | * @param $docSpace the space of the annotated document | ||
| 144 | * @param $docPage the page of the annotated document | ||
| 145 | * @deprecated since 7.2M3, use #displayAnnotationFromReference instead | ||
| 146 | *# | ||
| 147 | #macro(displayAnnotation $ann $mode $docWiki $docSpace $docPage) | ||
| 148 | #displayAnnotationFromReference($ann, $mode, $services.model.createDocumentReference($docWiki, $docSpace, $docPage)) | ||
| 149 | #end | ||
| 150 | #** | ||
| 151 | * Display an annotation | ||
| 152 | * | ||
| 153 | * @param $ann the annotation object | ||
| 154 | * @param $mode view (default), list, create or edit | ||
| 155 | * @param $docRef the reference of the annotated document | ||
| 156 | *# | ||
| 157 | #macro(displayAnnotationFromReference $ann $mode $docRef) | ||
| 158 | #set ($availableModes = ['view', 'list', 'create', 'edit']) | ||
| 159 | #if(!$availableModes.contains("$!{mode}")) | ||
| 160 | #set($mode = 'view') | ||
| 161 | #end | ||
| 162 | <div class="annotation $!{ann.state}" id="annotation_${mode}_$!{ann.id}"> | ||
| 163 | <div class="annotationAvatar">#mediumUserAvatar($ann.author)</div> | ||
| 164 | <div class="annotationHeader"> | ||
| 165 | #displayAnnotationTitle($ann) | ||
| 166 | #displayAnnotationToolboxFromReference($ann $mode $docRef) | ||
| 167 | </div> | ||
| 168 | <div class="annotationContent"> | ||
| 169 | #displayAnnotationBody($ann $mode $docRef) | ||
| 170 | #if ($mode == 'view' && $annotationClassDocName == 'XWiki.XWikiComments') | ||
| 171 | #set($annoDoc = $xwiki.getDocument($docRef)) | ||
| 172 | #set($repliesList = $annoDoc.getObjects('XWiki.XWikiComments', 'replyto', $ann.id)) | ||
| 173 | #if($repliesList.size() > 0) | ||
| 174 | <a href="#xwikicomment_${ann.id}">$services.localization.render('annotations.action.view.thread')</a> | ||
| 175 | #end | ||
| 176 | #end | ||
| 177 | </div> | ||
| 178 | </div> | ||
| 179 | #end | ||
| 180 | ## | ||
| 181 | ## | ||
| 182 | #** | ||
| 183 | * Display the annotation title, containing author and date | ||
| 184 | * | ||
| 185 | * @param $ann the annotation object | ||
| 186 | *# | ||
| 187 | #macro(displayAnnotationTitle $ann) | ||
| 188 | #set ($dateFormat = $xwiki.getXWikiPreference('dateformat', 'dd/MM/yyyy')) | ||
| 189 | <div class="annotationTitle"> | ||
| 190 | <span class="annotationAuthor">$!xwiki.getUserName($ann.author)</span>## | ||
| 191 | ## Do not indent this, or the comma will be misplaced | ||
| 192 | #if("$!{ann.date}" != '')<span class="sep">, </span><span class="annotationDate">$xwiki.formatDate($ann.date, $dateFormat)</span>#end | ||
| 193 | </div> | ||
| 194 | #end | ||
| 195 | ## | ||
| 196 | ## | ||
| 197 | #** | ||
| 198 | * Display the annotation toolbox: | ||
| 199 | * - 'verify' button for modified annotations | ||
| 200 | * - 'edit' button for annotation authors and other users with edit rights, unless already editing | ||
| 201 | * - 'delete' button for annotation authors and other users with edit rights | ||
| 202 | * | ||
| 203 | * @param $ann the annotation object | ||
| 204 | * @param $mode view (default), list, create or edit | ||
| 205 | * @param $docWiki the wiki of the annotated document | ||
| 206 | * @param $docSpace the space of the annotated document | ||
| 207 | * @param $docPage the page of the annotated document | ||
| 208 | *# | ||
| 209 | #macro(displayAnnotationToolbox $ann $mode $docWiki $docSpace $docPage) | ||
| 210 | #displayAnnotationToolboxFromReference($ann, $mode, $services.model.createDocumentReference($docWiki, $docSpace, $docPage)) | ||
| 211 | #end | ||
| 212 | #** | ||
| 213 | * Display the annotation toolbox: | ||
| 214 | * - 'verify' button for modified annotations | ||
| 215 | * - 'edit' button for annotation authors and other users with edit rights, unless already editing | ||
| 216 | * - 'delete' button for annotation authors and other users with edit rights | ||
| 217 | * | ||
| 218 | * @param $ann the annotation object | ||
| 219 | * @param $mode view (default), list, create or edit | ||
| 220 | * @param $docRef the reference of the annotated document | ||
| 221 | *# | ||
| 222 | #macro(displayAnnotationToolboxFromReference $ann $mode $docRef) | ||
| 223 | <span class="annotationTools btn-group"> | ||
| 224 | #if($mode != 'create' && $services.annotations.canEditAnnotation($ann.id, $docRef)) | ||
| 225 | #set($isUserComment = $services.model.resolveDocument("$!ann.get('author')", 'user') == $xcontext.userReference) | ||
| 226 | #set($editURL = "${services.rest.url($docRef)}/annotation/${escapetool.url($ann.id)}?method=PUT&media=json") | ||
| 227 | #if ("$!{ann.state}" == 'UPDATED') | ||
| 228 | <a class='btn btn-default btn-xs validate' href="${editURL}" | ||
| 229 | title="$services.localization.render('annotations.action.validate.tooltip')"> | ||
| 230 | $services.icon.renderHTML('check') | ||
| 231 | </a> | ||
| 232 | #end | ||
| 233 | #if ($mode != 'edit') | ||
| 234 | ## Comment reply button only for default annotations. | ||
| 235 | #if ("$!annotationClassDocName" == 'XWiki.XWikiComments') | ||
| 236 | #set($xredirect = $!escapetool.xml($request.get('xredirect'))) | ||
| 237 | #if("$!xredirect" == '') | ||
| 238 | #set($xredirect = "$xwiki.getURL($docRef, 'view', $!{request.queryString})") | ||
| 239 | #end | ||
| 240 | ## add "comment reply" button | ||
| 241 | <a class='btn btn-default btn-xs reply' href="${xredirect}#xwikicomment_${ann.id}" | ||
| 242 | title="$services.localization.render('core.viewers.comments.reply')"> | ||
| 243 | $services.icon.renderHTML('comment') | ||
| 244 | </a> | ||
| 245 | #end | ||
| 246 | #if($hasAdmin || $isUserComment) | ||
| 247 | ## use an edit url just for the fanciness of it, it won't really be used in this case. An edit form will be loaded | ||
| 248 | <a class='edit btn btn-default btn-xs' href="${editURL}" | ||
| 249 | title="$services.localization.render('annotations.action.edit.tooltip')"> | ||
| 250 | $services.icon.renderHTML('pencil') | ||
| 251 | </a> | ||
| 252 | #end | ||
| 253 | #end | ||
| 254 | #if($hasAdmin || $isUserComment) | ||
| 255 | #set($deleteURL = "${services.rest.url($docRef)}/annotation/${escapetool.url($ann.id)}?method=DELETE&media=json") | ||
| 256 | <a class='delete btn btn-default btn-xs' href="${deleteURL}" | ||
| 257 | title="$services.localization.render('annotations.action.delete.tooltip')"> | ||
| 258 | $services.icon.renderHTML('cross') | ||
| 259 | </a> | ||
| 260 | #end | ||
| 261 | #end | ||
| 262 | </span> | ||
| 263 | #end | ||
| 264 | ## | ||
| 265 | ## | ||
| 266 | #** | ||
| 267 | * Display the annotation body, containing all but the hidden fields. | ||
| 268 | * For annotation lists (which are not displayed near the actual annotation text), also display | ||
| 269 | * in a block quote the corresponding annotated extract. | ||
| 270 | * Depending on the mode, display for each field label + input (edit, create) or the field value (view, list). | ||
| 271 | * | ||
| 272 | * @param $ann the annotation object | ||
| 273 | * @param $mode view (default), list, create or edit | ||
| 274 | * @param $annotatedDocumentReference the reference of the annotated document | ||
| 275 | *# | ||
| 276 | #macro(displayAnnotationBody $ann $mode $annotatedDocumentReference) | ||
| 277 | #if ($mode == 'list') | ||
| 278 | #if ($ann.state == 'ALTERED') | ||
| 279 | <div class="annotationOriginalSelection">$services.localization.render('annotations.altered.text')</div> | ||
| 280 | #end | ||
| 281 | <blockquote class="annotatedText">$!escapetool.xml($ann.selection)</blockquote> | ||
| 282 | #end | ||
| 283 | ## in view mode and list mode, also display the original selection of this annotation | ||
| 284 | #if (($mode == 'view' || $mode == 'list') && $ann.state == 'UPDATED') | ||
| 285 | <div class="annotationOriginalSelection">$services.localization.render('annotations.updated.text')</div> | ||
| 286 | <blockquote class="annotatedText originalAnnotatedText">$!escapetool.xml($ann.originalSelection)</blockquote> | ||
| 287 | #end | ||
| 288 | #set ($annotatedDocument = $xwiki.getDocument($annotatedDocumentReference)) | ||
| 289 | ## Fake object that allows to use the API to generate the display | ||
| 290 | #set($fakeObj = $annotatedDocument.newObject($annotationClassDocName)) | ||
| 291 | #set($properties = $annotationClass.properties) | ||
| 292 | #if ($properties.size() == $hiddenProperties.size() + 1) | ||
| 293 | ## Do not show property labels if there's only one property to display | ||
| 294 | ## get the only property that is to be displayed | ||
| 295 | #set($onlyProperty = '') | ||
| 296 | #foreach($prop in $properties) | ||
| 297 | #if(!$hiddenProperties.contains($prop.name)) | ||
| 298 | #set($onlyProperty = $prop) | ||
| 299 | #break | ||
| 300 | #end | ||
| 301 | #end | ||
| 302 | #set($discard = $fakeObj.set($onlyProperty.name, "$!{ann.get($onlyProperty.name)}")) | ||
| 303 | <div class="annotationText"> | ||
| 304 | #displayAnnotationProperty($annotatedDocument $fakeObj $onlyProperty $mode) | ||
| 305 | </div> | ||
| 306 | #else | ||
| 307 | <dl> | ||
| 308 | #foreach($prop in $properties) | ||
| 309 | #if (!$hiddenProperties.contains($prop.name)) | ||
| 310 | #set($discard = $fakeObj.set($prop.name, "$!{ann.get($prop.name)}")) | ||
| 311 | <dt class="annotationLabel"><label for="$prop.name">$prop.prettyName</label></dt> | ||
| 312 | <dd>#displayAnnotationProperty($annotatedDocument $fakeObj $prop $mode)</dd> | ||
| 313 | #end | ||
| 314 | #end | ||
| 315 | </dl> | ||
| 316 | #end | ||
| 317 | #end | ||
| 318 | |||
| 319 | #macro(displayAnnotationProperty $annotatedDocument $annotationObject $annotationProperty $mode) | ||
| 320 | #if ($mode == 'edit' || $mode == 'create') | ||
| 321 | ## This startupFocus parameter is used by the CKEditor WYSIWYG editor. | ||
| 322 | #set ($wysiwygEditorConfig = { | ||
| 323 | 'startupFocus': true | ||
| 324 | }) | ||
| 325 | ## We use displayEdit and not display('edit') because we want the input field name to be just the property name | ||
| 326 | ## (without the "DocumentReference_objectNumber_" prefix). | ||
| 327 | $annotatedDocument.displayEdit($annotationProperty, '', $annotationObject) | ||
| 328 | #else | ||
| 329 | ## We use display('view') and not displayView because we want the property value to be evaluated in the context of | ||
| 330 | ## the annotated document (i.e. isolated), not the current document. | ||
| 331 | #set ($output = $annotatedDocument.display($annotationProperty.name, 'view', $annotationObject)) | ||
| 332 | ## Remove the HTML macro wrapping because the output is later injected in another HTML macro with wiki=false. | ||
| 333 | #unwrapXPropertyDisplay($output) | ||
| 334 | #end | ||
| 335 | #end | ||
| 336 | {{/velocity}} |