Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $.fn.paginate = function (setting) {
- var paginate = {
- classButton: 'button-circle',
- classScrollBox: 'scroll-box',
- classButtonBox: 'button-box',
- widthButton: 40,
- maxViewButton: 7,
- completeAjax: true,
- template: 'template',
- startScrollTop: false,
- init: function () {
- // если не указанная paginate считаем что это не плитки
- if ($(this.targetBlock).data('paginate') == undefined) this.template = 'default';
- // добавляем обертку для вставки пагинатора
- this.$main.append($('<span>').addClass(this.classScrollBox).append($('<span>').addClass(this.classButtonBox)));
- this.$scrollBox = this.returnDom(this.classScrollBox);
- this.$buttonBox = this.returnDom(this.classButtonBox);
- this.setCountPage();
- this.createPaginate();
- this.setStyles();
- this.setEvent();
- },
- setCountPage: function () { // максимальное кол-во страниц для пагинации
- if (this.template == 'default') this.namberButton = Math.ceil(this.maxCount / $(this.targetBlock).data('tileLine'));
- else this.namberButton = Math.ceil(this.maxCount / this.getCountItem());
- },
- getCountItem: function () { // максимально возможное колличество одновременно показаваевых плиток
- if (this.template == 'default') return $(this.targetBlock).data('tileLine');
- var value = Math.floor($(this.targetBlock).width() / this.tileWidth) * $(this.targetBlock).data('paginate').line;
- return !value ? 1 : value;
- },
- createPaginate: function () { // установка навигационных кнопок
- this.$main.prepend(this.createButton('left', 'prev', ''));
- this.$main.prepend(this.createButton('start', 'start', ''));
- this.createListButton();
- this.$main.append(this.createButton('right', 'next', ''));
- this.$main.append(this.createButton('finish', 'finish', ''));
- },
- createListButton: function () { // установ постраничных кнопок
- this.$buttonBox.html('');
- for (var i = 1; i <= this.namberButton; i++) {
- this.$buttonBox.append(this.createButton(i == this.pageBlock ? 'active' : '', i, i));
- }
- },
- createButton: function (c, p, t) { // создание кнопки
- var $button = $('<button>').addClass($.proxy(function () {
- return this.classButton + ' ' + c
- }, this)).attr('data-page', p).text(t).on('click', $.proxy(function (e) {
- this.clickButton($(e.target));
- }, this));
- if (t > 99) {
- $button.css('font-size', '12px');
- } else if (t > 999) {
- $button.css('font-size', '10px');
- }
- return $button
- },
- scrollButton: function ($button) { // Скроллинг кнопок при переходе
- this.pageBlock = $button.data('page');
- this.$scrollBox.animate({
- 'scrollLeft': $button.position().left + $button.outerWidth() / 2 - this.$scrollBox.outerWidth() / 2
- });
- },
- setEvent: function () { // отловка события изменения размеров родительской области
- $(this.targetBlock).on('reload', $.proxy(function () {
- if (this.template != 'default') {
- if (window.screen.width < 1100) this.screenRadio = 0.7;
- else if (window.screen.width < 1400) this.screenRadio = 0.9;
- else this.screenRadio = 1;
- this.tileWidth = $(this.targetBlock).data('paginate').w * this.screenRadio;
- this.tileHeight = $(this.targetBlock).data('paginate').h * this.screenRadio;
- }
- if (this.getCountItem() != $(this.targetBlock).find('figure').size()) {
- this.setCountPage();
- this.createListButton();
- this.setStyles();
- this.getContent();
- } else {
- this.chessBoardIn();
- }
- }, this)).trigger('reload');
- },
- setStyles: function () { // установка размеров и прочего
- this.$scrollBox.width(this.namberButton >= this.maxViewButton ? this.widthButton * this.maxViewButton : this.widthButton * this.namberButton)
- this.$buttonBox.width(this.widthButton * this.namberButton);
- this.$main.width(function () {
- var width = 0;
- $(this).children().each(function () {
- width += $(this).outerWidth(true);
- })
- return width;
- })
- if (this.namberButton <= 1) this.$main.addClass('dn');
- else this.$main.removeClass('dn');
- // TODO: сделать шрифт и выпилить этот дурдом ...
- var regBacPos = /(-?\d+)(p?x?)\s(-?\d+)(p?x?)/;
- var regInsert = '$1$2 -90px'; // mnogo || mystream
- var portal = false;
- if (/([^.]+?).mnogo/.test(window.location.host)) {
- portal = window.location.host.match(/([^.]+?).mnogo/)[1];
- }
- else if (/([^.]+?).mystream/.test(window.location.host)) {
- portal = window.location.host.match(/([^.]+?).mystream/)[1];
- }
- if (portal) {
- switch (portal) {
- case 'music':
- regInsert = '$1$2 -30px';
- break;
- case 'video':
- regInsert = '$1$2 -60px';
- break;
- case 'games':
- regInsert = '$1$2 -120px';
- break;
- case 'soft':
- regInsert = '$1$2 -150px';
- break;
- case 'radio':
- regInsert = '$1$2 -180px';
- break;
- case 'books':
- regInsert = '$1$2 -210px';
- break;
- case 'share':
- regInsert = '$1$2 -240px';
- break;
- default:
- regInsert = '$1$2 0';
- }
- }
- this.$main.find('button').each(function () {
- if ($(this).css('background-position') == undefined) return false
- if ($(this).hasClass('active')) {
- $(this).css('background-position', $(this).css('background-position').replace(regBacPos, regInsert))
- .off('mouseenter mouseleave')
- }
- else {
- $(this).css('background-position', $(this).css('background-position').replace(regBacPos, '$1$2 0'))
- .on({
- 'mouseenter': function () {
- $(this).css('background-position', $(this).css('background-position').replace(regBacPos, regInsert))
- },
- 'mouseleave': function () {
- $(this).css('background-position', $(this).css('background-position').replace(regBacPos, '$1$2 0'))
- }
- });
- }
- })
- // <-
- },
- returnDom: function (selector) { // Возращение Jquery обьекта
- return this.$main.find('#' + selector).size() != 0 ? this.$main.find('#' + selector) : this.$main.find('.' + selector);
- },
- clickButton: function ($button) { // отлов нажатия на всех кнопках
- if (this.completeAjax) {
- var page = $button.data('page');
- if (page % 1 === 0) {
- this.$buttonBox.find('button').removeClass('active');
- this.scrollButton($button.addClass('active'));
- this.getContent();
- } else if (page == 'start') {
- this.scrollButton(this.$buttonBox.find('button').removeClass('active').first().addClass('active'));
- this.getContent();
- } else if (page == 'prev') {
- if (this.$buttonBox.find('button.active').prev().size() != 0) {
- this.scrollButton(this.$buttonBox.find('button.active').removeClass('active').prev().addClass('active'));
- this.getContent();
- }
- } else if (page == 'next') {
- if (this.$buttonBox.find('button.active').next().size() != 0) {
- this.scrollButton(this.$buttonBox.find('button.active').removeClass('active').next().addClass('active'));
- this.getContent();
- }
- } else if (page == 'finish') {
- this.scrollButton(this.$buttonBox.find('button').removeClass('active').last().addClass('active'));
- this.getContent();
- }
- this.setStyles()
- }
- },
- getContent: function () { // загрузка контента
- if (this.completeAjax) {
- this.completeAjax = false;
- // скрытие и добавление статуса загрузки
- $(this.targetBlock).addClass('loader').children().animate({'opacity': 0}, 1000);
- $.ajax({
- type: this.methodAjax,
- url: this.urlAjax,
- data: "page=" + this.pageBlock + "&count=" + this.getCountItem(),
- success: $.proxy(function (data) {
- if (this.template == 'default') {
- $(this.targetBlock).html(data).removeClass('loader').children().animate({'opacity': 1}, 1000);
- if (this.startScrollTop) {
- $('body').animate({scrollTop: $(this.targetBlock).closest('section').offset().top - $('header.main').outerHeight()});
- }
- this.startScrollTop = true;
- } else {
- this.chessBoardIn(data);
- }
- this.completeAjax = true;
- }, this),
- error: $.proxy(function (data) {
- this.completeAjax = true;
- console.log(data, 'target: ' + this.targetBlock);
- }, this)
- }
- )
- }
- },
- chessBoardIn: function (data) { // шайтанство с анимацией и заменой контента
- var delay = 0;
- var radioBlock = this.tileWidth / this.tileHeight;
- var countLine = Math.floor($(this.targetBlock).outerWidth() / this.tileWidth);
- var widthBlock = Math.floor($(this.targetBlock).outerWidth() / countLine);
- var heightBlock = Math.floor((widthBlock / radioBlock));
- // снятие статуса загрузки и вставка контента
- if (data) {
- $(this.targetBlock).removeClass('loader').html(data);
- $(this.targetBlock).find('figure').each(function (i) {
- var key = (i + 1) / countLine;
- $(this).css({'width': (key ^ 0) == key ? widthBlock - 1 : widthBlock, 'height': heightBlock, 'opacity': 0}).delay(delay).animate({'opacity': 1 });
- delay += 50;
- })
- } else {
- $(this.targetBlock).find('figure').each(function (i) {
- var key = (i + 1) / countLine;
- $(this).css({'width': (key ^ 0) == key ? widthBlock - 1 : widthBlock, 'height': heightBlock});
- })
- }
- var sizeRow = Math.ceil(widthBlock * $(this.targetBlock).find('figure').size() / $(this.targetBlock).outerWidth());
- $(this.targetBlock).animate({'height': heightBlock * sizeRow});
- }
- }
- paginate.$main = $(this);
- paginate.pageBlock = setting.pageBlock;
- paginate.targetBlock = setting.targetBox;
- paginate.namberButton = setting.maxPages;
- paginate.maxCount = setting.maxCountItem;
- paginate.methodAjax = setting.methodRequest;
- paginate.urlAjax = setting.urlRequest;
- paginate.init();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement