Skip to content

Commit 1b62332

Browse files
committed
All functions in Block Component
1 parent 1acc58d commit 1b62332

File tree

1 file changed

+46
-47
lines changed

1 file changed

+46
-47
lines changed

plugin/frontend/js/block.js

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ jQuery( document ).ready( function( $ ) {
5555
SgdgEditorComponent.prototype = Object.create( wp.element.Component.prototype );
5656
SgdgEditorComponent.prototype.render = function() {
5757
if ( 0 === $( '#sgdg-block-editor-list' ).children().length ) {
58-
ajaxQuery( this.props, this.props.attributes.path );
58+
this.ajax( this.props.attributes.path );
5959
}
6060
return el( 'table', { class: 'widefat' }, [
6161
el( 'thead', {},
@@ -71,58 +71,24 @@ jQuery( document ).ready( function( $ ) {
7171
)
7272
]);
7373
};
74-
75-
wp.blocks.registerBlockType( 'skaut-google-drive-gallery/gallery', {
76-
title: sgdgBlockLocalize.block_name,
77-
description: sgdgBlockLocalize.block_description,
78-
category: 'common',
79-
icon: iconSvg,
80-
attributes: {
81-
path: {
82-
type: 'array',
83-
default: []
84-
}
85-
},
86-
edit: SgdgEditorComponent,
87-
save: renderFrontend,
88-
transforms: {
89-
from: [
90-
{
91-
type: 'shortcode',
92-
tag: [ 'sgdg' ],
93-
priority: 15,
94-
attributes: {
95-
path: {
96-
type: 'string',
97-
shortcode: extractFromShortcode
98-
}
99-
}
100-
}
101-
]
102-
}
103-
});
104-
105-
function renderFrontend( props ) {
106-
return null;
107-
}
108-
109-
function ajaxQuery( props, path ) {
74+
SgdgEditorComponent.prototype.ajax = function( path ) {
75+
var that = this;
11076
$( '#sgdg-block-editor-list' ).html( '' );
11177
$.get( sgdgBlockLocalize.ajax_url, {
11278
_ajax_nonce: sgdgBlockLocalize.nonce, // eslint-disable-line camelcase
11379
action: 'list_gallery_dir',
11480
'path': path
11581
}, function( data ) {
11682
if ( data.response ) {
117-
success( data.response, props, path );
83+
that.ajaxSuccess( data.response, path );
11884
} else if ( data.error ) {
119-
error( data.error );
85+
that.ajaxError( data.error );
12086
}
12187
}
12288
);
123-
}
124-
125-
function success( data, props, path ) {
89+
};
90+
SgdgEditorComponent.prototype.ajaxSuccess = function( data, path ) {
91+
var that = this;
12692
var i;
12793
var len = data.length;
12894
var html = '';
@@ -151,14 +117,47 @@ jQuery( document ).ready( function( $ ) {
151117
} else {
152118
path = path.concat( newDir );
153119
}
154-
props.setAttributes({'path': path});
155-
ajaxQuery( props, path );
120+
that.props.setAttributes({'path': path});
121+
that.ajax( path );
156122
});
157-
}
158-
159-
function error( message ) {
123+
};
124+
SgdgEditorComponent.prototype.ajaxError = function( message ) {
160125
var html = '<div class="notice notice-error"><p>' + message + '</p></div>';
161126
$( '#sgdg-block-editor-list' ).parent().replaceWith( html );
127+
};
128+
129+
wp.blocks.registerBlockType( 'skaut-google-drive-gallery/gallery', {
130+
title: sgdgBlockLocalize.block_name,
131+
description: sgdgBlockLocalize.block_description,
132+
category: 'common',
133+
icon: iconSvg,
134+
attributes: {
135+
path: {
136+
type: 'array',
137+
default: []
138+
}
139+
},
140+
edit: SgdgEditorComponent,
141+
save: renderFrontend,
142+
transforms: {
143+
from: [
144+
{
145+
type: 'shortcode',
146+
tag: [ 'sgdg' ],
147+
priority: 15,
148+
attributes: {
149+
path: {
150+
type: 'string',
151+
shortcode: extractFromShortcode
152+
}
153+
}
154+
}
155+
]
156+
}
157+
});
158+
159+
function renderFrontend( props ) {
160+
return null;
162161
}
163162

164163
function extractFromShortcode( named ) {

0 commit comments

Comments
 (0)