yanweiyuan3
2023-08-09 588bc7829387dfc761cc25f06f77d4c81818bd10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { ExtractionType } from '../e2elib/lib/src/helper/enumhelper';
import { GanttChartNode } from '../e2elib/lib/src/pageobjects/ganttchart/ganttchartnode';
import { GanttChartRow } from '../e2elib/lib/src/pageobjects/ganttchart/ganttchartrow';
import { getHtmlContent, getValuePairsFromHtmlTable } from './utils';
import { convertDateToUTC } from './timeutils';
import { promise } from '../e2elib/node_modules/@types/selenium-webdriver/';
import { protractor } from '../e2elib/node_modules/protractor';
import { GanttChartNodeCompartment } from '../e2elib/lib/src/pageobjects/ganttchart/qgcnodecompartment';
import { GanttChart } from '../e2elib/lib/src/pageobjects/ganttchart/ganttchart.component';
 
export class GanttChartBase extends GanttChart {
 
  /**
   * Find a node in an array which its Start Date & End Date matches
   *
   * @param nodes An array of nodes to find the node
   * @param startDate The start date of the node to find
   * @param endDate The end date of the node to find
   * @param isUTC Indicate whether both the start and end date is in UTC
   */
  public async findNodeByStartEndDate(nodes: GanttChartNode[], startDate: Date, endDate: Date, isUTC: boolean = true): Promise<GanttChartNode | undefined> {
    if (!isUTC) {
      startDate = convertDateToUTC(startDate);
      endDate = convertDateToUTC(endDate);
    }
 
    for (const node of nodes) {
      const start = await node.getStartDate();
      const end = await node.getEndDate();
 
      if (start && end) {
        const startDateIsMatch = start.getTime() === startDate.getTime();
        const endDataIsMatch = end.getTime() === endDate.getTime();
 
        if (startDateIsMatch && endDataIsMatch) {
          return node;
        }
      }
    }
    return undefined;
  }
 
  /**
   * Get all the visible rows in the gantt chart
   *
   */
  public async getAllRows(): Promise<GanttChartRow[]> {
    const promises: promise.Promise<any>[] = [];
    const numRows = await this.getNumRows();
    for (let i = 0; i < numRows; i++) {
      promises.push(this.getRow(i));
    }
    return protractor.promise.all(promises);
  }
 
  /**
   * Get all the visible nodes in the gantt chart. If gantt chart row is provided, it will proceed to get all visible nodes from the given row,.
   *
   * @param gcRow Gantt chart row which will be extracted for all visible nodes in the given row.
   */
  public async getAllNodes(gcRow?: GanttChartRow): Promise<GanttChartNode[]> {
    const promises: promise.Promise<any>[] = [];
    if (gcRow) {
      const numNodes = await gcRow.getNumNodes();
      for (let i = 0; i < numNodes; i++) {
        promises.push(gcRow.getNode(i));
      }
    } else {
      // when gcRow param is not given, perform recursion for each row present in the gantt chart with gcRow param.
      // with gcRow param passed in, this else block will not execute. only if block will be executed.
      const rows = await this.getAllRows();
      for (const row of rows) {
        promises.concat(this.getAllNodes(row));
      }
    }
 
    return protractor.promise.all(promises);
  }
 
  /**
   * Get gantt chart row title
   *
   * @param gcRow Target GCRow to retrieve row title value
   */
  public async getGCRowTitle(gcRow: GanttChartRow): Promise<string> {
    const titleHTML = await gcRow.getTitle();
    return getHtmlContent(titleHTML).join(' ');
  }
 
  /**
   * Get the last node of a row in gantt chart
   *
   * @param row row to get its last node
   * @returns last node of the specified row
   */
  public async getLastNode(row: GanttChartRow): Promise<GanttChartNode> {
    const numNodes = await row.getNumNodes();
    return await row.getNode(numNodes - 1) as GanttChartNode;
  }
 
  /**
   * Get node of a row based on node text in gantt chart
   *
   * @param row row to get its last node
   * @returns node of the specified row based on node text
   */
  public async getNode(row: GanttChartRow, nodeText: string): Promise<GanttChartNode | undefined> {
    const rowNodes = await this.getAllNodes(row);
    for (const node of rowNodes) {
      if ((await this.getNodeText(node)) === nodeText) {
        return node;
      }
    }
    return undefined;
  }
 
  /**
   * Get nodes of a row that fall between the specified start and end date
   *
   * @param rowName Name of the row to get nodes
   * @param startDate Start date to get from
   * @param endDate End date to get from
   * @param isUTC Indicates to convert the start and end date into UTC
   * @returns Array of GanttChartNode
   */
  public async getNodesBetweenDates(rowName: string, startDate: Date, endDate: Date, isUTC: boolean = true): Promise<GanttChartNode[]> {
    const rows = await this.getRowsByTitle(rowName);
    if (!isUTC) {
      startDate = convertDateToUTC(startDate);
      endDate = convertDateToUTC(endDate);
    }
 
    expect(rows.length).toBe(1, `Row with title ${rowName} couldn't be found.`);
    expect(rows.length).not.toBeGreaterThan(1, `${rows.length} of rows with title ${rowName} are retrieved.`);
 
    const row = rows[0];
    const nodes = await row.getNodes(startDate, endDate, ExtractionType.BOTH, true);
 
    return nodes;
  }
 
  /**
   * Get the color of the Gantt chart nodes
   *
   * @param nodes Nodes to get its color
   * @param compartmentIndex The index of the compartment of the node
   */
  public async getNodesColor(nodes: GanttChartNode[], compartmentIndex: number = 0): Promise<string[]> {
    const colors: promise.Promise<string>[] = [];
 
    for (const node of nodes) {
      const color = node.getCompartment(compartmentIndex).then((gcCompartment: GanttChartNodeCompartment | undefined) => {
        if (gcCompartment) {
          return gcCompartment.getBackgroundColor();
        }
        return node.getBackgroundColor();
      });
 
      colors.push(color);
    }
 
    return protractor.promise.all(colors);
  }
 
  /**
   * Get the total number of nodes of all rows within the specified period
   *
   * @param periodStart Date of start of period
   * @param periodEnd Date of end of period
   * @returns Number of nodes of all rows within period
   */
  public async getNumNodesFromAllRowsWithinPeriod(periodStart: Date, periodEnd: Date): Promise<number> {
    let numNodes = 0;
    const rowNum = await this.getNumRows();
    for (let i = 0; i <= rowNum; i++) {
      const row = await this.getRow(i);
      if (row !== undefined) {
        const nodes = await row.getNodes(periodStart, periodEnd);
        numNodes += nodes.length;
      }
    }
    return numNodes;
  }
 
  /**
   * Get all the visible nodes' text of Gantt chart row
   *
   * @param rowTitle The title of the Gantt chart row
   */
  public async getNodesText(row: GanttChartRow): Promise<string[]> {
    const texts: promise.Promise<string>[] = [];
    let text: Promise<string>;
 
    const nodes = await this.getAllNodes(row);
 
    for (const node of nodes) {
      text = this.getNodeText(node);
      texts.push(text);
    }
 
    return protractor.promise.all(texts);
  }
 
  /**
   * Get GanttChartNode text
   *
   * @param gcNode Target GCNode to retrieve node text value
   */
  public async getNodeText(gcNode: GanttChartNode): Promise<string> {
    const nodeHTMLText = await gcNode.getLeftNodeText();
    const nodeText = getHtmlContent(nodeHTMLText).join(' ');
    return nodeText;
  }
 
  /**
   * Trigger the tooltip on the GC Node and retrieve its value
   *
   * @param gcNode Target GC node to retrieve tooltip value
   * @returns Map object with titles map to values
   *
   */
  public async getNodeTooltipValuePairs(gcNode: GanttChartNode): Promise<Map<string, string>> {
    const tooltip = await this.triggerTooltip(gcNode);
 
    return getValuePairsFromHtmlTable(tooltip);
  }
}