Advertisement
Guest User

Paginate Fuction

a guest
Nov 24th, 2014
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $.fn.paginate = function (setting) {
  2.     var paginate = {
  3.         classButton: 'button-circle',
  4.         classScrollBox: 'scroll-box',
  5.         classButtonBox: 'button-box',
  6.         widthButton: 40,
  7.         maxViewButton: 7,
  8.         completeAjax: true,
  9.         template: 'template',
  10.         startScrollTop: false,
  11.         init: function () {
  12.             // если не указанная paginate считаем что это не плитки
  13.             if ($(this.targetBlock).data('paginate') == undefined) this.template = 'default';
  14.  
  15.             // добавляем обертку для вставки пагинатора
  16.             this.$main.append($('<span>').addClass(this.classScrollBox).append($('<span>').addClass(this.classButtonBox)));
  17.             this.$scrollBox = this.returnDom(this.classScrollBox);
  18.             this.$buttonBox = this.returnDom(this.classButtonBox);
  19.             this.setCountPage();
  20.             this.createPaginate();
  21.             this.setStyles();
  22.             this.setEvent();
  23.         },
  24.         setCountPage: function () { // максимальное кол-во страниц для пагинации
  25.             if (this.template == 'default') this.namberButton = Math.ceil(this.maxCount / $(this.targetBlock).data('tileLine'));
  26.             else this.namberButton = Math.ceil(this.maxCount / this.getCountItem());
  27.         },
  28.         getCountItem: function () { // максимально возможное колличество одновременно показаваевых плиток
  29.             if (this.template == 'default') return $(this.targetBlock).data('tileLine');
  30.             var value = Math.floor($(this.targetBlock).width() / this.tileWidth) * $(this.targetBlock).data('paginate').line;
  31.             return !value ? 1 : value;
  32.         },
  33.         createPaginate: function () { // установка навигационных кнопок
  34.             this.$main.prepend(this.createButton('left', 'prev', ''));
  35.             this.$main.prepend(this.createButton('start', 'start', ''));
  36.             this.createListButton();
  37.             this.$main.append(this.createButton('right', 'next', ''));
  38.             this.$main.append(this.createButton('finish', 'finish', ''));
  39.         },
  40.         createListButton: function () { // установ постраничных кнопок
  41.             this.$buttonBox.html('');
  42.             for (var i = 1; i <= this.namberButton; i++) {
  43.                 this.$buttonBox.append(this.createButton(i == this.pageBlock ? 'active' : '', i, i));
  44.             }
  45.         },
  46.         createButton: function (c, p, t) { // создание кнопки
  47.             var $button = $('<button>').addClass($.proxy(function () {
  48.                     return this.classButton + ' ' + c
  49.                 }, this)).attr('data-page', p).text(t).on('click', $.proxy(function (e) {
  50.                     this.clickButton($(e.target));
  51.                 }, this));
  52.             if (t > 99) {
  53.                 $button.css('font-size', '12px');
  54.             } else if (t > 999) {
  55.                 $button.css('font-size', '10px');
  56.             }
  57.             return $button
  58.         },
  59.         scrollButton: function ($button) { // Скроллинг кнопок при переходе
  60.             this.pageBlock = $button.data('page');
  61.             this.$scrollBox.animate({
  62.                 'scrollLeft': $button.position().left + $button.outerWidth() / 2 - this.$scrollBox.outerWidth() / 2
  63.             });
  64.         },
  65.         setEvent: function () { // отловка события изменения размеров родительской области
  66.             $(this.targetBlock).on('reload', $.proxy(function () {
  67.  
  68.                 if (this.template != 'default') {
  69.                     if (window.screen.width < 1100) this.screenRadio = 0.7;
  70.                     else if (window.screen.width < 1400) this.screenRadio = 0.9;
  71.                     else this.screenRadio = 1;
  72.                     this.tileWidth = $(this.targetBlock).data('paginate').w * this.screenRadio;
  73.                     this.tileHeight = $(this.targetBlock).data('paginate').h * this.screenRadio;
  74.                 }
  75.  
  76.                 if (this.getCountItem() != $(this.targetBlock).find('figure').size()) {
  77.                     this.setCountPage();
  78.                     this.createListButton();
  79.                     this.setStyles();
  80.                     this.getContent();
  81.                 } else {
  82.                     this.chessBoardIn();
  83.                 }
  84.             }, this)).trigger('reload');
  85.         },
  86.         setStyles: function () { // установка размеров и прочего
  87.             this.$scrollBox.width(this.namberButton >= this.maxViewButton ? this.widthButton * this.maxViewButton : this.widthButton * this.namberButton)
  88.             this.$buttonBox.width(this.widthButton * this.namberButton);
  89.             this.$main.width(function () {
  90.                 var width = 0;
  91.                 $(this).children().each(function () {
  92.                     width += $(this).outerWidth(true);
  93.                 })
  94.                 return width;
  95.             })
  96.             if (this.namberButton <= 1)  this.$main.addClass('dn');
  97.             else this.$main.removeClass('dn');
  98.  
  99.             // TODO: сделать шрифт и выпилить этот дурдом ...
  100.             var regBacPos = /(-?\d+)(p?x?)\s(-?\d+)(p?x?)/;
  101.             var regInsert = '$1$2 -90px'; // mnogo || mystream
  102.             var portal = false;
  103.             if (/([^.]+?).mnogo/.test(window.location.host)) {
  104.                 portal = window.location.host.match(/([^.]+?).mnogo/)[1];
  105.             }
  106.             else if (/([^.]+?).mystream/.test(window.location.host)) {
  107.                 portal = window.location.host.match(/([^.]+?).mystream/)[1];
  108.             }
  109.             if (portal) {
  110.                 switch (portal) {
  111.                     case 'music':
  112.                         regInsert = '$1$2 -30px';
  113.                         break;
  114.                     case 'video':
  115.                         regInsert = '$1$2 -60px';
  116.                         break;
  117.                     case 'games':
  118.                         regInsert = '$1$2 -120px';
  119.                         break;
  120.                     case 'soft':
  121.                         regInsert = '$1$2 -150px';
  122.                         break;
  123.                     case 'radio':
  124.                         regInsert = '$1$2 -180px';
  125.                         break;
  126.                     case 'books':
  127.                         regInsert = '$1$2 -210px';
  128.                         break;
  129.                     case 'share':
  130.                         regInsert = '$1$2 -240px';
  131.                         break;
  132.                     default:
  133.                         regInsert = '$1$2 0';
  134.                 }
  135.             }
  136.             this.$main.find('button').each(function () {
  137.                 if ($(this).css('background-position') == undefined) return false
  138.  
  139.                 if ($(this).hasClass('active')) {
  140.                     $(this).css('background-position', $(this).css('background-position').replace(regBacPos, regInsert))
  141.                         .off('mouseenter mouseleave')
  142.                 }
  143.                 else {
  144.                     $(this).css('background-position', $(this).css('background-position').replace(regBacPos, '$1$2 0'))
  145.                         .on({
  146.                             'mouseenter': function () {
  147.                                 $(this).css('background-position', $(this).css('background-position').replace(regBacPos, regInsert))
  148.                             },
  149.                             'mouseleave': function () {
  150.                                 $(this).css('background-position', $(this).css('background-position').replace(regBacPos, '$1$2 0'))
  151.                             }
  152.                         });
  153.                 }
  154.             })
  155.             // <-
  156.         },
  157.         returnDom: function (selector) { // Возращение Jquery обьекта
  158.             return this.$main.find('#' + selector).size() != 0 ? this.$main.find('#' + selector) : this.$main.find('.' + selector);
  159.         },
  160.         clickButton: function ($button) { // отлов нажатия на всех кнопках
  161.             if (this.completeAjax) {
  162.                 var page = $button.data('page');
  163.                 if (page % 1 === 0) {
  164.                     this.$buttonBox.find('button').removeClass('active');
  165.                     this.scrollButton($button.addClass('active'));
  166.                     this.getContent();
  167.                 } else if (page == 'start') {
  168.                     this.scrollButton(this.$buttonBox.find('button').removeClass('active').first().addClass('active'));
  169.                     this.getContent();
  170.                 } else if (page == 'prev') {
  171.                     if (this.$buttonBox.find('button.active').prev().size() != 0) {
  172.                         this.scrollButton(this.$buttonBox.find('button.active').removeClass('active').prev().addClass('active'));
  173.                         this.getContent();
  174.                     }
  175.                 } else if (page == 'next') {
  176.                     if (this.$buttonBox.find('button.active').next().size() != 0) {
  177.                         this.scrollButton(this.$buttonBox.find('button.active').removeClass('active').next().addClass('active'));
  178.                         this.getContent();
  179.                     }
  180.                 } else if (page == 'finish') {
  181.                     this.scrollButton(this.$buttonBox.find('button').removeClass('active').last().addClass('active'));
  182.                     this.getContent();
  183.                 }
  184.                 this.setStyles()
  185.             }
  186.         },
  187.         getContent: function () { // загрузка контента
  188.             if (this.completeAjax) {
  189.                 this.completeAjax = false;
  190.                 // скрытие и добавление статуса загрузки
  191.                 $(this.targetBlock).addClass('loader').children().animate({'opacity': 0}, 1000);
  192.                 $.ajax({
  193.                         type: this.methodAjax,
  194.                         url: this.urlAjax,
  195.                         data: "page=" + this.pageBlock + "&count=" + this.getCountItem(),
  196.                         success: $.proxy(function (data) {
  197.                             if (this.template == 'default') {
  198.                                 $(this.targetBlock).html(data).removeClass('loader').children().animate({'opacity': 1}, 1000);
  199.                                 if (this.startScrollTop) {
  200.                                     $('body').animate({scrollTop: $(this.targetBlock).closest('section').offset().top - $('header.main').outerHeight()});
  201.                                 }
  202.                                 this.startScrollTop = true;
  203.                             } else {
  204.                                 this.chessBoardIn(data);
  205.                             }
  206.                             this.completeAjax = true;
  207.                         }, this),
  208.                         error: $.proxy(function (data) {
  209.                             this.completeAjax = true;
  210.                             console.log(data, 'target: ' + this.targetBlock);
  211.                         }, this)
  212.                     }
  213.                 )
  214.             }
  215.         },
  216.         chessBoardIn: function (data) { // шайтанство с анимацией и заменой контента
  217.             var delay = 0;
  218.             var radioBlock = this.tileWidth / this.tileHeight;
  219.             var countLine = Math.floor($(this.targetBlock).outerWidth() / this.tileWidth);
  220.             var widthBlock = Math.floor($(this.targetBlock).outerWidth() / countLine);
  221.             var heightBlock = Math.floor((widthBlock / radioBlock));
  222.  
  223.             // снятие статуса загрузки и вставка контента
  224.             if (data) {
  225.                 $(this.targetBlock).removeClass('loader').html(data);
  226.                 $(this.targetBlock).find('figure').each(function (i) {
  227.                     var key = (i + 1) / countLine;
  228.                     $(this).css({'width': (key ^ 0) == key ? widthBlock - 1 : widthBlock, 'height': heightBlock, 'opacity': 0}).delay(delay).animate({'opacity': 1 });
  229.                     delay += 50;
  230.                 })
  231.             } else {
  232.                 $(this.targetBlock).find('figure').each(function (i) {
  233.                     var key = (i + 1) / countLine;
  234.                     $(this).css({'width': (key ^ 0) == key ? widthBlock - 1 : widthBlock, 'height': heightBlock});
  235.                 })
  236.             }
  237.             var sizeRow = Math.ceil(widthBlock * $(this.targetBlock).find('figure').size() / $(this.targetBlock).outerWidth());
  238.             $(this.targetBlock).animate({'height': heightBlock * sizeRow});
  239.         }
  240.     }
  241.     paginate.$main = $(this);
  242.     paginate.pageBlock = setting.pageBlock;
  243.     paginate.targetBlock = setting.targetBox;
  244.     paginate.namberButton = setting.maxPages;
  245.     paginate.maxCount = setting.maxCountItem;
  246.     paginate.methodAjax = setting.methodRequest;
  247.     paginate.urlAjax = setting.urlRequest;
  248.     paginate.init();
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement