| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910 |
- describe('ContextMenu', () => {
- var id = 'testContainer';
- beforeEach(function () {
- this.$container = $(`<div id="${id}"></div>`).appendTo('body');
- });
- afterEach(function () {
- if (this.$container) {
- destroy();
- this.$container.remove();
- }
- });
- it('should update context menu items by calling `updateSettings` method', () => {
- var hot = handsontable({
- contextMenu: ['row_above', 'row_below', '---------', 'remove_row'],
- height: 100
- });
- contextMenu();
- var items = $('.htContextMenu tbody td');
- var actions = items.not('.htSeparator');
- var separators = items.filter('.htSeparator');
- expect(actions.length).toEqual(3);
- expect(separators.length).toEqual(1);
- expect(actions.text()).toEqual([
- 'Insert row above',
- 'Insert row below',
- 'Remove row',
- ].join(''));
- hot.updateSettings({
- contextMenu: ['remove_row']
- });
- contextMenu();
- items = $('.htContextMenu tbody td');
- actions = items.not('.htSeparator');
- separators = items.filter('.htSeparator');
- expect(actions.length).toEqual(1);
- expect(separators.length).toEqual(0);
- expect(actions.text()).toEqual([
- 'Remove row',
- ].join(''));
- hot.updateSettings({
- contextMenu: {
- items: {
- remove_col: true,
- hsep1: '---------',
- custom: {name: 'My custom item'},
- }
- }
- });
- contextMenu();
- items = $('.htContextMenu tbody td');
- actions = items.not('.htSeparator');
- separators = items.filter('.htSeparator');
- expect(actions.length).toEqual(2);
- expect(separators.length).toEqual(1);
- expect(actions.text()).toEqual([
- 'Remove column',
- 'My custom item',
- ].join(''));
- });
- describe('menu opening', () => {
- it('should open menu after right click on table cell', () => {
- var hot = handsontable({
- contextMenu: true,
- height: 100
- });
- expect(hot.getPlugin('contextMenu')).toBeDefined();
- expect($('.htContextMenu').is(':visible')).toBe(false);
- contextMenu();
- expect($('.htContextMenu').is(':visible')).toBe(true);
- });
- it('should not open the menu after clicking an open editor', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- height: 100
- });
- selectCell(2, 2);
- keyDownUp('enter');
- expect(hot.getPlugin('contextMenu')).toBeDefined();
- expect($('.htContextMenu').is(':visible')).toBe(false);
- contextMenu(hot.getActiveEditor().TEXTAREA);
- expect($('.htContextMenu').is(':visible')).toBe(false);
- });
- it('should open menu after right click on header cell when only header cells are visible', () => {
- var hot = handsontable({
- data: [],
- colHeaders: ['Year', 'Kia'],
- columns: [{data: 0}, {data: 1}],
- contextMenu: true,
- height: 100
- });
- expect(hot.getPlugin('contextMenu')).toBeDefined();
- expect($('.htContextMenu').is(':visible')).toBe(false);
- contextMenu(hot.rootElement.querySelector('.ht_clone_top thead th'));
- expect($('.htContextMenu').is(':visible')).toBe(true);
- });
- it('should open menu after right click on header corner', () => {
- var hot = handsontable({
- data: [],
- colHeaders: true,
- rowHeaders: true,
- contextMenu: true,
- height: 100
- });
- expect(hot.getPlugin('contextMenu')).toBeDefined();
- expect($('.htContextMenu').is(':visible')).toBe(false);
- contextMenu(hot.rootElement.querySelector('.ht_clone_top_left_corner thead th'));
- expect($('.htContextMenu').is(':visible')).toBe(true);
- });
- it('should open menu after right click active cell border', function () {
- var hot = handsontable({
- contextMenu: true,
- height: 100
- });
- expect(hot.getPlugin('contextMenu')).toBeDefined();
- expect($('.htContextMenu').is(':visible')).toBe(false);
- selectCell(0, 0);
- this.$container.find('.wtBorder.current:eq(0)').simulate('contextmenu');
- expect($('.htContextMenu').is(':visible')).toBe(true);
- });
- });
- describe('menu closing', () => {
- it('should close menu after click', function () {
- var hot = handsontable({
- contextMenu: true,
- height: 100
- });
- contextMenu();
- expect($('.htContextMenu').is(':visible')).toBe(true);
- mouseDown(this.$container);
- expect($('.htContextMenu').is(':visible')).toBe(false);
- });
- it('should close menu after click under the menu', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(500, 10),
- contextMenu: true,
- height: 500
- });
- contextMenu();
- expect($('.htContextMenu').is(':visible')).toBe(true);
- var rect = $('.htContextMenu')[0].getBoundingClientRect();
- var x = parseInt(rect.left + (rect.width / 2), 10);
- var y = parseInt(rect.top + rect.height, 10);
- mouseDown(document.elementFromPoint(x, y));
- expect($('.htContextMenu').is(':visible')).toBe(false);
- });
- });
- describe('menu disabled', () => {
- it('should not open menu after right click', () => {
- var hot = handsontable({
- contextMenu: true,
- height: 100
- });
- hot.getPlugin('contextMenu').disablePlugin();
- expect($('.htContextMenu').is(':visible')).toBe(false);
- contextMenu();
- expect($('.htContextMenu').is(':visible')).toBe(false);
- });
- it('should not create context menu if it\'s disabled in constructor options', () => {
- var hot = handsontable({
- contextMenu: false,
- height: 100
- });
- expect(hot.getPlugin('contextMenu').isEnabled()).toBe(false);
- });
- it('should reenable menu', () => {
- var hot = handsontable({
- contextMenu: true,
- height: 100
- });
- hot.getPlugin('contextMenu').disablePlugin();
- expect($('.htContextMenu').is(':visible')).toBe(false);
- contextMenu();
- expect($('.htContextMenu').is(':visible')).toBe(false);
- hot.getPlugin('contextMenu').enablePlugin();
- contextMenu();
- expect($('.htContextMenu').is(':visible')).toBe(true);
- });
- it('should reenable menu with updateSettings when it was disabled in constructor', () => {
- var hot = handsontable({
- contextMenu: false,
- height: 100
- });
- expect(hot.getPlugin('contextMenu').isEnabled()).toBe(false);
- updateSettings({
- contextMenu: true
- });
- expect(hot.getPlugin('contextMenu').isEnabled()).toBe(true);
- expect($('.htContextMenu').is(':visible')).toBe(false);
- contextMenu();
- expect($('.htContextMenu').is(':visible')).toBe(true);
- });
- it('should disable menu with updateSettings when it was enabled in constructor', () => {
- var hot = handsontable({
- contextMenu: true,
- height: 100
- });
- expect(hot.getPlugin('contextMenu').isEnabled()).toBe(true);
- updateSettings({
- contextMenu: false
- });
- expect(hot.getPlugin('contextMenu').isEnabled()).toBe(false);
- });
- it('should work properly (remove row) after destroy and new init', () => {
- var test = function () {
- handsontable({
- startRows: 5,
- contextMenu: ['remove_row'],
- height: 100
- });
- selectCell(0, 0);
- contextMenu();
- $('.htContextMenu .ht_master .htCore tbody').find('td').not('.htSeparator').eq(0).simulate('mousedown');
- expect(getData().length).toEqual(4);
- };
- test();
- destroy();
- test();
- });
- });
- describe('menu hidden items', () => {
- it('should remove separators from top, bottom and duplicated', () => {
- var hot = handsontable({
- contextMenu: [
- '---------',
- '---------',
- 'row_above',
- '---------',
- '---------',
- 'row_below',
- '---------',
- 'remove_row'
- ],
- height: 100
- });
- contextMenu();
- var items = $('.htContextMenu tbody td');
- var actions = items.not('.htSeparator');
- var separators = items.filter('.htSeparator');
- expect(actions.length).toEqual(3);
- expect(separators.length).toEqual(2);
- });
- it('should hide option if hidden function return true', () => {
- var hot = handsontable({
- startCols: 5,
- colHeaders: true,
- contextMenu: [
- {
- key: '',
- name: 'Custom option',
- hidden() {
- return !this.selection.selectedHeader.cols;
- }
- }
- ]
- });
- contextMenu();
- var items = $('.htContextMenu tbody td');
- var actions = items.not('.htSeparator');
- expect(actions.length).toEqual(0);
- var header = $('.ht_clone_top thead th').eq(1);
- header.simulate('mousedown');
- contextMenu();
- items = $('.htContextMenu tbody td');
- actions = items.not('.htSeparator');
- expect(actions.length).toEqual(1);
- });
- });
- describe('menu destroy', () => {
- it('should close context menu when HOT is being destroyed', () => {
- var hot = handsontable({
- contextMenu: true,
- height: 100
- });
- contextMenu();
- expect($('.htContextMenu').is(':visible')).toBe(true);
- destroy();
- expect($('.htContextMenu').is(':visible')).toBe(false);
- });
- });
- describe('subMenu', () => {
- it('should not open subMenu immediately', (done) => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- height: 100
- });
- contextMenu();
- var item = $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(9);
- item.simulate('mouseover');
- var contextSubMenu = $(`.htContextMenuSub_${item.text()}`).find('tbody td');
- expect(contextSubMenu.length).toEqual(0);
- setTimeout(() => {
- var contextSubMenu = $(`.htContextMenuSub_${item.text()}`).find('tbody td');
- expect(contextSubMenu.length).toEqual(0);
- done();
- }, 100);
- });
- it('should open subMenu with delay', (done) => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- height: 100
- });
- contextMenu();
- var item = $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(9);
- item.simulate('mouseover');
- setTimeout(() => {
- var contextSubMenu = $(`.htContextMenuSub_${item.text()}`);
- expect(contextSubMenu.length).toEqual(1);
- done();
- }, 350); // menu opens after 300ms
- });
- it('should NOT open subMenu if there is no subMenu for item', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- height: 100
- });
- contextMenu();
- var item = $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(8);
- item.simulate('mouseover');
- expect(item.hasClass('htSubmenu')).toBe(false);
- var contextSubMenu = $(`.htContextMenuSub_${item.text()}`);
- expect(contextSubMenu.length).toEqual(0);
- });
- it('should open subMenu on the left of main menu if on the right there\'s no space left', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, Math.floor(window.innerWidth / 50)),
- contextMenu: true,
- width: window.innerWidth
- });
- selectCell(0, countCols() - 1);
- contextMenu();
- var item = $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(9);
- var contextMenuRoot = $('.htContextMenu');
- item.simulate('mouseover');
- expect(item.text()).toBe('Alignment');
- expect(item.hasClass('htSubmenu')).toBe(true);
- var contextSubMenu = $(`.htContextMenuSub_${item.text()}`);
- expect(contextSubMenu.offset().left).toBeLessThan(contextMenuRoot.offset().left - contextSubMenu.width() + 30); // 30 - scroll
- });
- it('should open subMenu on the right of main menu if there\'s free space', (done) => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, Math.floor(window.innerWidth / 50)),
- contextMenu: true,
- width: window.innerWidth
- });
- selectCell(0, countCols() - 9);
- contextMenu();
- var item = $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(9);
- var contextMenuRoot = $('.htContextMenu');
- item.simulate('mouseover');
- setTimeout(() => {
- expect(item.text()).toBe('Alignment');
- expect(item.hasClass('htSubmenu')).toBe(true);
- var contextSubMenu = $(`.htContextMenuSub_${item.text()}`);
- expect(contextSubMenu.offset().left).toBeGreaterThan(contextMenuRoot.offset().left + contextMenuRoot.width() - 30); // 30 - scroll
- done();
- }, 350); // menu opens after 300ms
- });
- it('should open subMenu on the left-bottom of main menu if there\'s free space', (done) => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(Math.floor(window.innerHeight / 23), Math.floor(window.innerWidth / 50)),
- contextMenu: true,
- height: window.innerHeight,
- });
- window.scrollTo(0, document.body.clientHeight);
- selectCell(0, countCols() - 1);
- contextMenu();
- var item = $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(9);
- var contextMenuRoot = $('.htContextMenu');
- item.simulate('mouseover');
- setTimeout(() => {
- expect(item.text()).toBe('Alignment');
- expect(item.hasClass('htSubmenu')).toBe(true);
- var contextSubMenu = $(`.htContextMenuSub_${item.text()}`);
- expect(parseInt(contextSubMenu.offset().top, 10)).toBeAroundValue(parseInt(item.offset().top, 10) - 1);
- expect(parseInt(contextSubMenu.offset().left, 10)).toBeLessThan(contextMenuRoot.offset().left - contextSubMenu.width() + 30); // 30 - scroll
- done();
- }, 350); // menu opens after 300ms
- });
- it('should open subMenu on the right-bottom of main menu if there\'s free space', (done) => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(Math.floor(window.innerHeight / 23), Math.floor(window.innerWidth / 50)),
- contextMenu: true,
- height: window.innerHeight
- });
- window.scrollTo(0, document.body.clientHeight);
- selectCell(0, countCols() - 9);
- contextMenu();
- var item = $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(9);
- var contextMenuRoot = $('.htContextMenu');
- item.simulate('mouseover');
- setTimeout(() => {
- expect(item.text()).toBe('Alignment');
- expect(item.hasClass('htSubmenu')).toBe(true);
- var contextSubMenu = $(`.htContextMenuSub_${item.text()}`);
- expect(parseInt(contextSubMenu.offset().top, 10)).toBeAroundValue(parseInt(item.offset().top, 10) - 1);
- expect(parseInt(contextSubMenu.offset().left, 10)).toBeGreaterThan(contextMenuRoot.offset().left + contextMenuRoot.width() - 30); // 30 - scroll
- done();
- }, 350); // menu opens after 300ms
- });
- it('should open subMenu on the left-top of main menu if there\'s no free space on bottom', (done) => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(Math.floor(window.innerHeight / 23), Math.floor(window.innerWidth / 50)),
- contextMenu: true,
- height: window.innerHeight
- });
- selectCell(countRows() - 1, countCols() - 1);
- contextMenu();
- var item = $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(9);
- var contextMenuRoot = $('.htContextMenu');
- item.simulate('mouseover');
- setTimeout(() => {
- expect(item.text()).toBe('Alignment');
- expect(item.hasClass('htSubmenu')).toBe(true);
- var contextSubMenu = $(`.htContextMenuSub_${item.text()}`);
- expect(contextSubMenu.offset().top + contextSubMenu.height() - 28).toBeAroundValue(item.offset().top);
- expect(contextSubMenu.offset().left).toBeLessThan(contextMenuRoot.offset().left - contextSubMenu.width() + 30); // 30 - scroll
- done();
- }, 350); // menu opens after 300ms
- });
- it('should open subMenu on the right-top of main menu if there\'s no free space on bottom', (done) => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(Math.floor(window.innerHeight / 23), Math.floor(window.innerWidth / 50)),
- contextMenu: true,
- height: window.innerHeight
- });
- selectCell(countRows() - 1, countCols() - 9);
- contextMenu();
- var item = $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(9);
- var contextMenuRoot = $('.htContextMenu');
- item.simulate('mouseover');
- setTimeout(() => {
- expect(item.text()).toBe('Alignment');
- expect(item.hasClass('htSubmenu')).toBe(true);
- var contextSubMenu = $(`.htContextMenuSub_${item.text()}`);
- expect(contextSubMenu.offset().top + contextSubMenu.height() - 28).toBeAroundValue(item.offset().top);
- expect(contextSubMenu.offset().left).toBeGreaterThan(contextMenuRoot.offset().left + contextMenuRoot.width() - 30); // 30 - scroll
- done();
- }, 350); // menu opens after 300ms
- });
- });
- describe('default context menu actions', () => {
- it('should display the default set of actions', () => {
- var hot = handsontable({
- contextMenu: true,
- comments: true,
- height: 100
- });
- contextMenu();
- var items = $('.htContextMenu tbody td');
- var actions = items.not('.htSeparator');
- var separators = items.filter('.htSeparator');
- expect(actions.length).toEqual(13);
- expect(separators.length).toEqual(6);
- expect(actions.text()).toEqual([
- 'Insert row above',
- 'Insert row below',
- 'Insert column on the left',
- 'Insert column on the right',
- 'Remove row',
- 'Remove column',
- 'Undo',
- 'Redo',
- 'Read only',
- 'Alignment',
- 'Add comment',
- 'Delete comment',
- 'Read only comment'
- ].join(''));
- });
- it('should disable column manipulation when row header selected', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- colHeaders: true,
- rowHeaders: true,
- height: 100
- });
- $('.ht_clone_left .htCore').eq(0).find('tbody').find('th').eq(0).simulate('mousedown', {which: 3});
- contextMenu();
- expect($('.htContextMenu tbody td.htDisabled').text()).toBe([
- 'Insert column on the left',
- 'Insert column on the right',
- 'Remove column',
- 'Undo',
- 'Redo',
- ].join(''));
- });
- it('should disable row manipulation when column header selected', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- colHeaders: true,
- rowHeaders: true,
- height: 100
- });
- $('.ht_clone_top .htCore').find('thead').find('th').eq(2).simulate('mousedown', {which: 3});
- contextMenu();
- expect($('.htContextMenu tbody td.htDisabled').text()).toBe([
- 'Insert row above',
- 'Insert row below',
- 'Remove row',
- 'Undo',
- 'Redo',
- ].join(''));
- });
- it('should disable cells manipulation when corner header selected', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- colHeaders: true,
- rowHeaders: true,
- height: 100
- });
- $('.ht_clone_top_left_corner .htCore').find('thead').find('th').eq(0).simulate('mousedown', {which: 3});
- contextMenu();
- expect($('.htContextMenu tbody td.htDisabled').text()).toBe([
- 'Remove row',
- 'Remove column',
- 'Undo',
- 'Redo',
- 'Read only',
- 'Alignment',
- ].join(''));
- });
- it('should insert row above selection', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- height: 400
- });
- var afterCreateRowCallback = jasmine.createSpy('afterCreateRowCallback');
- hot.addHook('afterCreateRow', afterCreateRowCallback);
- expect(countRows()).toEqual(4);
- selectCell(1, 0, 3, 0);
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(0).simulate('mousedown'); // Insert row above
- expect(afterCreateRowCallback).toHaveBeenCalledWith(1, 1, 'ContextMenu.rowAbove', undefined, undefined, undefined);
- expect(countRows()).toEqual(5);
- expect($('.htContextMenu').is(':visible')).toBe(false);
- });
- it('should insert row above selection when initial data is empty', () => {
- var hot = handsontable({
- rowHeaders: true,
- colHeaders: true,
- data: [],
- dataSchema: [],
- contextMenu: true,
- height: 400
- });
- var afterCreateRowCallback = jasmine.createSpy('afterCreateRowCallback');
- hot.addHook('afterCreateRow', afterCreateRowCallback);
- expect(countRows()).toEqual(0);
- var cell = $('.ht_clone_top_left_corner .htCore').find('thead').find('th').eq(0);
- cell.simulate('mousedown', {which: 3});
- contextMenu(cell[0]);
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(0).simulate('mousedown'); // Insert row above
- expect(afterCreateRowCallback).toHaveBeenCalledWith(0, 1, 'ContextMenu.rowAbove', undefined, undefined, undefined);
- expect(countRows()).toEqual(1);
- expect($('.htContextMenu').is(':visible')).toBe(false);
- });
- it('should NOT display insert row selection', () => {
- var hot = handsontable({
- contextMenu: true,
- allowInsertRow: false
- });
- contextMenu();
- var items = $('.htContextMenu tbody td');
- var actions = items.not('.htSeparator');
- var separators = items.filter('.htSeparator');
- expect(actions.length).toEqual(8);
- expect(separators.length).toEqual(4);
- expect(actions.text()).toEqual([
- 'Insert column on the left',
- 'Insert column on the right',
- 'Remove row',
- 'Remove column',
- 'Undo',
- 'Redo',
- 'Read only',
- 'Alignment'
- ].join(''));
- });
- it('should NOT display insert column selection', () => {
- var hot = handsontable({
- contextMenu: true,
- allowInsertColumn: false
- });
- contextMenu();
- var items = $('.htContextMenu tbody td');
- var actions = items.not('.htSeparator');
- expect(actions.length).toEqual(8);
- expect(actions.text()).toEqual([
- 'Insert row above',
- 'Insert row below',
- 'Remove row',
- 'Remove column',
- 'Undo',
- 'Redo',
- 'Read only',
- 'Alignment'
- ].join(''));
- });
- it('should insert row above selection (reverse selection)', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- height: 100
- });
- var afterCreateRowCallback = jasmine.createSpy('afterCreateRowCallback');
- hot.addHook('afterCreateRow', afterCreateRowCallback);
- expect(countRows()).toEqual(4);
- selectCell(3, 0, 1, 0);
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(0).simulate('mousedown'); // Insert row above
- expect(afterCreateRowCallback).toHaveBeenCalledWith(1, 1, 'ContextMenu.rowAbove', undefined, undefined, undefined);
- expect(countRows()).toEqual(5);
- expect($('.htContextMenu').is(':visible')).toBe(false);
- });
- it('should insert row below selection', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- height: 100
- });
- var afterCreateRowCallback = jasmine.createSpy('afterCreateRowCallback');
- hot.addHook('afterCreateRow', afterCreateRowCallback);
- expect(countRows()).toEqual(4);
- selectCell(1, 0, 3, 0);
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(1).simulate('mousedown'); // Insert row above
- expect(afterCreateRowCallback).toHaveBeenCalledWith(4, 1, 'ContextMenu.rowBelow', undefined, undefined, undefined);
- expect(countRows()).toEqual(5);
- expect($('.htContextMenu').is(':visible')).toBe(false);
- });
- it('should insert row below selection when initial data is empty', () => {
- var hot = handsontable({
- rowHeaders: true,
- colHeaders: true,
- data: [],
- dataSchema: [],
- contextMenu: true,
- height: 400
- });
- var afterCreateRowCallback = jasmine.createSpy('afterCreateRowCallback');
- hot.addHook('afterCreateRow', afterCreateRowCallback);
- expect(countRows()).toEqual(0);
- var cell = $('.ht_clone_top_left_corner .htCore').find('thead').find('th').eq(0);
- cell.simulate('mousedown', {which: 3});
- contextMenu(cell[0]);
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(1).simulate('mousedown'); // Insert row below
- expect(afterCreateRowCallback).toHaveBeenCalledWith(0, 1, 'ContextMenu.rowBelow', undefined, undefined, undefined);
- expect(countRows()).toEqual(1);
- expect($('.htContextMenu').is(':visible')).toBe(false);
- });
- it('should insert row below selection (reverse selection)', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- height: 100
- });
- var afterCreateRowCallback = jasmine.createSpy('afterCreateRowCallback');
- hot.addHook('afterCreateRow', afterCreateRowCallback);
- expect(countRows()).toEqual(4);
- selectCell(3, 0, 1, 0);
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(1).simulate('mousedown'); // Insert row below
- expect(afterCreateRowCallback).toHaveBeenCalledWith(4, 1, 'ContextMenu.rowBelow', undefined, undefined, undefined);
- expect(countRows()).toEqual(5);
- expect($('.htContextMenu').is(':visible')).toBe(false);
- });
- it('should insert column on the left of selection', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- width: 400,
- height: 400
- });
- var afterCreateColCallback = jasmine.createSpy('afterCreateColCallback');
- hot.addHook('afterCreateCol', afterCreateColCallback);
- expect(countCols()).toEqual(4);
- selectCell(0, 1, 0, 3);
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(2).simulate('mousedown'); // Insert col left
- expect(afterCreateColCallback).toHaveBeenCalledWith(1, 1, 'ContextMenu.columnLeft', undefined, undefined, undefined);
- expect(countCols()).toEqual(5);
- expect($('.htContextMenu').is(':visible')).toBe(false);
- });
- it('should insert column on the left of selection when initial data is empty', () => {
- var hot = handsontable({
- rowHeaders: true,
- colHeaders: true,
- data: [],
- dataSchema: [],
- contextMenu: true,
- height: 400
- });
- var afterCreateColCallback = jasmine.createSpy('afterCreateColCallback');
- hot.addHook('afterCreateCol', afterCreateColCallback);
- expect(countCols()).toEqual(0);
- var cell = $('.ht_clone_top_left_corner .htCore').find('thead').find('th').eq(0);
- cell.simulate('mousedown', {which: 3});
- contextMenu(cell[0]);
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(3).simulate('mousedown'); // Insert column on the left
- expect(afterCreateColCallback).toHaveBeenCalledWith(0, 1, 'ContextMenu.columnRight', undefined, undefined, undefined);
- expect(countCols()).toEqual(1);
- expect($('.htContextMenu').is(':visible')).toBe(false);
- });
- it('should insert column on the left of selection (reverse selection)', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- height: 100
- });
- var afterCreateColCallback = jasmine.createSpy('afterCreateColCallback');
- hot.addHook('afterCreateCol', afterCreateColCallback);
- expect(countCols()).toEqual(4);
- selectCell(0, 3, 0, 1);
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(2).simulate('mousedown'); // Insert col left
- expect(afterCreateColCallback).toHaveBeenCalledWith(1, 1, 'ContextMenu.columnLeft', undefined, undefined, undefined);
- expect(countCols()).toEqual(5);
- expect($('.htContextMenu').is(':visible')).toBe(false);
- });
- it('should insert column on the right of selection', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- height: 100
- });
- var afterCreateColCallback = jasmine.createSpy('afterCreateColCallback');
- hot.addHook('afterCreateCol', afterCreateColCallback);
- expect(countCols()).toEqual(4);
- selectCell(0, 1, 0, 3);
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(2).simulate('mousedown'); // Insert col right
- expect(afterCreateColCallback).toHaveBeenCalledWith(1, 1, 'ContextMenu.columnLeft', undefined, undefined, undefined);
- expect(countCols()).toEqual(5);
- expect($('.htContextMenu').is(':visible')).toBe(false);
- });
- it('should insert column on the right of selection when initial data is empty', () => {
- var hot = handsontable({
- rowHeaders: true,
- colHeaders: true,
- data: [],
- dataSchema: [],
- contextMenu: true,
- height: 400
- });
- var afterCreateColCallback = jasmine.createSpy('afterCreateColCallback');
- hot.addHook('afterCreateCol', afterCreateColCallback);
- expect(countCols()).toEqual(0);
- var cell = $('.ht_clone_top_left_corner .htCore').find('thead').find('th').eq(0);
- cell.simulate('mousedown', {which: 3});
- contextMenu(cell[0]);
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(3).simulate('mousedown'); // Insert column on the right
- expect(afterCreateColCallback).toHaveBeenCalledWith(0, 1, 'ContextMenu.columnRight', undefined, undefined, undefined);
- expect(countCols()).toEqual(1);
- expect($('.htContextMenu').is(':visible')).toBe(false);
- });
- it('should insert column on the right of selection (reverse selection)', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- height: 100
- });
- var afterCreateColCallback = jasmine.createSpy('afterCreateColCallback');
- hot.addHook('afterCreateCol', afterCreateColCallback);
- expect(countCols()).toEqual(4);
- selectCell(0, 3, 0, 1);
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(3).simulate('mousedown'); // Insert col right
- expect(afterCreateColCallback).toHaveBeenCalledWith(4, 1, 'ContextMenu.columnRight', undefined, undefined, undefined);
- expect(countCols()).toEqual(5);
- expect($('.htContextMenu').is(':visible')).toBe(false);
- });
- it('should remove selected rows', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- height: 100
- });
- var afterRemoveRowCallback = jasmine.createSpy('afterRemoveRowCallback');
- hot.addHook('afterRemoveRow', afterRemoveRowCallback);
- expect(countRows()).toEqual(4);
- selectCell(1, 0, 3, 0);
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(4).simulate('mousedown'); // Remove row
- expect(afterRemoveRowCallback).toHaveBeenCalledWith(1, 3, [1, 2, 3], 'ContextMenu.removeRow', undefined, undefined);
- expect(countRows()).toEqual(1);
- expect($('.htContextMenu').is(':visible')).toBe(false);
- });
- it('should allow to remove the latest row', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(1, 4),
- contextMenu: true,
- height: 100
- });
- var afterRemoveRowCallback = jasmine.createSpy('afterRemoveRowCallback');
- hot.addHook('afterRemoveRow', afterRemoveRowCallback);
- expect(countRows()).toBe(1);
- selectCell(0, 0, 0, 0);
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(4).simulate('mousedown'); // Remove row
- expect(afterRemoveRowCallback).toHaveBeenCalledWith(0, 1, [0], 'ContextMenu.removeRow', undefined, undefined);
- expect(countRows()).toBe(0);
- expect($('.htContextMenu').is(':visible')).toBe(false);
- });
- it('should remove selected rows (reverse selection)', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- height: 100
- });
- var afterRemoveRowCallback = jasmine.createSpy('afterRemoveRowCallback');
- hot.addHook('afterRemoveRow', afterRemoveRowCallback);
- expect(countRows()).toBe(4);
- selectCell(3, 0, 1, 0);
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(4).simulate('mousedown'); // Remove row
- expect(afterRemoveRowCallback).toHaveBeenCalledWith(1, 3, [1, 2, 3], 'ContextMenu.removeRow', undefined, undefined);
- expect(countRows()).toBe(1);
- expect($('.htContextMenu').is(':visible')).toBe(false);
- });
- it('should remove selected columns', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- height: 100
- });
- var afterRemoveColCallback = jasmine.createSpy('afterRemoveColCallback');
- hot.addHook('afterRemoveCol', afterRemoveColCallback);
- expect(countCols()).toBe(4);
- selectCell(0, 1, 0, 3);
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(5).simulate('mousedown'); // Remove col
- expect(afterRemoveColCallback).toHaveBeenCalledWith(1, 3, [1, 2, 3], 'ContextMenu.removeColumn', undefined, undefined);
- expect(countCols()).toBe(1);
- expect($('.htContextMenu').is(':visible')).toBe(false);
- });
- it('should allow to remove the latest column', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 1),
- contextMenu: true,
- height: 100
- });
- var afterRemoveColCallback = jasmine.createSpy('afterRemoveColCallback');
- hot.addHook('afterRemoveCol', afterRemoveColCallback);
- expect(countCols()).toBe(1);
- selectCell(0, 0, 0, 0);
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(5).simulate('mousedown'); // Remove column
- expect(afterRemoveColCallback).toHaveBeenCalledWith(0, 1, [0], 'ContextMenu.removeColumn', undefined, undefined);
- expect(countCols()).toBe(0);
- expect($('.htContextMenu').is(':visible')).toBe(false);
- });
- it('should remove selected columns (reverse selection)', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- height: 100
- });
- var afterRemoveColCallback = jasmine.createSpy('afterRemoveColCallback');
- hot.addHook('afterRemoveCol', afterRemoveColCallback);
- expect(countCols()).toEqual(4);
- selectCell(0, 3, 0, 1);
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(5).simulate('mousedown'); // Remove col
- expect(afterRemoveColCallback).toHaveBeenCalledWith(1, 3, [1, 2, 3], 'ContextMenu.removeColumn', undefined, undefined);
- expect(countCols()).toEqual(1);
- });
- it('should undo changes', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- height: 100
- });
- selectCell(0, 0);
- expect(getDataAtCell(0, 0)).toEqual('A1');
- setDataAtCell(0, 0, 'XX');
- expect(getDataAtCell(0, 0)).toEqual('XX');
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(6).simulate('mousedown'); // Undo
- expect(getDataAtCell(0, 0)).toEqual('A1');
- });
- it('should redo changes', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- height: 100
- });
- selectCell(0, 0);
- expect(getDataAtCell(0, 0)).toEqual('A1');
- setDataAtCell(0, 0, 'XX');
- expect(getDataAtCell(0, 0)).toEqual('XX');
- hot.undo();
- expect(getDataAtCell(0, 0)).toEqual('A1');
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(7).simulate('mousedown'); // Redo
- expect(getDataAtCell(0, 0)).toEqual('XX');
- });
- it('should display only the specified actions', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: ['remove_row', 'undo'],
- height: 100
- });
- contextMenu();
- expect($('.htContextMenu .ht_master .htCore').find('tbody td').length).toEqual(2);
- });
- it('should make a single selected cell read-only', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- height: 100
- });
- selectCell(0, 0);
- expect(getDataAtCell(0, 0)).toEqual('A1');
- expect(hot.getCellMeta(0, 0).readOnly).toBe(false);
- selectCell(0, 0);
- contextMenu();
- var menu = $('.htContextMenu .ht_master .htCore tbody');
- menu.find('td').not('.htSeparator').eq(8).simulate('mousedown'); // Make read-only
- expect(hot.getCellMeta(0, 0).readOnly).toBe(true);
- });
- it('should make a single selected cell writable, when it\'s set to read-only', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- height: 100
- });
- selectCell(0, 0);
- expect(getDataAtCell(0, 0)).toEqual('A1');
- hot.getCellMeta(0, 0).readOnly = true;
- selectCell(0, 0);
- contextMenu();
- var menu = $('.htContextMenu .ht_master .htCore tbody');
- menu.find('td').not('.htSeparator').eq(8).simulate('mousedown');
- // $(hot.contextMenu.menu).find('tbody td').not('.htSeparator').eq(8).trigger('mousedown'); //Make writable
- expect(hot.getCellMeta(0, 0).readOnly).toBe(false);
- });
- it('should make a group of selected cells read-only, if all of them are writable', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- height: 100
- });
- for (let i = 0; i < 2; i++) {
- for (let j = 0; j < 2; j++) {
- expect(hot.getCellMeta(i, j).readOnly).toEqual(false);
- }
- }
- selectCell(0, 0, 2, 2);
- contextMenu();
- var menu = $('.htContextMenu .ht_master .htCore tbody');
- menu.find('td').not('.htSeparator').eq(8).simulate('mousedown');
- // $(hot.contextMenu.menu).find('tbody td').not('.htSeparator').eq(8).trigger('mousedown'); //Make read-only
- for (let i = 0; i < 2; i++) {
- for (let j = 0; j < 2; j++) {
- expect(hot.getCellMeta(i, j).readOnly).toEqual(true);
- }
- }
- });
- it('should not close menu after clicking on submenu root item', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: ['row_above', 'remove_row', '---------', 'alignment'],
- height: 400
- });
- selectCell(1, 0, 3, 0);
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(2).simulate('mousedown'); // Alignment
- expect($('.htContextMenu').is(':visible')).toBe(true);
- });
- it('should make a group of selected cells read-only, if all of them are writable (reverse selection)', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- height: 100
- });
- for (let i = 0; i < 2; i++) {
- for (let j = 0; j < 2; j++) {
- expect(hot.getCellMeta(i, j).readOnly).toEqual(false);
- }
- }
- selectCell(2, 2, 0, 0);
- contextMenu();
- var menu = $('.htContextMenu .ht_master .htCore tbody');
- menu.find('td').not('.htSeparator').eq(8).simulate('mousedown'); // Make read-only
- for (let i = 0; i < 2; i++) {
- for (let j = 0; j < 2; j++) {
- expect(hot.getCellMeta(i, j).readOnly).toEqual(true);
- }
- }
- });
- it('should make a group of selected cells writable if at least one of them is read-only', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- height: 100
- });
- for (let i = 0; i < 2; i++) {
- for (let j = 0; j < 2; j++) {
- expect(hot.getCellMeta(i, j).readOnly).toEqual(false);
- }
- }
- hot.getCellMeta(1, 1).readOnly = true;
- selectCell(0, 0, 2, 2);
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(8).simulate('mousedown'); // Make writable
- for (let i = 0; i < 2; i++) {
- for (let j = 0; j < 2; j++) {
- expect(hot.getCellMeta(i, j).readOnly).toEqual(false);
- }
- }
- });
- it('should make a group of selected cells writable if at least one of them is read-only (reverse selection)', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: true,
- height: 100
- });
- for (let i = 0; i < 2; i++) {
- for (let j = 0; j < 2; j++) {
- expect(hot.getCellMeta(i, j).readOnly).toEqual(false);
- }
- }
- hot.getCellMeta(1, 1).readOnly = true;
- selectCell(2, 2, 0, 0);
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(8).simulate('mousedown'); // Make writable
- for (let i = 0; i < 2; i++) {
- for (let j = 0; j < 2; j++) {
- expect(hot.getCellMeta(i, j).readOnly).toEqual(false);
- }
- }
- });
- });
- describe('disabling actions', () => {
- it('should not close menu after clicking on disabled item', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(4, 4),
- contextMenu: ['undo', 'redo'],
- height: 400
- });
- selectCell(1, 0, 3, 0);
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(0).simulate('mousedown'); // Undo
- expect($('.htContextMenu').is(':visible')).toBe(true);
- });
- it('should disable undo and redo action if undoRedo plugin is not enabled ', () => {
- var hot = handsontable({
- contextMenu: true,
- undoRedo: false,
- height: 100
- });
- contextMenu();
- var $menu = $('.htContextMenu .ht_master .htCore');
- expect($menu.find('tbody td:eq(9)').text()).toEqual('Undo');
- expect($menu.find('tbody td:eq(9)').hasClass('htDisabled')).toBe(true);
- expect($menu.find('tbody td:eq(10)').text()).toEqual('Redo');
- expect($menu.find('tbody td:eq(10)').hasClass('htDisabled')).toBe(true);
- });
- it('should disable undo when there is nothing to undo ', () => {
- var hot = handsontable({
- contextMenu: true,
- height: 100
- });
- contextMenu();
- var $menu = $('.htContextMenu .ht_master .htCore');
- expect(hot.undoRedo.isUndoAvailable()).toBe(false);
- expect($menu.find('tbody td:eq(9)').text()).toEqual('Undo');
- expect($menu.find('tbody td:eq(9)').hasClass('htDisabled')).toBe(true);
- closeContextMenu();
- setDataAtCell(0, 0, 'foo');
- contextMenu();
- $menu = $('.htContextMenu .ht_master .htCore');
- expect(hot.undoRedo.isUndoAvailable()).toBe(true);
- expect($menu.find('tbody td:eq(9)').hasClass('htDisabled')).toBe(false);
- });
- it('should disable redo when there is nothing to redo ', () => {
- var hot = handsontable({
- contextMenu: true,
- height: 100
- });
- contextMenu();
- var $menu = $('.htContextMenu .ht_master .htCore');
- expect(hot.undoRedo.isRedoAvailable()).toBe(false);
- expect($menu.find('tbody td:eq(10)').text()).toEqual('Redo');
- expect($menu.find('tbody td:eq(10)').hasClass('htDisabled')).toBe(true);
- closeContextMenu();
- setDataAtCell(0, 0, 'foo');
- hot.undo();
- contextMenu();
- $menu = $('.htContextMenu .ht_master .htCore');
- expect(hot.undoRedo.isRedoAvailable()).toBe(true);
- expect($menu.find('tbody td:eq(10)').hasClass('htDisabled')).toBe(false);
- });
- it('should disable Insert row in context menu when maxRows is reached', () => {
- var hot = handsontable({
- contextMenu: true,
- maxRows: 6,
- height: 100
- });
- contextMenu();
- var $menu = $('.htContextMenu .ht_master .htCore');
- expect($menu.find('tbody td:eq(0)').text()).toEqual('Insert row above');
- expect($menu.find('tbody td:eq(0)').hasClass('htDisabled')).toBe(false);
- expect($menu.find('tbody td:eq(1)').text()).toEqual('Insert row below');
- expect($menu.find('tbody td:eq(1)').hasClass('htDisabled')).toBe(false);
- closeContextMenu();
- alter('insert_row');
- contextMenu();
- $menu = $('.htContextMenu .ht_master .htCore');
- expect($menu.find('tbody td:eq(0)').hasClass('htDisabled')).toBe(true);
- expect($menu.find('tbody td:eq(1)').hasClass('htDisabled')).toBe(true);
- });
- it('should disable Insert col in context menu when maxCols is reached', () => {
- var hot = handsontable({
- contextMenu: true,
- maxCols: 6,
- height: 100
- });
- contextMenu();
- var $menu = $('.htContextMenu .ht_master .htCore');
- expect($menu.find('tbody td:eq(3)').text()).toEqual('Insert column on the left');
- expect($menu.find('tbody td:eq(3)').hasClass('htDisabled')).toBe(false);
- expect($menu.find('tbody td:eq(4)').text()).toEqual('Insert column on the right');
- expect($menu.find('tbody td:eq(4)').hasClass('htDisabled')).toBe(false);
- closeContextMenu();
- alter('insert_col');
- contextMenu();
- $menu = $('.htContextMenu .ht_master .htCore');
- expect($menu.find('tbody td:eq(3)').hasClass('htDisabled')).toBe(true);
- expect($menu.find('tbody td:eq(4)').hasClass('htDisabled')).toBe(true);
- });
- it('should NOT disable Insert col in context menu when only one column exists', () => {
- var hot = handsontable({
- data: [['single col']],
- contextMenu: true,
- maxCols: 10,
- height: 100
- });
- selectCell(0, 0);
- contextMenu();
- var $menu = $('.htContextMenu .ht_master .htCore');
- expect($menu.find('tbody td:eq(3)').text()).toEqual('Insert column on the left');
- expect($menu.find('tbody td:eq(3)').hasClass('htDisabled')).toBe(false);
- expect($menu.find('tbody td:eq(4)').text()).toEqual('Insert column on the right');
- expect($menu.find('tbody td:eq(4)').hasClass('htDisabled')).toBe(false);
- });
- it('should disable Remove col in context menu when rows are selected by headers', function() {
- var hot = handsontable({
- contextMenu: ['remove_col', 'remove_row'],
- height: 100,
- colHeaders: true,
- rowHeaders: true
- });
- var $rowsHeaders = this.$container.find('.ht_clone_left tr th');
- $rowsHeaders.eq(1).simulate('mousedown');
- $rowsHeaders.eq(2).simulate('mouseover');
- $rowsHeaders.eq(3).simulate('mouseover');
- $rowsHeaders.eq(3).simulate('mousemove');
- $rowsHeaders.eq(3).simulate('mouseup');
- contextMenu();
- var $menu = $('.htContextMenu .ht_master .htCore');
- expect($menu.find('tbody td:eq(0)').text()).toEqual('Remove column');
- expect($menu.find('tbody td:eq(0)').hasClass('htDisabled')).toBe(true);
- });
- it('should disable Remove row in context menu when columns are selected by headers', function() {
- var hot = handsontable({
- contextMenu: ['remove_col', 'remove_row'],
- height: 100,
- colHeaders: true,
- rowHeaders: true
- });
- this.$container.find('thead tr:eq(0) th:eq(1)').simulate('mousedown');
- this.$container.find('thead tr:eq(0) th:eq(2)').simulate('mouseover');
- this.$container.find('thead tr:eq(0) th:eq(3)').simulate('mouseover');
- this.$container.find('thead tr:eq(0) th:eq(3)').simulate('mousemove');
- this.$container.find('thead tr:eq(0) th:eq(3)').simulate('mouseup');
- contextMenu();
- var $menu = $('.htContextMenu .ht_master .htCore');
- expect($menu.find('tbody td:eq(1)').text()).toEqual('Remove row');
- expect($menu.find('tbody td:eq(1)').hasClass('htDisabled')).toBe(true);
- });
- });
- describe('custom options', () => {
- it('should have custom items list', () => {
- var callback1 = jasmine.createSpy('callback1');
- var callback2 = jasmine.createSpy('callback2');
- var hot = handsontable({
- contextMenu: {
- items: {
- cust1: {
- name: 'CustomItem1',
- callback: callback1
- },
- cust2: {
- name: 'CustomItem2',
- callback: callback2
- }
- }
- },
- height: 100
- });
- contextMenu();
- expect($('.htContextMenu .ht_master .htCore').find('tbody td').length).toEqual(2);
- expect($('.htContextMenu .ht_master .htCore').find('tbody td').text()).toEqual(['CustomItem1', 'CustomItem2'].join(''));
- $('.htContextMenu .ht_master .htCore').find('tbody td:eq(0)').simulate('mousedown');
- expect(callback1.calls.count()).toEqual(1);
- expect(callback2.calls.count()).toEqual(0);
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td:eq(1)').simulate('mousedown');
- expect(callback1.calls.count()).toEqual(1);
- expect(callback2.calls.count()).toEqual(1);
- });
- it('should have custom items list (defined as a function)', () => {
- var enabled = false;
- var hot = handsontable({
- contextMenu: {
- items: {
- cust1: {
- name() {
- if (!enabled) {
- return 'Enable my custom option';
- }
- return 'Disable my custom option';
- },
- callback() {
- }
- }
- }
- },
- height: 100
- });
- contextMenu();
- expect($('.htContextMenu .ht_master .htCore').find('tbody td').text()).toEqual('Enable my custom option');
- $('.htContextMenu .ht_master .htCore').find('tbody td:eq(0)').simulate('mousedown');
- enabled = true;
- contextMenu();
- expect($('.htContextMenu .ht_master .htCore').find('tbody td').text()).toEqual('Disable my custom option');
- $('.htContextMenu .ht_master .htCore').find('tbody td:eq(0)').simulate('mousedown');
- });
- it('should enable to define item options globally', () => {
- var callback = jasmine.createSpy('callback');
- var hot = handsontable({
- contextMenu: {
- callback,
- items: {
- cust1: {
- name: 'CustomItem1'
- },
- cust2: {
- name: 'CustomItem2'
- }
- }
- },
- height: 100
- });
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td:eq(0)').simulate('mousedown');
- expect(callback.calls.count()).toEqual(1);
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td:eq(1)').simulate('mousedown');
- expect(callback.calls.count()).toEqual(2);
- });
- it('should override default items options', () => {
- var callback = jasmine.createSpy('callback');
- var hot = handsontable({
- contextMenu: {
- items: {
- remove_row: {
- callback
- },
- remove_col: {
- name: 'Delete column'
- }
- }
- },
- height: 100
- });
- contextMenu();
- expect($('.htContextMenu .ht_master .htCore').find('tbody td').length).toEqual(2);
- expect($('.htContextMenu .ht_master .htCore').find('tbody td').text()).toEqual(['Remove row', 'Delete column'].join(''));
- $('.htContextMenu .ht_master .htCore').find('tbody td:eq(0)').simulate('mousedown');
- expect(callback.calls.count()).toEqual(1);
- expect(countCols()).toEqual(5);
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td:eq(1)').simulate('mousedown');
- expect(countCols()).toEqual(4);
- });
- it('should fire item callback after item has been clicked', () => {
- var customItem = {
- name: 'Custom item',
- callback() {}
- };
- spyOn(customItem, 'callback');
- var hot = handsontable({
- contextMenu: {
- items: {
- customItemKey: customItem
- }
- },
- height: 100
- });
- contextMenu();
- $('.htContextMenu .ht_master .htCore').find('tbody td:eq(0)').simulate('mousedown');
- expect(customItem.callback.calls.count()).toEqual(1);
- expect(customItem.callback.calls.argsFor(0)[0]).toEqual('customItemKey');
- });
- });
- describe('keyboard navigation', () => {
- describe('no item selected', () => {
- it('should select the first item in menu, when user hits ARROW_DOWN', () => {
- var hot = handsontable({
- contextMenu: true,
- height: 100
- });
- contextMenu();
- var menuHot = hot.getPlugin('contextMenu').menu.hotMenu;
- expect(menuHot.getSelected()).toBeUndefined();
- keyDownUp('arrow_down');
- expect(menuHot.getSelected()).toEqual([0, 0, 0, 0]);
- });
- it('should scroll down, when user hits ARROW_DOWN for item in menu below the viewport', () => {
- var hot = handsontable({
- height: 100,
- contextMenu: {
- items: {
- item1: {
- name: 'Item1'
- },
- item2: {
- name: 'Item2'
- },
- item3: {
- name: 'Item3'
- },
- item4: {
- name: 'Item4'
- },
- item5: {
- name: 'Item5'
- },
- item6: {
- name: 'Item6'
- },
- item7: {
- name: 'Item7'
- },
- item8: {
- name: 'Item8'
- },
- item9: {
- name: 'Item9'
- },
- item10: {
- name: 'Item10'
- },
- item11: {
- name: 'Item11'
- },
- item12: {
- name: 'Item12'
- },
- item13: {
- name: 'Item13'
- },
- item14: {
- name: 'Item14'
- },
- item15: {
- name: 'Item15'
- },
- item16: {
- name: 'Item16'
- },
- item17: {
- name: 'Item17'
- },
- item18: {
- name: 'Item18'
- },
- item19: {
- name: 'Item19'
- },
- item20: {
- name: 'Item20'
- },
- item21: {
- name: 'Item21'
- },
- item22: {
- name: 'Item22'
- },
- item23: {
- name: 'Item23'
- },
- item24: {
- name: 'Item24'
- },
- item25: {
- name: 'Item25'
- },
- item26: {
- name: 'Item26'
- },
- item27: {
- name: 'Item27'
- },
- item28: {
- name: 'Item28'
- },
- item29: {
- name: 'Item29'
- },
- item30: {
- name: 'Item30'
- },
- item31: {
- name: 'Item31'
- },
- item32: {
- name: 'Item32'
- },
- item33: {
- name: 'Item33'
- },
- item34: {
- name: 'Item34'
- },
- item35: {
- name: 'Item35'
- },
- item36: {
- name: 'Item36'
- },
- item37: {
- name: 'Item37'
- },
- item38: {
- name: 'Item38'
- },
- item39: {
- name: 'Item39'
- },
- item40: {
- name: 'Item40'
- }
- }
- }
- }),
- scrollHeight;
- contextMenu();
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- keyDownUp('arrow_down');
- if (typeof window.scrollY !== 'undefined') {
- scrollHeight = window.scrollY;
- } else {
- scrollHeight = document.documentElement.scrollTop;
- }
- expect(scrollHeight).not.toBe(0);
- });
- it('should select the first NOT DISABLED item in menu, when user hits ARROW_DOWN', () => {
- var hot = handsontable({
- contextMenu: {
- items: {
- item1: {
- name: 'Item1',
- disabled: true
- },
- item2: {
- name: 'Item2',
- disabled: true
- },
- item3: {
- name: 'Item3'
- }
- }
- },
- height: 100
- });
- contextMenu();
- var menuHot = hot.getPlugin('contextMenu').menu.hotMenu;
- expect(menuHot.getSelected()).toBeUndefined();
- keyDownUp('arrow_down');
- expect(menuHot.getSelected()).toEqual([2, 0, 2, 0]);
- });
- it('should NOT select any items in menu, when user hits ARROW_DOWN and there is no items enabled', () => {
- var hot = handsontable({
- contextMenu: {
- items: {
- item1: {
- name: 'Item1',
- disabled: true
- },
- item2: {
- name: 'Item2',
- disabled: true
- },
- item3: {
- name: 'Item3',
- disabled: true
- }
- }
- },
- height: 100
- });
- contextMenu();
- var menuHot = hot.getPlugin('contextMenu').menu.hotMenu;
- expect(menuHot.getSelected()).toBeUndefined();
- keyDownUp('arrow_down');
- expect(menuHot.getSelected()).toBeUndefined();
- });
- it('should select the last item in menu, when user hits ARROW_UP', () => {
- var hot = handsontable({
- contextMenu: {
- items: {
- item1: 'Item1',
- item2: 'Item2',
- item3: 'Item3'
- }
- },
- height: 100
- });
- contextMenu();
- var menuHot = hot.getPlugin('contextMenu').menu.hotMenu;
- expect(menuHot.getSelected()).toBeUndefined();
- keyDownUp('arrow_up');
- expect(menuHot.getSelected()).toEqual([2, 0, 2, 0]);
- });
- it('should select the last NOT DISABLED item in menu, when user hits ARROW_UP', () => {
- var hot = handsontable({
- contextMenu: {
- items: {
- item1: {
- name: 'Item1'
- },
- item2: {
- name: 'Item2',
- disabled: true
- },
- item3: {
- name: 'Item3',
- disabled: true
- }
- }
- },
- height: 100
- });
- contextMenu();
- var menuHot = hot.getPlugin('contextMenu').menu.hotMenu;
- expect(menuHot.getSelected()).toBeUndefined();
- keyDownUp('arrow_up');
- expect(menuHot.getSelected()).toEqual([0, 0, 0, 0]);
- });
- it('should NOT select any items in menu, when user hits ARROW_UP and there is no items enabled', () => {
- var hot = handsontable({
- contextMenu: {
- items: {
- item1: {
- name: 'Item1',
- disabled: true
- },
- item2: {
- name: 'Item2',
- disabled: true
- },
- item3: {
- name: 'Item3',
- disabled: true
- }
- }
- },
- height: 100
- });
- contextMenu();
- var id = $('.htContextMenu')[0].id;
- var menuHot = hot.getPlugin('contextMenu').menu.hotMenu;
- expect(menuHot.getSelected()).toBeUndefined();
- keyDownUp('arrow_up');
- expect(menuHot.getSelected()).toBeUndefined();
- });
- });
- describe('item selected', () => {
- it('should select next item when user hits ARROW_DOWN', () => {
- var hot = handsontable({
- contextMenu: {
- items: {
- item1: {
- name: 'Item1'
- },
- item2: {
- name: 'Item2'
- },
- item3: {
- name: 'Item3'
- }
- }
- },
- height: 100
- });
- contextMenu();
- var menuHot = hot.getPlugin('contextMenu').menu.hotMenu;
- keyDownUp('arrow_down');
- expect(menuHot.getSelected()).toEqual([0, 0, 0, 0]);
- keyDownUp('arrow_down');
- expect(menuHot.getSelected()).toEqual([1, 0, 1, 0]);
- keyDownUp('arrow_down');
- expect(menuHot.getSelected()).toEqual([2, 0, 2, 0]);
- });
- it('should select next item (skipping disabled items) when user hits ARROW_DOWN', () => {
- var hot = handsontable({
- contextMenu: {
- items: {
- item1: {
- name: 'Item1'
- },
- item2: {
- name: 'Item2',
- disabled: true
- },
- item3: {
- name: 'Item3'
- }
- }
- },
- height: 100
- });
- contextMenu();
- var menuHot = hot.getPlugin('contextMenu').menu.hotMenu;
- keyDownUp('arrow_down');
- expect(menuHot.getSelected()).toEqual([0, 0, 0, 0]);
- keyDownUp('arrow_down');
- expect(menuHot.getSelected()).toEqual([2, 0, 2, 0]);
- });
- it('should select next item (skipping separators) when user hits ARROW_DOWN', () => {
- var hot = handsontable({
- contextMenu: {
- items: {
- item1: {
- name: 'Item1'
- },
- sep1: Handsontable.plugins.ContextMenu.SEPARATOR,
- item2: {
- name: 'Item2'
- },
- item3: {
- name: 'Item3'
- }
- }
- },
- height: 100
- });
- contextMenu();
- var menuHot = hot.getPlugin('contextMenu').menu.hotMenu;
- keyDownUp('arrow_down');
- expect(menuHot.getSelected()).toEqual([0, 0, 0, 0]);
- keyDownUp('arrow_down');
- expect(menuHot.getSelected()).toEqual([2, 0, 2, 0]);
- keyDownUp('arrow_down');
- expect(menuHot.getSelected()).toEqual([3, 0, 3, 0]);
- });
- it('should not change selection when last item is selected and user hits ARROW_DOWN', () => {
- var hot = handsontable({
- contextMenu: {
- items: {
- item1: {
- name: 'Item1'
- },
- item2: {
- name: 'Item2'
- },
- item3: {
- name: 'Item3'
- }
- }
- },
- height: 100
- });
- contextMenu();
- var menuHot = hot.getPlugin('contextMenu').menu.hotMenu;
- keyDownUp('arrow_down');
- expect(menuHot.getSelected()).toEqual([0, 0, 0, 0]);
- keyDownUp('arrow_down');
- expect(menuHot.getSelected()).toEqual([1, 0, 1, 0]);
- keyDownUp('arrow_down');
- expect(menuHot.getSelected()).toEqual([2, 0, 2, 0]);
- keyDownUp('arrow_down');
- expect(menuHot.getSelected()).toEqual([2, 0, 2, 0]);
- });
- it('should not change selection when last enabled item is selected and user hits ARROW_DOWN', () => {
- var hot = handsontable({
- contextMenu: {
- items: {
- item1: {
- name: 'Item1'
- },
- item2: {
- name: 'Item2'
- },
- item3: {
- name: 'Item3',
- disabled: true
- }
- }
- },
- height: 100
- });
- contextMenu();
- var menuHot = hot.getPlugin('contextMenu').menu.hotMenu;
- keyDownUp('arrow_down');
- expect(menuHot.getSelected()).toEqual([0, 0, 0, 0]);
- keyDownUp('arrow_down');
- expect(menuHot.getSelected()).toEqual([1, 0, 1, 0]);
- keyDownUp('arrow_down');
- expect(menuHot.getSelected()).toEqual([1, 0, 1, 0]);
- });
- it('should select next item when user hits ARROW_UP', () => {
- var hot = handsontable({
- contextMenu: {
- items: {
- item1: {
- name: 'Item1'
- },
- item2: {
- name: 'Item2'
- },
- item3: {
- name: 'Item3'
- }
- }
- },
- height: 100
- });
- contextMenu();
- var menuHot = hot.getPlugin('contextMenu').menu.hotMenu;
- keyDownUp('arrow_up');
- expect(menuHot.getSelected()).toEqual([2, 0, 2, 0]);
- keyDownUp('arrow_up');
- expect(menuHot.getSelected()).toEqual([1, 0, 1, 0]);
- keyDownUp('arrow_up');
- expect(menuHot.getSelected()).toEqual([0, 0, 0, 0]);
- });
- it('should select next item (skipping disabled items) when user hits ARROW_UP', () => {
- var hot = handsontable({
- contextMenu: {
- items: {
- item1: {
- name: 'Item1'
- },
- item2: {
- name: 'Item2',
- disabled: true
- },
- item3: {
- name: 'Item3'
- }
- }
- },
- height: 100
- });
- contextMenu();
- var menuHot = hot.getPlugin('contextMenu').menu.hotMenu;
- keyDownUp('arrow_up');
- expect(menuHot.getSelected()).toEqual([2, 0, 2, 0]);
- keyDownUp('arrow_up');
- expect(menuHot.getSelected()).toEqual([0, 0, 0, 0]);
- });
- it('should select next item (skipping separators) when user hits ARROW_UP', () => {
- var hot = handsontable({
- contextMenu: {
- items: {
- item1: {
- name: 'Item1'
- },
- sep1: Handsontable.plugins.ContextMenu.SEPARATOR,
- item2: {
- name: 'Item2'
- },
- item3: {
- name: 'Item3'
- }
- }
- },
- height: 100
- });
- contextMenu();
- var menuHot = hot.getPlugin('contextMenu').menu.hotMenu;
- keyDownUp('arrow_up');
- expect(menuHot.getSelected()).toEqual([3, 0, 3, 0]);
- keyDownUp('arrow_up');
- expect(menuHot.getSelected()).toEqual([2, 0, 2, 0]);
- keyDownUp('arrow_up');
- expect(menuHot.getSelected()).toEqual([0, 0, 0, 0]);
- });
- it('should not change selection when first item is selected and user hits ARROW_UP', () => {
- var hot = handsontable({
- contextMenu: {
- items: {
- item1: {
- name: 'Item1'
- },
- item2: {
- name: 'Item2'
- },
- item3: {
- name: 'Item3'
- }
- }
- },
- height: 100
- });
- contextMenu();
- var menuHot = hot.getPlugin('contextMenu').menu.hotMenu;
- keyDownUp('arrow_up');
- expect(menuHot.getSelected()).toEqual([2, 0, 2, 0]);
- keyDownUp('arrow_up');
- expect(menuHot.getSelected()).toEqual([1, 0, 1, 0]);
- keyDownUp('arrow_up');
- expect(menuHot.getSelected()).toEqual([0, 0, 0, 0]);
- keyDownUp('arrow_up');
- expect(menuHot.getSelected()).toEqual([0, 0, 0, 0]);
- });
- it('should not change selection when first enabled item is selected and user hits ARROW_UP', () => {
- var hot = handsontable({
- contextMenu: {
- items: {
- item1: {
- name: 'Item1',
- disabled: true
- },
- item2: {
- name: 'Item2'
- },
- item3: {
- name: 'Item3'
- }
- }
- },
- height: 100
- });
- contextMenu();
- var menuHot = hot.getPlugin('contextMenu').menu.hotMenu;
- keyDownUp('arrow_up');
- expect(menuHot.getSelected()).toEqual([2, 0, 2, 0]);
- keyDownUp('arrow_up');
- expect(menuHot.getSelected()).toEqual([1, 0, 1, 0]);
- keyDownUp('arrow_up');
- expect(menuHot.getSelected()).toEqual([1, 0, 1, 0]);
- });
- it('should perform a selected item action, when user hits ENTER', () => {
- var itemAction = jasmine.createSpy('itemAction');
- var hot = handsontable({
- contextMenu: {
- items: {
- item1: {
- name: 'Item1',
- callback: itemAction
- },
- item2: 'Item2'
- }
- },
- height: 100
- });
- contextMenu();
- var menuHot = hot.getPlugin('contextMenu').menu.hotMenu;
- keyDownUp('arrow_down');
- expect(menuHot.getSelected()).toEqual([0, 0, 0, 0]);
- expect(itemAction).not.toHaveBeenCalled();
- keyDownUp('enter');
- expect(itemAction).toHaveBeenCalled();
- expect($(hot.getPlugin('contextMenu').menu).is(':visible')).toBe(false);
- });
- });
- it('should close menu when user hits ESC', () => {
- handsontable({
- contextMenu: true,
- height: 100
- });
- contextMenu();
- expect($('.htContextMenu').is(':visible')).toBe(true);
- keyDownUp('esc');
- expect($('.htContextMenu').is(':visible')).toBe(false);
- });
- it('should close sub-menu and parent menu in proper order when user hits ESC twice', (done) => {
- handsontable({
- contextMenu: true,
- height: 100
- });
- contextMenu();
- var item = $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(9);
- var contextMenuRoot = $('.htContextMenu');
- item.simulate('mouseover');
- setTimeout(() => {
- expect($('.htContextMenuSub_Alignment').is(':visible')).toBe(true);
- keyDownUp('esc');
- expect($('.htContextMenuSub_Alignment').is(':visible')).toBe(false);
- keyDownUp('esc');
- expect($('.htContextMenu').is(':visible')).toBe(false);
- done();
- }, 350); // waits for submenu open delay
- });
- });
- describe('mouse navigation', () => {
- it('should not scroll window position after fireing mouseenter on menu item', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(1000, 5),
- contextMenu: true,
- }),
- scrollHeight;
- hot.selectCell(100, 0);
- contextMenu();
- window.scrollTo(0, 0);
- $('.htContextMenu .ht_master .htCore').find('tr td:eq("0")').simulate('mouseenter');
- if (typeof window.scrollY !== 'undefined') {
- scrollHeight = window.scrollY;
- } else {
- scrollHeight = document.documentElement.scrollTop;
- }
- expect(scrollHeight).toBe(0);
- });
- it('should not scroll window position after fireing click on menu', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(1000, 5),
- contextMenu: {
- items: {
- item1: {
- name: 'Item1'
- },
- sep1: Handsontable.plugins.ContextMenu.SEPARATOR,
- item2: {
- name: 'Item2'
- },
- item3: {
- name: 'Item3'
- }
- }
- }
- }),
- scrollHeight;
- hot.selectCell(100, 0);
- contextMenu();
- window.scrollTo(0, 0);
- $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator').eq(0).simulate('mousedown');
- if (typeof window.scrollY !== 'undefined') {
- scrollHeight = window.scrollY;
- } else {
- scrollHeight = document.documentElement.scrollTop;
- }
- expect(scrollHeight).toBe(0);
- });
- });
- describe('working with multiple tables', () => {
- beforeEach(function () {
- this.$container2 = $(`<div id="${id}-2"></div>`).appendTo('body');
- });
- afterEach(function () {
- if (this.$container2) {
- this.$container2.handsontable('destroy');
- this.$container2.remove();
- }
- });
- it('should apply enabling/disabling contextMenu using updateSetting only to particular instance of HOT ', function () {
- var hot1 = handsontable({
- contextMenu: false,
- height: 100
- });
- var hot2 = this.$container2.handsontable({
- contextMenu: true,
- height: 100
- });
- hot2 = hot2.handsontable('getInstance');
- var contextMenuContainer = $('.htContextMenu');
- contextMenu();
- expect(hot1.getPlugin('contextMenu').isEnabled()).toBe(false);
- expect(contextMenuContainer.is(':visible')).toBe(false);
- contextMenu2();
- expect(hot2.getPlugin('contextMenu').isEnabled()).toBe(true);
- expect($('.htContextMenu').is(':visible')).toBe(true);
- mouseDown(hot2.rootElement); // close menu
- hot1.updateSettings({
- contextMenu: true
- });
- hot2.updateSettings({
- contextMenu: false
- });
- contextMenu2();
- expect(hot2.getPlugin('contextMenu').isEnabled()).toBe(false);
- contextMenu();
- expect($('.htContextMenu').is(':visible')).toBe(true);
- function contextMenu2() {
- var hot = spec().$container2.data('handsontable');
- var selected = hot.getSelected();
- if (!selected) {
- hot.selectCell(0, 0);
- selected = hot.getSelected();
- }
- var cell = hot.getCell(selected[0], selected[1]);
- var cellOffset = $(cell).offset();
- $(cell).simulate('contextmenu', {
- pageX: cellOffset.left,
- pageY: cellOffset.top
- });
- }
- });
- it('should perform a contextMenu action only for particular instance of HOT ', function () {
- var hot1 = handsontable({
- contextMenu: true,
- height: 100
- });
- var hot2 = this.$container2.handsontable({
- contextMenu: true,
- height: 100
- });
- hot2 = hot2.handsontable('getInstance');
- hot1.selectCell(0, 0);
- contextMenu();
- expect(hot1.countRows()).toEqual(5);
- expect(hot2.countRows()).toEqual(5);
- $('.htContextMenu .ht_master .htCore').find('tr td:eq("0")').simulate('mousedown'); // insert row above
- expect(hot1.countRows()).toEqual(6);
- expect(hot2.countRows()).toEqual(5);
- hot2.selectCell(0, 0);
- contextMenu2();
- expect(hot1.countRows()).toEqual(6);
- expect(hot2.countRows()).toEqual(5);
- $('.htContextMenu .ht_master .htCore').find('tr td:eq("0")').simulate('mousedown'); // insert row above
- expect(hot1.countRows()).toEqual(6);
- expect(hot2.countRows()).toEqual(6);
- function contextMenu2() {
- var hot = spec().$container2.data('handsontable');
- var selected = hot.getSelected();
- if (!selected) {
- hot.selectCell(0, 0);
- selected = hot.getSelected();
- }
- var cell = hot.getCell(selected[0], selected[1]);
- var cellOffset = $(cell).offset();
- $(cell).simulate('contextmenu', {
- pageX: cellOffset.left,
- pageY: cellOffset.top
- });
- }
- });
- });
- describe('context menu with native scroll', () => {
- beforeEach(function () {
- var wrapper = $('<div></div>').css({
- width: 400,
- height: 200,
- overflow: 'scroll'
- });
- this.$wrapper = this.$container.wrap(wrapper).parent();
- });
- afterEach(function () {
- if (this.$container) {
- destroy();
- this.$container.remove();
- }
- this.$wrapper.remove();
- });
- it('should display menu table is not scrolled', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(40, 30),
- colWidths: 50, // can also be a number or a function
- rowHeaders: true,
- colHeaders: true,
- contextMenu: true,
- height: 100
- });
- contextMenu();
- expect($('.htContextMenu').is(':visible')).toBe(true);
- });
- it('should display menu table is scrolled', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(40, 30),
- colWidths: 50, // can also be a number or a function
- rowHeaders: true,
- colHeaders: true,
- contextMenu: true,
- height: 100
- });
- var mainHolder = hot.view.wt.wtTable.holder;
- $(mainHolder).scrollTop(300);
- $(mainHolder).scroll();
- selectCell(15, 3);
- contextMenu();
- expect($('.htContextMenu').is(':visible')).toBe(true);
- });
- it('should not close the menu, when table is scrolled', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(40, 30),
- colWidths: 50, // can also be a number or a function
- rowHeaders: true,
- colHeaders: true,
- contextMenu: true,
- height: 100
- });
- var $mainHolder = $(hot.view.wt.wtTable.holder);
- selectCell(15, 3);
- var scrollTop = $mainHolder.scrollTop();
- contextMenu();
- expect($('.htContextMenu').is(':visible')).toBe(true);
- $mainHolder.scrollTop(scrollTop + 60).scroll();
- expect($('.htContextMenu').is(':visible')).toBe(true);
- contextMenu();
- expect($('.htContextMenu').is(':visible')).toBe(true);
- $mainHolder.scrollTop(scrollTop + 100).scroll();
- expect($('.htContextMenu').is(':visible')).toBe(true);
- });
- it('should not attempt to close menu, when table is scrolled and the menu is already closed', () => {
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(40, 30),
- colWidths: 50, // can also be a number or a function
- rowHeaders: true,
- colHeaders: true,
- contextMenu: true,
- height: 100
- });
- var mainHolder = $(hot.view.wt.wtTable.holder);
- selectCell(15, 3);
- var scrollTop = mainHolder.scrollTop();
- contextMenu();
- var $menu = $('.htContextMenu');
- spyOn(hot.getPlugin('contextMenu'), 'close');
- mainHolder.scrollTop(scrollTop + 100).scroll();
- expect(hot.getPlugin('contextMenu').close).not.toHaveBeenCalled();
- });
- it('should not scroll the window when hovering over context menu items (#1897 reopen)', function () {
- this.$wrapper.css('overflow', 'visible');
- var hot = handsontable({
- data: Handsontable.helper.createSpreadsheetData(403, 303),
- colWidths: 50, // can also be a number or a function
- contextMenu: true
- });
- var beginningScrollX = window.scrollX;
- selectCell(2, 4);
- contextMenu();
- var cmInstance = hot.getPlugin('contextMenu').menu.hotMenu;
- cmInstance.selectCell(3, 0);
- expect(window.scrollX).toEqual(beginningScrollX);
- cmInstance.selectCell(4, 0);
- expect(window.scrollX).toEqual(beginningScrollX);
- cmInstance.selectCell(6, 0);
- expect(window.scrollX).toEqual(beginningScrollX);
- });
- });
- describe('afterContextMenuDefaultOptions hook', () => {
- it('should call afterContextMenuDefaultOptions hook with context menu options as the first param', () => {
- var options;
- var afterContextMenuDefaultOptions = function(options_) {
- options = options_;
- options.items.cust1 = {
- name: 'My custom item',
- callback() {
- }
- };
- };
- Handsontable.hooks.add('afterContextMenuDefaultOptions', afterContextMenuDefaultOptions);
- var hot = handsontable({
- contextMenu: true,
- height: 100
- });
- contextMenu();
- var $menu = $('.htContextMenu .ht_master .htCore');
- expect(options).toBeDefined();
- expect(options.items).toBeDefined();
- expect($menu.find('tbody td').text()).toContain('My custom item');
- $menu.find('tbody td:eq(0)').simulate('mousedown');
- Handsontable.hooks.remove('afterContextMenuDefaultOptions', afterContextMenuDefaultOptions);
- });
- });
- describe('beforeContextMenuSetItems hook', () => {
- it('should add new menu item even when item is excluded from plugin settings', () => {
- Handsontable.hooks.add('beforeContextMenuSetItems', function(options) {
- if (this === hot || !hot) {
- options.push({
- key: 'test',
- name: 'Test'
- });
- }
- });
- var hot = handsontable({
- contextMenu: ['make_read_only'],
- height: 100
- });
- contextMenu();
- var items = $('.htContextMenu tbody td');
- var actions = items.not('.htSeparator');
- expect(actions.text()).toEqual([
- 'Read only',
- 'Test',
- ].join(''));
- });
- it('should be called only with items selected in plugin settings', () => {
- var keys = [];
- Handsontable.hooks.add('beforeContextMenuSetItems', function(items) {
- if (this === hot || !hot) {
- keys = items.map((v) => v.key);
- }
- });
- var hot = handsontable({
- contextMenu: ['make_read_only', 'col_left'],
- height: 100
- });
- contextMenu();
- expect(keys).toEqual(['make_read_only', 'col_left']);
- });
- });
- });
|