[ADD] blahblahblahblah
[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 _t = instance.web._t;
4     var _lt = instance.web._lt;
5
6     /**
7      * Safari does not deal well at all with raw JSON data being
8      * returned. As a result, we're going to cheat by using a
9      * pseudo-jsonp: instead of getting JSON data in the iframe, we're
10      * getting a ``script`` tag which consists of a function call and
11      * the returned data (the json dump).
12      *
13      * The function is an auto-generated name bound to ``window``,
14      * which calls back into the callback provided here.
15      *
16      * @param {Object} form the form element (DOM or jQuery) to use in the call
17      * @param {Object} attributes jquery.form attributes object
18      * @param {Function} callback function to call with the returned data
19      */
20     function jsonp(form, attributes, callback) {
21         attributes = attributes || {};
22         var options = {jsonp: _.uniqueId('import_callback_')};
23         window[options.jsonp] = function () {
24             delete window[options.jsonp];
25             callback.apply(null, arguments);
26         };
27         if ('data' in attributes) {
28             _.extend(attributes.data, options);
29         } else {
30             _.extend(attributes, {data: options});
31         }
32         _.extend(attributes, {
33             dataType: 'script',
34         });
35         $(form).ajaxSubmit(attributes);
36     }
37
38     instance.web.DataImport = instance.web.Dialog.extend({
39         template: 'ImportView',
40         dialog_title: _lt("Import Data"),
41         events: {
42             'change input.oe_import_file': 'file_update',
43             'click input.oe_import_has_header': function (e) {
44                 this.$element.toggleClass(
45                     'oe_import_noheaders', !e.target.checked);
46                 this.settings_updated();
47             },
48             'click a.oe_import_csv': function (e) {
49                 e.preventDefault();
50             },
51             'click a.oe_import_export': function (e) {
52                 e.preventDefault();
53             },
54             'click dt a': function (e) {
55                 e.preventDefault();
56                 $(e.target).parent().next().toggle();
57             }
58         },
59         init: function (parent, dataset) {
60             var self = this;
61             this._super(parent, {
62                 buttons: [
63                     {text: _t("Import File"), click: function () {
64                         self.do_import();
65                     }, 'class': 'oe_import_dialog_button'}
66                 ]
67             });
68             this.res_model = parent.model;
69             // import object id
70             this.id = null;
71             this.Import = new instance.web.Model('base_import.import');
72         },
73         start: function () {
74             var self = this;
75             return this.Import.call('create', [{
76                 'res_model': this.res_model
77             }]).then(function (id) {
78                 self.id = id;
79                 self.$('input[name=import_id]').val(id);
80             });
81         },
82
83         import_options: function () {
84             return {
85                 // TODO: customizable gangnam style
86                 quote: '"',
87                 separator: ',',
88                 headers: this.$('input.oe_import_has_header').prop('checked'),
89             };
90         },
91
92         //- File & settings change section
93         file_update: function (e) {
94             if (!this.$('input.oe_import_file').val()) { return; }
95
96             this.$element.removeClass('oe_import_preview oe_import_error');
97             jsonp(this.$element, {
98                 url: '/base_import/set_file'
99             }, this.proxy('settings_updated'));
100         },
101         settings_updated: function () {
102             this.$element.addClass('oe_import_with_file');
103             // TODO: test that write // succeeded?
104             this.Import.call(
105                 'parse_preview', [this.id, this.import_options()])
106                 .then(this.proxy('preview'));
107         },
108         preview: function (result) {
109             if (result.error) {
110                 this.$element.addClass('oe_import_error');
111                 this.$('.oe_import_error_report').html(
112                     QWeb.render('ImportView.preview.error', result));
113             } else {
114                 this.$element.addClass('oe_import_preview');
115                 this.$('table').html(
116                     QWeb.render('ImportView.preview', result));
117             }
118         },
119
120         //- import itself
121         do_import: function () {
122             var fields = this.$('.oe_import_fields input').map(function (index, el) {
123                 return el.value || false;
124             }).get();
125             this.Import.call(
126                 'do', [this.id, fields, this.import_options()], {
127                     // maybe could do a dryrun after successful
128                     // preview or something (note: don't go to
129                     // this.result if dryrun=true)
130                     dryrun: false
131                 })
132                 .then(this.proxy('result'));
133         },
134         result: function (errors) {
135             if (!errors.length) {
136                 if (this.getParent().reload_content) {
137                     this.getParent().reload_content();
138                 }
139                 this.close();
140                 return;
141             }
142             // import failed (or maybe just warnings, if we ever get
143             // warnings?)
144             this.$element.addClass('oe_import_error');
145             this.$('.oe_import_error_report').html(
146                 QWeb.render('ImportView.error', {errors: errors}));
147         },
148     });
149 };