# HG changeset patch
# User Fabien Amarger <fabien.amarger@logilab.fr>
# Date 1612349357 -3600
#      Wed Feb 03 11:49:17 2021 +0100
# Node ID feb04ebd0174ca125b0293b80413c5356408ec5e
# Parent  1ab132c64d8aa55dfdfccbf9e055602e52872afb
feat(csv export): Add a button to export the query result as CSV content

diff --git a/src/index.html b/src/index.html
--- a/src/index.html
+++ b/src/index.html
@@ -92,6 +92,7 @@
         </div>
       </div>
       <div class="row justify-content-center">
+        <button class="btn btn-primary" id="csv-export-button" disabled>Télécharger en CSV</button>
         <div class="col-md-11">
           <div id="yasr"></div>
         </div>
@@ -316,12 +317,19 @@
                 .replace(/>/g, "&gt;")
             );
             yasqe.setValue(queryString);
+            if(queryString.length === 0) {
+                $("#csv-export-button").prop("disabled", true);
+            } else {
+                $("#csv-export-button").prop("disabled", false);
+            }
+
             yasqe.query();
           },
         });
       });
 
       var yasqe = YASQE.fromTextArea(document.getElementById("yasqe"), {
+        persistencyExpire: 0,
         sparql: {
           showQueryButton: true,
           endpoint: $("#endpoint").text(),
@@ -339,6 +347,15 @@
 
       // link yasqe and yasr
       yasqe.options.sparql.callbacks.complete = yasr.setResponse;
+
+
+      document.getElementById("csv-export-button").onclick = () => {
+        const query = yasqe.getValue().replace("LIMIT 5000", "");
+        const endpoint = $("#endpoint").text();
+        const url = endpoint + "?query=" + encodeURIComponent(query) + "&format=text/csv";
+        var win = window.open(url, '_blank');
+        win.focus();
+      };
     </script>
   </body>
 </html>