Changes for page Attachments Macro

Last modified by Drunk Monkey on 2023-04-26 18:21

From version 4.1
edited by Drunk Monkey
on 2022-08-02 10:47
Change comment: Migrated property [defaultCategories] from class [XWiki.WikiMacroClass]
To version 2.1
edited by Drunk Monkey
on 2021-03-01 08:43
Change comment: Migrated property [priority] from class [XWiki.WikiMacroClass]

Summary

Details

Page properties
Content
... ... @@ -1,4 +1,7 @@
1 1  {{velocity output="false"}}
2 +#if ("$!request.xpage" == 'plain')
3 + $response.setContentType('application/json')
4 +#end
2 2  ##
3 3  ## Offset = item # at which to start displaying data
4 4  ## -------------------------------
... ... @@ -18,7 +18,7 @@
18 18   #set ($limit = '10')
19 19  #end
20 20  #set ($limit = $mathtool.max($numbertool.integer($limit), 1))
21 -##
24 +##
22 22  ## Document parameter
23 23  ## -------------------------------
24 24  #set ($aDocFullName = "$!request.document")
... ... @@ -25,7 +25,7 @@
25 25  #if ($aDocFullName != '' && $xwiki.exists($aDocFullName))
26 26   #set ($aDoc = $xwiki.getDocument($aDocFullName))
27 27  #end
28 -##
31 +##
29 29  ## Sort parameter
30 30  ## -------------------------------
31 31  #set ($sortColumn = "$!request.sort.toLowerCase()")
... ... @@ -38,7 +38,7 @@
38 38   #set ($filterByName = true)
39 39   #set ($namePattern = "$!request.name.toLowerCase()")
40 40  #end
41 -##
44 +##
42 42  ## Macros
43 43  ## -------------------------------
44 44  #macro (getOrderedAttachmentNameList)
... ... @@ -51,7 +51,7 @@
51 51   #if ($sortColumn == 'date')
52 52   #set ($prefix = $datetool.format('yyyyMMddHHmmss', $attach.date))
53 53   #elseif ($sortColumn == 'size')
54 - #set ($fileSize = "$!attach.longSize")
57 + #set ($fileSize = "$!attach.filesize")
55 55   #set ($maxFileSize = "100000000000")
56 56   #set ($index = $maxFileSize.length() - $fileSize.length())
57 57   #set ($prefix = $maxFileSize.substring(0, $index) + $fileSize)
... ... @@ -60,7 +60,7 @@
60 60   #set ($discard = $attachmentComposedNameList.add($attachComposedName))
61 61   #end
62 62   #end
63 - #set ($orderedAttachmentComposedNameList = $collectiontool.sort($attachmentComposedNameList))
66 + #set ($orderedAttachmentComposedNameList = $sorttool.sort($attachmentComposedNameList))
64 64   #foreach ($attachComposedName in $orderedAttachmentComposedNameList)
65 65   #set ($authorLastIndex = $attachComposedName.indexOf('|') + 1)
66 66   #set ($attachName = $attachComposedName.substring($authorLastIndex))
... ... @@ -79,7 +79,7 @@
79 79   #set ($discard = $orderedAttachmentNameList.add($attach.filename))
80 80   #end
81 81   #end
82 - #set ($orderedAttachmentNameList = $collectiontool.sort($orderedAttachmentNameList))
85 + #set ($orderedAttachmentNameList = $sorttool.sort($orderedAttachmentNameList))
83 83   #elseif ($sortColumn == 'type')
84 84   #set ($attachmentReversedNameList = [])
85 85   #set ($orderedAttachmentReversedNameList = [])
... ... @@ -94,7 +94,7 @@
94 94   #set ($discard = $attachmentReversedNameList.add($attachReversedName))
95 95   #end
96 96   #end
97 - #set ($orderedAttachmentReversedNameList = $collectiontool.sort($attachmentReversedNameList))
100 + #set ($orderedAttachmentReversedNameList = $sorttool.sort($attachmentReversedNameList))
98 98   #foreach ($attachReversedName in $orderedAttachmentReversedNameList)
99 99   #set ($extensionLastIndex = $attachReversedName.indexOf('|'))
100 100   #set ($nameIndex = $extensionLastIndex + 1)
... ... @@ -108,7 +108,7 @@
108 108  ##
109 109  ## Determine the attachments sub list to display.
110 110  ## -------------------------------
111 -#if ($services.security.authorization.hasAccess('view', $aDoc.documentReference))
114 +#if ($xwiki.hasAccessLevel('view', $context.user, $aDoc.getPrefixedFullName()))
112 112   #set ($totalRows = $orderedAttachmentNameList.size())
113 113   #set ($returnedRows = $mathtool.min($limit, $totalRows))
114 114   #if ($offset + $returnedRows > $totalRows)
... ... @@ -115,7 +115,7 @@
115 115   #set ($returnedRows = $totalRows - $offset + 1)
116 116   #end
117 117   #if ($request.dir == 'desc')
118 - #set ($discard = $collectiontool.reverseModifiable($orderedAttachmentNameList))
121 + #set ($discard = $collectionstool.reverse($orderedAttachmentNameList))
119 119   #end
120 120   #set ($start = $offset - 1)
121 121   #set ($end = $start + $returnedRows)
... ... @@ -140,19 +140,21 @@
140 140   "reqNo": $numbertool.integer($request.reqNo),
141 141   "rows": []
142 142  })
146 +## $type is required by the user display.
147 +#set ($type = 'view')
143 143  #foreach ($attachName in $attachNames)
144 144   #set ($attach = $aDoc.getAttachment($attachName))
150 + ## $value is required by the user display
151 + #set ($value = $attach.author)
145 145   #set ($discard = $json.rows.add({
146 146   "name": $attachName,
147 147   "name_url": $aDoc.getAttachmentURL($attachName),
148 148   "type": "#mimetypeimg($attach.mimeType.toLowerCase() $attach.filename.toLowerCase())",
149 - "size": "#dynamicsize($attach.longSize)",
150 - "author": "#displayUser($attach.author)",
156 + "size": "#dynamicsize($attach.filesize)",
157 + "author": "#template('displayer_users.vm')",
151 151   "date": "$!xwiki.formatDate($attach.date)",
152 152   "doc_viewable": true
153 153   }))
154 154  #end
155 -#if ($xcontext.action == 'get')
156 - #jsonResponse($json)
157 -#end
162 +$jsontool.serialize($json)
158 158  {{/velocity}}
XWiki.WikiMacroClass[0]
Cached
... ... @@ -1,1 +1,0 @@
1 -No
Asynchronous rendering
... ... @@ -1,1 +1,0 @@
1 -No
Macro code
... ... @@ -1,33 +1,33 @@
1 1  {{velocity}}
2 2  #set ($discard = $xwiki.ssfx.use('uicomponents/widgets/userpicker/userPicker.css', true))
3 3  #set ($discard = $services.localization.use('document', 'Main.AttachmentsMacroTranslations'))
4 -##
4 +##
5 5  ## Sort parameter
6 -## -------------------------------
6 +## -------------------------------
7 7  #set ($sortList = ['name', 'type', 'size', 'author', 'date'])
8 -#set ($aSort = "$!xcontext.macro.params.get('sort').toLowerCase()")
8 +#set ($aSort = "$!context.macro.params.get('sort').toLowerCase()")
9 9  #if (!$sortList.contains($aSort))
10 10   #set ($aSort = 'name')
11 11  #end
12 -##
12 +##
13 13  ## Order parameter
14 14  ## -------------------------------
15 -#set ($aOrder = "$!xcontext.macro.params.get('order').toLowerCase()")
15 +#set ($aOrder = "$!context.macro.params.get('order').toLowerCase()")
16 16  #if ($aOrder != 'desc')
17 17   #set ($aOrder = 'asc')
18 18  #end
19 -##
19 +##
20 20  ## Limit parameter
21 21  ## -------------------------------
22 -#set ($limit = "$!xcontext.macro.params.get('limit')")
22 +#set ($limit = "$!context.macro.params.get('limit')")
23 23  #if ($limit == '')
24 24   #set ($limit = '10')
25 25  #end
26 26  #set ($aLimit = $numbertool.integer($limit))
27 -##
27 +##
28 28  ## Document parameter
29 29  ## -------------------------------
30 -#set ($aDocFullName = "$!xcontext.macro.params.get('document')")
30 +#set ($aDocFullName = "$!context.macro.params.get('document')")
31 31  #if ($aDocFullName != '' && $xwiki.exists($aDocFullName))
32 32   #set ($aDoc = $aDocFullName)
33 33  #else