Skip to content

Commit 3c1a233

Browse files
committed
Add comments to bubble, list and parser
1 parent 40beb3e commit 3c1a233

File tree

3 files changed

+54
-18
lines changed

3 files changed

+54
-18
lines changed

src/bubble.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1+
// A bubble represents the colorized area with a start date, end date and a
2+
// label. Tags will be added as HTML classes for custom colors.
13
export default class Bubble {
2-
constructor(start, end, label) {
4+
constructor(start, end, label, tags) {
35
this.label = label;
6+
this.tags = tags || [];
47

58
this.date = {
69
start: Date.parse(start),
710
end: Date.parse(end)
811
};
912
}
1013

14+
// Label returns the Bubble's label text
1115
Label() {
1216
return this.label;
1317
}
1418

19+
// Start returns the Bubble's start date object
1520
Start() {
1621
return this.date.start;
1722
}
1823

24+
// End returns the Bubble's end date object
1925
End() {
2026
return this.date.end;
2127
}

src/list.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
// List is a basic wrapper for Array access
12
export default class List {
3+
4+
// When initializing a new List item you can pass a custom function to sort
5+
// the items stored in the List
26
constructor(sort) {
37
this.storage = [];
48
this.position = 0;
@@ -13,12 +17,18 @@ export default class List {
1317
this.storage.sort(this.sorter)
1418
}
1519

20+
// Next increase the position pointer and return the current element
1621
Next() {
1722
this.position++;
1823

24+
if (this.position > this.storage.length) {
25+
return null;
26+
}
27+
1928
return this.storage[this.position-1];
2029
}
2130

31+
// Add can receive one or multiple parameters which are added to the List
2232
Add() {
2333
for (var i = 0, m = arguments.length; i < m; i++) {
2434
this.storage.push(arguments[i])
@@ -27,22 +37,27 @@ export default class List {
2737
this.sort();
2838
}
2939

40+
// Walk is an alias for forEach
3041
Walk(func) {
3142
return this.storage.forEach(func);
3243
}
3344

45+
// First returns the first item of the List
3446
First() {
3547
return this.storage[0]
3648
}
3749

50+
// Last returns the last item of the List
3851
Last() {
3952
return this.storage[this.Size()-1]
4053
}
4154

55+
// Get returns all items in the List
4256
Get() {
4357
return this.storage;
4458
}
4559

60+
// Size returns the length of the List
4661
Size() {
4762
return this.storage.length;
4863
}

src/parser.js

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,53 @@
11
import Bubble from './bubble.js'
22
import List from './list.js'
33

4-
export default class Parser {
5-
constructor() {
4+
// CLASS_MAIN identifies the main timesheet container
5+
const CLASS_MAIN = 'timesheet';
66

7-
}
7+
// CLASS_ITEM stores the HTML class name which indicates an item for timesheet
8+
const CLASS_ITEM = 'timesheet-item';
89

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) {
1123
return a.Start() < b.Start() ? -1 : 1
1224
});
25+
}
1326

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;
2132
}
2233

23-
34+
// Get all items with timesheet-item class from the list
35+
var items = html.querySelectorAll('.' + CLASS_ITEM);
2436
for (var i = 0, m = items.length; i < m; i++) {
2537
var item = items[i];
2638

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);
3043

44+
// Skip the item if not all needed elements are found
3145
if (dateStart === null || dateEnd === null || label === null) {
3246
continue;
3347
}
3448

35-
list.Add(new Bubble(
49+
// Add the parsed Bubble to the List
50+
this.list.Add(new Bubble(
3651
dateStart ? dateStart.innerHTML : null,
3752
dateEnd ? dateEnd.innerHTML : null,
3853
label ? label.innerHTML : null

0 commit comments

Comments
 (0)