ui-grid.row-edit.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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.rowEdit
  10. * @description
  11. *
  12. * # ui.grid.rowEdit
  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 extends the edit feature to provide tracking and saving of rows
  17. * of data. The tutorial provides more information on how this feature is best
  18. * used {@link tutorial/205_row_editable here}.
  19. * <br/>
  20. * This feature depends on usage of the ui-grid-edit feature, and also benefits
  21. * from use of ui-grid-cellNav to provide the full spreadsheet-like editing
  22. * experience
  23. *
  24. */
  25. var module = angular.module('ui.grid.rowEdit', ['ui.grid', 'ui.grid.edit', 'ui.grid.cellNav']);
  26. /**
  27. * @ngdoc object
  28. * @name ui.grid.rowEdit.constant:uiGridRowEditConstants
  29. *
  30. * @description constants available in row edit module
  31. */
  32. module.constant('uiGridRowEditConstants', {
  33. });
  34. /**
  35. * @ngdoc service
  36. * @name ui.grid.rowEdit.service:uiGridRowEditService
  37. *
  38. * @description Services for row editing features
  39. */
  40. module.service('uiGridRowEditService', ['$interval', '$q', 'uiGridConstants', 'uiGridRowEditConstants', 'gridUtil',
  41. function ($interval, $q, uiGridConstants, uiGridRowEditConstants, gridUtil) {
  42. var service = {
  43. initializeGrid: function (scope, grid) {
  44. /**
  45. * @ngdoc object
  46. * @name ui.grid.rowEdit.api:PublicApi
  47. *
  48. * @description Public Api for rowEdit feature
  49. */
  50. grid.rowEdit = {};
  51. var publicApi = {
  52. events: {
  53. rowEdit: {
  54. /**
  55. * @ngdoc event
  56. * @eventOf ui.grid.rowEdit.api:PublicApi
  57. * @name saveRow
  58. * @description raised when a row is ready for saving. Once your
  59. * row has saved you may need to use angular.extend to update the
  60. * data entity with any changed data from your save (for example,
  61. * lock version information if you're using optimistic locking,
  62. * or last update time/user information).
  63. *
  64. * Your method should call setSavePromise somewhere in the body before
  65. * returning control. The feature will then wait, with the gridRow greyed out
  66. * whilst this promise is being resolved.
  67. *
  68. * <pre>
  69. * gridApi.rowEdit.on.saveRow(scope,function(rowEntity){})
  70. * </pre>
  71. * and somewhere within the event handler:
  72. * <pre>
  73. * gridApi.rowEdit.setSavePromise( rowEntity, savePromise)
  74. * </pre>
  75. * @param {object} rowEntity the options.data element that was edited
  76. * @returns {promise} Your saveRow method should return a promise, the
  77. * promise should either be resolved (implying successful save), or
  78. * rejected (implying an error).
  79. */
  80. saveRow: function (rowEntity) {
  81. }
  82. }
  83. },
  84. methods: {
  85. rowEdit: {
  86. /**
  87. * @ngdoc method
  88. * @methodOf ui.grid.rowEdit.api:PublicApi
  89. * @name setSavePromise
  90. * @description Sets the promise associated with the row save, mandatory that
  91. * the saveRow event handler calls this method somewhere before returning.
  92. * <pre>
  93. * gridApi.rowEdit.setSavePromise(rowEntity, savePromise)
  94. * </pre>
  95. * @param {object} rowEntity a data row from the grid for which a save has
  96. * been initiated
  97. * @param {promise} savePromise the promise that will be resolved when the
  98. * save is successful, or rejected if the save fails
  99. *
  100. */
  101. setSavePromise: function ( rowEntity, savePromise) {
  102. service.setSavePromise(grid, rowEntity, savePromise);
  103. },
  104. /**
  105. * @ngdoc method
  106. * @methodOf ui.grid.rowEdit.api:PublicApi
  107. * @name getDirtyRows
  108. * @description Returns all currently dirty rows
  109. * <pre>
  110. * gridApi.rowEdit.getDirtyRows(grid)
  111. * </pre>
  112. * @returns {array} An array of gridRows that are currently dirty
  113. *
  114. */
  115. getDirtyRows: function () {
  116. return grid.rowEdit.dirtyRows ? grid.rowEdit.dirtyRows : [];
  117. },
  118. /**
  119. * @ngdoc method
  120. * @methodOf ui.grid.rowEdit.api:PublicApi
  121. * @name getErrorRows
  122. * @description Returns all currently errored rows
  123. * <pre>
  124. * gridApi.rowEdit.getErrorRows(grid)
  125. * </pre>
  126. * @returns {array} An array of gridRows that are currently in error
  127. *
  128. */
  129. getErrorRows: function () {
  130. return grid.rowEdit.errorRows ? grid.rowEdit.errorRows : [];
  131. },
  132. /**
  133. * @ngdoc method
  134. * @methodOf ui.grid.rowEdit.api:PublicApi
  135. * @name flushDirtyRows
  136. * @description Triggers a save event for all currently dirty rows, could
  137. * be used where user presses a save button or navigates away from the page
  138. * <pre>
  139. * gridApi.rowEdit.flushDirtyRows(grid)
  140. * </pre>
  141. * @returns {promise} a promise that represents the aggregate of all
  142. * of the individual save promises - i.e. it will be resolved when all
  143. * the individual save promises have been resolved.
  144. *
  145. */
  146. flushDirtyRows: function () {
  147. return service.flushDirtyRows(grid);
  148. },
  149. /**
  150. * @ngdoc method
  151. * @methodOf ui.grid.rowEdit.api:PublicApi
  152. * @name setRowsDirty
  153. * @description Sets each of the rows passed in dataRows
  154. * to be dirty. note that if you have only just inserted the
  155. * rows into your data you will need to wait for a $digest cycle
  156. * before the gridRows are present - so often you would wrap this
  157. * call in a $interval or $timeout
  158. * <pre>
  159. * $interval( function() {
  160. * gridApi.rowEdit.setRowsDirty(myDataRows);
  161. * }, 0, 1);
  162. * </pre>
  163. * @param {array} dataRows the data entities for which the gridRows
  164. * should be set dirty.
  165. *
  166. */
  167. setRowsDirty: function ( dataRows) {
  168. service.setRowsDirty(grid, dataRows);
  169. },
  170. /**
  171. * @ngdoc method
  172. * @methodOf ui.grid.rowEdit.api:PublicApi
  173. * @name setRowsClean
  174. * @description Sets each of the rows passed in dataRows
  175. * to be clean, removing them from the dirty cache and the error cache,
  176. * and clearing the error flag and the dirty flag
  177. * <pre>
  178. * var gridRows = $scope.gridApi.rowEdit.getDirtyRows();
  179. * var dataRows = gridRows.map( function( gridRow ) { return gridRow.entity; });
  180. * $scope.gridApi.rowEdit.setRowsClean( dataRows );
  181. * </pre>
  182. * @param {array} dataRows the data entities for which the gridRows
  183. * should be set clean.
  184. *
  185. */
  186. setRowsClean: function ( dataRows) {
  187. service.setRowsClean(grid, dataRows);
  188. }
  189. }
  190. }
  191. };
  192. grid.api.registerEventsFromObject(publicApi.events);
  193. grid.api.registerMethodsFromObject(publicApi.methods);
  194. grid.api.core.on.renderingComplete( scope, function ( gridApi ) {
  195. grid.api.edit.on.afterCellEdit( scope, service.endEditCell );
  196. grid.api.edit.on.beginCellEdit( scope, service.beginEditCell );
  197. grid.api.edit.on.cancelCellEdit( scope, service.cancelEditCell );
  198. if ( grid.api.cellNav ) {
  199. grid.api.cellNav.on.navigate( scope, service.navigate );
  200. }
  201. });
  202. },
  203. defaultGridOptions: function (gridOptions) {
  204. /**
  205. * @ngdoc object
  206. * @name ui.grid.rowEdit.api:GridOptions
  207. *
  208. * @description Options for configuring the rowEdit feature, these are available to be
  209. * set using the ui-grid {@link ui.grid.class:GridOptions gridOptions}
  210. */
  211. },
  212. /**
  213. * @ngdoc method
  214. * @methodOf ui.grid.rowEdit.service:uiGridRowEditService
  215. * @name saveRow
  216. * @description Returns a function that saves the specified row from the grid,
  217. * and returns a promise
  218. * @param {object} grid the grid for which dirty rows should be flushed
  219. * @param {GridRow} gridRow the row that should be saved
  220. * @returns {function} the saveRow function returns a function. That function
  221. * in turn, when called, returns a promise relating to the save callback
  222. */
  223. saveRow: function ( grid, gridRow ) {
  224. var self = this;
  225. return function() {
  226. gridRow.isSaving = true;
  227. if ( gridRow.rowEditSavePromise ){
  228. // don't save the row again if it's already saving - that causes stale object exceptions
  229. return gridRow.rowEditSavePromise;
  230. }
  231. var promise = grid.api.rowEdit.raise.saveRow( gridRow.entity );
  232. if ( gridRow.rowEditSavePromise ){
  233. gridRow.rowEditSavePromise.then( self.processSuccessPromise( grid, gridRow ), self.processErrorPromise( grid, gridRow ));
  234. } else {
  235. gridUtil.logError( 'A promise was not returned when saveRow event was raised, either nobody is listening to event, or event handler did not return a promise' );
  236. }
  237. return promise;
  238. };
  239. },
  240. /**
  241. * @ngdoc method
  242. * @methodOf ui.grid.rowEdit.service:uiGridRowEditService
  243. * @name setSavePromise
  244. * @description Sets the promise associated with the row save, mandatory that
  245. * the saveRow event handler calls this method somewhere before returning.
  246. * <pre>
  247. * gridApi.rowEdit.setSavePromise(grid, rowEntity)
  248. * </pre>
  249. * @param {object} grid the grid for which dirty rows should be returned
  250. * @param {object} rowEntity a data row from the grid for which a save has
  251. * been initiated
  252. * @param {promise} savePromise the promise that will be resolved when the
  253. * save is successful, or rejected if the save fails
  254. *
  255. */
  256. setSavePromise: function (grid, rowEntity, savePromise) {
  257. var gridRow = grid.getRow( rowEntity );
  258. gridRow.rowEditSavePromise = savePromise;
  259. },
  260. /**
  261. * @ngdoc method
  262. * @methodOf ui.grid.rowEdit.service:uiGridRowEditService
  263. * @name processSuccessPromise
  264. * @description Returns a function that processes the successful
  265. * resolution of a save promise
  266. * @param {object} grid the grid for which the promise should be processed
  267. * @param {GridRow} gridRow the row that has been saved
  268. * @returns {function} the success handling function
  269. */
  270. processSuccessPromise: function ( grid, gridRow ) {
  271. var self = this;
  272. return function() {
  273. delete gridRow.isSaving;
  274. delete gridRow.isDirty;
  275. delete gridRow.isError;
  276. delete gridRow.rowEditSaveTimer;
  277. delete gridRow.rowEditSavePromise;
  278. self.removeRow( grid.rowEdit.errorRows, gridRow );
  279. self.removeRow( grid.rowEdit.dirtyRows, gridRow );
  280. };
  281. },
  282. /**
  283. * @ngdoc method
  284. * @methodOf ui.grid.rowEdit.service:uiGridRowEditService
  285. * @name processErrorPromise
  286. * @description Returns a function that processes the failed
  287. * resolution of a save promise
  288. * @param {object} grid the grid for which the promise should be processed
  289. * @param {GridRow} gridRow the row that is now in error
  290. * @returns {function} the error handling function
  291. */
  292. processErrorPromise: function ( grid, gridRow ) {
  293. return function() {
  294. delete gridRow.isSaving;
  295. delete gridRow.rowEditSaveTimer;
  296. delete gridRow.rowEditSavePromise;
  297. gridRow.isError = true;
  298. if (!grid.rowEdit.errorRows){
  299. grid.rowEdit.errorRows = [];
  300. }
  301. if (!service.isRowPresent( grid.rowEdit.errorRows, gridRow ) ){
  302. grid.rowEdit.errorRows.push( gridRow );
  303. }
  304. };
  305. },
  306. /**
  307. * @ngdoc method
  308. * @methodOf ui.grid.rowEdit.service:uiGridRowEditService
  309. * @name removeRow
  310. * @description Removes a row from a cache of rows - either
  311. * grid.rowEdit.errorRows or grid.rowEdit.dirtyRows. If the row
  312. * is not present silently does nothing.
  313. * @param {array} rowArray the array from which to remove the row
  314. * @param {GridRow} gridRow the row that should be removed
  315. */
  316. removeRow: function( rowArray, removeGridRow ){
  317. if (typeof(rowArray) === 'undefined' || rowArray === null){
  318. return;
  319. }
  320. rowArray.forEach( function( gridRow, index ){
  321. if ( gridRow.uid === removeGridRow.uid ){
  322. rowArray.splice( index, 1);
  323. }
  324. });
  325. },
  326. /**
  327. * @ngdoc method
  328. * @methodOf ui.grid.rowEdit.service:uiGridRowEditService
  329. * @name isRowPresent
  330. * @description Checks whether a row is already present
  331. * in the given array
  332. * @param {array} rowArray the array in which to look for the row
  333. * @param {GridRow} gridRow the row that should be looked for
  334. */
  335. isRowPresent: function( rowArray, removeGridRow ){
  336. var present = false;
  337. rowArray.forEach( function( gridRow, index ){
  338. if ( gridRow.uid === removeGridRow.uid ){
  339. present = true;
  340. }
  341. });
  342. return present;
  343. },
  344. /**
  345. * @ngdoc method
  346. * @methodOf ui.grid.rowEdit.service:uiGridRowEditService
  347. * @name flushDirtyRows
  348. * @description Triggers a save event for all currently dirty rows, could
  349. * be used where user presses a save button or navigates away from the page
  350. * <pre>
  351. * gridApi.rowEdit.flushDirtyRows(grid)
  352. * </pre>
  353. * @param {object} grid the grid for which dirty rows should be flushed
  354. * @returns {promise} a promise that represents the aggregate of all
  355. * of the individual save promises - i.e. it will be resolved when all
  356. * the individual save promises have been resolved.
  357. *
  358. */
  359. flushDirtyRows: function(grid){
  360. var promises = [];
  361. grid.api.rowEdit.getDirtyRows().forEach( function( gridRow ){
  362. service.cancelTimer( grid, gridRow );
  363. service.saveRow( grid, gridRow )();
  364. promises.push( gridRow.rowEditSavePromise );
  365. });
  366. return $q.all( promises );
  367. },
  368. /**
  369. * @ngdoc method
  370. * @methodOf ui.grid.rowEdit.service:uiGridRowEditService
  371. * @name endEditCell
  372. * @description Receives an afterCellEdit event from the edit function,
  373. * and sets flags as appropriate. Only the rowEntity parameter
  374. * is processed, although other params are available. Grid
  375. * is automatically provided by the gridApi.
  376. * @param {object} rowEntity the data entity for which the cell
  377. * was edited
  378. */
  379. endEditCell: function( rowEntity, colDef, newValue, previousValue ){
  380. var grid = this.grid;
  381. var gridRow = grid.getRow( rowEntity );
  382. if ( !gridRow ){ gridUtil.logError( 'Unable to find rowEntity in grid data, dirty flag cannot be set' ); return; }
  383. if ( newValue !== previousValue || gridRow.isDirty ){
  384. if ( !grid.rowEdit.dirtyRows ){
  385. grid.rowEdit.dirtyRows = [];
  386. }
  387. if ( !gridRow.isDirty ){
  388. gridRow.isDirty = true;
  389. grid.rowEdit.dirtyRows.push( gridRow );
  390. }
  391. delete gridRow.isError;
  392. service.considerSetTimer( grid, gridRow );
  393. }
  394. },
  395. /**
  396. * @ngdoc method
  397. * @methodOf ui.grid.rowEdit.service:uiGridRowEditService
  398. * @name beginEditCell
  399. * @description Receives a beginCellEdit event from the edit function,
  400. * and cancels any rowEditSaveTimers if present, as the user is still editing
  401. * this row. Only the rowEntity parameter
  402. * is processed, although other params are available. Grid
  403. * is automatically provided by the gridApi.
  404. * @param {object} rowEntity the data entity for which the cell
  405. * editing has commenced
  406. */
  407. beginEditCell: function( rowEntity, colDef ){
  408. var grid = this.grid;
  409. var gridRow = grid.getRow( rowEntity );
  410. if ( !gridRow ){ gridUtil.logError( 'Unable to find rowEntity in grid data, timer cannot be cancelled' ); return; }
  411. service.cancelTimer( grid, gridRow );
  412. },
  413. /**
  414. * @ngdoc method
  415. * @methodOf ui.grid.rowEdit.service:uiGridRowEditService
  416. * @name cancelEditCell
  417. * @description Receives a cancelCellEdit event from the edit function,
  418. * and if the row was already dirty, restarts the save timer. If the row
  419. * was not already dirty, then it's not dirty now either and does nothing.
  420. *
  421. * Only the rowEntity parameter
  422. * is processed, although other params are available. Grid
  423. * is automatically provided by the gridApi.
  424. *
  425. * @param {object} rowEntity the data entity for which the cell
  426. * editing was cancelled
  427. */
  428. cancelEditCell: function( rowEntity, colDef ){
  429. var grid = this.grid;
  430. var gridRow = grid.getRow( rowEntity );
  431. if ( !gridRow ){ gridUtil.logError( 'Unable to find rowEntity in grid data, timer cannot be set' ); return; }
  432. service.considerSetTimer( grid, gridRow );
  433. },
  434. /**
  435. * @ngdoc method
  436. * @methodOf ui.grid.rowEdit.service:uiGridRowEditService
  437. * @name navigate
  438. * @description cellNav tells us that the selected cell has changed. If
  439. * the new row had a timer running, then stop it similar to in a beginCellEdit
  440. * call. If the old row is dirty and not the same as the new row, then
  441. * start a timer on it.
  442. * @param {object} newRowCol the row and column that were selected
  443. * @param {object} oldRowCol the row and column that was left
  444. *
  445. */
  446. navigate: function( newRowCol, oldRowCol ){
  447. var grid = this.grid;
  448. if ( newRowCol.row.rowEditSaveTimer ){
  449. service.cancelTimer( grid, newRowCol.row );
  450. }
  451. if ( oldRowCol && oldRowCol.row && oldRowCol.row !== newRowCol.row ){
  452. service.considerSetTimer( grid, oldRowCol.row );
  453. }
  454. },
  455. /**
  456. * @ngdoc property
  457. * @propertyOf ui.grid.rowEdit.api:GridOptions
  458. * @name rowEditWaitInterval
  459. * @description How long the grid should wait for another change on this row
  460. * before triggering a save (in milliseconds). If set to -1, then saves are
  461. * never triggered by timer (implying that the user will call flushDirtyRows()
  462. * manually)
  463. *
  464. * @example
  465. * Setting the wait interval to 4 seconds
  466. * <pre>
  467. * $scope.gridOptions = { rowEditWaitInterval: 4000 }
  468. * </pre>
  469. *
  470. */
  471. /**
  472. * @ngdoc method
  473. * @methodOf ui.grid.rowEdit.service:uiGridRowEditService
  474. * @name considerSetTimer
  475. * @description Consider setting a timer on this row (if it is dirty). if there is a timer running
  476. * on the row and the row isn't currently saving, cancel it, using cancelTimer, then if the row is
  477. * dirty and not currently saving then set a new timer
  478. * @param {object} grid the grid for which we are processing
  479. * @param {GridRow} gridRow the row for which the timer should be adjusted
  480. *
  481. */
  482. considerSetTimer: function( grid, gridRow ){
  483. service.cancelTimer( grid, gridRow );
  484. if ( gridRow.isDirty && !gridRow.isSaving ){
  485. if ( grid.options.rowEditWaitInterval !== -1 ){
  486. var waitTime = grid.options.rowEditWaitInterval ? grid.options.rowEditWaitInterval : 2000;
  487. gridRow.rowEditSaveTimer = $interval( service.saveRow( grid, gridRow ), waitTime, 1);
  488. }
  489. }
  490. },
  491. /**
  492. * @ngdoc method
  493. * @methodOf ui.grid.rowEdit.service:uiGridRowEditService
  494. * @name cancelTimer
  495. * @description cancel the $interval for any timer running on this row
  496. * then delete the timer itself
  497. * @param {object} grid the grid for which we are processing
  498. * @param {GridRow} gridRow the row for which the timer should be adjusted
  499. *
  500. */
  501. cancelTimer: function( grid, gridRow ){
  502. if ( gridRow.rowEditSaveTimer && !gridRow.isSaving ){
  503. $interval.cancel(gridRow.rowEditSaveTimer);
  504. delete gridRow.rowEditSaveTimer;
  505. }
  506. },
  507. /**
  508. * @ngdoc method
  509. * @methodOf ui.grid.rowEdit.service:uiGridRowEditService
  510. * @name setRowsDirty
  511. * @description Sets each of the rows passed in dataRows
  512. * to be dirty. note that if you have only just inserted the
  513. * rows into your data you will need to wait for a $digest cycle
  514. * before the gridRows are present - so often you would wrap this
  515. * call in a $interval or $timeout
  516. * <pre>
  517. * $interval( function() {
  518. * gridApi.rowEdit.setRowsDirty( myDataRows);
  519. * }, 0, 1);
  520. * </pre>
  521. * @param {object} grid the grid for which rows should be set dirty
  522. * @param {array} dataRows the data entities for which the gridRows
  523. * should be set dirty.
  524. *
  525. */
  526. setRowsDirty: function( grid, myDataRows ) {
  527. var gridRow;
  528. myDataRows.forEach( function( value, index ){
  529. gridRow = grid.getRow( value );
  530. if ( gridRow ){
  531. if ( !grid.rowEdit.dirtyRows ){
  532. grid.rowEdit.dirtyRows = [];
  533. }
  534. if ( !gridRow.isDirty ){
  535. gridRow.isDirty = true;
  536. grid.rowEdit.dirtyRows.push( gridRow );
  537. }
  538. delete gridRow.isError;
  539. service.considerSetTimer( grid, gridRow );
  540. } else {
  541. gridUtil.logError( "requested row not found in rowEdit.setRowsDirty, row was: " + value );
  542. }
  543. });
  544. },
  545. /**
  546. * @ngdoc method
  547. * @methodOf ui.grid.rowEdit.service:uiGridRowEditService
  548. * @name setRowsClean
  549. * @description Sets each of the rows passed in dataRows
  550. * to be clean, clearing the dirty flag and the error flag, and removing
  551. * the rows from the dirty and error caches.
  552. * @param {object} grid the grid for which rows should be set clean
  553. * @param {array} dataRows the data entities for which the gridRows
  554. * should be set clean.
  555. *
  556. */
  557. setRowsClean: function( grid, myDataRows ) {
  558. var gridRow;
  559. myDataRows.forEach( function( value, index ){
  560. gridRow = grid.getRow( value );
  561. if ( gridRow ){
  562. delete gridRow.isDirty;
  563. service.removeRow( grid.rowEdit.dirtyRows, gridRow );
  564. service.cancelTimer( grid, gridRow );
  565. delete gridRow.isError;
  566. service.removeRow( grid.rowEdit.errorRows, gridRow );
  567. } else {
  568. gridUtil.logError( "requested row not found in rowEdit.setRowsClean, row was: " + value );
  569. }
  570. });
  571. }
  572. };
  573. return service;
  574. }]);
  575. /**
  576. * @ngdoc directive
  577. * @name ui.grid.rowEdit.directive:uiGridEdit
  578. * @element div
  579. * @restrict A
  580. *
  581. * @description Adds row editing features to the ui-grid-edit directive.
  582. *
  583. */
  584. module.directive('uiGridRowEdit', ['gridUtil', 'uiGridRowEditService', 'uiGridEditConstants',
  585. function (gridUtil, uiGridRowEditService, uiGridEditConstants) {
  586. return {
  587. replace: true,
  588. priority: 0,
  589. require: '^uiGrid',
  590. scope: false,
  591. compile: function () {
  592. return {
  593. pre: function ($scope, $elm, $attrs, uiGridCtrl) {
  594. uiGridRowEditService.initializeGrid($scope, uiGridCtrl.grid);
  595. },
  596. post: function ($scope, $elm, $attrs, uiGridCtrl) {
  597. }
  598. };
  599. }
  600. };
  601. }]);
  602. /**
  603. * @ngdoc directive
  604. * @name ui.grid.rowEdit.directive:uiGridViewport
  605. * @element div
  606. *
  607. * @description Stacks on top of ui.grid.uiGridViewport to alter the attributes used
  608. * for the grid row to allow coloring of saving and error rows
  609. */
  610. module.directive('uiGridViewport',
  611. ['$compile', 'uiGridConstants', 'gridUtil', '$parse',
  612. function ($compile, uiGridConstants, gridUtil, $parse) {
  613. return {
  614. priority: -200, // run after default directive
  615. scope: false,
  616. compile: function ($elm, $attrs) {
  617. var rowRepeatDiv = angular.element($elm.children().children()[0]);
  618. var existingNgClass = rowRepeatDiv.attr("ng-class");
  619. var newNgClass = '';
  620. if ( existingNgClass ) {
  621. newNgClass = existingNgClass.slice(0, -1) + ", 'ui-grid-row-dirty': row.isDirty, 'ui-grid-row-saving': row.isSaving, 'ui-grid-row-error': row.isError}";
  622. } else {
  623. newNgClass = "{'ui-grid-row-dirty': row.isDirty, 'ui-grid-row-saving': row.isSaving, 'ui-grid-row-error': row.isError}";
  624. }
  625. rowRepeatDiv.attr("ng-class", newNgClass);
  626. return {
  627. pre: function ($scope, $elm, $attrs, controllers) {
  628. },
  629. post: function ($scope, $elm, $attrs, controllers) {
  630. }
  631. };
  632. }
  633. };
  634. }]);
  635. })();