@@ -2,6 +2,73 @@ import { $$, ajaxForm } from "./utils.js";
22
33const djDebug = document . getElementById ( "djDebug" ) ;
44
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+ function pluckData ( array , key ) {
14+ const data = [ ] ;
15+ array . forEach ( function ( obj ) {
16+ data . push ( obj . dataset [ key ] ) ;
17+ } ) ;
18+ return data ;
19+ }
20+
21+ function refreshHistory ( ) {
22+ const formTarget = djDebug . querySelector ( ".refreshHistory" ) ;
23+ const container = document . getElementById ( "djdtHistoryRequests" ) ;
24+ const oldIds = new Set (
25+ pluckData ( container . querySelectorAll ( "tr[data-store-id]" ) , "storeId" )
26+ ) ;
27+
28+ return ajaxForm ( formTarget )
29+ . then ( function ( data ) {
30+ // Remove existing rows first then re-populate with new data
31+ container
32+ . querySelectorAll ( "tr[data-store-id]" )
33+ . forEach ( function ( node ) {
34+ node . remove ( ) ;
35+ } ) ;
36+ data . requests . forEach ( function ( request ) {
37+ container . innerHTML = request . content + container . innerHTML ;
38+ } ) ;
39+ } )
40+ . then ( function ( ) {
41+ const allIds = new Set (
42+ pluckData (
43+ container . querySelectorAll ( "tr[data-store-id]" ) ,
44+ "storeId"
45+ )
46+ ) ;
47+ const newIds = difference ( allIds , oldIds ) ;
48+ const lastRequestId = newIds . values ( ) . next ( ) . value ;
49+ return {
50+ allIds,
51+ newIds,
52+ lastRequestId,
53+ } ;
54+ } )
55+ . then ( function ( refreshInfo ) {
56+ refreshInfo . newIds . forEach ( function ( newId ) {
57+ const row = container . querySelector (
58+ `tr[data-store-id="${ newId } "]`
59+ ) ;
60+ row . classList . add ( "flash-new" ) ;
61+ } ) ;
62+ setTimeout ( ( ) => {
63+ container
64+ . querySelectorAll ( "tr[data-store-id]" )
65+ . forEach ( ( row ) => {
66+ row . classList . remove ( "flash-new" ) ;
67+ } ) ;
68+ } , 2000 ) ;
69+ } ) ;
70+ }
71+
572$$ . on ( djDebug , "click" , ".switchHistory" , function ( event ) {
673 event . preventDefault ( ) ;
774 const newStoreId = this . dataset . storeId ;
@@ -36,16 +103,5 @@ $$.on(djDebug, "click", ".switchHistory", function (event) {
36103
37104$$ . on ( djDebug , "click" , ".refreshHistory" , function ( event ) {
38105 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- } ) ;
106+ refreshHistory ( ) ;
51107} ) ;
0 commit comments