|
1 | 1 | import Bubble from './bubble.js'
|
2 | 2 | import List from './list.js'
|
3 | 3 |
|
4 |
| -export default class Parser { |
5 |
| - constructor() { |
| 4 | +// CLASS_MAIN identifies the main timesheet container |
| 5 | +const CLASS_MAIN = 'timesheet'; |
6 | 6 |
|
7 |
| - } |
| 7 | +// CLASS_ITEM stores the HTML class name which indicates an item for timesheet |
| 8 | +const CLASS_ITEM = 'timesheet-item'; |
8 | 9 |
|
9 |
| - parse(html) { |
10 |
| - var list = new List((a, b) { |
| 10 | +// CLASS_ITEM_DATE_START identifies the start date of an item |
| 11 | +const CLASS_ITEM_DATE_START = 'timesheet-item--date-start'; |
| 12 | + |
| 13 | +// CLASS_ITEM_DATE_END identifies the end date of an item |
| 14 | +const CLASS_ITEM_DATE_END = 'timesheet-item--date-end'; |
| 15 | + |
| 16 | +// CLASS_ITEM_LABEL identifies the label of an item |
| 17 | +const CLASS_ITEM_LABEL = 'timesheet-item--label'; |
| 18 | + |
| 19 | +// Parser is used to parse the HTML DOM into timesheet data |
| 20 | +export default class Parser { |
| 21 | + constructor() { |
| 22 | + this.list = new List((a, b) { |
11 | 23 | return a.Start() < b.Start() ? -1 : 1
|
12 | 24 | });
|
| 25 | + } |
13 | 26 |
|
14 |
| - if (!html.classList.contains('timesheet')) { |
15 |
| - return list; |
16 |
| - } |
17 |
| - |
18 |
| - var items = html.querySelectorAll('.timesheet-item'); |
19 |
| - if (items.length === 0) { |
20 |
| - return list; |
| 27 | + parse(html) { |
| 28 | + // Return an empty List if the passed element does not have the needed |
| 29 | + // timesheet class. |
| 30 | + if (!html.classList.contains(CLASS_MAIN)) { |
| 31 | + return this.list; |
21 | 32 | }
|
22 | 33 |
|
23 |
| - |
| 34 | + // Get all items with timesheet-item class from the list |
| 35 | + var items = html.querySelectorAll('.' + CLASS_ITEM); |
24 | 36 | for (var i = 0, m = items.length; i < m; i++) {
|
25 | 37 | var item = items[i];
|
26 | 38 |
|
27 |
| - var dateStart = item.querySelector('.timesheet-item--date-start') |
28 |
| - , dateEnd = item.querySelector('.timesheet-item--date-end') |
29 |
| - , label = item.querySelector('.timesheet-item--label'); |
| 39 | + // Get needed elements from timsheet item |
| 40 | + var dateStart = item.querySelector('.' + CLASS_ITEM_DATE_START) |
| 41 | + , dateEnd = item.querySelector('.' + CLASS_ITEM_DATE_END) |
| 42 | + , label = item.querySelector('.' + CLASS_ITEM_LABEL); |
30 | 43 |
|
| 44 | + // Skip the item if not all needed elements are found |
31 | 45 | if (dateStart === null || dateEnd === null || label === null) {
|
32 | 46 | continue;
|
33 | 47 | }
|
34 | 48 |
|
35 |
| - list.Add(new Bubble( |
| 49 | + // Add the parsed Bubble to the List |
| 50 | + this.list.Add(new Bubble( |
36 | 51 | dateStart ? dateStart.innerHTML : null,
|
37 | 52 | dateEnd ? dateEnd.innerHTML : null,
|
38 | 53 | label ? label.innerHTML : null
|
|
0 commit comments