[ADD] error reporting to user during preview
[odoo/odoo.git] / addons / base_import / static / src / js / import.js
1 openerp.base_import = function (instance) {
2     var QWeb = instance.web.qweb;
3     var _lt = instance.web._lt;
4
5     /**
6      * Safari does not deal well at all with raw JSON data being
7      * returned. As a result, we're going to cheat by using a
8      * pseudo-jsonp: instead of getting JSON data in the iframe, we're
9      * getting a ``script`` tag which consists of a function call and
10      * the returned data (the json dump).
11      *
12      * The function is an auto-generated name bound to ``window``,
13      * which calls back into the callback provided here.
14      *
15      * @param {Object} form the form element (DOM or jQuery) to use in the call
16      * @param {Object} attributes jquery.form attributes object
17      * @param {Function} callback function to call with the returned data
18      */
19     function jsonp(form, attributes, callback) {
20         attributes = attributes || {};
21         var options = {jsonp: _.uniqueId('import_callback_')};
22         window[options.jsonp] = function () {
23             delete window[options.jsonp];
24             callback.apply(null, arguments);
25         };
26         if ('data' in attributes) {
27             _.extend(attributes.data, options);
28         } else {
29             _.extend(attributes, {data: options});
30         }
31         _.extend(attributes, {
32             dataType: 'script',
33         });
34         $(form).ajaxSubmit(attributes);
35     }
36
37     instance.web.DataImport = instance.web.Dialog.extend({
38         template: 'ImportView',
39         dialog_title: _lt("Import Data"),
40         events: {
41             'change input.oe_import_file': 'file_update'
42         },
43         init: function (parent, dataset) {
44             this._super(parent, {});
45             this.res_model = parent.model;
46             // import object id
47             this.id = null;
48             this.Import = new instance.web.Model('base_import.import');
49         },
50         start: function () {
51             var self = this;
52             return this.Import.call('create', [{
53                 'res_model': this.res_model
54             }]).then(function (id) {
55                 self.id = id;
56                 self.$('input[name=import_id]').val(id);
57             });
58         },
59
60         //- File change section
61         file_update: function (e) {
62             if (!this.$('input.oe_import_file').val()) { return; }
63
64             this.$element.removeClass('oe_import_preview oe_import_error');
65             jsonp(this.$element, {
66                 url: '/base_import/set_file'
67             }, this.proxy('file_updated'));
68         },
69         file_updated: function () {
70             // immediately trigger preview...
71             // TODO: test that write // succeeded?
72             this.Import.call('parse_preview', [this.id, {
73                 quote: '"',
74                 separator: ',',
75                 headers: true,
76             }]).then(this.proxy('preview'));
77         },
78         preview: function (result) {
79             if (result.error) {
80                 this.$element.addClass('oe_import_error');
81                 this.$('.oe_import_error_report').html(
82                     QWeb.render('ImportView.error', result));
83             } else {
84                 this.$element.addClass('oe_import_preview');
85                 this.$('table').html(
86                     QWeb.render('ImportView.preview', result));
87             }
88         },
89     });
90 };