if ( window.$usgb === undefined ) {
window.$usgb = {};
}
$usgb.isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test( navigator.userAgent );
jQuery( document ).ready( function( $ ) {
if ( window.$usof.mixins === undefined ) {
window.$usof.mixins = {};
}
$usof.ajaxUrl = $( '.us-bld' ).data( 'ajaxurl' );
/**
* $usgb.Tabs class
*
* Boundable events: beforeShow, afterShow, beforeHide, afterHide
*
* @param container
* @constructor
*/
$usgb.Tabs = function( container ) {
this.$container = $( container );
this.$list = this.$container.find( '.usof-tabs-list:first' );
this.$items = this.$list.children( '.usof-tabs-item' );
this.$sections = this.$container.find( '.usof-tabs-section' );
this.items = this.$items.toArray().map( $ );
this.sections = this.$sections.toArray().map( $ );
this.active = 0;
this.items.forEach( function( $elm, index ) {
$elm.on( 'click', this.open.bind( this, index ) );
}.bind( this ) );
};
$.extend( $usgb.Tabs.prototype, $usof.mixins.Events, {
open: function( index ) {
if ( index == this.active || this.sections[ index ] == undefined ) {
return;
}
if ( this.sections[ this.active ] !== undefined ) {
this.trigger( 'beforeHide', this.active, this.sections[ this.active ], this.items[ this.active ] );
this.sections[ this.active ].hide();
this.items[ this.active ].removeClass( 'active' );
this.trigger( 'afterHide', this.active, this.sections[ this.active ], this.items[ this.active ] );
}
this.trigger( 'beforeShow', index, this.sections[ index ], this.items[ index ] );
this.sections[ index ].show();
this.items[ index ].addClass( 'active' );
this.trigger( 'afterShow', index, this.sections[ index ], this.items[ index ] );
this.active = index;
}
} );
/**
* $usgb.EForm class
* @param container
* @constructor
*/
$usgb.EForm = function( container ) {
this.$container = $( container );
this.$tabs = this.$container.find( '.usof-tabs' );
if ( this.$tabs.length ) {
this.tabs = new $usgb.Tabs( this.$tabs );
}
this.initFields( this.$container );
// Delete all fields that are in design_options since they will be initialized by design_options,
// otherwise there will be duplication events on different parent objects
for ( var k in this.fields ) {
if (
this.fields[ k ].type === 'color'
&& this.fields[ k ].$row.closest( '.type_design_options' ).length
) {
delete this.fields[ k ];
}
}
};
$.extend( $usgb.EForm.prototype, $usof.mixins.Fieldset );
/**
* $usgb.Elist class: A popup with elements list to choose from. Behaves as a singleton.
* Boundable events: beforeShow, afterShow, beforeHide, afterHide, select
* @constructor
*/
$usgb.EList = function() {
if ( $usgb.elist !== undefined ) {
return $usgb.elist;
}
this.$container = $( '.us-bld-window.for_adding' );
if ( this.$container.length > 0 ) {
this.$container.appendTo( $( document.body ) );
this.init();
}
};
$.extend( $usgb.EList.prototype, $usof.mixins.Events, {
init: function() {
this.$closer = this.$container.find( '.us-bld-window-closer' );
this.$list = this.$container.find( '.us-bld-window-list' );
this._events = {
select: function( event ) {
var $item = $( event.target ).closest( '.us-bld-window-item' );
this.hide();
this.trigger( 'select', $item.data( 'name' ) );
}.bind( this ),
hide: this.hide.bind( this )
};
this.$closer.on( 'click', this._events.hide );
this.$list.on( 'click', '.us-bld-window-item', this._events.select );
},
show: function() {
if ( this.$container.length == 0 ) {
// Loading elements list html via ajax
$.ajax( {
type: 'post',
url: $usgb.ajaxUrl,
data: {
action: 'usgb_get_elist_html'
},
success: function( html ) {
this.$container = $( html ).css( 'display', 'none' ).appendTo( $( document.body ) );
this.init();
this.show();
}.bind( this )
} );
return;
}
this.trigger( 'beforeShow' );
this.$container.css( 'display', 'block' );
this.trigger( 'afterShow' );
},
hide: function() {
this.trigger( 'beforeHide' );
this.$container.css( 'display', 'none' );
this.trigger( 'afterHide' );
}
} );
// Singleton instance
$usgb.elist = new $usgb.EList;
/**
* $usgb.EBuilder class: A popup with loadable elements forms
* Boundable events: beforeShow, afterShow, beforeHide, afterHide, save
* @constructor
*/
$usgb.EBuilder = function() {
this.$container = $( '.us-bld-window.for_editing' );
this.loaded = false;
if ( this.$container.length != 0 ) {
this.$container.appendTo( $( document.body ) );
this.init();
}
};
$.extend( $usgb.EBuilder.prototype, $usof.mixins.Events, {
init: function() {
this.$title = this.$container.find( '.us-bld-window-title' );
this.titles = this.$title[ 0 ].onclick() || {};
this.$title.removeAttr( 'onclick' );
this.$closer = this.$container.find( '.us-bld-window-closer' );
this.$header = this.$container.find( '.us-bld-window-header' );
// EForm containers and class instances
this.$eforms = {};
this.eforms = {};
// Set of default values for each elements form
this.defaults = {};
this.$container.find( '.usof-form' ).each( function( index, eform ) {
var $eform = $( eform ).css( 'display', 'none' ),
name = $eform.usMod( 'for' );
this.$eforms[ name ] = $eform;
}.bind( this ) );
this.$btnSave = this.$container.find( '.usof-button.type_save' );
// Actve element
this.active = false;
this._events = {
hide: this.hide.bind( this ),
save: this.save.bind( this )
};
this.$closer.on( 'click', this._events.hide );
this.$btnSave.on( 'click', this._events.save );
},
/**
* Show element form for a specified element name and initial values
* @param {String} name
* @param {Object} values
*/
show: function( name, values ) {
if ( this.$container.css( 'display' ) == 'block' ) {
// If some other form is already shown, hiding it before proceeding
this.hide();
}
if ( ! this.loaded ) {
this.$title.html( this.titles[ name ] || '' );
this.$container.css( 'display', 'block' );
// Loading ebuilder and initial form's html
$.ajax( {
type: 'post',
url: $usof.ajaxUrl,
data: {
action: 'usgb_get_ebuilder_html'
},
success: function( html ) {
if ( html == '' ) {
return;
}
// Removing additionally appended assets
var regexp = /(\]+?\>)|(\