/* This file is part of Tryton. The COPYRIGHT file at the top level of this repository contains the full copyright notices and license terms. */ (function() { 'use strict'; Sao.Tab = Sao.class_(Object, { init: function() { Sao.Tab.tabs.push(this); this.buttons = {}; this.id = 'tab-' + Sao.Tab.counter++; this.name_el = jQuery(''); }, create_tabcontent: function() { this.el = jQuery('
', { 'class': this.class_ }); var toolbar = this.create_toolbar().appendTo(this.el); this.title = toolbar.find('a.navbar-brand'); this.content = jQuery('
').appendTo(this.el); if (this.info_bar) { this.el.append(this.info_bar.el); } }, set_menu: function(menu) { this.menu_def().forEach(function(definition) { var icon = definition[0]; var name = definition[1]; var func = definition[2]; var item = jQuery('
  • ', { 'role': 'presentation' }).appendTo(menu); var link = jQuery('', { 'role': 'menuitem', 'href': '#', 'tabindex': -1 }).append(jQuery('', { 'class': 'glyphicon ' + icon, 'aria-hidden': 'true' })).append(' ' + name).appendTo(item); if (func) { link.click(function() { this[func](); }.bind(this)); } else { item.addClass('disabled'); } }.bind(this)); }, create_toolbar: function() { var toolbar = jQuery( '' ); var wrapper = jQuery('
    ', { 'class': 'nav-wrapper' }).append(toolbar); this.set_menu(toolbar.find('ul[role*="menu"]')); var add_button = function(tool) { var item = jQuery('
  • ', { 'role': 'presentation' }).appendTo(toolbar.find('.navbar-collapse > ul')); this.buttons[tool[0]] = jQuery('', { 'role': 'menuitem', 'href': '#', 'id': tool[0] }) .append(jQuery('', { 'class': 'glyphicon ' + tool[1], 'aria-hidden': 'true' })) .append(jQuery('', { 'class': 'hidden-sm' }).append(' ' + tool[2])) .appendTo(item); if (tool[4]) { this.buttons[tool[0]].click(this[tool[4]].bind(this)); } else { item.addClass('disabled'); } // TODO tooltip }; this.toolbar_def().forEach(add_button.bind(this)); var tabs = jQuery('#tabs'); toolbar.affix({ 'target': tabs.parent(), 'offset': { 'top': function() { return tabs.find('.nav-tabs').height(); } } }); toolbar.on('affix.bs.affix', function() { wrapper.height(toolbar.height()); }); toolbar.on('affix-top.bs.affix affix-bottom.bs.affix', function() { wrapper.height(''); }); 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; }, close: function() { var tabs = jQuery('#tabs'); var tab = tabs.find('#nav-' + this.id); var content = tabs.find('#' + this.id); tabs.find('a[href="#' + this.id + '"]').tab('show'); return this._close_allowed().then(function() { var next = tab.next(); if (!next.length) { next = tab.prev(); } tab.remove(); content.remove(); Sao.Tab.tabs.splice(Sao.Tab.tabs.indexOf(this), 1); if (next) { next.find('a').tab('show'); } tabs.trigger('ready'); }.bind(this)); }, _close_allowed: function() { return jQuery.when(); }, set_name: function(name) { this.name_el.text(name); } }); 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); } }); }); Sao.Tab.counter = 0; Sao.Tab.tabs = []; Sao.Tab.tabs.close = function(warning) { if (warning && Sao.Tab.tabs.length) { return Sao.common.sur.run( 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); }); } 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) { return Sao.main_menu_screen.save_tree_state().then(function() { Sao.main_menu_screen = null; }); } return jQuery.when(); }; Sao.Tab.tabs.get_current = function() { var tabs = jQuery('#tabs > ul'); 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(); }; 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); } Sao.Tab.add(tab); }; Sao.Tab.add = function(tab) { var tabs = jQuery('#tabs'); var tab_link = jQuery('', { 'aria-controls': tab.id, 'role': 'tab', 'data-toggle': 'tab', 'href': '#' + tab.id }) .append(jQuery('