Opal-Estate-Pro/assets/js/frontend/submission.js

243 lines
8.9 KiB
JavaScript
Raw Permalink Normal View History

2019-09-10 06:27:33 +02:00
jQuery( document ).ready( function ( $ ) {
'use strict';
2019-10-15 05:54:23 +02:00
var toggleSubmit = function ( _this ) {
if ( $( _this ).attr( 'disabled' ) == 'disabled' ) {
$( _this ).removeAttr( 'disabled' );
$( _this ).find( 'i' ).remove();
2019-09-10 06:27:33 +02:00
} else {
2019-10-15 05:54:23 +02:00
$( _this ).attr( 'disabled', 'disabled' );
$( _this ).append( '<i class="fa fa-spinner fa-spin"></i>' );
2019-09-10 06:27:33 +02:00
}
2019-10-15 05:54:23 +02:00
};
2019-09-10 06:27:33 +02:00
$( '.opalestate-submission-tab' ).each( function () {
var $submission_tab = $( this );
var $submit_btn = $submission_tab.find( '[name=submit-cmb]' );
var $next_btn = $( '.submission-next-btn' );
var $back_btn = $( '.submission-back-btn' );
var $tab_content = $submission_tab.find( '.opalestate-tab-content' );
$submission_tab.find( '.tab-item' ).first().addClass( 'active' );
$tab_content.first().addClass( 'active' );
if ( $tab_content.length != 1 ) {
$submit_btn.hide();
} else {
$next_btn.hide();
}
$submit_btn.on( 'click', function ( e ) {
e.preventDefault();
var empty_required_inputs = opalestate_get_empty_required_inputs( $submission_tab );
if ( empty_required_inputs.length === 0 ) {
$submit_btn.parents( 'form' ).submit();
}
} );
2019-10-15 05:54:23 +02:00
/*
$next_btn.click( function(){
// $submit_btn.click();
return false;
});
*/
2019-09-10 06:27:33 +02:00
var submitFormFiles = function ( name, files ) {
2019-12-06 08:10:12 +01:00
if( typeof(tinyMCE) != "undefined" ) {
tinyMCE.triggerSave();
}
2019-09-10 06:27:33 +02:00
var formData = new FormData();
2019-10-15 05:54:23 +02:00
formData.append( 'section', 'general' );
$( '.cmb2-uploader-files' ).each( function () {
var file_btn = $( 'input.select-file', this );
2019-09-10 06:27:33 +02:00
2019-10-15 05:54:23 +02:00
var files = $( '.uploader-item-preview', this );
var name = $( this ).data( 'name' );
var issingle = $( this ).data( 'single' );
$( files ).each( function ( i, element ) {
var file = $( this ).prop( 'file' );
if ( file ) {
if ( issingle ) {
formData.append( name, file );
2019-09-10 06:27:33 +02:00
} else {
2019-10-15 05:54:23 +02:00
formData.append( name + '[' + i + ']', file );
2019-09-10 06:27:33 +02:00
}
}
} );
2019-10-15 05:54:23 +02:00
} );
2019-09-10 06:27:33 +02:00
2019-10-15 05:54:23 +02:00
var dataSubmit = $submit_btn.parents( 'form' ).serializeArray();
2019-09-10 06:27:33 +02:00
$.each( dataSubmit, function ( key, input ) {
2019-10-15 05:54:23 +02:00
formData.append( input.name, input.value );
} );
2019-09-10 06:27:33 +02:00
2019-10-15 05:54:23 +02:00
formData.append( 'action', 'opalestate_save_agency_data' );
2019-12-06 08:10:12 +01:00
2019-09-10 06:27:33 +02:00
toggleSubmit( $submit_btn );
2019-10-15 05:54:23 +02:00
$.ajax( {
url: opalesateJS.ajaxurl,
data: formData,
type: 'POST',
2019-09-10 06:27:33 +02:00
processData: false,
contentType: false,
2019-10-15 05:54:23 +02:00
dataType: 'json',
success: function ( response ) {
if ( response.status == true ) {
if ( response.redirect ) {
2019-09-10 06:27:33 +02:00
window.location.href = response.redirect;
}
2019-10-15 05:54:23 +02:00
var myToast = $.toast( {
2019-09-10 06:27:33 +02:00
heading: response.heading,
text: response.message,
icon: 'success',
2019-10-15 05:54:23 +02:00
position: 'bottom-right',
hideAfter: 5000,
2019-09-10 06:27:33 +02:00
showHideTransition: 'fade',
2019-10-15 05:54:23 +02:00
} );
2019-09-10 06:27:33 +02:00
} else {
toggleSubmit( $submit_btn );
2019-10-15 05:54:23 +02:00
var myToast = $.toast( {
2019-09-10 06:27:33 +02:00
heading: response.heading,
text: response.message,
icon: 'error',
2019-10-15 05:54:23 +02:00
position: 'bottom-right',
hideAfter: 5000,
showHideTransition: 'fade'
} );
2019-09-10 06:27:33 +02:00
}
}
2019-10-15 05:54:23 +02:00
} );
};
2019-09-10 06:27:33 +02:00
2019-12-06 08:10:12 +01:00
$submit_btn.parents( 'form' ).on( 'submit', function (e) {
2019-10-15 05:54:23 +02:00
submitFormFiles();
2019-12-06 08:10:12 +01:00
return false;
2019-10-15 05:54:23 +02:00
} );
2019-09-10 06:27:33 +02:00
// Clicking Next button
$next_btn.on( 'click', function ( e ) {
e.preventDefault();
var $tab_content = $( this ).parents( '.opalestate-tab-content' );
var empty_required_inputs = opalestate_get_empty_required_inputs( $tab_content );
if ( empty_required_inputs.length === 0 ) {
var $next_tab_content = $tab_content.next();
if ( $next_tab_content.length != 0 ) {
$submission_tab.find( '.opalestate-tab-content' ).removeClass( 'active' );
$submission_tab.find( '.tab-item.active' )
.removeClass( 'active' )
.addClass( 'validated' )
.addClass( 'passed' )
.next()
.addClass( 'active' );
$tab_content.addClass( 'validated' ).addClass( 'passed' );
$next_tab_content.addClass( 'active' );
$( 'html, body' ).animate( {
scrollTop: $next_tab_content.offset().top - 100
}, 500 );
// Show Save button if is last tab.
if ( $next_tab_content.is( ':last-child' ) ) {
$next_btn.hide();
$submit_btn.show();
}
}
}
} );
// Clicking Back button
$back_btn.on( 'click', function ( e ) {
e.preventDefault();
var $tab_content = $( this ).parents( '.opalestate-tab-content' );
$submission_tab.find( '.opalestate-tab-content' ).removeClass( 'active' );
$submission_tab.find( '.tab-item.active' )
.removeClass( 'active' )
.removeClass( 'passed' )
.prev()
.addClass( 'active' );
$tab_content.removeClass( 'active' );
var $prev_tab_content = $tab_content.prev();
if ( $prev_tab_content.length != 0 ) {
$prev_tab_content.addClass( 'active' ).removeClass( 'passed' );
$( 'html, body' ).animate( {
scrollTop: $prev_tab_content.offset().top - 100
}, 500 );
}
$submit_btn.hide();
$next_btn.show();
} );
$( '.tab-item' ).on( 'click', function ( e ) {
e.preventDefault();
var $el = $( this );
var $prev_tab_item = $el.prev();
2019-10-15 05:54:23 +02:00
if ( $el.hasClass( 'validated' ) ||
( $prev_tab_item.length != 0 && $prev_tab_item.hasClass( 'validated' ) &&
$prev_tab_item.hasClass( 'passed' ) ) ) {
2019-09-10 06:27:33 +02:00
$submission_tab.find( '.opalestate-tab-content' ).removeClass( 'active' );
$submission_tab.find( '.tab-item.active' ).removeClass( 'active' );
var $tab_id = $el.attr( 'href' );
var $prev_tab_content = $( $tab_id ).prev();
var $next_tab_content = $( $tab_id ).next();
if ( $prev_tab_content.length != 0 ) {
$back_btn.show();
} else {
$back_btn.hide();
}
if ( $next_tab_content.length != 0 ) {
$next_btn.show();
$submit_btn.hide();
} else {
$next_btn.hide();
$submit_btn.show();
}
$el.addClass( 'active' );
$( $tab_id ).addClass( 'active' );
}
} );
} );
function opalestate_get_empty_required_inputs( el_wrapper ) {
var empty_required_inputs = [];
el_wrapper.find( 'input' ).each( function () {
$( this ).removeClass( 'required' );
$( this ).blur();
if ( $( this ).prop( 'required' ) ) {
if ( $( this ).val() == '' ) {
$( this ).addClass( 'required' );
$( this ).focus();
empty_required_inputs.push( $( this ) );
}
}
} );
return empty_required_inputs;
}
$( '.opalestate-tab-content input' ).each( function ( e ) {
if ( $( this ).prop( 'required' ) ) {
$( this ).on( 'input', function () {
if ( $( this ).val() == '' ) {
$( this ).addClass( 'required' );
$( this ).focus();
} else {
$( this ).removeClass( 'required' );
}
} );
}
} );
} );