diff --git a/static/plugin_dbui/src/App.js b/static/plugin_dbui/src/App.js index 92f3fbe5be7505383f30072955a2a38d21ae3aa0..52d2833de4e5cc02369a9a759b53fb764f36b200 100644 --- a/static/plugin_dbui/src/App.js +++ b/static/plugin_dbui/src/App.js @@ -86,6 +86,46 @@ Ext.define('App', { */ REMOTE_API: null, + /** + * Helper method to build URI string. + * + * The URI is built from the arguments and the data are HTML + * encoded to preserve special character like quote, /, ?, ... + * + * @param {String} + * The mine type of the return file, e.g "text/csv". + * + * @param {String} + * The data encoding, e.g. "utf-8". + * + * @param {String/String[]} + * The content of the file. Either a string of a list of string, + * one string per line. + * + * @return {String} + * + */ + buildURI: function (minetype, encoding, data) { + + "use strict"; + + var uri; + + // build the data string in which special + // character like quote, / , ? are HTML encoded + if (Ext.isArray(data)) { + data = data.join("\n"); + } + data = encodeURIComponent(data); + + // build the URI + uri = "data:" + minetype + ";"; + uri += "charset=" + encoding +","; + uri += data; + + return uri; + }, + /** * Close the user session on the server. * Can be triggered by the window event *beforeunload*. @@ -142,9 +182,9 @@ Ext.define('App', { var modelName; - for (modelName in App.config.models) { - if (App.config.models.hasOwnProperty(modelName)) { - Ext.define(modelName, App.config.models[modelName]); + for (modelName in this.config.models) { + if (this.config.models.hasOwnProperty(modelName)) { + Ext.define(modelName, this.config.models[modelName]); } } }, @@ -204,7 +244,7 @@ Ext.define('App', { } // get the configuration - cfg = App.config.stores[storeId]; + cfg = this.config.stores[storeId]; // instantiate the store switch (cfg.xtype) { @@ -287,5 +327,67 @@ Ext.define('App', { } } return false; + }, + + /** + * Save the local data as a file using URI technique. + * The file name is defined by the browser. + * + * The URI is built from the arguments and the data are HTML + * encoded to preserve special character like quote, /, ?, ... + * + * + * @param {String} + * The mine type of the return file, e.g "text/csv". + * + * @param {String} + * The data encoding, e.g. "utf-8". + * + * @param {String/String[]} + * The content of the file. Either a string of a list of string, + * one string per line. + * + */ + save: function (minetype, encoding, data) { + + "use strict"; + window.location.href = this.buildURI(minetype, encoding, data); + }, + + /** + * Save the local data as a file using URI technique. + * Similar to the #save method but the file name is defined by the user. + * + * The URI is built from the arguments and the data are HTML + * encoded to preserve special character like quote, /, ?, ... + * + * @param {String} + * The mine type of the return file, e.g "text/csv". + * + * @param {String} + * The data encoding, e.g. "utf-8". + * + * @param {String/String[]} + * The content of the file. Either a string of a list of string, + * one string per line. + * + * @param {String} + * The name of the file. + */ + saveAs: function (minetype, encoding, data, filename) { + + "use strict"; + + var link; + + // download the file by simulating a click on a link + link = Ext.getBody().createChild({ + tag: "a", + href: this.buildURI(minetype, encoding, data), + download: filename + }); + + link.dom.click(); + link.destroy(); } }); \ No newline at end of file diff --git a/static/plugin_dbui/src/grid/plugin/Export.js b/static/plugin_dbui/src/grid/plugin/Export.js index 21ffedf209521d7b43102c00b52dc305379ac2a9..fda92cc463141f06e481952ab88b318f841d07e8 100644 --- a/static/plugin_dbui/src/grid/plugin/Export.js +++ b/static/plugin_dbui/src/grid/plugin/Export.js @@ -235,7 +235,7 @@ Ext.define('App.grid.plugin.Export', { } // trigger the transfer using URI technique - this.save("text/csv", "utf-8", lines); + App.save("text/csv", "utf-8", lines); }, /** @@ -247,7 +247,7 @@ Ext.define('App.grid.plugin.Export', { "use strict"; var lines = this.toLaTeX(); - this.save("application/x-latex", "utf-8", lines); + App.save("application/x-latex", "utf-8", lines); }, /** @@ -280,44 +280,6 @@ Ext.define('App.grid.plugin.Export', { form.getForm().submit(); }, - /** - * Save the data as a file using an URI. - * The URI is built from the arguments and the data are HTML - * encoded to preserve special character like quote, /, ?, ... - * - * @param {String} - * The mine type of the return file, e.g "text/csv". - * - * @param {String} - * The data encoding, e.g. "utf-8". - * - * @param {String/String[]} - * The content of the file. Either a string of a list of string, - * one string per line. - * - */ - save: function (minetype, encoding, data) { - - "use strict"; - - var uri; - - // build the data string in which special - // character like quote, / , ? are HTML encoded - if (Ext.isArray(data)) { - data = data.join(this.newLine); - } - data = encodeURIComponent(data); - - // build the URI - uri = "data:" + minetype + ";"; - uri += "charset=" + encoding +","; - uri += data; - - // stimulate the browser to mimic the field download - window.location.href = uri; - }, - /** * Convert the Ext.grid.View content into a string. * the information are encoded using LaTeX standard.