Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cedk committed Nov 29, 2012
0 parents commit c1fdf7b
Show file tree
Hide file tree
Showing 13 changed files with 1,515 additions and 0 deletions.
17 changes: 17 additions & 0 deletions COPYRIGHT
@@ -0,0 +1,17 @@
Copyright (C) 2012 Nicolas Évrard.
Copyright (C) 2012 Cédric Krier.
Copyright (C) 2012 Bertrand Chenal.
Copyright (C) 2012 B2CK SPRL.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions index.html
@@ -0,0 +1,50 @@
<!DOCTYPE html>
<!-- This file is part of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. -->
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<TITLE>Tryton</TITLE>
<SCRIPT type="text/javascript" src="external/jquery.js"></SCRIPT>
<SCRIPT type="text/javascript">
if (typeof jQuery == 'undefined') {
document.write(unescape('%3CSCRIPT type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.js"%3E%3C/SCRIPT%3E'));
}
</SCRIPT>
<SCRIPT type="text/javascript" src="external/jquery-ui.js"></SCRIPT>
<LINK rel="stylesheet" type="text/css" href="external/jquery-ui.css"
media="screen"/>
<SCRIPT type="text/javascript">
if (typeof jQuery.ui == 'undefined') {
document.write(unescape('%3CSCRIPT type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"%3E%3C/SCRIPT%3E'));
document.write(unescape('%3CLINK rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" media="screen"/%3E'));
}
</SCRIPT>
<SCRIPT type="text/javascript" src="js/class.js"></SCRIPT>
<SCRIPT type="text/javascript" src="js/sao.js"></SCRIPT>
<SCRIPT type="text/javascript" src="js/rpc.js"></SCRIPT>
<SCRIPT type="text/javascript" src="js/session.js"></SCRIPT>
<SCRIPT type="text/javascript" src="js/model.js"></SCRIPT>
<SCRIPT type="text/javascript" src="js/tab.js"></SCRIPT>
<SCRIPT type="text/javascript" src="js/screen.js"></SCRIPT>
<SCRIPT type="text/javascript" src="js/view.js"></SCRIPT>
</HEAD>
<BODY>
<SCRIPT type="text/javascript">
$(function() {
dfd = jQuery.Deferred();
Sao.Session.get_credentials(dfd);
dfd.done(function(session) {
Sao.Session.current_session = session;
var form = new Sao.Tab.Form('ir.ui.menu', {});
form.screen.load_next_view().pipe(function() {
return form.screen.switch_view();
}).done(function() {
$('#menu').html(form.screen.el);
});
});
});
</SCRIPT>
<DIV id="menu"/>
</BODY>
</HTML>
24 changes: 24 additions & 0 deletions js/class.js
@@ -0,0 +1,24 @@
/* This file is part of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. */
'use strict';

var Class = function(Parent, props) {

var ClassConstructor = function() {
if (!(this instanceof ClassConstructor))
throw Error('Constructor function requires new operator');
if (this.init) {
this.init.apply(this, arguments);
}
};

// Plug prototype chain
ClassConstructor.prototype = Object.create(Parent.prototype);
ClassConstructor._super = Parent.prototype;
if (props) {
for (var name in props) {
ClassConstructor.prototype[name] = props[name];
}
}
return ClassConstructor;
};
231 changes: 231 additions & 0 deletions js/model.js
@@ -0,0 +1,231 @@
/* This file is part of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. */
'use strict';

Sao.Model = Class(Object, {
init: function(name, 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);
}
}
},
execute: function(method, params, context) {
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 prm = this.execute('search',
[condition, offset, limit, order], context);
var instanciate = function(ids) {
return Sao.Group(this, ids.map(function(id) {
return new Sao.Record(this, id);
}));
};
return prm.pipe(instanciate.bind(this));
},
delete: function(records) {
var context = {}; // TODO
return this.execute('delete', [records.map(function(record) {
return record.id;
})], context);
}
});

Sao.Group = function(model, array) {
array.model = model;
return array;
};

Sao.Record = Class(Object, {
id_counter: -1,
init: function(model, id) {
this.model = model;
this.id = id || Sao.Record.prototype.id_counter--;
this._values = {};
this._changed = {};
this._loaded = {};
this.fields = {};
},
has_changed: function() {
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;
},
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);
return prm;
},
reload: function() {
this._values = {};
this._loaded = {};
this._changed = {};
},
load: function(name) {
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');
}
// TODO group
var context = {}; // TODO
var fnames = [];
if (loading == 'eager') {
for (var fname in this.model.fields) {
if (!this.model.fields.hasOwnProperty(fname)) {
continue;
}
if ((this.mode.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 this._loaded);
});
// TODO add rec_name
if (!('rec_name' in fnames)) {
fnames.push('rec_name');
}
fnames.push('_timestamp');
// TODO size of binary
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
value = id2value[id];
if (record && value) {
record.set(value);
}
}
};
var failed = function() {
// TODO call succeed
};
return prm;
},
get: function(name) {
return this.model.fields[name].get(this);
},
set: function(name, value) {
this.model.fields[name].set(this, value);
},
get_client: function(name) {
return this.model.fields[name].get_client(this);
},
set_client: function(name) {
this.model.fields[name].set_client(this, value);
}
});

Sao.field = {};

Sao.field.get = function(type) {
switch (type) {
default:
return Sao.field.Char;
}
};

Sao.field.Field = Class(Object, {
_default: null,
init: function(description) {
this.description = description;
this.name = description.name;
},
set: function(record, value) {
record._values[this.name] = value;
},
get: function(record) {
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);
}
},
get_client: function(record) {
return this.get(record);
},
changed: function(record) {
// TODO
}
});

Sao.field.Char = Class(Sao.field.Field, {
_default: ''
});
59 changes: 59 additions & 0 deletions js/rpc.js
@@ -0,0 +1,59 @@
/* This file is part of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. */
'use strict';

Sao.rpc = function(args, session) {
var dfd = jQuery.Deferred();
if (!session) {
session = new Sao.Session();
}


var ajax_prm = jQuery.ajax({
'contentType': 'application/json',
'data': JSON.stringify({
'method': args.method,
'params': [session.user_id, session.session].concat(args.params)
}),
'dataType': 'json',
'url': '/' + (session.database || ''),
'type': 'post'
});

var ajax_success = function(data) {
if (data === null) {
Sao.warning('Unable to reach the server');
dfd.reject();
} else if (data.error) {
if (data.error[0] == 'UserWarning') {
} else if (data.error[0] == 'UserError') {
// TODO
} else if (data.error[0] == 'ConcurrencyException') {
// TODO
} else if (data.error[0] == 'NotLogged') {
//Try to relog
var cred_prm = jQuery.Deferred();
Sao.Session.renew_credentials(session, cred_prm);
cred_prm.done(function() {
Sao.rpc(session, args, dfd);
});
cred_prm.fail(dfd.reject);
} else {
console.log('ERROR');
Sao.error(data.error[0], data.error[1]);
}
dfd.reject();
} else {
dfd.resolve(data.result);
}
};

var ajax_error = function() {
console.log('ERROR');
dfd.reject();
};
ajax_prm.success(ajax_success);
ajax_prm.error(ajax_error);

return dfd.promise();
};
13 changes: 13 additions & 0 deletions js/sao.js
@@ -0,0 +1,13 @@
/* This file is part of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. */
'use strict';

var Sao = {};

Sao.error = function(title, message) {
alert(title + '\n' + (message || ''));
};

Sao.warning = function(title, message) {
alert(title + '\n' + (message || ''));
};

0 comments on commit c1fdf7b

Please sign in to comment.