/ Gists / jQuery

Gists - jQuery

On gists

jQuery - keyup with delay (timeout)

jQuery Helpers-Filters-Plugins jQuery-plugins

keyup-delay1.js #

var delay = (function(){
  var timer = 0;
  return function(callback, ms){
    clearTimeout (timer);
    timer = setTimeout(callback, ms);
  };
})();


$('input').keyup(function() {
    delay(function(){
      alert('Time elapsed!');
    }, 1000 );
});

On gists

MathUtils

jQuery Helpers-Filters-Plugins jQuery-plugins

mathutils.jquery.js #

(function($) {
  $.mathUtils = {
    sum: function(array) {
      var total = 0;

      $.each(array, function(index, value) {
        total += $.mathUtils.parseCzechFloat(value);
      });
      return total;
    },
    parseCzechFloat: function(string) {
      var value = $.trim(string);
      return parseFloat(value.replace(' ', '').replace(',', '.')) || 0;
    },
    formatCzechFloat: function(number) {
      return String(number).replace('.', ',');
    },
    average: function(array) {
      if ($.isArray(array)) {
        return $.mathUtils.sum(array) / array.length;
      }
      return '';
    }
  };
})(jQuery);

On gists

Modal object with private functions

jQuery jQuery-plugins

modal.js #

    function feedbackModalFn(selector){

        //var feedbackModal = $('.feedback-modal');
        var feedbackModal = $(selector);
        var winH;
        var topPane;
        var feedbackModalHeight;

        // private scope
        function init()
        {
            winH                = $(window).height();
            //topPane             = $('.top-pane').height();
            topPane             = 0;
            feedbackModalHeight = winH - topPane;

            feedbackModal.height(feedbackModalHeight);
        
        }

        // private scope
        function show()
        {
            init();

            feedbackModal.animate({
                bottom: 0
            }, 600);
        }

        // private scope
        function hide()
        {

            init();

            feedbackModal.animate({
                bottom: "-" + feedbackModalHeight
            }, 600);
        }


        return {

            'init': init,
            'show': show,
            'hide': hide
        };

    }

On gists

Jquery - řazení tabulky - tablesorter demo - https://jsfiddle.net/bmfz1pau/

jQuery Helpers-Filters-Plugins jQuery-plugins

tablesortersimple.js #

$(document).ready(function() {
  $('th').each(function(col) {
    $(this).hover(
      function() {
        $(this).addClass('focus');
      },
      function() {
        $(this).removeClass('focus');
      }
    );


    $(this).click(function() {
      if ($(this).is('.asc')) {
        $(this).removeClass('asc');
        $(this).addClass('desc selected');
        sortOrder = -1;
      } else {
        $(this).addClass('asc selected');
        $(this).removeClass('desc');
        sortOrder = 1;
      }
      $(this).siblings().removeClass('asc selected');
      $(this).siblings().removeClass('desc selected');
      var arrData = $('table').find('tbody >tr:has(td)').get();

      arrData.sort(function(a, b) {
        var val1 = $(a).children('td').eq(col).text().toUpperCase();
        var val2 = $(b).children('td').eq(col).text().toUpperCase();
        if ($.isNumeric(val1) && $.isNumeric(val2))
          return sortOrder == 1 ? val1 - val2 : val2 - val1;
        else
          return (val1 < val2) ? -sortOrder : (val1 > val2) ? sortOrder : 0;
      });
      $.each(arrData, function(index, row) {
        $('tbody').append(row);
      });
    });
  });
});

On gists

jQuery Tiny Pub/Sub: A really, really, REALLY tiny pub/sub implementation for jQuery.

jQuery jQuery-plugins

jquery.ba-tinypubsub.min.js #

/* jQuery Tiny Pub/Sub - v0.7 - 10/27/2011
 * http://benalman.com/
 * Copyright (c) 2011 "Cowboy" Ben Alman; Licensed MIT, GPL */
(function(a){var b=a({});a.subscribe=function(){b.on.apply(b,arguments)},a.unsubscribe=function(){b.off.apply(b,arguments)},a.publish=function(){b.trigger.apply(b,arguments)}})(jQuery)

On gists

From https://www.sitepoint.com/introduction-jquery-deferred-objects/

jQuery jQuery-plugins

deferred-timeout.js #

function timeout(milliseconds) {
  // Create a new Deferred object
  var deferred = $.Deferred();

  // Resolve the Deferred after the amount of time specified by milliseconds
  setTimeout(deferred.resolve, milliseconds);

  // Return the Deferred's Promise object
  return deferred.promise();
}

timeout(1000).then(function() {
  console.log('I waited for 1 second!');
});

On gists

jQuery e-mail plugin validation

jQuery jQuery-plugins

jquery-email-validation.js #

(function ($) {

    $.fn.validateEmail = function () {
        return this.each(function () {
            var $this = $(this);
            $this.change(function () {
                var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
                if ($this.val() == "") {
                    $this.removeClass("badEmail").removeClass("goodEmail")
                }else if(reg.test($this.val()) == false) {
                    $this.removeClass("goodEmail");
                    $this.addClass("badEmail");
                }else{
                    $this.removeClass("badEmail");
                    $this.addClass("goodEmail");
                }
            });
        });
    };
})(jQuery);

On gists

jQuery - own methods

jQuery jQuery-plugins

jquery-own-methods.js #

$(function(){
	jQuery.fn.putBefore = function(dest){
		return this.each(function(){
			$(dest).before($(this));
		});
	}
	jQuery.fn.putAfter = function(dest){
		return this.each(function(){
			$(dest).after($(this));
		});
	}
});

On gists

jQuery - remove widows

jQuery Helpers-Filters-Plugins

jquery-widow.js #

$(function(){
	//Loop through each title
	$("h3").each(function(){
		var content = $(this).text().split(" ");
		var widow = "&amp;nbsp;"+content.pop();
		$(this).html(content.join(" ")+widow);
	});
});

On gists

jQuery - reverse each

jQuery Helpers-Filters-Plugins

reverse-each.js #

$(function(){
	var reversedSet = $("li").get().reverse();
	//Use get() to return an array of elements, and then reverse it
 
	$(reversedSet).each(function(){
		//Now we can plug our reversed set right into the each function. Could it be easier?
	});
});