/ Gists / Helpers-Filters-Plugins

Gists - Helpers-Filters-Plugins

On gists

JS plugin aka jQuery

JavaScript Plugin patterns Helpers-Filters-Plugins

Test.plugin.js #

class Test {
    constructor(el, options) {
        this.el = el;
        const defaultOptions = {
            onHover: (element, e) => {
                console.log(':)))', element, e);
            },
        };

        this.options = { ...defaultOptions, ...options };
        this.mouseOverHandler = this.handleMouseOver.bind(this); // Vytvoření odkazu na metodu handleMouseOver
    }

    init() {
        this.el.addEventListener('mouseover', this.mouseOverHandler); // Přidání posluchače události
    }

    destroy() {
        this.el.removeEventListener('mouseover', this.mouseOverHandler); // Odstranění posluchače události
    }

    handleMouseOver(e) {
        if (this.options.onHover) {
            this.options.onHover(this.el, e);
        }
    }
}

On gists

Is element in viewport?

JavaScript Helpers-Filters-Plugins

is-in-viewport.js #

/**
 * Check if DOM element is in browsers view port
 * @param el The element
 * @return boolean
 */
function isInViewport(el) {
    var rect = el.getBoundingClientRect();

    return (
        rect.bottom >= 0 &&
        rect.right >= 0 &&

        rect.top <= (
            window.innerHeight ||
            document.documentElement.clientHeight) &&

        rect.left <= (
            window.innerWidth ||
            document.documentElement.clientWidth)
    );
}

On gists

Anonymous self invoke functions

PHP Helpers-Filters-Plugins

anonymous.php #

<?php

call_user_func(function() { 
  
  // your code here ...

});

On gists

Check datum is in range

PHP Helpers-Filters-Plugins

in-range.php #

<?php
    public function checkDatum(\DateTime $date)
    {
        $startDate = new \DateTime('2018-12-17 00:00:00');
        $endDate   = new \DateTime('2018-12-23 23:59:59');
        
        return $date > $startDate && $date < $endDate;
     
    }

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

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

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

CURL - functions

PHP Helpers-Filters-Plugins

CURL.php #

function curl_file_get_contents($url) 
{	
	 $c = curl_init(); // iniciuje práci s curl
	 curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
	 curl_setopt($c, CURLOPT_URL, $url);
	 $contents = curl_exec($c);
	 curl_close($c); 
 
	 return $contents;
 }


function better_curl($url)
{
	
  $curl = curl_init();
 
  $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
  $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
  $header[] = "Cache-Control: max-age=0";
  $header[] = "Connection: keep-alive";
  $header[] = "Keep-Alive: 300";
  $header[] = "Accept-Charset: ISO-8859-1,utf-8,utf-16;q=0.7,*;q=0.7";
  $header[] = "Accept-Language: en-us,en;q=0.5";
  $header[] = "Pragma: "; // browsers keep this blank.
 
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.1) Gecko/20061223 Firefox/2.0.0.1');
//  curl_setopt($curl, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)');
  curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
//  curl_setopt($curl, CURLOPT_REFERER, 'http://www.google.com');
  curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
  curl_setopt($curl, CURLOPT_AUTOREFERER, true);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_TIMEOUT, 10);
 
  $html = curl_exec($curl); // execute the curl command
  curl_close($curl); // close the connection
 
  return $html; // and finally, return $html
}

On gists

Nette - expand

Nette Nette-Tricks Helpers-Filters-Plugins

nette-expand.php #

<?php
$pathToFile = $this->context->expand("%wwwDir%/files/soubor.pdf");