PluginProbe
codoc / 0.4
codoc v0.4
0.9.8.8 0.9.8.9 0.9.9 0.9.9.1 trunk 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.8.1 0.8.2 0.8.3 0.8.4 0.8.6 0.8.7 0.8.8 0.8.9 0.9 0.9.1 0.9.10 0.9.11 All 114 releases
codoc / src / block / block.js

block.js in codoc 0.4, at src/block/block.js

395 lines 11.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * BLOCK: codoc-block
3 *
4 * Registering a basic block with Gutenberg.
5 * Simple block, renders and saves the same content without any interactivity.
6 */
7
8 // Import CSS.
9 import './style.scss';
10 import './editor.scss';
11 import icon from './icon';
12 //import { ENTER } from '@wordpress/keycodes';
13 const { ENTER } = wp.keycodes;
14
15 const { __ } = wp.i18n; // Import __() from wp.i18n
16 const { Component } = wp.element;
17
18 const { registerBlockType } = wp.blocks; // Import registerBlockType() from wp.blocks
19 const {
20 InspectorControls,
21 RichText
22 } = wp.editor;
23
24 const {
25 SelectControl,
26 PanelBody,
27 ToggleControl,
28 CheckboxControl,
29 RangeControl,
30 } = wp.components;
31
32 const {
33 getDefaultBlockName,
34 createBlock,
35 } = wp.blocks;
36
37 const { withState } = wp.compose;
38
39 const CODOC_URL = OPTIONS.codoc_url;
40 const CODOC_USER_CODE = OPTIONS.codoc_usercode;
41 const CODOC_PLUGIN_VERSION = OPTIONS.codoc_plugin_version;
42 const CODOC_ENTRIES_CLASS = 'codoc-entries';
43
44 import Cookies from 'universal-cookie';
45 const cookies = new Cookies();
46
47 class CodocControls extends Component {
48 constructor( props ) {
49 super( ...arguments );
50
51 this.subscriptionsFetched = [];
52 this.fetchSubscriptions();
53 }
54
55 fetchSubscriptions() {
56 const {
57 setAttributes,
58 attributes: {
59 fetching,
60 }
61 } = this.props;
62
63 if (fetching) {
64 return
65 }
66
67 setAttributes( { fetching: true } )
68 let url = CODOC_URL + '/api/v1/cms/' + CODOC_USER_CODE + '/subscriptions?without_token=1';
69
70 fetch(url)
71 .then( res => res.json() )
72 .then( res => {
73 let list = [];
74 if (res.status && res.subscriptions) {
75 for ( var i = 0; i < res.subscriptions.length; i++ ) {
76 let info = {
77 value: res.subscriptions[i].code,
78 label: res.subscriptions[i].title,
79 }
80 list[i] = info
81 }
82 }
83 if (list.length) {
84 this.subscriptionsFetched = list;
85 }
86 setAttributes( { fetching: false } )
87 })
88 }
89
90 getShowPriceHelp( checked ) {
91 return checked ?
92 __( 'する' ) :
93 __( 'しない' );
94 }
95 getShowSupportHelp( checked ) {
96 return checked ?
97 __( '受け付ける' ) :
98 __( '受け付けない' );
99 }
100
101 render() {
102 const {
103 setAttributes,
104 attributes: {
105 showPrice,
106 price,
107 limited,
108 limitedCount,
109
110 affiliateMode,
111 affiliateRate,
112
113 showSupport,
114
115 subscriptions,
116 }
117 } = this.props;
118
119 const toggleShowPrice = () => setAttributes( { showPrice: ! showPrice } );
120 const toggleLimited = () => setAttributes( { limited: ! limited } );
121 const toggleAffiliateMode = () => setAttributes( { affiliateMode: ! affiliateMode } );
122 const toggleShowSupport = () => setAttributes( { showSupport: ! showSupport } );
123
124 const SubscriptionCheckBoxes = withState({
125 checked_obj: Object.assign(new Object, subscriptions)
126 })( ( { checked_obj , setState } ) => (
127 <ul>
128 {
129 this.subscriptionsFetched.map((v) => (
130 <li><CheckboxControl
131 className="check_items"
132 label={v.label}
133 checked={checked_obj[v.value]}
134
135 onChange={ ( check ) => {
136 check ? checked_obj[v.value] = true : delete checked_obj[v.value]
137 setAttributes({subscriptions : checked_obj})
138 setState({checked_obj})
139 } }
140 /></li>
141 ) )
142 }
143 </ul>
144 ) )
145 const affiliateRateOptions = [
146 { value: '0.0500', label:'販売価格の5%' },
147 { value: '0.1000', label:'販売価格の10%' },
148 { value: '0.1500', label:'販売価格の15%' },
149 { value: '0.2000', label:'販売価格の20%' },
150 { value: '0.2500', label:'販売価格の25%' },
151 { value: '0.3000', label:'販売価格の30%' },
152 { value: '0.3500', label:'販売価格の35%' },
153 { value: '0.4000', label:'販売価格の40%' },
154 { value: '0.4500', label:'販売価格の45%' },
155 { value: '0.5000', label:'販売価格の50%' },
156 ];
157
158 return(
159 <InspectorControls key="CodocControls">
160
161 <PanelBody>
162
163 <ToggleControl
164 label={ __( '単体販売' ) }
165 checked={ !! showPrice }
166 help={ this.getShowPriceHelp }
167 ref="showPrice"
168 onChange={ toggleShowPrice }
169 />
170
171 <div style={{ display: showPrice ? 'initial' : 'none' }}>
172
173 <RangeControl
174 label={ __( '価格(円)' ) }
175 value={ price }
176 initialPosition={ price }
177 onChange={ ( value ) => {
178 // do validate this
179 setAttributes({ price: value });
180 }}
181 min="100"
182 max="50000"
183 /><br />
184
185 <ToggleControl
186 label={ __( '数量限定販売' ) }
187 checked={ !! limited }
188 onChange={ toggleLimited }
189 />
190
191 <div style={{ display: limited ? 'initial' : 'none' }}>
192 <RangeControl
193 label={ __( '限定数' ) }
194 value={ limitedCount }
195 onChange={ ( value ) => {
196 // do validate this
197 setAttributes({ limitedCount: value });
198 }}
199 initialPosition={ limitedCount }
200 min="1"
201 max="100"
202 /><br />
203 </div>
204
205 <ToggleControl
206 label={ __( 'アフィリエイト' ) }
207 checked={ !! affiliateMode }
208 onChange={ toggleAffiliateMode }
209 />
210
211 <div style={{ display: affiliateMode ? 'initial' : 'none' }}>
212 <SelectControl
213 label={ __( '料率' ) }
214 description={ __( '購�
215 ��
216 を対象に指定した料率でアフィリエイトをオファーできます') }
217 options={ affiliateRateOptions }
218 value={ affiliateRate }
219 onChange={ ( value ) => {
220 setAttributes( { affiliateRate: value } );
221 }}/><br />
222 </div>
223
224 </div>
225
226 <ToggleControl
227 label={ __( 'サポート' ) }
228 checked={ !! showSupport }
229 value="1"
230 onChange={ toggleShowSupport }
231 help={ this.getShowSupportHelp }
232 />
233
234 <label>サブスクリプション</label>
235 <SubscriptionCheckBoxes />
236
237 </PanelBody>
238 </InspectorControls>
239 );
240 }
241 };
242
243 class EditBlockContent extends Component {
244 constructor() {
245 super( ...arguments );
246 // デフォルト値
247 this.props.setAttributes({ version: CODOC_PLUGIN_VERSION });
248 this.props.setAttributes({ showPrice: this.props.attributes.showPrice == null ? true : this.props.attributes.showPrice });
249 this.props.setAttributes({ price: this.props.attributes.price == null ? 100 : this.props.attributes.price });
250 this.props.setAttributes({ limited: this.props.attributes.limited == null ? false : this.props.attributes.limited });
251 this.props.setAttributes({ limitedCount: this.props.attributes.limitedCount == null ? 10 : this.props.attributes.limitedCount });
252 this.props.setAttributes({ affiliateMode: this.props.attributes.affiliateMode == null ? false : this.props.attributes.affiliateMode });
253 this.props.setAttributes({ affiliateRate: this.props.attributes.affiliateRate == null ? '0.0500' : this.props.attributes.affiliateRate });
254 this.props.setAttributes({ showSupport: this.props.attributes.showSupport == null ? false : this.props.attributes.showSupport });
255 this.props.setAttributes({ subscriptions: this.props.attributes.subscriptions == null ? {} : this.props.attributes.subscriptions });
256
257 this.onChangeInput = this.onChangeInput.bind( this );
258 this.onKeyDown = this.onKeyDown.bind( this );
259 this.state = {
260 defaultText: __( 'この続きをみるには' ),
261 };
262 }
263 onChangeInput( event ) {
264 this.setState( {
265 defaultText: '',
266 } );
267
268 const value = event.target.value.length === 0 ? undefined : event.target.value;
269 this.props.setAttributes( { customText: value } );
270 }
271 onKeyDown( event ) {
272 const { keyCode } = event;
273 const { insertBlocksAfter } = this.props;
274 if ( keyCode === ENTER ) {
275 insertBlocksAfter( [ createBlock( getDefaultBlockName() ) ] );
276 }
277 }
278
279 render() {
280 const {
281 attributes: {
282 customText,
283 },
284 setAttributes
285 } = this.props;
286
287 const { defaultText } = this.state;
288 //const value = customText !== undefined ? customText : defaultText;
289 const value = customText ? customText : defaultText;
290 //const value = defaultText;
291 const inputLength = value.length + 10;
292
293 return[
294 <CodocControls { ...{setAttributes, ...this.props } } />,
295
296 <div className="wp-block-more">
297 <input
298 type="text"
299 value={ value }
300 size={ inputLength }
301 onChange={ this.onChangeInput }
302 onKeyDown={ this.onKeyDown }
303 />
304 </div>
305
306 ];
307 }
308 };
309
310
311 registerBlockType( 'codoc/codoc-block', {
312 title: __( 'codoc' ),
313 description: __( 'このブロックから下のブロックは認証されたcodocユーザーのみが閲覧できる有料エリアとなります。' ),
314 icon,
315 category: 'layout',
316 supports: {
317 customClassName: false,
318 className: false,
319 html: false,
320 multiple: false,
321 },
322 keywords: [
323 __( 'codoc Block' ),
324 __( 'plugin for codoc' ),
325 __( 'codoc-block' ),
326 ],
327 attributes: {
328 showPrice: {
329 type: 'boolean',
330 default: null,
331 },
332 price: {
333 type: 'number',
334 default: null,
335 },
336 limited: {
337 type: 'boolean',
338 default: null,
339 },
340 limitedCount: {
341 type: 'number',
342 default: null,
343 },
344 affiliateMode: {
345 type: 'boolean',
346 default: null,
347 },
348 affiliateRate: {
349 type: 'string',
350 default: null,
351 },
352 showSupport: {
353 type: 'boolean',
354 default: null,
355 },
356 subscriptions: {
357 type: 'object',
358 default: null,
359 },
360 customText: {
361 type: 'string',
362 default: '',
363 },
364 version: {
365 type: 'string',
366 default: null,
367 },
368 },
369
370 edit: EditBlockContent,
371
372 save: function( props ) {
373 const {
374 setAttributes,
375 attributes: {
376 customText,
377 }
378 } = props;
379 return ( // return if link behavior normal
380 <div>
381 <span
382 data-id='codoc-tag'
383 className={ CODOC_ENTRIES_CLASS }
384 >
385 { customText && !! customText.length && (
386 <RichText.Content
387 value={ customText }
388 />
389 )}
390 </span>
391 </div>
392 )
393 },
394 } );
395