1- const $$ = {
1+ export const $$ = {
22 on ( root , eventName , selector , fn ) {
33 root . removeEventListener ( eventName , fn ) ;
44 root . addEventListener ( eventName , ( event ) => {
@@ -8,15 +8,15 @@ const $$ = {
88 }
99 } ) ;
1010 } ,
11+ /**
12+ * This is a helper function to attach a handler for a `djdt.panel.render`
13+ * event of a specific panel.
14+ *
15+ * root: The container element that the listener should be attached to.
16+ * panelId: The Id of the panel.
17+ * fn: A function to execute when the event is triggered.
18+ */
1119 onPanelRender ( root , panelId , fn ) {
12- /*
13- This is a helper function to attach a handler for a `djdt.panel.render`
14- event of a specific panel.
15-
16- root: The container element that the listener should be attached to.
17- panelId: The Id of the panel.
18- fn: A function to execute when the event is triggered.
19- */
2020 root . addEventListener ( "djdt.panel.render" , ( event ) => {
2121 if ( event . detail . panelId === panelId ) {
2222 fn . call ( event ) ;
@@ -48,12 +48,12 @@ const $$ = {
4848 document . head . appendChild ( el ) ;
4949 }
5050 } ,
51+ /**
52+ * Given a container element, apply styles set via data-djdt-styles attribute.
53+ * The format is data-djdt-styles="styleName1:value;styleName2:value2"
54+ * The style names should use the CSSStyleDeclaration camel cased names.
55+ */
5156 applyStyles ( container ) {
52- /*
53- * Given a container element, apply styles set via data-djdt-styles attribute.
54- * The format is data-djdt-styles="styleName1:value;styleName2:value2"
55- * The style names should use the CSSStyleDeclaration camel cased names.
56- */
5757 for ( const element of container . querySelectorAll (
5858 "[data-djdt-styles]"
5959 ) ) {
@@ -85,34 +85,31 @@ function getDebugElement() {
8585 return root . querySelector ( "#djDebug" ) ;
8686}
8787
88- function ajax ( url , init ) {
89- return fetch ( url , Object . assign ( { credentials : "same-origin" } , init ) )
90- . then ( ( response ) => {
91- if ( response . ok ) {
92- return response
93- . json ( )
94- . catch ( ( error ) =>
95- Promise . reject (
96- new Error (
97- `The response is a invalid Json object : ${ error } `
98- )
99- )
100- ) ;
101- }
102- return Promise . reject (
103- new Error ( `${ response . status } : ${ response . statusText } ` )
104- ) ;
105- } )
106- . catch ( ( error ) => {
107- const djDebug = getDebugElement ( ) ;
108- const win = djDebug . querySelector ( "#djDebugWindow" ) ;
109- win . innerHTML = `<div class="djDebugPanelTitle"><h3>${ error . message } </h3><button type="button" class="djDebugClose">»</button></div>` ;
110- $$ . show ( win ) ;
111- throw error ;
88+ export async function ajax ( url , init ) {
89+ try {
90+ const response = await fetch ( url , {
91+ credentials : "same-origin" ,
92+ ...init ,
11293 } ) ;
94+ if ( response . ok ) {
95+ try {
96+ return response . json ( ) ;
97+ } catch ( error ) {
98+ throw new Error (
99+ `The response is a invalid Json object : ${ error } `
100+ ) ;
101+ }
102+ }
103+ throw new Error ( `${ response . status } : ${ response . statusText } ` ) ;
104+ } catch ( error ) {
105+ const win = document . getElementById ( "djDebugWindow" ) ;
106+ win . innerHTML = `<div class="djDebugPanelTitle"><h3>${ error . message } </h3><button type="button" class="djDebugClose">»</button></div>` ;
107+ $$ . show ( win ) ;
108+ throw error ;
109+ }
113110}
114111
115- function ajaxForm ( element ) {
112+ export function ajaxForm ( element ) {
116113 const form = element . closest ( "form" ) ;
117114 const url = new URL ( form . action ) ;
118115 const formData = new FormData ( form ) ;
@@ -125,7 +122,7 @@ function ajaxForm(element) {
125122 return ajax ( url , ajaxData ) ;
126123}
127124
128- function replaceToolbarState ( newRequestId , data ) {
125+ export function replaceToolbarState ( newRequestId , data ) {
129126 const djDebug = getDebugElement ( ) ;
130127 djDebug . setAttribute ( "data-request-id" , newRequestId ) ;
131128 // Check if response is empty, it could be due to an expired requestId.
@@ -155,5 +152,3 @@ export function debounce(func, timeout) {
155152 timer = setTimeout ( ( ) => func ( ...args ) , timeout ) ;
156153 } ;
157154}
158-
159- export { $$ , ajax , ajaxForm , getDebugElement , replaceToolbarState } ;
0 commit comments