Skip to content

Instantly share code, notes, and snippets.

@nfeldman
Created August 19, 2011 20:56
Show Gist options
  • Save nfeldman/1157984 to your computer and use it in GitHub Desktop.
Save nfeldman/1157984 to your computer and use it in GitHub Desktop.
genericish implementation of a block toggling script for Drupal
/**
* @fileoverview, written for my own amusement, these few lines make a robust
* replacement for the first 340ish lines found within the jQuery on ready
* handler in the main.js file on the new site. This is fine as a standalone,
* but there are redundancies if this is used with the other scripts I've
* contributed to this project. If used, the duplication should be fixed.
* @author Noah Feldman
* updated 2011-08-22: removed superfluous semicolon
*/
// PUT THIS IN THE `$(document).ready(function () {` IN main.js
// IN PLACE OF LINES 4 - 349
var sidebar = $('.share-block'),
rBlacklist = /^(?:block-search_.+|.+-(5204|2634))$/, // blocks to not toggle, add tests as needed
rStartClosed = /^\/patient-care\/(?:diagnoses-disorders\/.+|patient-care-programs\/outpatient-programs\/.+)/, // paths for pages to collapse the first two blocks on by default
headings = [], sidebarToggler;
function toggler (toggles, toggleds, nodeName, show, hide) {
var rTest, toggleClass = {}, u = void(0);
// can be a string or an array
nodeName = typeof nodeName == 'string' ? RegExp(nodeName, 'i') : RegExp('(?:' + nodeName.join('|') + ')', 'i');
// should check for existence of just one or the other, as well...
if (!show && !hide)
((show = 'show') && (hide = 'hide'));
toggleClass[show] = hide;
toggleClass[hide] = show;
rTest = RegExp('(' + show + '|' + hide + ')');
function indexOf (item, list) { // cross-browser indexOf for anything
if (!!list.indexOf) // with a length. Is also used by menu
return list.indexOf(item); // highlight script, so should combine
for (var i = 0, len = list.length; i < len; i++)
if (list[i] === item)
return i;
return -1;
}
return function (e, idx) {
// if the event target has an expected nodeName
if (e.target.nodeName && nodeName.test(e.target.nodeName)) {
if (typeof idx != 'number')
idx = indexOf(e.target, toggles);
if (idx > -1) {
toggles[idx].className = toggles[idx].className.replace(rTest, function (m) {
return toggleClass[m]; });
toggleds.eq(idx).stop(true, true).slideToggle(333);
}
}
}
}
// simple crossbrowser each function for anything with a length
function each (list, callback, thisObject) {
for (var i = 0, len = list.length; i < len; i++)
callback.call(thisObject, list[i], i);
}
if (sidebar[0]) {
each(sidebar[0].getElementsByTagName('h2'), function(el, i) {
if (el.parentNode && !rBlacklist.test(el.parentNode.id)) {
el.className += ' show-block';
return headings[i] = el; // create a sparse array to keep sync
} // between toggleds and toggles indices
});
sidebarToggler = toggler(headings, sidebar.find('h2 + .content'), 'h2');
if (rStartClosed.test(location.pathname)) {
sidebarToggler({target: {nodeName: 'h2'}}, 0); // start collapsed
sidebarToggler({target: {nodeName: 'h2'}}, 1); // start collapsed
}
sidebar.bind('click', sidebarToggler);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment