/*--------------------------------------------------------- * OpenERP Web chrome *---------------------------------------------------------*/ openerp.web.chrome = function(instance) { var QWeb = instance.web.qweb, _t = instance.web._t; instance.web.Notification = instance.web.Widget.extend({ template: 'Notification', init: function() { this._super.apply(this, arguments); instance.web.notification = this; }, start: function() { this._super.apply(this, arguments); this.$element.notify({ speed: 500, expires: 2500 }); }, notify: function(title, text, sticky) { sticky = !!sticky; var opts = {}; if (sticky) { opts.expires = false; } this.$element.notify('create', { title: title, text: text }, opts); }, warn: function(title, text, sticky) { sticky = !!sticky; var opts = {}; if (sticky) { opts.expires = false; } this.$element.notify('create', 'oe_notification_alert', { title: title, text: text }, opts); } }); /** * The very minimal function everything should call to create a dialog * in OpenERP Web Client. */ instance.web.dialog = function(element) { var result = element.dialog.apply(element, _.rest(_.toArray(arguments))); result.dialog("widget").addClass("openerp"); return result; }; instance.web.Dialog = instance.web.Widget.extend({ dialog_title: "", init: function (parent, options, content) { var self = this; this._super(parent); if (content) { this.$element = content instanceof $ ? content : $(content); } this.dialog_options = { modal: true, destroy_on_close: true, width: 900, min_width: 0, max_width: '95%', height: 'auto', min_height: 0, max_height: this.get_height('100%') - 140, autoOpen: false, position: [false, 50], buttons: {}, beforeClose: function () { self.on_close(); }, resizeStop: this.on_resized }; for (var f in this) { if (f.substr(0, 10) == 'on_button_') { this.dialog_options.buttons[f.substr(10)] = this[f]; } } if (options) { _.extend(this.dialog_options, options); } if (this.dialog_options.autoOpen) { this.open(); } else { instance.web.dialog(this.$element, this.get_options()); } }, get_options: function(options) { var self = this, o = _.extend({}, this.dialog_options, options || {}); _.each(['width', 'height'], function(unit) { o[unit] = self['get_' + unit](o[unit]); o['min_' + unit] = self['get_' + unit](o['min_' + unit] || 0); o['max_' + unit] = self['get_' + unit](o['max_' + unit] || 0); if (o[unit] !== 'auto' && o['min_' + unit] && o[unit] < o['min_' + unit]) o[unit] = o['min_' + unit]; if (o[unit] !== 'auto' && o['max_' + unit] && o[unit] > o['max_' + unit]) o[unit] = o['max_' + unit]; }); if (!o.title && this.dialog_title) { o.title = this.dialog_title; } return o; }, get_width: function(val) { return this.get_size(val.toString(), $(window.top).width()); }, get_height: function(val) { return this.get_size(val.toString(), $(window.top).height()); }, get_size: function(val, available_size) { if (val === 'auto') { return val; } else if (val.slice(-1) == "%") { return Math.round(available_size / 100 * parseInt(val.slice(0, -1), 10)); } else { return parseInt(val, 10); } }, open: function(options) { // TODO fme: bind window on resize if (this.template) { this.$element.html(this.renderElement()); } var o = this.get_options(options); instance.web.dialog(this.$element, o).dialog('open'); if (o.height === 'auto' && o.max_height) { this.$element.css({ 'max-height': o.max_height, 'overflow-y': 'auto' }); } return this; }, close: function() { this.$element.dialog('close'); }, on_close: function() { if (this.dialog_options.destroy_on_close) { this.destroy(); } }, on_resized: function() { }, destroy: function () { _.each(this.getChildren(), function(el) { el.destroy(); }); if (! this.isDestroyed()) { this.$element.dialog('destroy'); } this._super(); } }); instance.web.CrashManager = instance.web.CallbackEnabled.extend({ on_rpc_error: function(error) { if (error.data.fault_code) { var split = ("" + error.data.fault_code).split('\n')[0].split(' -- '); if (split.length > 1) { error.type = split.shift(); error.data.fault_code = error.data.fault_code.substr(error.type.length + 4); } } if (error.code === 200 && error.type) { this.on_managed_error(error); } else { this.on_traceback(error); } }, on_managed_error: function(error) { instance.web.dialog($('
';
}
}
this.session.session_bind().then(function() {
self.destroy_content();
self.show_common();
if (!self.session.session_is_valid()) {
self.show_login();
} else {
self.show_application();
}
});
this.$element.on('mouseenter', '.oe_systray > div:not([data-tipsy=true])', function() {
$(this).attr('data-tipsy', 'true').tipsy().trigger('mouseenter');
});
this.$element.on('click', '.oe_dropdown_toggle', function(ev) {
ev.preventDefault();
var $toggle = $(this);
var $menu = $toggle.find('.oe_dropdown_menu');
var state = $menu.is('.oe_opened');
setTimeout(function() {
// Do not alter propagation
$toggle.add($menu).toggleClass('oe_opened', !state);
if (!state) {
// Move $menu if outside window's edge
var doc_width = $(document).width();
var offset = $menu.offset();
var menu_width = $menu.width();
var x = doc_width - offset.left - menu_width - 2;
if (x < 0) {
$menu.offset({ left: offset.left + x }).width(menu_width);
}
}
}, 0);
});
instance.web.bus.on('click', this, function() {
self.$element.find('.oe_dropdown_menu.oe_opened').removeClass('oe_opened');
});
},
show_common: function() {
var self = this;
this.crashmanager = new instance.web.CrashManager();
instance.connection.on_rpc_error.add(this.crashmanager.on_rpc_error);
window.onerror = function (message, file, line) {
self.crashmanager.on_traceback({
type: _t("Client Error"),
message: message,
data: {debug: file + ':' + line}
});
};
self.notification = new instance.web.Notification(this);
self.notification.appendTo(self.$element);
self.loading = new instance.web.Loading(self);
self.loading.appendTo(self.$element);
self.login = new instance.web.Login(self);
self.login.on("login",self,self.show_application);
self.$table = $(QWeb.render("WebClient", {}));
self.action_manager = new instance.web.ActionManager(self);
self.action_manager.appendTo(self.$table.find('.oe_application'));
},
show_login: function() {
var self = this;
self.login.appendTo(self.$element);
},
show_application: function() {
var self = this;
self.$element.append(self.$table);
self.login.$element.hide();
self.menu = new instance.web.Menu(self);
self.menu.replace(this.$element.find('.oe_menu_placeholder'));
self.menu.on('menu_click', this, this.on_menu_action);
self.user_menu = new instance.web.UserMenu(self);
self.user_menu.replace(this.$element.find('.oe_user_menu_placeholder'));
self.user_menu.on_menu_logout.add(this.proxy('on_logout'));
self.user_menu.on_action.add(this.proxy('on_menu_action'));
self.user_menu.do_update();
self.bind_hashchange();
if (!self.session.openerp_entreprise) {
var version_label = self._get_version_label();
self.$element.find('.oe_footer_powered').append(_.str.sprintf(' - %s', version_label));
}
self.set_title();
},
destroy_content: function() {
_.each(_.clone(this.getChildren()), function(el) {
el.destroy();
});
this.$element.children().remove();
},
do_reload: function() {
var self = this;
return this.session.session_reload().pipe(function () {
instance.connection.load_modules(true).pipe(
self.menu.proxy('do_reload')); });
},
do_notify: function() {
var n = this.notification;
n.notify.apply(n, arguments);
},
do_warn: function() {
var n = this.notification;
n.warn.apply(n, arguments);
},
on_logout: function() {
var self = this;
this.session.session_logout().then(function () {
$(window).unbind('hashchange', self.on_hashchange);
self.do_push_state({});
//would be cool to be able to do this, but I think it will make addons do strange things
//this.show_login();
window.location.reload();
});
},
bind_hashchange: function() {
var self = this;
$(window).bind('hashchange', this.on_hashchange);
var state = $.bbq.getState(true);
if (! _.isEmpty(state)) {
$(window).trigger('hashchange');
} else {
self.menu.has_been_loaded.then(function() {
var first_menu_id = self.menu.$element.find("a:first").data("menu");
if(first_menu_id) {
self.menu.menu_click(first_menu_id);
}
});
}
},
on_hashchange: function(event) {
var self = this;
var state = event.getState(true);
if (!_.isEqual(this._current_state, state)) {
if(state.action_id === undefined && state.menu_id) {
self.menu.has_been_loaded.then(function() {
self.menu.do_reload().then(function() {
self.menu.menu_click(state.menu_id)
});
});
} else {
this.action_manager.do_load_state(state, !!this._current_state);
}
}
this._current_state = state;
},
do_push_state: function(state) {
this.set_title(state.title);
var url = '#' + $.param(state);
this._current_state = _.clone(state);
$.bbq.pushState(url);
},
on_menu_action: function(options) {
var self = this;
this.rpc("/web/action/load", { action_id: options.action_id })
.then(function (result) {
var action = result.result;
if (options.needaction) {
action.context.search_default_needaction_pending = true;
}
self.action_manager.clear_breadcrumbs();
self.action_manager.do_action(action);
});
},
do_action: function(action) {
var self = this;
// TODO replace by client action menuclick
if(action.menu_id) {
this.do_reload().then(function () {
self.menu.menu_click(action.menu_id);
});
}
},
set_content_full_screen: function(fullscreen) {
if (fullscreen)
$(".oe_webclient", this.$element).addClass("oe_content_full_screen");
else
$(".oe_webclient", this.$element).removeClass("oe_content_full_screen");
}
});
instance.web.EmbeddedClient = instance.web.Widget.extend({
template: 'EmptyComponent',
init: function(parent, action_id, options) {
this._super(parent);
// TODO take the xmlid of a action instead of its id
this.action_id = action_id;
this.options = options || {};
this.am = new instance.web.ActionManager(this);
},
start: function() {
var self = this;
this.am.appendTo(this.$element.addClass('openerp'));
return this.rpc("/web/action/load", { action_id: this.action_id }, function(result) {
var action = result.result;
action.flags = _.extend({
//views_switcher : false,
search_view : false,
action_buttons : false,
sidebar : false
//pager : false
}, self.options, action.flags || {});
self.am.do_action(action);
});
}
});
instance.web.embed = function (origin, dbname, login, key, action, options) {
$('head').append($('', {
'rel': 'stylesheet',
'type': 'text/css',
'href': origin +'/web/webclient/css'
}));
var currentScript = document.currentScript;
if (!currentScript) {
var sc = document.getElementsByTagName('script');
currentScript = sc[sc.length-1];
}
instance.connection.session_bind(origin).then(function () {
instance.connection.session_authenticate(dbname, login, key, true).then(function () {
var client = new instance.web.EmbeddedClient(null, action, options);
client.insertAfter(currentScript);
});
});
};
};
// vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: