September 2014

Drupal Bootstrap 3 theme media queries javascript event (like Omega 3)

Having used the Omega 3 Drupal theme for a few years now I got quite use to writing javascript functions that would react to the 'reponsivelayout' event that was supplied by the theme. This event would fire after the responsive classes were applied to the body tag when the page loaded or when the media queries changed the site layout.
I often used this event to create things like mobile menus and such.
Now on the latest site I am building I am trying out the Bootstrap theme which does not fire such an event, so I thought I would add one that basically uses javascript similar to the Omega 3 theme.
If you would like the same, just add a js file to your Bootstrap sub theme with the js below:

 (function($) { 

var setCurrentLayout = function (oldLayout, newLayout) {
$.event.trigger('responsivelayout', {from: oldLayout, to: newLayout});

var checkCurrentLayout = function () {
var current = 'xs';
var dummy = $('<div id="bootstrap-media-query-dummy" class="container"></div>').prependTo('body');
var width = dummy.outerWidth();

if (width > 750 - 1) {
current = 'sm';
if (width > 970 - 1) {
current = 'md';
if (width > 1170 - 1) {
current = 'lg';
if (typeof Drupal.settings.currentLayout == 'undefined') {
Drupal.settings.currentLayout = current;
setCurrentLayout('', current);
} else if (Drupal.settings.currentLayout != current) {
var old = Drupal.settings.currentLayout;
Drupal.settings.currentLayout = current;
setCurrentLayout(old, current);