(function($){
Drupal.behaviors.contextReactionBlock = {attach: function(context) {
$('form.context-editor:not(.context-block-processed)')
.addClass('context-block-processed')
.each(function() {
var id = $(this).attr('id');
Drupal.contextBlockEditor = Drupal.contextBlockEditor || {};
$(this).bind('init.pageEditor', function(event) {
Drupal.contextBlockEditor[id] = new DrupalContextBlockEditor($(this));
});
$(this).bind('start.pageEditor', function(event, context) {
// Fallback to first context if param is empty.
if (!context) {
context = $(this).data('defaultContext');
}
Drupal.contextBlockEditor[id].editStart($(this), context);
});
$(this).bind('end.pageEditor', function(event) {
Drupal.contextBlockEditor[id].editFinish();
});
});
//
// Admin Form =======================================================
//
// ContextBlockForm: Init.
$('#context-blockform:not(.processed)').each(function() {
$(this).addClass('processed');
Drupal.contextBlockForm = new DrupalContextBlockForm($(this));
Drupal.contextBlockForm.setState();
});
// ContextBlockForm: Attach block removal handlers.
// Lives in behaviors as it may be required for attachment to new DOM elements.
$('#context-blockform a.remove:not(.processed)').each(function() {
$(this).addClass('processed');
$(this).click(function() {
$(this).parents('tr').eq(0).remove();
Drupal.contextBlockForm.setState();
return false;
});
});
}};
/**
* Context block form. Default form for editing context block reactions.
*/
DrupalContextBlockForm = function(blockForm) {
this.state = {};
this.setState = function() {
$('table.context-blockform-region', blockForm).each(function() {
var region = $(this).attr('id').split('context-blockform-region-')[1];
var blocks = [];
$('tr', $(this)).each(function() {
var bid = $(this).attr('id');
var weight = $(this).find('select').val();
blocks.push({'bid' : bid, 'weight' : weight});
});
Drupal.contextBlockForm.state[region] = blocks;
});
// Serialize here and set form element value.
$('form input.context-blockform-state').val(JSON.stringify(this.state));
// Hide enabled blocks from selector that are used
$('table.context-blockform-region tr').each(function() {
var bid = $(this).attr('id');
$('div.context-blockform-selector input[value='+bid+']').parents('div.form-item').eq(0).hide();
});
// Show blocks in selector that are unused
$('div.context-blockform-selector input').each(function() {
var bid = $(this).val();
if ($('table.context-blockform-region tr#'+bid).size() === 0) {
$(this).parents('div.form-item').eq(0).show();
}
});
};
// make sure we update the state right before submits, this takes care of an
// apparent race condition between saving the state and the weights getting set
// by tabledrag
$('#ctools-export-ui-edit-item-form').submit(function() { Drupal.contextBlockForm.setState(); });
// Tabledrag
// Add additional handlers to update our blocks.
$.each(Drupal.settings.tableDrag, function(base) {
var table = $('#' + base + ':not(.processed)', blockForm);
if (table && table.is('.context-blockform-region')) {
table.addClass('processed');
table.bind('mouseup', function(event) {
Drupal.contextBlockForm.setState();
return;
});
}
});
// Add blocks to a region
$('td.blocks a', blockForm).each(function() {
$(this).click(function() {
var region = $(this).attr('href').split('#')[1];
var selected = $("div.context-blockform-selector input:checked");
if (selected.size() > 0) {
selected.each(function() {
// create new block markup
var block = document.createElement('tr');
var text = $(this).parents('div.form-item').eq(0).hide().children('label').text();
var select = '