diff options
| author | Vidhin Mehta (OpenERP) <vme@tinyerp.com> | 2011-10-25 16:55:48 +0530 |
|---|---|---|
| committer | Vidhin Mehta (OpenERP) <vme@tinyerp.com> | 2011-10-25 16:55:48 +0530 |
| commit | c17c8a0bd10d5c9cedd6c34bc0110c02ecdcd6b2 (patch) | |
| tree | b4543c28941307f8ebdd60191f2eb2494139aad2 /addons/web/static/src/js/data_import.js | |
| parent | 1ee677eadbfa3e4c7d9836c48bea735f41f6a5a7 (diff) | |
| parent | 55ad46220db83d2f5d6dd86d243d0d9843b74511 (diff) | |
[IMP]merge with main branch of view editor.
bzr revid: vme@tinyerp.com-20111025112548-eakr7fio6diovn2m
Diffstat (limited to 'addons/web/static/src/js/data_import.js')
| -rw-r--r-- | addons/web/static/src/js/data_import.js | 84 |
1 files changed, 73 insertions, 11 deletions
diff --git a/addons/web/static/src/js/data_import.js b/addons/web/static/src/js/data_import.js index 9c686846751..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 @@ -37,6 +38,7 @@ openerp.web.DataImport = openerp.web.Dialog.extend({ this.model = parent.model; this.fields = []; this.all_fields = []; + this.fields_with_defaults = []; this.required_fields = null; var convert_fields = function (root, prefix) { @@ -50,7 +52,9 @@ openerp.web.DataImport = openerp.web.Dialog.extend({ }; this.ready = $.Deferred.queue().then(function () { self.required_fields = _(self.fields).chain() - .filter(function (field) { return field.required; }) + .filter(function (field) { + return field.required && + !_.include(self.fields_with_defaults, field.id); }) .pluck('name') .value(); convert_fields(self); @@ -76,20 +80,38 @@ 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) { self.graft_fields(fields); - })); + self.ready.push(new openerp.web.DataSet(self, self.model) + .default_get(_.pluck(self.fields, 'id'), function (fields) { + _.each(fields, function(val, key) { + if (val) { + self.fields_with_defaults.push(key); + } + }); + }) + ) + })); }, graft_fields: function (fields, parent, level) { parent = parent || this; 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, @@ -172,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(); @@ -182,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 () { @@ -198,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. * |
