Wiki-Quellcode von LiveTableGenerator
Zuletzt geändert von admin am 2025/02/23 10:22
Verstecke letzte Bearbeiter
| author | version | line-number | content |
|---|---|---|---|
| |
1.1 | 1 | {{velocity output="false" filter="none"}} |
| 2 | #macro(escapeSingleQuotes $string) | ||
| 3 | $string.replace("'", "''")## | ||
| 4 | #end | ||
| 5 | |||
| 6 | #macro(prepareColumnProperties) | ||
| 7 | #set ($columnProperties = {}) | ||
| 8 | #set ($genericColumns = ['doc.title', 'doc.name', 'doc.space', 'doc.fullName', 'doc.location', 'doc.author', 'doc.date', 'doc.creator', 'doc.creationDate']) | ||
| 9 | #set ($specialColumns = ['_avatar', '_images', '_attachments', '_actions']) | ||
| 10 | ## Field types that aren't sortable nor filterable. | ||
| 11 | #set ($unsortableFieldTypes = ['TextArea', 'ComputedField']) | ||
| 12 | ## Field types that have HTML display. | ||
| 13 | #set ($htmlFieldTypes = ['TextArea', 'Users', 'Groups', 'Page']) | ||
| 14 | #foreach ($column in $columns) | ||
| 15 | #set ($field = $xclass.get($column)) | ||
| 16 | #if ($field.getProperty('customDisplay').value == '{{include reference="AppWithinMinutes.Title"/}}') | ||
| 17 | ## This field is a place-holder for the document title. | ||
| 18 | #set ($titleDisplayNamePlaceHolder = $field.translatedPrettyName) | ||
| 19 | #set ($field = $NULL) | ||
| 20 | #set ($column = 'doc.title') | ||
| 21 | #set ($discard = $columns.set($mathtool.sub($foreach.count, 1), $column)) | ||
| 22 | #end | ||
| 23 | #if ($field) | ||
| 24 | #set ($properties = {}) | ||
| 25 | ## Sortable/Filterable columns. | ||
| 26 | #if ($unsortableFieldTypes.contains($field.classType)) | ||
| 27 | #set ($discard = $properties.put('filterable', false)) | ||
| 28 | #set ($discard = $properties.put('sortable', false)) | ||
| 29 | #end | ||
| 30 | ## Columns with HTML display. | ||
| 31 | ## Note that the check for custom display is too generic because you can have a custom displayer that generates | ||
| 32 | ## HTML in edit mode and plain text in view mode. | ||
| 33 | #if ($htmlFieldTypes.contains($field.classType) || $field.getProperty('customDisplay').value != '') | ||
| 34 | #set ($discard = $properties.put('html', true)) | ||
| 35 | #end | ||
| 36 | #elseif ($genericColumns.contains($column)) | ||
| 37 | #set ($properties = {}) | ||
| 38 | #if ($column == 'doc.title') | ||
| 39 | #if ("$!titleDisplayNamePlaceHolder" != '') | ||
| 40 | #set ($discard = $properties.put('displayName', $titleDisplayNamePlaceHolder)) | ||
| 41 | #set ($titleDisplayNamePlaceHolder = $NULL) | ||
| 42 | #end | ||
| 43 | #end | ||
| 44 | #if ($column == 'doc.location') | ||
| 45 | #set ($discard = $properties.put('html', true)) | ||
| 46 | #elseif ($column == 'doc.creator' || $column == 'doc.author') | ||
| 47 | #set ($discard = $properties.put('link', 'author')) | ||
| 48 | #elseif ($column != 'doc.date' && $column != 'doc.creationDate') | ||
| 49 | #set ($discard = $properties.put('link', 'view')) | ||
| 50 | #end | ||
| 51 | #elseif ($specialColumns.contains($column)) | ||
| 52 | #set ($properties = {'sortable': false, 'filterable': false, 'html': true}) | ||
| 53 | #if ($column == '_actions') | ||
| 54 | #set ($discard = $properties.put('actions', ['edit', 'delete'])) | ||
| 55 | #end | ||
| 56 | #end | ||
| 57 | #set ($discard = $columnProperties.put($column, $properties)) | ||
| 58 | #end | ||
| 59 | #end | ||
| 60 | |||
| 61 | #macro(generateColumnProperties) | ||
| 62 | #set ($discard = "#prepareColumnProperties()") | ||
| 63 | ${escapetool.h}set (${escapetool.d}columnsProperties = { | ||
| 64 | #foreach($entry in $columnProperties.entrySet()) | ||
| 65 | '#escapeSingleQuotes($entry.key)': $jsontool.serialize($entry.value)#if ($foreach.hasNext),#end | ||
| 66 | |||
| 67 | #end | ||
| 68 | }) | ||
| 69 | #end | ||
| 70 | |||
| 71 | #macro(maybeGenerateLiveTable) | ||
| 72 | #set($liveTableObj = $doc.getObject('AppWithinMinutes.LiveTableClass')) | ||
| 73 | #if($liveTableObj) | ||
| 74 | #set($columns = $liveTableObj.getProperty('columns').value.trim()) | ||
| 75 | #if("$!columns" != '') | ||
| 76 | #set($columns = $columns.split('\s+')) | ||
| 77 | #generateLiveTable() | ||
| 78 | #end | ||
| 79 | #end | ||
| 80 | #end | ||
| 81 | |||
| 82 | #macro(generateLiveTable) | ||
| 83 | {{{## | ||
| 84 | {{velocity}} | ||
| 85 | #set($className = $liveTableObj.getProperty('class').value) | ||
| 86 | #set($classDoc = $xwiki.getDocument($className)) | ||
| 87 | #set($xclass = $classDoc.getxWikiClass()) | ||
| 88 | #set($shortName = $classDoc.documentReference.name.replaceAll("Class", "").toLowerCase()) | ||
| 89 | #generateColumnProperties() | ||
| 90 | ${escapetool.h}set (${escapetool.d}options = { | ||
| 91 | 'className': '#escapeSingleQuotes($className)', | ||
| 92 | 'translationPrefix': '#escapeSingleQuotes($shortName).livetable.', | ||
| 93 | 'tagCloud': true, | ||
| 94 | 'rowCount': 15, | ||
| 95 | 'maxPages': 10, | ||
| 96 | 'selectedColumn': '#escapeSingleQuotes($columns.get(0))', | ||
| 97 | 'defaultOrder': 'asc' | ||
| 98 | }) | ||
| 99 | ${escapetool.h}set (${escapetool.d}columns = [#foreach($column in $columns)#if($foreach.count > 1), #end'#escapeSingleQuotes($column)'#end]) | ||
| 100 | ${escapetool.h}livetable('#escapeSingleQuotes($shortName)' ${escapetool.d}columns ${escapetool.d}columnsProperties ${escapetool.d}options) | ||
| 101 | {{/velocity}}## | ||
| 102 | }}} | ||
| 103 | #end | ||
| 104 | |||
| 105 | #** | ||
| 106 | * We keep this macro only to preserve backwards compatibility with applications created on versions older than 4.5M1. | ||
| 107 | * New applications don't use this document as a custom live table results page any more. A dedicated live table results | ||
| 108 | * page is now generated for each application. | ||
| 109 | * | ||
| 110 | * @deprecated since 4.5M1 | ||
| 111 | *# | ||
| 112 | #macro (outputLiveTableResults) | ||
| 113 | #set ($output = "#gridresult($!request.classname $request.collist.split(','))") | ||
| 114 | ## We have to escape the translation key because it appears as JSON value in the output. | ||
| 115 | #set ($escapedEmptyValueTranslationKey = $escapetool.javascript("${request.transprefix}emptyvalue")) | ||
| 116 | $output.replace($escapedEmptyValueTranslationKey, '-') | ||
| 117 | #end | ||
| 118 | {{/velocity}}{{velocity}} | ||
| 119 | ## We keep the following code only to preserve backwards compatibility with old applications (<4.5M1) that use this | ||
| 120 | ## document as a live table results page. For these applications we need to distinguish between a request for generating | ||
| 121 | ## a live table and a request for live table data. | ||
| 122 | #set ($isLiveTableResultsRequest = $xcontext.action == 'get' && "$!{request.outputSyntax}" == 'plain') | ||
| 123 | ## We do the include separately because the wiki syntax is evaluated after the Velocity code. | ||
| 124 | #if ($isLiveTableResultsRequest) | ||
| 125 | {{include reference="XWiki.LiveTableResultsMacros" /}} | ||
| 126 | #end | ||
| 127 | {{/velocity}}{{velocity wiki="false"}} | ||
| 128 | ## Old applications need custom live table results just because they don't have a document translation bundle generated. | ||
| 129 | #if ($isLiveTableResultsRequest) | ||
| 130 | #outputLiveTableResults() | ||
| 131 | #end | ||
| 132 | {{/velocity}}{{velocity}} | ||
| 133 | #if (!$isLiveTableResultsRequest) | ||
| 134 | #maybeGenerateLiveTable() | ||
| 135 | #end | ||
| 136 | {{/velocity}} |