handlerLoopElements.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. async function loopElements({ data, id }, { refData }) {
  2. try {
  3. if (!this.activeTab.id) throw new Error('no-tab');
  4. if (this.loopList[data.loopId]) {
  5. const index = this.loopList[data.loopId].index + 1;
  6. this.loopList[data.loopId].index = index;
  7. refData.loopData[data.loopId] = {
  8. $index: index,
  9. data: this.loopList[data.loopId].data[index],
  10. };
  11. } else {
  12. const maxLoop = +data.maxLoop || 0;
  13. const { elements, url, loopId } = await this._sendMessageToTab({
  14. id,
  15. label: 'loop-data',
  16. data: {
  17. max: maxLoop,
  18. multiple: true,
  19. ...data,
  20. },
  21. });
  22. this.loopEls.push({
  23. url,
  24. loopId,
  25. max: maxLoop,
  26. blockId: id,
  27. findBy: data.findBy,
  28. selector: data.selector,
  29. });
  30. const loopPayload = {
  31. maxLoop,
  32. index: 0,
  33. blockId: id,
  34. data: elements,
  35. id: data.loopId,
  36. type: 'elements',
  37. };
  38. if (data.loadMoreAction !== 'none') {
  39. loopPayload.loadMoreAction = {
  40. maxLoop,
  41. loopAttrId: loopId,
  42. loopId: data.loopId,
  43. findBy: data.findBy,
  44. type: data.loadMoreAction,
  45. selector: data.selector.trim(),
  46. scrollToBottom: data.scrollToBottom,
  47. actionElMaxWaitTime: data.actionElMaxWaitTime,
  48. actionElSelector: data.actionElSelector.trim(),
  49. actionPageMaxWaitTime: data.actionPageMaxWaitTime,
  50. };
  51. }
  52. this.loopList[data.loopId] = loopPayload;
  53. /* eslint-disable-next-line */
  54. refData.loopData[data.loopId] = {
  55. $index: 0,
  56. data: elements[0],
  57. };
  58. }
  59. return {
  60. data: refData.loopData[data.loopId],
  61. nextBlockId: this.getBlockConnections(id),
  62. };
  63. } catch (error) {
  64. if (error?.message === 'element-not-found') {
  65. error.data = { selector: data.selector };
  66. }
  67. throw error;
  68. }
  69. }
  70. export default loopElements;