Extending colorbox.js

Colorbox is a very flexible lightbox plugin for jQuery which I have used for 2 to 3 years now and found virtually no limitations to what I’ve wanted to do with it. I’ve used it for many things like simple image previews, custom image cropping, image file uploads, as well as custom product checkout processes.

Resizing Colorbox Behavior

There is one aspect that I found difficult achieve. Recently I was working with some content in colorbox which was taller than the viewable browser window and certain actions withing the colorbox content would cause it to grow. This was primarily because the client didn’t want the scroll bars, understandably I also think they look a little ugly with modal windows. Colorbox has a helper method $.colorbox.resize() which handles resizing colorbox to fit the content within it. It has fixed and absolute positioning, with absolute being the default. The default behavior is when the method is called it also repositions the window to have the top of colorbox nicely positioned at the top of your browsers viewable area. This is exactly what my client wanted when opening colorbox, but not while the user is editing the content within colorbox.

Extending Colorbox

I prefer not to ‘hack’ the source code, and if the client ever wanted to update to the latest version of the colorbox plugin, I wanted this adjustment to the behaviour to be forgiving. So I wrote a small extension to colorbox which provided the clients desired behaviour and could be toggled on off. It works really well in Firefox and IE (7, 8, + 9), and worked reasonably well in Chrome.

I believe this would be similar to a JavaScript Decorator Pattern, since it wraps the original object behavior to modify / enhance what can be done. The difference being that a Decorated object technically would get extended at runtime, where as this is more of wrapping the object to modify the behaviour and allows it to fall back to the original functionality.

/*
 * Extend $.colorbox to allow fixed position during resize.
 */
$.extend($.colorbox, {
	_resize: $.colorbox.resize,
	resize: function(options) {
		var options = options || {reposition: this.settings.reposition};
		//add style to absolute position colorbox if reposition == false
		if('reposition' in options && !options.reposition) {
			var $style = $('head style').last();
			//and if it doesn't already exist
			if(!RegExp(/#colorbox/).test($style.html())) {
				$('head').append(
					'<style> #colorbox { ' +
					'top: ' + parseInt($('#colorbox').css('top')) + 'px !important;' +
					'} </style>'
				);
			}
		} else {
			//remove css rule if repostioning is allowed.
			var $style = $('head style').last();
			if(RegExp(/#colorbox/).test($style.html())) {
				$style.remove();
			}
		}
		return this._resize(options);
	}
});

Related Posts

Enhance Angular.js Directive - ngRepeat

Recently I started a social Phone Gap project for a client and decided it would be wise to use a JavaScript framework. It was first suggested that I look into Backbone.js. I investigated it and did a little research and digging. I looked at Backbone.js, jQuery Mobile, Ember.js, various others, and Angular.js. The long and the short is I read a slew of reviews, some documentation, a bunch or tutorials, and in the end I decided to go with Angular.js for my framework of choice.

Read More

Hello World!

Hello fellow webbers! It feels good to finally have some web space where I can get my geek on.

Read More

Useful log files

We have a simple API where I work that isn’t anything impressive, it is just a couple of scripts that accept POST and GET requests. However the previous developer who worked on it hadn’t considered the total ramifications and logged all requests using an email with print statements. Additionally, for each request there could be up to 3 emails generated, one for the initial request, one for errors, and one that included the payment gateway request and response. Not mention it definitely breaks PCI Compliance by emailing people’s credit card information, but we were also experiencing load issues on the web server for all the emails that were being generated through PHP Send Mail and the end result is about 10,000 to 100,000 emails waiting in my email account.

Read More