1
- function toggleRadio ( radio ) {
2
- const startDateInput = document . getElementById ( 'startingDate' ) ;
3
- const endDateInput = document . getElementById ( 'endingDate' ) ;
4
-
5
- if ( radio . id === 'lastWeekContribution' ) {
6
- startDateInput . value = getLastWeek ( ) ;
7
- endDateInput . value = getToday ( ) ;
8
- chrome . storage . local . set ( {
9
- startingDate : startDateInput . value ,
10
- endingDate : endDateInput . value ,
11
- lastWeekContribution : true ,
12
- yesterdayContribution : false
13
- } , ( ) => {
14
- window . generateScrumReport ( ) ;
15
- } ) ;
16
- } else {
17
- startDateInput . value = getYesterday ( ) ;
18
- endDateInput . value = getToday ( ) ;
19
- chrome . storage . local . set ( {
20
- startingDate : startDateInput . value ,
21
- endingDate : endDateInput . value ,
22
- lastWeekContribution : false ,
23
- yesterdayContribution : true
24
- } , ( ) => {
25
- window . generateScrumReport ( ) ;
26
- } ) ;
27
- }
28
- startDateInput . disabled = endDateInput . disabled = true ;
1
+ function getLastWeek ( ) {
2
+ var today = new Date ( ) ;
3
+ var lastWeek = new Date ( today . getFullYear ( ) , today . getMonth ( ) , today . getDate ( ) - 7 ) ;
4
+ var lastWeekMonth = lastWeek . getMonth ( ) + 1 ;
5
+ var lastWeekDay = lastWeek . getDate ( ) ;
6
+ var lastWeekYear = lastWeek . getFullYear ( ) ;
7
+ var lastWeekDisplayPadded =
8
+ ( '0000' + lastWeekYear . toString ( ) ) . slice ( - 4 ) +
9
+ '-' +
10
+ ( '00' + lastWeekMonth . toString ( ) ) . slice ( - 2 ) +
11
+ '-' +
12
+ ( '00' + lastWeekDay . toString ( ) ) . slice ( - 2 ) ;
13
+ return lastWeekDisplayPadded ;
29
14
}
30
- // Function to activate the date containers after toggling radios of last week and day
31
- document . getElementById ( 'customDateContainer' ) . addEventListener ( 'click' , ( ) => {
32
- document . querySelectorAll ( 'input[name="timeframe"]' ) . forEach ( radio => radio . checked = false ) ;
33
- document . getElementById ( 'startingDate' ) . disabled = false ;
34
- document . getElementById ( 'endingDate' ) . disabled = false ;
35
- chrome . storage . local . set ( {
36
- lastWeekContribution : false ,
37
- yesterdayContribution : false
38
- } ) ;
39
- } ) ;
40
-
41
15
42
- // Date change
43
- document . getElementById ( 'startingDate' ) . addEventListener ( 'change' , function ( ) {
44
- chrome . storage . local . set ( {
45
- startingDate : this . value ,
46
- lastWeekContribution : false ,
47
- yesterdayContribution : false
48
- } , ( ) => {
49
- if ( document . getElementById ( 'endingDate' ) . value ) {
50
- window . generateScrumReport ( ) ;
51
- }
52
- } ) ;
53
- } ) ;
54
-
55
- document . getElementById ( 'endingDate' ) . addEventListener ( 'change' , function ( ) {
56
- chrome . storage . local . set ( {
57
- endingDate : this . value ,
58
- lastWeekContribution : false ,
59
- yesterdayContribution : false
60
- } , ( ) => {
61
- if ( document . getElementById ( 'startingDate' ) . value ) {
62
- window . generateScrumReport ( ) ;
63
- }
64
- } ) ;
65
- } ) ;
16
+ function getToday ( ) {
17
+ var today = new Date ( ) ;
18
+ var WeekMonth = today . getMonth ( ) + 1 ;
19
+ var WeekDay = today . getDate ( ) ;
20
+ var WeekYear = today . getFullYear ( ) ;
21
+ var WeekDisplayPadded =
22
+ ( '0000' + WeekYear . toString ( ) ) . slice ( - 4 ) +
23
+ '-' +
24
+ ( '00' + WeekMonth . toString ( ) ) . slice ( - 2 ) +
25
+ '-' +
26
+ ( '00' + WeekDay . toString ( ) ) . slice ( - 2 ) ;
27
+ return WeekDisplayPadded ;
28
+ }
66
29
30
+ function getYesterday ( ) {
31
+ let today = new Date ( ) ;
32
+ let yesterday = new Date ( today . getFullYear ( ) , today . getMonth ( ) , today . getDate ( ) - 1 ) ;
33
+ let yesterdayMonth = yesterday . getMonth ( ) + 1 ;
34
+ let yesterdayDay = yesterday . getDate ( ) ;
35
+ let yesterdayYear = yesterday . getFullYear ( ) ;
36
+ let yesterdayPadded =
37
+ ( '0000' + yesterdayYear . toString ( ) ) . slice ( - 4 ) +
38
+ '-' +
39
+ ( '00' + yesterdayMonth . toString ( ) ) . slice ( - 2 ) +
40
+ '-' +
41
+ ( '00' + yesterdayDay . toString ( ) ) . slice ( - 2 ) ;
42
+ return yesterdayPadded ;
43
+ }
67
44
68
- // Dark mode
69
45
document . addEventListener ( 'DOMContentLoaded' , function ( ) {
46
+ // Dark mode setup
70
47
const darkModeToggle = document . querySelector ( 'img[alt="Night Mode"]' ) ;
71
48
const body = document . body ;
72
49
@@ -80,56 +57,165 @@ document.addEventListener('DOMContentLoaded', function() {
80
57
darkModeToggle . addEventListener ( 'click' , function ( ) {
81
58
body . classList . toggle ( 'dark-mode' ) ;
82
59
const isDarkMode = body . classList . contains ( 'dark-mode' ) ;
83
-
84
- // Save preference
85
60
chrome . storage . local . set ( { darkMode : isDarkMode } ) ;
86
-
87
- // Toggle icon
88
61
this . src = isDarkMode ? 'icons/light-mode.png' : 'icons/night-mode.png' ;
89
62
} ) ;
90
- } )
91
- document . addEventListener ( 'DOMContentLoaded' , function ( ) {
63
+
64
+ // Button setup
92
65
const generateBtn = document . getElementById ( 'generateReport' ) ;
93
66
const copyBtn = document . getElementById ( 'copyReport' ) ;
94
67
95
68
generateBtn . addEventListener ( 'click' , function ( ) {
96
69
this . innerHTML = '<i class="fa fa-spinner fa-spin"></i> Generating...' ;
97
70
this . disabled = true ;
98
-
99
71
window . generateScrumReport ( ) ;
100
72
} ) ;
101
73
102
74
copyBtn . addEventListener ( 'click' , function ( ) {
103
75
const scrumReport = document . getElementById ( 'scrumReport' ) ;
104
-
105
- // Create container for HTML content
106
76
const tempDiv = document . createElement ( 'div' ) ;
107
77
tempDiv . innerHTML = scrumReport . innerHTML ;
108
78
document . body . appendChild ( tempDiv ) ;
109
79
tempDiv . style . position = 'absolute' ;
110
80
tempDiv . style . left = '-9999px' ;
111
81
112
- // Select the content
113
82
const range = document . createRange ( ) ;
114
83
range . selectNode ( tempDiv ) ;
115
84
const selection = window . getSelection ( ) ;
116
85
selection . removeAllRanges ( ) ;
117
86
selection . addRange ( range ) ;
118
87
119
88
try {
120
- // Copy HTML content
121
- const success = document . execCommand ( 'copy' ) ;
122
- if ( ! success ) {
123
- throw new Error ( 'Copy command failed' ) ;
124
- }
125
- Materialize . toast ( 'Report copied with formatting!' , 3000 , 'green' ) ;
89
+ document . execCommand ( 'copy' ) ;
90
+ this . innerHTML = '<i class="fa fa-check"></i> Copied!' ;
91
+ setTimeout ( ( ) => {
92
+ this . innerHTML = '<i class="fa fa-copy"></i> Copy Report' ;
93
+ } , 2000 ) ;
126
94
} catch ( err ) {
127
- console . error ( 'Failed to copy:' , err ) ;
128
- Materialize . toast ( 'Failed to copy report' , 3000 , 'red' ) ;
95
+ console . error ( 'Failed to copy: ' , err ) ;
129
96
} finally {
130
- // Cleanup
131
97
selection . removeAllRanges ( ) ;
132
98
document . body . removeChild ( tempDiv ) ;
133
99
}
134
100
} ) ;
135
- } ) ;
101
+
102
+ // Custom date container click handler
103
+ document . getElementById ( 'customDateContainer' ) . addEventListener ( 'click' , ( ) => {
104
+ document . querySelectorAll ( 'input[name="timeframe"]' ) . forEach ( radio => {
105
+ radio . checked = false
106
+ radio . dataset . wasChecked = 'false'
107
+ } ) ;
108
+
109
+ const startDateInput = document . getElementById ( 'startingDate' ) ;
110
+ const endDateInput = document . getElementById ( 'endingDate' ) ;
111
+ startDateInput . disabled = false ;
112
+ endDateInput . disabled = false ;
113
+
114
+ chrome . storage . local . set ( {
115
+ lastWeekContribution : false ,
116
+ yesterdayContribution : false ,
117
+ selectedTimeframe : null
118
+ } ) ;
119
+ } ) ;
120
+
121
+ chrome . storage . local . get ( [
122
+ 'selectedTimeframe' ,
123
+ 'lastWeekContribution' ,
124
+ 'yesterdayContribution'
125
+ ] , ( items ) => {
126
+ console . log ( 'Restoring state:' , items ) ;
127
+
128
+ if ( ! items . selectedTimeframe ) {
129
+ items . selectedTimeframe = 'yesterdayContribution' ;
130
+ items . lastWeekContribution = false ;
131
+ items . yesterdayContribution = true ;
132
+ }
133
+
134
+ const radio = document . getElementById ( items . selectedTimeframe ) ;
135
+ if ( radio ) {
136
+ radio . checked = true ;
137
+ radio . dataset . wasChecked = 'true' ;
138
+
139
+ const startDateInput = document . getElementById ( 'startingDate' ) ;
140
+ const endDateInput = document . getElementById ( 'endingDate' ) ;
141
+
142
+ if ( items . selectedTimeframe === 'lastWeekContribution' ) {
143
+ startDateInput . value = getLastWeek ( ) ;
144
+ endDateInput . value = getToday ( ) ;
145
+ } else {
146
+ startDateInput . value = getYesterday ( ) ;
147
+ endDateInput . value = getToday ( ) ;
148
+ }
149
+
150
+ startDateInput . disabled = endDateInput . disabled = true ;
151
+
152
+ chrome . storage . local . set ( {
153
+ startingDate : startDateInput . value ,
154
+ endingDate : endDateInput . value ,
155
+ lastWeekContribution : items . selectedTimeframe === 'lastWeekContribution' ,
156
+ yesterdayContribution : items . selectedTimeframe === 'yesterdayContribution' ,
157
+ selectedTimeframe : items . selectedTimeframe
158
+ } ) ;
159
+ }
160
+ } ) ;
161
+
162
+ // Radio button click handlers with toggle functionality
163
+ document . querySelectorAll ( 'input[name="timeframe"]' ) . forEach ( radio => {
164
+ radio . addEventListener ( 'click' , function ( ) {
165
+ if ( this . dataset . wasChecked === 'true' ) {
166
+ this . checked = false ;
167
+ this . dataset . wasChecked = 'false' ;
168
+
169
+ const startDateInput = document . getElementById ( 'startingDate' ) ;
170
+ const endDateInput = document . getElementById ( 'endingDate' ) ;
171
+ startDateInput . disabled = false ;
172
+ endDateInput . disabled = false ;
173
+
174
+ chrome . storage . local . set ( {
175
+ lastWeekContribution : false ,
176
+ yesterdayContribution : false ,
177
+ selectedTimeframe : null
178
+ } ) ;
179
+ } else {
180
+ document . querySelectorAll ( 'input[name="timeframe"]' ) . forEach ( r => {
181
+ r . dataset . wasChecked = 'false' ;
182
+ } ) ;
183
+ this . dataset . wasChecked = 'true' ;
184
+ toggleRadio ( this ) ;
185
+ }
186
+ } ) ;
187
+ } ) ;
188
+ } ) ;
189
+
190
+ function toggleRadio ( radio ) {
191
+ const startDateInput = document . getElementById ( 'startingDate' ) ;
192
+ const endDateInput = document . getElementById ( 'endingDate' ) ;
193
+
194
+ console . log ( 'Toggling radio:' , radio . id ) ;
195
+
196
+ if ( radio . id === 'lastWeekContribution' ) {
197
+ startDateInput . value = getLastWeek ( ) ;
198
+ endDateInput . value = getToday ( ) ;
199
+ } else if ( radio . id === 'yesterdayContribution' ) {
200
+ startDateInput . value = getYesterday ( ) ;
201
+ endDateInput . value = getToday ( ) ;
202
+ }
203
+
204
+ startDateInput . disabled = endDateInput . disabled = true ;
205
+
206
+ chrome . storage . local . set ( {
207
+ startingDate : startDateInput . value ,
208
+ endingDate : endDateInput . value ,
209
+ lastWeekContribution : radio . id === 'lastWeekContribution' ,
210
+ yesterdayContribution : radio . id === 'yesterdayContribution' ,
211
+ selectedTimeframe : radio . id ,
212
+ githubCache : null // Clear cache to force new fetch
213
+ } , ( ) => {
214
+ console . log ( 'State saved, dates:' , {
215
+ start : startDateInput . value ,
216
+ end : endDateInput . value ,
217
+ isLastWeek : radio . id === 'lastWeekContribution'
218
+ } ) ;
219
+ window . generateScrumReport ( ) ;
220
+ } ) ;
221
+ }
0 commit comments