Monday, February 28, 2011

Providing Default Values to Wijmo Tooltips

The Wijmo Tooltip widget has certain properties set to default values.  Developers may find that  these defaults are not appropriate for the web site we are using them in.  In this post I will demonstrate how to provide your own default options for the Wijmo Tooltip widget without modifying the Wijmo source code.  This will allow you to attach tooltips to your markup without needing to duplicate the options object you provide on every page.

By default the display of the tooltip will include a callout element.  This is the wedge that points the tooltip bubble to the element the tooltip is associated with.  Below is a screenshot of a tooltip which includes the callout.
If I want to omit the callout element I can do so by using the showCallout option and setting the value to false.  Rather than include this option everytime the tooltip is used I can add a JavaScript file that will override the default options for the tooltip.  Below is the content of the file.


jQuery(document).ready( function() {
 jQuery.extend( jQuery.wijmo.wijtooltip.prototype.options, { showCallout: false } );
});


Note that I have used jQuery's extend method to merge my default values into those used by the Wijmo tooltip.

Here is a screenshot of the tooltip without the callout.
I will need to include the file which includes my custom configuration after the Wijmo tooltip file is included.  Below is the complete HTML code for the example. Note that the "custom-options.js" file is included after the usual jQuery and Wijmo files.  This will cause any Wijmo tooltips included on the page to use the new default settings which will not display the callout element.  I would include this JS file on every page of my site that uses Wijmo in order to set my own default values without modifying the Wijmo source code.
<!doctype html>
<html>
<head>
 <title>Wijmo Tooltip - Set Defaults</title>
 <link type="text/css" rel="stylesheet" href="jquery.ui.all.css">
 <link type="text/css" rel="stylesheet" href="jquery.wijmo-open.1.1.2.css">
 <script type="text/javascript" src="jquery-1.4.4.min.js"></script>
 <script type="text/javascript" src="jquery-ui-1.8.7.custom.min.js"></script>
 <script type="text/javascript" src="jquery.wijmo-open.1.1.2.min.js"></script>
 <script type="text/javascript" src="custom-options.js"></script> 
</head>
<body>

<h1>Weather Tooltip - Customized Defaults</h1>

<div>
 <ul id="cityList">
  <li data-zipcode="78701"><span class="x-tooltip">Austin, TX</span></li>
  <li data-zipcode="75210"><span class="x-tooltip">Dallas, TX</span></li>
  <li data-zipcode="76710"><span class="x-tooltip">Waco, TX</span></li>
 </ul>
</div>

<script type="text/javascript">
 
 jQuery(document).ready(function() {

  jQuery("#cityList>li>span.x-tooltip").each(function() {

   var item = jQuery(this);

   item.wijtooltip({ content: item.parent().attr("data-zipcode") });

  });
    
 });

</script>

</body>
</html>

References

Wijmo - The Wijmo JavaScript and CSS files are available at the Wijmo website, as are the required jQuery files.

http://www.wijmo.com

No comments:

Post a Comment