Skip to content

Commit

Permalink
Adding some JS snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhund committed May 6, 2010
0 parents commit 8b1c6e1
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
@@ -0,0 +1,6 @@
# VS-JS
## Basic(!), reusable JS snippets and components

This is simply a collection of reusable JS components.

I'm pretty sure there's heaps of room for improvement: feedback is more than welcome!
2 changes: 2 additions & 0 deletions snippets/console.log.js
@@ -0,0 +1,2 @@
// Safeguard console.log('log this'); function :-)
if(typeof(console)=="undefined"){console={log:function(m){ alert(m);}}};
50 changes: 50 additions & 0 deletions snippets/jquery.inputempty.js
@@ -0,0 +1,50 @@
/*
* jQuery Input Empty on Focus plugin 1.0
*
* http://valuedstandards.com/jquery-plugins/jquery-input-empty-on-focus/
*
* Copyright (c) 2009 - 2010 David Hund
* Use it as you like: some link-love would be nice though :-)
*
*/

if(typeof jQuery != 'undefined') {
(function($) {
jQuery.fn.emptyonfocus = function(options) {
var defaults = {
validInputsSelector: ":visible:enabled:text, :visible:enabled:password, textarea:visible:enabled", // A jquery selector targeting only valid inputs
// If the given el's do not contain valid inputs, catch ALL in the document...
getAllInputsIfEmpty: true,
focusClassName: "focused"
},
options = $.extend(defaults, options);

return this.each(function(){
var $inputEls = $(this);
// Check if given elements are text[type="input"] or textarea
if($inputEls.is(options.validInputsSelector)){
var vf = $(elements);
// OR if the elements are forms that _contain_ those elements!
}else if($inputEls.is('form')){
var vf = $inputEls.find(options.validInputsSelector);
}else{
// OR use ALL valid inputs in the whole document!
if(options.getAllInputsIfEmpty){
var vf = $(options.validInputsSelector);
}
}

if(vf){
vf.focus(function(){
if($(this).val() === this.defaultValue){$(this).val("")};
$(this).addClass(options.focusClassName);
})
.blur(function(){
if(!$(this).val()){$(this).val(this.defaultValue)};
$(this).removeClass(options.focusClassName);
});
}
});
};
})(jQuery);
}
1 change: 1 addition & 0 deletions snippets/jquery.inputempty.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions snippets/jquery.replacemail.js
@@ -0,0 +1,42 @@
/*
* jQuery Replace Mail link plugin 1.0
*
* http://valuedstandards.com/jquery-plugins/jquery-replace-mail/
*
* Copyright (c) 2009 - 2010 David Hund
* Use it as you like: some link-love would be nice though :-)
*
* - Looks for elements with a classname and an alternative @-symbol
* - .. then replaces those with a valid mail link
*/

if(typeof jQuery != 'undefined') {
(function($) {
jQuery.fn.replacemail = function(options) {
// Default: <span class="email">info [AT] this domain</span>
var defaults = {
atSymbol: " [AT] ", // Used to hide an email address in plain text
domain: window.location.domain, // the default second part of email address: the domain.com
domainString: "this domain", // A JS-off replacement text
className: "email" // Extra classname added to created email links
},
options = $.extend(defaults, options);

return this.each(function(){
var textmail = $(this);
var text1 = textmail.text().toLowerCase();
var at = options.atSymbol.toLowerCase();
if(text1.indexOf(at) > 0){
var pre = text1.substring(0, text1.indexOf(at)).replace(/^\s+|\s+$/g,""); // Remove whitespace
var post = text1.substring(text1.length, (text1.indexOf(at)+at.length)).replace(/^\s+|\s+$/g,"");
var chosen_domain = options.domain;
if(post !== window.location.domain && post !== options.domainString){chosen_domain = post};
var address = pre+"&#64;"+chosen_domain;
var link = "<a href='mailto:"+address+"' class='"+options.className+"'>"+address+"</a>";
textmail.replaceWith(link);
}
});
};
})(jQuery);
}

1 change: 1 addition & 0 deletions snippets/jquery.replacemail.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions snippets/jquery.smooth-scrolling-anchors.js
@@ -0,0 +1,52 @@
/* ===============================================================================
SMOOTHLY SCROLL INTERNAL LINKS ('ANCHORS'): (a href="#idofelement"):
* Copyright (c) 2009 - 2010 David Hund - http://valuedstandards.com
* Use it as you like: some link-love would be nice though :-)
==================================================================================
NOTES:
- Taken from: http://snipt.net/Jonic/jquery-smooth-scrolling-on-internal-links/
- Adapted to function as a jQuery Plugin
- Added 'easing' options
EASING OPTIONS (jQuery default: 'swing'):
To enable easing:
1) include jQuery Easing Plugin: http://gsgd.co.uk/sandbox/jquery/easing/
2) set 'easing' option to one of http://www.robertpenner.com/easing/easing_demo.html
E.g. easing: 'easeInOutBack'
================================================================================*/

if(typeof jQuery != 'undefined') {
(function($) {
jQuery.fn.scrollanchors = function(options) {
var defaults = {
speed: 900,
focusClass: false, // Set to classname, e.g. 'focused' and it will be added upon focus
easing: false
},
options = $.extend(defaults, options);

return this.each(function(){
// find all anchors in given element(s) with only an #target in the href
var allAnchors = $(this).find('a[href*=#]');
// Now hijack clicks on those links and SCROLL SMOOTHLY to their target
allAnchors.click(function(evnt) {
evnt.preventDefault();
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var $target = $(this.hash);
$target = $target.length && $target || $('[id=' + this.hash.slice(1) + ']');
if ($target.length) {
var targetOffset = $target.offset().top;
$('html,body').animate({scrollTop: targetOffset}, options.speed, options.easing);
if(options.focusClass){
// set options.focusClass on target
$target.addClass(options.focusClass);
}
}
}
});
});
};
})(jQuery);
}
1 change: 1 addition & 0 deletions snippets/jquery.smooth-scrolling-anchors.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8b1c6e1

Please sign in to comment.