ui-grid.saveState.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /*!
  2. * ui-grid - v4.4.6 - 2018-04-06
  3. * Copyright (c) 2018 ; License: MIT
  4. */
  5. (function () {
  6. 'use strict';
  7. /**
  8. * @ngdoc overview
  9. * @name ui.grid.saveState
  10. * @description
  11. *
  12. * # ui.grid.saveState
  13. *
  14. * <div class="alert alert-success" role="alert"><strong>Stable</strong> This feature is stable. There should no longer be breaking api changes without a deprecation warning.</div>
  15. *
  16. * This module provides the ability to save the grid state, and restore
  17. * it when the user returns to the page.
  18. *
  19. * No UI is provided, the caller should provide their own UI/buttons
  20. * as appropriate. Usually the navigate events would be used to save
  21. * the grid state and restore it.
  22. *
  23. * <br/>
  24. * <br/>
  25. *
  26. * <div doc-module-components="ui.grid.save-state"></div>
  27. */
  28. var module = angular.module('ui.grid.saveState', ['ui.grid', 'ui.grid.selection', 'ui.grid.cellNav', 'ui.grid.grouping', 'ui.grid.pinning', 'ui.grid.treeView']);
  29. /**
  30. * @ngdoc object
  31. * @name ui.grid.saveState.constant:uiGridSaveStateConstants
  32. *
  33. * @description constants available in save state module
  34. */
  35. module.constant('uiGridSaveStateConstants', {
  36. featureName: 'saveState'
  37. });
  38. /**
  39. * @ngdoc service
  40. * @name ui.grid.saveState.service:uiGridSaveStateService
  41. *
  42. * @description Services for saveState feature
  43. */
  44. module.service('uiGridSaveStateService', ['$q', 'uiGridSaveStateConstants', 'gridUtil', '$compile', '$interval', 'uiGridConstants',
  45. function ($q, uiGridSaveStateConstants, gridUtil, $compile, $interval, uiGridConstants ) {
  46. var service = {
  47. initializeGrid: function (grid) {
  48. //add feature namespace and any properties to grid for needed state
  49. grid.saveState = {};
  50. this.defaultGridOptions(grid.options);
  51. /**
  52. * @ngdoc object
  53. * @name ui.grid.saveState.api:PublicApi
  54. *
  55. * @description Public Api for saveState feature
  56. */
  57. var publicApi = {
  58. events: {
  59. saveState: {
  60. }
  61. },
  62. methods: {
  63. saveState: {
  64. /**
  65. * @ngdoc function
  66. * @name save
  67. * @methodOf ui.grid.saveState.api:PublicApi
  68. * @description Packages the current state of the grid into
  69. * an object, and provides it to the user for saving
  70. * @returns {object} the state as a javascript object that can be saved
  71. */
  72. save: function () {
  73. return service.save(grid);
  74. },
  75. /**
  76. * @ngdoc function
  77. * @name restore
  78. * @methodOf ui.grid.saveState.api:PublicApi
  79. * @description Restores the provided state into the grid
  80. * @param {scope} $scope a scope that we can broadcast on
  81. * @param {object} state the state that should be restored into the grid
  82. * @returns {object} the promise created by refresh
  83. */
  84. restore: function ( $scope, state) {
  85. return service.restore(grid, $scope, state);
  86. }
  87. }
  88. }
  89. };
  90. grid.api.registerEventsFromObject(publicApi.events);
  91. grid.api.registerMethodsFromObject(publicApi.methods);
  92. },
  93. defaultGridOptions: function (gridOptions) {
  94. //default option to true unless it was explicitly set to false
  95. /**
  96. * @ngdoc object
  97. * @name ui.grid.saveState.api:GridOptions
  98. *
  99. * @description GridOptions for saveState feature, these are available to be
  100. * set using the ui-grid {@link ui.grid.class:GridOptions gridOptions}
  101. */
  102. /**
  103. * @ngdoc object
  104. * @name saveWidths
  105. * @propertyOf ui.grid.saveState.api:GridOptions
  106. * @description Save the current column widths. Note that unless
  107. * you've provided the user with some way to resize their columns (say
  108. * the resize columns feature), then this makes little sense.
  109. * <br/>Defaults to true
  110. */
  111. gridOptions.saveWidths = gridOptions.saveWidths !== false;
  112. /**
  113. * @ngdoc object
  114. * @name saveOrder
  115. * @propertyOf ui.grid.saveState.api:GridOptions
  116. * @description Restore the current column order. Note that unless
  117. * you've provided the user with some way to reorder their columns (for
  118. * example the move columns feature), this makes little sense.
  119. * <br/>Defaults to true
  120. */
  121. gridOptions.saveOrder = gridOptions.saveOrder !== false;
  122. /**
  123. * @ngdoc object
  124. * @name saveScroll
  125. * @propertyOf ui.grid.saveState.api:GridOptions
  126. * @description Save the current scroll position. Note that this
  127. * is saved as the percentage of the grid scrolled - so if your
  128. * user returns to a grid with a significantly different number of
  129. * rows (perhaps some data has been deleted) then the scroll won't
  130. * actually show the same rows as before. If you want to scroll to
  131. * a specific row then you should instead use the saveFocus option, which
  132. * is the default.
  133. *
  134. * Note that this element will only be saved if the cellNav feature is
  135. * enabled
  136. * <br/>Defaults to false
  137. */
  138. gridOptions.saveScroll = gridOptions.saveScroll === true;
  139. /**
  140. * @ngdoc object
  141. * @name saveFocus
  142. * @propertyOf ui.grid.saveState.api:GridOptions
  143. * @description Save the current focused cell. On returning
  144. * to this focused cell we'll also scroll. This option is
  145. * preferred to the saveScroll option, so is set to true by
  146. * default. If saveScroll is set to true then this option will
  147. * be disabled.
  148. *
  149. * By default this option saves the current row number and column
  150. * number, and returns to that row and column. However, if you define
  151. * a saveRowIdentity function, then it will return you to the currently
  152. * selected column within that row (in a business sense - so if some
  153. * rows have been deleted, it will still find the same data, presuming it
  154. * still exists in the list. If it isn't in the list then it will instead
  155. * return to the same row number - i.e. scroll percentage)
  156. *
  157. * Note that this option will do nothing if the cellNav
  158. * feature is not enabled.
  159. *
  160. * <br/>Defaults to true (unless saveScroll is true)
  161. */
  162. gridOptions.saveFocus = gridOptions.saveScroll !== true && gridOptions.saveFocus !== false;
  163. /**
  164. * @ngdoc object
  165. * @name saveRowIdentity
  166. * @propertyOf ui.grid.saveState.api:GridOptions
  167. * @description A function that can be called, passing in a rowEntity,
  168. * and that will return a unique id for that row. This might simply
  169. * return the `id` field from that row (if you have one), or it might
  170. * concatenate some fields within the row to make a unique value.
  171. *
  172. * This value will be used to find the same row again and set the focus
  173. * to it, if it exists when we return.
  174. *
  175. * <br/>Defaults to undefined
  176. */
  177. /**
  178. * @ngdoc object
  179. * @name saveVisible
  180. * @propertyOf ui.grid.saveState.api:GridOptions
  181. * @description Save whether or not columns are visible.
  182. *
  183. * <br/>Defaults to true
  184. */
  185. gridOptions.saveVisible = gridOptions.saveVisible !== false;
  186. /**
  187. * @ngdoc object
  188. * @name saveSort
  189. * @propertyOf ui.grid.saveState.api:GridOptions
  190. * @description Save the current sort state for each column
  191. *
  192. * <br/>Defaults to true
  193. */
  194. gridOptions.saveSort = gridOptions.saveSort !== false;
  195. /**
  196. * @ngdoc object
  197. * @name saveFilter
  198. * @propertyOf ui.grid.saveState.api:GridOptions
  199. * @description Save the current filter state for each column
  200. *
  201. * <br/>Defaults to true
  202. */
  203. gridOptions.saveFilter = gridOptions.saveFilter !== false;
  204. /**
  205. * @ngdoc object
  206. * @name saveSelection
  207. * @propertyOf ui.grid.saveState.api:GridOptions
  208. * @description Save the currently selected rows. If the `saveRowIdentity` callback
  209. * is defined, then it will save the id of the row and select that. If not, then
  210. * it will attempt to select the rows by row number, which will give the wrong results
  211. * if the data set has changed in the mean-time.
  212. *
  213. * Note that this option only does anything
  214. * if the selection feature is enabled.
  215. *
  216. * <br/>Defaults to true
  217. */
  218. gridOptions.saveSelection = gridOptions.saveSelection !== false;
  219. /**
  220. * @ngdoc object
  221. * @name saveGrouping
  222. * @propertyOf ui.grid.saveState.api:GridOptions
  223. * @description Save the grouping configuration. If set to true and the
  224. * grouping feature is not enabled then does nothing.
  225. *
  226. * <br/>Defaults to true
  227. */
  228. gridOptions.saveGrouping = gridOptions.saveGrouping !== false;
  229. /**
  230. * @ngdoc object
  231. * @name saveGroupingExpandedStates
  232. * @propertyOf ui.grid.saveState.api:GridOptions
  233. * @description Save the grouping row expanded states. If set to true and the
  234. * grouping feature is not enabled then does nothing.
  235. *
  236. * This can be quite a bit of data, in many cases you wouldn't want to save this
  237. * information.
  238. *
  239. * <br/>Defaults to false
  240. */
  241. gridOptions.saveGroupingExpandedStates = gridOptions.saveGroupingExpandedStates === true;
  242. /**
  243. * @ngdoc object
  244. * @name savePinning
  245. * @propertyOf ui.grid.saveState.api:GridOptions
  246. * @description Save pinning state for columns.
  247. *
  248. * <br/>Defaults to true
  249. */
  250. gridOptions.savePinning = gridOptions.savePinning !== false;
  251. /**
  252. * @ngdoc object
  253. * @name saveTreeView
  254. * @propertyOf ui.grid.saveState.api:GridOptions
  255. * @description Save the treeView configuration. If set to true and the
  256. * treeView feature is not enabled then does nothing.
  257. *
  258. * <br/>Defaults to true
  259. */
  260. gridOptions.saveTreeView = gridOptions.saveTreeView !== false;
  261. },
  262. /**
  263. * @ngdoc function
  264. * @name save
  265. * @methodOf ui.grid.saveState.service:uiGridSaveStateService
  266. * @description Saves the current grid state into an object, and
  267. * passes that object back to the caller
  268. * @param {Grid} grid the grid whose state we'd like to save
  269. * @returns {object} the state ready to be saved
  270. */
  271. save: function (grid) {
  272. var savedState = {};
  273. savedState.columns = service.saveColumns( grid );
  274. savedState.scrollFocus = service.saveScrollFocus( grid );
  275. savedState.selection = service.saveSelection( grid );
  276. savedState.grouping = service.saveGrouping( grid );
  277. savedState.treeView = service.saveTreeView( grid );
  278. savedState.pagination = service.savePagination( grid );
  279. return savedState;
  280. },
  281. /**
  282. * @ngdoc function
  283. * @name restore
  284. * @methodOf ui.grid.saveState.service:uiGridSaveStateService
  285. * @description Applies the provided state to the grid
  286. *
  287. * @param {Grid} grid the grid whose state we'd like to restore
  288. * @param {scope} $scope a scope that we can broadcast on
  289. * @param {object} state the state we'd like to restore
  290. * @returns {object} the promise created by refresh
  291. */
  292. restore: function( grid, $scope, state ){
  293. if ( state.columns ) {
  294. service.restoreColumns( grid, state.columns );
  295. }
  296. if ( state.scrollFocus ){
  297. service.restoreScrollFocus( grid, $scope, state.scrollFocus );
  298. }
  299. if ( state.selection ){
  300. service.restoreSelection( grid, state.selection );
  301. }
  302. if ( state.grouping ){
  303. service.restoreGrouping( grid, state.grouping );
  304. }
  305. if ( state.treeView ){
  306. service.restoreTreeView( grid, state.treeView );
  307. }
  308. if ( state.pagination ){
  309. service.restorePagination( grid, state.pagination );
  310. }
  311. return grid.refresh();
  312. },
  313. /**
  314. * @ngdoc function
  315. * @name saveColumns
  316. * @methodOf ui.grid.saveState.service:uiGridSaveStateService
  317. * @description Saves the column setup, including sort, filters, ordering,
  318. * pinning and column widths.
  319. *
  320. * Works through the current columns, storing them in order. Stores the
  321. * column name, then the visible flag, width, sort and filters for each column.
  322. *
  323. * @param {Grid} grid the grid whose state we'd like to save
  324. * @returns {array} the columns state ready to be saved
  325. */
  326. saveColumns: function( grid ) {
  327. var columns = [];
  328. grid.getOnlyDataColumns().forEach( function( column ) {
  329. var savedColumn = {};
  330. savedColumn.name = column.name;
  331. if ( grid.options.saveVisible ){
  332. savedColumn.visible = column.visible;
  333. }
  334. if ( grid.options.saveWidths ){
  335. savedColumn.width = column.width;
  336. }
  337. // these two must be copied, not just pointed too - otherwise our saved state is pointing to the same object as current state
  338. if ( grid.options.saveSort ){
  339. savedColumn.sort = angular.copy( column.sort );
  340. }
  341. if ( grid.options.saveFilter ){
  342. savedColumn.filters = [];
  343. column.filters.forEach( function( filter ){
  344. var copiedFilter = {};
  345. angular.forEach( filter, function( value, key) {
  346. if ( key !== 'condition' && key !== '$$hashKey' && key !== 'placeholder'){
  347. copiedFilter[key] = value;
  348. }
  349. });
  350. savedColumn.filters.push(copiedFilter);
  351. });
  352. }
  353. if ( !!grid.api.pinning && grid.options.savePinning ){
  354. savedColumn.pinned = column.renderContainer ? column.renderContainer : '';
  355. }
  356. columns.push( savedColumn );
  357. });
  358. return columns;
  359. },
  360. /**
  361. * @ngdoc function
  362. * @name saveScrollFocus
  363. * @methodOf ui.grid.saveState.service:uiGridSaveStateService
  364. * @description Saves the currently scroll or focus.
  365. *
  366. * If cellNav isn't present then does nothing - we can't return
  367. * to the scroll position without cellNav anyway.
  368. *
  369. * If the cellNav module is present, and saveFocus is true, then
  370. * it saves the currently focused cell. If rowIdentity is present
  371. * then saves using rowIdentity, otherwise saves visibleRowNum.
  372. *
  373. * If the cellNav module is not present, and saveScroll is true, then
  374. * it approximates the current scroll row and column, and saves that.
  375. *
  376. * @param {Grid} grid the grid whose state we'd like to save
  377. * @returns {object} the selection state ready to be saved
  378. */
  379. saveScrollFocus: function( grid ){
  380. if ( !grid.api.cellNav ){
  381. return {};
  382. }
  383. var scrollFocus = {};
  384. if ( grid.options.saveFocus ){
  385. scrollFocus.focus = true;
  386. var rowCol = grid.api.cellNav.getFocusedCell();
  387. if ( rowCol !== null ) {
  388. if ( rowCol.col !== null ){
  389. scrollFocus.colName = rowCol.col.colDef.name;
  390. }
  391. if ( rowCol.row !== null ){
  392. scrollFocus.rowVal = service.getRowVal( grid, rowCol.row );
  393. }
  394. }
  395. }
  396. if ( grid.options.saveScroll || grid.options.saveFocus && !scrollFocus.colName && !scrollFocus.rowVal ) {
  397. scrollFocus.focus = false;
  398. if ( grid.renderContainers.body.prevRowScrollIndex ){
  399. scrollFocus.rowVal = service.getRowVal( grid, grid.renderContainers.body.visibleRowCache[ grid.renderContainers.body.prevRowScrollIndex ]);
  400. }
  401. if ( grid.renderContainers.body.prevColScrollIndex ){
  402. scrollFocus.colName = grid.renderContainers.body.visibleColumnCache[ grid.renderContainers.body.prevColScrollIndex ].name;
  403. }
  404. }
  405. return scrollFocus;
  406. },
  407. /**
  408. * @ngdoc function
  409. * @name saveSelection
  410. * @methodOf ui.grid.saveState.service:uiGridSaveStateService
  411. * @description Saves the currently selected rows, if the selection feature is enabled
  412. * @param {Grid} grid the grid whose state we'd like to save
  413. * @returns {array} the selection state ready to be saved
  414. */
  415. saveSelection: function( grid ){
  416. if ( !grid.api.selection || !grid.options.saveSelection ){
  417. return [];
  418. }
  419. var selection = grid.api.selection.getSelectedGridRows().map( function( gridRow ) {
  420. return service.getRowVal( grid, gridRow );
  421. });
  422. return selection;
  423. },
  424. /**
  425. * @ngdoc function
  426. * @name saveGrouping
  427. * @methodOf ui.grid.saveState.service:uiGridSaveStateService
  428. * @description Saves the grouping state, if the grouping feature is enabled
  429. * @param {Grid} grid the grid whose state we'd like to save
  430. * @returns {object} the grouping state ready to be saved
  431. */
  432. saveGrouping: function( grid ){
  433. if ( !grid.api.grouping || !grid.options.saveGrouping ){
  434. return {};
  435. }
  436. return grid.api.grouping.getGrouping( grid.options.saveGroupingExpandedStates );
  437. },
  438. /**
  439. * @ngdoc function
  440. * @name savePagination
  441. * @methodOf ui.grid.saveState.service:uiGridSaveStateService
  442. * @description Saves the pagination state, if the pagination feature is enabled
  443. * @param {Grid} grid the grid whose state we'd like to save
  444. * @returns {object} the pagination state ready to be saved
  445. */
  446. savePagination: function( grid ) {
  447. if ( !grid.api.pagination || !grid.options.paginationPageSize ){
  448. return {};
  449. }
  450. return {
  451. paginationCurrentPage: grid.options.paginationCurrentPage,
  452. paginationPageSize: grid.options.paginationPageSize
  453. };
  454. },
  455. /**
  456. * @ngdoc function
  457. * @name saveTreeView
  458. * @methodOf ui.grid.saveState.service:uiGridSaveStateService
  459. * @description Saves the tree view state, if the tree feature is enabled
  460. * @param {Grid} grid the grid whose state we'd like to save
  461. * @returns {object} the tree view state ready to be saved
  462. */
  463. saveTreeView: function( grid ){
  464. if ( !grid.api.treeView || !grid.options.saveTreeView ){
  465. return {};
  466. }
  467. return grid.api.treeView.getTreeView();
  468. },
  469. /**
  470. * @ngdoc function
  471. * @name getRowVal
  472. * @methodOf ui.grid.saveState.service:uiGridSaveStateService
  473. * @description Helper function that gets either the rowNum or
  474. * the saveRowIdentity, given a gridRow
  475. * @param {Grid} grid the grid the row is in
  476. * @param {GridRow} gridRow the row we want the rowNum for
  477. * @returns {object} an object containing { identity: true/false, row: rowNumber/rowIdentity }
  478. *
  479. */
  480. getRowVal: function( grid, gridRow ){
  481. if ( !gridRow ) {
  482. return null;
  483. }
  484. var rowVal = {};
  485. if ( grid.options.saveRowIdentity ){
  486. rowVal.identity = true;
  487. rowVal.row = grid.options.saveRowIdentity( gridRow.entity );
  488. } else {
  489. rowVal.identity = false;
  490. rowVal.row = grid.renderContainers.body.visibleRowCache.indexOf( gridRow );
  491. }
  492. return rowVal;
  493. },
  494. /**
  495. * @ngdoc function
  496. * @name restoreColumns
  497. * @methodOf ui.grid.saveState.service:uiGridSaveStateService
  498. * @description Restores the columns, including order, visible, width,
  499. * pinning, sort and filters.
  500. *
  501. * @param {Grid} grid the grid whose state we'd like to restore
  502. * @param {object} columnsState the list of columns we had before, with their state
  503. */
  504. restoreColumns: function( grid, columnsState ){
  505. var isSortChanged = false;
  506. columnsState.forEach( function( columnState, index ) {
  507. var currentCol = grid.getColumn( columnState.name );
  508. if ( currentCol && !grid.isRowHeaderColumn(currentCol) ){
  509. if ( grid.options.saveVisible &&
  510. ( currentCol.visible !== columnState.visible ||
  511. currentCol.colDef.visible !== columnState.visible ) ){
  512. currentCol.visible = columnState.visible;
  513. currentCol.colDef.visible = columnState.visible;
  514. grid.api.core.raise.columnVisibilityChanged(currentCol);
  515. }
  516. if ( grid.options.saveWidths && currentCol.width !== columnState.width){
  517. currentCol.width = columnState.width;
  518. currentCol.hasCustomWidth = true;
  519. }
  520. if ( grid.options.saveSort &&
  521. !angular.equals(currentCol.sort, columnState.sort) &&
  522. !( currentCol.sort === undefined && angular.isEmpty(columnState.sort) ) ){
  523. currentCol.sort = angular.copy( columnState.sort );
  524. isSortChanged = true;
  525. }
  526. if ( grid.options.saveFilter &&
  527. !angular.equals(currentCol.filters, columnState.filters ) ){
  528. columnState.filters.forEach( function( filter, index ){
  529. angular.extend( currentCol.filters[index], filter );
  530. if ( typeof(filter.term) === 'undefined' || filter.term === null ){
  531. delete currentCol.filters[index].term;
  532. }
  533. });
  534. grid.api.core.raise.filterChanged();
  535. }
  536. if ( !!grid.api.pinning && grid.options.savePinning && currentCol.renderContainer !== columnState.pinned ){
  537. grid.api.pinning.pinColumn(currentCol, columnState.pinned);
  538. }
  539. var currentIndex = grid.getOnlyDataColumns().indexOf( currentCol );
  540. if (currentIndex !== -1) {
  541. if (grid.options.saveOrder && currentIndex !== index) {
  542. var column = grid.columns.splice(currentIndex + grid.rowHeaderColumns.length, 1)[0];
  543. grid.columns.splice(index + grid.rowHeaderColumns.length, 0, column);
  544. }
  545. }
  546. }
  547. });
  548. if ( isSortChanged ) {
  549. grid.api.core.raise.sortChanged( grid, grid.getColumnSorting() );
  550. }
  551. },
  552. /**
  553. * @ngdoc function
  554. * @name restoreScrollFocus
  555. * @methodOf ui.grid.saveState.service:uiGridSaveStateService
  556. * @description Scrolls to the position that was saved. If focus is true, then
  557. * sets focus to the specified row/col. If focus is false, then scrolls to the
  558. * specified row/col.
  559. *
  560. * @param {Grid} grid the grid whose state we'd like to restore
  561. * @param {scope} $scope a scope that we can broadcast on
  562. * @param {object} scrollFocusState the scroll/focus state ready to be restored
  563. */
  564. restoreScrollFocus: function( grid, $scope, scrollFocusState ){
  565. if ( !grid.api.cellNav ){
  566. return;
  567. }
  568. var colDef, row;
  569. if ( scrollFocusState.colName ){
  570. var colDefs = grid.options.columnDefs.filter( function( colDef ) { return colDef.name === scrollFocusState.colName; });
  571. if ( colDefs.length > 0 ){
  572. colDef = colDefs[0];
  573. }
  574. }
  575. if ( scrollFocusState.rowVal && scrollFocusState.rowVal.row ){
  576. if ( scrollFocusState.rowVal.identity ){
  577. row = service.findRowByIdentity( grid, scrollFocusState.rowVal );
  578. } else {
  579. row = grid.renderContainers.body.visibleRowCache[ scrollFocusState.rowVal.row ];
  580. }
  581. }
  582. var entity = row && row.entity ? row.entity : null ;
  583. if ( colDef || entity ) {
  584. if (scrollFocusState.focus ){
  585. grid.api.cellNav.scrollToFocus( entity, colDef );
  586. } else {
  587. grid.scrollTo( entity, colDef );
  588. }
  589. }
  590. },
  591. /**
  592. * @ngdoc function
  593. * @name restoreSelection
  594. * @methodOf ui.grid.saveState.service:uiGridSaveStateService
  595. * @description Selects the rows that are provided in the selection
  596. * state. If you are using `saveRowIdentity` and more than one row matches the identity
  597. * function then only the first is selected.
  598. * @param {Grid} grid the grid whose state we'd like to restore
  599. * @param {object} selectionState the selection state ready to be restored
  600. */
  601. restoreSelection: function( grid, selectionState ){
  602. if ( !grid.api.selection ){
  603. return;
  604. }
  605. grid.api.selection.clearSelectedRows();
  606. selectionState.forEach( function( rowVal ) {
  607. if ( rowVal.identity ){
  608. var foundRow = service.findRowByIdentity( grid, rowVal );
  609. if ( foundRow ){
  610. grid.api.selection.selectRow( foundRow.entity );
  611. }
  612. } else {
  613. grid.api.selection.selectRowByVisibleIndex( rowVal.row );
  614. }
  615. });
  616. },
  617. /**
  618. * @ngdoc function
  619. * @name restoreGrouping
  620. * @methodOf ui.grid.saveState.service:uiGridSaveStateService
  621. * @description Restores the grouping configuration, if the grouping feature
  622. * is enabled.
  623. * @param {Grid} grid the grid whose state we'd like to restore
  624. * @param {object} groupingState the grouping state ready to be restored
  625. */
  626. restoreGrouping: function( grid, groupingState ){
  627. if ( !grid.api.grouping || typeof(groupingState) === 'undefined' || groupingState === null || angular.equals(groupingState, {}) ){
  628. return;
  629. }
  630. grid.api.grouping.setGrouping( groupingState );
  631. },
  632. /**
  633. * @ngdoc function
  634. * @name restoreTreeView
  635. * @methodOf ui.grid.saveState.service:uiGridSaveStateService
  636. * @description Restores the tree view configuration, if the tree view feature
  637. * is enabled.
  638. * @param {Grid} grid the grid whose state we'd like to restore
  639. * @param {object} treeViewState the tree view state ready to be restored
  640. */
  641. restoreTreeView: function( grid, treeViewState ){
  642. if ( !grid.api.treeView || typeof(treeViewState) === 'undefined' || treeViewState === null || angular.equals(treeViewState, {}) ){
  643. return;
  644. }
  645. grid.api.treeView.setTreeView( treeViewState );
  646. },
  647. /**
  648. * @ngdoc function
  649. * @name restorePagination
  650. * @methodOf ui.grid.saveState.service:uiGridSaveStateService
  651. * @description Restores the pagination information, if pagination is enabled.
  652. * @param {Grid} grid the grid whose state we'd like to restore
  653. * @param {object} pagination the pagination object to be restored
  654. * @param {number} pagination.paginationCurrentPage the page number to restore
  655. * @param {number} pagination.paginationPageSize the number of items displayed per page
  656. */
  657. restorePagination: function( grid, pagination ){
  658. if ( !grid.api.pagination || !grid.options.paginationPageSize ){
  659. return;
  660. }
  661. grid.options.paginationCurrentPage = pagination.paginationCurrentPage;
  662. grid.options.paginationPageSize = pagination.paginationPageSize;
  663. },
  664. /**
  665. * @ngdoc function
  666. * @name findRowByIdentity
  667. * @methodOf ui.grid.saveState.service:uiGridSaveStateService
  668. * @description Finds a row given it's identity value, returns the first found row
  669. * if any are found, otherwise returns null if no rows are found.
  670. * @param {Grid} grid the grid whose state we'd like to restore
  671. * @param {object} rowVal the row we'd like to find
  672. * @returns {gridRow} the found row, or null if none found
  673. */
  674. findRowByIdentity: function( grid, rowVal ){
  675. if ( !grid.options.saveRowIdentity ){
  676. return null;
  677. }
  678. var filteredRows = grid.rows.filter( function( gridRow ) {
  679. if ( grid.options.saveRowIdentity( gridRow.entity ) === rowVal.row ){
  680. return true;
  681. } else {
  682. return false;
  683. }
  684. });
  685. if ( filteredRows.length > 0 ){
  686. return filteredRows[0];
  687. } else {
  688. return null;
  689. }
  690. }
  691. };
  692. return service;
  693. }
  694. ]);
  695. /**
  696. * @ngdoc directive
  697. * @name ui.grid.saveState.directive:uiGridSaveState
  698. * @element div
  699. * @restrict A
  700. *
  701. * @description Adds saveState features to grid
  702. *
  703. * @example
  704. <example module="app">
  705. <file name="app.js">
  706. var app = angular.module('app', ['ui.grid', 'ui.grid.saveState']);
  707. app.controller('MainCtrl', ['$scope', function ($scope) {
  708. $scope.data = [
  709. { name: 'Bob', title: 'CEO' },
  710. { name: 'Frank', title: 'Lowly Developer' }
  711. ];
  712. $scope.gridOptions = {
  713. columnDefs: [
  714. {name: 'name'},
  715. {name: 'title', enableCellEdit: true}
  716. ],
  717. data: $scope.data
  718. };
  719. }]);
  720. </file>
  721. <file name="index.html">
  722. <div ng-controller="MainCtrl">
  723. <div ui-grid="gridOptions" ui-grid-save-state></div>
  724. </div>
  725. </file>
  726. </example>
  727. */
  728. module.directive('uiGridSaveState', ['uiGridSaveStateConstants', 'uiGridSaveStateService', 'gridUtil', '$compile',
  729. function (uiGridSaveStateConstants, uiGridSaveStateService, gridUtil, $compile) {
  730. return {
  731. replace: true,
  732. priority: 0,
  733. require: '^uiGrid',
  734. scope: false,
  735. link: function ($scope, $elm, $attrs, uiGridCtrl) {
  736. uiGridSaveStateService.initializeGrid(uiGridCtrl.grid);
  737. }
  738. };
  739. }
  740. ]);
  741. })();