Skip to content

Instantly share code, notes, and snippets.

@meglio
meglio / highlight-numerical.js
Created September 13, 2012 18:37
Highlights numerical string in numerical string, while both strings are free formatted and containing more then just digits
function highlightNumerical(searchString, inString) {
// http://jsfiddle.net/meglio/p9KUV/
if (typeof searchString == "object") {
if (searchString.length)
searchString = searchString[0]
else
return inString
}
var prefix = '<strong class="highlight">', suffix = '</strong>',
@meglio
meglio / ajax-items.js
Created September 16, 2012 22:13
Controller of elements usually obtained by ajax
function AjaxItems($container, config) {
var defaultOpts = {
textNoItems: "Не знайдено",
removalActionClass: "actionRemove",
removalDialog: function(el) { return confirm('Видалити?') },
showNoItems: function($container, message) {
$container.empty().append( $div().text(message) )
},
// If you want item removal to be handled, add an element with class .actionRemove (or another specified in removalActionClass option)
itemDom: function(el) {
@meglio
meglio / EditableDomElement.js
Created September 16, 2012 22:14
Inline-editable dom elements
function EditableDomElements($container, selector, config) {
var defaultOpts = {
textCancel: "Відміна",
textSave: "Зберегти",
textNoText: null,
inputType: 'textarea', // textarea|input
extractTextFromView: function($view) { return $view.text().trim() },
setTextToView: function($view, text) { $view.empty().text(text) },
configureTextarea: function($editor, $view) { $editor.height($view.outerHeight()) },
placeEditor: function($editor, $view) { $editor.insertAfter($view) },
@meglio
meglio / css.php
Created September 17, 2012 15:08
css.php joins and minifies css files from html template into cached.css and controls Cache-Control headers based on mtime of all files engaged
<?php
header('Content-Type: text/css');
$cacheFile = __DIR__.'/css/cached.css';
$listFile = __DIR__.'/css/cached-list.txt';
$htmlFile = __DIR__.'/tmpl/web/html5.twig';
$htmlTime = filemtime($htmlFile);
$cacheTime = file_exists($cacheFile)? filemtime($cacheFile) : 0;
@meglio
meglio / TimebasedUrl.php
Created September 19, 2012 10:39
Generates file.ddd.js/css filenames based on their mtime
<?php
namespace proj\utils;
class TimebasedUrl
{
static function js($path)
{
return '<script type="text/javascript" src="'.self::url($path).'"></script>';
}
@meglio
meglio / js.php
Created September 19, 2012 11:41
js.php Joins and minifies js files from html template into cached.js and controls Cache-Control headers based on mtime of all files engaged
<?php
header('Content-Type: text/javascript');
$cacheFile = __DIR__.'/js/cached.js';
$listFile = __DIR__.'/js/cached-list.txt';
$htmlFile = __DIR__.'/tmpl/web/html5.twig';
$htmlTime = filemtime($htmlFile);
$cacheTime = file_exists($cacheFile)? filemtime($cacheFile) : 0;
@meglio
meglio / support.pdf.js
Created October 27, 2012 10:10
JQuery PDF support detection
/*
* Check for PDF support
* Based on http://downloads.beninzambia.com/blog/acrobat_detection.js.txt
*/
$.support.pdf = (function() {
try {
// IE
if(!$.support.cssFloat) {
var control = null;
@meglio
meglio / aes256cbc.php
Created October 27, 2012 17:12
Enc/Dec AES 256 CBC, with data consistency validation
private static function iv()
{
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
return mcrypt_create_iv($iv_size, MCRYPT_RAND);
}
static function encrypt($str, $key32)
{
# Prepend 4-chars data hash to the data itself for validation after decryption
$str = substr(md5($str), 0, 4).$str;
@meglio
meglio / OpenCorpora.scala
Created November 28, 2012 11:22
Parse opencorpora dictionary
package com.frazemet
import scala.io.Source
import xml.pull._
import collection.mutable.ListBuffer
object OpenCorpora {
def importFromXml(file: String) {
@meglio
meglio / highlight.js
Created December 3, 2012 14:38
highligh(string, [words]) JS function
// Returns { highlighted: ..., count: ... }
function highlight(str, words)
{
var before = '<strong class="highlight">', beforeLen = before.length,
after = '</strong>', afterLen = after.length
if (typeof words == "string")
words = [words]