diff options
Diffstat (limited to 'addons/base_import/static/src')
| -rw-r--r-- | addons/base_import/static/src/js/import.js | 82 | ||||
| -rw-r--r-- | addons/base_import/static/src/xml/import.xml | 25 |
2 files changed, 107 insertions, 0 deletions
diff --git a/addons/base_import/static/src/js/import.js b/addons/base_import/static/src/js/import.js new file mode 100644 index 00000000000..5bb81d23cbb --- /dev/null +++ b/addons/base_import/static/src/js/import.js @@ -0,0 +1,82 @@ +openerp.base_import = function (instance) { + var QWeb = instance.web.qweb; + var _lt = instance.web._lt; + + /** + * Safari does not deal well at all with raw JSON data being + * returned. As a result, we're going to cheat by using a + * pseudo-jsonp: instead of getting JSON data in the iframe, we're + * getting a ``script`` tag which consists of a function call and + * the returned data (the json dump). + * + * The function is an auto-generated name bound to ``window``, + * which calls back into the callback provided here. + * + * @param {Object} form the form element (DOM or jQuery) to use in the call + * @param {Object} attributes jquery.form attributes object + * @param {Function} callback function to call with the returned data + */ + function jsonp(form, attributes, callback) { + attributes = attributes || {}; + var options = {jsonp: _.uniqueId('import_callback_')}; + window[options.jsonp] = function () { + delete window[options.jsonp]; + callback.apply(null, arguments); + }; + if ('data' in attributes) { + _.extend(attributes.data, options); + } else { + _.extend(attributes, {data: options}); + } + _.extend(attributes, { + dataType: 'script', + }); + $(form).ajaxSubmit(attributes); + } + + instance.web.DataImport = instance.web.Dialog.extend({ + template: 'ImportView', + dialog_title: _lt("Import Data"), + events: { + 'change input.oe_import_file': 'file_update' + }, + init: function (parent, dataset) { + this._super(parent, {}); + this.res_model = parent.model; + // import object id + this.id = null; + this.Import = new instance.web.Model('base_import.import'); + }, + start: function () { + var self = this; + return this.Import.call('create', [{ + 'res_model': this.res_model + }]).then(function (id) { + self.id = id; + self.$('input[name=import_id]').val(id); + }); + }, + + //- File change section + file_update: function (e) { + if (!this.$('input.oe_import_file').val()) { return; } + + // TODO: hide preview before calling set_file + jsonp(this.$element, { + url: '/base_import/set_file' + }, this.proxy('file_updated')); + }, + file_updated: function () { + // immediately trigger preview... + // TODO: test that write // succeeded? + this.Import.call('parse_preview', [this.id, { + quote: '"', + separator: ',', + headers: true, + }]).then(this.proxy('preview')); + }, + preview: function (result) { + this.$('table').html(QWeb.render('ImportView.preview', result)); + }, + }); +}; diff --git a/addons/base_import/static/src/xml/import.xml b/addons/base_import/static/src/xml/import.xml new file mode 100644 index 00000000000..d2a3ec4f66d --- /dev/null +++ b/addons/base_import/static/src/xml/import.xml @@ -0,0 +1,25 @@ +<templates> + <t t-name="ImportView"> + <form action="" method="post" enctype="multipart/form-data"> + <input type="hidden" name="session_id" + t-att-value="widget.session.session_id"/> + <input type="hidden" name="import_id"/> + <label for="csvfile">CSV File:</label> + <input type="file" id="csvfile" name="file" class="oe_import_file"/> + + <table class="oe_import_grid" width="100%"> + </table> + </form> + </t> + <!-- TODO: column matcher? --> + <t t-name="ImportView.preview"> + <tr t-if="headers" class="oe_import_grid-header"> + <td t-foreach="headers" t-as="header" class="oe_import_grid-cell" + ><t t-esc="header"/></td> + </tr> + <tr t-foreach="preview" t-as="row" class="oe_import_grid-row"> + <td t-foreach="row" t-as="cell" class="oe_import_grid-cell" + ><t t-esc="cell"/></td> + </tr> + </t> +</templates> |
