/ Gists

Gists

On gists

Selector Caching in jQuery

jQuery

Selector Caching in jQuery.js #

function SelectorCache() {
    var collection = {};
    function getFromCache(selector) {
        if (undefined === collection[selector]) {
            collection[selector] = $(selector);
        }
        return collection[selector];
    }
    return {
        get: getFromCache
    };
}
 
var selectors = new SelectorCache();
 
// Usage $('#element') becomes:
selectors.get('#element');

On gists

jQuery document ready

jQuery

document-ready.js #

// =======================================================================
//             Document Ready Example 1
// =======================================================================
 
$(document).ready(function () {
  // do jQuery stuff when DOM is ready
});
 
// =======================================================================
//             Document Ready Example 2
// =======================================================================
 
$(function() {
  // jQuery code here
  // This is equivalent to example 1 they literally mean the same thing.
});
 
// =======================================================================
//             Document Ready Example 3
// =======================================================================
 
jQuery(document).ready(function ($) {
  // do jQuery stuff when DOM is ready
});
 
// Adding the jQuery can help prevent conflicts with other JS frameworks.
// 
// Why do conflicts happen?
// Conflicts typically happen because many JavaScript Libraries/Frameworks use the same shortcut
// name which is the dollar symbol $. Then if they have the same named functions the browser gets
// confused!
// 
// How do we prevent conflicts?
// Well, to prevent conflicts i recommend aliasing the jQuery namespace (ie by using example 3 above).
// Then when you call $.noConflict() to avoid namespace difficulties (as the $ shortcut is no longer available)
// we are forcing it to wrtie jQuery each time it is required.
 
jQuery.noConflict(); // Reverts '$' variable back to other JS libraries
jQuery(document).ready(function () {
  // do jQuery stuff when DOM is ready with no conflicts
});
 
// =======================================================================
//               Document Ready Example 4
// =======================================================================
 
$(window).load(function () {
  //initialize after images are loaded
});
 
// Sometimes you want to manipulate pictures and with $(document).ready() you won't be able to do that
// if the visitor doesn't have the image already loaded. In which case you need to initialize the
// jQuery alignment function when the image finishes loading.

On gists

jQuery form select helpers

jQuery Helpers-Filters-Plugins jQuery-plugins

jQuery form select helpers.js #

jQuery.fn.containsOption = function (query) {
  var found = false;
 
  this.each(function () {
    if (this.nodeName.toLowerCase() == 'select') {
      for (var i = 0; i < this.options.length; i++) {
        if (query.value) {
          found = (query.value.constructor == RegExp) ? this.options[i].value.match(query.value) : this.options[i].value == query.value;
        } else if (query.text) {
          found = (query.text.constructor == RegExp) ? this.options[i].text.match(query.text) : this.options[i].text == query.text;
        }
        if (found) break;
      }
    } else {
      return this;
    }
  });
 
  return found;
};
 
jQuery.fn.addOption = function (o) {
  var opt = o;
 
  this.each(function () {
    if (this.nodeName.toLowerCase() == 'select') {
      var option = document.createElement('OPTION');
      option.value = opt.value;
      option.text = opt.text;
 
      if (opt.selected) option.selected = opt.selected;
 
      this.options[this.options.length] = option;
    } else return this;
  });
 
  return this;
};
 
jQuery.fn.clearOptions = function () {
  this.each(function () {
    if (this.nodeName.toLowerCase() == 'select') {
      this.options.length = 0;
    }
  });
};
 
jQuery.fn.removeOptionByValue = function (val) {
  this.each(function () {
    if (this.nodeName.toLowerCase() == 'select') {
      for (var i = 0; i < this.options.length; i++) {
        if (this.options[i].value == val) {
          this.options[i] = null;
        }
      }
    } else {
      return this;
    }
  });
  return this;
};
 
jQuery.fn.removeOptionByText = function (txt) {
  this.each(function () {
    if (this.nodeName.toLowerCase() == 'select') {
      for (var i = 0; i < this.options.length; i++) {
        if (this.options[i].text == txt) {
          this.options[i] = null;
        }
      }
    } else {
      return this;
    }
  });
  return this;
};
 
jQuery.fn.selectOptionByValue = function (val) {
  this.each(function () {
    if (this.nodeName.toLowerCase() == 'select') {
      for (var i = 0; i < this.options.length; i++) {
        if (this.options[i].value == val) {
          this.options[i].selected = true;
        } else {
          this.options[i].selected = false;
        }
      }
    } else return this;
  });
  return this;
};
 
jQuery.fn.selectOptionByText = function (txt) {
  this.each(function () {
    if (this.nodeName.toLowerCase() == 'select') {
      for (var i = 0; i < this.options.length; i++) {
        if (this.options[i].text == txt) {
          this.options[i].selected = true;
        } else {
          this.options[i].selected = false;
        }
      }
    } else return this;
  });
  return this;
};
 
// USAGE
$('select#languages').containsOption({
  text: 'Text'
});
 
$('select#languages').containsOption({
  value: '19'
});
 
$('select#languages').selectOptionByValue('19');
$('select#languages').selectOptionByText('Apache');
 
$('select#languages').addOption({
  'text': 'rubyonrails',
  'value': '100'
});
 
$('select#languages').removeOptionByValue('19');
$('select#languages').removeOptionByText('Apache');
 
$('select#languages').clearOptions(); // deletes all options

On gists

form in popup

JavaScript jQuery

form-in-poup.js #

$(document).ready(function() {
  $('#myform').submit(function() {
    window.open('', 'formpopup', 'width=400,height=400,resizeable,scrollbars');
    this.target = 'formpopup';
  });
});

On gists

ArrayToObject / ObjectToArray

PHP Helpers-Filters-Plugins

object-to-array-vice-versa.php #

<?php

function objectToArray($d) {
		if (is_object($d)) {
			// Gets the properties of the given object
			// with get_object_vars function
			$d = get_object_vars($d);
		}
 
		if (is_array($d)) {
			/*
			* Return array converted to object
			* Using __FUNCTION__ (Magic constant)
			* for recursive call
			*/
			return array_map(__FUNCTION__, $d);
		}
		else {
			// Return array
			return $d;
		}
	}
 
 
 
 	function arrayToObject($d) {
		if (is_array($d)) {
			/*
			* Return array converted to object
			* Using __FUNCTION__ (Magic constant)
			* for recursive call
			*/
			return (object) array_map(__FUNCTION__, $d);
		}
		else {
			// Return object
			return $d;
		}
	}
 

On gists

JS - remove diacritics fn

JavaScript Helpers-Filters-Plugins

make-url.js #

			function makeUrl(s)
			{

				var nodiac = { 'á': 'a', 'č': 'c', 'ď': 'd', 'é': 'e', 'ě': 'e', 'í': 'i', 'ň': 'n', 'ó': 'o', 'ř': 'r', 'š': 's', 'ť': 't', 'ú': 'u', 'ů': 'u', 'ý': 'y', 'ž': 'z' };
				s = s.toLowerCase();
			    var s2 = '';
			    for (var i=0; i < s.length; i++) {
			        s2 += (typeof nodiac[s.charAt(i)] != 'undefined' ? nodiac[s.charAt(i)] : s.charAt(i));
			    }

			    return s2;
			    //return s2.replace(/[^a-z0-9_]+/g, '-').replace(/^-|-$/g, '');
			}

On gists

Sql - nalezeni duplicitnich radku (více sloupců)

MySql

sql-duplicity-rows.sql #

SELECT DISTINCT t1.id
FROM tabulka t1
JOIN tabulka t2 ON t2.sloupec1 = t1.sloupec1
AND t2.sloupec2 = t1.sloupec2
AND t2.id < t1.id

On gists

Nette form - filter

Nette Nette-Forms Helpers-Filters-Plugins

nette-filter.php #

<?php

$form->addText('zip', 'PSČ:')
    ->addCondition($form::FILLED)
    ->addFilter(function ($value) {
        return str_replace(' ', '', $value);
    });

On gists

Nette - flash messages

Nette

nette-flash-message.php #

<?php
$message = $this->flashMessage('Uspesne prihlaseni', 'success');
$message->id = 'success';

On gists

Nette form - setOption

Nette

Nette-form.php #

// Použití description do automaticky generovaného formuláře pro přípichnutí HTML či čehokoliv z formuláře 
<?php

// da se pouziti i Nette\Utils\Html::el(); 
$form['upload']->setOption('description', 'nazev souboru');