2011-07-15 14:16:00 +00:00
|
|
|
/*---------------------------------------------------------
|
2011-08-11 03:10:00 +00:00
|
|
|
* OpenERP Web core
|
2011-07-15 14:16:00 +00:00
|
|
|
*--------------------------------------------------------*/
|
2011-09-21 08:16:51 +00:00
|
|
|
var console;
|
|
|
|
if (!console) {
|
|
|
|
console = {log: function () {}};
|
|
|
|
}
|
2011-09-13 12:08:00 +00:00
|
|
|
if (!console.debug) {
|
|
|
|
console.debug = console.log;
|
|
|
|
}
|
|
|
|
|
2011-09-05 11:03:09 +00:00
|
|
|
openerp.web.core = function(openerp) {
|
2011-07-15 14:16:00 +00:00
|
|
|
/**
|
|
|
|
* John Resig Class with factory improvement
|
|
|
|
*/
|
|
|
|
(function() {
|
2011-08-08 08:11:01 +00:00
|
|
|
var initializing = false,
|
|
|
|
fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
|
2011-09-05 11:03:09 +00:00
|
|
|
// The web Class implementation (does nothing)
|
2011-08-17 08:55:32 +00:00
|
|
|
/**
|
2011-09-12 10:35:03 +00:00
|
|
|
* Extended version of John Resig's Class pattern
|
|
|
|
*
|
2011-08-17 08:55:32 +00:00
|
|
|
* @class
|
|
|
|
*/
|
2011-09-05 11:03:09 +00:00
|
|
|
openerp.web.Class = function(){};
|
2011-07-15 14:16:00 +00:00
|
|
|
|
2011-09-12 10:35:03 +00:00
|
|
|
/**
|
|
|
|
* Subclass an existing class
|
|
|
|
*
|
|
|
|
* @param {Object} prop class-level properties (class attributes and instance methods) to set on the new class
|
|
|
|
*/
|
2011-09-05 11:03:09 +00:00
|
|
|
openerp.web.Class.extend = function(prop) {
|
2011-08-08 08:11:01 +00:00
|
|
|
var _super = this.prototype;
|
2011-07-15 14:16:00 +00:00
|
|
|
|
2011-09-05 11:03:09 +00:00
|
|
|
// Instantiate a web class (but only create the instance,
|
2011-08-08 08:11:01 +00:00
|
|
|
// don't run the init constructor)
|
|
|
|
initializing = true;
|
|
|
|
var prototype = new this();
|
|
|
|
initializing = false;
|
2011-07-15 14:16:00 +00:00
|
|
|
|
2011-08-08 08:11:01 +00:00
|
|
|
// Copy the properties over onto the new prototype
|
|
|
|
for (var name in prop) {
|
|
|
|
// Check if we're overwriting an existing function
|
|
|
|
prototype[name] = typeof prop[name] == "function" &&
|
|
|
|
typeof _super[name] == "function" &&
|
|
|
|
fnTest.test(prop[name]) ?
|
|
|
|
(function(name, fn) {
|
|
|
|
return function() {
|
|
|
|
var tmp = this._super;
|
2011-07-15 14:16:00 +00:00
|
|
|
|
2011-08-08 08:11:01 +00:00
|
|
|
// Add a new ._super() method that is the same
|
|
|
|
// method but on the super-class
|
|
|
|
this._super = _super[name];
|
2011-07-15 14:16:00 +00:00
|
|
|
|
2011-08-08 08:11:01 +00:00
|
|
|
// The method only need to be bound temporarily, so
|
|
|
|
// we remove it when we're done executing
|
|
|
|
var ret = fn.apply(this, arguments);
|
|
|
|
this._super = tmp;
|
2011-07-15 14:16:00 +00:00
|
|
|
|
2011-08-08 08:11:01 +00:00
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
})(name, prop[name]) :
|
|
|
|
prop[name];
|
|
|
|
}
|
2011-07-15 14:16:00 +00:00
|
|
|
|
2011-08-08 08:11:01 +00:00
|
|
|
// The dummy class constructor
|
|
|
|
function Class() {
|
|
|
|
// All construction is actually done in the init method
|
|
|
|
if (!initializing && this.init) {
|
|
|
|
var ret = this.init.apply(this, arguments);
|
|
|
|
if (ret) { return ret; }
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
2011-08-08 09:57:50 +00:00
|
|
|
Class.include = function (properties) {
|
2011-08-08 09:40:30 +00:00
|
|
|
for (var name in properties) {
|
|
|
|
if (typeof properties[name] !== 'function'
|
|
|
|
|| !fnTest.test(properties[name])) {
|
|
|
|
prototype[name] = properties[name];
|
|
|
|
} else if (typeof prototype[name] === 'function'
|
|
|
|
&& prototype.hasOwnProperty(name)) {
|
|
|
|
prototype[name] = (function (name, fn, previous) {
|
|
|
|
return function () {
|
|
|
|
var tmp = this._super;
|
|
|
|
this._super = previous;
|
|
|
|
var ret = fn.apply(this, arguments);
|
|
|
|
this._super = tmp;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
})(name, properties[name], prototype[name]);
|
|
|
|
} else if (typeof _super[name] === 'function') {
|
|
|
|
prototype[name] = (function (name, fn) {
|
|
|
|
return function () {
|
|
|
|
var tmp = this._super;
|
|
|
|
this._super = _super[name];
|
|
|
|
var ret = fn.apply(this, arguments);
|
|
|
|
this._super = tmp;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
})(name, properties[name]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2011-07-15 14:16:00 +00:00
|
|
|
|
2011-08-08 08:11:01 +00:00
|
|
|
// Populate our constructed prototype object
|
|
|
|
Class.prototype = prototype;
|
2011-07-15 14:16:00 +00:00
|
|
|
|
2011-08-08 08:11:01 +00:00
|
|
|
// Enforce the constructor to be what we expect
|
|
|
|
Class.constructor = Class;
|
2011-07-15 14:16:00 +00:00
|
|
|
|
2011-08-08 08:11:01 +00:00
|
|
|
// And make this class extendable
|
|
|
|
Class.extend = arguments.callee;
|
2011-07-15 14:16:00 +00:00
|
|
|
|
2011-08-08 08:11:01 +00:00
|
|
|
return Class;
|
|
|
|
};
|
2011-07-22 11:21:08 +00:00
|
|
|
})();
|
2011-07-15 14:16:00 +00:00
|
|
|
|
2011-09-05 11:03:09 +00:00
|
|
|
openerp.web.callback = function(obj, method) {
|
2011-07-15 14:21:12 +00:00
|
|
|
var callback = function() {
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
var r;
|
|
|
|
for(var i = 0; i < callback.callback_chain.length; i++) {
|
|
|
|
var c = callback.callback_chain[i];
|
|
|
|
if(c.unique) {
|
|
|
|
callback.callback_chain.splice(i, 1);
|
|
|
|
i -= 1;
|
|
|
|
}
|
2011-11-17 13:08:02 +00:00
|
|
|
var result = c.callback.apply(c.self, c.args.concat(args));
|
|
|
|
if (c.callback === method) {
|
|
|
|
// return the result of the original method
|
|
|
|
r = result;
|
|
|
|
}
|
2011-07-15 14:21:12 +00:00
|
|
|
// TODO special value to stop the chain
|
2011-09-05 11:03:09 +00:00
|
|
|
// openerp.web.callback_stop
|
2011-07-15 14:21:12 +00:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
};
|
|
|
|
callback.callback_chain = [];
|
|
|
|
callback.add = function(f) {
|
|
|
|
if(typeof(f) == 'function') {
|
|
|
|
f = { callback: f, args: Array.prototype.slice.call(arguments, 1) };
|
|
|
|
}
|
|
|
|
f.self = f.self || null;
|
|
|
|
f.args = f.args || [];
|
|
|
|
f.unique = !!f.unique;
|
|
|
|
if(f.position == 'last') {
|
|
|
|
callback.callback_chain.push(f);
|
|
|
|
} else {
|
|
|
|
callback.callback_chain.unshift(f);
|
|
|
|
}
|
|
|
|
return callback;
|
|
|
|
};
|
|
|
|
callback.add_first = function(f) {
|
|
|
|
return callback.add.apply(null,arguments);
|
|
|
|
};
|
|
|
|
callback.add_last = function(f) {
|
|
|
|
return callback.add({
|
|
|
|
callback: f,
|
|
|
|
args: Array.prototype.slice.call(arguments, 1),
|
|
|
|
position: "last"
|
|
|
|
});
|
|
|
|
};
|
2012-01-04 18:11:50 +00:00
|
|
|
callback.remove = function(f) {
|
|
|
|
callback.callback_chain = _.difference(callback.callback_chain, _.filter(callback.callback_chain, function(el) {
|
|
|
|
return el.callback === f;
|
|
|
|
}));
|
|
|
|
return callback;
|
|
|
|
};
|
2011-07-15 14:21:12 +00:00
|
|
|
|
|
|
|
return callback.add({
|
|
|
|
callback: method,
|
|
|
|
self:obj,
|
|
|
|
args:Array.prototype.slice.call(arguments, 2)
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2011-07-22 11:21:08 +00:00
|
|
|
/**
|
|
|
|
* Generates an inherited class that replaces all the methods by null methods (methods
|
|
|
|
* that does nothing and always return undefined).
|
2011-08-02 07:36:54 +00:00
|
|
|
*
|
2011-07-22 11:21:08 +00:00
|
|
|
* @param {Class} claz
|
2011-09-12 10:35:03 +00:00
|
|
|
* @param {Object} add Additional functions to override.
|
2011-07-22 11:21:08 +00:00
|
|
|
* @return {Class}
|
|
|
|
*/
|
2011-09-05 11:03:09 +00:00
|
|
|
openerp.web.generate_null_object_class = function(claz, add) {
|
2011-07-22 11:21:08 +00:00
|
|
|
var newer = {};
|
|
|
|
var copy_proto = function(prototype) {
|
|
|
|
for (var name in prototype) {
|
|
|
|
if(typeof prototype[name] == "function") {
|
|
|
|
newer[name] = function() {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (prototype.prototype)
|
|
|
|
copy_proto(prototype.prototype);
|
|
|
|
};
|
|
|
|
copy_proto(claz.prototype);
|
2011-09-05 11:03:09 +00:00
|
|
|
newer.init = openerp.web.Widget.prototype.init;
|
2011-07-22 11:21:08 +00:00
|
|
|
var tmpclass = claz.extend(newer);
|
|
|
|
return tmpclass.extend(add || {});
|
|
|
|
};
|
|
|
|
|
2011-07-29 12:40:16 +00:00
|
|
|
/**
|
2011-09-05 11:03:09 +00:00
|
|
|
* web error for lookup failure
|
2011-07-29 12:40:16 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
*/
|
2011-09-05 11:03:09 +00:00
|
|
|
openerp.web.NotFound = openerp.web.Class.extend( /** @lends openerp.web.NotFound# */ {
|
2011-07-29 12:40:16 +00:00
|
|
|
});
|
2011-09-05 11:03:09 +00:00
|
|
|
openerp.web.KeyNotFound = openerp.web.NotFound.extend( /** @lends openerp.web.KeyNotFound# */ {
|
2011-07-29 12:40:16 +00:00
|
|
|
/**
|
|
|
|
* Thrown when a key could not be found in a mapping
|
|
|
|
*
|
2011-09-12 10:35:03 +00:00
|
|
|
* @constructs openerp.web.KeyNotFound
|
2011-09-05 11:03:09 +00:00
|
|
|
* @extends openerp.web.NotFound
|
2011-07-29 12:40:16 +00:00
|
|
|
* @param {String} key the key which could not be found
|
|
|
|
*/
|
|
|
|
init: function (key) {
|
|
|
|
this.key = key;
|
|
|
|
},
|
|
|
|
toString: function () {
|
|
|
|
return "The key " + this.key + " was not found";
|
|
|
|
}
|
|
|
|
});
|
2011-09-05 11:03:09 +00:00
|
|
|
openerp.web.ObjectNotFound = openerp.web.NotFound.extend( /** @lends openerp.web.ObjectNotFound# */ {
|
2011-07-29 12:40:16 +00:00
|
|
|
/**
|
|
|
|
* Thrown when an object path does not designate a valid class or object
|
|
|
|
* in the openerp hierarchy.
|
|
|
|
*
|
2011-09-12 10:35:03 +00:00
|
|
|
* @constructs openerp.web.ObjectNotFound
|
2011-09-05 11:03:09 +00:00
|
|
|
* @extends openerp.web.NotFound
|
2011-07-29 12:40:16 +00:00
|
|
|
* @param {String} path the invalid object path
|
|
|
|
*/
|
|
|
|
init: function (path) {
|
|
|
|
this.path = path;
|
|
|
|
},
|
|
|
|
toString: function () {
|
|
|
|
return "Could not find any object of path " + this.path;
|
|
|
|
}
|
|
|
|
});
|
2011-09-05 11:03:09 +00:00
|
|
|
openerp.web.Registry = openerp.web.Class.extend( /** @lends openerp.web.Registry# */ {
|
2011-07-29 12:40:16 +00:00
|
|
|
/**
|
|
|
|
* Stores a mapping of arbitrary key (strings) to object paths (as strings
|
|
|
|
* as well).
|
|
|
|
*
|
|
|
|
* Resolves those paths at query time in order to always fetch the correct
|
|
|
|
* object, even if those objects have been overloaded/replaced after the
|
|
|
|
* registry was created.
|
|
|
|
*
|
|
|
|
* An object path is simply a dotted name from the openerp root to the
|
2011-10-27 15:31:32 +00:00
|
|
|
* object pointed to (e.g. ``"openerp.web.Connection"`` for an OpenERP
|
|
|
|
* connection object).
|
2011-07-29 12:40:16 +00:00
|
|
|
*
|
2011-09-12 10:35:03 +00:00
|
|
|
* @constructs openerp.web.Registry
|
2011-07-29 12:40:16 +00:00
|
|
|
* @param {Object} mapping a mapping of keys to object-paths
|
|
|
|
*/
|
|
|
|
init: function (mapping) {
|
|
|
|
this.map = mapping || {};
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Retrieves the object matching the provided key string.
|
|
|
|
*
|
|
|
|
* @param {String} key the key to fetch the object for
|
2011-12-16 11:20:54 +00:00
|
|
|
* @param {Boolean} [silent_error=false] returns undefined if the key or object is not found, rather than throwing an exception
|
2011-07-29 12:40:16 +00:00
|
|
|
* @returns {Class} the stored class, to initialize
|
|
|
|
*
|
2011-09-05 11:03:09 +00:00
|
|
|
* @throws {openerp.web.KeyNotFound} if the object was not in the mapping
|
|
|
|
* @throws {openerp.web.ObjectNotFound} if the object path was invalid
|
2011-07-29 12:40:16 +00:00
|
|
|
*/
|
2011-12-16 11:20:54 +00:00
|
|
|
get_object: function (key, silent_error) {
|
2011-07-29 12:40:16 +00:00
|
|
|
var path_string = this.map[key];
|
|
|
|
if (path_string === undefined) {
|
2011-12-16 11:20:54 +00:00
|
|
|
if (silent_error) { return void 'nooo'; }
|
2011-09-05 11:03:09 +00:00
|
|
|
throw new openerp.web.KeyNotFound(key);
|
2011-07-29 12:40:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var object_match = openerp;
|
|
|
|
var path = path_string.split('.');
|
|
|
|
// ignore first section
|
|
|
|
for(var i=1; i<path.length; ++i) {
|
|
|
|
object_match = object_match[path[i]];
|
|
|
|
|
|
|
|
if (object_match === undefined) {
|
2011-12-16 11:20:54 +00:00
|
|
|
if (silent_error) { return void 'noooooo'; }
|
2011-09-05 11:03:09 +00:00
|
|
|
throw new openerp.web.ObjectNotFound(path_string);
|
2011-07-29 12:40:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return object_match;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Tries a number of keys, and returns the first object matching one of
|
|
|
|
* the keys.
|
|
|
|
*
|
|
|
|
* @param {Array} keys a sequence of keys to fetch the object for
|
|
|
|
* @returns {Class} the first class found matching an object
|
|
|
|
*
|
2011-09-05 11:03:09 +00:00
|
|
|
* @throws {openerp.web.KeyNotFound} if none of the keys was in the mapping
|
|
|
|
* @trows {openerp.web.ObjectNotFound} if a found object path was invalid
|
2011-07-29 12:40:16 +00:00
|
|
|
*/
|
|
|
|
get_any: function (keys) {
|
|
|
|
for (var i=0; i<keys.length; ++i) {
|
2011-10-12 13:12:55 +00:00
|
|
|
var key = keys[i];
|
|
|
|
if (key === undefined || !(key in this.map)) {
|
|
|
|
continue;
|
2011-07-29 12:40:16 +00:00
|
|
|
}
|
2011-10-12 13:12:55 +00:00
|
|
|
|
|
|
|
return this.get_object(key);
|
2011-07-29 12:40:16 +00:00
|
|
|
}
|
2011-09-05 11:03:09 +00:00
|
|
|
throw new openerp.web.KeyNotFound(keys.join(','));
|
2011-07-29 12:40:16 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Adds a new key and value to the registry.
|
|
|
|
*
|
|
|
|
* This method can be chained.
|
|
|
|
*
|
|
|
|
* @param {String} key
|
|
|
|
* @param {String} object_path fully qualified dotted object path
|
2011-09-05 11:03:09 +00:00
|
|
|
* @returns {openerp.web.Registry} itself
|
2011-07-29 12:40:16 +00:00
|
|
|
*/
|
|
|
|
add: function (key, object_path) {
|
|
|
|
this.map[key] = object_path;
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Creates and returns a copy of the current mapping, with the provided
|
|
|
|
* mapping argument added in (replacing existing keys if needed)
|
|
|
|
*
|
|
|
|
* @param {Object} [mapping={}] a mapping of keys to object-paths
|
|
|
|
*/
|
|
|
|
clone: function (mapping) {
|
2011-09-05 11:03:09 +00:00
|
|
|
return new openerp.web.Registry(
|
2011-07-29 12:40:16 +00:00
|
|
|
_.extend({}, this.map, mapping || {}));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-09-12 10:35:03 +00:00
|
|
|
openerp.web.CallbackEnabled = openerp.web.Class.extend(/** @lends openerp.web.CallbackEnabled# */{
|
|
|
|
/**
|
|
|
|
* @constructs openerp.web.CallbackEnabled
|
|
|
|
* @extends openerp.web.Class
|
|
|
|
*/
|
2011-08-12 10:19:52 +00:00
|
|
|
init: function() {
|
2011-09-05 11:03:09 +00:00
|
|
|
// Transform on_* method into openerp.web.callbacks
|
2011-07-15 14:21:12 +00:00
|
|
|
for (var name in this) {
|
|
|
|
if(typeof(this[name]) == "function") {
|
|
|
|
this[name].debug_name = name;
|
|
|
|
// bind ALL function to this not only on_and _do ?
|
|
|
|
if((/^on_|^do_/).test(name)) {
|
2011-09-05 11:03:09 +00:00
|
|
|
this[name] = openerp.web.callback(this, this[name]);
|
2011-07-15 14:21:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-12 08:49:10 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Proxies a method of the object, in order to keep the right ``this`` on
|
|
|
|
* method invocations.
|
|
|
|
*
|
|
|
|
* This method is similar to ``Function.prototype.bind`` or ``_.bind``, and
|
|
|
|
* even more so to ``jQuery.proxy`` with a fundamental difference: its
|
|
|
|
* resolution of the method being called is lazy, meaning it will use the
|
|
|
|
* method as it is when the proxy is called, not when the proxy is created.
|
|
|
|
*
|
|
|
|
* Other methods will fix the bound method to what it is when creating the
|
|
|
|
* binding/proxy, which is fine in most javascript code but problematic in
|
|
|
|
* OpenERP Web where developers may want to replace existing callbacks with
|
|
|
|
* theirs.
|
|
|
|
*
|
|
|
|
* The semantics of this precisely replace closing over the method call.
|
|
|
|
*
|
|
|
|
* @param {String} method_name name of the method to invoke
|
|
|
|
* @returns {Function} proxied method
|
|
|
|
*/
|
|
|
|
proxy: function (method_name) {
|
|
|
|
var self = this;
|
|
|
|
return function () {
|
|
|
|
return self[method_name].apply(self, arguments);
|
|
|
|
}
|
2011-08-12 10:19:52 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-10-27 15:31:32 +00:00
|
|
|
openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp.web.Connection# */{
|
2011-08-11 02:42:46 +00:00
|
|
|
/**
|
2011-10-27 15:31:32 +00:00
|
|
|
* @constructs openerp.web.Connection
|
2011-09-12 10:35:03 +00:00
|
|
|
* @extends openerp.web.CallbackEnabled
|
|
|
|
*
|
|
|
|
* @param {String} [server] JSON-RPC endpoint hostname
|
|
|
|
* @param {String} [port] JSON-RPC endpoint port
|
2011-08-11 02:42:46 +00:00
|
|
|
*/
|
2011-11-15 17:04:08 +00:00
|
|
|
init: function() {
|
2011-08-12 10:19:52 +00:00
|
|
|
this._super();
|
2011-12-16 14:03:16 +00:00
|
|
|
this.server = null;
|
|
|
|
this.debug = ($.deparam($.param.querystring()).debug != undefined);
|
2011-12-16 00:44:02 +00:00
|
|
|
// TODO: session store in cookie should be optional
|
2011-11-22 12:20:16 +00:00
|
|
|
this.name = openerp._session_id;
|
2011-12-19 14:34:06 +00:00
|
|
|
this.qweb_mutex = new $.Mutex();
|
2011-11-15 17:04:08 +00:00
|
|
|
},
|
2011-12-16 15:05:41 +00:00
|
|
|
bind: function(origin) {
|
2012-02-06 13:41:55 +00:00
|
|
|
var window_origin = location.protocol+"//"+location.host, self=this;
|
2011-12-16 15:05:41 +00:00
|
|
|
this.origin = origin ? _.str.rtrim(origin,'/') : window_origin;
|
|
|
|
this.prefix = this.origin;
|
|
|
|
this.server = this.origin; // keep chs happy
|
|
|
|
openerp.web.qweb.default_dict['_s'] = this.origin;
|
|
|
|
this.rpc_function = (this.origin == window_origin) ? this.rpc_json : this.rpc_jsonp;
|
2011-08-11 02:42:46 +00:00
|
|
|
this.session_id = false;
|
2011-09-13 10:29:36 +00:00
|
|
|
this.uid = false;
|
2011-11-18 11:15:21 +00:00
|
|
|
this.username = false;
|
2011-09-13 10:29:36 +00:00
|
|
|
this.user_context= {};
|
|
|
|
this.db = false;
|
2011-12-27 15:59:15 +00:00
|
|
|
this.openerp_entreprise = false;
|
2012-02-06 13:41:55 +00:00
|
|
|
this.module_list = openerp._modules.slice();
|
|
|
|
this.module_loaded = {};
|
|
|
|
_(this.module_list).each(function (mod) {
|
|
|
|
self.module_loaded[mod] = true;
|
|
|
|
});
|
2011-08-11 02:42:46 +00:00
|
|
|
this.context = {};
|
2011-08-16 14:25:15 +00:00
|
|
|
this.shortcuts = [];
|
|
|
|
this.active_id = null;
|
2011-12-16 00:48:23 +00:00
|
|
|
return this.session_init();
|
2011-08-11 02:42:46 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Executes an RPC call, registering the provided callbacks.
|
|
|
|
*
|
|
|
|
* Registers a default error callback if none is provided, and handles
|
|
|
|
* setting the correct session id and session context in the parameter
|
|
|
|
* objects
|
|
|
|
*
|
|
|
|
* @param {String} url RPC endpoint
|
|
|
|
* @param {Object} params call parameters
|
|
|
|
* @param {Function} success_callback function to execute on RPC call success
|
|
|
|
* @param {Function} error_callback function to execute on RPC call failure
|
|
|
|
* @returns {jQuery.Deferred} jquery-provided ajax deferred
|
|
|
|
*/
|
|
|
|
rpc: function(url, params, success_callback, error_callback) {
|
|
|
|
var self = this;
|
2011-12-15 18:32:59 +00:00
|
|
|
// url can be an $.ajax option object
|
|
|
|
if (_.isString(url)) {
|
|
|
|
url = { url: url };
|
|
|
|
}
|
2011-08-11 02:42:46 +00:00
|
|
|
// Construct a JSON-RPC2 request, method is currently unused
|
|
|
|
params.session_id = this.session_id;
|
2011-10-17 14:54:30 +00:00
|
|
|
if (this.debug)
|
|
|
|
params.debug = 1;
|
2011-12-15 18:32:59 +00:00
|
|
|
var payload = {
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
method: 'call',
|
|
|
|
params: params,
|
2011-12-16 01:49:45 +00:00
|
|
|
id: _.uniqueId('r')
|
2011-12-15 18:32:59 +00:00
|
|
|
};
|
2011-08-11 02:42:46 +00:00
|
|
|
var deferred = $.Deferred();
|
2011-12-15 18:32:59 +00:00
|
|
|
this.on_rpc_request();
|
2012-01-24 14:19:27 +00:00
|
|
|
var aborter = params.aborter;
|
|
|
|
delete params.aborter;
|
|
|
|
var request = this.rpc_function(url, payload).then(
|
2011-12-15 18:32:59 +00:00
|
|
|
function (response, textStatus, jqXHR) {
|
|
|
|
self.on_rpc_response();
|
|
|
|
if (!response.error) {
|
|
|
|
deferred.resolve(response["result"], textStatus, jqXHR);
|
|
|
|
} else if (response.error.data.type === "session_invalid") {
|
|
|
|
self.uid = false;
|
2011-12-16 00:48:23 +00:00
|
|
|
// TODO deprecate or use a deferred on login.do_ask_login()
|
2011-12-15 18:32:59 +00:00
|
|
|
self.on_session_invalid(function() {
|
|
|
|
self.rpc(url, payload.params,
|
|
|
|
function() { deferred.resolve.apply(deferred, arguments); },
|
|
|
|
function() { deferred.reject.apply(deferred, arguments); });
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
deferred.reject(response.error, $.Event());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function(jqXHR, textStatus, errorThrown) {
|
|
|
|
self.on_rpc_response();
|
|
|
|
var error = {
|
|
|
|
code: -32098,
|
|
|
|
message: "XmlHttpRequestError " + errorThrown,
|
|
|
|
data: {type: "xhr"+textStatus, debug: jqXHR.responseText, objects: [jqXHR, errorThrown] }
|
|
|
|
};
|
|
|
|
deferred.reject(error, $.Event());
|
|
|
|
});
|
2012-01-24 14:19:27 +00:00
|
|
|
if (aborter) {
|
|
|
|
aborter.abort_last = function () {
|
|
|
|
if (!(request.isResolved() || request.isRejected())) {
|
|
|
|
deferred.fail(function (error, event) {
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
request.abort();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2011-12-15 18:32:59 +00:00
|
|
|
// Allow deferred user to disable on_rpc_error in fail
|
|
|
|
deferred.fail(function() {
|
2011-08-11 02:42:46 +00:00
|
|
|
deferred.fail(function(error, event) {
|
|
|
|
if (!event.isDefaultPrevented()) {
|
|
|
|
self.on_rpc_error(error, event);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).then(success_callback, error_callback).promise();
|
2011-12-15 18:32:59 +00:00
|
|
|
return deferred;
|
2011-08-11 02:42:46 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Raw JSON-RPC call
|
|
|
|
*
|
2011-09-05 11:03:09 +00:00
|
|
|
* @returns {jQuery.Deferred} ajax-webd deferred object
|
2011-08-11 02:42:46 +00:00
|
|
|
*/
|
2011-12-15 18:32:59 +00:00
|
|
|
rpc_json: function(url, payload) {
|
2011-08-11 02:42:46 +00:00
|
|
|
var self = this;
|
|
|
|
var ajax = _.extend({
|
|
|
|
type: "POST",
|
2011-12-15 18:32:59 +00:00
|
|
|
dataType: 'json',
|
2011-08-11 02:42:46 +00:00
|
|
|
contentType: 'application/json',
|
2011-12-15 18:32:59 +00:00
|
|
|
data: JSON.stringify(payload),
|
2012-01-17 13:28:43 +00:00
|
|
|
processData: false
|
2011-08-11 02:42:46 +00:00
|
|
|
}, url);
|
2011-12-19 16:14:41 +00:00
|
|
|
if (this.synch)
|
|
|
|
ajax.async = false;
|
2011-12-15 18:32:59 +00:00
|
|
|
return $.ajax(ajax);
|
|
|
|
},
|
|
|
|
rpc_jsonp: function(url, payload) {
|
|
|
|
var self = this;
|
2011-12-16 01:49:45 +00:00
|
|
|
// extracted from payload to set on the url
|
|
|
|
var data = {
|
|
|
|
session_id: this.session_id,
|
2012-01-17 13:28:43 +00:00
|
|
|
id: payload.id
|
2011-12-16 01:49:45 +00:00
|
|
|
};
|
|
|
|
url.url = this.get_url(url.url);
|
2011-12-15 18:32:59 +00:00
|
|
|
var ajax = _.extend({
|
|
|
|
type: "GET",
|
|
|
|
dataType: 'jsonp',
|
|
|
|
jsonp: 'jsonp',
|
|
|
|
cache: false,
|
2011-12-16 01:49:45 +00:00
|
|
|
data: data
|
2011-12-15 18:32:59 +00:00
|
|
|
}, url);
|
2011-12-19 16:14:41 +00:00
|
|
|
if (this.synch)
|
|
|
|
ajax.async = false;
|
2011-12-15 18:32:59 +00:00
|
|
|
var payload_str = JSON.stringify(payload);
|
|
|
|
var payload_url = $.param({r:payload_str});
|
2011-12-16 16:47:39 +00:00
|
|
|
if(payload_url.length < 2000) {
|
2011-12-16 01:49:45 +00:00
|
|
|
// Direct jsonp request
|
2011-12-15 18:32:59 +00:00
|
|
|
ajax.data.r = payload_str;
|
|
|
|
return $.ajax(ajax);
|
|
|
|
} else {
|
2011-12-16 01:49:45 +00:00
|
|
|
// Indirect jsonp request
|
|
|
|
var ifid = _.uniqueId('oe_rpc_iframe');
|
2011-12-15 18:32:59 +00:00
|
|
|
var display = options.openerp.debug ? 'block' : 'none';
|
|
|
|
var $iframe = $(_.str.sprintf("<iframe src='javascript:false;' name='%s' id='%s' style='display:%s'></iframe>", ifid, ifid, display));
|
|
|
|
var $form = $('<form>')
|
|
|
|
.attr('method', 'POST')
|
|
|
|
.attr('target', ifid)
|
|
|
|
.attr('enctype', "multipart/form-data")
|
2011-12-16 01:49:45 +00:00
|
|
|
.attr('action', ajax.url + '?' + $.param(data))
|
2011-12-15 18:32:59 +00:00
|
|
|
.append($('<input type="hidden" name="r" />').attr('value', payload_str))
|
|
|
|
.hide()
|
2012-01-04 15:53:29 +00:00
|
|
|
.appendTo($('body'));
|
2011-12-15 18:32:59 +00:00
|
|
|
var cleanUp = function() {
|
|
|
|
if ($iframe) {
|
|
|
|
$iframe.unbind("load").attr("src", "javascript:false;").remove();
|
|
|
|
}
|
|
|
|
$form.remove();
|
2011-08-11 02:42:46 +00:00
|
|
|
};
|
2011-12-15 18:32:59 +00:00
|
|
|
var deferred = $.Deferred();
|
|
|
|
// the first bind is fired up when the iframe is added to the DOM
|
|
|
|
$iframe.bind('load', function() {
|
|
|
|
// the second bind is fired up when the result of the form submission is received
|
|
|
|
$iframe.unbind('load').bind('load', function() {
|
|
|
|
$.ajax(ajax).always(function() {
|
|
|
|
cleanUp();
|
|
|
|
}).then(
|
|
|
|
function() { deferred.resolve.apply(deferred, arguments); },
|
|
|
|
function() { deferred.reject.apply(deferred, arguments); }
|
|
|
|
);
|
|
|
|
});
|
|
|
|
// now that the iframe can receive data, we fill and submit the form
|
|
|
|
$form.submit();
|
|
|
|
});
|
|
|
|
// append the iframe to the DOM (will trigger the first load)
|
|
|
|
$form.after($iframe);
|
2011-12-16 01:49:45 +00:00
|
|
|
return deferred;
|
2011-12-15 18:32:59 +00:00
|
|
|
}
|
2011-08-11 02:42:46 +00:00
|
|
|
},
|
|
|
|
on_rpc_request: function() {
|
|
|
|
},
|
|
|
|
on_rpc_response: function() {
|
|
|
|
},
|
|
|
|
on_rpc_error: function(error) {
|
|
|
|
},
|
|
|
|
/**
|
2011-12-16 00:44:02 +00:00
|
|
|
* Init a session, reloads from cookie, if it exists
|
2011-08-11 02:42:46 +00:00
|
|
|
*/
|
2011-12-16 00:48:23 +00:00
|
|
|
session_init: function () {
|
2011-08-11 02:42:46 +00:00
|
|
|
var self = this;
|
2011-12-16 00:44:02 +00:00
|
|
|
// TODO: session store in cookie should be optional
|
|
|
|
this.session_id = this.get_cookie('session_id');
|
2012-01-11 14:31:44 +00:00
|
|
|
return this.session_reload().pipe(function(result) {
|
|
|
|
var modules = openerp._modules.join(',');
|
|
|
|
var deferred = self.rpc('/web/webclient/qweblist', {mods: modules}).pipe(self.do_load_qweb);
|
|
|
|
if(self.session_is_valid()) {
|
|
|
|
return deferred.pipe(function() { return self.load_modules(); });
|
|
|
|
}
|
|
|
|
return deferred;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* (re)loads the content of a session: db name, username, user id, session
|
|
|
|
* context and status of the support contract
|
|
|
|
*
|
|
|
|
* @returns {$.Deferred} deferred indicating the session is done reloading
|
|
|
|
*/
|
|
|
|
session_reload: function () {
|
|
|
|
var self = this;
|
|
|
|
return this.rpc("/web/session/get_session_info", {}).then(function(result) {
|
2011-12-16 00:44:02 +00:00
|
|
|
// If immediately follows a login (triggered by trying to restore
|
|
|
|
// an invalid session or no session at all), refresh session data
|
|
|
|
// (should not change, but just in case...)
|
2011-11-18 11:15:21 +00:00
|
|
|
_.extend(self, {
|
|
|
|
db: result.db,
|
2011-12-16 00:44:02 +00:00
|
|
|
username: result.login,
|
|
|
|
uid: result.uid,
|
2011-12-21 17:11:37 +00:00
|
|
|
user_context: result.context,
|
2011-12-27 15:59:15 +00:00
|
|
|
openerp_entreprise: result.openerp_entreprise
|
2011-11-18 11:15:21 +00:00
|
|
|
});
|
2011-11-15 17:04:08 +00:00
|
|
|
});
|
2011-08-11 02:42:46 +00:00
|
|
|
},
|
2011-12-16 15:05:41 +00:00
|
|
|
session_is_valid: function() {
|
2011-12-16 14:17:07 +00:00
|
|
|
return !!this.uid;
|
|
|
|
},
|
2011-08-18 19:53:49 +00:00
|
|
|
/**
|
2011-12-16 00:44:02 +00:00
|
|
|
* The session is validated either by login or by restoration of a previous session
|
2011-08-18 19:53:49 +00:00
|
|
|
*/
|
2012-01-03 16:18:31 +00:00
|
|
|
session_authenticate: function(db, login, password, _volatile) {
|
2011-09-13 10:23:20 +00:00
|
|
|
var self = this;
|
2011-12-16 00:44:02 +00:00
|
|
|
var base_location = document.location.protocol + '//' + document.location.host;
|
|
|
|
var params = { db: db, login: login, password: password, base_location: base_location };
|
|
|
|
return this.rpc("/web/session/authenticate", params).pipe(function(result) {
|
2011-11-18 11:15:21 +00:00
|
|
|
_.extend(self, {
|
2011-12-16 00:44:02 +00:00
|
|
|
session_id: result.session_id,
|
2011-11-18 11:15:21 +00:00
|
|
|
db: result.db,
|
2011-12-16 00:44:02 +00:00
|
|
|
username: result.login,
|
|
|
|
uid: result.uid,
|
2011-12-21 17:11:37 +00:00
|
|
|
user_context: result.context,
|
2011-12-27 15:59:15 +00:00
|
|
|
openerp_entreprise: result.openerp_entreprise
|
2011-11-18 11:15:21 +00:00
|
|
|
});
|
2012-01-03 16:18:31 +00:00
|
|
|
if (!_volatile) {
|
2011-12-21 10:40:31 +00:00
|
|
|
self.set_cookie('session_id', self.session_id);
|
|
|
|
}
|
2011-12-16 00:44:02 +00:00
|
|
|
return self.load_modules();
|
2011-09-13 10:23:20 +00:00
|
|
|
});
|
2011-08-18 19:53:49 +00:00
|
|
|
},
|
2011-12-16 00:44:02 +00:00
|
|
|
session_logout: function() {
|
2011-08-18 19:53:49 +00:00
|
|
|
this.set_cookie('session_id', '');
|
|
|
|
},
|
2012-01-04 18:21:12 +00:00
|
|
|
on_session_valid: function() {
|
|
|
|
},
|
2011-12-16 15:05:41 +00:00
|
|
|
/**
|
|
|
|
* Called when a rpc call fail due to an invalid session.
|
|
|
|
* By default, it's a noop
|
|
|
|
*/
|
|
|
|
on_session_invalid: function(retry_callback) {
|
|
|
|
},
|
2011-08-18 19:53:49 +00:00
|
|
|
/**
|
|
|
|
* Fetches a cookie stored by an openerp session
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param name the cookie's name
|
|
|
|
*/
|
|
|
|
get_cookie: function (name) {
|
2011-10-20 15:05:40 +00:00
|
|
|
if (!this.name) { return null; }
|
|
|
|
var nameEQ = this.name + '|' + name + '=';
|
2011-08-18 19:53:49 +00:00
|
|
|
var cookies = document.cookie.split(';');
|
|
|
|
for(var i=0; i<cookies.length; ++i) {
|
|
|
|
var cookie = cookies[i].replace(/^\s*/, '');
|
|
|
|
if(cookie.indexOf(nameEQ) === 0) {
|
|
|
|
return JSON.parse(decodeURIComponent(cookie.substring(nameEQ.length)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Create a new cookie with the provided name and value
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param name the cookie's name
|
|
|
|
* @param value the cookie's value
|
|
|
|
* @param ttl the cookie's time to live, 1 year by default, set to -1 to delete
|
|
|
|
*/
|
|
|
|
set_cookie: function (name, value, ttl) {
|
2011-10-20 15:05:40 +00:00
|
|
|
if (!this.name) { return; }
|
2011-08-18 19:53:49 +00:00
|
|
|
ttl = ttl || 24*60*60*365;
|
|
|
|
document.cookie = [
|
2011-10-20 15:05:40 +00:00
|
|
|
this.name + '|' + name + '=' + encodeURIComponent(JSON.stringify(value)),
|
|
|
|
'path=/',
|
2011-08-18 19:53:49 +00:00
|
|
|
'max-age=' + ttl,
|
|
|
|
'expires=' + new Date(new Date().getTime() + ttl*1000).toGMTString()
|
|
|
|
].join(';');
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Load additional web addons of that instance and init them
|
2012-01-13 15:08:29 +00:00
|
|
|
*
|
|
|
|
* @param {Boolean} [no_session_valid_signal=false] prevents load_module from triggering ``on_session_valid``.
|
2011-08-18 19:53:49 +00:00
|
|
|
*/
|
2012-01-13 15:08:29 +00:00
|
|
|
load_modules: function(no_session_valid_signal) {
|
2011-08-18 19:53:49 +00:00
|
|
|
var self = this;
|
2011-12-21 10:41:49 +00:00
|
|
|
return this.rpc('/web/session/modules', {}).pipe(function(result) {
|
2012-02-06 13:41:55 +00:00
|
|
|
var lang = self.user_context.lang,
|
|
|
|
all_modules = _.uniq(self.module_list.concat(result));
|
|
|
|
var params = { mods: all_modules, lang: lang};
|
2012-01-13 15:08:29 +00:00
|
|
|
var to_load = _.difference(result, self.module_list).join(',');
|
2012-02-06 13:41:55 +00:00
|
|
|
self.module_list = all_modules;
|
2011-12-21 10:41:49 +00:00
|
|
|
return $.when(
|
2012-01-13 15:08:29 +00:00
|
|
|
self.rpc('/web/webclient/csslist', {mods: to_load}, self.do_load_css),
|
|
|
|
self.rpc('/web/webclient/qweblist', {mods: to_load}).pipe(self.do_load_qweb),
|
2011-12-21 10:41:49 +00:00
|
|
|
self.rpc('/web/webclient/translations', params).pipe(function(trans) {
|
|
|
|
openerp.web._t.database.set_bundle(trans);
|
|
|
|
var file_list = ["/web/static/lib/datejs/globalization/" + lang.replace("_", "-") + ".js"];
|
2012-01-13 15:08:29 +00:00
|
|
|
return self.rpc('/web/webclient/jslist', {mods: to_load}).pipe(function(files) {
|
2011-12-16 01:30:51 +00:00
|
|
|
return self.do_load_js(file_list.concat(files));
|
2011-12-19 15:52:29 +00:00
|
|
|
});
|
2011-12-21 10:41:49 +00:00
|
|
|
})
|
|
|
|
).then(function() {
|
2012-01-12 11:21:38 +00:00
|
|
|
self.on_modules_loaded();
|
2012-01-13 15:08:29 +00:00
|
|
|
if (!no_session_valid_signal) {
|
|
|
|
self.on_session_valid();
|
|
|
|
}
|
2011-08-18 19:53:49 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2011-12-15 12:07:32 +00:00
|
|
|
do_load_css: function (files) {
|
2011-09-12 10:08:19 +00:00
|
|
|
var self = this;
|
2011-08-18 19:53:49 +00:00
|
|
|
_.each(files, function (file) {
|
|
|
|
$('head').append($('<link>', {
|
2011-12-16 00:44:02 +00:00
|
|
|
'href': self.get_url(file),
|
2011-08-18 19:53:49 +00:00
|
|
|
'rel': 'stylesheet',
|
|
|
|
'type': 'text/css'
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
},
|
2011-12-15 12:07:32 +00:00
|
|
|
do_load_js: function(files) {
|
2011-08-18 19:53:49 +00:00
|
|
|
var self = this;
|
2011-12-16 00:44:02 +00:00
|
|
|
var d = $.Deferred();
|
2011-08-18 19:53:49 +00:00
|
|
|
if(files.length != 0) {
|
|
|
|
var file = files.shift();
|
|
|
|
var tag = document.createElement('script');
|
|
|
|
tag.type = 'text/javascript';
|
2011-12-16 00:44:02 +00:00
|
|
|
tag.src = self.get_url(file);
|
2011-08-18 19:53:49 +00:00
|
|
|
tag.onload = tag.onreadystatechange = function() {
|
|
|
|
if ( (tag.readyState && tag.readyState != "loaded" && tag.readyState != "complete") || tag.onload_done )
|
|
|
|
return;
|
|
|
|
tag.onload_done = true;
|
2011-12-16 00:44:02 +00:00
|
|
|
self.do_load_js(files).then(function () {
|
|
|
|
d.resolve();
|
|
|
|
});
|
2011-08-18 19:53:49 +00:00
|
|
|
};
|
2011-10-18 12:26:07 +00:00
|
|
|
var head = document.head || document.getElementsByTagName('head')[0];
|
|
|
|
head.appendChild(tag);
|
2011-08-18 19:53:49 +00:00
|
|
|
} else {
|
2011-12-16 00:44:02 +00:00
|
|
|
d.resolve();
|
2011-08-18 19:53:49 +00:00
|
|
|
}
|
2011-12-16 00:44:02 +00:00
|
|
|
return d;
|
2011-08-18 19:53:49 +00:00
|
|
|
},
|
2011-12-15 12:07:32 +00:00
|
|
|
do_load_qweb: function(files) {
|
2011-11-02 11:11:05 +00:00
|
|
|
var self = this;
|
2011-12-19 14:34:06 +00:00
|
|
|
_.each(files, function(file) {
|
|
|
|
self.qweb_mutex.exec(function() {
|
|
|
|
return self.rpc('/web/proxy/load', {path: file}).pipe(function(xml) {
|
2012-01-13 15:06:41 +00:00
|
|
|
if (!xml) { return; }
|
2011-12-19 14:34:06 +00:00
|
|
|
openerp.web.qweb.add_template(_.str.trim(xml));
|
|
|
|
});
|
2011-11-09 08:16:26 +00:00
|
|
|
});
|
2011-12-19 14:34:06 +00:00
|
|
|
});
|
|
|
|
return self.qweb_mutex.def;
|
2011-11-02 11:11:05 +00:00
|
|
|
},
|
2011-08-18 19:53:49 +00:00
|
|
|
on_modules_loaded: function() {
|
|
|
|
for(var j=0; j<this.module_list.length; j++) {
|
|
|
|
var mod = this.module_list[j];
|
|
|
|
if(this.module_loaded[mod])
|
|
|
|
continue;
|
|
|
|
openerp[mod] = {};
|
|
|
|
// init module mod
|
|
|
|
if(openerp._openerp[mod] != undefined) {
|
|
|
|
openerp._openerp[mod](openerp);
|
|
|
|
this.module_loaded[mod] = true;
|
|
|
|
}
|
|
|
|
}
|
2011-08-30 08:53:51 +00:00
|
|
|
},
|
2011-12-16 01:49:45 +00:00
|
|
|
get_url: function (file) {
|
|
|
|
return this.prefix + file;
|
|
|
|
},
|
2011-08-30 09:05:14 +00:00
|
|
|
/**
|
|
|
|
* Cooperative file download implementation, for ajaxy APIs.
|
|
|
|
*
|
|
|
|
* Requires that the server side implements an httprequest correctly
|
|
|
|
* setting the `fileToken` cookie to the value provided as the `token`
|
|
|
|
* parameter. The cookie *must* be set on the `/` path and *must not* be
|
|
|
|
* `httpOnly`.
|
|
|
|
*
|
|
|
|
* It would probably also be a good idea for the response to use a
|
|
|
|
* `Content-Disposition: attachment` header, especially if the MIME is a
|
|
|
|
* "known" type (e.g. text/plain, or for some browsers application/json
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
2011-08-30 11:02:04 +00:00
|
|
|
* @param {String} [options.url] used to dynamically create a form
|
|
|
|
* @param {Object} [options.data] data to add to the form submission. If can be used without a form, in which case a form is created from scratch. Otherwise, added to form data
|
|
|
|
* @param {HTMLFormElement} [options.form] the form to submit in order to fetch the file
|
2011-08-30 09:05:14 +00:00
|
|
|
* @param {Function} [options.success] callback in case of download success
|
|
|
|
* @param {Function} [options.error] callback in case of request error, provided with the error body
|
|
|
|
* @param {Function} [options.complete] called after both ``success`` and ``error` callbacks have executed
|
|
|
|
*/
|
2011-08-30 09:06:09 +00:00
|
|
|
get_file: function (options) {
|
2011-08-30 08:53:51 +00:00
|
|
|
// need to detect when the file is done downloading (not used
|
|
|
|
// yet, but we'll need it to fix the UI e.g. with a throbber
|
|
|
|
// while dump is being generated), iframe load event only fires
|
|
|
|
// when the iframe content loads, so we need to go smarter:
|
|
|
|
// http://geekswithblogs.net/GruffCode/archive/2010/10/28/detecting-the-file-download-dialog-in-the-browser.aspx
|
|
|
|
var timer, token = new Date().getTime(),
|
|
|
|
cookie_name = 'fileToken', cookie_length = cookie_name.length,
|
2011-08-30 11:02:04 +00:00
|
|
|
CHECK_INTERVAL = 1000, id = _.uniqueId('get_file_frame'),
|
|
|
|
remove_form = false;
|
|
|
|
|
|
|
|
var $form, $form_data = $('<div>');
|
|
|
|
|
2011-08-30 09:05:14 +00:00
|
|
|
var complete = function () {
|
|
|
|
if (options.complete) { options.complete(); }
|
|
|
|
clearTimeout(timer);
|
2011-08-30 11:02:04 +00:00
|
|
|
$form_data.remove();
|
2011-08-30 09:05:14 +00:00
|
|
|
$target.remove();
|
2011-08-30 11:02:04 +00:00
|
|
|
if (remove_form && $form) { $form.remove(); }
|
2011-08-30 09:05:14 +00:00
|
|
|
};
|
2011-08-30 08:53:51 +00:00
|
|
|
var $target = $('<iframe style="display: none;">')
|
|
|
|
.attr({id: id, name: id})
|
|
|
|
.appendTo(document.body)
|
|
|
|
.load(function () {
|
2012-01-19 14:48:37 +00:00
|
|
|
try {
|
|
|
|
if (options.error) {
|
|
|
|
options.error(JSON.parse(
|
|
|
|
this.contentDocument.body.childNodes[1].textContent
|
|
|
|
));
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
complete();
|
|
|
|
}
|
2011-08-30 08:53:51 +00:00
|
|
|
});
|
2011-08-30 11:02:04 +00:00
|
|
|
|
|
|
|
if (options.form) {
|
|
|
|
$form = $(options.form);
|
|
|
|
} else {
|
|
|
|
remove_form = true;
|
|
|
|
$form = $('<form>', {
|
|
|
|
action: options.url,
|
|
|
|
method: 'POST'
|
|
|
|
}).appendTo(document.body);
|
|
|
|
}
|
|
|
|
|
|
|
|
_(_.extend({}, options.data || {},
|
|
|
|
{session_id: this.session_id, token: token}))
|
|
|
|
.each(function (value, key) {
|
|
|
|
$('<input type="hidden" name="' + key + '">')
|
|
|
|
.val(value)
|
|
|
|
.appendTo($form_data);
|
|
|
|
});
|
|
|
|
|
|
|
|
$form
|
|
|
|
.append($form_data)
|
2011-08-30 08:53:51 +00:00
|
|
|
.attr('target', id)
|
|
|
|
.get(0).submit();
|
|
|
|
|
|
|
|
var waitLoop = function () {
|
|
|
|
var cookies = document.cookie.split(';');
|
|
|
|
// setup next check
|
|
|
|
timer = setTimeout(waitLoop, CHECK_INTERVAL);
|
|
|
|
for (var i=0; i<cookies.length; ++i) {
|
|
|
|
var cookie = cookies[i].replace(/^\s*/, '');
|
|
|
|
if (!cookie.indexOf(cookie_name === 0)) { continue; }
|
|
|
|
var cookie_val = cookie.substring(cookie_length + 1);
|
|
|
|
if (parseInt(cookie_val, 10) !== token) { continue; }
|
|
|
|
|
|
|
|
// clear cookie
|
2011-11-15 12:30:59 +00:00
|
|
|
document.cookie = _.str.sprintf("%s=;expires=%s;path=/",
|
2011-08-30 08:53:51 +00:00
|
|
|
cookie_name, new Date().toGMTString());
|
|
|
|
if (options.success) { options.success(); }
|
|
|
|
complete();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
timer = setTimeout(waitLoop, CHECK_INTERVAL);
|
2011-12-19 16:14:41 +00:00
|
|
|
},
|
|
|
|
synchronized_mode: function(to_execute) {
|
|
|
|
var synch = this.synch;
|
|
|
|
this.synch = true;
|
|
|
|
try {
|
|
|
|
return to_execute();
|
|
|
|
} finally {
|
|
|
|
this.synch = synch;
|
|
|
|
}
|
2012-01-17 13:28:43 +00:00
|
|
|
}
|
2011-08-18 19:53:49 +00:00
|
|
|
});
|
|
|
|
|
2011-09-16 12:00:53 +00:00
|
|
|
/**
|
|
|
|
* Base class for all visual components. Provides a lot of functionalities helpful
|
|
|
|
* for the management of a part of the DOM.
|
|
|
|
*
|
|
|
|
* Widget handles:
|
|
|
|
* - Rendering with QWeb.
|
|
|
|
* - Life-cycle management and parenting (when a parent is destroyed, all its children are
|
|
|
|
* destroyed too).
|
|
|
|
* - Insertion in DOM.
|
|
|
|
*
|
|
|
|
* Guide to create implementations of the Widget class:
|
|
|
|
* ==============================================
|
|
|
|
*
|
|
|
|
* Here is a sample child class:
|
|
|
|
*
|
|
|
|
* MyWidget = openerp.base.Widget.extend({
|
|
|
|
* // the name of the QWeb template to use for rendering
|
|
|
|
* template: "MyQWebTemplate",
|
|
|
|
*
|
|
|
|
* init: function(parent) {
|
|
|
|
* this._super(parent);
|
|
|
|
* // stuff that you want to init before the rendering
|
|
|
|
* },
|
|
|
|
* start: function() {
|
|
|
|
* // stuff you want to make after the rendering, `this.$element` holds a correct value
|
|
|
|
* this.$element.find(".my_button").click(/* an example of event binding * /);
|
|
|
|
*
|
|
|
|
* // if you have some asynchronous operations, it's a good idea to return
|
|
|
|
* // a promise in start()
|
|
|
|
* var promise = this.rpc(...);
|
|
|
|
* return promise;
|
|
|
|
* }
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* Now this class can simply be used with the following syntax:
|
|
|
|
*
|
|
|
|
* var my_widget = new MyWidget(this);
|
|
|
|
* my_widget.appendTo($(".some-div"));
|
|
|
|
*
|
|
|
|
* With these two lines, the MyWidget instance was inited, rendered, it was inserted into the
|
|
|
|
* DOM inside the ".some-div" div and its events were binded.
|
|
|
|
*
|
|
|
|
* And of course, when you don't need that widget anymore, just do:
|
|
|
|
*
|
|
|
|
* my_widget.stop();
|
|
|
|
*
|
|
|
|
* That will kill the widget in a clean way and erase its content from the dom.
|
|
|
|
*/
|
2011-10-27 15:31:32 +00:00
|
|
|
openerp.web.Widget = openerp.web.CallbackEnabled.extend(/** @lends openerp.web.Widget# */{
|
2011-08-18 19:53:49 +00:00
|
|
|
/**
|
|
|
|
* The name of the QWeb template that will be used for rendering. Must be
|
|
|
|
* redefined in subclasses or the default render() method can not be used.
|
|
|
|
*
|
|
|
|
* @type string
|
|
|
|
*/
|
|
|
|
template: null,
|
2011-11-09 10:43:12 +00:00
|
|
|
/**
|
|
|
|
* Tag name when creating a default $element.
|
|
|
|
* @type string
|
|
|
|
*/
|
|
|
|
tag_name: 'div',
|
2011-08-18 19:53:49 +00:00
|
|
|
/**
|
2011-11-22 06:24:31 +00:00
|
|
|
* Constructs the widget and sets its parent if a parent is given.
|
2011-08-18 19:53:49 +00:00
|
|
|
*
|
2011-09-12 10:35:03 +00:00
|
|
|
* @constructs openerp.web.Widget
|
2011-10-27 15:31:32 +00:00
|
|
|
* @extends openerp.web.CallbackEnabled
|
2011-09-12 10:35:03 +00:00
|
|
|
*
|
2011-09-05 11:03:09 +00:00
|
|
|
* @param {openerp.web.Widget} parent Binds the current instance to the given Widget instance.
|
2011-08-18 19:53:49 +00:00
|
|
|
* When that widget is destroyed by calling stop(), the current instance will be
|
|
|
|
* destroyed too. Can be null.
|
|
|
|
* @param {String} element_id Deprecated. Sets the element_id. Only useful when you want
|
|
|
|
* to bind the current Widget to an already existing part of the DOM, which is not compatible
|
|
|
|
* with the DOM insertion methods provided by the current implementation of Widget. So
|
|
|
|
* for new components this argument should not be provided any more.
|
|
|
|
*/
|
2012-01-24 14:58:10 +00:00
|
|
|
init: function(parent) {
|
2011-10-27 15:31:32 +00:00
|
|
|
this._super();
|
2011-11-08 15:27:23 +00:00
|
|
|
this.session = openerp.connection;
|
2012-01-24 15:42:46 +00:00
|
|
|
|
|
|
|
this.$element = $(document.createElement(this.tag_name));
|
2011-08-18 19:53:49 +00:00
|
|
|
|
|
|
|
this.widget_parent = parent;
|
|
|
|
this.widget_children = [];
|
|
|
|
if(parent && parent.widget_children) {
|
|
|
|
parent.widget_children.push(this);
|
|
|
|
}
|
|
|
|
// useful to know if the widget was destroyed and should not be used anymore
|
|
|
|
this.widget_is_stopped = false;
|
|
|
|
},
|
|
|
|
/**
|
2011-11-22 06:24:31 +00:00
|
|
|
* Renders the current widget and appends it to the given jQuery object or Widget.
|
2011-08-18 19:53:49 +00:00
|
|
|
*
|
|
|
|
* @param target A jQuery object or a Widget instance.
|
|
|
|
*/
|
|
|
|
appendTo: function(target) {
|
|
|
|
var self = this;
|
|
|
|
return this._render_and_insert(function(t) {
|
|
|
|
self.$element.appendTo(t);
|
|
|
|
}, target);
|
|
|
|
},
|
|
|
|
/**
|
2011-11-22 06:24:31 +00:00
|
|
|
* Renders the current widget and prepends it to the given jQuery object or Widget.
|
2011-08-18 19:53:49 +00:00
|
|
|
*
|
|
|
|
* @param target A jQuery object or a Widget instance.
|
|
|
|
*/
|
|
|
|
prependTo: function(target) {
|
|
|
|
var self = this;
|
|
|
|
return this._render_and_insert(function(t) {
|
|
|
|
self.$element.prependTo(t);
|
|
|
|
}, target);
|
2011-08-11 02:42:46 +00:00
|
|
|
},
|
|
|
|
/**
|
2011-11-22 06:24:31 +00:00
|
|
|
* Renders the current widget and inserts it after to the given jQuery object or Widget.
|
2011-08-18 19:53:49 +00:00
|
|
|
*
|
|
|
|
* @param target A jQuery object or a Widget instance.
|
2011-08-11 02:42:46 +00:00
|
|
|
*/
|
2011-08-18 19:53:49 +00:00
|
|
|
insertAfter: function(target) {
|
|
|
|
var self = this;
|
|
|
|
return this._render_and_insert(function(t) {
|
|
|
|
self.$element.insertAfter(t);
|
|
|
|
}, target);
|
2011-08-11 02:42:46 +00:00
|
|
|
},
|
|
|
|
/**
|
2011-11-22 06:24:31 +00:00
|
|
|
* Renders the current widget and inserts it before to the given jQuery object or Widget.
|
2011-08-18 19:53:49 +00:00
|
|
|
*
|
|
|
|
* @param target A jQuery object or a Widget instance.
|
2011-08-11 02:42:46 +00:00
|
|
|
*/
|
2011-08-18 19:53:49 +00:00
|
|
|
insertBefore: function(target) {
|
|
|
|
var self = this;
|
|
|
|
return this._render_and_insert(function(t) {
|
|
|
|
self.$element.insertBefore(t);
|
|
|
|
}, target);
|
2011-08-11 02:42:46 +00:00
|
|
|
},
|
2011-11-22 06:24:31 +00:00
|
|
|
/**
|
|
|
|
* Renders the current widget and replaces the given jQuery object.
|
|
|
|
*
|
|
|
|
* @param target A jQuery object or a Widget instance.
|
|
|
|
*/
|
|
|
|
replace: function(target) {
|
|
|
|
return this._render_and_insert(_.bind(function(t) {
|
|
|
|
this.$element.replaceAll(t);
|
|
|
|
}, this), target);
|
|
|
|
},
|
2011-08-18 19:53:49 +00:00
|
|
|
_render_and_insert: function(insertion, target) {
|
2011-11-09 11:02:16 +00:00
|
|
|
this.render_element();
|
2011-09-05 11:03:09 +00:00
|
|
|
if (target instanceof openerp.web.Widget)
|
2011-08-18 19:53:49 +00:00
|
|
|
target = target.$element;
|
|
|
|
insertion(target);
|
2011-09-12 09:59:37 +00:00
|
|
|
this.on_inserted(this.$element, this);
|
2011-08-18 19:53:49 +00:00
|
|
|
return this.start();
|
2011-08-11 02:42:46 +00:00
|
|
|
},
|
2011-09-12 09:59:37 +00:00
|
|
|
on_inserted: function(element, widget) {},
|
2011-11-09 11:02:16 +00:00
|
|
|
/**
|
2012-02-07 12:34:25 +00:00
|
|
|
* Renders the element. The default implementation renders the widget using QWeb,
|
|
|
|
* `this.template` must be defined. The context given to QWeb contains the "widget"
|
|
|
|
* key that references `this`.
|
2011-11-09 11:02:16 +00:00
|
|
|
*/
|
|
|
|
render_element: function() {
|
2012-02-07 12:34:25 +00:00
|
|
|
var rendered = null;
|
|
|
|
if (this.template)
|
|
|
|
rendered = openerp.web.qweb.render(this.template, {widget: this});
|
2011-11-10 09:32:21 +00:00
|
|
|
if (rendered) {
|
|
|
|
var elem = $(rendered);
|
|
|
|
this.$element.replaceWith(elem);
|
|
|
|
this.$element = elem;
|
|
|
|
}
|
2011-08-11 02:42:46 +00:00
|
|
|
},
|
|
|
|
/**
|
2011-08-18 19:53:49 +00:00
|
|
|
* Method called after rendering. Mostly used to bind actions, perform asynchronous
|
|
|
|
* calls, etc...
|
2011-08-11 02:42:46 +00:00
|
|
|
*
|
2011-08-18 19:53:49 +00:00
|
|
|
* By convention, the method should return a promise to inform the caller when
|
|
|
|
* this widget has been initialized.
|
|
|
|
*
|
|
|
|
* @returns {jQuery.Deferred}
|
2011-08-11 02:42:46 +00:00
|
|
|
*/
|
2011-08-18 19:53:49 +00:00
|
|
|
start: function() {
|
|
|
|
return $.Deferred().done().promise();
|
2011-08-11 02:42:46 +00:00
|
|
|
},
|
|
|
|
/**
|
2011-11-22 06:24:31 +00:00
|
|
|
* Destroys the current widget, also destroys all its children before destroying itself.
|
2011-08-11 02:42:46 +00:00
|
|
|
*/
|
2011-08-18 19:53:49 +00:00
|
|
|
stop: function() {
|
|
|
|
_.each(_.clone(this.widget_children), function(el) {
|
|
|
|
el.stop();
|
2011-08-11 02:42:46 +00:00
|
|
|
});
|
2011-08-18 19:53:49 +00:00
|
|
|
if(this.$element != null) {
|
|
|
|
this.$element.remove();
|
|
|
|
}
|
|
|
|
if (this.widget_parent && this.widget_parent.widget_children) {
|
|
|
|
this.widget_parent.widget_children = _.without(this.widget_parent.widget_children, this);
|
|
|
|
}
|
|
|
|
this.widget_parent = null;
|
|
|
|
this.widget_is_stopped = true;
|
2011-08-11 02:42:46 +00:00
|
|
|
},
|
2011-08-18 19:53:49 +00:00
|
|
|
/**
|
2011-11-22 06:24:31 +00:00
|
|
|
* Informs the action manager to do an action. This supposes that
|
2011-08-18 19:53:49 +00:00
|
|
|
* the action manager can be found amongst the ancestors of the current widget.
|
|
|
|
* If that's not the case this method will simply return `false`.
|
|
|
|
*/
|
|
|
|
do_action: function(action, on_finished) {
|
|
|
|
if (this.widget_parent) {
|
|
|
|
return this.widget_parent.do_action(action, on_finished);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
do_notify: function() {
|
|
|
|
if (this.widget_parent) {
|
|
|
|
return this.widget_parent.do_notify.apply(this,arguments);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
do_warn: function() {
|
|
|
|
if (this.widget_parent) {
|
|
|
|
return this.widget_parent.do_warn.apply(this,arguments);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
2011-12-09 16:31:14 +00:00
|
|
|
|
2011-08-18 19:53:49 +00:00
|
|
|
rpc: function(url, data, success, error) {
|
|
|
|
var def = $.Deferred().then(success, error);
|
|
|
|
var self = this;
|
2011-11-08 15:27:23 +00:00
|
|
|
openerp.connection.rpc(url, data). then(function() {
|
2011-08-18 19:53:49 +00:00
|
|
|
if (!self.widget_is_stopped)
|
|
|
|
def.resolve.apply(def, arguments);
|
|
|
|
}, function() {
|
|
|
|
if (!self.widget_is_stopped)
|
|
|
|
def.reject.apply(def, arguments);
|
2011-08-11 02:42:46 +00:00
|
|
|
});
|
2011-08-18 19:53:49 +00:00
|
|
|
return def.promise();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-01-24 15:27:08 +00:00
|
|
|
/**
|
2012-01-25 08:42:44 +00:00
|
|
|
* @deprecated use :class:`openerp.web.Widget`
|
2012-01-24 15:27:08 +00:00
|
|
|
*/
|
2012-01-24 14:58:10 +00:00
|
|
|
openerp.web.OldWidget = openerp.web.Widget.extend({
|
2012-01-24 15:27:08 +00:00
|
|
|
init: function(parent, element_id) {
|
2012-01-24 14:58:10 +00:00
|
|
|
this._super(parent);
|
|
|
|
this.element_id = element_id;
|
2012-01-24 15:13:43 +00:00
|
|
|
this.element_id = this.element_id || _.uniqueId('widget-');
|
2012-01-24 14:58:10 +00:00
|
|
|
var tmp = document.getElementById(this.element_id);
|
|
|
|
this.$element = tmp ? $(tmp) : $(document.createElement(this.tag_name));
|
2012-02-07 12:34:25 +00:00
|
|
|
},
|
|
|
|
render_element: function() {
|
|
|
|
var rendered = this.render();
|
|
|
|
if (rendered) {
|
|
|
|
var elem = $(rendered);
|
|
|
|
this.$element.replaceWith(elem);
|
|
|
|
this.$element = elem;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
render: function (additional) {
|
|
|
|
if (this.template)
|
|
|
|
return openerp.web.qweb.render(this.template, _.extend({widget: this}, additional || {}));
|
|
|
|
return null;
|
2012-02-07 17:00:08 +00:00
|
|
|
}
|
2011-08-18 19:53:49 +00:00
|
|
|
});
|
|
|
|
|
2011-09-12 10:35:03 +00:00
|
|
|
openerp.web.TranslationDataBase = openerp.web.Class.extend(/** @lends openerp.web.TranslationDataBase# */{
|
|
|
|
/**
|
|
|
|
* @constructs openerp.web.TranslationDataBase
|
|
|
|
* @extends openerp.web.Class
|
|
|
|
*/
|
2011-08-18 19:53:49 +00:00
|
|
|
init: function() {
|
|
|
|
this.db = {};
|
|
|
|
this.parameters = {"direction": 'ltr',
|
|
|
|
"date_format": '%m/%d/%Y',
|
|
|
|
"time_format": '%H:%M:%S',
|
2011-11-14 15:45:10 +00:00
|
|
|
"grouping": [],
|
2011-08-18 19:53:49 +00:00
|
|
|
"decimal_point": ".",
|
|
|
|
"thousands_sep": ","};
|
2011-08-11 02:42:46 +00:00
|
|
|
},
|
2011-08-18 19:53:49 +00:00
|
|
|
set_bundle: function(translation_bundle) {
|
2011-08-11 02:42:46 +00:00
|
|
|
var self = this;
|
2011-08-18 19:53:49 +00:00
|
|
|
this.db = {};
|
2011-11-28 15:39:41 +00:00
|
|
|
var modules = _.keys(translation_bundle.modules);
|
|
|
|
modules.sort();
|
2011-09-05 11:03:09 +00:00
|
|
|
if (_.include(modules, "web")) {
|
|
|
|
modules = ["web"].concat(_.without(modules, "web"));
|
2011-08-18 19:53:49 +00:00
|
|
|
}
|
|
|
|
_.each(modules, function(name) {
|
|
|
|
self.add_module_translation(translation_bundle.modules[name]);
|
|
|
|
});
|
|
|
|
if (translation_bundle.lang_parameters) {
|
|
|
|
this.parameters = translation_bundle.lang_parameters;
|
2011-11-14 15:45:10 +00:00
|
|
|
this.parameters.grouping = py.eval(
|
|
|
|
this.parameters.grouping).toJSON();
|
2011-08-11 02:42:46 +00:00
|
|
|
}
|
|
|
|
},
|
2011-08-18 19:53:49 +00:00
|
|
|
add_module_translation: function(mod) {
|
|
|
|
var self = this;
|
|
|
|
_.each(mod.messages, function(message) {
|
|
|
|
if (self.db[message.id] === undefined) {
|
|
|
|
self.db[message.id] = message.string;
|
2011-08-11 02:42:46 +00:00
|
|
|
}
|
2011-08-18 19:53:49 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
build_translation_function: function() {
|
|
|
|
var self = this;
|
|
|
|
var fcnt = function(str) {
|
|
|
|
var tmp = self.get(str);
|
|
|
|
return tmp === undefined ? str : tmp;
|
2011-09-06 08:58:53 +00:00
|
|
|
};
|
2011-08-18 19:53:49 +00:00
|
|
|
fcnt.database = this;
|
|
|
|
return fcnt;
|
|
|
|
},
|
|
|
|
get: function(key) {
|
|
|
|
if (this.db[key])
|
|
|
|
return this.db[key];
|
|
|
|
return undefined;
|
2011-08-11 02:42:46 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-12-15 12:07:32 +00:00
|
|
|
/** Configure blockui */
|
2011-10-27 12:45:03 +00:00
|
|
|
if ($.blockUI) {
|
|
|
|
$.blockUI.defaults.baseZ = 1100;
|
|
|
|
$.blockUI.defaults.message = '<img src="/web/static/src/img/throbber2.gif">';
|
|
|
|
}
|
2011-11-08 15:27:23 +00:00
|
|
|
|
|
|
|
/** Configure default qweb */
|
2011-09-05 11:03:09 +00:00
|
|
|
openerp.web._t = new openerp.web.TranslationDataBase().build_translation_function();
|
2011-12-16 13:00:00 +00:00
|
|
|
/**
|
|
|
|
* Lazy translation function, only performs the translation when actually
|
|
|
|
* printed (e.g. inserted into a template)
|
|
|
|
*
|
|
|
|
* Useful when defining translatable strings in code evaluated before the
|
|
|
|
* translation database is loaded, as class attributes or at the top-level of
|
|
|
|
* an OpenERP Web module
|
|
|
|
*
|
|
|
|
* @param {String} s string to translate
|
|
|
|
* @returns {Object} lazy translation object
|
|
|
|
*/
|
|
|
|
openerp.web._lt = function (s) {
|
|
|
|
return {toString: function () { return openerp.web._t(s); }}
|
|
|
|
};
|
2011-10-20 09:16:19 +00:00
|
|
|
openerp.web.qweb = new QWeb2.Engine();
|
|
|
|
openerp.web.qweb.debug = (window.location.search.indexOf('?debug') !== -1);
|
2011-10-26 08:25:43 +00:00
|
|
|
openerp.web.qweb.default_dict = {
|
2011-11-24 21:06:00 +00:00
|
|
|
'_' : _,
|
|
|
|
'_t' : openerp.web._t
|
2011-12-16 13:00:00 +00:00
|
|
|
};
|
2012-02-07 13:29:01 +00:00
|
|
|
openerp.web.qweb.preprocess_node = function() {
|
|
|
|
// Note that 'this' is the Qweb Node
|
|
|
|
switch (this.node.nodeType) {
|
|
|
|
case 3:
|
|
|
|
case 4:
|
|
|
|
// Text and CDATAs
|
|
|
|
var translation = this.node.parentNode.attributes['t-translation'];
|
|
|
|
if (translation && translation.value === 'off') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var ts = _.str.trim(this.node.data);
|
|
|
|
if (ts.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var tr = openerp.web._t(ts);
|
|
|
|
if (tr !== ts) {
|
|
|
|
this.node.data = tr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// Element
|
|
|
|
var attr, attrs = ['label', 'title', 'alt'];
|
|
|
|
while (attr = attrs.pop()) {
|
|
|
|
if (this.attributes[attr]) {
|
|
|
|
this.attributes[attr] = openerp.web._t(this.attributes[attr]);
|
|
|
|
}
|
|
|
|
}
|
2011-10-20 09:16:19 +00:00
|
|
|
}
|
2012-01-25 08:53:10 +00:00
|
|
|
};
|
2011-08-18 19:53:49 +00:00
|
|
|
|
2011-12-15 12:07:32 +00:00
|
|
|
/** Jquery extentions */
|
2011-12-12 15:56:35 +00:00
|
|
|
$.Mutex = (function() {
|
|
|
|
function Mutex() {
|
|
|
|
this.def = $.Deferred().resolve();
|
2012-01-25 08:53:10 +00:00
|
|
|
}
|
2011-12-12 15:56:35 +00:00
|
|
|
Mutex.prototype.exec = function(action) {
|
|
|
|
var current = this.def;
|
|
|
|
var next = this.def = $.Deferred();
|
|
|
|
return current.pipe(function() {
|
|
|
|
return $.when(action()).always(function() {
|
|
|
|
next.resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
return Mutex;
|
|
|
|
})();
|
|
|
|
|
2011-12-19 14:34:06 +00:00
|
|
|
/** Setup default connection */
|
|
|
|
openerp.connection = new openerp.web.Connection();
|
|
|
|
openerp.web.qweb.default_dict['__debug__'] = openerp.connection.debug;
|
|
|
|
|
|
|
|
|
2011-12-12 15:56:35 +00:00
|
|
|
$.async_when = function() {
|
|
|
|
var async = false;
|
|
|
|
var def = $.Deferred();
|
|
|
|
$.when.apply($, arguments).then(function() {
|
|
|
|
var args = arguments;
|
|
|
|
var action = function() {
|
|
|
|
def.resolve.apply(def, args);
|
|
|
|
};
|
|
|
|
if (async)
|
|
|
|
action();
|
|
|
|
else
|
|
|
|
setTimeout(action, 0);
|
|
|
|
}, function() {
|
|
|
|
var args = arguments;
|
|
|
|
var action = function() {
|
|
|
|
def.reject.apply(def, args);
|
|
|
|
};
|
|
|
|
if (async)
|
|
|
|
action();
|
|
|
|
else
|
|
|
|
setTimeout(action, 0);
|
|
|
|
});
|
|
|
|
async = true;
|
|
|
|
return def;
|
|
|
|
};
|
|
|
|
|
2011-12-19 16:14:41 +00:00
|
|
|
// special tweak for the web client
|
|
|
|
var old_async_when = $.async_when;
|
|
|
|
$.async_when = function() {
|
|
|
|
if (openerp.connection.synch)
|
|
|
|
return $.when.apply(this, arguments);
|
|
|
|
else
|
|
|
|
return old_async_when.apply(this, arguments);
|
|
|
|
};
|
|
|
|
|
2011-08-02 07:36:54 +00:00
|
|
|
};
|
2011-08-11 03:10:00 +00:00
|
|
|
|
2011-07-15 14:16:00 +00:00
|
|
|
// vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax:
|