]> git.0d.be Git - panikdb.git/blobdiff - panikdb/static/js/combo.wiki.js
wiki: add document upload support
[panikdb.git] / panikdb / static / js / combo.wiki.js
index a94d8cb6876a4913a62051a9ef2f96f6fb9cef51..d1464e3f7b145d3fd260029b27e0b6700b782c29 100644 (file)
@@ -39,6 +39,7 @@ function auto_anchors() {
           {name: 'intertitre', tag: 'H4', klass: 'intertitle'},
           {name: 'liste', special: 'list', tag: 'UL', klass: 'list'},
           {name: 'illustration', special: 'img', tag: 'DIV', subtag: true, klass: 'figure'},
+          {name: 'document', special: 'doc', tag: 'DIV', subtag: true, klass: 'document'},
           {name: 'code', tag: 'PRE', klass: 'code'},
           {name: 'note', tag: 'DIV', subtag: true, klass: 'note'},
     ],
@@ -133,20 +134,30 @@ function auto_anchors() {
           $(inline_style_toolbar).hide();
         }
       });
-      var $image_upload = $('<input type="file" nam="image" id="image-upload">');
+      var $image_upload = $('<input type="file" nam="image" id="image-upload" accept="image/*">');
       $image_upload.on('change', upload_image);
       $image_upload.appendTo(document.body);
 
+      var $document_upload = $('<input type="file" nam="document" id="document-upload">');
+      $document_upload.on('change', upload_document);
+      $document_upload.appendTo(document.body);
+
       document.execCommand('defaultParagraphSeparator', false, 'p');
       $(document).on('click', 'div.figure span.empty', function() {
         window.active_figure = this.parentNode;
         $('#image-upload').trigger('click');
         return true;
       });
+      $(document).on('click', 'div.document span.empty', function() {
+        window.active_document = this.parentNode;
+        $('#document-upload').trigger('click');
+        return true;
+      });
     },
 
     off: function() {
       $('#image-upload').remove();
+      $('#document-upload').remove();
       if (block_style_toolbar) { block_style_toolbar.hide(); }
       if (inline_style_toolbar) { inline_style_toolbar.hide(); }
       $(document).off('selectionchange');
@@ -184,8 +195,8 @@ function auto_anchors() {
     if ($(this).prop('files').length > 0) {
       var file = $(this).prop('files')[0];
       var params = new FormData();
-      params.append('image', file);
-      $.post({url: '/wiki/ajax/image/', processData: false, data: params, contentType: false}).success(function(data) {
+      params.append('upload', file);
+      $.post({url: '/wiki/ajax/upload/', processData: false, data: params, contentType: false}).success(function(data) {
         var img = document.createElement('IMG');
         img.src = data.url;
         if (data.orig_url) {
@@ -196,6 +207,21 @@ function auto_anchors() {
     }
   }
 
+  function upload_document() {
+    if ($(this).prop('files').length > 0) {
+      var file = $(this).prop('files')[0];
+      var params = new FormData();
+      params.append('upload', file);
+      $.post({url: '/wiki/ajax/upload/', processData: false, data: params, contentType: false}).success(function(data) {
+        var doc_link = document.createElement('A');
+        doc_link.className = 'button';
+        doc_link.textContent = 'Télécharger ' + data.filename;
+        doc_link.href = data.url;
+        $(window.active_document).empty().append(doc_link);
+      });
+    }
+  }
+
   function get_contenteditable_subnode(node) {
     if (node === null) return null;
     if (node.contentEditable === 'true') return node;  // but we shouldn't arrive at root
@@ -232,7 +258,19 @@ function auto_anchors() {
       sel.removeAllRanges();
       sel.addRange(range);
       update_block_style_toolbar();
-
+      return;
+    }
+    if (this.action_block.special == 'doc') {
+      action = 'insertHTML';
+      param = '<div class="document"><span class="empty"></span></div><p id="new-p"></p>';
+      document.execCommand(action, false, param);
+      current_anchor = $('#new-p')[0];
+      $(current_anchor).attr('id', null);
+      var range = document.createRange();
+      range.setStart(current_anchor, 0);
+      sel.removeAllRanges();
+      sel.addRange(range);
+      update_block_style_toolbar();
       return;
     }
     if (this.action_block.special == 'list') {