123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125 |
- /* global sinon */
- import {
- bootstrapDiagram,
- getDiagramJS,
- inject
- } from 'test/TestHelper';
- import testImage from './resources/a.png';
- import {
- query as domQuery,
- queryAll as domQueryAll,
- classes as domClasses
- } from 'min-dom';
- import { createEvent as globalEvent } from '../../../util/MockEvents';
- import popupMenuModule from 'lib/features/popup-menu';
- import modelingModule from 'lib/features/modeling';
- function queryEntry(popupMenu, id) {
- return queryPopup(popupMenu, '[data-id="' + id + '"]');
- }
- function queryPopup(popupMenu, selector) {
- return domQuery(selector, popupMenu._current.container);
- }
- var menuProvider = {
- getHeaderEntries: function() {
- return [
- { id: 'entry1', label: 'foo' }
- ];
- },
- getEntries: function() {
- return [
- { id: 'entry2', label: 'foo' },
- { id: 'entry3', label: 'bar' }
- ];
- }
- };
- var betterMenuProvider = {
- getHeaderEntries: function() {
- return [
- { id: 'entryA', label: 'A' }
- ];
- },
- getEntries: function() {
- return [
- { id: 'entryB', label: 'B' }
- ];
- }
- };
- describe('features/popup', function() {
- beforeEach(bootstrapDiagram({
- modules: [
- popupMenuModule,
- modelingModule
- ]
- }));
- describe('bootstrap', function() {
- it('overlay to be defined', inject(function(popupMenu) {
- expect(popupMenu).to.exist;
- expect(popupMenu.open).to.exist;
- }));
- });
- describe('#registerProvider', function() {
- it('should add provider', inject(function(popupMenu) {
- // given
- var provider = {};
- // when
- popupMenu.registerProvider('provider', provider);
- // then
- expect(popupMenu._providers.provider).to.exist;
- }));
- it('should add two providers', inject(function(popupMenu) {
- // given
- var provider1 = {};
- var provider2 = {};
- // when
- popupMenu.registerProvider('provider1', provider1);
- popupMenu.registerProvider('provider2', provider2);
- // then
- expect(popupMenu._providers.provider1).to.exist;
- expect(popupMenu._providers.provider2).to.exist;
- }));
- });
- describe('#isEmpty', function() {
- it('should return true if empty', inject(function(popupMenu) {
- // when
- popupMenu.registerProvider('empty-menu', {
- getEntries: function() { return []; },
- getHeaderEntries: function() { return []; }
- });
- // then
- expect(popupMenu.isEmpty({}, 'empty-menu')).to.be.true;
- }));
- it('should return false if entries', inject(function(popupMenu) {
- // when
- popupMenu.registerProvider('entry-menu', {
- getEntries: function() { return [ { id: 1 } ]; }
- });
- // then
- expect(popupMenu.isEmpty({}, 'entry-menu')).to.be.false;
- }));
- it('should return false if header entries', inject(function(popupMenu) {
- // when
- popupMenu.registerProvider('header-entry-menu', {
- getEntries: function() { return [ { id: 1 } ]; }
- });
- // then
- expect(popupMenu.isEmpty({}, 'header-entry-menu')).to.be.false;
- }));
- it('should throw error when provider id is missing', inject(
- function(popupMenu) {
- // when
- popupMenu.registerProvider('header-entry-menu', {
- getEntries: function() { return [ { id: 1 } ]; }
- });
- // then
- expect(function() {
- popupMenu.isEmpty({});
- }).to.throw('providerId parameter is missing');
- }
- ));
- it('should throw error when element is missing', inject(
- function(popupMenu) {
- // when
- popupMenu.registerProvider('header-entry-menu', {
- getEntries: function() { return [ { id: 1 } ]; }
- });
- // then
- expect(function() {
- popupMenu.isEmpty();
- }).to.throw('element parameter is missing');
- }
- ));
- });
- describe('#open', function() {
- beforeEach(inject(function(popupMenu) {
- popupMenu.registerProvider('menu', menuProvider);
- }));
- it('should open', inject(function(popupMenu, eventBus) {
- // given
- var openSpy = sinon.spy();
- eventBus.on('popupMenu.open', openSpy);
- // when
- popupMenu.open({}, 'menu', { x: 100, y: 100 });
- // then
- expect(popupMenu._current).to.exist;
- expect(openSpy).to.have.been.calledOnce;
- }));
- it('should attach popup to html', inject(function(popupMenu) {
- // when
- popupMenu.open({}, 'menu' ,{ x: 100, y: 100 });
- var container = popupMenu._current.container;
- // then
- expect(domClasses(container).has('djs-popup')).to.be.true;
- expect(domClasses(container).has('menu')).to.be.true;
- }));
- it('should add entries to menu', inject(function(popupMenu) {
- // when
- popupMenu.open({}, 'menu' ,{ x: 100, y: 100 });
- // then
- var domEntry = queryEntry(popupMenu, 'entry1');
- expect(domEntry.textContent).to.eql('foo');
- }));
- it('should add action-id to entry', inject(function(popupMenu) {
- // when
- popupMenu.registerProvider('item-menu', {
- getEntries: function() {
- return [
- { id: 'save', label: 'SAVE' },
- { id: 'load', label: 'LOAD' },
- { id: 'undo', label: 'UNDO' }
- ];
- },
- getHeaderEntries: function() {}
- });
- popupMenu.open({}, 'item-menu' ,{ x: 100, y: 100 });
- // then
- var parent = queryPopup(popupMenu, '.djs-popup-body');
- var entry1 = parent.childNodes[0];
- var entry2 = parent.childNodes[1];
- var entry3 = parent.childNodes[2];
- expect(entry1.getAttribute('data-id')).to.eql('save');
- expect(entry2.getAttribute('data-id')).to.eql('load');
- expect(entry3.getAttribute('data-id')).to.eql('undo');
- }));
- it('should open menu for specific element', inject(function(popupMenu) {
- // when
- popupMenu.registerProvider('menu', menuProvider);
- popupMenu.open({}, 'menu', { x: 100, y: 100 });
- var currentProvider = popupMenu._current.provider;
- // then
- expect(currentProvider.getHeaderEntries()).to.deep.include({
- id: 'entry1',
- label: 'foo'
- });
- expect(currentProvider.getEntries()).to.deep.include({
- id: 'entry2',
- label: 'foo'
- });
- }));
- it('should throw error when no provider', inject(function(popupMenu) {
- // when not registering a provider
- // then
- expect(function() {
- popupMenu.open({}, 'foo', { x: 100, y: 100 });
- }).to.throw('Provider is not registered: foo');
- }));
- it('should throw error when element is missing', inject(function(popupMenu) {
- popupMenu.registerProvider('menu', menuProvider);
- expect(function() {
- popupMenu.open();
- }).to.throw('Element is missing');
- }));
- describe('multiple providers', function() {
- beforeEach(inject(function(popupMenu) {
- popupMenu.registerProvider('better-menu', betterMenuProvider);
- }));
- it('should open first menu', inject(function(popupMenu, eventBus) {
- // given
- var openSpy = sinon.spy();
- eventBus.on('popupMenu.open', openSpy);
- // when
- popupMenu.open({}, 'menu', { x: 100, y: 100 });
- // then
- expect(popupMenu._current).to.exist;
- expect(popupMenu._current.headerEntries[0].id).to.eql('entry1');
- expect(popupMenu._current.entries[0].id).to.eql('entry2');
- }));
- it('should open second menu', inject(function(popupMenu, eventBus) {
- // given
- var openSpy = sinon.spy();
- eventBus.on('popupMenu.open', openSpy);
- // when
- popupMenu.open({}, 'better-menu', { x: 100, y: 100 });
- // then
- expect(popupMenu._current).to.exist;
- expect(popupMenu._current.headerEntries[0].id).to.eql('entryA');
- expect(popupMenu._current.entries[0].id).to.eql('entryB');
- }));
- });
- });
- describe('#close', function() {
- beforeEach(inject(function(popupMenu) {
- popupMenu.registerProvider('menu', menuProvider);
- popupMenu.open({}, 'menu' ,{ x: 100, y: 100 });
- }));
- it('should close', inject(function(popupMenu, eventBus) {
- // given
- var closeSpy = sinon.spy();
- eventBus.on('popupMenu.close', closeSpy);
- // when
- popupMenu.close();
- // then
- var open = popupMenu.isOpen();
- expect(open).to.be.false;
- expect(closeSpy).to.have.been.calledOnce;
- }));
- it('should not fail if already closed', inject(function(popupMenu) {
- // when
- popupMenu.close();
- // then
- expect(popupMenu.close).not.to.throw;
- }));
- });
- describe('#isOpen', function() {
- it('should not be open initially', inject(function(popupMenu) {
- // when
- popupMenu.registerProvider('menu', menuProvider);
- // then
- expect(popupMenu.isOpen()).to.be.false;
- }));
- it('should be open after opening', inject(function(popupMenu) {
- // when
- popupMenu.registerProvider('menu', menuProvider);
- popupMenu.open({}, 'menu' ,{ x: 100, y: 100 });
- // then
- expect(popupMenu.isOpen()).to.be.true;
- }));
- it('should be closed after closing', inject(function(popupMenu) {
- // given
- popupMenu.registerProvider('menu', menuProvider);
- popupMenu.open({}, 'menu' ,{ x: 100, y: 100 });
- // when
- popupMenu.close();
- // then
- expect(popupMenu.isOpen()).to.be.false;
- }));
- });
- describe('#trigger', function() {
- it('should trigger the right action handler', inject(function(popupMenu) {
- // given
- popupMenu.registerProvider('test-menu', {
- getEntries: function() {
- return [
- {
- id: '1',
- label: 'Entry 1',
- className: 'Entry_1',
- action: function(event, entry) {
- return 'Entry 1';
- }
- }, {
- id: '2',
- label: 'Entry 2',
- className: 'Entry_2',
- action: function(event, entry) {
- return 'Entry 2';
- }
- }
- ];
- }
- });
- popupMenu.open({}, 'test-menu' ,{ x: 100, y: 100 });
- var entry = queryPopup(popupMenu, '.Entry_2');
- // when
- var trigger = popupMenu.trigger(globalEvent(entry, { x: 0, y: 0 }));
- // then
- expect(trigger).to.eql('Entry 2');
- }));
- });
- describe('integration', function() {
- describe('events', function() {
- it('should close menu (contextPad.close)', inject(function(popupMenu, eventBus) {
- // given
- popupMenu.registerProvider('menu', menuProvider);
- popupMenu.open({}, 'menu' ,{ x: 100, y: 100 });
- // when
- eventBus.fire('contextPad.close');
- // then
- var open = popupMenu.isOpen();
- expect(open).to.be.false;
- }));
- it('should close menu (canvas.viewbox.changing)', inject(function(popupMenu, eventBus) {
- // given
- popupMenu.registerProvider('menu', menuProvider);
- popupMenu.open({}, 'menu' ,{ x: 100, y: 100 });
- // when
- eventBus.fire('canvas.viewbox.changing');
- // then
- var open = popupMenu.isOpen();
- expect(open).to.be.false;
- }));
- });
- });
- describe('menu styling', function() {
- it('should add standard class to entry', inject(function(popupMenu) {
- // given
- var testMenuProvider = {
- getEntries: function() {
- return [
- { id: '1', label: 'Entry 1' },
- { id: '2', label: 'Entry 2' }
- ];
- }
- };
- popupMenu.registerProvider('test-menu', testMenuProvider);
- // when
- popupMenu.open({}, 'test-menu' ,{ x: 100, y: 100 });
- // then
- var elements = domQueryAll('.entry', popupMenu._current.container);
- expect(elements.length).to.eql(2);
- }));
- it('should add custom class to entry if specfied', inject(function(popupMenu) {
- // given
- var testMenuProvider = {
- getEntries: function() {
- return [
- {
- id: '1',
- label: 'Entry 1'
- },
- {
- id: '2',
- label: 'Entry 2 - special',
- className: 'special-entry cls2 cls3'
- }
- ];
- }
- };
- popupMenu.registerProvider('test-menu', testMenuProvider);
- // when
- popupMenu.open({}, 'test-menu' ,{ x: 100, y: 100 });
- // then
- var element = queryPopup(popupMenu, '.special-entry');
- expect(element.textContent).to.eql('Entry 2 - special');
- expect(element.className).to.eql('entry special-entry cls2 cls3');
- }));
- it('should name the css classes correctly', inject(function(popupMenu) {
- // given
- var testMenuProvider = {
- getEntries: function() {
- return [
- { id: '1', label: 'Entry 1' },
- { id: '2', label: 'Entry 2' }
- ];
- },
- getHeaderEntries: function() {
- return [{ id: 'A', label: 'Header Entry A' }];
- }
- };
- popupMenu.registerProvider('test-menu', testMenuProvider);
- // when
- popupMenu.open({}, 'test-menu' ,{ x: 100, y: 100 });
- var popupBody = queryPopup(popupMenu, '.djs-popup-body');
- var popupHeader = queryPopup(popupMenu, '.djs-popup-header');
- // then
- expect(domQueryAll('.entry', popupBody).length).to.eql(2);
- expect(domQueryAll('.entry', popupHeader).length).to.eql(1);
- }));
- it('should look awesome', inject(function(popupMenu) {
- // given
- var testMenuProvider = {
- getEntries: function() {
- return [
- { id: '1', label: 'Entry 1' },
- { id: '2', label: 'Entry 2', active: true },
- { id: '3', label: 'Entry 3' },
- { id: '4', label: 'Entry 4', disabled: true },
- { id: '5', label: 'Entry 5' }
- ];
- },
- getHeaderEntries: function() {
- return [
- { id: 'A', label: 'A' },
- { id: 'B', label: 'B' },
- { id: 'C', label: 'C', active: true },
- { id: 'D', label: 'D', disabled: true },
- { id: 'E', label: 'E', disabled: true }
- ];
- }
- };
- popupMenu.registerProvider('test-menu', testMenuProvider);
- // when
- popupMenu.open({}, 'test-menu' ,{ x: 100, y: 100 });
- // then
- // looks awesome?
- }));
- });
- describe('singleton handling', function() {
- it('should update the popup menu, when it is opened again' , inject(
- function(popupMenu) {
- // given
- var popupMenu1 = {
- getEntries: function() {
- return [
- { id: '1', label: 'Entry 1' },
- { id: '2', label: 'Entry 2' }
- ];
- }
- };
- popupMenu.registerProvider('popup-menu1', popupMenu1);
- var popupMenu2 = {
- getEntries: function() {
- return [
- { id: '3', label: 'Entry A' },
- { id: '4', label: 'Entry B' }
- ];
- }
- };
- popupMenu.registerProvider('popup-menu2', popupMenu2);
- // when
- popupMenu.open({}, 'popup-menu1', { x: 100, y: 100 });
- popupMenu.open({}, 'popup-menu2', { x: 200, y: 200 });
- var container = popupMenu._current.container,
- entriesContainer = domQuery('.djs-popup-body', container);
- // then
- expect(domQuery('.popup-menu1', document)).to.be.null;
- expect(domQuery('.popup-menu2', document)).not.to.be.null;
- expect(domClasses(container).has('popup-menu2')).to.be.true;
- expect(container.style.left).to.eql('200px');
- expect(container.style.top).to.eql('200px');
- expect(entriesContainer.childNodes[0].textContent).to.eql('Entry A');
- expect(entriesContainer.childNodes[1].textContent).to.eql('Entry B');
- }
- ));
- });
- describe('header', function() {
- it('should throw an error, if the id of a header entry is not set', inject(
- function(popupMenu) {
- // when
- popupMenu.registerProvider('test-menu', {
- getEntries: function() {
- return [ { label: 'foo' } ];
- }
- });
- // then
- expect(function() {
- popupMenu.open({}, 'test-menu', { x: 100, y: 100 });
- }).to.throw('every entry must have the id property set');
- }
- ));
- it('should be attached to the top of the popup menu, if set' , inject(
- function(popupMenu) {
- // when
- popupMenu.registerProvider('menu', menuProvider);
- popupMenu.open({}, 'menu', { x: 100, y: 100 });
- // then
- expect(queryPopup(popupMenu, '.djs-popup-header')).to.exist;
- }
- ));
- it('should add a custom css class to the header section, if specified', inject(
- function(popupMenu) {
- var testMenuProvider = {
- getHeaderEntries: function() {
- return [ { id: '1', className: 'header-entry-1' } ];
- },
- getEntries: function() {
- return [ { id: '2', label: 'foo' } ];
- }
- };
- popupMenu.registerProvider('test-menu', testMenuProvider);
- popupMenu.open({}, 'test-menu', { x: 100, y: 100 });
- // then
- expect(queryPopup(popupMenu, '.header-entry-1')).to.exist;
- }
- ));
- it('should add an image to the header section, if specified', inject(function(popupMenu) {
- // given
- var testMenuProvider = {
- getHeaderEntries: function() {
- return [
- {
- id: '1',
- imageUrl: testImage,
- className: 'image-1'
- }
- ];
- },
- getEntries: function() { return []; }
- };
- popupMenu.registerProvider('test-menu', testMenuProvider);
- popupMenu.open({}, 'test-menu', { x: 100, y: 100 });
- // then
- var img = queryPopup(popupMenu, '.image-1 img');
- expect(img).to.exist;
- expect(img.getAttribute('src')).to.eql(testImage);
- }));
- it('should add a labeled element to the header section, if specified', inject(
- function(popupMenu) {
- var testMenuProvider = {
- getHeaderEntries: function() {
- return [ { id: '1', label: 'foo', className: 'label-1' } ];
- },
- getEntries: function() { return []; }
- };
- popupMenu.registerProvider('test-menu', testMenuProvider);
- popupMenu.open({}, 'test-menu', { x: 100, y: 100 });
- // then
- var headerEntry = queryPopup(popupMenu, '.label-1');
- expect(headerEntry.textContent).to.eql('foo');
- }
- ));
- it('should throw an error if the position argument is missing', inject(
- function(popupMenu) {
- popupMenu.registerProvider('menu', menuProvider);
- // then
- expect(function() {
- popupMenu.open({}, 'menu');
- }).to.throw('the position argument is missing');
- }
- ));
- it('should only render body if entries exist', inject(function(popupMenu) {
- // when
- var testMenuProvider = {
- getEntries: function() {
- return [ ]; }
- };
- popupMenu.registerProvider('test-menu', testMenuProvider);
- popupMenu.open({}, 'test-menu', { x: 100, y: 100 });
- // then
- expect(queryPopup(popupMenu, '.djs-popup-header')).not.to.exist;
- expect(queryPopup(popupMenu, '.djs-popup-body')).not.to.exist;
- }));
- it('should trigger action on click', inject(function(popupMenu) {
- // given
- var actionListener = sinon.spy();
- var testProvider = {
- getHeaderEntries: function() {
- return [{
- id: '1',
- action: actionListener,
- label: 'foo'
- }];
- },
- getEntries: function() { return []; }
- };
- popupMenu.registerProvider('test-menu', testProvider);
- popupMenu.open({}, 'test-menu', { x: 100, y: 100 });
- var entry = queryPopup(popupMenu, '.entry');
- // when
- popupMenu.trigger(globalEvent(entry, { x: 0, y: 0 }));
- // then
- expect(actionListener).to.have.been.called;
- }));
- it('should add disabled and active classes', inject(function(popupMenu) {
- // given
- var entry;
- var testMenuProvider = {
- getHeaderEntries: function() {
- return [
- {
- id: 'foo',
- active: true,
- disabled: true,
- label: 'foo'
- }
- ];
- },
- getEntries: function() { return []; }
- };
- // when
- popupMenu.registerProvider('test-menu', testMenuProvider);
- popupMenu.open({}, 'test-menu', { x: 100, y: 100 });
- // then
- entry = queryEntry(popupMenu, 'foo');
- expect(domClasses(entry).has('active')).to.be.true;
- expect(domClasses(entry).has('disabled')).to.be.true;
- }));
- });
- // different browsers, different outcomes
- describe('position', function() {
- beforeEach(inject(function(popupMenu, elementRegistry) {
- var customProvider = {
- getEntries: function() {
- return [
- { id: '1', label: 'Entry 1' },
- { id: '2', label: 'Entry 2', active: true },
- { id: '3', label: 'Entry 3' },
- { id: '4', label: 'Entry 4', disabled: true },
- { id: '5', label: 'Entry 5' }
- ];
- },
- getHeaderEntries: function() {
- return [
- { id: 'A', label: 'A' },
- { id: 'B', label: 'B' },
- { id: 'C', label: 'C', active: true },
- { id: 'D', label: 'D', disabled: true },
- { id: 'E', label: 'E', disabled: true }
- ];
- }
- };
- popupMenu.registerProvider('custom-provider', customProvider);
- }));
- it('should open within bounds above', inject(function(popupMenu, canvas) {
- // given
- var clientRect = canvas._container.getBoundingClientRect();
- var cursorPosition = { x: clientRect.left + 100, y: clientRect.top + 500 };
- // when
- popupMenu.open({}, 'custom-provider', { x: 100, y: 500, cursor: cursorPosition });
- var menu = popupMenu._current.container;
- var menuDimensions = {
- width: menu.scrollWidth,
- height: menu.scrollHeight
- };
- // then
- expect(menu.offsetTop).to.equal(500 - menuDimensions.height);
- }));
- it('should open within bounds above (limited client rect height)', inject(
- function(popupMenu, canvas) {
- // given
- // limited client rect height
- canvas._container.parentElement.style.height = '200px';
- var clientRect = canvas._container.getBoundingClientRect();
- var cursorPosition = { x: clientRect.left + 10, y: clientRect.top + 150 };
- // when
- popupMenu.open({}, 'custom-provider', { x: 100, y: 150, cursor: cursorPosition });
- var menu = popupMenu._current.container;
- // then
- expect(menu.offsetTop).to.equal(10);
- }
- ));
- it('should open within bounds to the left', inject(function(popupMenu, canvas) {
- // given
- var clientRect = canvas._container.getBoundingClientRect();
- var cursorPosition = { x: clientRect.left + 2000, y: clientRect.top + 100 };
- // when
- popupMenu.open({}, 'custom-provider', { x: 2000, y: 100, cursor: cursorPosition });
- var menu = popupMenu._current.container;
- var menuDimensions = {
- width: menu.scrollWidth,
- height: menu.scrollHeight
- };
- expect(menu.offsetLeft).to.equal(2000 - menuDimensions.width);
- }));
- });
- describe('scaling', function() {
- var NUM_REGEX = /([+-]?\d*[.]?\d+)(?=,|\))/g;
- var zoomLevels = [ 1.0, 1.2, 3.5, 10, 0.5 ];
- function asVector(scaleStr) {
- if (scaleStr && scaleStr !== 'none') {
- var m = scaleStr.match(NUM_REGEX);
- var x = parseFloat(m[0], 10);
- var y = m[1] ? parseFloat(m[1], 10) : x;
- return {
- x: x,
- y: y
- };
- }
- }
- function scaleVector(element) {
- return asVector(element.style.transform);
- }
- function verifyScales(expectedScales) {
- getDiagramJS().invoke(function(canvas, popupMenu) {
- // given
- popupMenu.registerProvider('menu', menuProvider);
- // test multiple zoom steps
- zoomLevels.forEach(function(zoom, index) {
- var expectedScale = expectedScales[index];
- // when
- canvas.zoom(zoom);
- popupMenu.open({}, 'menu', { x: 100, y: 100 });
- var menu = popupMenu._current.container;
- var actualScale = scaleVector(menu) || { x: 1, y: 1 };
- // then
- expect(actualScale.x).to.eql(actualScale.y);
- expect(actualScale.x).to.be.closeTo(expectedScale, 0.00001);
- });
- });
- }
- it('should scale within [ 1.0, 1.5 ] by default', function() {
- // given
- var expectedScales = [ 1.0, 1.2, 1.5, 1.5, 1.0 ];
- bootstrapDiagram({
- modules: [
- popupMenuModule,
- modelingModule
- ]
- })();
- // when
- verifyScales(expectedScales);
- });
- it('should scale within [ 1.0, 1.5 ] without scale config', function() {
- // given
- var expectedScales = [ 1.0, 1.2, 1.5, 1.5, 1.0 ];
- bootstrapDiagram({
- modules: [
- popupMenuModule,
- modelingModule
- ],
- popupMenu: {}
- })();
- // when
- verifyScales(expectedScales);
- });
- it('should scale within the limits set in config', function() {
- // given
- var config = {
- scale: {
- min: 1.0,
- max: 1.2
- }
- };
- var expectedScales = [ 1.0, 1.2, 1.2, 1.2, 1.0 ];
- bootstrapDiagram({
- modules: [
- popupMenuModule,
- modelingModule
- ],
- popupMenu: config
- })();
- // when
- verifyScales(expectedScales);
- });
- it('should scale with scale = true', function() {
- // given
- var config = {
- scale: true
- };
- var expectedScales = zoomLevels;
- bootstrapDiagram({
- modules: [
- popupMenuModule,
- modelingModule
- ],
- popupMenu: config
- })();
- // when
- verifyScales(expectedScales);
- });
- it('should not scale with scale = false', function() {
- // given
- var config = {
- scale: false
- };
- var expectedScales = [ 1.0, 1.0, 1.0, 1.0, 1.0 ];
- bootstrapDiagram({
- modules: [
- popupMenuModule,
- modelingModule
- ],
- popupMenu: config
- })();
- // when
- verifyScales(expectedScales);
- });
- });
- });
|