From 7b757d3bf4c1537959055e724fbe9f305798f47a Mon Sep 17 00:00:00 2001 From: Luis Felipe Date: Sun, 19 Oct 2014 23:55:37 -0200 Subject: [PATCH 01/10] fixed identation --- jvfloat.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/jvfloat.js b/jvfloat.js index f36245b..03d25e2 100644 --- a/jvfloat.js +++ b/jvfloat.js @@ -3,30 +3,30 @@ * modified on: 18/09/2014 */ -(function($) { +(function ($) { 'use strict'; - + // Init Plugin Functions $.fn.jvFloat = function () { // Check input type - filter submit buttons. - return this.filter('input:not([type=submit]), textarea, select').each(function() { + return this.filter('input:not([type=submit]), textarea, select').each(function () { function getPlaceholderText($el) { var text = $el.attr('placeholder'); if (typeof text == 'undefined') { - text = $el.attr('title'); + text = $el.attr('title'); } return text; } - function setState () { + + function setState() { // change span.placeHolder to span.placeHolder.active var currentValue = $el.val(); if (currentValue == null) { currentValue = ''; - } - else if ($el.is('select')) { + } else if ($el.is('select')) { var placeholderValue = getPlaceholderText($el); if (placeholderValue == currentValue) { @@ -36,13 +36,15 @@ placeholder.toggleClass('active', currentValue !== ''); } - function generateUIDNotMoreThan1million () { + + function generateUIDNotMoreThan1million() { var id = ''; do { - id = ('0000' + (Math.random()*Math.pow(36,4) << 0).toString(36)).substr(-4); + id = ('0000' + (Math.random() * Math.pow(36, 4) << 0).toString(36)).substr(-4); } while (!!$('#' + id).length); return id; } + function createIdOnElement($el) { var id = generateUIDNotMoreThan1million(); $el.prop('id', id); @@ -51,11 +53,13 @@ // Wrap the input in div.jvFloat var $el = $(this).wrap('
'); var forId = $el.attr('id'); - if (!forId) { forId = createIdOnElement($el);} + if (!forId) { + forId = createIdOnElement($el); + } // Store the placeholder text in span.placeHolder // added `required` input detection and state var required = $el.attr('required') || ''; - + // adds a different class tag for text areas (.jvFloat .placeHolder.textarea) // to allow better positioning of the element for multiline text area inputs var placeholder = ''; @@ -71,5 +75,5 @@ $el.bind('keyup blur', setState); }); }; -// Make Zeptojs & jQuery Compatible + // Make Zeptojs & jQuery Compatible })(window.jQuery || window.Zepto || window.$); From 375eb5edc1a173af0c2a5ddaf91fa4755ac6e1ca Mon Sep 17 00:00:00 2001 From: Luis Felipe Date: Mon, 20 Oct 2014 00:01:04 -0200 Subject: [PATCH 02/10] given names to functions for better debug --- jvfloat.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/jvfloat.js b/jvfloat.js index 03d25e2..5991eac 100644 --- a/jvfloat.js +++ b/jvfloat.js @@ -3,13 +3,13 @@ * modified on: 18/09/2014 */ -(function ($) { +(function _jv_float_module($) { 'use strict'; // Init Plugin Functions - $.fn.jvFloat = function () { + $.fn.jvFloat = function _jv_float_plugin() { // Check input type - filter submit buttons. - return this.filter('input:not([type=submit]), textarea, select').each(function () { + return this.filter('input:not([type=submit]), textarea, select').each(function _jv_float_each() { function getPlaceholderText($el) { var text = $el.attr('placeholder'); From 8c2e124e7e8a7c5dee1a29607d452d5dfa524523 Mon Sep 17 00:00:00 2001 From: Luis Felipe Date: Mon, 20 Oct 2014 00:12:50 -0200 Subject: [PATCH 03/10] generate a global ID and make less access to DOM [!!$(#...)] ; --- jvfloat.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/jvfloat.js b/jvfloat.js index 5991eac..0347de7 100644 --- a/jvfloat.js +++ b/jvfloat.js @@ -3,9 +3,11 @@ * modified on: 18/09/2014 */ -(function _jv_float_module($) { +(function _jv_float_module($, window) { 'use strict'; + var namespace = 'jv_float'; + // Init Plugin Functions $.fn.jvFloat = function _jv_float_plugin() { // Check input type - filter submit buttons. @@ -37,16 +39,20 @@ placeholder.toggleClass('active', currentValue !== ''); } - function generateUIDNotMoreThan1million() { - var id = ''; - do { - id = ('0000' + (Math.random() * Math.pow(36, 4) << 0).toString(36)).substr(-4); - } while (!!$('#' + id).length); - return id; + /** + * Generate a unique id + * @return {string} id generated + */ + function generate_id() { + // if window don't have UUID, create with 1, else append one each time + !('JVFLOAT_UUID' in window) ? window.JVFLOAT_UUID = 1 : window.JVFLOAT_UUID += 1; + + return [namespace, window.JVFLOAT_UUID].join('__'); + } function createIdOnElement($el) { - var id = generateUIDNotMoreThan1million(); + var id = generate_id(); $el.prop('id', id); return id; } @@ -76,4 +82,4 @@ }); }; // Make Zeptojs & jQuery Compatible -})(window.jQuery || window.Zepto || window.$); +})(window.jQuery || window.Zepto || window.$, window); From 6ac029a535267fffc191a43a565f0a37d13c572a Mon Sep 17 00:00:00 2001 From: Luis Felipe Date: Mon, 20 Oct 2014 00:14:19 -0200 Subject: [PATCH 04/10] 'window.jquery' and 'window.$' is same function --- jvfloat.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jvfloat.js b/jvfloat.js index 0347de7..7194c42 100644 --- a/jvfloat.js +++ b/jvfloat.js @@ -82,4 +82,4 @@ }); }; // Make Zeptojs & jQuery Compatible -})(window.jQuery || window.Zepto || window.$, window); +})(window.jQuery || window.Zepto, window); From 22b7a2305931060efc06329dc192f63afded1675 Mon Sep 17 00:00:00 2001 From: Luis Felipe Date: Mon, 20 Oct 2014 02:06:36 -0200 Subject: [PATCH 05/10] removed confused variables names, like placeholder to describe a label. Created placeholder by native attributes (it's more fast). Organized variables at top --- jvfloat.js | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/jvfloat.js b/jvfloat.js index 7194c42..a4eedde 100644 --- a/jvfloat.js +++ b/jvfloat.js @@ -12,15 +12,20 @@ $.fn.jvFloat = function _jv_float_plugin() { // Check input type - filter submit buttons. return this.filter('input:not([type=submit]), textarea, select').each(function _jv_float_each() { - function getPlaceholderText($el) { - var text = $el.attr('placeholder'); + var $el = $(this), + el = $el[0], // native element + placeholder = el.getAttribute("placeholder") || el.getAttribute("title"), // get placeholder with native function + forId = $el.attr('id'), - if (typeof text == 'undefined') { - text = $el.attr('title'); - } + // added `required` input detection and state + required = $el.attr('required') || ''; - return text; - } + // Wrap the input in div.jvFloat + $el.wrap('
'); + + // adds a different class tag for text areas (.jvFloat .placeHolder.textarea) + // to allow better positioning of the element for multiline text area inputs + var $label = ''; function setState() { // change span.placeHolder to span.placeHolder.active @@ -29,14 +34,12 @@ if (currentValue == null) { currentValue = ''; } else if ($el.is('select')) { - var placeholderValue = getPlaceholderText($el); - - if (placeholderValue == currentValue) { + if (placeholder == currentValue) { currentValue = ''; } } - placeholder.toggleClass('active', currentValue !== ''); + $label.toggleClass('active', currentValue !== ''); } /** @@ -56,25 +59,14 @@ $el.prop('id', id); return id; } - // Wrap the input in div.jvFloat - var $el = $(this).wrap('
'); - var forId = $el.attr('id'); if (!forId) { forId = createIdOnElement($el); } - // Store the placeholder text in span.placeHolder - // added `required` input detection and state - var required = $el.attr('required') || ''; - - // adds a different class tag for text areas (.jvFloat .placeHolder.textarea) - // to allow better positioning of the element for multiline text area inputs - var placeholder = ''; - var placeholderText = getPlaceholderText($el); if ($(this).is('textarea')) { - placeholder = $('').insertBefore($el); + $label = $('').insertBefore($el); } else { - placeholder = $('').insertBefore($el); + $label = $('').insertBefore($el); } // checks to see if inputs are pre-populated and adds active to span.placeholder setState(); From e1a51e7f45380023a430c8dc49804aef687dcd30 Mon Sep 17 00:00:00 2001 From: Luis Felipe Date: Mon, 20 Oct 2014 02:13:34 -0200 Subject: [PATCH 06/10] change "$label" to use jQuery creation method --- jvfloat.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/jvfloat.js b/jvfloat.js index a4eedde..7564eaa 100644 --- a/jvfloat.js +++ b/jvfloat.js @@ -25,7 +25,7 @@ // adds a different class tag for text areas (.jvFloat .placeHolder.textarea) // to allow better positioning of the element for multiline text area inputs - var $label = ''; + var $label = null; function setState() { // change span.placeHolder to span.placeHolder.active @@ -59,15 +59,18 @@ $el.prop('id', id); return id; } - if (!forId) { - forId = createIdOnElement($el); + if (!el.getAttribute('id')) { + el.setAttribute('id', createIdOnElement($el)); } - if ($(this).is('textarea')) { - $label = $('').insertBefore($el); - } else { - $label = $('').insertBefore($el); - } + $label = $('