Änderungen von Dokument Attachments

Zuletzt geändert von xwikiadmin am 2026/03/05 08:03

Von Version 3.1
bearbeitet von xwikiadmin
am 2023/10/26 09:37
Änderungskommentar: Install extension [com.xwiki.pro:xwiki-pro-macros/1.9.2]
Auf Version 2.1
bearbeitet von xwikiadmin
am 2023/10/26 09:24
Änderungskommentar: Migrated property [type] from class [XWiki.WikiMacroParameterClass]

Zusammenfassung

Details

Seiteneigenschaften
Inhalt
... ... @@ -5,7 +5,6 @@
5 5  * {{{ labels }}} is not supported since XWiki does not support attachment tags
6 6  * {{{ preview }}} does not make sense for the XWiki macro as attachments are displayed differently
7 7  * {{{ old }}} has not been implemented in this bridge macro because it does not make really sense for XWiki
8 -* {{{ upload }}} has not been implemented yet because it conflicts with the standard attachment panel upload
9 9  * {{{ "created date" }}} value of the {{{ sortBy }}} property behave the same way as the {{{ "date" }}} value
10 10  
11 11  = Parameters =
... ... @@ -14,6 +14,7 @@
14 14  |**patterns**|Comma-separated list of regular expressions, used to filter attachments by name.|No|
15 15  |**sortBy**|Sort attachments by {{{ date }}}, {{{ size }}} or {{{ name }}}|No|{{{ date }}}
16 16  |**sortOrder**|Sort attachments in {{{ ascending }}} or {{{ descending }}} order|No|{{{ ascending }}}
16 +|**upload**|Allow users to attach new files|No|{{{ true }}}
17 17  |**page**|Pages containing the attachments to display. Current page if empty.|No|
18 18  
19 19  = Example Usage =
... ... @@ -20,7 +20,7 @@
20 20  
21 21  
22 22  {{code}}
23 -{{confluence_attachments
23 +{{attachments
24 24   patterns=".*png"
25 25   sortBy="name"
26 26  /}}
XWiki.WikiMacroClass[0]
Makro-Code
... ... @@ -1,6 +1,48 @@
1 1  {{velocity output="false"}}
2 -#macro (getLiveDataSort $return)
3 - ##property
2 +#macro (filterAndSortAttachments $attachmentsFiltered $invalidSortBy $invalidSortOrder)
3 + $xwiki.ssx.use('Confluence.Macros.Attachments')
4 + $xwiki.jsx.use('Confluence.Macros.Attachments')
5 + #if ("$!{request.forceTestRights}" == "1")#template("xwikivars.vm")#end
6 + #template('attachment_macros.vm')
7 + ## Get attachments
8 + #if ("$!wikimacro.parameters.tags" != '')
9 + #set ($tags = $!wikimacro.parameters.tags.split(','))
10 + #set ($pageReferenceSet = $collectiontool.getSet())
11 + #foreach ($tag in $tags)
12 + #set ($references = $xwiki.tag.getDocumentsWithTag("$tag"))
13 + #set ($discard = $pageReferenceSet.addAll($references))
14 + #end
15 + #set ($attachments = [])
16 + #foreach ($pageReference in $pageReferenceSet)
17 + #set ($document = $xwiki.getDocument($pageReference))
18 + #set ($documentAttachments = $document.attachmentList)
19 + #set ($discard = $attachments.addAll($documentAttachments))
20 + #end
21 + #else
22 + #set ($attachments = $doc.attachmentList)
23 + #if ("$!wikimacro.parameters.page" != '')
24 + #set ($document = $xwiki.getDocument("$!wikimacro.parameters.page"))
25 + #set ($attachments = $document.attachmentList)
26 + #end
27 + #end
28 + ## Filter attachments
29 + #set ($confluencePatterns = "$!wikimacro.parameters.patterns")
30 + #if ($confluencePatterns == '')
31 + #set($attachmentsFiltered = $attachments)
32 + #else
33 + #set ($patterns = $!confluencePatterns.split(','))
34 + #set($attachmentsFiltered = [])
35 + #foreach ($attachment in $attachments)
36 + #foreach ($pattern in $patterns)
37 + #set ($matches = $attachment.filename.matches("(?i)$!pattern"))
38 + #if ($matches)
39 + #set ($discard = $attachmentsFiltered.add($attachment))
40 + #break
41 + #end
42 + #end
43 + #end
44 + #end
45 + ## Sort attachments
4 4   #set ($confluenceSortBy = "$!wikimacro.parameters.sortBy")
5 5   #set ($invalidSortBy = false)
6 6   #if ($confluenceSortBy == 'date')
... ... @@ -14,7 +14,6 @@
14 14   #else
15 15   #set ($invalidSortBy = true)
16 16   #end
17 - ## order
18 18   #set ($confluenceSortOrder = "$!wikimacro.parameters.sortOrder")
19 19   #set ($invalidSortOrder = false)
20 20   #set ($reverseSortOrderProperties = ['filesize', 'date'])
... ... @@ -31,103 +31,101 @@
31 31   #set ($sortOrder = 'desc')
32 32   #end
33 33   #else
34 - #set ($invalidSortOrder = true)
75 + #set ($invalidSortOrder = false)
35 35   #end
36 - ## return
37 - #if ($invalidSortBy || $invalidSortOrder)
38 - #setVariable("$return", {})
39 - #else
40 - #setVariable("$return", "$sortBy:$sortOrder")
41 - #end
77 + #set ($attachmentsFiltered = $collectiontool.sort($attachmentsFiltered, "$sortBy:$sortOrder"))
42 42  #end
43 43  
44 -#set ($confluenceAttachmentMacroIndex = -1)
45 -## Code taken and adapted
46 -#macro (showConfluenceAttachments $document)
47 - #template('attachment_macros.vm')
48 - #template('display_macros.vm')
49 - #initRequiredSkinExtensions()
50 - #set ($confluenceAttachmentMacroIndex = $confluenceAttachmentMacroIndex + 1)
51 - #set ($confluenceAttachmentMacroId = "confluenceAttachments$confluenceAttachmentMacroIndex")
52 - ##
53 - #showConfluenceAttachmentsLiveData($document $confluenceAttachmentMacroId)
54 -#end
55 -
56 -## Display a liveData with attachments from the specified document.
57 -#macro (showConfluenceAttachmentsLiveData $attachmentsDoc $liveDataId)
58 - #set ($liveDataConfig = {
59 - 'meta': {
60 - 'propertyDescriptors': [
61 - { 'id': 'mimeType', 'displayer': 'html'},
62 - { 'id': 'filename', 'displayer': 'html' },
63 - { 'id': 'filesize', 'displayer': 'html' },
64 - { 'id': 'date', 'filter': 'date'},
65 - { 'id': 'author', 'displayer': 'html' }
66 - ],
67 - 'entryDescriptor': {
68 - 'idProperty': 'id'
69 - }
70 - }
71 - })
72 -
73 - #if ("$!wikimacro.parameters.patterns" != '')
74 - #set ($liveDataConfig.query = {})
75 - #set ($liveDataConfig.query.filters = [
76 - {
77 - "property": "filename",
78 - "matchAll": true,
79 - "constraints": []
80 - }
81 - ])
82 - #set ($filters = $stringtool.split($wikimacro.parameters.patterns, ','))
83 - #foreach ($filter in $filters)
84 - #set ($discard = $liveDataConfig.query.filters[0].constraints.add(
85 - { "operator": "contains", "value": "$!filter.trim()" }
86 - ))
87 - #end
88 - #end
89 - #set ($sourceParameters = $escapetool.url({
90 - 'template': 'xpart.vm',
91 - 'translationPrefix': 'core.viewers.attachments.livetable.',
92 - 'className': 'XWiki.AllAttachments',
93 - "\$doc": "$attachmentsDoc",
94 - 'vm': 'attachmentsjson.vm'
95 - }))
96 - #getLiveDataSort($liveDataSort)
80 +#macro (executeMacro)
81 + #filterAndSortAttachments($attachmentsFiltered $invalidSortBy $invalidSortOrder)
97 97   #if ($invalidSortBy)
98 - {{warning}}
99 - Attachment macro: sortBy parameter must be one of 'date', 'size', or 'name'
100 - {{/warning}}
83 + {{error}}
84 + Attachment macro error: sortBy parameter must be one of 'date', 'size', or 'name'
85 + {{/error}}
86 + #break
101 101   #end
102 102   #if ($invalidSortOrder)
103 - {{warning}}
104 - Attachment macro: sortOrder parameter must be one of 'ascending' or 'descending'
105 - {{/warning}}
89 + {{error}}
90 + Attachment macro error: sortOrder parameter must be one of 'ascending' or 'descending'
91 + {{/error}}
92 + #break
106 106   #end
94 + ## Display attachments
95 + {{html clean="false"}}
96 + <div class="confluence-attachment-macro">
97 + #if ($attachmentsFiltered.size() > 0)
98 + #displayAttachments($attachmentsFiltered)
99 + #deleteAttachmentModal()
100 + #else
101 + <p class="noitems">
102 + $!escapetool.xml($services.localization.render('core.viewers.attachments.noAttachments'))
103 + </p>
104 + #end
107 107  
108 - {{liveData
109 - id="$liveDataId"
110 - properties="mimeType,filename,filesize,date,author"
111 - source='liveTable'
112 - sourceParameters="$sourceParameters"
113 - sort="$liveDataSort"
114 - limit=5
115 - }}$jsontool.serialize($liveDataConfig){{/liveData}}
106 + ## Allow upload
107 + #set ($upload = "$!wikimacro.parameters.upload")
108 + #if ($upload == 'true' && ($hasEdit || $hasAdmin) && $xcontext.action == 'view')
109 + <form
110 + id="AddAttachment"
111 + action="$doc.getURL("upload")"
112 + method="post"
113 + enctype="multipart/form-data"
114 + >
115 + <div>
116 + ## CSRF prevention
117 + <input
118 + type="hidden"
119 + name="form_token"
120 + value="$!{services.csrf.getToken()}"
121 + />
116 116  
117 - {{html clean="false"}}#deleteAttachmentModal(){{/html}}
118 -#end
123 + <fieldset id="attachform">
124 + <legend>
125 + $!escapetool.xml($services.localization.render('core.viewers.attachments.upload.title'))
126 + </legend>
119 119  
128 + <div class="fileupload-field">
129 + <label
130 + class="hidden"
131 + for="xwikiuploadfile"
132 + >
133 + $!escapetool.xml($services.localization.render('core.viewers.attachments.upload.file'))
134 + </label>
120 120  
121 -#macro (executeMacro)
122 - #set ($document = $doc)
123 - #if ("$!wikimacro.parameters.page" != '')
124 - #set ($document = $xwiki.getDocument("$!wikimacro.parameters.page"))
125 - #end
136 + <input
137 + id="xwikiuploadfile"
138 + class="uploadFileInput noitems"
139 + type="file"
140 + name="filepath"
141 + size="40"
142 + data-max-file-size="$!escapetool.xml($xwiki.getSpacePreference('upload_maxsize'))"
143 + />
144 + </div>
126 126  
127 - {{html clean="false" wiki="true"}}
128 - #showConfluenceAttachments($document)
129 - {{/html}}
146 + <div>
147 + <span class="buttonwrapper">
148 + <input
149 + class="button btn btn-primary"
150 + type="submit"
151 + value="$!escapetool.xml($services.localization.render('core.viewers.attachments.upload.submit'))"
152 + />
153 + </span>
130 130  
155 + <span class="buttonwrapper">
156 + <a
157 + class="cancel secondary button btn btn-primary"
158 + href="$doc.getURL()"
159 + >
160 + $!escapetool.xml($services.localization.render('core.viewers.attachments.upload.cancel'))
161 + </a>
162 + </span>
163 + </div>
164 + </fieldset>
165 + </div>
166 + </form>
167 + #end
168 + </div> ## .confluence-attachment-macro
169 + {{/html}}
131 131  #end
132 132  {{/velocity}}
133 133  
Standardkategorie
... ... @@ -1,0 +1,1 @@
1 +confluence
XWiki.JavaScriptExtension[0]
Pufferstrategie
... ... @@ -1,0 +1,1 @@
1 +long
Code
... ... @@ -1,0 +1,8 @@
1 +require(['jquery', 'xwiki-events-bridge'], function ($) {
2 + $('#xwikiuploadfile').on('xwiki:html5upload:message', function (event, data) {
3 + if (data.content === 'UPLOAD_FINISHED' && data.type === 'done') {
4 + location.reload();
5 + }
6 + });
7 +});
8 +
Inhalt parsen
... ... @@ -1,0 +1,1 @@
1 +Nein
Benutze diese Erweiterung
... ... @@ -1,0 +1,1 @@
1 +onDemand
XWiki.StyleSheetExtension[0]
Pufferstrategie
... ... @@ -1,0 +1,1 @@
1 +long
Code
... ... @@ -1,0 +1,15 @@
1 +.confluence-attachment-macro {
2 + .upload-response {
3 + display: none;
4 + }
5 +
6 + #AddAttachment {
7 + margin-top: 15px;
8 +
9 + #attachform {
10 + .buttonwrapper {
11 + display: none;
12 + }
13 + }
14 + }
15 +}
Content Type
... ... @@ -1,0 +1,1 @@
1 +LESS
Inhalt parsen
... ... @@ -1,0 +1,1 @@
1 +Nein
Benutze diese Erweiterung
... ... @@ -1,0 +1,1 @@
1 +onDemand