From 2f2bc1ae96f025883e077e84adf340b950225d88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Fri, 16 Oct 2015 01:43:02 +0200 Subject: [PATCH] Manage path for setting current_record review18681002 --- src/model.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/screen.js | 26 +++++++++++++++++++++++--- src/view.js | 21 +++++++++++++++------ 3 files changed, 82 insertions(+), 9 deletions(-) diff --git a/src/model.js b/src/model.js index 645eb48e61..26da228fac 100644 --- a/src/model.js +++ b/src/model.js @@ -402,6 +402,35 @@ } return this.__domain4inversion[1]; }; + array.get_by_path = function(path) { + path = jQuery.extend([], path); + var record = null; + var group = this; + + var browse_child = function() { + if (jQuery.isEmptyObject(path)) { + return record; + } + var child_name = path[0][0]; + var id = path[0][1]; + path.splice(0, 1); + record = group.get(id); + if (!record) { + return null; + } + if (!child_name) { + return browse_child(); + } + return record.load(child_name).then(function() { + group = record._values[child_name]; + if (!group) { + return null; + } + return browse_child(); + }); + }; + return jQuery.when().then(browse_child); + }; return array; }; @@ -1080,6 +1109,21 @@ } return parent; }, + get_path: function(group) { + var path = []; + var i = this; + var child_name = ''; + while (i) { + path.push([child_name, i.id]); + if (i.group === group) { + break; + } + child_name = i.group.child_name; + i = i.group.parent; + } + path.reverse(); + return path; + }, deleted: function() { return Boolean(~this.group.record_deleted.indexOf(this)); }, diff --git a/src/screen.js b/src/screen.js index 760dc27907..6211a48243 100644 --- a/src/screen.js +++ b/src/screen.js @@ -951,7 +951,7 @@ } this.current_view.set_value(); var fields = this.current_view.get_fields(); - // TODO path + var path = this.current_record.get_path(this.group); var prm = jQuery.Deferred(); if (this.current_view.view_type == 'tree') { prm = this.group.save(); @@ -968,6 +968,11 @@ }.bind(this)); } var dfd = jQuery.Deferred(); + prm = prm.then(function() { + return this.group.get_by_path(path).then(function(record) { + this.set_current_record(record); + }.bind(this)); + }.bind(this)); prm.then(function() { this.display().always(dfd.resolve); }.bind(this), function() { @@ -1015,6 +1020,10 @@ // TODO delete children before parent prm = this.model.delete_(records); } + var top_record = records[0]; + var top_group = top_record.group; + var idx = top_group.indexOf(top_record); + var path = top_record.get_path(this.group); return prm.then(function() { records.forEach(function(record) { record.group.remove(record, remove, true, force_remove); @@ -1036,8 +1045,19 @@ // TODO destroy }); } - // TODO set current_record - this.set_current_record(null); + if (idx > 0) { + var record = top_group[idx - 1]; + path.splice(-1, 1, [path[path.length - 1][0], record.id]); + } else { + path.splice(-1, 1); + } + if (!jQuery.isEmptyObject(path)) { + prms.push(this.group.get_by_path(path).then(function(record) { + this.set_current_record(record); + }.bind(this))); + } else if (self.group.length) { + this.set_current_record(this.group[0]); + } // TODO set_cursor return jQuery.when.apply(jQuery, prms).then(function() { this.display(); diff --git a/src/view.js b/src/view.js index ec124f6d5c..5bba8f3d14 100644 --- a/src/view.js +++ b/src/view.js @@ -274,17 +274,26 @@ var current_record = this.screen.current_record; if (!selected) { selected = this.get_selected_paths(); - if (current_record && !Sao.common.contains(selected, - [[current_record.id]])) { - selected = [[current_record.id]]; + if (current_record) { + var current_path = current_record.get_path(this.screen.group); + current_path = current_path.map(function(e) { + return e[1]; + }); + if (!Sao.common.contains(selected, current_path)) { + selected = [current_path]; + } + } else if (!current_record) { + selected = []; } } expanded = expanded || []; if ((this.screen.group.length != this.rows.length) || - !Sao.common.compare(this.screen.group, this.rows.map(function(row) { - return row.record; - }))) { + !Sao.common.compare( + this.screen.group, this.rows.map(function(row) { + return row.record; + })) || this.children_field) { // XXX find better check + // to keep focus this.construct(selected, expanded); }