if (window.$ushb === undefined) window.$ushb = {};
$ushb.isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
!function($){
if (window.$ushb.mixins === undefined) window.$ushb.mixins = {};
// TODO: replace AJAX URL;
$ushb.ajaxUrl = $('.us-bld').data('ajaxurl');
/**
* $ushb.Tabs class
*
* Boundable events: beforeShow, afterShow, beforeHide, afterHide
*
* @param container
* @constructor
*/
$ushb.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($ushb.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;
}
});
/**
* $ushb.EForm class
* @param container
* @constructor
*/
$ushb.EForm = function(container){
this.$container = $(container);
this.$tabs = this.$container.find('.usof-tabs');
if (this.$tabs.length) {
this.tabs = new $ushb.Tabs(this.$tabs);
}
this.initFields(this.$container);
};
$.extend($ushb.EForm.prototype, $usof.mixins.Fieldset);
/**
* $ushb.Elist class: A popup with elements list to choose from. Behaves as a singleton.
* Boundable events: beforeShow, afterShow, beforeHide, afterHide, select
* @constructor
*/
$ushb.EList = function(){
if ($ushb.elist !== undefined) return $ushb.elist;
this.$container = $('.us-bld-window.for_adding');
if (this.$container.length > 0) {
this.$container.appendTo($(document.body));
this.init();
}
};
$.extend($ushb.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: $ushb.ajaxUrl,
data: {
action: 'ushb_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
$ushb.elist = new $ushb.EList;
/**
* $ushb.EBuilder class: A popup with loadable elements forms
* Boundable events: beforeShow, afterShow, beforeHide, afterHide, save
* @constructor
*/
$ushb.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($ushb.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, .us-bld-window-btn.for_close');
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('.us-bld-window-btn.for_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: $ushb.ajaxUrl,
data: {
action: 'ushb_get_ebuilder_html'
},
success: function(html){
if (html == '') return;
// Removing additionally appended assets
var regexp = /(\]+?\>)|(\