summaryrefslogtreecommitdiff
blob: c529db34621686f4f141b01389c5747441219421 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/* global jQuery, jpSimplePaymentsStrings, confirm, _ */
/* eslint no-var: 0, quote-props: 0 */

( function( api, wp, $ ) {
	var $document = $( document );

	$document.ready( function() {
		$document.on( 'widget-added', function( event, widgetContainer ) {
			if ( widgetContainer.is( '[id*="jetpack_simple_payments_widget"]' ) ) {
				initWidget( widgetContainer );
			}
		} );

		$document.on( 'widget-synced widget-updated', function( event, widgetContainer ) {
			//this fires for all widgets, this prevent errors for non SP widgets
			if ( ! widgetContainer.is( '[id*="jetpack_simple_payments_widget"]' ) ) {
				return;
			}

			event.preventDefault();

			syncProductLists();

			var widgetForm = widgetContainer.find( '> .widget-inside > .form, > .widget-inside > form' );

			enableFormActions( widgetForm );

			updateProductImage( widgetForm );
		} );
	} );

	function initWidget( widgetContainer ) {
		var widgetForm = widgetContainer.find( '> .widget-inside > .form, > .widget-inside > form' );

		//Add New Button
		widgetForm
			.find( '.jetpack-simple-payments-add-product' )
			.on( 'click', showAddNewForm( widgetForm ) );
		//Edit Button
		widgetForm
			.find( '.jetpack-simple-payments-edit-product' )
			.on( 'click', showEditForm( widgetForm ) );
		//Select an Image
		widgetForm
			.find(
				'.jetpack-simple-payments-image-fieldset .placeholder, .jetpack-simple-payments-image > img'
			)
			.on( 'click', selectImage( widgetForm ) );
		//Remove Image Button
		widgetForm
			.find( '.jetpack-simple-payments-remove-image' )
			.on( 'click', removeImage( widgetForm ) );
		//Save Product button
		widgetForm
			.find( '.jetpack-simple-payments-save-product' )
			.on( 'click', saveChanges( widgetForm ) );
		//Cancel Button
		widgetForm
			.find( '.jetpack-simple-payments-cancel-form' )
			.on( 'click', clearForm( widgetForm ) );
		//Delete Selected Product
		widgetForm
			.find( '.jetpack-simple-payments-delete-product' )
			.on( 'click', deleteProduct( widgetForm ) );
		//Input, Select and Checkbox change
		widgetForm.find( 'select, input, textarea, checkbox' ).on(
			'change input propertychange',
			_.debounce( function() {
				disableFormActions( widgetForm );
			}, 250 )
		);
	}

	function syncProductLists() {
		var request = wp.ajax.post( 'customize-jetpack-simple-payments-buttons-get', {
			'customize-jetpack-simple-payments-nonce':
				api.settings.nonce[ 'customize-jetpack-simple-payments' ],
			customize_changeset_uuid: api.settings.changeset.uuid,
		} );

		request.done( function( data ) {
			var selectedProduct = 0;

			$( document )
				.find( 'select.jetpack-simple-payments-products' )
				.each( function( index, select ) {
					var $select = $( select );
					selectedProduct = $select.val();

					$select.find( 'option' ).remove();
					$select.append(
						$.map( data, function( product ) {
							return $( '<option>', { value: product.ID, text: product.post_title } );
						} )
					);
					$select.val( selectedProduct );
				} );
		} );
	}

	function showForm( widgetForm ) {
		//reset validations
		widgetForm.find( '.invalid' ).removeClass( 'invalid' );
		//disable widget title and product selector
		widgetForm
			.find( '.jetpack-simple-payments-widget-title' )
			.add( '.jetpack-simple-payments-products' )
			//disable add and edit buttons
			.add( '.jetpack-simple-payments-add-product' )
			.add( '.jetpack-simple-payments-edit-product' )
			//disable save, delete and cancel until the widget update event is fired
			.add( '.jetpack-simple-payments-save-product' )
			.add( '.jetpack-simple-payments-cancel-form' )
			.add( '.jetpack-simple-payments-delete-product' )
			.attr( 'disabled', 'disabled' );
		//show form
		widgetForm.find( '.jetpack-simple-payments-form' ).show();
	}

	function hideForm( widgetForm ) {
		//enable widget title and product selector
		widgetForm
			.find( '.jetpack-simple-payments-widget-title' )
			.add( '.jetpack-simple-payments-products' )
			.removeAttr( 'disabled' );
		//hide the form
		widgetForm.find( '.jetpack-simple-payments-form' ).hide();
	}

	function changeFormAction( widgetForm, action ) {
		widgetForm
			.find( '.jetpack-simple-payments-form-action' )
			.val( action )
			.change();
	}

	function showAddNewForm( widgetForm ) {
		return function( event ) {
			event.preventDefault();

			showForm( widgetForm );
			changeFormAction( widgetForm, 'add' );
		};
	}

	function showEditForm( widgetForm ) {
		return function( event ) {
			event.preventDefault();

			showForm( widgetForm );
			changeFormAction( widgetForm, 'edit' );
		};
	}

	function clearForm( widgetForm ) {
		return function( event ) {
			event.preventDefault();

			hideForm( widgetForm );
			widgetForm
				.find( '.jetpack-simple-payments-add-product, .jetpack-simple-payments-edit-product' )
				.attr( 'disabled', 'disabled' );
			changeFormAction( widgetForm, 'clear' );
		};
	}

	function enableFormActions( widgetForm ) {
		var isFormVisible = widgetForm.find( '.jetpack-simple-payments-form' ).is( ':visible' );
		var isProductSelectVisible = widgetForm
			.find( '.jetpack-simple-payments-products' )
			.is( ':visible' ); //areProductsVisible ?
		var isEdit = widgetForm.find( '.jetpack-simple-payments-form-action' ).val() === 'edit';

		if ( isFormVisible ) {
			widgetForm
				.find( '.jetpack-simple-payments-save-product' )
				.add( '.jetpack-simple-payments-cancel-form' )
				.removeAttr( 'disabled' );
		} else {
			widgetForm.find( '.jetpack-simple-payments-add-product' ).removeAttr( 'disabled' );
		}

		if ( isFormVisible && isEdit ) {
			widgetForm.find( '.jetpack-simple-payments-delete-product' ).removeAttr( 'disabled' );
		}

		if ( isProductSelectVisible && ! isFormVisible ) {
			widgetForm.find( '.jetpack-simple-payments-edit-product' ).removeAttr( 'disabled' );
		}
	}

	function disableFormActions( widgetForm ) {
		widgetForm
			.find( '.jetpack-simple-payments-add-product' )
			.add( '.jetpack-simple-payments-edit-product' )
			.add( '.jetpack-simple-payments-save-product' )
			.add( '.jetpack-simple-payments-cancel-form' )
			.add( '.jetpack-simple-payments-delete-product' )
			.attr( 'disabled', 'disabled' );
	}

	function selectImage( widgetForm ) {
		return function( event ) {
			event.preventDefault();

			var imageContainer = widgetForm.find( '.jetpack-simple-payments-image' );

			var mediaFrame = new wp.media.view.MediaFrame.Select( {
				title: 'Choose Product Image',
				multiple: false,
				library: { type: 'image' },
				button: { text: 'Choose Image' },
			} );

			mediaFrame.on( 'select', function() {
				var selection = mediaFrame
					.state()
					.get( 'selection' )
					.first()
					.toJSON();
				//hide placeholder
				widgetForm.find( '.jetpack-simple-payments-image-fieldset .placeholder' ).hide();

				//load image from media library
				imageContainer
					.find( 'img' )
					.attr( 'src', selection.url )
					.show();

				//show image and remove button
				widgetForm.find( '.jetpack-simple-payments-image' ).show();

				//set hidden field for the selective refresh
				widgetForm
					.find( '.jetpack-simple-payments-form-image-id' )
					.val( selection.id )
					.change();
			} );

			mediaFrame.open();
		};
	}

	function removeImage( widgetForm ) {
		return function( event ) {
			event.preventDefault();

			//show placeholder
			widgetForm.find( '.jetpack-simple-payments-image-fieldset .placeholder' ).show();

			//hide image and remove button
			widgetForm.find( '.jetpack-simple-payments-image' ).hide();

			//set hidden field for the selective refresh
			widgetForm
				.find( '.jetpack-simple-payments-form-image-id' )
				.val( '' )
				.change();
		};
	}

	function updateProductImage( widgetForm ) {
		var newImageId = parseInt(
			widgetForm.find( '.jetpack-simple-payments-form-image-id' ).val(),
			10
		);
		var newImageSrc = widgetForm.find( '.jetpack-simple-payments-form-image-src' ).val();

		var placeholder = widgetForm.find( '.jetpack-simple-payments-image-fieldset .placeholder' );
		var image = widgetForm.find( '.jetpack-simple-payments-image > img' );
		var imageControls = widgetForm.find( '.jetpack-simple-payments-image' );

		if ( newImageId && newImageSrc ) {
			image.attr( 'src', newImageSrc );
			placeholder.hide();
			imageControls.show();
		} else {
			placeholder.show();
			image.removeAttr( 'src' );
			imageControls.hide();
		}
	}

	function decimalPlaces( number ) {
		var parts = number.split( '.' );
		if ( parts.length > 2 ) {
			return null;
		}

		return parts[ 1 ] ? parts[ 1 ].length : 0;
	}

	function isFormValid( widgetForm ) {
		widgetForm.find( '.invalid' ).removeClass( 'invalid' );

		var errors = false;

		var postTitle = widgetForm.find( '.jetpack-simple-payments-form-product-title' ).val();
		if ( ! postTitle ) {
			widgetForm.find( '.jetpack-simple-payments-form-product-title' ).addClass( 'invalid' );
			errors = true;
		}

		var productPrice = widgetForm.find( '.jetpack-simple-payments-form-product-price' ).val();
		if ( ! productPrice || isNaN( productPrice ) || parseFloat( productPrice ) <= 0 ) {
			widgetForm.find( '.jetpack-simple-payments-form-product-price' ).addClass( 'invalid' );
			errors = true;
		}

		// Japan's Yen is the only supported currency with a zero decimal precision.
		var precision =
			widgetForm.find( '.jetpack-simple-payments-form-product-currency' ).val() === 'JPY' ? 0 : 2;
		var priceDecimalPlaces = decimalPlaces( productPrice );
		if ( priceDecimalPlaces === null || priceDecimalPlaces > precision ) {
			widgetForm.find( '.jetpack-simple-payments-form-product-price' ).addClass( 'invalid' );
			errors = true;
		}

		var productEmail = widgetForm.find( '.jetpack-simple-payments-form-product-email' ).val();
		var isProductEmailValid = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(
			productEmail
		);
		if ( ! productEmail || ! isProductEmailValid ) {
			widgetForm.find( '.jetpack-simple-payments-form-product-email' ).addClass( 'invalid' );
			errors = true;
		}

		return ! errors;
	}

	function saveChanges( widgetForm ) {
		return function( event ) {
			event.preventDefault();
			var productPostId = widgetForm.find( '.jetpack-simple-payments-form-product-id' ).val();

			if ( ! isFormValid( widgetForm ) ) {
				return;
			}

			disableFormActions( widgetForm );

			widgetForm.find( '.spinner' ).show();

			var request = wp.ajax.post( 'customize-jetpack-simple-payments-button-save', {
				'customize-jetpack-simple-payments-nonce':
					api.settings.nonce[ 'customize-jetpack-simple-payments' ],
				customize_changeset_uuid: api.settings.changeset.uuid,
				params: {
					product_post_id: productPostId,
					post_title: widgetForm.find( '.jetpack-simple-payments-form-product-title' ).val(),
					post_content: widgetForm
						.find( '.jetpack-simple-payments-form-product-description' )
						.val(),
					image_id: widgetForm.find( '.jetpack-simple-payments-form-image-id' ).val(),
					currency: widgetForm.find( '.jetpack-simple-payments-form-product-currency' ).val(),
					price: widgetForm.find( '.jetpack-simple-payments-form-product-price' ).val(),
					multiple: widgetForm
						.find( '.jetpack-simple-payments-form-product-multiple' )
						.is( ':checked' )
						? 1
						: 0,
					email: widgetForm.find( '.jetpack-simple-payments-form-product-email' ).val(),
				},
			} );

			request.done( function( data ) {
				var select = widgetForm.find( 'select.jetpack-simple-payments-products' );
				var productOption = select.find( 'option[value="' + productPostId + '"]' );

				if ( productOption.length > 0 ) {
					productOption.text( data.product_post_title );
				} else {
					select.append(
						$( '<option>', {
							value: data.product_post_id,
							text: data.product_post_title,
						} )
					);
					select.val( data.product_post_id ).change();
				}

				widgetForm.find( '.jetpack-simple-payments-products-fieldset' ).show();
				widgetForm.find( '.jetpack-simple-payments-products-warning' ).hide();

				changeFormAction( widgetForm, 'clear' );
				hideForm( widgetForm );
			} );

			request.fail( function( data ) {
				var validCodes = {
					post_title: 'product-title',
					price: 'product-price',
					email: 'product-email',
				};

				data.forEach( function( item ) {
					if ( validCodes.hasOwnProperty( item.code ) ) {
						widgetForm
							.find( '.jetpack-simple-payments-form-' + validCodes[ item.code ] )
							.addClass( 'invalid' );
					}
				} );

				enableFormActions( widgetForm );
			} );
		};
	}

	function deleteProduct( widgetForm ) {
		return function( event ) {
			event.preventDefault();

			if ( ! confirm( jpSimplePaymentsStrings.deleteConfirmation ) ) {
				return;
			}

			var formProductId = parseInt(
				widgetForm.find( '.jetpack-simple-payments-form-product-id' ).val(),
				10
			);
			if ( ! formProductId ) {
				return;
			}

			disableFormActions( widgetForm );

			widgetForm.find( '.spinner' ).show();

			var request = wp.ajax.post( 'customize-jetpack-simple-payments-button-delete', {
				'customize-jetpack-simple-payments-nonce':
					api.settings.nonce[ 'customize-jetpack-simple-payments' ],
				customize_changeset_uuid: api.settings.changeset.uuid,
				params: {
					product_post_id: formProductId,
				},
			} );

			request.done( function() {
				var productList = widgetForm.find( 'select.jetpack-simple-payments-products' )[ 0 ];
				productList.remove( productList.selectedIndex );
				productList.dispatchEvent( new Event( 'change' ) );

				if ( $( productList ).has( 'option' ).length === 0 ) {
					//hide products select and label
					widgetForm.find( '.jetpack-simple-payments-products-fieldset' ).hide();
					//show empty products list warning
					widgetForm.find( '.jetpack-simple-payments-products-warning' ).show();
				}

				changeFormAction( widgetForm, 'clear' );
				hideForm( widgetForm );
			} );
		};
	}
} )( wp.customize, wp, jQuery );