1- import { Snippet } from './types' ;
1+ import { Snippet , window } from './types' ;
22
33type SuccessCallback = ( response : { success : boolean , data ?: unknown } ) => void ;
44
@@ -37,14 +37,14 @@ const send_snippet_request = (query: string, success_callback?: SuccessCallback)
3737const update_snippet = ( field : string , row_element : Element , snippet : Partial < Snippet > , success_callback ?: SuccessCallback ) => {
3838 const id_column = row_element . querySelector ( '.column-id' ) ;
3939
40- if ( ! id_column || ! parseInt ( id_column . textContent , 10 ) ) {
40+ if ( ! id_column ?. textContent || ! parseInt ( id_column . textContent , 10 ) ) {
4141 return ;
4242 }
4343
4444 snippet . id = parseInt ( id_column . textContent , 10 ) ;
4545 snippet . shared_network = Boolean ( row_element . className . match ( / \b s h a r e d - n e t w o r k - s n i p p e t \b / ) ) ;
4646 snippet . network = snippet . shared_network || network_admin ;
47- snippet . scope = row_element . getAttribute ( 'data-snippet-scope' ) ;
47+ snippet . scope = row_element . getAttribute ( 'data-snippet-scope' ) ?? snippet . scope ;
4848
4949 const query_string = `action=update_code_snippet&_ajax_nonce=${ nonce } &field=${ field } &snippet=${ JSON . stringify ( snippet ) } ` ;
5050 send_snippet_request ( query_string , success_callback ) ;
@@ -56,9 +56,14 @@ const update_snippet = (field: string, row_element: Element, snippet: Partial<Sn
5656 * Update the priority of a snippet
5757 */
5858const update_snippet_priority = ( element : HTMLInputElement ) => {
59- const row = element . parentElement . parentElement ;
59+ const row = element . parentElement ? .parentElement ;
6060 const snippet : Partial < Snippet > = { priority : parseFloat ( element . value ) } ;
61- update_snippet ( 'priority' , row , snippet ) ;
61+ if ( row ) {
62+ update_snippet ( 'priority' , row , snippet ) ;
63+ } else {
64+ console . error ( 'Could not update snippet information.' , snippet , row )
65+ }
66+
6267} ;
6368
6469for ( const field of document . getElementsByClassName ( 'snippet-priority' ) as HTMLCollectionOf < HTMLInputElement > ) {
@@ -74,9 +79,13 @@ for (const field of document.getElementsByClassName('snippet-priority') as HTMLC
7479 * @param increment
7580 */
7681const update_view_count = ( view_count : HTMLElement , increment : boolean ) => {
77- let count = parseInt ( view_count . textContent . replace ( / \( (?< count > \d + ) \) / , '$1' ) , 10 ) ;
78- count += increment ? 1 : - 1 ;
79- view_count . textContent = `(${ count . toString ( ) } )` ;
82+ if ( view_count ?. textContent ) {
83+ let count = parseInt ( view_count . textContent . replace ( / \( (?< count > \d + ) \) / , '$1' ) , 10 ) ;
84+ count += increment ? 1 : - 1 ;
85+ view_count . textContent = `(${ count . toString ( ) } )` ;
86+ } else {
87+ console . error ( 'Could not update view count.' , view_count ) ;
88+ }
8089} ;
8190
8291/**
@@ -85,9 +94,12 @@ const update_view_count = (view_count: HTMLElement, increment: boolean) => {
8594 * @param event
8695 */
8796const toggle_snippet_active = ( link : HTMLAnchorElement , event : Event ) => {
97+ const row = link ?. parentElement ?. parentElement ; // Switch < cell < row
98+ if ( ! row ) {
99+ console . error ( 'Could not toggle snippet active status.' , row )
100+ return ;
101+ }
88102
89- // Switch < cell < row
90- const row = link . parentElement . parentElement ;
91103 const match = row . className . match ( / \b (?: i n ) ? a c t i v e - s n i p p e t \b / ) ;
92104 if ( ! match ) return ;
93105
@@ -105,8 +117,11 @@ const toggle_snippet_active = (link: HTMLAnchorElement, event: Event) => {
105117 row . className . replace ( / \b a c t i v e - s n i p p e t \b / , 'inactive-snippet' ) ;
106118
107119 const views = document . querySelector ( '.subsubsub' ) ;
108- update_view_count ( views . querySelector ( '.active .count' ) , activating ) ;
109- update_view_count ( views . querySelector ( '.inactive .count' ) , activating ) ;
120+ const activeCount = views ?. querySelector < HTMLElement > ( '.active .count' ) ;
121+ const inactiveCount = views ?. querySelector < HTMLElement > ( '.inactive .count' ) ;
122+
123+ activeCount ? update_view_count ( activeCount , activating ) : null ;
124+ inactiveCount ? update_view_count ( inactiveCount , activating ) : null ;
110125
111126 button . title = activating ? strings . deactivate : strings . activate ;
112127 } else {
0 commit comments