Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

jsarnowski / jsarnowski/jet-booking   php

Repository URL to install this package:

Version: 2.2.1 

/ js / admin / meta-extras.js

(function( $, extrasSettings ) {

	'use strict';

	window.JetABAFMeta = new Vue( {
		el: '#jet_abaf_meta_exras',
		template: '#jet-abaf-meta-extras',
		data: {
			saving: false,
			isActive: false,
			defaultPrice: 0,
			rates: extrasSettings.pricing_rates,
			nonce: extrasSettings.nonce,
		},
		created: function() {
			var self       = this,
				priceInput = document.getElementById( '_apartment_price' ),
				button     = document.createElement( 'button' );

			button.type = 'button';
			button.classList.add( 'button' );
			button.classList.add( 'button-secondary' );
			button.style.margin = '10px 0 -20px';
			button.innerHTML = extrasSettings.button_label

			priceInput.after( button );

			button.addEventListener( 'click', function() {
				
				self.isActive = ! self.isActive;
				self.defaultPrice = priceInput.value;

				if ( ! self.defaultPrice ) {
					self.defaultPrice = 0;
				}

			} );
		},
		methods: {
			newRate: function() {
				this.rates.push( {
					duration: 2,
					value: this.defaultPrice,
				} )
			},
			deleteRate: function( index ) {
				if ( window.confirm( extrasSettings.confirm_message ) ) {
					this.rates.splice( index, 1 );
				}
			},
			saveRates: function() {

				var self = this;

				self.saving = true;
				
				jQuery.ajax({
					url: window.ajaxurl,
					type: 'POST',
					dataType: 'json',
					data: {
						action: 'jet_booking_save_price_rates',
						post_id: extrasSettings.apartment,
						nonce: self.nonce,
						rates: self.rates,
					},
				}).done( function( response ) {

					self.saving = false;

					if ( response.success ) {
						self.isActive = false;
					} else {
						alert( response.data.message );
					}
				} ).fail( function( jqXHR, textStatus, errorThrown ) {
					self.saving = false;
					alert( errorThrown );
				} );

			},
		}
	} );

})( jQuery, window.JetABAFMetaExtras );