Newer
Older
Dries Buytaert
committed
(function ($) {
/**
* Implementation of Drupal.behaviors for dashboard.
*/
Drupal.behaviors.dashboard = {
Angie Byron
committed
attach: function (context, settings) {
$('#dashboard', context).once(function () {
Angie Byron
committed
$(this).prepend('<div class="customize clearfix"><ul class="action-links"><li><a href="#">' + Drupal.t('Customize dashboard') + '</a></li></ul><div class="canvas"></div></div>');
Angie Byron
committed
$('.customize .action-links a', this).click(Drupal.behaviors.dashboard.enterCustomizeMode);
});
Dries Buytaert
committed
Drupal.behaviors.dashboard.addPlaceholders();
if (Drupal.settings.dashboard.launchCustomize) {
Drupal.behaviors.dashboard.enterCustomizeMode();
}
},
addPlaceholders: function() {
$('#dashboard .dashboard-region .region').each(function () {
var empty_text = "";
Dries Buytaert
committed
// If the region is empty
Dries Buytaert
committed
if ($('.block', this).length == 0) {
Dries Buytaert
committed
// Check if we are in customize mode and grab the correct empty text
Dries Buytaert
committed
if ($('#dashboard').hasClass('customize-mode')) {
Dries Buytaert
committed
empty_text = Drupal.settings.dashboard.emptyRegionTextActive;
} else {
empty_text = Drupal.settings.dashboard.emptyRegionTextInactive;
}
Dries Buytaert
committed
// We need a placeholder.
Dries Buytaert
committed
if ($('.placeholder', this).length == 0) {
$(this).append('<div class="placeholder"></div>');
}
$('.placeholder', this).html(empty_text);
}
else {
$('.placeholder', this).remove();
}
});
},
/**
* Enter "customize" mode by displaying disabled blocks.
*/
enterCustomizeMode: function () {
Dries Buytaert
committed
$('#dashboard').addClass('customize-mode customize-inactive');
Dries Buytaert
committed
Drupal.behaviors.dashboard.addPlaceholders();
Dries Buytaert
committed
// Hide the customize link
Dries Buytaert
committed
$('#dashboard .customize .action-links').hide();
Dries Buytaert
committed
// Load up the disabled blocks
Dries Buytaert
committed
$('div.customize .canvas').load(Drupal.settings.dashboard.drawer, Drupal.behaviors.dashboard.setupDrawer);
},
/**
* Exit "customize" mode by simply forcing a page refresh.
*/
exitCustomizeMode: function () {
Dries Buytaert
committed
$('#dashboard').removeClass('customize-mode customize-inactive');
Dries Buytaert
committed
Drupal.behaviors.dashboard.addPlaceholders();
location.href = Drupal.settings.dashboard.dashboard;
},
/**
* Helper for enterCustomizeMode; sets up drag-and-drop and close button.
*/
setupDrawer: function () {
$('div.customize .canvas-content input').click(Drupal.behaviors.dashboard.exitCustomizeMode);
Angie Byron
committed
$('div.customize .canvas-content').append('<a class="button" href="' + Drupal.settings.dashboard.dashboard + '">' + Drupal.t('Done') + '</a>');
Dries Buytaert
committed
// Initialize drag-and-drop.
var regions = $('#dashboard div.region');
regions.sortable({
connectWith: regions,
cursor: 'move',
cursorAt: {top:0},
dropOnEmpty: true,
Dries Buytaert
committed
items: '> div.block, > div.disabled-block',
Dries Buytaert
committed
placeholder: 'block-placeholder clearfix',
Dries Buytaert
committed
tolerance: 'pointer',
Dries Buytaert
committed
start: Drupal.behaviors.dashboard.start,
Dries Buytaert
committed
over: Drupal.behaviors.dashboard.over,
sort: Drupal.behaviors.dashboard.sort,
Dries Buytaert
committed
update: Drupal.behaviors.dashboard.update
});
},
/**
* While dragging, make the block appear as a disabled block
*
* This function is called on the jQuery UI Sortable "start" event.
*
* @param event
* The event that triggered this callback.
* @param ui
* An object containing information about the item that is being dragged.
*/
start: function (event, ui) {
Dries Buytaert
committed
$('#dashboard').removeClass('customize-inactive');
Dries Buytaert
committed
var item = $(ui.item);
// If the block is already in disabled state, don't do anything.
if (!item.hasClass('disabled-block')) {
item.css({height: 'auto'});
}
},
Dries Buytaert
committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* While dragging, adapt block's width to the width of the region it is moved
* into.
*
* This function is called on the jQuery UI Sortable "over" event.
*
* @param event
* The event that triggered this callback.
* @param ui
* An object containing information about the item that is being dragged.
*/
over: function (event, ui) {
var item = $(ui.item);
// If the block is in disabled state, remove width.
if ($(this).closest('#disabled-blocks').length) {
item.css('width', '');
}
else {
item.css('width', $(this).width());
}
},
/**
* While dragging, adapt block's position to stay connected with the position
* of the mouse pointer.
*
* This function is called on the jQuery UI Sortable "sort" event.
*
* @param event
* The event that triggered this callback.
* @param ui
* An object containing information about the item that is being dragged.
*/
sort: function (event, ui) {
var item = $(ui.item);
if (event.pageX > ui.offset.left + item.width()) {
item.css('left', event.pageX);
}
},
Dries Buytaert
committed
/**
* Send block order to the server, and expand previously disabled blocks.
*
* This function is called on the jQuery UI Sortable "update" event.
*
* @param event
* The event that triggered this callback.
* @param ui
* An object containing information about the item that was just dropped.
*/
update: function (event, ui) {
Dries Buytaert
committed
$('#dashboard').addClass('customize-inactive');
Dries Buytaert
committed
var item = $(ui.item);
// If the user dragged a disabled block, load the block contents.
if (item.hasClass('disabled-block')) {
var module, delta, itemClass;
itemClass = item.attr('class');
// Determine the block module and delta.
module = itemClass.match(/\bmodule-(\S+)\b/)[1];
delta = itemClass.match(/\bdelta-(\S+)\b/)[1];
// Load the newly enabled block's content.
$.get(Drupal.settings.dashboard.blockContent + '/' + module + '/' + delta, {},
function (block) {
if (block) {
item.html(block);
Dries Buytaert
committed
}
Dries Buytaert
committed
if (item.find('div.content').is(':empty')) {
item.find('div.content').html(Drupal.settings.dashboard.emptyBlockText);
Dries Buytaert
committed
}
Dries Buytaert
committed
Drupal.attachBehaviors(item);
Dries Buytaert
committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
},
'html'
);
// Remove the "disabled-block" class, so we don't reload its content the
// next time it's dragged.
item.removeClass("disabled-block");
}
Drupal.behaviors.dashboard.addPlaceholders();
// Let the server know what the new block order is.
$.post(Drupal.settings.dashboard.updatePath, {
'form_token': Drupal.settings.dashboard.formToken,
'regions': Drupal.behaviors.dashboard.getOrder
}
);
},
/**
* Return the current order of the blocks in each of the sortable regions,
* in query string format.
*/
getOrder: function () {
var order = [];
$('#dashboard div.region').each(function () {
var region = $(this).parent().attr('id').replace(/-/g, '_');
var blocks = $(this).sortable('toArray');
Dries Buytaert
committed
$.each(blocks, function() {
order.push(region + '[]=' + this);
});
Dries Buytaert
committed
});
order = order.join('&');
return order;
}
};
})(jQuery);