From f18e378be1a956d7e75f077fc5bebddc802cc4f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Fri, 22 Mar 2013 16:56:41 +0100 Subject: [PATCH] Add reload toolbar function --- src/model.js | 44 +++++++++++++++++++++++++++++++++++++- src/screen.js | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/tab.js | 14 +++++++++++++ src/view.js | 14 ++++++++++++- 4 files changed, 128 insertions(+), 2 deletions(-) diff --git a/src/model.js b/src/model.js index cef3191d21..6dbb9f12e3 100644 --- a/src/model.js +++ b/src/model.js @@ -41,7 +41,7 @@ }, delete_: function(records) { if (jQuery.isEmptyObject(records)) { - return; + return jQuery.when(); } var record = records[0]; var root_group = record.group.root_group; @@ -151,6 +151,44 @@ this.changed(); return record; }; + array.remove = function(record, remove, modified, force_remove) { + if (modified === undefined) { + modified = true; + } + var idx = this.indexOf(record); + if (record.id >= 0) { + if (remove) { + if (record in this.record_deleted) { + this.record_deleted.splice( + this.record_deleted.indexOf(record), 1); + } + this.record_removed.push(record); + } else { + if (record in this.record_removed) { + this.record_removed.splice( + this.record_removed.indexOf(record), 1); + } + this.record_deleted.push(record); + } + } + if (record.parent) { + record.parent._changed.id = true; + } + if (modified) { + record._changed.id = true; + } + if (!(record.parent) || (record.id < 0) || force_remove) { + this._remove(record); + } + record.group.changed(); + record.group.root_group().screens.forEach(function(screen) { + screen.display(); + }); + }; + array._remove = function(record) { + var idx = this.indexOf(record); + this.splice(idx, 1); + }; array.changed = function() { if (!this.parent) { return; @@ -483,6 +521,10 @@ var result = true; // TODO return result; + }, + cancel: function() { + this._loaded = {}; + this._changed = {}; } }); diff --git a/src/screen.js b/src/screen.js index 9374e7c27c..8284d47a86 100644 --- a/src/screen.js +++ b/src/screen.js @@ -179,6 +179,16 @@ // TODO editable return position; }, + cancel_current: function() { + var prms = []; + if (this.current_record) { + this.current_record.cancel(); + if (this.current_record.id < 0) { + prms.push(this.remove()); + } + } + return jQuery.when(prms); + }, save_current: function() { if (!this.current_record) { if ((this.current_view.view_type == 'tree') && @@ -225,6 +235,54 @@ } // TODO test view modified return false; + }, + remove: function(delete_, remove, force_remove) { + var result = jQuery.Deferred(); + var records = null; + if ((this.current_view.view_type == 'form') && + this.current_record) { + records = [this.current_record]; + } else if (this.current_view.view_type == 'tree') { + records = this.current_view.selected_records(); + } + if (jQuery.isEmptyObject(records)) { + return; + } + var prm = jQuery.when(); + if (delete_) { + // TODO delete children before parent + prm = this.model.delete_(records); + } + prm.done(function() { + records.forEach(function(record) { + record.group.remove(record, remove, true, force_remove); + }); + var prms = []; + if (delete_) { + records.forEach(function(record) { + if (record.parent) { + prms.push(record.parent.save()); + } + if (record in record.group.record_deleted) { + record.group.record_deleted.splice( + record.group.record_deleted.indexOf(record), 1); + } + if (record in record.group.record_removed) { + record.group.record_removed.splice( + record.group.record_removed.indexOf(record), 1); + } + // TODO destroy + }); + } + // TODO set current_record + this.current_record = null; + // TODO set_cursor + jQuery.when(prms).done(function() { + this.display(); + result.resolve(); + }.bind(this)); + }.bind(this)); + return result; } }); }()); diff --git a/src/tab.js b/src/tab.js index 0a6c96f9ad..56b6ddc279 100644 --- a/src/tab.js +++ b/src/tab.js @@ -194,6 +194,20 @@ switch_: function() { // TODO modified this.screen.switch_view(); + }, + reload: function(test_modified) { + if (test_modified && this.screen.modified()) { + // TODO popup + } + this.screen.cancel_current().done(function() { + if (this.screen.current_view.view_type != 'form') { + this.screen.search_filter(); // TODO set search text + // TODO set current_record + } + this.screen.display(); + // TODO message + // TODO activate_save + }.bind(this)); } }); }()); diff --git a/src/view.js b/src/view.js index fc3dd34c3a..f31d84fcec 100644 --- a/src/view.js +++ b/src/view.js @@ -181,6 +181,15 @@ this.screen.current_record = record; // TODO validate if editable // TODO update_children + }, + selected_records: function() { + var records = []; + this.columns.forEach(function(column) { + if (column.is_selected()) { + records.push(column.record); + } + }); + return records; } }); @@ -277,11 +286,14 @@ }, select_row: function() { this.el.toggleClass('ui-state-highlight'); - if (this.el.hasClass('ui-state-highlight')) { + if (this.is_selected()) { this.tree.select_changed(this.record); } else { this.tree.select_changed(null); } + }, + is_selected: function() { + return this.el.hasClass('ui-state-highlight'); } });