summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/data_import.js
diff options
context:
space:
mode:
Diffstat (limited to 'addons/web/static/src/js/data_import.js')
-rw-r--r--addons/web/static/src/js/data_import.js68
1 files changed, 59 insertions, 9 deletions
diff --git a/addons/web/static/src/js/data_import.js b/addons/web/static/src/js/data_import.js
index 0864c5e594e..355c6c1b493 100644
--- a/addons/web/static/src/js/data_import.js
+++ b/addons/web/static/src/js/data_import.js
@@ -1,5 +1,6 @@
openerp.web.data_import = function(openerp) {
-var QWeb = openerp.web.qweb;
+var QWeb = openerp.web.qweb,
+ _t = openerp.web._t;
/**
* 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
@@ -79,8 +80,8 @@ openerp.web.DataImport = openerp.web.Dialog.extend({
this.toggle_import_button(false);
this.$element.find('#csvfile').change(this.on_autodetect_data);
this.$element.find('fieldset').change(this.on_autodetect_data);
- this.$element.find('fieldset legend').click(function() {
- $(this).next().toggle();
+ this.$element.delegate('fieldset legend', 'click', function() {
+ $(this).parent().toggleClass('oe-closed');
});
this.ready.push(new openerp.web.DataSet(this, this.model).call(
'fields_get', [], function (fields) {
@@ -101,7 +102,16 @@ openerp.web.DataImport = openerp.web.Dialog.extend({
level = level || 0;
var self = this;
+ if (level === 0) {
+ parent.fields.push({
+ id: 'id',
+ name: 'id',
+ string: _t('External ID'),
+ required: false
+ });
+ }
_(fields).each(function (field, field_name) {
+ if (field_name === 'id') { return; }
var f = {
id: field_name,
name: field_name,
@@ -184,9 +194,11 @@ openerp.web.DataImport = openerp.web.Dialog.extend({
: with_headers ? results.records.slice(1)
: results.records
}));
+ this.$element.find('fieldset').addClass('oe-closed');
} else if (results['error']) {
result_node.append(QWeb.render('ImportView.error', {
'error': results['error']}));
+ this.$element.find('fieldset').removeClass('oe-closed');
} else if (results['success']) {
if (this.widget_parent.widget_parent.active_view == "list") {
this.widget_parent.reload_content();
@@ -194,6 +206,11 @@ openerp.web.DataImport = openerp.web.Dialog.extend({
this.stop();
return;
}
+ this.$element.find('form').removeClass('oe-import-no-result');
+
+ this.$element.delegate('.oe-m2o-drop-down-button', 'click', function () {
+ $(this).prev('input').focus();
+ });
var self = this;
this.ready.then(function () {
@@ -210,18 +227,51 @@ openerp.web.DataImport = openerp.web.Dialog.extend({
});
// Column auto-detection
_(headers).each(function (header, index) {
- var f =_(self.fields).detect(function (field) {
- // TODO: levenshtein between header and field.string
- return field.name === header || field.string.toLowerCase() === header;
- });
- if (f) {
- $fields.eq(index).val(f.name);
+ var field_name = self.match_column_to_field(header);
+ if (field_name) {
+ $fields.eq(index).val(field_name);
}
});
self.on_check_field_values();
});
},
/**
+ * Returns the name of the field (nested) matching the provided column name
+ *
+ * @param {String} name column name to look for
+ * @param {Array} [fields] fields to look into for the provided name
+ * @returns {String|undefined}
+ */
+ match_column_to_field: function (name, fields) {
+ fields = fields || this.fields;
+ var f;
+ f = _(fields).detect(function (field) {
+ // TODO: levenshtein between header and field.string
+ return field.name === name
+ || field.string.toLowerCase() === name.toLowerCase();
+ });
+ if (f) { return f.name; }
+
+ // if ``name`` is a path (o2m), we need to recurse through its .fields
+ var index = name.indexOf('/');
+ if (index === -1) { return undefined; }
+ // Get the first path section, try to find the matching field
+ var column_name = name.substring(0, index);
+ f = _(fields).detect(function (field) {
+ // field.name for o2m is $foo/id, so we want to match on id
+ return field.id === column_name
+ || field.string.toLowerCase() === column_name.toLowerCase()
+ });
+ if (!f) { return undefined; }
+
+ // if we found a matching field for the first path section, recurse in
+ // its own .fields to try and get the rest of the path matched
+ var rest = this.match_column_to_field(
+ name.substring(index+1), f.fields);
+ if (!rest) { return undefined; }
+ return f.id + '/' + rest;
+ },
+ /**
* Looks through all the field selections, and tries to find if two
* (or more) columns were matched to the same model field.
*