$xwiki.jsfx.use('js/xwiki/viewers/attachments.js', {'forceSkinAction': true, 'language': ${xcontext.language}})
$xwiki.ssfx.use('js/xwiki/viewers/attachments.css', true)
###
### List document attachments
###
### TODO: web service?
###
#set($redirect = "$!{request.xredirect}")
#if($redirect == '')
  #set($redirect = "${doc.getURL()}#Attachments")
#end
#set($showactions = 0)
#set($attachments = $doc.attachmentList)
<div id="attachmentscontent" class="xwikiintracontent">
<div id="attw">
<div id="_attachments">
#if($attachments.size() > 0)
  #foreach ($attach in $attachments)
    <div class="attachment #if($velocityCount % 2 == 0) even #else odd #end">
    <span class="mime">#mimetypeimg($attach.getMimeType().toLowerCase() $attach.getFilename().toLowerCase())</span>
    <div class="information">
    <span class="name"><a href="$doc.getAttachmentURL(${attach.filename}, 'download')" title="$msg.get('core.viewers.attachments.download')">#packName($attach.filename)</a></span>
    <span class="xwikibuttonlinks">
    #if($hasEdit || $hasAdmin)
      ## Delete attachment link
      <a class="deletelink" href="$doc.getAttachmentURL(${attach.filename}, 'delattachment', "form_token=$!{services.csrf.getToken()}&amp;xredirect=$escapetool.url(${redirect})")"  title="$msg.get('core.viewers.attachments.deleteTitle', [$escapetool.xml($attach.filename)])">$msg.get('core.viewers.attachments.delete')</a>
      ## Dav Edit attachment link. We display the link hidden by default, and let the WebDAV JSX extension display it if
      ## the browser supports Dav Edit 
      #set($documentUrl=$doc.getExternalURL("download"))
      #set($attachmentUrl="$documentUrl/$attach.filename")
      #set($attachmentSignature="${request.contextPath}${request.servletPath}/download/")
      #set($webdavSignature="${request.contextPath}/webdav/spaces/")
      #set($fragments=$attachmentUrl.split($attachmentSignature))
      #if($listtool.size($fragments) == 2)
        #set($prefix=$listtool.get($fragments, 0))
        #set($suffix=$listtool.get($fragments, 1))
        #set($davUrl="$prefix$webdavSignature$suffix")
        <a title="$msg.get('core.viewers.attachments.webdavEdit.title')" class="editlink hidden" href="$davUrl" onclick="if(XWiki && XWiki.WebDAV){XWiki.WebDAV.davEdit('$davUrl')}; return false;">$msg.get('core.viewers.attachments.webdavEdit')</a>
      #end
    #end ## hasEdit
    #if("$!{oomanager.serverState}" == "Connected" && $services.officeviewer.isMimeTypeSupported($attach.getMimeType().toLowerCase()))
      <a class="viewlink" title="$msg.get('core.viewers.attachments.officeView.title')" href="${doc.getURL('view', "xpage=office&attachment=$escapetool.url($attach.filename)")}" target="_blank">$msg.get('core.viewers.attachments.officeView')</a>
    #end
    </span> ## xwikibuttonlinks
    <span class="version"><a href="$doc.getAttachmentURL(${attach.filename}, 'viewattachrev')" title="$msg.get('core.viewers.attachments.showHistory')">$attach.version</a></span>
    <div class="meta">
    <span class="publisher">$msg.get('core.viewers.attachments.author', [$!{xwiki.getUserName($attach.author)}])</span>
    <span class="date">$msg.get('core.viewers.attachments.date', [$!{xwiki.formatDate($attach.date)}])</span>
    <span class="size">(#dynamicsize($attach.filesize))</span>
    </div>
    </div>
    </div> ## row
  #end ## foreach
<script type="text/javascript">
// <![CDATA[
// Let XWiki.WebDAV JS display links only if the browser support WebDAV.
if (XWiki && XWiki.WebDAV) { XWiki.WebDAV.displayDavLinks($('_attachments')); }
// ]]>
</script>
#else ## No attachments
  <p class="noitems">$msg.get('core.viewers.attachments.noAttachments')</p>
#end
#if($hasEdit || $hasAdmin)
  <form action="$doc.getURL("upload")" enctype="multipart/form-data" method="post" id="AddAttachment">
  <div>
  ## CSRF prevention
  <input type="hidden" name="form_token" value="$!{services.csrf.getToken()}" />
  <input type="hidden" name="xredirect" value="$escapetool.xml($redirect)" />
  <fieldset id="attachform">
    <legend>$msg.get('core.viewers.attachments.upload.title')</legend>
    ## Temporarily disabled, until we fix attachment name handling
    ## <div><label id="xwikiuploadnamelabel" for="xwikiuploadname">$msg.get('core.viewers.attachments.upload.filename')</label></div>
    <div><input id="xwikiuploadname" type="hidden" name="filename" value="" size="40"/></div>
    <div><label class="hidden" for="xwikiuploadfile">$msg.get('core.viewers.attachments.upload.file')</label><input id="xwikiuploadfile" type="file" name="filepath" value="" size="40" class="uploadFileInput"/></div>
    <div>
    <span class="buttonwrapper"><input type="submit" value="$msg.get('core.viewers.attachments.upload.submit')" class="button"/></span>
    <span class="buttonwrapper"><a class="cancel secondary button" href="$doc.getURL()">$msg.get('core.viewers.attachments.upload.cancel')</a></span> 
    </div>
  </fieldset>
  </div>
  </form>
#end
</div> ## attachments
</div> ## attw
</div> ## attachmentscontent
