/ Gists / Helpers-Filters-Plugins

Gists - Helpers-Filters-Plugins

On gists

Vytváření elementů v jQuery

jQuery Helpers-Filters-Plugins

new-element.js #

// <a style="color: red;" onclick="..." href="https://phpfashion.com">blogísek</a>
var $el = $('<a>', {
    href: url,
    text: title,
    click: function(e) {
        alert(this.href);
    },
    css: {
        color: 'red'
    }
});



// <a href="https://phpfashion.com"><img alt="Logo" src="images/logo.gif"></a>
var $el = $('<a>', {
    href: url,
    html: $('<img>', {
        src: 'images/logo.gif',
        alt: 'Logo'
    })
});

On gists

JS Caret

JavaScript Helpers-Filters-Plugins

caret.js #


jQuery.fn.extend({
insertAtCaret: function(myValue){
  return this.each(function(i) {
    if (document.selection) {
      //For browsers like Internet Explorer
      this.focus();
      sel = document.selection.createRange();
      sel.text = myValue;
      this.focus();
    }
    else if (this.selectionStart || this.selectionStart == '0') {
      //For browsers like Firefox and Webkit based
      var startPos = this.selectionStart;
      var endPos = this.selectionEnd;
      var scrollTop = this.scrollTop;
      this.value = this.value.substring(0, startPos)+myValue+this.value.substring(endPos,this.value.length);
      this.focus();
      this.selectionStart = startPos + myValue.length;
      this.selectionEnd = startPos + myValue.length;
      this.scrollTop = scrollTop;
    } else {
      this.value += myValue;
      this.focus();
    }
  })
}
});

On gists

Recursive dir delete

PHP Helpers-Filters-Plugins

recursive-delete-dir.php #

<?php

 function rrmdir($dir) { 
   if (is_dir($dir)) { 
     $objects = scandir($dir); 
     foreach ($objects as $object) { 
       if ($object != "." && $object != "..") { 
         if (is_dir($dir."/".$object))
           rrmdir($dir."/".$object);
         else
           unlink($dir."/".$object); 
       } 
     }
     rmdir($dir); 
   } 
 }

On gists

jQuery exists fn

jQuery Helpers-Filters-Plugins

jquery-exists-fn.js #

// Tiny jQuery Plugin
// by Chris Goodchild
$.fn.exists = function(callback) {
  var args = [].slice.call(arguments, 1);

  if (this.length) {
    callback.call(this, args);
  }

  return this;
};

// Usage
$('div.test').exists(function() {
  this.append('<p>I exist!</p>');
});

On gists

Wrap image with caption

jQuery Helpers-Filters-Plugins jQuery-plugins

image-with-caption.js #

	$(".img-left, .img-right, .article-detail img", this).each(function() 
	{
	    var image = $(this);
	    var imageCaption = image.attr("alt");
	    if (imageCaption != '') 
	    {
	    	image.wrap('<div class="image-w-caption">');
	    	image.parent('.image-w-caption')
	    	     .css('max-width', image.width())
	    	     .addClass(image.attr('class'))
	    	     .append('<div class="caption">'+imageCaption+'</div>');
	    }
	});

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

Very simple autosave functionality using local storage

JavaScript Helpers-Filters-Plugins jQuery-plugins

test.html #

<html>
    <body>    
        <textarea rows=24 cols=80></textarea>
        <script type="text/javascript" src="autosave.js"></script>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.1.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                AutoSave.start();
            })

            $(window).unload(function(){
                AutoSave.stop();
            });
        </script>
    </body>
</html>

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

On responsive no hover etc

JavaScript Helpers-Filters-Plugins

on-responsive-no-hover.js #


      if ('createTouch' in document)
      {
          try
          {
              var ignore = /:hover/;
              for (var i=0; i<document.styleSheets.length; i++)
              {
                  var sheet = document.styleSheets[i];
                  for (var j=sheet.cssRules.length-1; j>=0; j--)
                  {
                      var rule = sheet.cssRules[j];
                      if (rule.type === CSSRule.STYLE_RULE && ignore.test(rule.selectorText))
                      {
                          sheet.deleteRule(j);
                      }
                  }
              }
          }
          catch(e){}
      }

On gists

Wrap text into paragraphs

PHP Helpers-Filters-Plugins

text-in-para.php #

<?php


$text = <<<TEXT
Morbi nisl tortor, consectetur vitae laoreet eu, lobortis id ipsum. Integer scelerisque blandit pulvinar. Nam tempus mi eget nunc laoreet venenatis. Proin viverra, erat at accumsan tincidunt, ante mi cursus elit, non

congue mauris dolor ac elit. Maecenas mollis nisl a sem semper ornare. Integer nunc purus, dapibus nec dignissim sed, dictum eget leo. Etiam in mi ut erat pretium fringilla sed
TEXT;

$paragraphedText = "<p>" . implode( "</p>\n\n<p>", preg_split( '/\n(?:\s*\n)+/', $text ) ) . "</p>";


echo $paragraphedText;