Files
tryton/src/tab.js
T

1103 lines
43 KiB
JavaScript
Raw Normal View History

2012-11-29 11:32:58 +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-11-29 11:32:58 +01:00
2013-01-04 21:29:48 +01:00
Sao.Tab = Sao.class_(Object, {
init: function() {
2013-11-25 12:18:32 +01:00
Sao.Tab.tabs.push(this);
2013-01-08 13:32:10 +01:00
this.buttons = {};
2017-08-30 18:19:04 +02:00
this.menu_buttons = {};
2015-02-06 00:32:51 +01:00
this.id = 'tab-' + Sao.Tab.counter++;
2017-02-25 18:38:58 +01:00
this.name = '';
this.name_el = jQuery('<span/>');
2018-05-12 00:39:59 +02:00
this.view_prm = jQuery.when();
2013-01-08 13:32:10 +01:00
},
2017-08-30 18:19:04 +02:00
menu_def: function() {
return [
{
id: 'switch_',
2018-08-19 20:01:49 +02:00
icon: 'tryton-switch',
2017-08-30 18:19:04 +02:00
label: Sao.i18n.gettext('Switch'),
tooltip: Sao.i18n.gettext('Switch view'),
}, {
id: 'previous',
2018-08-19 20:01:49 +02:00
icon: 'tryton-back',
2017-08-30 18:19:04 +02:00
label: Sao.i18n.gettext('Previous'),
tooltip: Sao.i18n.gettext('Previous Record')
}, {
id: 'next',
2018-08-19 20:01:49 +02:00
icon: 'tryton-forward',
2017-08-30 18:19:04 +02:00
label: Sao.i18n.gettext('Next'),
tooltip: Sao.i18n.gettext('Next Record'),
}, {
id: 'search',
2018-08-19 20:01:49 +02:00
icon: 'tryton-search',
2017-08-30 18:19:04 +02:00
label: Sao.i18n.gettext('Search'),
}, null, {
id: 'new_',
2018-08-19 20:01:49 +02:00
icon: 'tryton-create',
2017-08-30 18:19:04 +02:00
label: Sao.i18n.gettext('New'),
tooltip: Sao.i18n.gettext('Create a new record'),
}, {
id: 'save',
2018-08-19 20:01:49 +02:00
icon: 'tryton-save',
2017-08-30 18:19:04 +02:00
label: Sao.i18n.gettext('Save'),
tooltip: Sao.i18n.gettext('Save this record'),
}, {
id: 'reload',
2018-08-19 20:01:49 +02:00
icon: 'tryton-refresh',
2017-08-30 18:19:04 +02:00
label: Sao.i18n.gettext('Reload/Undo'),
tooltip: Sao.i18n.gettext('Reload'),
}, {
id: 'copy',
2018-08-19 20:01:49 +02:00
icon: 'tryton-copy',
2017-08-30 18:19:04 +02:00
label: Sao.i18n.gettext('Duplicate'),
}, {
id: 'delete_',
2018-08-19 20:01:49 +02:00
icon: 'tryton-delete',
2017-08-30 18:19:04 +02:00
label: Sao.i18n.gettext('Delete'),
}, null, {
id: 'logs',
2018-08-19 20:01:49 +02:00
icon: 'tryton-log',
2017-08-30 18:19:04 +02:00
label: Sao.i18n.gettext('View Logs...'),
}, {
2017-10-20 14:55:55 +02:00
id: (this.screen &&
Sao.common.MODELHISTORY.contains(this.screen.model_name)) ?
2017-08-30 18:19:04 +02:00
'revision': null,
2018-08-19 20:01:49 +02:00
icon: 'tryton-history',
2017-08-30 18:19:04 +02:00
label: Sao.i18n.gettext('Show revisions...'),
}, null, {
id: 'attach',
2018-08-19 20:01:49 +02:00
icon: 'tryton-attach',
2017-08-30 18:19:04 +02:00
label: Sao.i18n.gettext('Attachment'),
tooltip: Sao.i18n.gettext('Add an attachment to the record'),
}, {
id: 'note',
2018-08-19 20:01:49 +02:00
icon: 'tryton-note',
2017-08-30 18:19:04 +02:00
label: Sao.i18n.gettext('Note'),
tooltip: Sao.i18n.gettext('Add a note to the record'),
}, {
id: 'action',
2018-08-19 20:01:49 +02:00
icon: 'tryton-launch',
2017-08-30 18:19:04 +02:00
label: Sao.i18n.gettext('Action'),
}, null, {
id: 'relate',
2018-08-19 20:01:49 +02:00
icon: 'tryton-link',
2017-08-30 18:19:04 +02:00
label: Sao.i18n.gettext('Relate'),
}, {
id: 'print',
2018-08-19 20:01:49 +02:00
icon: 'tryton-print',
2017-08-30 18:19:04 +02:00
label: Sao.i18n.gettext('Print'),
}, null, {
id: 'export',
2018-08-19 20:01:49 +02:00
icon: 'tryton-export',
2017-08-30 18:19:04 +02:00
label: Sao.i18n.gettext('Export'),
}, {
id: 'import',
2018-08-19 20:01:49 +02:00
icon: 'tryton-import',
2017-08-30 18:19:04 +02:00
label: Sao.i18n.gettext('Import'),
}, null, {
id: 'close',
2018-08-19 20:01:49 +02:00
icon: 'tryton-close',
2017-08-30 18:19:04 +02:00
label: Sao.i18n.gettext('Close Tab'),
},
];
},
2013-10-14 09:02:47 +00:00
create_tabcontent: function() {
this.el = jQuery('<div/>', {
'class': this.class_
});
2015-10-24 13:30:12 +02:00
var toolbar = this.create_toolbar().appendTo(this.el);
2016-12-27 18:17:05 +01:00
this.title = toolbar.find('.title');
2015-10-24 13:30:12 +02:00
2016-09-19 22:21:09 +02:00
this.content = jQuery('<div/>').appendTo(this.el);
2015-10-15 10:33:37 +02:00
if (this.info_bar) {
this.el.append(this.info_bar.el);
}
2013-10-14 09:02:47 +00:00
},
2015-02-06 00:32:51 +01:00
set_menu: function(menu) {
2017-08-30 18:19:04 +02:00
var previous;
this.menu_def().forEach(function(item) {
var menuitem;
if (item) {
if (!this[item.id]) {
return;
}
menuitem = jQuery('<li/>', {
'role': 'presentation'
});
var link = jQuery('<a/>', {
2017-08-30 18:27:52 +02:00
'id': item.id,
2017-08-30 18:19:04 +02:00
'role': 'menuitem',
'href': '#',
'tabindex': -1
2018-08-19 20:01:49 +02:00
}).append(Sao.common.ICONFACTORY.get_icon_img(item.icon, {
'aria-hidden': 'true',
2017-08-30 18:19:04 +02:00
})).append(' ' + item.label).appendTo(menuitem);
this.menu_buttons[item.id] = menuitem;
2018-05-29 10:30:32 +02:00
link.click(function(evt) {
evt.preventDefault();
2017-08-30 18:19:04 +02:00
this[item.id]();
2015-10-22 11:41:16 +02:00
}.bind(this));
2017-08-30 18:19:04 +02:00
} else if (!item && previous) {
menuitem = jQuery('<li/>', {
'role': 'separator',
2018-03-23 22:26:45 +01:00
'class': 'divider hidden-xs',
2017-08-30 18:19:04 +02:00
});
2015-10-22 11:41:16 +02:00
} else {
2017-08-30 18:19:04 +02:00
return;
2015-10-22 11:41:16 +02:00
}
2017-08-30 18:19:04 +02:00
previous = menuitem;
menuitem.appendTo(menu);
2013-10-14 09:02:47 +00:00
}.bind(this));
},
2013-01-08 13:32:10 +01:00
create_toolbar: function() {
2016-12-27 18:17:05 +01:00
var toolbar = jQuery('<nav/>', {
'class': 'toolbar navbar navbar-default',
'role': 'toolbar'
}).append(jQuery('<div/>', {
'class': 'container-fluid'
}).append(jQuery('<div/>', {
2018-08-30 16:09:18 +02:00
'class': 'dropdown navbar-header navbar-left flip'
2016-12-27 18:17:05 +01:00
}).append(jQuery('<a/>', {
'href': "#",
'class': "navbar-brand dropdown-toggle",
'data-toggle': 'dropdown',
'role': 'button',
'aria-expanded': false,
'aria-haspopup': true
}).append(jQuery('<span/>', {
'class': 'title'
})).append(jQuery('<span/>', {
'class': 'caret'
}))).append(jQuery('<ul/>', {
'class': 'dropdown-menu',
'role': 'menu'
2017-01-15 18:59:21 +01:00
})).append(jQuery('<button/>', {
'type': 'button',
'class': 'close visible-xs visible-sm',
'aria-label': Sao.i18n.gettext('Close')
}).append(jQuery('<span/>', {
'aria-hidden': true
}).append('&times;')).click(function() {
this.close();
}.bind(this)))).append(jQuery('<div/>', {
2018-08-30 16:09:18 +02:00
'class': 'btn-toolbar navbar-right flip',
2016-12-27 18:17:05 +01:00
'role': 'toolbar'
})));
var wrapper = jQuery('<div/>', {
2016-12-27 18:17:05 +01:00
'class': 'toolbar-wrapper'
}).append(toolbar);
2015-02-06 00:32:51 +01:00
this.set_menu(toolbar.find('ul[role*="menu"]'));
2016-12-27 18:17:05 +01:00
var group;
2017-08-30 18:19:04 +02:00
var add_button = function(item) {
if (!item || !item.tooltip) {
2016-12-27 18:17:05 +01:00
group = null;
return;
}
2017-10-20 14:55:55 +02:00
if (!item.id || !this[item.id]) {
2017-08-30 18:19:04 +02:00
return;
}
2016-12-27 18:17:05 +01:00
if (!group) {
group = jQuery('<div/>', {
'class': 'btn-group',
'role': 'group'
}).appendTo(toolbar.find('.btn-toolbar'));
}
2017-08-30 18:19:04 +02:00
this.buttons[item.id] = jQuery('<button/>', {
2016-12-27 18:17:05 +01:00
'type': 'button',
'class': 'btn btn-default navbar-btn',
2017-08-30 18:19:04 +02:00
'title': item.label,
'id': item.id
2013-01-08 13:32:10 +01:00
})
2018-08-19 20:01:49 +02:00
.append(Sao.common.ICONFACTORY.get_icon_img(item.icon, {
'aria-hidden': 'true',
2015-02-06 00:32:51 +01:00
}))
2018-03-22 16:58:40 +01:00
.appendTo(group);
2018-03-27 10:49:21 +02:00
this.buttons[item.id].click(item, function(event) {
var item = event.data;
var button = this.buttons[item.id];
button.prop('disabled', true);
(this[item.id](this) || jQuery.when())
.always(function() {
button.prop('disabled', false);
});
}.bind(this));
2013-01-08 13:32:10 +01:00
};
2017-08-30 18:19:04 +02:00
this.menu_def().forEach(add_button.bind(this));
this.status_label = jQuery('<span/>', {
'class': 'badge',
}).appendTo(jQuery('<div/>', {
'class': 'navbar-text hidden-xs',
}).insertAfter(this.buttons.previous));
2016-12-27 18:17:05 +01:00
toolbar.find('.btn-toolbar > .btn-group').last().addClass(
'hidden-xs');
2015-10-22 11:41:56 +02:00
var tabs = jQuery('#tabs');
toolbar.affix({
'target': tabs.parent(),
'offset': {
'top': function() {
2017-02-23 09:31:16 +01:00
return jQuery('#main_navbar').height();
2015-10-22 11:41:56 +02:00
}
}
});
toolbar.on('affix.bs.affix', function() {
2017-02-23 09:31:16 +01:00
wrapper.height(jQuery('#main_navbar').height());
});
toolbar.on('affix-top.bs.affix affix-bottom.bs.affix',
function() {
wrapper.height('');
});
2015-10-22 11:41:56 +02:00
toolbar.on('affixed.bs.affix', function() {
Sao.Tab.affix_set_with(toolbar);
});
toolbar.on('affixed-top.bs.affix affixed-bottom.bs.affix',
function() {
Sao.Tab.affix_unset_width(toolbar);
});
return wrapper;
2013-10-14 09:02:47 +00:00
},
2018-05-12 00:39:59 +02:00
show: function() {
jQuery('#tablist').find('a[href="#' + this.id + '"]').tab('show');
},
2013-10-14 09:02:47 +00:00
close: function() {
2015-02-06 00:32:51 +01:00
var tabs = jQuery('#tabs');
2017-01-15 18:59:21 +01:00
var tablist = jQuery('#tablist');
var tab = tablist.find('#nav-' + this.id);
2015-02-06 00:32:51 +01:00
var content = tabs.find('#' + this.id);
2018-05-12 00:39:59 +02:00
this.show();
2015-03-16 19:14:57 +01:00
return this._close_allowed().then(function() {
2017-01-27 16:57:14 +01:00
var next = tab.nextAll('li').first();
2015-02-06 00:32:51 +01:00
if (!next.length) {
2017-01-27 16:57:14 +01:00
next = tab.prevAll('li').first();
2013-10-18 17:09:10 +02:00
}
2018-03-22 16:58:40 +01:00
tab.remove();
2015-02-06 00:32:51 +01:00
content.remove();
2013-11-25 12:18:32 +01:00
Sao.Tab.tabs.splice(Sao.Tab.tabs.indexOf(this), 1);
2018-05-12 00:39:59 +02:00
if (next.length) {
2015-02-06 00:32:51 +01:00
next.find('a').tab('show');
2018-05-12 00:39:59 +02:00
} else {
Sao.set_url();
2015-02-06 00:32:51 +01:00
}
2015-10-23 19:47:08 +02:00
tabs.trigger('ready');
2013-11-25 12:18:32 +01:00
}.bind(this));
2015-03-16 19:14:57 +01:00
},
_close_allowed: function() {
return jQuery.when();
},
set_name: function(name) {
2017-02-25 18:38:58 +01:00
this.name = name;
this.name_el.text(Sao.common.ellipsize(name, 20));
2018-03-22 16:58:40 +01:00
this.name_el.parents('li').first().attr('title', name);
2018-05-12 00:39:59 +02:00
},
get_url: function() {
},
2013-01-04 21:29:48 +01:00
});
2012-11-29 11:32:58 +01:00
2015-10-22 11:41:56 +02:00
Sao.Tab.affix_set_with = function(toolbar) {
var width = jQuery(toolbar.parent()).width();
toolbar.css('width', width);
};
Sao.Tab.affix_unset_width = function(toolbar) {
toolbar.css('width', '');
};
jQuery(window).resize(function() {
jQuery('.toolbar').each(function(i, toolbar) {
toolbar = jQuery(toolbar);
toolbar.affix('checkPosition');
if (toolbar.hasClass('affix')) {
Sao.Tab.affix_set_with(toolbar);
} else {
Sao.Tab.affix_unset_width(toolbar);
}
});
});
2013-01-04 21:29:48 +01:00
Sao.Tab.counter = 0;
2013-11-25 12:18:32 +01:00
Sao.Tab.tabs = [];
Sao.Tab.tabs.close = function(warning) {
if (warning && Sao.Tab.tabs.length) {
return Sao.common.sur.run(
2015-05-29 10:44:10 +02:00
Sao.i18n.gettext(
'The following action requires to close all tabs.\n' +
'Do you want to continue?')).then(function() {
return Sao.Tab.tabs.close(false);
});
2013-11-25 12:18:32 +01:00
}
if (Sao.Tab.tabs.length) {
var tab = Sao.Tab.tabs[0];
return tab.close().then(function() {
if (!~Sao.Tab.tabs.indexOf(tab)) {
return Sao.Tab.tabs.close();
} else {
return jQuery.Deferred().reject();
}
});
}
if (Sao.main_menu_screen) {
2015-03-27 15:06:29 +01:00
return Sao.main_menu_screen.save_tree_state().then(function() {
Sao.main_menu_screen = null;
});
}
2013-11-25 12:18:32 +01:00
return jQuery.when();
};
Sao.Tab.tabs.get_current = function() {
2017-01-27 16:57:14 +01:00
var tabs = jQuery('#tablist');
2015-02-06 00:32:51 +01:00
var i = tabs.find('li').index(tabs.find('li.active'));
return Sao.Tab.tabs[i];
};
Sao.Tab.tabs.close_current = function() {
var tab = this.get_current();
tab.close();
};
2013-01-04 21:29:48 +01:00
Sao.Tab.create = function(attributes) {
if (attributes.context === undefined) {
attributes.context = {};
}
var tab;
if (attributes.model) {
tab = new Sao.Tab.Form(attributes.model, attributes);
} else {
tab = new Sao.Tab.Board(attributes);
}
2018-05-12 00:39:59 +02:00
tab.view_prm.done(function() {
Sao.Tab.add(tab);
});
2015-03-16 19:14:57 +01:00
};
Sao.Tab.add = function(tab) {
2015-02-06 00:32:51 +01:00
var tabs = jQuery('#tabs');
2017-01-15 18:59:21 +01:00
var tablist = jQuery('#tablist');
var tabcontent = jQuery('#tabcontent');
2014-10-22 11:56:20 +02:00
var tab_link = jQuery('<a/>', {
2015-02-06 00:32:51 +01:00
'aria-controls': tab.id,
'role': 'tab',
'data-toggle': 'tab',
'href': '#' + tab.id
2018-05-12 00:39:59 +02:00
}).on('show.bs.tab', function() {
Sao.set_url(tab.get_url(), tab.name);
2014-10-22 11:56:20 +02:00
})
2015-02-06 00:32:51 +01:00
.append(jQuery('<button/>', {
'class': 'close'
}).append(jQuery('<span/>', {
'aria-hidden': true
}).append('&times;')).append(jQuery('<span/>', {
'class': 'sr-only'
}).append(Sao.i18n.gettext('Close'))).click(function(evt) {
evt.preventDefault();
2014-10-22 11:56:20 +02:00
tab.close();
2015-02-06 00:32:51 +01:00
}))
.append(tab.name_el);
2014-10-22 11:56:20 +02:00
jQuery('<li/>', {
2015-02-06 00:32:51 +01:00
'role': 'presentation',
2017-01-15 18:59:21 +01:00
'data-placement': 'bottom',
2014-10-22 11:56:20 +02:00
id: 'nav-' + tab.id
2015-02-06 00:32:51 +01:00
}).append(tab_link)
2017-01-15 18:59:21 +01:00
.appendTo(tablist);
2014-10-22 11:56:20 +02:00
jQuery('<div/>', {
2015-02-06 00:32:51 +01:00
role: 'tabpanel',
'class': 'tab-pane',
2014-10-22 11:56:20 +02:00
id: tab.id
2015-02-06 00:32:51 +01:00
}).html(tab.el)
2017-01-15 18:59:21 +01:00
.appendTo(tabcontent);
2015-02-06 00:32:51 +01:00
tab_link.tab('show');
2015-10-23 19:47:08 +02:00
tabs.trigger('ready');
2013-01-04 21:29:48 +01:00
};
2017-08-30 18:27:52 +02:00
Sao.Tab.previous_tab = function() {
Sao.Tab.move('prevAll');
};
Sao.Tab.next_tab = function() {
Sao.Tab.move('nextAll');
};
Sao.Tab.move = function(direction) {
var current_tab = this.tabs.get_current();
var tabs = jQuery('#tabs');
var tablist = jQuery('#tablist');
var tab = tablist.find('#nav-' + current_tab.id);
var next = tab[direction]('li').first();
if (next) {
next.find('a').tab('show');
tabs.trigger('ready');
}
};
2013-01-04 21:29:48 +01:00
Sao.Tab.Form = Sao.class_(Sao.Tab, {
2013-10-14 09:02:47 +00:00
class_: 'tab-form',
2013-01-04 21:29:48 +01:00
init: function(model_name, attributes) {
Sao.Tab.Form._super.init.call(this);
var screen = new Sao.Screen(model_name, attributes);
2013-08-26 14:26:03 +02:00
screen.tab = this;
2013-01-04 21:29:48 +01:00
this.screen = screen;
this.attributes = jQuery.extend({}, attributes);
2013-10-14 09:02:47 +00:00
2015-07-03 19:35:28 +02:00
this.info_bar = new Sao.Window.InfoBar();
2013-10-14 09:02:47 +00:00
this.create_tabcontent();
2013-08-21 16:26:20 +02:00
screen.message_callback = this.record_message.bind(this);
2018-05-12 00:39:59 +02:00
screen.switch_callback = function() {
Sao.set_url(this.get_url(), this.name);
}.bind(this);
this.set_buttons_sensitive();
2013-08-21 16:26:20 +02:00
this.view_prm = this.screen.switch_view().done(function() {
2016-07-05 09:54:16 +02:00
this.set_name(attributes.name || '');
2016-09-19 22:21:09 +02:00
this.content.append(screen.screen_container.el);
2013-11-25 12:18:32 +01:00
if (attributes.res_id) {
2016-08-12 13:23:28 +02:00
if (!jQuery.isArray(attributes.res_id)) {
attributes.res_id = [attributes.res_id];
}
screen.group.load(attributes.res_id);
2013-11-25 12:18:32 +01:00
screen.set_current_record(
screen.group.get(attributes.res_id));
screen.display();
} else {
if (screen.current_view.view_type == 'form') {
screen.new_();
}
if (~['tree', 'graph', 'calendar'].indexOf(
screen.current_view.view_type)) {
screen.search_filter();
}
}
2014-03-18 19:56:07 +01:00
this.update_revision();
2013-10-14 09:02:47 +00:00
}.bind(this));
2013-01-08 13:32:10 +01:00
},
create_toolbar: function() {
var toolbar = Sao.Tab.Form._super.create_toolbar.call(this);
var screen = this.screen;
var buttons = this.buttons;
var prm = screen.model.execute('view_toolbar_get', [],
2018-03-09 19:10:58 +01:00
screen.context());
2013-01-08 13:32:10 +01:00
prm.done(function(toolbars) {
[
2018-08-19 20:01:49 +02:00
['action', 'tryton-launch',
2015-05-29 10:44:10 +02:00
Sao.i18n.gettext('Launch action')],
2018-08-19 20:01:49 +02:00
['relate', 'tryton-link',
2015-05-29 10:44:10 +02:00
Sao.i18n.gettext('Open related records')],
2018-08-19 20:01:49 +02:00
['print', 'tryton-print',
2015-05-29 10:44:10 +02:00
Sao.i18n.gettext('Print report')]
2013-01-08 13:32:10 +01:00
].forEach(function(menu_action) {
2016-12-27 18:17:05 +01:00
var button = jQuery('<div/>', {
'class': 'btn-group',
'role': 'group'
2015-02-06 00:32:51 +01:00
})
2016-12-27 18:17:05 +01:00
.append(jQuery('<button/>', {
'type': 'button',
'class': 'btn btn-default navbar-btn dropdown-toggle',
2015-02-06 00:32:51 +01:00
'data-toggle': 'dropdown',
2016-12-27 18:17:05 +01:00
'aria-expanded': false,
'aria-haspopup': true,
'title': menu_action[2],
2017-01-20 09:25:05 +01:00
'id': menu_action[0],
2015-02-06 00:32:51 +01:00
})
2018-08-19 20:01:49 +02:00
.append(Sao.common.ICONFACTORY.get_icon_img(
menu_action[1], {
'aria-hidden': 'true',
}))
2015-02-06 00:32:51 +01:00
.append(jQuery('<span/>', {
'class': 'caret'
2018-03-22 16:58:40 +01:00
})))
2015-02-06 00:32:51 +01:00
.append(jQuery('<ul/>', {
'class': 'dropdown-menu',
2016-12-27 18:17:05 +01:00
'role': 'menu',
2015-03-20 13:10:56 +01:00
'aria-labelledby': menu_action[0]
2015-02-06 00:32:51 +01:00
}))
2016-12-27 18:17:05 +01:00
.appendTo(toolbar.find('.btn-toolbar > .btn-group').last());
2013-01-08 13:32:10 +01:00
buttons[menu_action[0]] = button;
2016-12-27 18:17:05 +01:00
var dropdown = button
.on('show.bs.dropdown', function() {
jQuery(this).parents('.btn-group').removeClass(
'hidden-xs');
}).on('hide.bs.dropdown', function() {
jQuery(this).parents('.btn-group').addClass(
'hidden-xs');
});
2015-02-06 00:32:51 +01:00
var menu = button.find('ul[role*="menu"]');
2018-01-24 14:35:44 +01:00
button.click(function() {
menu.find('.' + menu_action[0] + '_button').remove();
var buttons = screen.get_buttons();
buttons.forEach(function(button) {
var button_kw = (button.attributes.keyword ||
'action');
if (button_kw != menu_action[0]) {
return;
}
var item = jQuery('<li/>', {
'role': 'presentation',
'class': menu_action[0] + '_button'
})
.append(
jQuery('<a/>', {
'role': 'menuitem',
'href': '#',
'tabindex': -1
}).append(
button.attributes.string || ''))
.click(function(evt) {
evt.preventDefault();
2018-01-24 14:35:44 +01:00
screen.button(button.attributes);
})
.appendTo(menu);
2015-02-06 00:32:51 +01:00
});
2018-01-24 14:35:44 +01:00
});
2013-01-08 13:32:10 +01:00
toolbars[menu_action[0]].forEach(function(action) {
2015-03-20 13:10:56 +01:00
var item = jQuery('<li/>', {
'role': 'presentation'
})
.append(jQuery('<a/>', {
'role': 'menuitem',
'href': '#',
'tabindex': -1
}).append(action.name))
.click(function(evt) {
evt.preventDefault();
var prm = jQuery.when();
if (this.screen.modified()) {
prm = this.save();
}
prm.then(function() {
2014-02-20 23:36:38 +01:00
var exec_action = jQuery.extend({}, action);
var record_id = null;
if (screen.current_record) {
record_id = screen.current_record.id;
}
var record_ids = screen.current_view
.selected_records().map(function(record) {
return record.id;
});
exec_action = Sao.Action.evaluate(exec_action,
menu_action[0], screen.current_record);
var data = {
model: screen.model_name,
id: record_id,
ids: record_ids
};
Sao.Action.exec_action(exec_action, data,
2018-03-09 19:10:58 +01:00
screen.context());
2014-02-20 23:36:38 +01:00
});
}.bind(this))
2015-02-06 00:32:51 +01:00
.appendTo(menu);
}.bind(this));
}.bind(this));
}.bind(this));
2013-01-08 13:32:10 +01:00
return toolbar;
2013-01-08 14:58:18 +01:00
},
2015-03-16 19:14:57 +01:00
_close_allowed: function() {
return this.modified_save();
},
2013-03-04 19:08:16 +01:00
modified_save: function() {
2013-10-18 17:09:10 +02:00
this.screen.save_tree_state();
2013-03-04 19:08:16 +01:00
this.screen.current_view.set_value();
if (this.screen.modified()) {
2015-05-29 10:44:10 +02:00
return Sao.common.sur_3b.run(
Sao.i18n.gettext('This record has been modified\n' +
'do you want to save it?'))
2013-11-25 12:18:32 +01:00
.then(function(result) {
switch(result) {
case 'ok':
return this.save();
case 'ko':
return this.reload(false);
default:
return jQuery.Deferred().reject();
}
}.bind(this));
2013-03-04 19:08:16 +01:00
}
2013-11-25 12:18:32 +01:00
return jQuery.when();
2013-03-04 19:08:16 +01:00
},
new_: function() {
2013-08-21 16:26:20 +02:00
if (!Sao.common.MODELACCESS.get(this.screen.model_name).create) {
2018-03-27 10:49:21 +02:00
return jQuery.when();
2013-08-21 16:26:20 +02:00
}
2018-03-27 10:49:21 +02:00
return this.modified_save().then(function() {
return this.screen.new_().then(function() {
2015-07-03 19:35:28 +02:00
this.info_bar.message();
}.bind(this));
2013-11-25 12:18:32 +01:00
// TODO activate_save
}.bind(this));
2013-03-04 19:08:16 +01:00
},
save: function() {
var access = Sao.common.MODELACCESS.get(this.screen.model_name);
if (!(access.write || access.create)) {
return jQuery.Deferred().reject();
2013-08-21 16:26:20 +02:00
}
2015-07-03 19:35:28 +02:00
return this.screen.save_current().then(
function() {
this.info_bar.message(
Sao.i18n.gettext('Record saved.'), 'info');
this.screen.count_tab_domain();
2015-07-03 19:35:28 +02:00
}.bind(this),
function() {
this.info_bar.message(
2015-07-16 22:36:11 +02:00
this.screen.invalid_message(), 'danger');
return jQuery.Deferred().reject();
2015-07-03 19:35:28 +02:00
}.bind(this));
2013-03-04 19:08:16 +01:00
},
2013-01-08 14:58:18 +01:00
switch_: function() {
2018-03-27 10:49:21 +02:00
return this.modified_save().done(function() {
2013-11-25 12:18:32 +01:00
this.screen.switch_view();
}.bind(this));
2013-03-22 16:56:41 +01:00
},
reload: function(test_modified) {
2013-11-25 12:18:32 +01:00
if (test_modified === undefined) {
test_modified = true;
}
var reload = function() {
2015-03-13 16:45:47 +01:00
return this.screen.cancel_current().then(function() {
2018-07-24 19:10:09 +02:00
var set_cursor = false;
var record_id = null;
if (this.screen.current_record) {
record_id = this.screen.current_record.id;
}
2013-11-25 12:18:32 +01:00
this.screen.save_tree_state(false);
if (this.screen.current_view.view_type != 'form') {
2018-07-24 19:10:09 +02:00
return this.screen.search_filter(
this.screen.screen_container.search_entry.val())
.then(function() {
this.screen.group.forEach(function(record) {
if (record.id == record_id) {
this.screen.set_current_record(record);
set_cursor = true;
}
}.bind(this));
return set_cursor;
}.bind(this));
2013-11-25 12:18:32 +01:00
}
2018-07-24 19:10:09 +02:00
return set_cursor;
}.bind(this))
.then(function(set_cursor) {
return this.screen.display(set_cursor).then(function() {
2015-07-03 19:35:28 +02:00
this.info_bar.message();
2018-07-24 19:10:09 +02:00
// TODO activate_save
this.screen.count_tab_domain();
2015-07-03 19:35:28 +02:00
}.bind(this));
2013-11-25 12:18:32 +01:00
}.bind(this));
}.bind(this);
if (test_modified) {
return this.modified_save().then(reload);
} else {
return reload();
2013-03-22 16:56:41 +01:00
}
2013-08-26 14:26:03 +02:00
},
2013-10-14 09:02:47 +00:00
copy: function() {
if (!Sao.common.MODELACCESS.get(this.screen.model_name).create) {
2018-03-27 10:49:21 +02:00
return jQuery.when();
2013-10-14 09:02:47 +00:00
}
2018-03-27 10:49:21 +02:00
return this.modified_save().then(function() {
return this.screen.copy().then(function() {
2015-07-03 19:35:28 +02:00
this.info_bar.message(
Sao.i18n.gettext(
'Working now on the duplicated record(s).'),
'info');
this.screen.count_tab_domain();
2015-07-03 19:35:28 +02:00
}.bind(this));
2013-11-25 12:18:32 +01:00
}.bind(this));
2013-10-14 09:02:47 +00:00
},
delete_: function() {
if (!Sao.common.MODELACCESS.get(this.screen.model_name)['delete']) {
2018-03-27 10:49:21 +02:00
return jQuery.when();
2013-10-14 09:02:47 +00:00
}
2013-11-25 12:18:32 +01:00
var msg;
if (this.screen.current_view.view_type == 'form') {
2015-10-16 02:40:18 +02:00
msg = Sao.i18n.gettext('Are you sure to remove this record?');
2013-11-25 12:18:32 +01:00
} else {
2015-05-29 10:44:10 +02:00
msg = Sao.i18n.gettext('Are you sure to remove those records?');
2013-11-25 12:18:32 +01:00
}
2018-03-27 10:49:21 +02:00
return Sao.common.sur.run(msg).then(function() {
return this.screen.remove(true, false, true).then(
2015-07-03 19:35:28 +02:00
function() {
this.info_bar.message(
Sao.i18n.gettext('Records removed.'),
'info');
this.screen.count_tab_domain();
2015-07-03 19:35:28 +02:00
}.bind(this), function() {
this.info_bar.message(
Sao.i18n.gettext('Records not removed.'),
'danger');
}.bind(this));
2013-11-25 12:18:32 +01:00
}.bind(this));
2013-10-14 09:02:47 +00:00
},
previous: function() {
2018-03-27 10:49:21 +02:00
return this.modified_save().then(function() {
2013-11-25 12:18:32 +01:00
this.screen.display_previous();
2015-07-03 19:35:28 +02:00
this.info_bar.message();
// TODO activate_save
2013-11-25 12:18:32 +01:00
}.bind(this));
},
next: function() {
2018-03-27 10:49:21 +02:00
return this.modified_save().then(function() {
2013-11-25 12:18:32 +01:00
this.screen.display_next();
2015-07-03 19:35:28 +02:00
this.info_bar.message();
// TODO activate_save
2013-11-25 12:18:32 +01:00
}.bind(this));
},
2013-10-14 09:02:47 +00:00
search: function() {
var search_entry = this.screen.screen_container.search_entry;
if (search_entry.is(':visible')) {
window.setTimeout(function() {
search_entry.focus();
}, 0);
}
2018-03-27 10:49:21 +02:00
return jQuery.when();
2013-10-14 09:02:47 +00:00
},
logs: function() {
var record = this.screen.current_record;
if ((!record) || (record.id < 0)) {
2015-07-03 19:35:28 +02:00
this.info_bar.message(
Sao.i18n.gettext('You have to select one record.'),
'info');
2018-03-27 10:49:21 +02:00
return jQuery.when();
2013-10-14 09:02:47 +00:00
}
var fields = [
2015-05-29 10:44:10 +02:00
['id', Sao.i18n.gettext('ID:')],
['create_uid.rec_name',
Sao.i18n.gettext('Creation User:')],
['create_date', Sao.i18n.gettext('Creation Date:')],
['write_uid.rec_name',
Sao.i18n.gettext('Latest Modification by:')],
['write_date', Sao.i18n.gettext('Latest Modification Date:')]
2013-10-14 09:02:47 +00:00
];
2018-03-27 10:49:21 +02:00
return this.screen.model.execute('read', [[record.id],
2013-10-14 09:02:47 +00:00
fields.map(function(field) {
return field[0];
2018-03-09 19:10:58 +01:00
})], this.screen.context())
2013-10-14 09:02:47 +00:00
.then(function(result) {
result = result[0];
var message = '';
fields.forEach(function(field) {
var key = field[0];
var label = field[1];
var value = result[key] || '/';
if (result[key] &&
~['create_date', 'write_date'].indexOf(key)) {
value = Sao.common.format_datetime(
Sao.common.date_format(),
'%H:%M:%S',
value);
}
message += label + ' ' + value + '\n';
});
2015-05-29 10:44:10 +02:00
message += Sao.i18n.gettext('Model: ') + this.screen.model.name;
2013-10-14 09:02:47 +00:00
Sao.common.message.run(message);
}.bind(this));
},
2014-03-18 19:56:07 +01:00
revision: function() {
var current_id = null;
if (this.screen.current_record) {
current_id = this.screen.current_record.id;
}
2014-10-10 10:00:52 +02:00
var set_revision = function(revisions) {
return function(revision) {
if (revision) {
// Add a millisecond as microseconds are truncated
2016-04-02 15:23:39 +02:00
revision.add(1, 'milliseconds');
2014-03-18 19:56:07 +01:00
}
2014-10-10 10:00:52 +02:00
if ((this.screen.current_view.view_type == 'form') &&
(revision < revisions[revisions.length - 1][0])) {
revision = revisions[revisions.length - 1][0];
}
2018-03-09 19:10:58 +01:00
if (revision != this.screen.context()._datetime) {
this.screen.clear();
2018-03-09 19:10:58 +01:00
// Update group context that will be propagated by
2014-10-10 10:00:52 +02:00
// recreating new group
2018-03-09 19:10:58 +01:00
this.screen.group._context._datetime = revision;
2014-10-10 10:00:52 +02:00
if (this.screen.current_view.view_type != 'form') {
this.screen.search_filter(
this.screen.screen_container
.search_entry.val());
} else {
2018-03-09 19:10:58 +01:00
this.screen.group.load([current_id]);
2014-10-10 10:00:52 +02:00
}
2015-10-27 10:55:57 +01:00
this.screen.display(true);
2014-10-10 10:00:52 +02:00
this.update_revision();
}
}.bind(this);
2014-03-18 19:56:07 +01:00
}.bind(this);
2018-03-27 10:49:21 +02:00
return this.modified_save().then(function() {
2014-03-18 19:56:07 +01:00
var ids = this.screen.current_view.selected_records().map(
function(record) {
return record.id;
});
2018-03-27 10:49:21 +02:00
return this.screen.model.execute('history_revisions',
2018-03-09 19:10:58 +01:00
[ids], this.screen.context())
2014-03-18 19:56:07 +01:00
.then(function(revisions) {
2014-10-10 10:00:52 +02:00
new Sao.Window.Revision(revisions, set_revision(revisions));
2014-03-18 19:56:07 +01:00
});
}.bind(this));
},
update_revision: function() {
2018-03-09 19:10:58 +01:00
var revision = this.screen.context()._datetime;
2018-07-10 17:08:56 +02:00
var label, title;
2014-03-18 19:56:07 +01:00
if (revision) {
var date_format = Sao.common.date_format();
var time_format = '%H:%M:%S.%f';
2018-07-10 17:08:56 +02:00
var revision_label = ' @ ' + Sao.common.format_datetime(
date_format, time_format, revision);
label = Sao.common.ellipsize(
this.name, 80 - revision_label.length) + revision_label;
title = this.name + revision_label;
2014-03-18 19:56:07 +01:00
} else {
2018-07-10 17:08:56 +02:00
label = Sao.common.ellipsize(this.name, 80);
title = this.name;
2014-03-18 19:56:07 +01:00
}
2018-07-10 17:08:56 +02:00
this.title.text(label);
this.title.attr('title', title);
this.set_buttons_sensitive(revision);
},
set_buttons_sensitive: function(revision) {
if (!revision) {
var access = Sao.common.MODELACCESS.get(this.screen.model_name);
2017-08-30 18:19:04 +02:00
[['new_', access.create],
['save', access.create || access.write],
['delete_', access.delete],
['copy', access.create],
['import', access.create],
].forEach(function(e) {
var button = e[0];
var access = e[1];
2017-08-30 18:19:04 +02:00
if (this.buttons[button]) {
this.buttons[button].toggleClass('disabled', !access);
}
if (this.menu_buttons[name]) {
this.menu_buttons[name]
.toggleClass('disabled', !access);
}
}.bind(this));
} else {
2017-08-30 18:19:04 +02:00
['new_', 'save', 'delete_', 'copy', 'import'].forEach(
function(name) {
if (this.buttons[name]) {
this.buttons[name].addClass('disabled');
}
if (this.menu_buttons[name]) {
this.menu_buttons[name].addClass('disabled');
}
}.bind(this));
}
2014-03-18 19:56:07 +01:00
},
2013-08-26 14:26:03 +02:00
attach: function() {
var record = this.screen.current_record;
if (!record || (record.id < 0)) {
return;
}
new Sao.Window.Attachment(record, function() {
this.update_attachment_count(true);
}.bind(this));
},
update_attachment_count: function(reload) {
var record = this.screen.current_record;
if (record) {
record.get_attachment_count(reload).always(
this.attachment_count.bind(this));
} else {
this.attachment_count(0);
}
},
attachment_count: function(count) {
2016-12-27 18:17:05 +01:00
var badge = this.buttons.attach.find('.badge');
if (!badge.length) {
badge = jQuery('<span/>', {
'class': 'badge'
}).appendTo(this.buttons.attach);
}
var text = count || '';
if (count > 99) {
text = '99+';
}
badge.text(text);
2018-03-24 00:23:45 +01:00
this.buttons.attach.attr(
2018-04-06 18:29:18 +02:00
'title', Sao.i18n.gettext("Attachment(%1)", count));
2013-08-26 14:26:03 +02:00
var record_id = this.screen.get_id();
this.buttons.attach.prop('disabled',
record_id < 0 || record_id === null);
2013-10-14 09:02:47 +00:00
},
2016-02-02 19:15:58 +01:00
note: function() {
var record = this.screen.current_record;
if (!record || (record.id < 0)) {
return;
}
new Sao.Window.Note(record, function() {
this.update_unread_note(true);
}.bind(this));
},
update_unread_note: function(reload) {
var record = this.screen.current_record;
if (record) {
record.get_unread_note(reload).always(
this._unread_note.bind(this));
} else {
this._unread_note(0);
}
},
_unread_note: function(unread) {
2016-12-27 18:17:05 +01:00
var badge = this.buttons.note.find('.badge');
if (!badge.length) {
badge = jQuery('<span/>', {
'class': 'badge'
}).appendTo(this.buttons.note);
}
var text = unread || '';
if (unread > 99) {
text = '99+';
}
badge.text(text);
2018-03-24 00:23:45 +01:00
this.buttons.note.attr(
'title', Sao.i18n.gettext("Note(%1)", unread));
2016-02-02 19:15:58 +01:00
var record_id = this.screen.get_id();
this.buttons.note.prop('disabled',
record_id < 0 || record_id === null);
},
record_message: function(data) {
if (data) {
var name = "_";
if (data[0] !== 0) {
name = data[0];
}
var buttons = ['print', 'relate', 'attach'];
buttons.forEach(function(button_id){
var button = this.buttons[button_id];
if (button) {
var disabled = button.is(':disabled');
button.prop('disabled', disabled || data[0] === 0);
}
}.bind(this));
this.buttons.switch_.prop('disabled',
this.attributes.view_ids > 1);
var msg = name + ' / ' + data[1];
if (data[1] < data[2]) {
msg += Sao.i18n.gettext(' of ') + data[2];
}
this.status_label.text(msg).attr('title', msg);
}
2015-07-03 19:35:28 +02:00
this.info_bar.message();
// TODO activate_save
2015-07-03 19:35:28 +02:00
},
2013-10-14 09:02:47 +00:00
action: function() {
2015-10-16 03:16:24 +02:00
window.setTimeout(function() {
this.buttons.action.find('button').click();
2015-10-16 03:16:24 +02:00
}.bind(this));
2013-10-14 09:02:47 +00:00
},
relate: function() {
2015-10-16 03:16:24 +02:00
window.setTimeout(function() {
this.buttons.relate.find('button').click();
2015-10-16 03:16:24 +02:00
}.bind(this));
2013-10-14 09:02:47 +00:00
},
print: function() {
2015-10-16 03:16:24 +02:00
window.setTimeout(function() {
this.buttons.print.find('button').click();
2015-10-16 03:16:24 +02:00
}.bind(this));
2016-10-31 10:09:43 +01:00
},
export: function(){
2018-08-30 15:57:43 +02:00
new Sao.Window.Export(
this.title.text(), this.screen,
2016-10-31 10:09:43 +01:00
this.screen.current_view.selected_records().map(function(r) {
return r.id;
}),
2017-02-21 17:34:28 +01:00
this.screen.current_view.get_fields(),
2018-03-09 19:10:58 +01:00
this.screen.context());
2016-10-31 10:09:43 +01:00
},
import: function(){
2018-08-30 15:57:43 +02:00
new Sao.Window.Import(this.title.text(), this.screen);
2018-05-12 00:39:59 +02:00
},
get_url: function() {
return this.screen.get_url(this.attributes.name);
},
2013-01-04 21:29:48 +01:00
});
2015-03-16 19:14:57 +01:00
2015-10-15 10:33:37 +02:00
Sao.Tab.Board = Sao.class_(Sao.Tab, {
class_: 'tab-board',
init: function(attributes) {
var UIView, view_prm;
Sao.Tab.Board._super.init.call(this);
this.model = attributes.model;
this.view_id = (attributes.view_ids.length > 0 ?
attributes.view_ids[0] : null);
this.context = attributes.context;
2016-07-05 09:54:16 +02:00
this.name = attributes.name || '';
2015-10-15 10:33:37 +02:00
this.dialogs = [];
this.board = null;
UIView = new Sao.Model('ir.ui.view');
2018-05-12 00:39:59 +02:00
this.view_prm = UIView.execute('read', [[this.view_id], ['arch']],
2015-10-15 10:33:37 +02:00
this.context);
2018-05-12 00:39:59 +02:00
this.view_prm.done(function(views) {
2015-10-15 10:33:37 +02:00
var view, board;
view = jQuery(jQuery.parseXML(views[0].arch));
this.board = new Sao.View.Board(view, this.context);
this.board.actions_prms.done(function() {
var i, len, action;
for (i = 0, len = this.board.actions.length; i < len; i++) {
action = this.board.actions[i];
action.screen.tab = this;
}
}.bind(this));
this.el.append(this.board.el);
}.bind(this));
this.create_tabcontent();
this.set_name(this.name);
this.title.html(this.name_el.text());
},
2015-10-22 11:41:16 +02:00
reload: function() {
this.board.reload();
2015-10-15 10:33:37 +02:00
},
record_message: function() {
var i, len;
var action;
len = this.board.actions.length;
for (i = 0, len=len; i < len; i++) {
action = this.board.actions[i];
action.update_domain(this.board.actions);
}
},
attachment_count: function() {
2016-02-02 19:15:58 +01:00
},
note: function() {
},
update_unread_note: function() {
2015-10-15 10:33:37 +02:00
}
});
2015-03-16 19:14:57 +01:00
Sao.Tab.Wizard = Sao.class_(Sao.Tab, {
class_: 'tab-wizard',
init: function(wizard) {
Sao.Tab.Wizard._super.init.call(this);
this.wizard = wizard;
this.set_name(wizard.name);
2015-03-16 19:14:57 +01:00
wizard.tab = this;
this.create_tabcontent();
this.title.html(this.name_el.text());
2015-03-16 19:14:57 +01:00
this.el.append(wizard.form);
},
create_toolbar: function() {
return jQuery('<span/>');
},
_close_allowed: function() {
var wizard = this.wizard;
var prm = jQuery.when();
if ((wizard.state !== wizard.end_state) &&
(wizard.end_state in wizard.states)) {
prm = wizard.response(wizard.end_state);
}
var dfd = jQuery.Deferred();
prm.always(function() {
if (wizard.state === wizard.end_state) {
dfd.resolve();
} else {
dfd.reject();
}
});
return dfd.promise();
}
});
2013-01-04 21:29:48 +01:00
}());