Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move confirm delete row and before delete row check to util #1235

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased]
### Added
* Add isDesktop, isMobile function to control device service.
* Add `check-function-when-delete-rows-and-records` util for check actions that triggered before delete row and confirm.

## [2.6.0-beta.1] - 2020-11-15
### Added
Expand Down
1 change: 1 addition & 0 deletions addon/components/flexberry-groupedit.js
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ export default FlexberryBaseComponent.extend({
@method confirmDeleteRow
@param {DS.Model} record The record to be deleted.
@return {Boolean|Promise} If `true`, then delete row, if `Promise`, then delete row after successful resolve, else cancel.
@deprecated Use `confirmDeleteRows`.
*/
confirmDeleteRow: undefined,

Expand Down
1 change: 1 addition & 0 deletions addon/components/flexberry-objectlistview.js
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,7 @@ export default FlexberryBaseComponent.extend({
@method confirmDeleteRow
@param {DS.Model} record The record to be deleted.
@return {Boolean|Promise} If `true`, then delete row, if `Promise`, then delete row after successful resolve, else cancel.
@deprecated Use `confirmDeleteRows`.
*/
confirmDeleteRow: undefined,

Expand Down
44 changes: 9 additions & 35 deletions addon/components/flexberry-simpleolv.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { translationMacro as t } from 'ember-i18n';
import { getValueFromLocales } from 'ember-flexberry-data/utils/model-functions';
import serializeSortingParam from '../utils/serialize-sorting-param';
import getAttrLocaleKey from '../utils/get-attr-locale-key';
import { checkConfirmDeleteRows, checkBeforeDeleteRecord } from '../utils/check-function-when-delete-rows-and-records';
const { getOwner } = Ember;

/**
Expand Down Expand Up @@ -864,26 +865,13 @@ export default folv.extend(
return;
}

let confirmDeleteRow = this.get('confirmDeleteRow');
let possiblePromise = null;

if (confirmDeleteRow) {
Ember.assert('Error: confirmDeleteRow must be a function.', typeof confirmDeleteRow === 'function');
let checkConfirmDeleteRowsResult = checkConfirmDeleteRows(this.get('confirmDeleteRow'));

possiblePromise = confirmDeleteRow(recordWithKey.data);
if (!checkConfirmDeleteRowsResult) return;

if ((!possiblePromise || !(possiblePromise instanceof Ember.RSVP.Promise))) {
return;
}
}

if (possiblePromise || (possiblePromise instanceof Ember.RSVP.Promise)) {
possiblePromise.then(() => {
this._deleteRecord(recordWithKey.data, this.get('immediateDelete'));
});
} else {
checkConfirmDeleteRowsResult.then(() => {
this._deleteRecord(recordWithKey.data, this.get('immediateDelete'));
}
});
},

/**
Expand Down Expand Up @@ -2468,32 +2456,18 @@ export default folv.extend(
@param {Boolean} immediately If `true`, relationships have been destroyed (delete and save)
*/
_deleteRecord(record, immediately) {
let beforeDeleteRecord = this.get('beforeDeleteRecord');
let possiblePromise = null;
let data = {
immediately: immediately,
cancel: false
};

if (beforeDeleteRecord) {
Ember.assert('beforeDeleteRecord must be a function', typeof beforeDeleteRecord === 'function');

possiblePromise = beforeDeleteRecord(record, data);
let checkBeforeDeleteRecordResult = checkBeforeDeleteRecord(this.get('beforeDeleteRecord'), record, data);

if ((!possiblePromise || !(possiblePromise instanceof Ember.RSVP.Promise)) && data.cancel) {
return;
}
}
if (!checkBeforeDeleteRecordResult) return;

if (possiblePromise || (possiblePromise instanceof Ember.RSVP.Promise)) {
possiblePromise.then(() => {
if (!data.cancel) {
this._actualDeleteRecord(record, data.immediately);
}
});
} else {
checkBeforeDeleteRecordResult.then(() => {
this._actualDeleteRecord(record, data.immediately);
}
});
},

/**
Expand Down
24 changes: 6 additions & 18 deletions addon/components/groupedit-toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import Ember from 'ember';
import FlexberryBaseComponent from './flexberry-base-component';
import { checkConfirmDeleteRows } from '../utils/check-function-when-delete-rows-and-records';

/**
Toolbar component for {{#crossLink "FlexberryGroupeditComponent"}}{{/crossLink}}.
Expand Down Expand Up @@ -136,27 +137,14 @@ export default FlexberryBaseComponent.extend({
return;
}

let confirmDeleteRows = this.get('confirmDeleteRows');
let possiblePromise = null;
let checkConfirmDeleteRowsResult = checkConfirmDeleteRows(this.get('confirmDeleteRow'));

if (confirmDeleteRows) {
Ember.assert('Error: confirmDeleteRows must be a function.', typeof confirmDeleteRows === 'function');
if (!checkConfirmDeleteRowsResult) return;

possiblePromise = confirmDeleteRows();

if ((!possiblePromise || !(possiblePromise instanceof Ember.RSVP.Promise))) {
return;
}
}

let componentName = this.get('componentName');
if (possiblePromise || (possiblePromise instanceof Ember.RSVP.Promise)) {
possiblePromise.then(() => {
this.get('_groupEditEventsService').deleteRowsTrigger(componentName);
});
} else {
checkConfirmDeleteRowsResult.then(() => {
let componentName = this.get('componentName');
this.get('_groupEditEventsService').deleteRowsTrigger(componentName);
}
});
},

/**
Expand Down
22 changes: 5 additions & 17 deletions addon/components/mobile/object-list-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import Ember from 'ember';
import ObjectListViewComponent from '../object-list-view';
import checkConfirmDeleteRows from '../utils/check-function-when-delete-rows-and-records';

/**
Mobile version of {{#crossLink "ObjectListViewComponent"}}{{/crossLink}} (with mobile-specific defaults).
Expand Down Expand Up @@ -152,26 +153,13 @@ export default ObjectListViewComponent.extend({
@method actions.deleteSelectedRow
*/
deleteSelectedRow() {
let confirmDeleteRows = this.get('confirmDeleteRows');
let possiblePromise = null;
let checkConfirmDeleteRowsResult = checkConfirmDeleteRows(this.get('confirmDeleteRows'));

if (confirmDeleteRows) {
Ember.assert('Error: confirmDeleteRows must be a function.', typeof confirmDeleteRows === 'function');
if (!checkConfirmDeleteRowsResult) return;

possiblePromise = confirmDeleteRows();

if ((!possiblePromise || !(possiblePromise instanceof Ember.RSVP.Promise))) {
return;
}
}

if (possiblePromise || (possiblePromise instanceof Ember.RSVP.Promise)) {
possiblePromise.then(() => {
this._confirmDeleteRows();
});
} else {
checkConfirmDeleteRowsResult.then(() => {
this._confirmDeleteRows();
}
});
},

/**
Expand Down
44 changes: 9 additions & 35 deletions addon/components/object-list-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import FlexberryLookupCompatibleComponentMixin from '../mixins/flexberry-lookup-
import FlexberryFileCompatibleComponentMixin from '../mixins/flexberry-file-compatible-component';
import getProjectionByName from '../utils/get-projection-by-name';
import runAfter from '../utils/run-after';
import { checkConfirmDeleteRows, checkBeforeDeleteRecord } from '../utils/check-function-when-delete-rows-and-records';

/**
Object list view component.
Expand Down Expand Up @@ -992,26 +993,13 @@ export default FlexberryBaseComponent.extend(
return;
}

let confirmDeleteRow = this.get('confirmDeleteRow');
let possiblePromise = null;

if (confirmDeleteRow) {
Ember.assert('Error: confirmDeleteRow must be a function.', typeof confirmDeleteRow === 'function');

possiblePromise = confirmDeleteRow(recordWithKey.data);
let checkConfirmDeleteRowsResult = checkConfirmDeleteRows(this.get('confirmDeleteRow'));

if ((!possiblePromise || !(possiblePromise instanceof Ember.RSVP.Promise))) {
return;
}
}
if (!checkConfirmDeleteRowsResult) return;

if (possiblePromise || (possiblePromise instanceof Ember.RSVP.Promise)) {
possiblePromise.then(() => {
this._deleteRecord(recordWithKey.data, this.get('immediateDelete'));
});
} else {
checkConfirmDeleteRowsResult.then(() => {
this._deleteRecord(recordWithKey.data, this.get('immediateDelete'));
}
});
},

/**
Expand Down Expand Up @@ -2435,32 +2423,18 @@ export default FlexberryBaseComponent.extend(
_deleteRecord(record, immediately) {
let currentController = this.get('currentController');
currentController.onDeleteActionStarted();
let beforeDeleteRecord = this.get('beforeDeleteRecord');
let possiblePromise = null;
let data = {
immediately: immediately,
cancel: false
};

if (beforeDeleteRecord) {
Ember.assert('beforeDeleteRecord must be a function', typeof beforeDeleteRecord === 'function');

possiblePromise = beforeDeleteRecord(record, data);
let checkBeforeDeleteRecordResult = checkBeforeDeleteRecord(this.get('beforeDeleteRecord'), record, data);

if ((!possiblePromise || !(possiblePromise instanceof Ember.RSVP.Promise)) && data.cancel) {
return;
}
}
if (!checkBeforeDeleteRecordResult) return;

if (possiblePromise || (possiblePromise instanceof Ember.RSVP.Promise)) {
possiblePromise.then(() => {
if (!data.cancel) {
this._actualDeleteRecord(record, data.immediately);
}
});
} else {
checkBeforeDeleteRecordResult.then(() => {
this._actualDeleteRecord(record, data.immediately);
}
});
},

/**
Expand Down
22 changes: 5 additions & 17 deletions addon/components/olv-toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import Ember from 'ember';
import FlexberryBaseComponent from './flexberry-base-component';
import serializeSortingParam from '../utils/serialize-sorting-param';
import { checkConfirmDeleteRows } from '../utils/check-function-when-delete-rows-and-records';
const { getOwner } = Ember;

/**
Expand Down Expand Up @@ -550,26 +551,13 @@ export default FlexberryBaseComponent.extend({
@public
*/
delete() {
let confirmDeleteRows = this.get('confirmDeleteRows');
let possiblePromise = null;
let checkConfirmDeleteRowsResult = checkConfirmDeleteRows(this.get('confirmDeleteRows'));

if (confirmDeleteRows) {
Ember.assert('Error: confirmDeleteRows must be a function.', typeof confirmDeleteRows === 'function');
if (!checkConfirmDeleteRowsResult) return;

possiblePromise = confirmDeleteRows();

if ((!possiblePromise || !(possiblePromise instanceof Ember.RSVP.Promise))) {
return;
}
}

if (possiblePromise || (possiblePromise instanceof Ember.RSVP.Promise)) {
possiblePromise.then(() => {
this._confirmDeleteRows();
});
} else {
checkConfirmDeleteRowsResult.then(() => {
this._confirmDeleteRows();
}
});
},

/**
Expand Down
51 changes: 51 additions & 0 deletions addon/utils/check-function-when-delete-rows-and-records.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
@module ember-flexberry
*/

import Ember from 'ember';

/**
Used for check confirm delete rows function and get result.

@method checkConfirmDeleteRows
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@method checkConfirmDeleteRows
@private
@method checkConfirmDeleteRows

@param {Function} confirmDeleteRowsFunction confirm function
@return {RSVP.Promise} Retriveved object from path
*/
let checkConfirmDeleteRows = function (confirmDeleteRowsFunction) {
let possiblePromise = Ember.RSVP.resolve();

if (confirmDeleteRowsFunction) {
Ember.assert('Error: confirmDeleteRows must be a function.', typeof confirmDeleteRowsFunction === 'function');
possiblePromise = confirmDeleteRowsFunction();
possiblePromise = (possiblePromise && (possiblePromise instanceof Ember.RSVP.Promise)) ? possiblePromise : null;
}

return possiblePromise;
};

/**
Used for check before delete record function and get result.

@method checkBeforeDeleteRecord
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@method checkBeforeDeleteRecord
@private
@method checkBeforeDeleteRecord

@param {Function} beforeDeleteRecordFunction before function
@param {DS.Model} record A record to delete
@param {Object} data Data object
@return {RSVP.Promise} Retriveved object from path
*/
let checkBeforeDeleteRecord = function (beforeDeleteRecordFunction, record, data) {
let possiblePromise = Ember.RSVP.resolve();

if (beforeDeleteRecordFunction) {
Ember.assert('Error: beforeDeleteRecord must be a function', typeof beforeDeleteRecordFunction === 'function');
let promis = beforeDeleteRecordFunction(record, data);
possiblePromise = (promis && (promis instanceof Ember.RSVP.Promise)) ? promis : possiblePromise;
possiblePromise = (data.cancel) ? null : possiblePromise;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Проверку data.cancel видимо придётся оставить за функцией, потому что это нужно проверять только после резолва промиса.

}

return possiblePromise;
};

export {
checkConfirmDeleteRows,
checkBeforeDeleteRecord
};
1 change: 0 additions & 1 deletion app/templates/components/flexberry-objectlistview.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
filterByAllWords=filterByAllWords
configurateRow=this.attrs.configurateRow
configurateSelectedRows=this.attrs.configurateSelectedRows
confirmDeleteRow=confirmDeleteRow
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Пока только устарело, нужно оставить.

beforeDeleteRecord=beforeDeleteRecord
beforeDeleteAllRecords=beforeDeleteAllRecords
action='objectListViewRowClick'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@
filterByAllWords=filterByAllWords
configurateRow=this.attrs.configurateRow
configurateSelectedRows=this.attrs.configurateSelectedRows
confirmDeleteRow=confirmDeleteRow
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

И это.

beforeDeleteRecord=beforeDeleteRecord
beforeDeleteAllRecords=beforeDeleteAllRecords
action='objectListViewRowClick'
Expand Down
Loading