(function () {
'use strict';
var selector = {
$button: document.querySelector('a'),
$one: document.querySelector('.one'),
$two: document.querySelector('.two')
};
selector.$button.addEventListener('click', function (event) {
event.preventDefault();
if (isHidden(selector.$one)) {
changeDisplay(selector.$one, 'block');
changeDisplay(selector.$two, 'none');
} else {
changeDisplay(selector.$one, 'none');
changeDisplay(selector.$two, 'block');
}
});
function changeDisplay($node, value) {
$node.style.display = value;
return $node;
}
function isHidden($node) {
return window.getComputedStyle($node).display === 'none';
}
}());
(function ($) {
'use strict';
$(document).on('ajaxComplete', function (event, jqXHR, params) {
if (params && params.url) {
jqXHR.done(function (data) {
console.log({
url: params.url,
response: data
});
});
}
});
}(jQuery));
<script type="text/javascript">
;(function ($) {
'use strict';
$(document).on('load', function () {
window.slider = new IdealImageSlider.Slider('#slider_news');
window.slider.addBulletNav();
window.slider.addCaptions();
});
}(jQuery));
</script>
<a href="javascript:void(0);" id="1"></a>
<div id="1">Info 1 </div>
var test = {
foo: {
bar: {
test: {
prop: 'someProp123'
}
}
}
};
objectPath(test, 'foo.bar.test.prop'); //> "someProp123"
objectPath(test, 'foo.bar.test'); //> { prop: "someProp123" }
function objectPath(object, key, value) {
var path = key.split('.');
var get = function (collection, start) {
return collection.reduce(function (previous, current) {
return previous[current];
}, start);
};
if (value) {
var way = path.pop();
get(path, object)[way] = value;
return object;
} else {
return get(path, object);
}
}
$(selector).on('click touch', function () { ... });
numericFormat(2562343651.5263); //> "2 562 343 651.5263"
function numericFormat(value, decimal, thousand) {
if (!decimal) decimal = ' ';
if (!thousand) thousand = '.';
var parts = value.toString().split('.');
return parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, decimal) + (parts[1] ? thousand + parts[1] : '');
}
for (var i = 0; i < 10; i++) {
(function (index) {
setTimeout(function () {
alert(index);
}, 100);
}(i));
}
var text = "SELECT * FROM [driving license] WHERE name_driver={{user.name}}";
templater(text, {
user: {
name: 'Олег'
}
});
//> "SELECT * FROM [driving license] WHERE name_driver=Олег"
var text = "SELECT * FROM [driving license] WHERE name_driver={{name}}";
templater(text, {
name: 'Олег'
});
//> "SELECT * FROM [driving license] WHERE name_driver=Олег"
/**
* Обработка тегов, шаблонизатор
* @param {String} Входные данные
* @param {Object} Объект {тег - значение}
* @returns {String} Выходные, обработаные данные
**/
function templater(input, data) {
if (!['string', 'object'].includes(typeof input)) {
return input;
}
for (var key in data) {
if (Object.prototype.hasOwnProperty.call(data, key)) {
if (typeof data[key] === 'object' && data[key] !== null && !Array.isArray(data[key])) {
var nested = {};
for (var name in data[key]) {
nested[key + '.' + name] = data[key][name];
}
input = templater(input, nested);
} else {
input = input.replace(new RegExp('\{\{' + key + '\}\}', 'g'), data[key]);
}
}
}
return input;
}
function numberShorter(value, fixed, symbols) {
var schema = symbols || ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
var index = Math.floor(Math.log10(value) / 3);
return (value / Math.pow(10, 3 * index)).toFixed(fixed || 2) + (schema[index] === undefined ? ('e' + index * 3) : schema[index]);
}
var setRequestHeader = XMLHttpRequest.prototype.setRequestHeader;
var once = true;
XMLHttpRequest.prototype.setRequestHeader = function(key, value) {
if (once) {
console.warn(key, value);
setRequestHeader.call(this, key, value);
once = false;
}
}