Newer
Older
The Great Git Migration
committed
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
175
176
177
178
179
180
181
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
(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');
blocks.push(bid);
});
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();
}
});
};
// 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();
$(block).attr('id', $(this).attr('value')).addClass('draggable');
$(block).html("<td>"+ text + "<input class='block-weight' /></td><td><a href='' class='remove'>X</a></td>");
// add block item to region
var base = "context-blockform-region-"+ region;
Drupal.tableDrag[base].makeDraggable(block);
$('table#'+base).append(block);
Drupal.attachBehaviors($('table#'+base));
Drupal.contextBlockForm.setState();
$(this).removeAttr('checked');
});
}
return false;
});
});
};
/**
* Context block editor. AHAH editor for live block reaction editing.
*/
DrupalContextBlockEditor = function(editor) {
this.editor = editor;
this.state = {};
this.blocks = {};
this.regions = {};
// Category selector handler.
// Also set to "Choose a category" option as browsers can retain
// form values from previous page load.
$('select.context-block-browser-categories', editor).change(function() {
var category = $(this).val();
var params = {
containment: 'document',
revert: true,
dropOnEmpty: true,
placeholder: 'draggable-placeholder',
forcePlaceholderSize: true,
helper: 'clone',
appendTo: 'body',
connectWith: ($.ui.version === '1.6') ? ['.ui-sortable'] : '.ui-sortable'
};
$('div.category', editor).hide().sortable('destroy');
$('div.category-'+category, editor).show().sortable(params);
});
$('select.context-block-browser-categories', editor).val(0).change();
return this;
};
DrupalContextBlockEditor.prototype.initBlocks = function(blocks) {
var self = this;
this.blocks = blocks;
blocks.each(function() {
$(this).addClass('draggable');
$(this).prepend($('<a class="context-block-handle"></a>'));
$(this).prepend($('<a class="context-block-remove"></a>').click(function() {
$(this).parents('div.block').eq(0).fadeOut('medium', function() {
$(this).remove();
self.updateBlocks();
});
return false;
}));
});
};
DrupalContextBlockEditor.prototype.initRegions = function(regions) {
this.regions = regions;
};
/**
* Update UI to match the current block states.
*/
DrupalContextBlockEditor.prototype.updateBlocks = function() {
var browser = $('div.context-block-browser');
// For all enabled blocks, mark corresponding addables as having been added.
$('div.block, div.admin-block').each(function() {
var bid = $(this).attr('id').split('block-')[1]; // Ugh.
$('#context-block-addable-'+bid, browser).draggable('disable').addClass('context-block-added').removeClass('context-block-addable');
});
// For all hidden addables with no corresponding blocks, mark as addable.
$('.context-block-item', browser).each(function() {
var bid = $(this).attr('id').split('context-block-addable-')[1];
if ($('#block-'+bid).size() === 0) {
$(this).draggable('enable').removeClass('context-block-added').addClass('context-block-addable');
}
});
// Mark empty regions.
$(this.regions).each(function() {
if ($('div.block:has(a.context-block)', this).size() > 0) {
$(this).removeClass('context-block-region-empty');
}
else {
$(this).addClass('context-block-region-empty');
}
});
};
/**
* Live update a region.
*/
DrupalContextBlockEditor.prototype.updateRegion = function(event, ui, region, op) {
switch (op) {
case 'over':
$(region).removeClass('context-block-region-empty');
break;
case 'out':
if (
$('div.draggable-placeholder', region).size() === 0 &&
$('div.block:has(a.context-block)', region).size() == 1 &&
$('div.block:has(a.context-block)', region).attr('id') == ui.item.attr('id')
) {
$(region).addClass('context-block-region-empty');
}
break;
}
};
/**
* Remove script elements while dragging & dropping.
*/
DrupalContextBlockEditor.prototype.scriptFix = function(event, ui, editor, context) {
if ($('script', ui.item)) {
var placeholder = $(Drupal.settings.contextBlockEditor.scriptPlaceholder);
var label = $('div.handle label', ui.item).text();
placeholder.children('strong').html(label);
$('script', ui.item).parent().empty().append(placeholder);
}
};
/**
* Add a block to a region through an AHAH load of the block contents.
*/
DrupalContextBlockEditor.prototype.addBlock = function(event, ui, editor, context) {
var self = this;
if (ui.item.is('.context-block-addable')) {
var bid = ui.item.attr('id').split('context-block-addable-')[1];
// Construct query params for our AJAX block request.
var params = Drupal.settings.contextBlockEditor.params;
params.context_block = bid + ',' + context;
// Replace item with loading block.
var blockLoading = $('<div class="context-block-item context-block-loading"><span class="icon"></span></div>');
ui.item.addClass('context-block-added');
ui.item.after(blockLoading);
ui.sender.append(ui.item);
$.getJSON(Drupal.settings.contextBlockEditor.path, params, function(data) {
if (data.status) {
var newBlock = $(data.block);
if ($('script', newBlock)) {
$('script', newBlock).remove();
}
blockLoading.fadeOut(function() {
$(this).replaceWith(newBlock);
self.initBlocks(newBlock);
self.updateBlocks();
$.each(data.css, function(k, v){
var cssfile = Drupal.settings.basePath + v;
if ($('head link[href $='+cssfile+']').length === 0 ) {
$('head').append('<link type="text/css" rel="stylesheet" media="all" href="' + cssfile + " />'");
}
});
Drupal.attachBehaviors();
});
}
else {
blockLoading.fadeOut(function() { $(this).remove(); });
}
});
}
else if (ui.item.is(':has(a.context-block)')) {
self.updateBlocks();
}
};
/**
* Update form hidden field with JSON representation of current block visibility states.
*/
DrupalContextBlockEditor.prototype.setState = function() {
var self = this;
$(this.regions).each(function() {
var region = $('a.context-block-region', this).attr('id').split('context-block-region-')[1];
var blocks = [];
$('a.context-block', $(this)).each(function() {
if ($(this).attr('class').indexOf('edit-') != -1) {
var bid = $(this).attr('id').split('context-block-')[1];
var context = $(this).attr('class').split('edit-')[1].split(' ')[0];
context = context ? context : 0;
var block = {'bid': bid, 'context': context};
blocks.push(block);
}
});
self.state[region] = blocks;
});
// Serialize here and set form element value.
$('input.context-block-editor-state', this.editor).val(JSON.stringify(this.state));
};
/**
* Disable text selection.
*/
DrupalContextBlockEditor.prototype.disableTextSelect = function() {
if ($.browser.safari) {
$('div.block:has(a.context-block):not(:has(input,textarea))').css('WebkitUserSelect','none');
}
else if ($.browser.mozilla) {
$('div.block:has(a.context-block):not(:has(input,textarea))').css('MozUserSelect','none');
}
else if ($.browser.msie) {
$('div.block:has(a.context-block):not(:has(input,textarea))').bind('selectstart.contextBlockEditor', function() { return false; });
}
else {
$(this).bind('mousedown.contextBlockEditor', function() { return false; });
}
};
/**
* Enable text selection.
*/
DrupalContextBlockEditor.prototype.enableTextSelect = function() {
if ($.browser.safari) {
$('*').css('WebkitUserSelect','');
}
else if ($.browser.mozilla) {
$('*').css('MozUserSelect','');
}
else if ($.browser.msie) {
$('*').unbind('selectstart.contextBlockEditor');
}
else {
$(this).unbind('mousedown.contextBlockEditor');
}
};
/**
* Start editing. Attach handlers, begin draggable/sortables.
*/
DrupalContextBlockEditor.prototype.editStart = function(editor, context) {
var self = this;
// This is redundant to the start handler found in context_ui.js.
// However it's necessary that we trigger this class addition before
// we call .sortable() as the empty regions need to be visible.
$(document.body).addClass('context-editing');
this.editor.addClass('context-editing');
this.disableTextSelect();
this.initBlocks($('div.block:has(a.context-block.edit-'+context+')'));
this.initRegions($('a.context-block-region').parent());
this.updateBlocks();
// First pass, enable sortables on all regions.
$(this.regions).each(function() {
var region = $(this);
var params = {
containment: 'document',
revert: true,
dropOnEmpty: true,
placeholder: 'draggable-placeholder',
forcePlaceholderSize: true,
items: '> div.block:has(a.context-block.editable)',
handle: 'a.context-block-handle',
start: function(event, ui) { self.scriptFix(event, ui, editor, context); },
stop: function(event, ui) { self.addBlock(event, ui, editor, context); },
receive: function(event, ui) { self.addBlock(event, ui, editor, context); },
over: function(event, ui) { self.updateRegion(event, ui, region, 'over'); },
out: function(event, ui) { self.updateRegion(event, ui, region, 'out'); }
};
region.sortable(params);
});
// Second pass, hook up all regions via connectWith to each other.
$(this.regions).each(function() {
$(this).sortable('option', 'connectWith', ['.ui-sortable']);
});
// Terrible, terrible workaround for parentoffset issue in Safari.
// The proper fix for this issue has been committed to jQuery UI, but was
// not included in the 1.6 release. Therefore, we do a browser agent hack
// to ensure that Safari users are covered by the offset fix found here:
// http://dev.jqueryui.com/changeset/2073.
if ($.ui.version === '1.6' && $.browser.safari) {
$.browser.mozilla = true;
}
};
/**
* Finish editing. Remove handlers.
*/
DrupalContextBlockEditor.prototype.editFinish = function() {
this.editor.removeClass('context-editing');
this.enableTextSelect();
// Remove UI elements.
$(this.blocks).each(function() {
$('a.context-block-handle, a.context-block-remove', this).remove();
$(this).removeClass('draggable');
});
this.regions.sortable('destroy');
this.setState();
// Unhack the user agent.
if ($.ui.version === '1.6' && $.browser.safari) {
$.browser.mozilla = false;
}
};
})(jQuery);