1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
// $Id$
(function ($) {
/**
* Implementation of Drupal.behaviors for dashboard.
*/
Drupal.behaviors.dashboard = {
attach: function () {
$('#dashboard').prepend('<div class="customize"><ul class="action-links"><a href="#">' + Drupal.t('Customize') + '</a></ul><div class="canvas"></div></div>');
$('#dashboard .customize .action-links a').click(Drupal.behaviors.dashboard.enterCustomizeMode);
if ($('#dashboard .region .block').length == 0) {
Drupal.settings.dashboard.launchCustomize = true;
}
Drupal.behaviors.dashboard.addPlaceholders();
if (Drupal.settings.dashboard.launchCustomize) {
Drupal.behaviors.dashboard.enterCustomizeMode();
}
},
addPlaceholders: function() {
$('#dashboard .dashboard-region .region').each(function () {
var empty_text = "";
if ($('.block', this).length == 0) {
// We need a placeholder.
if ($('#dashboard').hasClass('customize_mode')) {
empty_text = Drupal.settings.dashboard.emptyRegionTextActive;
} else {
empty_text = Drupal.settings.dashboard.emptyRegionTextInactive;
}
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 () {
$('#dashboard').addClass('customize_mode');
Drupal.behaviors.dashboard.addPlaceholders();
$('#dashboard .customize .action-links').hide();
$('div.customize .canvas').load(Drupal.settings.dashboard.drawer, Drupal.behaviors.dashboard.setupDrawer);
},
/**
* Exit "customize" mode by simply forcing a page refresh.
*/
exitCustomizeMode: function () {
$('#dashboard').removeClass('customize_mode');
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').prepend('<input type="button" class="form-submit" value="' + Drupal.t('Done') + '"></input>');
$('div.customize .canvas-content input').click(Drupal.behaviors.dashboard.exitCustomizeMode);
// Initialize drag-and-drop.
var regions = $('#dashboard div.region');
regions.sortable({
connectWith: regions,
cursor: 'move',
cursorAt: {top:0},
dropOnEmpty: true,
items: '>div.block, div.disabled-block',
opacity: 0.8,
helper: 'block-dragging',
placeholder: 'block-placeholder clearfix',
start: Drupal.behaviors.dashboard.start,
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) {
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'});
}
},
/**
* 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) {
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) {
var blockContent = "";
if (block) {
blockContent = $("div.content", $(block));
}
if (!blockContent) {
blockContent = $('<div class="content">' + Drupal.settings.dashboard.emptyBlockText + '</div>');
}
$("div.content", item).after(blockContent).remove();
},
'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');
var i;
for (i = 0; i < blocks.length; i++) {
order.push(region + '[]=' + blocks[i]);
}
});
order = order.join('&');
return order;
}
};
})(jQuery);
|