Created
June 16, 2015 20:48
-
-
Save toliklunev/0dbecaebb670088e5803 to your computer and use it in GitHub Desktop.
jQuery Helpers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery.fn.extend({ | |
// Возвращает/назначает аттрибут src | |
src: function(src){ | |
if(src !== undefined){ | |
return this.attr('src', src); | |
} | |
else{ | |
return this.attr('src') | |
} | |
}, | |
// Возвращает/назначает аттрибут href | |
href: function(href){ | |
if(href !== undefined){ | |
return this.attr('href', href); | |
} | |
else{ | |
return this.attr('href'); | |
} | |
}, | |
// Возвращает/назначает аттрибут hash у ссылки | |
hash: function(hash){ | |
var href = this.href().split('#'); | |
if(hash !== undefined){ | |
return this.attr('href', href[0] + '#' + hash); | |
} | |
else{ | |
return '#' + href[1]; | |
} | |
}, | |
pathname: function(pathname){ | |
var href = this.href().split('#'); | |
if(pathname !== undefined){ | |
return this.attr('pathname', pathname + '#' + href[1]); | |
} | |
else{ | |
if(href[0][0] !== '/') href[0] = '/' + href[0]; | |
return href[0]; | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery.fn.extend({ | |
// Разбивка числа по разрядам | |
breakdown: function(num, seporator){ | |
var seporator = seporator || ' '; | |
var regular = /(\d)(?=((\d{3})+)(\D|$))/g; | |
var make = function(){ | |
return num.replace(regular, '$1' + seporator); | |
} | |
if(typeof this == 'object'){ | |
return this.each(function(){ | |
num = $(this).text(); | |
$(this).text(make()); | |
}); | |
} | |
else if(typeof num == 'number'){ | |
num = num.toString(); | |
return make(); | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.clear { | |
cursor: pointer; | |
font-size: 0; | |
line-height: 0; | |
width: 14px; | |
height: 14px; | |
position: absolute; | |
right: 4px; | |
top: 6px; } | |
.clear:before, .clear:after { | |
-webkit-transform-origin: 0 0; | |
-moz-transform-origin: 0 0; | |
-ms-transform-origin: 0 0; | |
-o-transform-origin: 0 0; | |
transform-origin: 0 0; | |
border-left: 2px solid; | |
height: 100%; | |
position: absolute; | |
top: 50%; | |
left: 50%; | |
content: ''; } | |
.clear:before { | |
-webkit-transform: rotate(-45deg) translate(-50%, -50%); | |
-moz-transform: rotate(-45deg) translate(-50%, -50%); | |
-ms-transform: rotate(-45deg) translate(-50%, -50%); | |
-o-transform: rotate(-45deg) translate(-50%, -50%); | |
transform: rotate(-45deg) translate(-50%, -50%); } | |
.clear:after { | |
-webkit-transform: rotate(45deg) translate(-50%, -50%); | |
-moz-transform: rotate(45deg) translate(-50%, -50%); | |
-ms-transform: rotate(45deg) translate(-50%, -50%); | |
-o-transform: rotate(45deg) translate(-50%, -50%); | |
transform: rotate(45deg) translate(-50%, -50%); } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Крестик удаления текста из инпута | |
jQuery.fn.extend({ | |
cleaner: function(){ | |
return this.each(function(){ | |
var $input = $(this); | |
var $clear = $('<a class="clear">x</a>'); | |
$input.before($clear); | |
$clear.click(function(){ | |
$input.val('').focus(); | |
$clear.hide(); | |
}); | |
$input.bind('keyup keydown focus blur change cleaner', function(){ | |
if($input.val() !== '' && !$input.is(':disabled')){ | |
$clear.show(); | |
} | |
else{ | |
$clear.hide(); | |
} | |
}); | |
$input.trigger('cleaner'); | |
}); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@mixin prefixes($property, $str){ | |
#{-webkit-}#{$property}: #{$str}; | |
#{-moz-}#{$property}: #{$str}; | |
#{-ms-}#{$property}: #{$str}; | |
#{-o-}#{$property}: #{$str}; | |
#{$property}: #{$str}; | |
} | |
@mixin transform($str...){ | |
@include prefixes('transform', $str); | |
} | |
@mixin transform-origin($str...){ | |
@include prefixes('transform-origin', $str); | |
} | |
.clear{ | |
cursor: pointer; | |
font-size: 0; | |
line-height: 0; | |
width: 14px; | |
height: 14px; | |
position: absolute; | |
right: 4px; | |
top: 6px; | |
&:before, &:after{ | |
@include transform-origin(0 0); | |
border-left: 2px solid; | |
height: 100%; | |
position: absolute; | |
top: 50%; | |
left: 50%; | |
content: ''; | |
} | |
&:before{ | |
@include transform(rotate(-45deg) translate(-50%, -50%)); | |
} | |
&:after{ | |
@include transform(rotate(45deg) translate(-50%, -50%)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery.fn.extend({ | |
// Циклический next prev | |
cycle: function(nav, selector){ | |
if(this.is(':only-child')){ | |
return this; | |
} | |
if(nav == 'next'){ | |
if(this.is(':last-child')){ | |
return this.siblings(':first-child'); | |
} | |
return this.next(selector); | |
} | |
if(nav == 'prev'){ | |
if(this.is(':first-child')){ | |
return this.siblings(':last-child'); | |
} | |
return this.prev(selector); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Возвращает объект, который задан в data- | |
jQuery.fn.extend({ | |
dataObject: function(data){ | |
var object = this.data(data); | |
if(typeof object !== 'object'){ | |
object = eval('('+ this.data(data) +')'); | |
} | |
return object; | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Включение / отключение элемента | |
jQuery.fn.extend({ | |
disable: function(){ | |
return this.addClass('disabled').attr('disabled', true); | |
}, | |
enable: function(){ | |
return this.removeClass('disabled').attr('disabled', false); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Хелпер для форматирования окончаний | |
jQuery.fn.extend({ | |
ending: function(amount, ending1, ending2, ending3){ | |
var ending = ending3; | |
var remain100 = amount % 100; | |
var remain10 = amount % 10; | |
if(!((remain10 == 0) || (9 < remain100 && remain100 < 20))){ | |
if(remain10 == 1){ | |
ending = ending1; | |
} | |
else if(remain10 < 5){ | |
ending = ending2; | |
} | |
} | |
if(typeof this == 'object'){ | |
return this.text(ending); | |
} | |
return ending; | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Генерирует нужное колличество элементов | |
jQuery.fn.extend({ | |
genHtml: function(code, count){ | |
var $items = $([]); | |
while(count--){ | |
$items = $items.add(code); | |
} | |
return $items; | |
}, | |
// Вставляет определённое количество элементов | |
insertCount: function(method, code, count){ | |
if(method !== 'append' && method !== 'prepend'){ | |
throw new Error('method mast be append or prepend'); | |
} | |
return this[method](jQuery.genHtml(code, count)); | |
}, | |
appendCount: function(code, count){ | |
return this.insertCount('append', code, count); | |
}, | |
prependCount: function(code, count){ | |
return this.insertCount('prepend', code, count); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Назначает элементу определённый класс, у соседних элементов убирает этот класс | |
jQuery.fn.extend({ | |
makeActive: function(arg){ | |
options = { | |
className: 'active', | |
selector: '' | |
} | |
if(typeof arg == 'string'){ | |
options.className = arg | |
} | |
else if (typeof arg == 'object'){ | |
var options = $.extend(options, arg); | |
} | |
return this.each(function(){ | |
$(this) | |
.addClass(options.className) | |
.siblings(options.selector) | |
.removeClass(options.className); | |
}); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Делает возможным ввод только цифр | |
jQuery.fn.extend({ | |
onlyDigits: function(){ | |
return this.each(function(){ | |
$(this).keypress(function(e){ | |
return Boolean((e.which > 47) & (e.which < 58) || e.which == 46 || e.which == 8 || e.which == 0); | |
}); | |
}); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Парсит строку, возвращает число | |
jQuery.fn.extend({ | |
parseInt: function(arg1, arg2){ | |
if(typeof this == 'object'){ | |
var text = this.text() || this.val(); | |
var notation = arg1 || 10; | |
} | |
else if(typeof arg1 == 'number'){ | |
return arg1; | |
} | |
else{ | |
var text = arg1; | |
var notation = arg2 || 10; | |
} | |
var num = parseInt(text.replace(/ /g, ''), notation); | |
if(!isNaN(num)){ | |
return num; | |
} | |
return false; | |
}, | |
parseFloat: function(str){ | |
if(typeof str != 'undefined'){ | |
if(typeof str != 'string'){ | |
return str; | |
} | |
var text = str; | |
} | |
else if(typeof this == 'object'){ | |
var text = this.text() || this.val(); | |
} | |
var num = parseFloat(text.replace(/ /g, '')); | |
if(!isNaN(num)){ | |
return num; | |
} | |
return false; | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Попап | |
jQuery.fn.extend({ | |
popup: function(options){ | |
var $popup = this; | |
if(typeof options == 'string'){ | |
options = {link: options}; | |
} | |
var showPopup = function(){ | |
$popup.addClass('is-shown'); | |
$popup.removeClass('is-hidden'); | |
$(document).click(function(e){ | |
$popup.removeClass('is-shown'); | |
$popup.addClass('is-hidden'); | |
$(document).unbind(e); | |
}); | |
$popup.click(function(e){ | |
e.stopPropagation(); | |
}); | |
} | |
if(options && options.link){ | |
var shown = false; | |
$(options.link).click(function(e){ | |
if(shown){ | |
$(document).click(); | |
shown = false; | |
} | |
else{ | |
shown = true; | |
e.stopPropagation(); | |
showPopup(); | |
} | |
return false; | |
}); | |
} | |
else{ | |
showPopup(); | |
} | |
if(options && options.close){ | |
$(options.close, $popup).click(function(){ | |
$(document).click(); | |
return false; | |
}); | |
} | |
return this; | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Удаление пробелов и текста между элементами, применяется для решения проблемм с инлайн блоками | |
jQuery.fn.extend({ | |
removeSpaces: function(recursively){ | |
return this.each(function(){ | |
$(this).contents().filter(function(){ | |
return this.nodeType == 3; | |
}).remove(); | |
}); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Разворачивает порядок следования jQuery объектов | |
jQuery.fn.extend({ | |
reverse: function(){ | |
return $(this.toArray().reverse()); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Вызов функции с контекстом в виде текущего объекта | |
jQuery.fn.extend({ | |
run: function(arg1, arg2){ | |
if(typeof arg1 == 'function'){ | |
if(this.length){ | |
arg1.call(this[0]); | |
} | |
} | |
else if(typeof arg1 == 'object' && arg2 == 'function'){ | |
arg2.call(arg1); | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery.fn.extend({ | |
switcher: function(){ | |
return this.each(function(){ | |
var $tabs = $('> .tabs > *', this); | |
var $content = $('> .content > *', this); | |
$tabs.click(function(e){ | |
e.preventDefault(); | |
$content.eq($(this).makeActive().index()).makeActive(); | |
}); | |
if(!$tabs.is('.active')){ | |
$tabs.first().click(); | |
} | |
}); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Возвращает анонимную функцию, замкнутую на методе текущего объекта | |
jQuery.fn.extend({ | |
transmit: function(method, args){ | |
var that = this; | |
return function(){that[method].apply(that, args)}; | |
// return $.proxy(this, method); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Обновляет текущую выборку | |
jQuery.fn.extend({ | |
update: function(){ | |
return $(this.selector.replace(/.filter\((.*)\)/gi, '$1'), this.context); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment