Difference between revisions of "FancyBox"
(use webroot correctly, and import common javascript.) |
(start documenting how to convert popups to fancybox.) |
||
Line 54: | Line 54: | ||
<nowiki></div></nowiki> | <nowiki></div></nowiki> | ||
== | == Converting == | ||
Pages being called via fancybox need to be aware that they are in an inframe, and not in a separate popup window. | Pages being called via fancybox need to be aware that they are in an inframe, and not in a separate popup window. | ||
The simple way to check in javascript, is to see if there is an 'opener' object available. if a page was called as a popup, opener will exist. if it does not exist, it is likely that the page was called as an iframe. | |||
for example: | |||
if (opener) | |||
{ // we are a popup | |||
if (!opener.closed && opener.refreshme) | |||
{ | |||
opener.refreshme(); | |||
window.close(); | |||
} | |||
} | |||
else | |||
{ // we are an iframe | |||
{ | |||
window.parent.refreshme(); | |||
window.parent.$.fn.fancybox.close(); | |||
} | |||
} | |||
This code shows a couple of things we should keep in mind, when converting code from being popup-only, to allowing code to be used in a fancybox, namely: | |||
:* opener does not exist. to call functions in the page that 'opened' you, use window.parent. | |||
:* window.close no longer works. instead, call the parent's fancybox close function: window.parent.$.fn.fancybox.close(); |
Revision as of 13:23, 21 December 2012
FancyBox In OpenEMR
Fancybox is used in OpenEMR as a replacement for pop-up windows. popups are becoming deprecated in modern browsers, and the window.close() function used to make a popup disappear is disabled in at least one browser.
Versions
Two versions of FancyBox exist in the OpenEMR codebase.
Integrating these two versions is not a big priority, see thread: http://osdir.com/ml/fancybox/2011-12/msg00192.html .
For new code, it is preferred to use the 1.3.4 version, rather than the 1.2.6 version.
Using
Required Javascript
First, a document using the fancybox code must include jquery, and fancybox:
<script type="text/javascript" src="<?php echo $GLOBALS['webroot']; ?>/library/js/jquery.1.3.2.js"></script> <script type="text/javascript" src=".<?php echo $GLOBALS['webroot']; ?>/library/js/fancybox-1.3.4/jquery.fancybox-1.3.4.pack.js"></script> <link rel="stylesheet" type="text/css" href="<?php echo $GLOBALS['webroot']; ?>/library/js/fancybox-1.3.4/jquery.fancybox-1.3.4.css" media="screen" /> <script type="text/javascript" src="<?php echo $GLOBALS['webroot']; ?>/library/js/common.js"></script>
Initialising
Inside of the document's ready function, the following code needs to exist, to initialise each link that is supposed to open a fancybox:
$(document).ready(function(){ // initialise fancy box enable_modals(); // initialise a link $(".linkclass_modal").fancybox( { 'overlayOpacity' : 0.0, 'showCloseButton' : true, 'frameHeight' : 400, 'frameWidth' : 500 }); });
Using with a link
To open a page into a fancybox, a link must either be clicked, or have its 'click' handler triggered.
This link (an 'a' entity) must have two classes associated with it: 'iframe', and the same ID supplied in the initialisation (linkclass_modal in our example).
Using inside of an onclick()
To open a page from an onclick() handler, its necessary to trigger the 'click' handle of an invisible link.
for example:
function popupfancybox() { $("a.findpatient_modal").trigger("click"); } <div style="display: none;"> <a class="iframe findpatient_modal" href="find_patient_popup.php"></a> </div>
Converting
Pages being called via fancybox need to be aware that they are in an inframe, and not in a separate popup window.
The simple way to check in javascript, is to see if there is an 'opener' object available. if a page was called as a popup, opener will exist. if it does not exist, it is likely that the page was called as an iframe.
for example:
if (opener) { // we are a popup if (!opener.closed && opener.refreshme) { opener.refreshme(); window.close(); } } else { // we are an iframe { window.parent.refreshme(); window.parent.$.fn.fancybox.close(); } }
This code shows a couple of things we should keep in mind, when converting code from being popup-only, to allowing code to be used in a fancybox, namely:
- opener does not exist. to call functions in the page that 'opened' you, use window.parent.
- window.close no longer works. instead, call the parent's fancybox close function: window.parent.$.fn.fancybox.close();