');
var sidebars = Sidebars.right.find('.widgets-holder-wrap');
var col_1 = Sidebars.right.find('.sidebars-column-1');
var col_2 = Sidebars.right.find('.sidebars-column-2');
// Sorting method
function sort_toggle() {
var obj = jQuery(this),
col = obj.closest('.sidebars-column-1, .sidebars-column-2'),
dir = col.data('sort-dir');
dir = ('asc' === dir ? 'desc' : 'asc');
Sidebars.sort_sidebars(col, dir);
}
title__f.find('h2').append('').css({'cursor': 'pointer'});
title__f.clone().prependTo(col_1).click(sort_toggle).find('.acq-title-val').text(SidebarsData.custom_sidebars);
title__f.clone().prependTo(col_2).click(sort_toggle).find('.acq-title-val').text(SidebarsData.theme_sidebars);
col_1 = jQuery('').appendTo(col_1);
col_2 = jQuery('').appendTo(col_2);
sidebars.each(function check_sidebar() {
var obj = jQuery(this),
sbar = obj.find('.widgets-sortables');
if (Sidebars.chkSidebar(sbar)) {
obj.appendTo(col_1);
} else {
obj.appendTo(col_2);
}
});
},
/**
* default load all sidebar
*/
loadSidebars: function () {
Sidebars.right.find('.widgets-sortables').each(function () {
var key, sb,
state = false,
obj = jQuery(this),
id = obj.attr('id');
if (obj.data('acq-init') === true) {
return;
}
obj.data('acq-init', true);
if (Sidebars.chkSidebar(this)) {
sb = Sidebars.add(id, 'custom');
} else {
sb = Sidebars.add(id, 'theme');
// Set correct "replaceable" flag for the toolbar.
for (key in SidebarsData.replaceable) {
if (!SidebarsData.replaceable.hasOwnProperty(key)) {
continue;
}
if (SidebarsData.replaceable[key] === id) {
state = true;
break;
}
}
Sidebars.setReplaceable(sb, state, false);
}
});
return Sidebars;
},
/**
* Load default tools
*/
loadTTools: function () {
var sidebar_create_btn = jQuery('.btn-create-sidebar'),
topbar = jQuery('.acq-options'),
data = {};
// Button: Add new sidebar.
sidebar_create_btn.click(function () {
data.id = '';
data.title = SidebarsData.title_new;
data.button = SidebarsData.btn_new;
data.description = '';
data.name = '';
Sidebars.Editor_show(data);
});
},
/**
* Load toolbar
*/
loadToolbar: function () {
function tool_action(e) {
var i = jQuery(e.target).closest(".tool"),
action = i.data("action"),
a = Sidebars.EditbarID(i),
r = Sidebars.find(a);
return !Sidebars.handleAction(action, r);
}
return Sidebars.registerAction("edit", Sidebars.Editor_show), Sidebars.registerAction("location", Sidebars.LocationShow), Sidebars.registerAction("delete", Sidebars.RemoveShow), Sidebars.registerAction("replaceable", Sidebars.setReplaceable), Sidebars.right.on("click", ".tool", tool_action), Sidebars
},
/**
* Triggers the callback method
*/
handleAction: function (action, sb) {
if ('function' === typeof Sidebars.action_handlers[ action ]) {
return !!Sidebars.action_handlers[ action ](sb);
}
return false;
},
/**
* Register new action when toolbar click
*/
registerAction: function (task, cb_func) {
Sidebars.action_handlers[ task ] = cb_func;
},
/* Display ajax error
*/
showAjaxError: function (details) {
var msg = {};
msg.message = SidebarsData.ajax_error;
msg.details = details;
msg.parent = '#widgets-right';
msg.insert_after = '#acq-title-options';
msg.id = 'editor';
msg.type = 'err';
acquaintUi.message(msg);
},
/**
* Sort sidebars
*/
sort_sidebars: function (col, dir) {
var sidebars = col.find('.widgets-holder-wrap'),
icon = col.find('.acq-title .acq-icon');
sidebars.sortElements(function (a, b) {
var val_a = jQuery(a).find('.sidebar-name h2').text(),
val_b = jQuery(b).find('.sidebar-name h2').text();
if (dir === 'asc') {
return val_a > val_b ? 1 : -1;
} else {
return val_a < val_b ? 1 : -1;
}
});
// Change the indicator.
col.data('sort-dir', dir);
if ('asc' === dir) {
icon
.removeClass('dashicons-arrow-down dashicons-sort')
.addClass('dashicons-arrow-up');
} else {
icon
.removeClass('dashicons-arrow-up dashicons-sort')
.addClass('dashicons-arrow-down');
}
},
/**
* Display add/edit sectrion in popup
*/
Editor_show: function (data) {
var popup = null,
ajax = null;
if (data instanceof ACq_Sidebar) {
data = {
id: data.getID(),
title: SidebarsData.title_edit.replace('[Sidebar]', data.name),
button: SidebarsData.btn_edit
};
}
// Show the "extra" fields
function extraShow() {
popup.$().addClass('acqb-has-more');
popup.size(782, 545);
}
// Hide the "extra" fields
function extraHide() {
popup.$().removeClass('acqb-has-more');
popup.size(782, 215);
}
// Toggle the "extra" fields based on the checkbox state.
function extraToggle() {
if (jQuery(this).prop('checked')) {
extraShow();
} else {
extraHide();
}
}
// Populates the input fields in the editor with given data.
function values_set(data, okay, xhr) {
popup.loading(false);
// Ignore error responses from Ajax.
if (!data) {
return false;
}
if (!okay) {
popup.destroy();
Sidebars.showAjaxError(data);
return false;
}
if (data.sidebar) {
data = data.sidebar;
}
// Populate known fields.
if (data.id) {
popup.$().find('#acqb-id').val(data.id);
}
if (data.name) {
popup.$().find('#acqb-name').val(data.name);
}
if (data.description) {
popup.$().find('#acqb-description').val(data.description);
}
if (data.before_title) {
popup.$().find('#acqb-before-title').val(data.before_title);
}
if (data.after_title) {
popup.$().find('#acqb-after-title').val(data.after_title);
}
if (data.before_widget) {
popup.$().find('#acqb-before-widget').val(data.before_widget);
}
if (data.after_widget) {
popup.$().find('#acqb-after-widget').val(data.after_widget);
}
if (data.button) {
popup.$().find('.btn-save').text(data.button);
}
}
// Close popup after ajax request
function save_data_ajax(resp, okay, xhr) {
var msg = {}, sb;
popup.loading(false);
popup.destroy();
msg.message = resp.message;
// msg.details = resp;
msg.parent = '#widgets-right';
msg.insert_after = '#acq-title-options';
msg.id = 'editor';
if (okay) {
if ('update' === resp.action) {
// Update the name/description of the sidebar.
sb = Sidebars.find(resp.data.id);
Sidebars.SidebarUpdate(sb, resp.data);
} else if ('insert' === resp.action) {
// Insert a brand new sidebar container.
Sidebars.SidebarInsert(resp.data);
}
} else {
msg.type = 'err';
}
acquaintUi.message(msg);
}
// Submit the data via ajax.
function save_data() {
var form = popup.$().find('form');
// Start loading-animation.
popup.loading(true);
ajax.reset()
.data(form)
.ondone(save_data_ajax)
.load_json();
return false;
}
// Show the EDITOR popup.
popup = acquaintUi.popup()
.modal(true)
.title(data.title)
.onshow(extraHide)
.content(Sidebars.edit_form);
extraHide();
values_set(data, true, null);
// Create new ajax object to get sidebar details.
ajax = acquaintUi.ajax(null, 'acq-ajax');
if (data.id) {
popup.loading(true);
ajax.reset()
.data({
'do': 'get',
'sb': data.id
})
.ondone(values_set)
.load_json();
}
popup.show();
popup.$().find('#acqb-name').focus();
// Add event hooks to the editor.
popup.$().on('click', '#acqb-more', extraToggle);
popup.$().on('click', '.btn-save', save_data);
popup.$().on('click', '.btn-cancel', popup.destroy);
return true;
},
/**
* Update the name/description of an existing sidebar container.
*
* @since 1.0.0
*/
SidebarUpdate: function (sb, data) {
// Update the title.
sb.sb
.find('.sidebar-name h2')
.text(data.name);
// Update description.
sb.sb
.find('.sidebar-description')
.html('')
.find('.description')
.text(data.description);
return Sidebars;
},
/**
* Insert a brand new sidebar container.
*
* @since 1.0.0
*/
SidebarInsert: function (data) {
var box = jQuery(''),
inner = jQuery(''),
name = jQuery('
'),
desc = jQuery(''),
col = Sidebars.right.find('.sidebars-column-1 > .inner:first');
// Set sidebar specific values.
inner.attr('id', data.id);
name
.find('h2')
.text(data.name);
desc
.html('')
.find('.description')
.text(data.description);
// Assemble the new sidebar box in correct order.
name.appendTo(inner);
desc.appendTo(inner);
inner.appendTo(box);
// Display the new sidebar on screen.
box.prependTo(col);
// Remove hooks added by wpWidgets.init()
jQuery('#widgets-right .sidebar-name').unbind('click');
jQuery('#widgets-left .sidebar-name').unbind('click');
jQuery(document.body).unbind('click.widgets-toggle');
jQuery('.widgets-chooser')
.off('click.widgets-chooser')
.off('keyup.widgets-chooser');
jQuery('#available-widgets .widget .widget-title').off('click.widgets-chooser');
jQuery('.widgets-chooser-sidebars').empty();
// Re-Init the page using wpWidgets.init()
window.wpWidgets.init();
// Add the plugin toolbar to the new sidebar.
Sidebars.loadSidebars();
return Sidebars;
},
/**
* =====================================================================
* Ask for confirmation before deleting a sidebar
*/
RemoveShow: function (sb) {
var popup = null,
ajax = null,
id = sb.getID(),
name = sb.name;
// Insert the sidebar name into the delete message.
function insert_name(el) {
el.find('.name').text(name);
}
// Closes the delete confirmation.
function close_popup() {
popup.loading(false);
popup.destroy();
}
// Handle response of the delete ajax-call.
function handle_done(resp, okay, xhr) {
var msg = {};
popup.loading(false);
popup.destroy();
msg.message = resp.message;
// msg.details = resp;
msg.parent = '#widgets-right';
msg.insert_after = '#acq-title-options';
msg.id = 'editor';
if (okay) {
// Remove the Sidebar from the page.
Sidebars.right
.find('#' + id)
.closest('.widgets-holder-wrap')
.remove();
// Remove object from internal collection.
Sidebars.remove(id);
} else {
msg.type = 'err';
}
acquaintUi.message(msg);
}
// Deletes the sidebar and closes the confirmation popup.
function delete_sidebar() {
popup.loading(true);
ajax.reset()
.data({
'do': 'delete',
'sb': id
})
.ondone(handle_done)
.load_json();
}
// Show the REMOVE popup.
popup = acquaintUi.popup()
.modal(true)
.size(560, 160)
.title(SidebarsData.title_delete)
.content(Sidebars.delete_form)
.onshow(insert_name)
.show();
// Create new ajax object.
ajax = acquaintUi.ajax(null, 'acq-ajax');
popup.$().on('click', '.btn-cancel', close_popup);
popup.$().on('click', '.btn-delete', delete_sidebar);
return true;
},
/**
* =====================================================================
* Show popup to assign sidebar to default categories.
*
* @since 2.0
*/
LocationShow: function (sb) {
var popup = null,
ajax = null,
form = null,
id = sb.getID();
// Display the location data after it was loaded by ajax.
function handle_done_load(resp, okay, xhr) {
var theme_sb, opt, name, msg = {}; // Only used in error case.
popup.loading(false);
if (!okay) {
popup.destroy();
Sidebars.showAjaxError(resp);
return;
}
// Display the sidebar name.
popup.$().find('.sb-name').text(resp.sidebar.name);
var sb_id = resp.sidebar.id;
// Only show settings for replaceable sidebars
var sidebars = popup.$().find('.acq-replaceable');
sidebars.hide();
resp.replaceable = acquaintUi.obj(resp.replaceable);
for (var key0 in resp.replaceable) {
if (!resp.replaceable.hasOwnProperty(key0)) {
continue;
}
sidebars.filter('.' + resp.replaceable[key0]).show();
}
// Add a new option to the replacement list.
function _add_option(item, lists, key) {
var opt = jQuery('');
opt.attr('value', key).text(item.name);
lists.append(opt);
}
// Check if the current sidebar is a replacement in the list.
function _select_option(replacement, sidebar, key, lists) {
var row = lists
.closest('.acq-replaceable')
.filter('.' + sidebar),
option = row
.find('option[value="' + key + '"]'),
group = row.find('optgroup.used'),
check = row.find('.detail-toggle');
if (replacement === sb_id) {
option.prop('selected', true);
if (true !== check.prop('checked')) {
check.prop('checked', true);
row.addClass('open');
// Upgrade the select list with chosen.
acquaintUi.upgrade_multiselect(row);
}
} else {
if (!group.length) {
group = jQuery('