/* * * (c) Copyright Ascensio System SIA 2010-2019 * * This program is a free software product. You can redistribute it and/or * modify it under the terms of the GNU Affero General Public License (AGPL) * version 3 as published by the Free Software Foundation. In accordance with * Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect * that Ascensio System SIA expressly excludes the warranty of non-infringement * of any third-party rights. * * This program is distributed WITHOUT ANY WARRANTY; without even the implied * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For * details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html * * You can contact Ascensio System SIA at 20A-12 Ernesta Birznieka-Upisha * street, Riga, Latvia, EU, LV-1050. * * The interactive user interfaces in modified source and object code versions * of the Program must display Appropriate Legal Notices, as required under * Section 5 of the GNU AGPL version 3. * * Pursuant to Section 7(b) of the License you must retain the original Product * logo when distributing the program. Pursuant to Section 7(e) we decline to * grant you any rights under trademark law for use of our trademarks. * * All the Product's GUI elements, including illustrations and icon sets, as * well as technical writing content are licensed under the terms of the * Creative Commons Attribution-ShareAlike 4.0 International. See the License * terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode * */ /** * TabBar.js * * Created by Maxim Kadushkin on 28 March 2014 * Copyright (c) 2018 Ascensio System SIA. All rights reserved. * */ define([ 'common/main/lib/component/BaseView', 'common/main/lib/component/Tab' ], function () { 'use strict'; var Events = { bind: function () { if (!this.o) this.o = $({}); this.o.on.apply(this.o, arguments); }, unbind: function () { if (this.o) this.o.off.apply(this.o, arguments); }, trigger: function () { if (!this.o) this.o = $({}); this.o.trigger.apply(this.o, arguments); } }; var StateManager = function (options) { this.initialize.call(this, options); }; _.extend(StateManager.prototype, Events); StateManager.prototype.initialize = function (options) { this.bar = options.bar; }; StateManager.prototype.attach = function (tab) { tab.changeState = $.proxy(function (select) { if (select) { tab.toggleClass('selected'); var selectTab = _.find(this.bar.selectTabs, function (item) {return item.sheetindex === tab.sheetindex;}); if (selectTab) { this.bar.selectTabs = _.without(this.bar.selectTabs, selectTab); } else { this.bar.selectTabs.push(tab); } } else { if (!tab.isSelected()) { this.bar.$el.find('ul > li.selected').removeClass('selected'); tab.addClass('selected'); this.bar.selectTabs.length = 0; this.bar.selectTabs.push(tab); } this.trigger('tab:change', tab); this.bar.$el.find('ul > li.active').removeClass('active'); tab.activate(); this.bar.trigger('tab:changed', this.bar, this.bar.tabs.indexOf(tab), tab); } }, this); var dragHelper = new (function() { return { bounds: [], drag: undefined, calculateBounds: function () { var me = this, length = me.bar.tabs.length, barBounds = me.bar.$bar.get(0).getBoundingClientRect(); me.leftBorder = barBounds.left; me.rightBorder = barBounds.right; if (barBounds) { me.bounds = []; me.scrollLeft = me.bar.$bar.scrollLeft(); me.bar.scrollX = this.scrollLeft; for (var i = 0; i < length; ++i) { this.bounds.push(me.bar.tabs[i].$el.get(0).getBoundingClientRect()); } me.lastTabRight = me.bounds[length - 1].right; me.tabBarLeft = me.bounds[0].left; me.tabBarRight = me.bounds[length - 1].right; me.tabBarRight = Math.min(me.tabBarRight, barBounds.right - 1); } }, setHookTabs: function (e, bar, tabs) { var me = this; function dragComplete() { if (!_.isUndefined(me.drag)) { bar.dragging = false; bar.$el.find('li.mousemove').removeClass('mousemove right'); var arrSelectIndex = []; tabs.forEach(function (item) { arrSelectIndex.push(item.sheetindex); }); if (!_.isUndefined(me.drag.place)) { me.bar.trigger('tab:move', arrSelectIndex, me.drag.place); me.bar.$bar.scrollLeft(me.scrollLeft); me.bar.scrollX = undefined; } else { me.bar.trigger('tab:move', arrSelectIndex); me.bar.$bar.scrollLeft(me.scrollLeft); me.bar.scrollX = undefined; } me.drag = undefined; me.bar.trigger('tab:drop', this); } } function dragMove (event) { if (!_.isUndefined(me.drag)) { me.drag.moveX = event.clientX*Common.Utils.zoom(); if (me.drag.moveX < me.leftBorder) { me.scrollLeft -= 20; me.bar.$bar.scrollLeft(me.scrollLeft); me.calculateBounds(); } else if (me.drag.moveX < me.tabBarRight && me.drag.moveX > me.tabBarLeft) { var name = $(event.target).parent().data('label'), currentTab = _.findIndex(bar.tabs, {label: name}); if (currentTab === -1) { bar.$el.find('li.mousemove').removeClass('mousemove right'); me.drag.place = undefined; } else if (me.bounds[currentTab].left - me.scrollLeft >= me.tabBarLeft) { me.drag.place = currentTab; $(event.target).parent().parent().find('li.mousemove').removeClass('mousemove right'); $(event.target).parent().addClass('mousemove'); } } else if (me.drag.moveX > me.lastTabRight && Math.abs(me.tabBarRight - me.bounds[me.bar.tabs.length - 1].right) < 1) { //move to end of list, right border of the right tab is visible bar.$el.find('li.mousemove').removeClass('mousemove right'); bar.tabs[bar.tabs.length - 1].$el.addClass('mousemove right'); me.drag.place = bar.tabs.length; } else if (me.drag.moveX - me.rightBorder > 3) { me.scrollLeft += 20; me.bar.$bar.scrollLeft(me.scrollLeft); me.calculateBounds(); } } } if (!_.isUndefined(bar) && !_.isUndefined(tabs) && bar.tabs.length > 1) { me.bar = bar; me.drag = {tabs: tabs}; bar.dragging = true; this.calculateBounds(); $(document).on('mousemove.tabbar', dragMove); $(document).on('mouseup.tabbar', function (e) { dragComplete(e); $(document).off('mouseup.tabbar'); $(document).off('mousemove.tabbar', dragMove); }); this.bar.trigger('tab:drag', this.bar.selectTabs); } } } }); tab.$el.on({ click: $.proxy(function (event) { if (!tab.disabled) { if (event.ctrlKey || event.metaKey) { if (!tab.isActive()) { tab.changeState(true); } } else if (event.shiftKey) { this.bar.$el.find('ul > li.selected').removeClass('selected'); this.bar.selectTabs.length = 0; var $active = this.bar.$el.find('ul > li.active'), indexAct = $active.index(), indexCur = this.bar.tabs.indexOf(tab); var startIndex = (indexCur > indexAct) ? indexAct : indexCur, endIndex = (indexCur > indexAct) ? indexCur : indexAct; for (var i = startIndex; i <= endIndex; i++) { this.bar.tabs[i].changeState(true); } } else if (!tab.$el.hasClass('active')) { if (this.bar.tabs.length === this.bar.selectTabs.length) { this.bar.$el.find('ul > li.selected').removeClass('selected'); this.bar.selectTabs.length = 0; } if (tab.control == 'manual') { this.bar.trigger('tab:manual', this.bar, this.bar.tabs.indexOf(tab), tab); } else { tab.changeState(); } } } !tab.disabled && Common.NotificationCenter.trigger('edit:complete', this.bar); }, this), dblclick: $.proxy(function() { this.trigger('tab:dblclick', this, this.tabs.indexOf(tab), tab); }, this.bar), contextmenu: $.proxy(function () { this.trigger('tab:contextmenu', this, this.tabs.indexOf(tab), tab, this.selectTabs); }, this.bar), mousedown: $.proxy(function (e) { if ((3 !== e.which) && !e.ctrlKey && !e.metaKey && !e.shiftKey) { var lockDrag = tab.isLockTheDrag; this.bar.selectTabs.forEach(function (item) { if (item.isLockTheDrag) { lockDrag = true; } }); this.bar.$el.find('ul > li > span').attr('draggable', !lockDrag); tab.changeState(); } else { this.bar.$el.find('ul > li > span').attr('draggable', 'false'); } this.bar.trigger('tab:drag', this.bar.selectTabs); }, this) }); tab.$el.children().on( {dragstart: $.proxy(function (e) { var event = e.originalEvent, img = document.createElement('div'); event.dataTransfer.setDragImage(img, 0, 0); event.dataTransfer.effectAllowed = 'move'; this.bar.trigger('tab:dragstart', event.dataTransfer, this.bar.selectTabs); }, this), dragenter: $.proxy(function (e) { this.bar.$el.find('.mousemove').removeClass('mousemove right'); $(e.currentTarget).parent().addClass('mousemove'); }, this), dragover: $.proxy(function (e) { var event = e.originalEvent; if (event.preventDefault) { event.preventDefault(); // Necessary. Allows us to drop. } event.dataTransfer.dropEffect = 'move'; this.bar.$el.find('.mousemove').removeClass('mousemove right'); $(e.currentTarget).parent().addClass('mousemove'); return false; }, this), dragleave: $.proxy(function (e) { $(e.currentTarget).parent().removeClass('mousemove right'); }, this), dragend: $.proxy(function (e) { var event = e.originalEvent; var data = event.dataTransfer.getData('status'); if (event.dataTransfer.dropEffect === 'move') { this.bar.trigger('tab:dragend', true); } else { this.bar.trigger('tab:dragend', false); } this.bar.$el.find('.mousemove').removeClass('mousemove right'); }, this), drop: $.proxy(function (e) { var event = e.originalEvent, index = $(event.currentTarget).data('index'); this.bar.$el.find('.mousemove').removeClass('mousemove right'); this.bar.trigger('tab:drop', event.dataTransfer, index); event.stopPropagation(); }, this) }); }; StateManager.prototype.detach = function (tab) { tab.$el.off(); }; Common.UI.TabBar = Common.UI.BaseView.extend({ config: { placement : 'top', items : [], draggable : false }, tabs: [], template: _.template('