1- import { $$ , ajaxForm } from "./utils.js" ;
1+ import { $$ , ajaxForm , pluckData } from "./utils.js" ;
22
33const djDebug = document . getElementById ( "djDebug" ) ;
44
5- $$ . on ( djDebug , "click" , ".switchHistory" , function ( event ) {
6- event . preventDefault ( ) ;
7- const newStoreId = this . dataset . storeId ;
8- const tbody = this . closest ( "tbody" ) ;
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 switchHistory ( newStoreId ) {
14+ const formTarget = djDebug . querySelector (
15+ ".switchHistory[data-store-id='" + newStoreId + "']"
16+ ) ;
17+ const tbody = formTarget . closest ( "tbody" ) ;
918
1019 const highlighted = tbody . querySelector ( ".djdt-highlighted" ) ;
1120 if ( highlighted ) {
1221 highlighted . classList . remove ( "djdt-highlighted" ) ;
1322 }
14- this . closest ( "tr" ) . classList . add ( "djdt-highlighted" ) ;
23+ formTarget . closest ( "tr" ) . classList . add ( "djdt-highlighted" ) ;
1524
16- ajaxForm ( this ) . then ( function ( data ) {
25+ ajaxForm ( formTarget ) . then ( function ( data ) {
1726 djDebug . setAttribute ( "data-store-id" , newStoreId ) ;
1827 // Check if response is empty, it could be due to an expired store_id.
1928 if ( Object . keys ( data ) . length === 0 ) {
@@ -32,20 +41,53 @@ $$.on(djDebug, "click", ".switchHistory", function (event) {
3241 } ) ;
3342 }
3443 } ) ;
35- } ) ;
44+ }
3645
37- $$ . on ( djDebug , "click" , ".refreshHistory" , function ( event ) {
38- event . preventDefault ( ) ;
46+ function refreshHistory ( ) {
47+ const formTarget = djDebug . querySelector ( ".refreshHistory" ) ;
3948 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 ( ) ;
49+ const oldIds = new Set (
50+ pluckData ( container . querySelectorAll ( "tr[data-store-id]" ) , "storeId" )
51+ ) ;
52+
53+ return ajaxForm ( formTarget )
54+ . then ( function ( data ) {
55+ // Remove existing rows first then re-populate with new data
56+ container
57+ . querySelectorAll ( "tr[data-store-id]" )
58+ . forEach ( function ( node ) {
59+ node . remove ( ) ;
60+ } ) ;
61+ data . requests . forEach ( function ( request ) {
62+ container . innerHTML = request . content + container . innerHTML ;
4663 } ) ;
47- data . requests . forEach ( function ( request ) {
48- container . innerHTML = request . content + container . innerHTML ;
64+ } )
65+ . then ( function ( ) {
66+ const allIds = new Set (
67+ pluckData (
68+ container . querySelectorAll ( "tr[data-store-id]" ) ,
69+ "storeId"
70+ )
71+ ) ;
72+ const newIds = difference ( allIds , oldIds ) ;
73+ const lastRequestId = newIds . values ( ) . next ( ) . value ;
74+ return {
75+ allIds,
76+ newIds,
77+ lastRequestId,
78+ } ;
4979 } ) ;
50- } ) ;
80+ }
81+
82+ $$ . on ( djDebug , "click" , ".switchHistory" , function ( event ) {
83+ event . preventDefault ( ) ;
84+ switchHistory ( this . dataset . storeId ) ;
85+ } ) ;
86+
87+ $$ . on ( djDebug , "click" , ".refreshHistory" , function ( event ) {
88+ event . preventDefault ( ) ;
89+ refreshHistory ( ) ;
5190} ) ;
91+
92+ window . djdt . refreshHistory = refreshHistory ;
93+ window . djdt . switchHistory = switchHistory ;
0 commit comments