You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by je...@apache.org on 2016/01/08 12:05:43 UTC

[3/7] allura git commit: [#7998] ticket:878 added upload, delete attachments in wiki page via ajax

[#7998] ticket:878 added upload, delete attachments in wiki page via ajax


Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/b5df9ce1
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/b5df9ce1
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/b5df9ce1

Branch: refs/heads/ib/7998a
Commit: b5df9ce1f55e267e0c2caed29c2b306b1417075b
Parents: dfa249a
Author: Denis Kotov <de...@gmail.com>
Authored: Wed Dec 16 14:22:30 2015 +0200
Committer: Denis Kotov <de...@gmail.com>
Committed: Thu Jan 7 21:17:38 2016 +0200

----------------------------------------------------------------------
 .../templates/widgets/attachment_add.html       |  2 +
 .../templates/widgets/attachment_list.html      |  9 +--
 .../forgewiki/templates/wiki/page_edit.html     | 69 +++++++++++++++++++-
 ForgeWiki/forgewiki/wiki_main.py                |  2 +
 4 files changed, 76 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/b5df9ce1/Allura/allura/templates/widgets/attachment_add.html
----------------------------------------------------------------------
diff --git a/Allura/allura/templates/widgets/attachment_add.html b/Allura/allura/templates/widgets/attachment_add.html
index d1a2775..cc8dbf7 100644
--- a/Allura/allura/templates/widgets/attachment_add.html
+++ b/Allura/allura/templates/widgets/attachment_add.html
@@ -18,7 +18,9 @@
 -#}
 {% import 'allura:templates/jinja_master/lib.html' as lib with context %}
 <form method="post"
+      id="attachment_form"
       action="{{action}}"
+      onsubmit="{{onsubmit}}"
       enctype="multipart/form-data">
       <a href="#" class="btn link attachment_form_add_button">Add attachments</a>
       <div class="attachment_form_fields" style="display:none">

http://git-wip-us.apache.org/repos/asf/allura/blob/b5df9ce1/Allura/allura/templates/widgets/attachment_list.html
----------------------------------------------------------------------
diff --git a/Allura/allura/templates/widgets/attachment_list.html b/Allura/allura/templates/widgets/attachment_list.html
index d26a35e..45f6c24 100644
--- a/Allura/allura/templates/widgets/attachment_list.html
+++ b/Allura/allura/templates/widgets/attachment_list.html
@@ -17,7 +17,7 @@
        under the License.
 -#}
 {% import 'allura:templates/jinja_master/lib.html' as lib with context %}
-<div>
+<div class="attachments">
   {% if attachments %}
     {% set attachments = attachments|list %}
     <strong>Attachments:</strong>
@@ -29,7 +29,7 @@
             </a><br/>
             {{att.filename}}
             {% if edit_mode %}
-            <form method="post" action="{{att.url()}}">
+            <form method="post" action="{{att.url()}}" onsubmit="{{onsubmit}}">
               <input type="hidden" name="delete" value="True"/>
               <input type="submit" value="Delete File"/>
               {{lib.csrf_token()}}
@@ -38,10 +38,10 @@
           </div>
       {% endfor %}
     </div>
-
+    <div class="attachment_files">
     {% for att in attachments if not att.is_image() %}
       <div>
-        <form method="post" action="{{att.url()}}">
+        <form method="post" action="{{att.url()}}" onsubmit="{{onsubmit}}">
           <a href="{{att.url()}}">{{att.filename}}</a>
           ({{att.length}} bytes)
           <input type="hidden" name="delete" value="True"/>
@@ -54,6 +54,7 @@
         </form>
       </div>
     {% endfor %}
+    </div>
   {% endif %}
   <div style="clear:both"></div>
 </div>

http://git-wip-us.apache.org/repos/asf/allura/blob/b5df9ce1/ForgeWiki/forgewiki/templates/wiki/page_edit.html
----------------------------------------------------------------------
diff --git a/ForgeWiki/forgewiki/templates/wiki/page_edit.html b/ForgeWiki/forgewiki/templates/wiki/page_edit.html
index 31ff5ae..69304a2 100644
--- a/ForgeWiki/forgewiki/templates/wiki/page_edit.html
+++ b/ForgeWiki/forgewiki/templates/wiki/page_edit.html
@@ -74,9 +74,9 @@
     {{lib.csrf_token()}}
 </form>
 <div class="grid-19">
-  {{c.attachment_list.display(attachments=page.attachments, edit_mode=page_exists and h.has_access(page, 'edit')())}}
+  {{c.attachment_list.display(onsubmit='del_attach(this); return false;', attachments=page.attachments, edit_mode=page_exists and h.has_access(page, 'edit')())}}
   {% if page_exists and h.has_access(page, 'edit')() %}
-    {{c.attachment_add.display(action=page.url() + 'attach', name='file_info')}}
+    {{c.attachment_add.display(onsubmit='send_attach(); return false;', name='file_info')}}
   {% endif %}
 </div>
 {{c.confirmation.display(content='')}}
@@ -100,5 +100,70 @@
     {% endif %}
   });
   /*]]>*/
+
+  function update_attachments(attachment_url, filename, filesize){
+    if ($('.attachment_files').length + $('.attachment_files').length == 0){
+      var attach_divs = '<strong>Attachments:</strong>' +
+                        '<div class="attachment_images"></div>' +
+                        '<div class="attachment_files"></div>';
+      $(attach_divs).prependTo($('.attachments'));
+    };
+    var ext = filename.split('.').pop().toLowerCase();
+    if($.inArray(ext, ['gif','png','jpg','jpeg']) != -1) {
+      new_attach = '<div class="attachment_thumb">' +
+                     '<a href="' + attachment_url + '">' +
+                     '<img src="' + attachment_url + '/thumb" alt="Thumbnail"/>' +
+                     '</a><br/>' + filename +
+                     '<form method="post" action="' + attachment_url + '">' +
+                       '<input type="hidden" name="delete" value="True"/>' +
+                       '<input type="submit" value="Delete File"/>' +
+                     '</form>' +
+                   '</div>';
+      $(new_attach).prependTo($('.attachment_images'));
+    } else {
+      var new_attach ='<form method="post" action="' + attachment_url + '">' +
+                        '<a href="' + attachment_url + '">' + filename + '</a> (' + filesize + ' bytes)' +
+                        '<input type="hidden" name="delete" value="True"/>' +
+                        '<span>' +
+                          '<input type="submit" value="Delete File"/>' +
+                        '</span>' +
+                      '</form>';
+      $(new_attach).prependTo($('.attachment_files'));
+    };
+  };
+
+  function send_attach() {
+    var page_url = '{{page.url()}}';
+    $.ajax({
+      type: "POST",
+      url: page_url + 'attach',
+      data: new FormData($('#attachment_form')[0]),
+      processData: false,
+      contentType: false,
+      headers: {
+        "X-CSRF-Token": $.cookie('_session_id')
+      },
+      success: function(data) {
+        var attachment_name = $("#file_info").val();
+        var attachment_url = page_url + 'attachment/' + attachment_name;
+        update_attachments(attachment_url, attachment_name, $("#file_info")[0].files[0].size);
+      }
+    });
+  };
+
+  function del_attach(this) {
+    var attr_url = $(this).attr('action');
+    $.ajax({
+      type: "POST",
+      url: attr_url,
+      data: $(this).serialize(),
+      headers: {
+        "X-CSRF-Token": $.cookie('_session_id')
+      },
+      success:  function() {
+        $(this).parent().remove();
+      }
+    });
+  };
 </script>
 {% endblock %}

http://git-wip-us.apache.org/repos/asf/allura/blob/b5df9ce1/ForgeWiki/forgewiki/wiki_main.py
----------------------------------------------------------------------
diff --git a/ForgeWiki/forgewiki/wiki_main.py b/ForgeWiki/forgewiki/wiki_main.py
index b08d03e..8bfa863 100644
--- a/ForgeWiki/forgewiki/wiki_main.py
+++ b/ForgeWiki/forgewiki/wiki_main.py
@@ -593,6 +593,8 @@ class PageController(BaseController, FeedController):
             page = self.page
         else:
             page = self.fake_page()
+        if request.headers.get('X-Requested-With', None) == 'XMLHttpRequest':
+            return
         c.confirmation = W.confirmation
         c.markdown_editor = W.markdown_editor
         c.attachment_add = W.attachment_add