1 openerp.base_import = function (instance) {
2 var QWeb = instance.web.qweb;
3 var _lt = instance.web._lt;
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).
12 * The function is an auto-generated name bound to ``window``,
13 * which calls back into the callback provided here.
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
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);
26 if ('data' in attributes) {
27 _.extend(attributes.data, options);
29 _.extend(attributes, {data: options});
31 _.extend(attributes, {
34 $(form).ajaxSubmit(attributes);
37 instance.web.DataImport = instance.web.Dialog.extend({
38 template: 'ImportView',
39 dialog_title: _lt("Import Data"),
41 'change input.oe_import_file': 'file_update'
43 init: function (parent, dataset) {
44 this._super(parent, {});
45 this.res_model = parent.model;
48 this.Import = new instance.web.Model('base_import.import');
52 return this.Import.call('create', [{
53 'res_model': this.res_model
54 }]).then(function (id) {
56 self.$('input[name=import_id]').val(id);
60 //- File change section
61 file_update: function (e) {
62 if (!this.$('input.oe_import_file').val()) { return; }
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'));
69 file_updated: function () {
70 // immediately trigger preview...
71 // TODO: test that write // succeeded?
72 this.Import.call('parse_preview', [this.id, {
76 }]).then(this.proxy('preview'));
78 preview: function (result) {
80 this.$element.addClass('oe_import_error');
81 this.$('.oe_import_error_report').html(
82 QWeb.render('ImportView.error', result));
84 this.$element.addClass('oe_import_preview');
86 QWeb.render('ImportView.preview', result));