Created
February 24, 2011 23:12
-
-
Save RichardDavies/843093 to your computer and use it in GitHub Desktop.
Enhancements to Fitted jQuery plugin to avoid form elements and additional links. My changes are identified by the 'Begin CGIS change' comments.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Fitted: a jQuery Plugin | |
* @author: Trevor Morris (trovster) | |
* @url: http://www.trovster.com/lab/code/plugins/jquery.fitted.js | |
* @documentation: http://www.trovster.com/lab/plugins/fitted/ | |
* @published: 11/09/2008 | |
* @updated: 29/09/2008 | |
* @requires: jQuery v.1.2.6 or above | |
* | |
* @notes: | |
* Also see BigTarget by Leevi Graham - http://newism.com.au/blog/post/58/bigtarget-js-increasing-the-size-of-clickable-targets/ | |
* | |
* @modifiedBy: Richard Davies http://www.richarddavies.us | |
* | |
*/ | |
if(typeof jQuery != 'undefined') { | |
jQuery(function($) { | |
$.fn.extend({ | |
fitted: function(options) { | |
var settings = $.extend({}, $.fn.fitted.defaults, options); | |
/** | |
* Begin CGIS change | |
*/ | |
var getSelectedText = function() { | |
if (window.getSelection) { | |
return window.getSelection().toString(); | |
} else if (document.getSelection) { | |
return document.getSelection(); | |
} else if (document.selection) { | |
// This is specifically for IE | |
return document.selection.createRange().text; | |
} | |
}; | |
/** | |
* End CGIS change | |
*/ | |
return this.each( | |
function() { | |
if($.fn.jquery < '1.2.6') {return;} | |
var $t = $(this); | |
var o = $.metadata ? $.extend({}, settings, $t.metadata()) : settings; | |
if($t.find(':has(a)')) { | |
/** | |
* Find the first Anchor | |
* @var object $a | |
*/ | |
var $a = $t.find('a:first'); | |
/** | |
* Get the Anchor Attributes | |
*/ | |
var href = $a.attr('href'); | |
var title = $a.attr('title'); | |
/** | |
* Setup the Container | |
* Add the 'container' class defined in settings | |
* @event hover | |
* @event click | |
*/ | |
$t.addClass(o['class']['container']).hover( | |
function(){ | |
/** | |
* Hovered Element | |
*/ | |
$h = $(this); | |
/** | |
* Add the 'hover' class defined in settings | |
*/ | |
$h.addClass(o['class']['hover']); | |
/** | |
* Add the Title Attribute if the option is set, and it's not empty | |
*/ | |
if(typeof o['title'] != 'undefined' && o['title']===true && title != '') { | |
$h.attr('title',title); | |
} | |
/** | |
* Set the Status bar string if the option is set | |
*/ | |
if(typeof o['status'] != 'undefined' && o['status']===true) { | |
if($.browser.safari) { | |
/** | |
* Safari Formatted Status bar string | |
*/ | |
window.status = 'Go to "' + href + '"'; | |
} | |
else { | |
/** | |
* Default Formatted Status bar string | |
*/ | |
window.status = href; | |
} | |
} | |
}, | |
function(){ | |
/** | |
* "un"-hovered Element | |
*/ | |
$h = $(this); | |
/** | |
* Remove the Title Attribute if it was set by the Plugin | |
*/ | |
if(typeof o['title'] != 'undefined' && o['title']===true && title != '') { | |
$h.removeAttr('title'); | |
} | |
/** | |
* Remove the 'hover' class defined in settings | |
*/ | |
$h.removeClass(o['class']['hover']); | |
/** | |
* Remove the Status bar string | |
*/ | |
window.status = ''; | |
} | |
).click( | |
function(event){ | |
/** | |
* Clicked! | |
* The Container has been Clicked | |
*/ | |
/** | |
* Begin CGIS change | |
*/ | |
// Don't do anything if selecting text inside fitted element | |
if (getSelectedText().length > 0) { | |
return true; | |
} | |
// Don't override clicks on form elements | |
if ($(event.target).is('input, select, option, optgroup textarea, button')) { | |
return true; | |
} | |
// Goto whatever link is clicked on (not just the first anchor) | |
if ($(event.target).is('a')) { | |
$a = $(event.target); | |
href = $a.attr('href'); | |
} | |
/** | |
* End CGIS change | |
*/ | |
/** | |
* Trigger the Anchor / Follow the Link | |
*/ | |
if($a.is('[rel*=external]')){ | |
window.open(href); | |
return false; | |
} | |
else { | |
//$a.click(); $a.trigger('click'); | |
window.location = href; | |
} | |
} | |
); | |
} | |
} | |
); | |
} | |
}); | |
/** | |
* Plugin Defaults | |
*/ | |
$.fn.fitted.defaults = { | |
'class' : { | |
'container' : 'fitted', | |
'hover' : 'hovered' | |
}, | |
'title' : true, | |
'status' : false | |
}; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment