2012-12-31 14:14:52 +01:00
|
|
|
/* This file is part of Tryton. The COPYRIGHT file at the top level of
|
|
|
|
this repository contains the full copyright notices and license terms. */
|
2013-01-04 21:29:48 +01:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
2012-12-31 14:14:52 +01:00
|
|
|
|
2013-01-04 21:29:48 +01:00
|
|
|
Sao.common = {};
|
2012-12-31 14:14:52 +01:00
|
|
|
|
2013-04-18 13:07:21 +02:00
|
|
|
Sao.common.BACKSPACE_KEYCODE = 8;
|
|
|
|
Sao.common.TAB_KEYCODE = 9;
|
|
|
|
Sao.common.RETURN_KEYCODE = 13;
|
|
|
|
Sao.common.DELETE_KEYCODE = 46;
|
|
|
|
Sao.common.F2_KEYCODE = 113;
|
|
|
|
Sao.common.F3_KEYCODE = 114;
|
|
|
|
|
2013-02-06 17:33:27 +01:00
|
|
|
Sao.common.compare = function(arr1, arr2) {
|
|
|
|
return (jQuery(arr1).not(arr2).length === 0 &&
|
|
|
|
jQuery(arr2).not(arr1).length === 0);
|
|
|
|
};
|
|
|
|
|
2013-04-08 19:15:49 +02:00
|
|
|
// Find the intersection of two arrays.
|
|
|
|
// The arrays must be sorted.
|
|
|
|
Sao.common.intersect = function(a, b) {
|
|
|
|
var ai = 0, bi = 0;
|
|
|
|
var result = [];
|
|
|
|
while (ai < a.length && bi < b.length) {
|
|
|
|
if (a[ai] < b[bi]) {
|
|
|
|
ai++;
|
|
|
|
} else if (a[ai] > b[bi]) {
|
|
|
|
bi++;
|
|
|
|
} else {
|
|
|
|
result.push(a[ai]);
|
|
|
|
ai++;
|
|
|
|
bi++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2013-01-04 21:29:48 +01:00
|
|
|
Sao.common.selection = function(title, values, alwaysask) {
|
|
|
|
if (alwaysask === undefined) {
|
|
|
|
alwaysask = false;
|
|
|
|
}
|
2013-04-17 11:42:12 +02:00
|
|
|
var prm = jQuery.Deferred();
|
2013-01-04 21:29:48 +01:00
|
|
|
if ((Object.keys(values).length == 1) && (!alwaysask)) {
|
|
|
|
var key = Object.keys(values)[0];
|
|
|
|
prm.resolve(values[key]);
|
|
|
|
return prm;
|
|
|
|
}
|
|
|
|
// TODO
|
2013-04-17 11:42:12 +02:00
|
|
|
return prm.fail();
|
2013-01-04 21:29:48 +01:00
|
|
|
};
|
2013-01-22 17:13:26 +01:00
|
|
|
|
|
|
|
Sao.common.date_format = function() {
|
|
|
|
// TODO
|
|
|
|
// http://stackoverflow.com/questions/2678230/how-to-getting-browser-current-locale-preference-using-javascript
|
|
|
|
return '%m/%d/%Y';
|
|
|
|
};
|
|
|
|
|
|
|
|
Sao.common.text_to_float_time = function(text, conversion, digit) {
|
|
|
|
// TODO
|
|
|
|
return text;
|
|
|
|
};
|
2013-01-23 19:37:27 +01:00
|
|
|
|
|
|
|
Sao.common.EvalEnvironment = function(parent_, eval_type) {
|
|
|
|
if (eval_type === undefined)
|
|
|
|
eval_type = 'eval';
|
|
|
|
var environment;
|
|
|
|
if (eval_type == 'eval') {
|
|
|
|
environment = parent_.get_eval();
|
|
|
|
} else {
|
|
|
|
environment = {};
|
2013-04-08 16:55:00 +02:00
|
|
|
for (var key in parent_.model.fields) {
|
|
|
|
var field = parent_.model.fields[key];
|
2013-01-23 19:37:27 +01:00
|
|
|
environment[key] = field.get_on_change_value(parent_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
environment.id = parent_.id;
|
|
|
|
if (parent_.parent)
|
2013-02-04 18:29:15 +01:00
|
|
|
Object.defineProperty(environment, '_parent_' +
|
|
|
|
parent_.parent_name, {
|
2013-01-23 19:37:27 +01:00
|
|
|
'enumerable': true,
|
2013-02-04 18:29:15 +01:00
|
|
|
'get': function() {
|
|
|
|
return Sao.common.EvalEnvironment(parent_.parent,
|
|
|
|
eval_type);
|
2013-01-23 19:37:27 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
environment.get = function(item, default_) {
|
|
|
|
if (this.hasOwnProperty(item))
|
|
|
|
return this[item];
|
|
|
|
return default_;
|
|
|
|
};
|
|
|
|
|
|
|
|
return environment;
|
|
|
|
};
|
2013-02-06 17:34:32 +01:00
|
|
|
|
|
|
|
Sao.common.selection_mixin = {};
|
|
|
|
Sao.common.selection_mixin.init = function() {
|
|
|
|
this.selection = null;
|
|
|
|
this._last_domain = null;
|
|
|
|
this._values2selection = {};
|
|
|
|
this._domain_cache = {};
|
|
|
|
};
|
|
|
|
Sao.common.selection_mixin.init_selection = function(key, callback) {
|
|
|
|
if (!key) {
|
|
|
|
key = [];
|
|
|
|
(this.attributes.selection_change_with || []).forEach(function(e) {
|
|
|
|
key.push([e, null]);
|
|
|
|
});
|
|
|
|
key.sort();
|
|
|
|
}
|
|
|
|
var selection = jQuery.extend([], this.attributes.selection || []);
|
|
|
|
var prepare_selection = function(selection) {
|
|
|
|
if (this.attributes.sort === undefined || this.attributes.sort) {
|
|
|
|
selection.sort(function(a, b) {
|
|
|
|
return a[1].localeCompare(b[1]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.selection = jQuery.extend([], selection);
|
|
|
|
if (callback) callback(this.selection);
|
|
|
|
};
|
|
|
|
if (!(selection instanceof Object) &&
|
|
|
|
!(key in this._values2selection)) {
|
|
|
|
var prm;
|
|
|
|
if (key) {
|
|
|
|
var params = {};
|
|
|
|
key.forEach(function(e) {
|
|
|
|
params[e[0]] = e[1];
|
|
|
|
});
|
|
|
|
prm = this.model.execute(selection, [params], {});
|
|
|
|
} else {
|
|
|
|
prm = this.model.execute(selection, [], {});
|
|
|
|
}
|
|
|
|
prm.pipe(prepare_selection.bind(this));
|
|
|
|
prm.pipe(this.set_selection.bind(this));
|
|
|
|
} else {
|
|
|
|
if (key in this._values2selection) {
|
|
|
|
selection = this._values2selection.selection;
|
|
|
|
}
|
|
|
|
prepare_selection.call(this, selection);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Sao.common.selection_mixin.update_selection = function(record, field,
|
|
|
|
callback) {
|
|
|
|
if (!field) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!('relation' in this.attributes)) {
|
|
|
|
var change_with = this.attributes.selection_change_with || [];
|
|
|
|
var key = [];
|
|
|
|
var args = record._get_on_change_args(change_with);
|
|
|
|
for (var k in args) {
|
|
|
|
key.push([k, args[k]]);
|
|
|
|
}
|
|
|
|
key.sort();
|
|
|
|
Sao.common.selection_mixin.init_selection.call(this, key,
|
|
|
|
callback);
|
|
|
|
} else {
|
|
|
|
var domain = field.get_domain(record);
|
|
|
|
var jdomain = JSON.stringify(domain);
|
|
|
|
if (jdomain in this._domain_cache) {
|
|
|
|
this.selection = this._domain_cache[jdomain];
|
|
|
|
this._last_domain = domain;
|
|
|
|
}
|
|
|
|
if (Sao.common.compare(domain, this._last_domain)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var prm = Sao.rpc({
|
|
|
|
'method': 'model.' + this.attributes.relation + '.search_read',
|
|
|
|
'params': [domain, 0, null, null, ['rec_name'], {}]
|
|
|
|
}, record.model.session);
|
|
|
|
prm.done(function(result) {
|
|
|
|
var selection = [];
|
|
|
|
result.forEach(function(x) {
|
|
|
|
selection.push([x.id, x.rec_name]);
|
|
|
|
});
|
|
|
|
selection.push([null, '']);
|
|
|
|
this._last_domain = domain;
|
|
|
|
this._domain_cache[jdomain] = selection;
|
|
|
|
this.selection = jQuery.extend([], selection);
|
|
|
|
if (callback) {
|
|
|
|
callback(this.selection);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
prm.fail(function() {
|
|
|
|
this._last_domain = null;
|
|
|
|
this.selection = [];
|
|
|
|
if (callback) {
|
|
|
|
callback(this.selection);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
};
|
2013-04-18 17:25:23 +02:00
|
|
|
|
2013-05-02 17:23:37 +02:00
|
|
|
Sao.common.Button = Sao.class_(Object, {
|
|
|
|
init: function(attributes) {
|
|
|
|
this.attributes = attributes;
|
|
|
|
this.el = jQuery('<button/>').button({
|
|
|
|
text: true,
|
|
|
|
label: attributes.string || ''
|
|
|
|
});
|
|
|
|
// TODO icon
|
|
|
|
},
|
|
|
|
state_set: function(record) {
|
2013-04-18 17:25:23 +02:00
|
|
|
// TODO
|
2013-05-02 17:23:37 +02:00
|
|
|
}
|
|
|
|
});
|
2013-01-04 21:29:48 +01:00
|
|
|
}());
|