UNPKG

angular-ui-grid

Version:

A data grid for Angular

7 lines (5 loc) 12.9 kB
/*! * ui-grid - v4.9.1 - 2020-10-26 * Copyright (c) 2020 ; License: MIT */ !function(){"use strict";var e=angular.module("ui.grid.cellNav",["ui.grid"]);e.constant("uiGridCellNavConstants",{FEATURE_NAME:"gridCellNav",CELL_NAV_EVENT:"cellNav",direction:{LEFT:0,RIGHT:1,UP:2,DOWN:3,PG_UP:4,PG_DOWN:5},EVENT_TYPE:{KEYDOWN:0,CLICK:1,CLEAR:2}}),e.factory("uiGridCellNavFactory",["gridUtil","uiGridConstants","uiGridCellNavConstants","GridRowColumn","$q",function(e,l,o,a,i){var n=function(e,l,i,o){this.rows=e.visibleRowCache,this.columns=l.visibleColumnCache,this.leftColumns=i?i.visibleColumnCache:[],this.rightColumns=o?o.visibleColumnCache:[],this.bodyContainer=e};return n.prototype.getFocusableCols=function(){return this.leftColumns.concat(this.columns,this.rightColumns).filter(function(e){return e.colDef.allowCellFocus})},n.prototype.getFocusableRows=function(){return this.rows.filter(function(e){return!1!==e.allowCellFocus})},n.prototype.getNextRowCol=function(e,l,i){switch(e){case o.direction.LEFT:return this.getRowColLeft(l,i);case o.direction.RIGHT:return this.getRowColRight(l,i);case o.direction.UP:return this.getRowColUp(l,i);case o.direction.DOWN:return this.getRowColDown(l,i);case o.direction.PG_UP:return this.getRowColPageUp(l,i);case o.direction.PG_DOWN:return this.getRowColPageDown(l,i)}},n.prototype.initializeSelection=function(){var e=this.getFocusableCols(),l=this.getFocusableRows();return 0===e.length||0===l.length?null:new a(l[0],e[0])},n.prototype.getRowColLeft=function(e,l){var i=this.getFocusableCols(),o=this.getFocusableRows(),n=i.indexOf(l),t=o.indexOf(e);-1===n&&(n=1);var r=0===n?i.length-1:n-1;return new a(n<=r?0===t?e:o[t-1]:e,i[r])},n.prototype.getRowColRight=function(e,l){var i=this.getFocusableCols(),o=this.getFocusableRows(),n=i.indexOf(l),t=o.indexOf(e);-1===n&&(n=0);var r=n===i.length-1?0:n+1;return r<=n?t===o.length-1?new a(e,i[r]):new a(o[t+1],i[r]):new a(e,i[r])},n.prototype.getRowColDown=function(e,l){var i=this.getFocusableCols(),o=this.getFocusableRows(),n=i.indexOf(l),t=o.indexOf(e);return-1===n&&(n=0),t===o.length-1?new a(e,i[n]):new a(o[t+1],i[n])},n.prototype.getRowColPageDown=function(e,l){var i=this.getFocusableCols(),o=this.getFocusableRows(),n=i.indexOf(l),t=o.indexOf(e);-1===n&&(n=0);var r=this.bodyContainer.minRowsToRender();return t>=o.length-r?new a(o[o.length-1],i[n]):new a(o[t+r],i[n])},n.prototype.getRowColUp=function(e,l){var i=this.getFocusableCols(),o=this.getFocusableRows(),n=i.indexOf(l),t=o.indexOf(e);return-1===n&&(n=0),new a(0===t?e:o[t-1],i[n])},n.prototype.getRowColPageUp=function(e,l){var i=this.getFocusableCols(),o=this.getFocusableRows(),n=i.indexOf(l),t=o.indexOf(e);-1===n&&(n=0);var r=this.bodyContainer.minRowsToRender();return new a(t-r<0?o[0]:o[t-r],i[n])},n}]),e.service("uiGridCellNavService",["gridUtil","uiGridConstants","uiGridCellNavConstants","$q","uiGridCellNavFactory","GridRowColumn","ScrollEvent",function(e,l,i,o,n,t,r){var a={initializeGrid:function(o){o.registerColumnBuilder(a.cellNavColumnBuilder),o.cellNav={},o.cellNav.lastRowCol=null,o.cellNav.focusedCells=[],a.defaultGridOptions(o.options);var e={events:{cellNav:{navigate:function(e,l){},viewPortKeyDown:function(e,l){},viewPortKeyPress:function(e,l){}}},methods:{cellNav:{scrollToFocus:function(e,l){return a.scrollToFocus(o,e,l)},getFocusedCell:function(){return o.cellNav.lastRowCol},getCurrentSelection:function(){return o.cellNav.focusedCells},rowColSelectIndex:function(e){for(var l=-1,i=0;i<o.cellNav.focusedCells.length;i++)if(o.cellNav.focusedCells[i].col.uid===e.col.uid&&o.cellNav.focusedCells[i].row.uid===e.row.uid){l=i;break}return l}}}};o.api.registerEventsFromObject(e.events),o.api.registerMethodsFromObject(e.methods)},defaultGridOptions:function(e){e.modifierKeysToMultiSelectCells=!0===e.modifierKeysToMultiSelectCells,e.keyDownOverrides=e.keyDownOverrides||[]},decorateRenderContainers:function(e){var l=e.hasRightContainer()?e.renderContainers.right:null,i=e.hasLeftContainer()?e.renderContainers.left:null;null!==i&&(e.renderContainers.left.cellNav=new n(e.renderContainers.body,i,l,e.renderContainers.body)),null!==l&&(e.renderContainers.right.cellNav=new n(e.renderContainers.body,l,e.renderContainers.body,i)),e.renderContainers.body.cellNav=new n(e.renderContainers.body,e.renderContainers.body,i,l)},getDirection:function(e){return e.keyCode===l.keymap.LEFT||e.keyCode===l.keymap.TAB&&e.shiftKey?i.direction.LEFT:e.keyCode===l.keymap.RIGHT||e.keyCode===l.keymap.TAB?i.direction.RIGHT:e.keyCode===l.keymap.UP||e.keyCode===l.keymap.ENTER&&e.shiftKey?i.direction.UP:e.keyCode===l.keymap.PG_UP?i.direction.PG_UP:e.keyCode===l.keymap.DOWN||e.keyCode===l.keymap.ENTER&&!e.ctrlKey&&!e.altKey?i.direction.DOWN:e.keyCode===l.keymap.PG_DOWN?i.direction.PG_DOWN:null},cellNavColumnBuilder:function(e,l,i){return e.allowCellFocus=void 0===e.allowCellFocus||e.allowCellFocus,o.all([])},scrollToFocus:function(l,e,i){var o=null,n=null;return null!=e&&(o=l.getRow(e)),null!=i&&(n=l.getColumn(i.name?i.name:i.field)),l.api.core.scrollToIfNecessary(o,n).then(function(){var e={row:o,col:n};null!==o&&null!==n&&l.cellNav.broadcastCellNav(e,null,null)})},getLeftWidth:function(e,l){var i=0;if(!l)return i;var o=e.renderContainers.body.visibleColumnCache.indexOf(l);e.renderContainers.body.visibleColumnCache.forEach(function(e,l){l<o&&(i+=e.drawnWidth)});var n=0===o?0:(o+1)/e.renderContainers.body.visibleColumnCache.length;return i+=l.drawnWidth*n}};return a}]),e.directive("uiGridCellnav",["gridUtil","uiGridCellNavService","uiGridCellNavConstants","uiGridConstants","GridRowColumn","$timeout","$compile","i18nService",function(e,s,d,u,v,l,r,f){return{replace:!0,priority:-150,require:"^uiGrid",scope:!1,controller:function(){},compile:function(){return{pre:function(e,l,i,a){var o=e,c=a.grid;s.initializeGrid(c),a.cellNav={},a.cellNav.makeRowCol=function(e){return e instanceof v||(e=new v(e.row,e.col)),e},a.cellNav.getActiveCell=function(){var e=l[0].getElementsByClassName("ui-grid-cell-focus");if(0<e.length)return e[0]},a.cellNav.broadcastCellNav=c.cellNav.broadcastCellNav=function(e,l,i){l=!(void 0===l||!l),e=a.cellNav.makeRowCol(e),a.cellNav.broadcastFocus(e,l,i),o.$broadcast(d.CELL_NAV_EVENT,e,l,i)},a.cellNav.clearFocus=c.cellNav.clearFocus=function(){c.cellNav.focusedCells=[],o.$broadcast(d.CELL_NAV_EVENT)},a.cellNav.broadcastFocus=function(e,l,i){l=!(void 0===l||!l);var o=(e=a.cellNav.makeRowCol(e)).row,n=e.col,t=a.grid.api.cellNav.rowColSelectIndex(e);if(null===c.cellNav.lastRowCol||-1===t||c.cellNav.lastRowCol.col===n&&c.cellNav.lastRowCol.row===o){var r=new v(o,n);(null===c.cellNav.lastRowCol||c.cellNav.lastRowCol.row!==r.row||c.cellNav.lastRowCol.col!==r.col||c.options.enableCellEditOnFocus)&&(c.api.cellNav.raise.navigate(r,c.cellNav.lastRowCol,i),c.cellNav.lastRowCol=r),a.grid.options.modifierKeysToMultiSelectCells&&l?c.cellNav.focusedCells.push(e):c.cellNav.focusedCells=[e]}else c.options.modifierKeysToMultiSelectCells&&l&&0<=t&&c.cellNav.focusedCells.splice(t,1)},a.cellNav.handleKeyDown=function(e){var l=s.getDirection(e);if(null===l)return null;var i="body";e.uiGridTargetRenderContainerId&&(i=e.uiGridTargetRenderContainerId);var o=a.grid.api.cellNav.getFocusedCell();if(o){var n=a.grid.renderContainers[i].cellNav.getNextRowCol(l,o.row,o.col),t=a.grid.renderContainers[i].cellNav.getFocusableCols(),r=a.grid.api.cellNav.rowColSelectIndex(n);return l===d.direction.LEFT&&n.col===t[t.length-1]&&n.row===o.row&&e.keyCode===u.keymap.TAB&&e.shiftKey?(c.cellNav.focusedCells.splice(r,1),a.cellNav.clearFocus(),!0):l!==d.direction.RIGHT||n.col!==t[0]||n.row!==o.row||e.keyCode!==u.keymap.TAB||e.shiftKey?(c.scrollToIfNecessary(n.row,n.col).then(function(){a.cellNav.broadcastCellNav(n,null,e)}),e.stopPropagation(),e.preventDefault(),!1):(c.cellNav.focusedCells.splice(r,1),a.cellNav.clearFocus(),!0)}}},post:function(e,l,i,o){var n,v,C=o.grid,t=!0;try{angular.module("ngAria")}catch(e){t=!1}t&&(n='<div id="'+C.id+'-aria-speakable" class="ui-grid-a11y-ariascreenreader-speakable ui-grid-offscreen" aria-live="assertive" role="alert" aria-atomic="true" aria-hidden="false" aria-relevant="additions" >&nbsp;</div>',v=r(n)(e),l.prepend(v),e.$on(d.CELL_NAV_EVENT,function(e,l,i,o){if(!o||"focus"!==o.type){for(var n,t,r=[],a=C.api.cellNav.getCurrentSelection(),c=0;c<a.length;c++){var s=("selectionRowHeaderCol"===(t=a[c]).col.field?t.row.isSelected?f.getSafeText("search.aria.selected"):f.getSafeText("search.aria.notSelected"):C.getCellDisplayValue(t.row,t.col))+(n=a[c].col,", "+f.getSafeText("headerCell.aria.column")+" "+n.displayName);r.push(s)}var d,u=r.toString();(d=u)!==v.text().trim()&&(v[0].style.clip="rect(0px,0px,0px,0px)",v[0].innerHTML="",v[0].style.visibility="hidden",v[0].style.visibility="visible",""!==d&&(v[0].style.clip="auto",v[0].appendChild(document.createTextNode(d+" ")),v[0].style.visibility="hidden",v[0].style.visibility="visible"))}}))}}}}}]),e.directive("uiGridRenderContainer",["$timeout","$document","gridUtil","uiGridConstants","uiGridCellNavService","$compile","uiGridCellNavConstants",function(u,e,v,l,C,f,g){return{replace:!0,priority:-99999,require:["^uiGrid","uiGridRenderContainer","?^uiGridCellnav"],scope:!1,compile:function(){return{post:function(e,l,i,o){var n=o[0],t=o[1],r=o[2];if(n.grid.api.cellNav){var a=t.containerId,c=n.grid;if(C.decorateRenderContainers(c),"body"===a){n.grid.options.modifierKeysToMultiSelectCells?l.attr("aria-multiselectable",!0):l.attr("aria-multiselectable",!1);var s=f('<div class="ui-grid-focuser" role="region" aria-live="assertive" aria-atomic="false" tabindex="0" aria-controls="'+c.id+"-aria-speakable "+c.id+'-grid-container" aria-owns="'+c.id+'-grid-container"></div>')(e);l.append(s),s.on("focus",function(e){e.uiGridTargetRenderContainerId=a;var l=n.grid.api.cellNav.getFocusedCell();null===l&&(l=n.grid.renderContainers[a].cellNav.getNextRowCol(g.direction.DOWN,null,null)).row&&l.col&&n.cellNav.broadcastCellNav(l)}),r.setAriaActivedescendant=function(e){l.attr("aria-activedescendant",e)},r.removeAriaActivedescendant=function(e){l.attr("aria-activedescendant")===e&&l.attr("aria-activedescendant","")},n.focus=function(){v.focus.byElement(s[0])};var d=null;s.on("keydown",function(i){i.uiGridTargetRenderContainerId=a;var e=n.grid.api.cellNav.getFocusedCell();null===(n.grid.options.keyDownOverrides.some(function(l){return Object.keys(l).every(function(e){return l[e]===i[e]})})?null:n.cellNav.handleKeyDown(i))&&(n.grid.api.cellNav.raise.viewPortKeyDown(i,e,n.cellNav.handleKeyDown),d=e)}),s.on("keypress",function(e){d&&(u(function(){n.grid.api.cellNav.raise.viewPortKeyPress(e,d)},4),d=null)}),e.$on("$destroy",function(){s.off()})}}}}}}}]),e.directive("uiGridViewport",function(){return{replace:!0,priority:-99999,require:["^uiGrid","^uiGridRenderContainer","?^uiGridCellnav"],scope:!1,compile:function(){return{pre:function(e,l,i,o){},post:function(e,l,i,o){var n=o[0],t=o[1];if(n.grid.api.cellNav&&"body"===t.containerId){var r=n.grid;r.api.core.on.scrollBegin(e,function(){var e=n.grid.api.cellNav.getFocusedCell();null!==e&&t.colContainer.containsColumn(e.col)&&n.cellNav.clearFocus()}),r.api.core.on.scrollEnd(e,function(e){var l=n.grid.api.cellNav.getFocusedCell();null!==l&&t.colContainer.containsColumn(l.col)&&n.cellNav.broadcastCellNav(l)}),r.api.cellNav.on.navigate(e,function(){n.focus()})}}}}}}),e.directive("uiGridCell",["$timeout","$document","uiGridCellNavService","gridUtil","uiGridCellNavConstants","uiGridConstants","GridRowColumn",function(e,l,i,o,u,v,C){return{priority:-150,restrict:"A",require:["^uiGrid","?^uiGridCellnav"],scope:!1,link:function(i,l,e,o){var n=o[0],t=o[1];if(n.grid.api.cellNav&&i.col.colDef.allowCellFocus){var r=n.grid;i.focused=!1,l.attr("tabindex",-1),l.find("div").on("click",function(e){n.cellNav.broadcastCellNav(new C(i.row,i.col),e.ctrlKey||e.metaKey,e),e.stopPropagation(),i.$apply()}),l.on("mousedown",c),n.grid.api.edit&&(n.grid.api.edit.on.beginCellEdit(i,function(){l.off("mousedown",c)}),n.grid.api.edit.on.afterCellEdit(i,function(){l.on("mousedown",c)}),n.grid.api.edit.on.cancelCellEdit(i,function(){l.on("mousedown",c)})),s(),l.on("focus",function(e){n.cellNav.broadcastCellNav(new C(i.row,i.col),!1,e),e.stopPropagation(),i.$apply()}),i.$on(u.CELL_NAV_EVENT,s);var a=n.grid.registerDataChangeCallback(function(e){d(),i.$applyAsync(s)},[v.dataChange.ROW]);i.$on("$destroy",function(){a(),l.find("div").off(),l.off()})}function c(e){e.preventDefault()}function s(){r.cellNav.focusedCells.some(function(e,l){return e.row===i.row&&e.col===i.col})?function(){if(!i.focused){var e=l.find("div");e.addClass("ui-grid-cell-focus"),l.attr("aria-selected",!0),t.setAriaActivedescendant(l.attr("id")),i.focused=!0}}():d()}function d(){i.focused&&(l.find("div").removeClass("ui-grid-cell-focus"),l.attr("aria-selected",!1),t.removeAriaActivedescendant(l.attr("id")),i.focused=!1)}}}}])}();