Control.Modal
Lightboxes, Modal Windows and Tooltips for Prototype.js
Introduction
By default, Control.Modal creates modal windows and lightboxes from any links/anchors elements on your pages. Since it attaches these behaviors to HTML that already has semantic meaning, it will degrade gracefully for browsers that do not support JavaScript, and is search engine friendly. It attaches in one line of code for simple use cases, but is highly customizable and can be used in a variety of edge cases.
Examples of Common Use Cases
Name | Description |
Relative Modal | Link HREF: #test_one_contents Parameters: opacity: 0.8, position: 'relative', width:300, height:50 |
Centered Modal | Link HREF: #test_two_contents Parameters: containerClassName: 'test', overlayClassName: 'test', width: 300 |
AJAX Modal | Link HREF: http://livepipe.net/projects/control_modal/modal_ajax Parameters: none |
IFrame Modal | Link HREF: http://google.com/ Parameters: iframe: true, width: 640, height: 480 |
Simple Lightbox | Link HREF: /ryan/photos/photos/Plants/Banners_Closeup.jpg Parameters: none |
Lightbox w/Effects | Link HREF: /ryan/photos/photos/Plants/Banners_Closeup.jpg Parameters: fade: true |
Hoverbox | Link HREF: #hoverbox_one_contents Parameters: hover: true, position: 'relative', offsetLeft: 100 |
<span> Hoverbox | Parameters: hover: true, position: 'relative', offsetLeft: 120, contents: 'Control.Modal can attach to any element.' |
<span> Tooltip | Parameters: position: 'mouse', offsetTop: 20, contents: 'Control.Modal can act as a tooltip too!' |
Sample Code
<a href="#test_one_contents" id="modal_link_one">Relative Modal</a>
<div id="test_one_contents">
I am the innerHTML of a div with the id "test_one_contents". You can make a modal window open relative to any element on the page.
</div>
<script>
new Control.Modal('modal_link_one',{
opacity: 0.8,
position: 'relative',
width: 300,
height: 50
});
</script>
Note that all of the examples above use id attributes in the anchor tags. If you were to assign class="modal" to each link, you could instead do the following if all modals are going to have the same options:
<a href="#modal_window_one" class="modal">Modal One</a>
<a href="#modal_window_two" class="modal">Modal Two</a>
<script>
document.getElementsByClassName('modal').each(function(link){
new Control.Modal(link);
});
</script>
HTML / CSS Containers & Names
This script automatically creates two nodes that are direct children of the body node of your document. They are #modal_overlay and #modal_container. The overlay is what makes the screen dim. The container contains the contents of the currently open modal window. You need to assign some default styles to these containers. This is more or less what is used on this page:
#modal_container {
padding:5px;
background-color:#fff;
border:1px solid #666;
overflow:auto;
font-family:"Lucida Grande",Verdana;
font-size:12px;
color:#333;
text-align:left;
}
#modal_overlay {
background-color:#000;
}
Using Control.Modal as a Lightbox
If your link ends in a file name that has any of the following extensions jpg, jpeg, gif, png, tif, tiff, Control.Modal will automatically treat it as an image modal / lightbox. An image element with an id of #modal_image is automatically created and destoryed each time the modal opens and closes.
Using Control.Modal as a Hoverbox
You can easily repurpose any modal window to act as a hoverbox by specifying {hover: true} in the options hash. In the example used above notice that position was set to relative and an offsetLeft was specified. The modal can be in any mode (local, iframe, ajax, image), and the two differences in functionality are that the overlay never appears, and instead of a click opening and closing the window, the onmouseover and onmouseout events on the link trigger it. Note that if the modal completely overlaps the link that opened it, you won't be able to close the modal in some browsers since the onmouseout even will no longer fire. You can prevent this by offsetting the modal window from the link or attaching Control.Modal.close events to other elements on the page.
Using Control.Modal as a Tooltip
Pass in the option {position: 'mouse'} on any modal to make it act like a tooltip (the modal will follow the mouse). {hover: true} is automatically applied when this happens, so most of the functionality and caveats that apply to a hoverbox apply here. Note that Safari 1 & 2 both have CPU usage problems on complex pages with this option. Firefox and IE both behave as expected. This will be fixed in an upcoming release.
Local, AJAX or iframe Modals
You can open a modal window to content that is already on the page, so long as it is conatained in any element that has an id. You create this kind of link just as you would link to any other element id <a href="#my_container_name">. If you want to link to an AJAX call just specify the full URL in your link's href. To create an iframe, do the same and pass in the parameter {iframe: true}. The iframe element will have an id of #modal_iframe by default.
Linking to a Modal Window from Another Page
You can have a modal window pre opened on the page if is local / on page (not an AJAX, iframe or image modal), by adding the id of the container to the page url. For example load the following URL in another window: http://livepipe.net/projects/control_modal/#test_one_contents
Closing the Modal Window with a Link
You can close any modal window by calling Control.Modal.close(); You don't need a reference to the currently open modal window to do that. From inside an opened iframe you'll need to call parent.Control.Modal.close();
How to Read the API Table
A "Class Method or Property" is often referred to in other languages as a static method or property. The "overlay" property would be referenced as "Control.Modal.overlay". An "Instance Method or Property" refers to something that is available to each instance of the class. For example, the "open" would be referenced like so: "var my_modal = new Control.Modal(); my_modal.open();". Anything in the method and property lists that ends with "()" is a method, and anything that doesn't is a property. All events are specifiable in the options hash, or via "observe()" if Object.Event has been included on the page before Control.Modal was. In a method signature, each argument is separated by a comma, if the words are lower case it refers to the data type and gives a hint to what it is used for, if there is an upper case word it refers to the class of object that must be used. Arguments with [square brackets] around them are optional.
Class Methods and Properties
Return | Name | Description |
Element | overlay | The #modal_overlay Element |
Element | container | The #modal_container Element |
booelan || Control.Modal | current | false, or the currently open Control.Modal instance. |
Control.Modal | open(mixed contents[,options Hash]) | Creates a new Control.Modal instance and opens it. |
void | close() | Closes the currently open Control.Modal instance if there are any. |
Instance Methods and Properties
Return | Name | Description |
Control.Modal | initialize(mixed element_id[,options Hash]) | Create a new instance by passing in a string id or Element object. |
void | open() | Open the modal window. |
void | close() | Close the modal window. |
void | update(mixed contents) | Update the contents of the instance with a string, or an array of Element nodes. |
Options
Type | Name | Default | Description |
string || function | contents | false | Manually specify the contents of the modal window. Required if the instance is not attached to any particular element. |
boolean || string | loading | false | Optional path to a loading indicator graphic for lightbox and Ajax modals only. |
int || function || null | width | null | Manually specify the width of the modal_container. |
int || function || null | height | null | Manually specify the height of the modal_container. |
string | position | 'absolute' | Wether to center the modal (absolute), or make it 'relative' to the element it is attached to, or have it follow the 'mouse'. |
int || function | offsetTop | 0 | For 'relative' positioned modals. |
int || function | offsetLeft | 0 | For 'relative' positioned modals. |
boolean | fade | false | Attaches fade and appear effects when opening and closing the modal window. Scriptaculous effects.js must be available. |
float | fadeDuration | 0.75 | Duration of the fade. |
boolean | image | false | Force the modal to act as an image/lightbox even if the given link href does not end with an image extension. |
boolean | imageCloseOnClick | true | Close the modal when the image is clicked. For lightboxes only. |
boolean | hover | false | Open and close the modal when the mouse is over the attached element instead of clicking. |
boolean | iframe | false | Load a given URL with an Iframe instead of an Ajax request. |
Hash | requestOptions | {} | For Ajax modals, the options that will be passed to Ajax.Request. |
boolean | overlayDisplay | true | Wether or not to display the modal_overlay. If hover is true, this automatically gets set to false. |
booelan | overlayCloseOnClick | true | Close this instance when the modal_overlay is clicked. |
string | overlayClassName | '' | Class name to add to modal_overlay when it is opened by this instance. |
string | containerClassName | '' | Class name to add to modal_container when it is opened by this instance. |
float | opacity | 0.3 | The opacity of the modal_overlay. |
int | zIndex | 9998 | The zIndex of the modal_overlay. The modal_container will have a property of this + 1. |
boolean | autoOpenIfLinked | true | If the modal link is to #container and the current url contains #container, it will auto open. |
function | beforeOpen | function(){} | Event callback. |
function | afterOpen | function(){} | Event callback. |
function | beforeClose | function(){} | Event callback. |
function | afterClose | function(){} | Event callback. |
function | onSuccess | function(){} | Event callback. |
function | onFailure | function(){} | Event callback. |
function | onException | function(){} | Event callback. |
function | beforeImageLoad | function(){} | Event callback. |
function | afterImageLoad | function(){} | Event callback. |
Events
All events can be passed in as an option, or if Object.Event has been included, they can be observed by calling Control.Modal.observe() to observe all instances, or by calling observe() on any particular instance to just observe that one.
Name | Description |
beforeOpen() | Throwing $break will stop the modal from opening. |
afterOpen() | |
beforeClose() | Throwing $break will stop the modal from closing. |
afterClose() | |
onSuccess(ajax_response) | For Ajax modals only. |
onFailure() | For Ajax modals only. |
onException() | For Ajax modals only. |
beforeImageLoad() | For lightbox modals only. |
afterImageLoad() | For lightbox modals only. |
Code Examples
//contents can be a string
new Control.Modal(link,{
contents: 'contents of modal'
});
//or a function that returns a string, called each time the modal is opened
//"this" will refer to the modal object
m = new Control.Modal(false,{
contents: function(){
return 'contents of modal';
}
});
m.open();
//or you can use Control.Modal.open() to do the same thing
Control.Modal.open('contents of modal');
link.observe('click',function(){
Control.Modal.open('contents of my modal',{
afterOpen: function(){
//attach behaviors to the contents here
}
});
});
//this is an alternate way of doing AJAX modals with more granular control
//your loading html will be replaced by the Ajax.Request response when the call completes
m = new Control.Modal(false,{
contents: function(){
new Ajax.Request(url,{
onComplete: function(request){
this.update(request.responseText);
}.bind(this)
});
return 'loading';
}
});
m.open();
RSS Changelog
Is available at http://livepipe.net/projects/control_modal/changelog.rss
Downloads & SVN
The new 2.0 version is available via the button in the upper right. It introduces a backwards compatibility break with 1.0 by removing the responders functionality. This functionality is superseded by using the Object.Event library.
The Control Suite subversion repository is available at: svn://livepipe.net/control_suite/
The last 1.0 series release is available at http://livepipe.net/downloads/control.modal.1.2.12.js
Online Forum
Do you have questions, bug reports or suggestions? Visit the Control.Modal online forum.
Other Resources
Have you written a post or article about Control.Modal? Please contact me!