From e03c62a063e2dc5f2a79b652fe2b331f4a18073f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 4 Mar 2013 19:08:16 +0100 Subject: [PATCH] Add new/save toolbar functions --- src/model.js | 57 +++++++++++++++++++++++++++++--------- src/screen.js | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/tab.js | 27 ++++++++++++++++++ src/view.js | 66 +++++++++++++++++++++++++++++++++++-------- 4 files changed, 202 insertions(+), 25 deletions(-) diff --git a/src/model.js b/src/model.js index 71e311b1f8..cef3191d21 100644 --- a/src/model.js +++ b/src/model.js @@ -39,10 +39,24 @@ }; return prm.pipe(instanciate); }, - delete_: function(records, context) { - return this.execute('delete', [records.map(function(record) { + delete_: function(records) { + if (jQuery.isEmptyObject(records)) { + return; + } + var record = records[0]; + var root_group = record.group.root_group; + // TODO test same model + // TODO test same root group + records = records.filter(function(record) { + return record.id >= 0; + }); + var context = {}; + // TODO timestamp + var record_ids = records.map(function(record) { return record.id; - })], context); + }); + // TODO reload ids + return this.execute('delete', [record_ids], context); } }); @@ -105,7 +119,7 @@ } }; array.new_ = function(default_, id) { - var record = Sao.Record(this.name, id); + var record = new Sao.Record(this.name, id); record.model = this.model; record.group = this; if (default_) { @@ -133,7 +147,7 @@ this.record_deleted.indexOf(record_del), 1); } } - record.changed.id = true; + record._changed.id = true; this.changed(); return record; }; @@ -155,6 +169,16 @@ } return root; }; + array.save = function() { + var deferreds = []; + this.forEach(function(record) { + deferreds.push(record.save()); + }); + if (!jQuery.isEmptyObject(this.record_deleted)) { + deferreds.push(this.model.delete_(this.record_deleted)); + } + return jQuery.when(deferreds); + }; return array; }; @@ -175,22 +199,24 @@ }, save: function() { var context = this.get_context(); - var prm, values; + var prm = jQuery.when(); + var values = this.get(); if (this.id < 0) { - values = this.get(); - prm = this.model.execute('create', [values], context); - var created = function(id) { - this.id = id; + prm = this.model.execute('create', [[values]], context); + var created = function(ids) { + this.id = ids[0]; }; prm.done(created.bind(this)); } else { - values = this.get(true); if (!jQuery.isEmptyObject(values)) { - prm = this.model.execute('write', [this.id, values], + // TODO timestamp + prm = this.model.execute('write', [[this.id], values], context); } } prm.done(this.reload.bind(this)); + // TODO group written + // TODO parent return prm; }, reload: function() { @@ -452,6 +478,11 @@ ctx['_parent_' + this.group.parent_name] = parent_env; } return new Sao.PYSON.Decoder(ctx).decode(expr); + }, + validate: function(fields, softvalidation) { + var result = true; + // TODO + return result; } }); @@ -835,7 +866,7 @@ return fields; }); if (!jQuery.isEmptyObject(value)) { - var context = this.get_context(); + var context = this.get_context(record); var field_names = {}; for (var val in value) { if (!value.hasOwnProperty(val)) { diff --git a/src/screen.js b/src/screen.js index d85ada89fd..9374e7c27c 100644 --- a/src/screen.js +++ b/src/screen.js @@ -148,6 +148,83 @@ if (this.current_record) { return this.current_record.id; } + }, + new_: function(default_) { + if (default_ === undefined) { + default_ = true; + } + var prm = jQuery.when(); + if (this.current_view && + ((this.current_view.view_type == 'tree' && + !this.current_view.editable) || + this.current_view.view_type == 'graph')) { + prm = this.switch_view('form'); + } + prm.done(function() { + var group; + if (this.current_record) { + group = this.current_record.group; + } else { + group = this.group; + } + var record = group.new_(default_); + group.add(record, this.new_model_position()); + this.current_record = record; + this.display(); + // TODO set_cursor + }.bind(this)); + }, + new_model_position: function() { + var position = -1; + // TODO editable + return position; + }, + save_current: function() { + if (!this.current_record) { + if ((this.current_view.view_type == 'tree') && + (!jQuery.isEmptyObject(this.group))) { + this.current_record = this.group[0]; + } else { + return true; + } + } + this.current_view.set_value(); + var fields = this.current_view.get_fields(); + // TODO path + var prm; + if (this.current_view.view_type == 'tree') { + prm = this.group.save(); + } else if (this.current_record.validate(fields)) { + prm = this.current_record.save(); + } else { + // TODO set_cursor + this.current_view.display(); + prm = jQuery.when(); + prm.reject(); + return prm; + } + prm.always(function() { + this.display(); + }.bind(this)); + return prm; + }, + modified: function() { + var test = function(record) { + return (record.has_changed() || record.id < 0); + }; + if (this.current_view.view_type != 'tree') { + if (this.current_record) { + if (test(this.current_record)) { + return true; + } + } + } else { + if (this.group.some(test)) { + return true; + } + } + // TODO test view modified + return false; } }); }()); diff --git a/src/tab.js b/src/tab.js index 563102f5c8..0a6c96f9ad 100644 --- a/src/tab.js +++ b/src/tab.js @@ -164,6 +164,33 @@ }); return toolbar; }, + modified_save: function() { + this.screen.current_view.set_value(); + if (this.screen.modified()) { + // TODO popup + return false; + } + return true; + }, + new_: function() { + // TODO test modelaccess + if (!this.modified_save()) { + return; + } + this.screen.new_(); + // TODO message + // TODO activate_save + }, + save: function() { + // TODO test modelaccess + if (this.screen.save_current()) { + // TODO message + return true; + } else { + // TODO message + return false; + } + }, switch_: function() { // TODO modified this.screen.switch_view(); diff --git a/src/view.js b/src/view.js index 6571f1c427..fc3dd34c3a 100644 --- a/src/view.js +++ b/src/view.js @@ -8,6 +8,12 @@ this.screen = screen; this.view_type = null; this.el = null; + this.fields = {}; + }, + set_value: function() { + }, + get_fields: function() { + return Object.keys(this.fields); } }); @@ -111,7 +117,6 @@ this.el.append(footer); }, create_columns: function(model, xml) { - var self = this; xml.find('tree').children().each(function(pos, child) { var attributes, column; if (child.tagName == 'field') { @@ -148,6 +153,7 @@ var ColumnFactory = Sao.View.tree_column_get( attributes.widget); column = new ColumnFactory(model, attributes); + this.fields[name] = true; } else if (child.tagName == 'button') { attributes = { 'help': child.getAttribute('help'), @@ -157,8 +163,8 @@ }; column = new Sao.View.Tree.ButtonColumn(attributes); } - self.columns.push(column); - }); + this.columns.push(column); + }.bind(this)); }, display: function() { this.tbody.empty(); @@ -635,6 +641,7 @@ this.widgets[name] = []; } this.widgets[name].push(widget); + this.fields[name] = true; }, _parse_group: function(model, node, container, attributes) { var group = new Sao.View.Form.Group(attributes); @@ -689,6 +696,21 @@ var state_widget = this.state_widgets[j]; state_widget.state_set(record); } + }, + set_value: function() { + var record = this.screen.current_record; + if (record) { + var set_value = function(widget) { + widget.set_value(record, this); + }; + for (var name in this.widgets) { + if (name in record.model.fields) { + var widgets = this.widgets[name]; + var field = record.model.fields[name]; + widgets.forEach(set_value, field); + } + } + } } }); @@ -867,8 +889,12 @@ }, display: function(record, field) { Sao.View.Form.Char._super.display.call(this, record, field); - var value = record.field_get_client(this.field_name); - this.el.val(value || ''); + if (record) { + var value = record.field_get_client(this.field_name); + this.el.val(value || ''); + } else { + this.el.val(''); + } }, set_value: function(record, field) { var value = this.el.val() || ''; @@ -898,8 +924,12 @@ }, display: function(record, field) { Sao.View.Form.Date._super.display.call(this, record, field); - var value = record.field_get_client(this.field_name); - this.el.datepicker('setDate', value); + if (record) { + var value = record.field_get_client(this.field_name); + this.el.datepicker('setDate', value); + } else { + this.el.datepicker('setDate', null); + } }, set_value: function(record, field) { var value = this.el.datepicker('getDate'); @@ -998,8 +1028,12 @@ }, display: function(record, field) { Sao.View.Form.FloatTime._super.display.call(this, record, field); - var value = record.field_get_client(this.field_name); - this.el.val(Sao.common.text_to_float_time(value, this.conv)); + if (record) { + var value = record.field_get_client(this.field_name); + this.el.val(Sao.common.text_to_float_time(value, this.conv)); + } else { + this.el.val(''); + } } }); @@ -1015,7 +1049,11 @@ }, display: function(record, field) { Sao.View.Form.Boolean._super.display.call(this, record, field); - this.el.prop('checked', record.field_get(this.field_name)); + if (record) { + this.el.prop('checked', record.field_get(this.field_name)); + } else { + this.el.prop('checked', false); + } }, set_value: function(record, field) { var value = this.el.prop('checked'); @@ -1034,8 +1072,12 @@ }, display: function(record, field) { Sao.View.Form.Text._super.display.call(this, record, field); - var value = record.field_get_client(this.field_name); - this.el.val(value); + if (record) { + var value = record.field_get_client(this.field_name); + this.el.val(value); + } else { + this.el.val(''); + } }, set_value: function(record, field) { var value = this.el.val() || '';