@@ -2,6 +2,79 @@ import { $$, ajaxForm } from "./utils.js";
2
2
3
3
const djDebug = document . getElementById ( "djDebug" ) ;
4
4
5
+ function difference ( setA , setB ) {
6
+ const _difference = new Set ( setA ) ;
7
+ for ( const elem of setB ) {
8
+ _difference . delete ( elem ) ;
9
+ }
10
+ return _difference ;
11
+ }
12
+
13
+ /**
14
+ * Create an array of dataset properties from a NodeList.
15
+ * @param nodes
16
+ * @param key
17
+ * @returns {[] }
18
+ */
19
+ function pluckData ( nodes , key ) {
20
+ const data = [ ] ;
21
+ nodes . forEach ( function ( obj ) {
22
+ data . push ( obj . dataset [ key ] ) ;
23
+ } ) ;
24
+ return data ;
25
+ }
26
+
27
+ function refreshHistory ( ) {
28
+ const formTarget = djDebug . querySelector ( ".refreshHistory" ) ;
29
+ const container = document . getElementById ( "djdtHistoryRequests" ) ;
30
+ const oldIds = new Set (
31
+ pluckData ( container . querySelectorAll ( "tr[data-store-id]" ) , "storeId" )
32
+ ) ;
33
+
34
+ return ajaxForm ( formTarget )
35
+ . then ( function ( data ) {
36
+ // Remove existing rows first then re-populate with new data
37
+ container
38
+ . querySelectorAll ( "tr[data-store-id]" )
39
+ . forEach ( function ( node ) {
40
+ node . remove ( ) ;
41
+ } ) ;
42
+ data . requests . forEach ( function ( request ) {
43
+ container . innerHTML = request . content + container . innerHTML ;
44
+ } ) ;
45
+ } )
46
+ . then ( function ( ) {
47
+ const allIds = new Set (
48
+ pluckData (
49
+ container . querySelectorAll ( "tr[data-store-id]" ) ,
50
+ "storeId"
51
+ )
52
+ ) ;
53
+ const newIds = difference ( allIds , oldIds ) ;
54
+ const lastRequestId = newIds . values ( ) . next ( ) . value ;
55
+ return {
56
+ allIds,
57
+ newIds,
58
+ lastRequestId,
59
+ } ;
60
+ } )
61
+ . then ( function ( refreshInfo ) {
62
+ refreshInfo . newIds . forEach ( function ( newId ) {
63
+ const row = container . querySelector (
64
+ `tr[data-store-id="${ newId } "]`
65
+ ) ;
66
+ row . classList . add ( "flash-new" ) ;
67
+ } ) ;
68
+ setTimeout ( ( ) => {
69
+ container
70
+ . querySelectorAll ( "tr[data-store-id]" )
71
+ . forEach ( ( row ) => {
72
+ row . classList . remove ( "flash-new" ) ;
73
+ } ) ;
74
+ } , 2000 ) ;
75
+ } ) ;
76
+ }
77
+
5
78
$$ . on ( djDebug , "click" , ".switchHistory" , function ( event ) {
6
79
event . preventDefault ( ) ;
7
80
const newStoreId = this . dataset . storeId ;
@@ -36,16 +109,5 @@ $$.on(djDebug, "click", ".switchHistory", function (event) {
36
109
37
110
$$ . on ( djDebug , "click" , ".refreshHistory" , function ( event ) {
38
111
event . preventDefault ( ) ;
39
- const container = document . getElementById ( "djdtHistoryRequests" ) ;
40
- ajaxForm ( this ) . then ( function ( data ) {
41
- // Remove existing rows first then re-populate with new data
42
- container
43
- . querySelectorAll ( "tr[data-store-id]" )
44
- . forEach ( function ( node ) {
45
- node . remove ( ) ;
46
- } ) ;
47
- data . requests . forEach ( function ( request ) {
48
- container . innerHTML = request . content + container . innerHTML ;
49
- } ) ;
50
- } ) ;
112
+ refreshHistory ( ) ;
51
113
} ) ;
0 commit comments