diff --git a/js/model.js b/js/model.js index 7c43a8a952..5dae5dd1da 100644 --- a/js/model.js +++ b/js/model.js @@ -4,47 +4,47 @@ Sao.Model = Class(Object, { init: function(name, attributes) { - attributes = attributes || {}; - this.name = name; - this.session = Sao.Session.current_session; - this.fields = {}; - this.context = attributes.context || {}; - }, + attributes = attributes || {}; + this.name = name; + this.session = Sao.Session.current_session; + this.fields = {}; + this.context = attributes.context || {}; + }, add_fields: function(descriptions) { - for (var name in descriptions) { - if (descriptions.hasOwnProperty(name) && - (!(name in this.fields))) { - var desc = descriptions[name]; - var Field = Sao.field.get(desc.type); - this.fields[name] = new Field(desc); - } - } - }, + for (var name in descriptions) { + if (descriptions.hasOwnProperty(name) && + (!(name in this.fields))) { + var desc = descriptions[name]; + var Field = Sao.field.get(desc.type); + this.fields[name] = new Field(desc); + } + } + }, execute: function(method, params, context) { - var args = { - 'method': 'model.' + this.name + '.' + method, - 'params': params.concat(context) - }; - return Sao.rpc(args, this.session); - }, + var args = { + 'method': 'model.' + this.name + '.' + method, + 'params': params.concat(context) + }; + return Sao.rpc(args, this.session); + }, find: function(condition, offset, limit, order, context) { - if (!offset) offset = 0; - var self = this; - var prm = this.execute('search', - [condition, offset, limit, order], context); - var instanciate = function(ids) { - return Sao.Group(self, ids.map(function(id) { - return new Sao.Record(self, id); - })); - }; - return prm.pipe(instanciate); - }, + if (!offset) offset = 0; + var self = this; + var prm = this.execute('search', + [condition, offset, limit, order], context); + var instanciate = function(ids) { + return Sao.Group(self, ids.map(function(id) { + return new Sao.Record(self, id); + })); + }; + return prm.pipe(instanciate); + }, delete: function(records) { - var context = {}; // TODO - return this.execute('delete', [records.map(function(record) { - return record.id; - })], context); - } + var context = {}; // TODO + return this.execute('delete', [records.map(function(record) { + return record.id; + })], context); + } }); Sao.Group = function(model, array) { @@ -58,186 +58,186 @@ Sao.Group = function(model, array) { Sao.Record = Class(Object, { id_counter: -1, init: function(model, id) { - this.model = model; - this.group = Sao.Group(model, []); - this.id = id || Sao.Record.prototype.id_counter--; - this._values = {}; - this._changed = {}; - this._loaded = {}; - this.fields = {}; - this._timestamp; - }, + this.model = model; + this.group = Sao.Group(model, []); + this.id = id || Sao.Record.prototype.id_counter--; + this._values = {}; + this._changed = {}; + this._loaded = {}; + this.fields = {}; + this._timestamp; + }, has_changed: function() { - return !jQuery.isEmptyObject(this._changed); - }, + return !jQuery.isEmptyObject(this._changed); + }, _get_values: function(fields) { - if (!fields) { - fields = Object.keys(this._values); - } - var values = {}; - for (var i = 0, len = fields.length; i < len; i++) { - var name = fields[i]; - var field = this.model.fields[name]; - values[name] = field.get(this); - } - return values; - }, + if (!fields) { + fields = Object.keys(this._values); + } + var values = {}; + for (var i = 0, len = fields.length; i < len; i++) { + var name = fields[i]; + var field = this.model.fields[name]; + values[name] = field.get(this); + } + return values; + }, save: function() { - // TODO context - var context = {}; - var prm, values; - if (this.id < 0) { - values = this._get_values(); - prm = this.model.execute('create', [values], context); - var created = function(id) { - this.id = id; - }; - prm.done(created.bind(this)); - } else { - if (!this.has_changed()) { - return jQuery.when(); - } - values = this._get_values(Object.keys(this._changed)); - prm = this.model.execute('write', [this.id, values], context); - } - prm.done(this.reload.bind(this)); - return prm; - }, + // TODO context + var context = {}; + var prm, values; + if (this.id < 0) { + values = this._get_values(); + prm = this.model.execute('create', [values], context); + var created = function(id) { + this.id = id; + }; + prm.done(created.bind(this)); + } else { + if (!this.has_changed()) { + return jQuery.when(); + } + values = this._get_values(Object.keys(this._changed)); + prm = this.model.execute('write', [this.id, values], context); + } + prm.done(this.reload.bind(this)); + return prm; + }, reload: function() { - this._values = {}; - this._loaded = {}; - this._changed = {}; - }, + this._values = {}; + this._loaded = {}; + this._changed = {}; + }, load: function(name) { - var self = this; - if ((this.id < 0) || (name in this._loaded)) { - return jQuery.when(); - } - var id2record = {}; - id2record[this.id] = this; - var loading; - if (name == '*') { - loading = 'eager'; - for (var fname in this.model.fields) { - if (!this.model.fields.hasOwnProperty(fname)) { - continue; - } - var field_loading = ( - this.model.fields[fname].description.loading || - 'eager'); - if (field_loading != 'eager') { - loading = 'lazy'; - break; - } - } - } else { - loading = (this.model.fields[name].description.loading || - 'eager'); - } - if ((this.group.indexOf(this) >= 0) && (loading == 'eager')) { - var idx = this.group.indexOf(this); - var length = this.group.length; - var n = 1; - while (Object.keys(id2record).length && - ((idx - n >= 0) || (idx + n < length)) && - n < 100) { - if (idx - n >= 0) { - var record = this.group[idx - n]; - if (!(name in record._loaded) && (record.id >= 0)) { - id2record[record.id] = record; - } - } - if (idx + n < length) { - var record = this.group[idx + n]; - if (!(name in record._loaded) && (record.id >= 0)) { - id2record[record.id] = record; - } - } - n++; - } - } - var context = {}; // TODO - var fnames = []; - if (loading == 'eager') { - for (var fname in this.model.fields) { - if (!this.model.fields.hasOwnProperty(fname)) { - continue; - } - if ((this.model.fields[fname].description.loading || - 'eager') == 'eager') { - fnames.push(fname); - } - } - } else { - fnames = Object.keys(this.model.fields); - } - fnames = fnames.filter(function(e, i, a) { - return !(e in self._loaded); - }); - // TODO add rec_name - if (!('rec_name' in fnames)) { - fnames.push('rec_name'); - } - fnames.push('_timestamp'); - // TODO size of binary - var prm = this.model.execute('read', [Object.keys(id2record), - fnames], context); - var succeed = function(values) { - var id2value = {}; - values.forEach(function(e, i, a) { - id2value[e.id] = e; - }); - for (var id in id2record) { - if (!id2record.hasOwnProperty(id)) { - continue; - } - record = id2record[id]; - // TODO exception - var value = id2value[id]; - if (record && value) { - record.set(value); - } - } - }; - var failed = function() { - // TODO call succeed - }; - return prm.then(succeed, failed); - }, - set: function(values) { - for (var name in values) { - if (!values.hasOwnProperty(name)) { - continue; - } - var value = values[name]; - if (name == '_timestamp') { - this._timestamp = value; - continue; - } - if (!(name in this.model.fields)) { - if (name == 'rec_name') { - this.value[name] = value; - } - continue; - } - // TODO delay O2M - // TODO Manage rec_name on M2O and Reference - this.model.fields[name].set(this, value); - this._loaded[name] = true; - } - }, - field_get: function(name) { - return this.model.fields[name].get(this); - }, - field_set: function(name, value) { - this.model.fields[name].set(this, value); - }, - field_get_client: function(name) { - return this.model.fields[name].get_client(this); - }, - field_set_client: function(name) { - this.model.fields[name].set_client(this, value); + var self = this; + if ((this.id < 0) || (name in this._loaded)) { + return jQuery.when(); + } + var id2record = {}; + id2record[this.id] = this; + var loading; + if (name == '*') { + loading = 'eager'; + for (var fname in this.model.fields) { + if (!this.model.fields.hasOwnProperty(fname)) { + continue; } + var field_loading = ( + this.model.fields[fname].description.loading || + 'eager'); + if (field_loading != 'eager') { + loading = 'lazy'; + break; + } + } + } else { + loading = (this.model.fields[name].description.loading || + 'eager'); + } + if ((this.group.indexOf(this) >= 0) && (loading == 'eager')) { + var idx = this.group.indexOf(this); + var length = this.group.length; + var n = 1; + while (Object.keys(id2record).length && + ((idx - n >= 0) || (idx + n < length)) && + n < 100) { + if (idx - n >= 0) { + var record = this.group[idx - n]; + if (!(name in record._loaded) && (record.id >= 0)) { + id2record[record.id] = record; + } + } + if (idx + n < length) { + var record = this.group[idx + n]; + if (!(name in record._loaded) && (record.id >= 0)) { + id2record[record.id] = record; + } + } + n++; + } + } + var context = {}; // TODO + var fnames = []; + if (loading == 'eager') { + for (var fname in this.model.fields) { + if (!this.model.fields.hasOwnProperty(fname)) { + continue; + } + if ((this.model.fields[fname].description.loading || + 'eager') == 'eager') { + fnames.push(fname); + } + } + } else { + fnames = Object.keys(this.model.fields); + } + fnames = fnames.filter(function(e, i, a) { + return !(e in self._loaded); + }); + // TODO add rec_name + if (!('rec_name' in fnames)) { + fnames.push('rec_name'); + } + fnames.push('_timestamp'); + // TODO size of binary + var prm = this.model.execute('read', [Object.keys(id2record), + fnames], context); + var succeed = function(values) { + var id2value = {}; + values.forEach(function(e, i, a) { + id2value[e.id] = e; + }); + for (var id in id2record) { + if (!id2record.hasOwnProperty(id)) { + continue; + } + record = id2record[id]; + // TODO exception + var value = id2value[id]; + if (record && value) { + record.set(value); + } + } + }; + var failed = function() { + // TODO call succeed + }; + return prm.then(succeed, failed); + }, + set: function(values) { + for (var name in values) { + if (!values.hasOwnProperty(name)) { + continue; + } + var value = values[name]; + if (name == '_timestamp') { + this._timestamp = value; + continue; + } + if (!(name in this.model.fields)) { + if (name == 'rec_name') { + this.value[name] = value; + } + continue; + } + // TODO delay O2M + // TODO Manage rec_name on M2O and Reference + this.model.fields[name].set(this, value); + this._loaded[name] = true; + } + }, + field_get: function(name) { + return this.model.fields[name].get(this); + }, + field_set: function(name, value) { + this.model.fields[name].set(this, value); + }, + field_get_client: function(name) { + return this.model.fields[name].get_client(this); + }, + field_set_client: function(name) { + this.model.fields[name].set_client(this, value); + } }); @@ -253,29 +253,29 @@ Sao.field.get = function(type) { Sao.field.Field = Class(Object, { _default: null, init: function(description) { - this.description = description; - this.name = description.name; - }, + this.description = description; + this.name = description.name; + }, set: function(record, value) { - record._values[this.name] = value; - }, + record._values[this.name] = value; + }, get: function(record) { - return record._values[this.name] || this._default; - }, + return record._values[this.name] || this._default; + }, set_client: function(record, value) { - var previous_value = this.get(record); - this.set(record, value); - if (previous_value != this.get(record)) { - record._changed[this.name] = true; - this.changed(record); - } - }, + var previous_value = this.get(record); + this.set(record, value); + if (previous_value != this.get(record)) { + record._changed[this.name] = true; + this.changed(record); + } + }, get_client: function(record) { - return this.get(record); - }, + return this.get(record); + }, changed: function(record) { - // TODO - } + // TODO + } }); Sao.field.Char = Class(Sao.field.Field, { diff --git a/js/rpc.js b/js/rpc.js index d119f33c83..274720ccc3 100644 --- a/js/rpc.js +++ b/js/rpc.js @@ -8,7 +8,6 @@ Sao.rpc = function(args, session) { session = new Sao.Session(); } - var ajax_prm = jQuery.ajax({ 'contentType': 'application/json', 'data': JSON.stringify({ diff --git a/js/screen.js b/js/screen.js index 2e526c3695..be887bb8c3 100644 --- a/js/screen.js +++ b/js/screen.js @@ -4,77 +4,75 @@ Sao.Screen = Class(Object, { init: function(model_name, attributes) { - this.model_name = model_name; - this.model = new Sao.Model(model_name, attributes); - this.attributes = jQuery.extend({}, attributes); - this.view_ids = jQuery.extend([], attributes.view_ids); - this.view_to_load = jQuery.extend([], - attributes.mode || ['tree', 'form']); - this.views = []; - this.current_view = null; - this.context = attributes.context || {}; - this.el = $('
', { - 'class': 'screen' - }); - }, + this.model_name = model_name; + this.model = new Sao.Model(model_name, attributes); + this.attributes = jQuery.extend({}, attributes); + this.view_ids = jQuery.extend([], attributes.view_ids); + this.view_to_load = jQuery.extend([], + attributes.mode || ['tree', 'form']); + this.views = []; + this.current_view = null; + this.context = attributes.context || {}; + this.el = $('
', { + 'class': 'screen' + }); + }, load_next_view: function() { - if (this.view_to_load) { - var view_id; - if (this.view_ids) { - view_id = this.view_ids.shift(); - } - var view_type = this.view_to_load.shift(); - return this.add_view_id(view_id, view_type); - } - return jQuery.when(); - }, + if (this.view_to_load) { + var view_id; + if (this.view_ids) { + view_id = this.view_ids.shift(); + } + var view_type = this.view_to_load.shift(); + return this.add_view_id(view_id, view_type); + } + return jQuery.when(); + }, add_view_id: function(view_id, view_type) { - // TODO preload - var prm = this.model.execute('fields_view_get', - [view_id, view_type], this.context); - prm.done(this.add_view.bind(this)); - return prm; - }, + // TODO preload + var prm = this.model.execute('fields_view_get', + [view_id, view_type], this.context); + prm.done(this.add_view.bind(this)); + return prm; + }, add_view: function(view) { - var arch = view.arch; - var fields = view.fields; - var xml_view = jQuery(jQuery.parseXML(arch)); - // TODO loading lazy/eager - this.model.add_fields(fields); - var view = Sao.View.parse(this, xml_view); - this.views.push(view); - return view; - }, + var arch = view.arch; + var fields = view.fields; + var xml_view = jQuery(jQuery.parseXML(arch)); + // TODO loading lazy/eager + this.model.add_fields(fields); + var view = Sao.View.parse(this, xml_view); + this.views.push(view); + return view; + }, number_of_views: function() { - return this.views.length + this.view_to_load.length; - }, + return this.views.length + this.view_to_load.length; + }, switch_view: function(view_type) { - // TODO check validity - var self = this; - if ((!view_type) || - (!this.current_view) || - (this.current_view.view_type != view_type)) { - for (var i = 0; i < this.number_of_views(); i++) { - if (this.view_to_load.length) { - return this.load_next_view().done(function() { - self.current_view = self.views.slice(-1); - return self.switch_view(view_type); - }); - } - this.current_view = this.views[ - (this.views.indexOf(this.current_view) + 1) % - this.views.length]; - if (!view_type) { - break; - } else if (this.current_view.view_type == - view_type) { - break; - } - } - } - this.el.remove(); - this.el.append(this.current_view.el); - // TODO display and cursor - return jQuery.when(); - } + // TODO check validity + var self = this; + if ((!view_type) || (!this.current_view) || + (this.current_view.view_type != view_type)) { + for (var i = 0; i < this.number_of_views(); i++) { + if (this.view_to_load.length) { + return this.load_next_view().done(function() { + self.current_view = self.views.slice(-1); + return self.switch_view(view_type); + }); + } + this.current_view = this.views[ + (this.views.indexOf(this.current_view) + 1) % + this.views.length]; + if (!view_type) { + break; + } else if (this.current_view.view_type == view_type) { + break; + } + } + } + this.el.remove(); + this.el.append(this.current_view.el); + // TODO display and cursor + return jQuery.when(); + } }); diff --git a/js/session.js b/js/session.js index fa31497bcf..2eea843405 100644 --- a/js/session.js +++ b/js/session.js @@ -4,66 +4,66 @@ Sao.Session = Class(Object, { init: function(database, login) { - this.database = database; - this.login = login; - this.user_id = null; - this.session = null; - if (!Sao.Session.current_session) { - Sao.Session.current_session = this; - } - }, + this.database = database; + this.login = login; + this.user_id = null; + this.session = null; + if (!Sao.Session.current_session) { + Sao.Session.current_session = this; + } + }, do_login: function(login, password) { - var dfd = jQuery.Deferred(); - var args = { - 'method': 'common.db.login', - 'params': [login, password] - }; - var ajax_prm = jQuery.ajax({ - 'contentType': 'application/json', - 'data': JSON.stringify(args), - 'dataType': 'json', - 'url': '/' + this.database, - 'type': 'post' - }); + var dfd = jQuery.Deferred(); + var args = { + 'method': 'common.db.login', + 'params': [login, password] + }; + var ajax_prm = jQuery.ajax({ + 'contentType': 'application/json', + 'data': JSON.stringify(args), + 'dataType': 'json', + 'url': '/' + this.database, + 'type': 'post' + }); - var ajax_success = function(data) { - if (data === null) { - Sao.warning('Unable to reach the server'); - dfd.reject(); - } else if (data.error) { - console.log('ERROR'); - Sao.error(data.error[0], data.error[1]); - dfd.reject(); - } else { - if (!data.result) { - this.user_id = null; - this.session = null; - } else { - this.user_id = data.result[0]; - this.session = data.result[1]; - } - dfd.resolve(); - } - }; - ajax_prm.success(ajax_success.bind(this)); - ajax_prm.error(dfd.reject); - return dfd.promise(); - }, + var ajax_success = function(data) { + if (data === null) { + Sao.warning('Unable to reach the server'); + dfd.reject(); + } else if (data.error) { + console.log('ERROR'); + Sao.error(data.error[0], data.error[1]); + dfd.reject(); + } else { + if (!data.result) { + this.user_id = null; + this.session = null; + } else { + this.user_id = data.result[0]; + this.session = data.result[1]; + } + dfd.resolve(); + } + }; + ajax_prm.success(ajax_success.bind(this)); + ajax_prm.error(dfd.reject); + return dfd.promise(); + }, do_logout: function() { - if (!(this.user_id && this.session)) { - return; - } - var args = { - 'method': 'common.db.logout', - 'params': [] - }; - var prm = Sao.rpc(this, args); - this.database = null; - this.login = null; - this.user_id = null; - this.session = null; - return prm; - } + if (!(this.user_id && this.session)) { + return; + } + var args = { + 'method': 'common.db.logout', + 'params': [] + }; + var prm = Sao.rpc(this, args); + this.database = null; + this.login = null; + this.user_id = null; + this.session = null; + return prm; + } }); Sao.Session.get_credentials = function(parent_dfd) { diff --git a/js/tab.js b/js/tab.js index f6edcaccfd..10f493faa1 100644 --- a/js/tab.js +++ b/js/tab.js @@ -9,8 +9,8 @@ Sao.Tab = Class(Object, { Sao.Tab.Form = Class(Sao.Tab, { init: function(model_name, attributes) { - Sao.Tab.Form._super.init.call(this); - this.screen = new Sao.Screen(model_name, attributes); - this.attributes = jQuery.extend({}, attributes); - } + Sao.Tab.Form._super.init.call(this); + this.screen = new Sao.Screen(model_name, attributes); + this.attributes = jQuery.extend({}, attributes); + } }); diff --git a/js/view.js b/js/view.js index a3c5adad31..e9fb1b783a 100644 --- a/js/view.js +++ b/js/view.js @@ -4,9 +4,9 @@ Sao.View = Class(Object, { init: function(screen, xml) { - this.view_type = null; - this.el = null; - } + this.view_type = null; + this.el = null; + } }); Sao.View.parse = function(screen, xml) { @@ -18,10 +18,10 @@ Sao.View.parse = function(screen, xml) { Sao.View.Tree = Class(Sao.View, { init: function(screen, xml) { - Sao.View.Tree._super.init.call(this, screen, xml); - this.view_type = 'tree'; - this.el = $('
', { - 'class': 'treeview' - }); - } + Sao.View.Tree._super.init.call(this, screen, xml); + this.view_type = 'tree'; + this.el = $('
', { + 'class': 'treeview' + }); + } });