Skip to content

Instantly share code, notes, and snippets.

@michallohnisky
Last active September 18, 2015 05:44
Show Gist options
  • Save michallohnisky/3fe1b54e265d7d6a829d to your computer and use it in GitHub Desktop.
Save michallohnisky/3fe1b54e265d7d6a829d to your computer and use it in GitHub Desktop.
JS funkce a komponenty
/**
* Executing a function after the user has stopped typing for a specified amount
* of time. Usage:
* $('input').keyup(function() {
* delay(function(){
* alert('Time elapsed!');
* }, 1000 );
* });
*/
var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
/**
* Převede číslo v sekundách do tvaru HH:MM:SS.
*/
function timeFormat(s) {
var sec_num = parseInt(s, 10);
var hours = Math.floor(sec_num / 3600);
var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
var seconds = sec_num - (hours * 3600) - (minutes * 60);
if (hours < 10) hours = "0"+hours;
if (minutes < 10) minutes = "0"+minutes;
if (seconds < 10) seconds = "0"+seconds;
var time = hours+':'+minutes+':'+seconds;
return time;
}
/**
* Naformatuje cislo jako menu. Je třeba vždy trochu přizpůsobit pro aplikaci.
* @param int
* @param object|undefined Defaultně window.currency .
* @param bool Zda se ma pridat symbol meny (napr.: 'Kc', '$', '€', ...). Default: true.
* @param bool Zda se ma mena prevadet podle kurzu. Default: true. // TODO: zatím zakomentováno
* @return string
*/
function priceFormat(price, cur, withSymbol, withRate)
{
if (withSymbol === undefined) {
withSymbol = true;
}
if (withRate === undefined) {
withRate = true;
}
if (cur === undefined) {
cur = window.app.currency
}
/*
if (withRate)
{
price = price / currency_getRate(cur);
}
*/
var nulls = 2;
if (price == Math.floor(price)) {
nulls = cur.nulls;
}
price = numberFormat(price, nulls, cur.dSep, cur.tSep);
if (withSymbol)
{
if (cur.position == 'after') {
price = price + cur.sSep + cur.symbol;
} else {
price = cur.symbol + cur.sSep + price;
}
}
return price;
}
/**
* Obdoba PHP number_format
*/
function numberFormat(number, decimals, dec_point, thousands_sep) {
// http://kevin.vanzonneveld.net
// Strip all characters but numerical ones.
number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
var n = !isFinite(+number) ? 0 : +number,
prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
s = '',
toFixedFix = function (n, prec) {
var k = Math.pow(10, prec);
return '' + Math.round(n * k) / k;
};
// Fix for IE parseFloat(0.55).toFixed(0) = 0;
s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
if (s[0].length > 3) {
s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
}
if ((s[1] || '').length < prec) {
s[1] = s[1] || '';
s[1] += new Array(prec - s[1].length + 1).join('0');
}
return s.join(dec);
}
(function(window, $, undefined){
/* constructor */
function FormReplicator(element, options) {
var self = this;
self.$el = element;
self.o = $.extend({}, self.defaults, options);
self.counter = self.$el.children().length;
self.updateButtonShow();
self.$el.on('click', self.o.deleteSelection, function(e){
self.removeRow(e, $(this));
});
if (self.o.addSelection) {
self.$el.on('click', self.o.addSelection, function(e){
self.addRow(e);
});
}
if (self.o.sensitiveSelection) {
self.$el.on('change keyup', self.o.sensitiveSelection, function(e){
// existuje již prázdný řádek?
var empty = true;
self.$el.children().last().find(self.o.sensitiveSelection).each(function(){
if ($(this).val() != '') {
empty = false;
return false;
}
});
if (empty) return;
self.addRow(e);
});
}
}
/* core prototype */
FormReplicator.prototype = {
updateButtonShow: function() {
var self = this;
var $add = self.$el.find(self.o.addSelection);
var $delete = self.$el.find(self.o.deleteSelection);
if (self.o.sensitiveSelection) {
$add.addClass(self.o.classHidden);
$delete.removeClass(self.o.classHidden);
$delete.last().addClass(self.o.classHidden);
} else {
$add.addClass(self.o.classHidden);
$add.last().removeClass(self.o.classHidden);
if (self.$el.children().length === 1) {
$delete.addClass(self.o.classHidden);
} else {
$delete.removeClass(self.o.classHidden);
}
}
},
addRow: function(e) {
var self = this;
var $oldRow = self.$el.children().first();
if (self.o.beforeClone) {
self.o.beforeClone.call(self, e, $oldRow);
}
var $newRow = $oldRow.clone();
var newRowCounter = self.counter;
self.$el.append($newRow);
self.updateButtonShow();
$newRow.find(':input').each(function(){
var $input = $(this);
var rules = Nette.parseJSON($input[0].getAttribute('data-nette-rules'));
for (var i in rules) {
var rule = rules[i];
if (rule.toggle !== undefined) {
for (var toggleId in rule.toggle) {
self.replaceElemAttr($newRow.find('[id='+ toggleId +']'), 'id', self.counter);
}
}
}
$.each([
'id', 'name'
], function(){
self.replaceElemAttr($input, this, self.counter);
});
var attrRules = $input.attr('data-nette-rules');
if (attrRules) {
attrRules.match(/"[^"]+"/g).forEach(function(string) {
var search = string.substring(1, string.length - 1);
var replace = self.replaceAttr(search, self.counter);
attrRules = attrRules.replace(search, replace);
});
$input.attr('data-nette-rules', attrRules);
}
if (self.o.inputClear === null || self.o.inputClear.call(self, e, $input, $newRow) !== false) {
if ($input.is('select')) {
$input.find(':selected').prop('selected', false);
} else if ($input.is('[type=submit]')) {
} else {
$input.val('');
}
}
});
$newRow.find('label').each(function(){
self.replaceElemAttr($(this), 'for', self.counter);
});
self.counter++;
self.toggleFormPart($newRow.find(':input'));
if (self.o.afterClone) {
self.o.afterClone.call(self, e, $newRow, $oldRow, newRowCounter);
}
},
replaceElemAttr: function($el, attrName, counter) {
var self = this;
var attrVal = $el.attr(attrName);
if (attrVal === undefined) return;
$el.attr(attrName, self.replaceAttr(attrVal, counter));
},
replaceAttr: function(string, counter) {
var self = this;
var idRegexp = '(?:'+ self.o.idPrefix +')?\\d+';
var regexp = new RegExp('(\\[)'+ idRegexp + '(\\])|(-)'+ idRegexp + '(-)', 'g');
var matchIndex = 0;
return string.replace(regexp, function(match, l1, r1, l2, r2, pos, original){
var out = match;
var l = l1 || l2;
var r = r1 || r2;
if (matchIndex === 0) { // first occurance
out = l + self.o.idPrefix + counter + r;
}
matchIndex++;
return out;
});
},
/* // last occurance
replaceAttr: function(string, counter) {
var self = this;
var idRegexp = '(?:'+ self.o.idPrefix +')?\\d+';
var regexp = new RegExp('(\\[)'+ idRegexp + '(\\])|(-)'+ idRegexp + '(-)', 'g');
var matchCount = string.match(regexp).length;
var matchIndex = 0;
return string.replace(regexp, function(match, l1, r1, l2, r2, pos, original){
var out = match;
var l = l1 || l2;
var r = r1 || r2;
if (matchIndex === matchCount-1) { // last occurance
out = l + self.o.idPrefix + counter + r;
}
matchIndex++;
return out;
});
},
*/
removeRow: function(e, $button) {
var self = this;
var $row = $button.closest(self.$el.children());
if(self.o.beforeDelete && self.o.beforeDelete.call(self, e, $row) === false) {
return;
}
$row.remove();
self.updateButtonShow();
self.counter--;
if(self.o.afterDelete) {
self.o.afterDelete.call(self, e, $row);
}
},
/**
* Process toggles in form on entered inputs.
*/
toggleFormPart: function($inputs) {
if (!Nette) return;
$inputs.each(function(){
var el = this;
if (el.tagName.toLowerCase() in {input: 1, select: 1, textarea: 1, button: 1}) {
Nette.toggleControl(el, null, null, true);
}
});
var i;
for (i in Nette.toggles) {
Nette.toggle(i, Nette.toggles[i]);
}
},
/* default option */
defaults: {
/**
* string
*/
deleteSelection: null,
/**
* string
*/
addSelection: null,
/**
* string
*/
sensitiveSelection: null,
/**
* Třída, která se použije pro skrývání elementů.
*/
classHidden: 'hidden',
/**
* Prefix před id, který se vyskytuje u nových položek.
*/
idPrefix: '_new_',
/**
* function (e, $oldRow)
*/
beforeClone: null,
/**
* function (e, $input, $newRow)
* Pokud vrátí false, defaultní mazání hodnot nebude provedeno.
*/
inputClear: null,
/**
* function (e, $newRow, $oldRow, newRowCounter)
* newRowCounter ... jaké číslo bylo použito pro tvorbu názvu $newRow.
*/
afterClone: null,
/**
* function (e, $row)
* Vrácením false lze zrušit smazání položky.
*/
beforeDelete: null,
/**
* function (e, $row)
*/
afterDelete: null,
}
};
$.fn.formReplicator = function(options) {
return this.each(function(){
var self = $(this);
if( !self.data('formReplicator') ) {
self.data('formReplicator', new FormReplicator(self, options));
}
});
};
})(window, jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment