Skip to content

Commit

Permalink
fix selector deleting bug (#126)
Browse files Browse the repository at this point in the history
* fix selector deleting bug

* fix

* fix

* add delete by bfs

* fix variables types, if-block conditions and methods names

* up version

* add list deleted children in modal window

* fix after review

* fix locales

Co-authored-by: S.duda <[email protected]>
  • Loading branch information
Hat331 and S.duda authored Jan 20, 2023
1 parent 9e1d7ac commit 574a620
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web-scraper-chrome-extension",
"version": "0.4.6",
"version": "0.4.7",
"description": "Web data extraction tool implemented as chrome extension",
"scripts": {
"lint": "eslint --ext .js src",
Expand Down
2 changes: 1 addition & 1 deletion src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@
"message": "Are you sure you want to delete sitemap <span id='modal-sitemap-id'></span>?"
},
"modal_confirm_action_message_delete_selector": {
"message": "The selector that is being deleted has child selectors (<span id='modal-child-count'></span> obj), which will be deleted"
"message": "The following selectors will be removed (<span id='modal-child-count'></span> obj):"
},
"modal_confirm_action_submit_delete_selector": { "message": "Confirm" },
"modal_confirm_action_cancel_delete_selector": { "message": "Cancel" },
Expand Down
2 changes: 1 addition & 1 deletion src/_locales/ru/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@
"message": "Вы действительно хотите удалить карту обхода <span id='modal-sitemap-id'></span>?"
},
"modal_confirm_action_message_delete_selector": {
"message": "У удаляемого селектора имеются дочерние (<span id='modal-child-count'></span> шт.), которые будут удалены"
"message": "Будут удалены следующие селекторы (<span id='modal-child-count'></span> шт.):"
},
"modal_confirm_action_submit_delete_selector": { "message": "Подтвердить" },
"modal_confirm_action_cancel_delete_selector": { "message": "Отмена" },
Expand Down
14 changes: 13 additions & 1 deletion src/scripts/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1499,11 +1499,23 @@ export default class SitemapController {
async deleteSelector(button) {
const selector = $(button).closest('tr').data('selector');
const sitemap = this.state.currentSitemap;
const childCount = sitemap.getDirectChildSelectors(selector.uuid).length;
const clearSelectorList = sitemap.createRemainingSelectorsList(selector);
const filteredChildren = sitemap.selectors
.filter(selector => !clearSelectorList.includes(selector))
.map(selector => {
return { uuid: selector.uuid, id: selector.id };
});
const childCount = filteredChildren.length;
this.initConfirmActionPanel({ action: 'delete_selector' });
$('#modal-selector-id').text(selector.id);
if (childCount) {
$('#modal-child-count').text(childCount);
$('#modal-message').after('<ul id="list-deleted-children"></ul>');
filteredChildren.forEach(child => {
const $child = $('<li></li>');
$child.text(`#${child.uuid} ${child.id}`);
$('#list-deleted-children').append($child);
});
$('#modal-message').show();
}
this.state.currentSelector = selector;
Expand Down
49 changes: 33 additions & 16 deletions src/scripts/Sitemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,24 +174,41 @@ export default class Sitemap {
}
}

deleteSelector(selectorToDelete) {
this.selectors.forEach(
function (selector) {
if (selector.hasParentSelector(selectorToDelete.uuid)) {
selector.removeParentSelector(selectorToDelete.uuid);
if (selector.parentSelectors.length === 0) {
this.deleteSelector(selector);
}
}
}.bind(this)
);
cleanRedundantParents(selectorsList) {
const currentUuidList = selectorsList.map(selector => selector.uuid);
currentUuidList.push(this.rootSelector.uuid);
selectorsList.forEach(selector => {
selector.parentSelectors = selector.parentSelectors.filter(uuid =>
currentUuidList.includes(uuid)
);
});
return selectorsList;
}

for (const i in this.selectors) {
if (this.selectors[i].uuid === selectorToDelete.uuid) {
this.selectors.splice(i, 1);
break;
}
createRemainingSelectorsList(selectorToDelete) {
const newList = [];
const selectorsQueue = [this.rootSelector];
while (selectorsQueue.length > 0) {
const currentSelector = selectorsQueue.shift();
const selectorChildren = this.selectors.filter(selector =>
selector.parentSelectors.includes(currentSelector.uuid)
);

selectorChildren.forEach(child => {
if (child.uuid !== selectorToDelete.uuid && !newList.includes(child)) {
selectorsQueue.push(child);
newList.push(child);
}
});
}
return newList;
}

deleteSelector(selectorToDelete) {
const newListSelectors = this.cleanRedundantParents(
this.createRemainingSelectorsList(selectorToDelete)
);
this.selectors = new SelectorList(newListSelectors);
}

getDataTableId() {
Expand Down

0 comments on commit 574a620

Please sign in to comment.