%PDF- %PDF-
Direktori : /home/bitrix/www/bitrix/js/orion.misc/infinitescroll/ |
Current File : /home/bitrix/www/bitrix/js/orion.misc/infinitescroll/infinitescroll.js |
/* * LLC Orion Soft, 2013 * www.orion-soft.ru * Alexandr Lobanov * version 2.2 - n-inst + window + self-ajax + events * version 2.2.1 - fix panel switching to last page * version 3.0 - table support * version 3.1 - fix bug */ function orionInfPageScroll(params){ this.window_bind = ''; this.smooth_scroll = 1; this.float_bar_show = 1; this.nav_bar_show = 1; this.nav_num = ''; this.limit_page = 0; this.current_page = 0; this.pagen_name = 'PAGEN_1'; this.showall_name = 'SHOWALL_1'; this.margin_from_end = -100; this.margin_from_begin = 40; this.margin_from_post = -10; this.self_ajax_url = ''; this.on_ajax_before = null; this.on_ajax_success = null; this.on_ajax_data = null; this.on_ajax_data4insert = null; this.on_ajax_error = null; this.on_ajax_complete = null; this.on_scroll = null; this.on_page_click_before = null; this.on_page_click_after = null; this.on_page_switch = null; this.setParams(params); this.init(); }; orionInfPageScroll.prototype.setParams = function (params){ for(var i in params){this[i] = params[i]} } orionInfPageScroll.prototype.switchNavBar = function (state){ if(state == 0) $('.' + this.bar_sp_name + this.nav_num).hide(); else $('.' + this.bar_sp_name + this.nav_num).show(); } orionInfPageScroll.prototype.ajaxLoader = function (state, id){ if(state == 'on' && $('.orion-isp-ajax-wait').size() == 0) $('<div class="orion-isp-ajax-wait"></div>').insertAfter($(id)); else if(state == 'off') $('.orion-isp-ajax-wait').fadeOut('fast', function(){$(this).remove()}); } orionInfPageScroll.prototype.OnPageClickAnimComplete = function(n){ if(this.on_page_click_after != null && typeof window[this.on_page_click_after] == 'function' ) window[this.on_page_click_after](n); } orionInfPageScroll.prototype.onPageClick = function(e){ var this1 = this; var re = new RegExp(this.pagen_name + '=[\\s]*([\\d]*)', 'i'); var re1 = new RegExp(this.showall_name + '=[\\s]*([\\d]*)', 'i'); var r = e.target.href.match(re); var r1 = e.target.href.match(re1); if(r1 == null){ var n = (r != null) ? parseInt(r[1]) : 1; if(this.on_page_click_before != null && typeof window[this.on_page_click_before] == 'function' ) window[this.on_page_click_before](n); if(n >= this.min_page && n <= this.max_page){ var wst = this.getScrollTop(n); if(this.window_bind == ''){ if(this.smooth_scroll) $('html, body').animate({scrollTop : wst}, function(){ this1.OnPageClickAnimComplete(n); }); else{ $('html, body').scrollTop(wst); this1.OnPageClickAnimComplete(n); } }else{ bwt = $(this.window_bind).offset().top; sp1t = this.getScrollTop(this.min_page); if(this.smooth_scroll) $(this.window_bind).animate({scrollTop : wst - sp1t}, function(){ this1.OnPageClickAnimComplete(n); }); else{ $(this.window_bind).scrollTop(wst - sp1t); this1.OnPageClickAnimComplete(n); } } return false; } } return true; } orionInfPageScroll.prototype.updatePaging = function(n, force){ if(!force && this.current_page == n) return ; var sp_class = '.' + this.bar_sp_name + this.nav_num; var sp = $(sp_class); var spf_class = '.' + this.fbar_sp_name + this.nav_num; var spf = $(spf_class + ' .txt').eq(0); if(this.data[n].pagesData){ spf.html(this.data[n].pagesData); sp.html(this.data[n].pagesData); this.current_page = n; if(this.on_page_switch != null && typeof window[this.on_page_switch] == 'function' ) window[this.on_page_switch](n); } var spf = $(spf_class); var bg = $(spf_class + ' .bg'); bg.width(spf.width()); } orionInfPageScroll.prototype.add_data = function(num){this.data[num] = {'number': num, pagesData: ''};} orionInfPageScroll.prototype.getScrollBtm = function(num){ var asp = this.getMark(this, 'e', num, 0); if(asp.size()){ if(this.list_mode == 'l'){ return asp.offset().top; } else { return asp.offset().top + asp.height(); } } return 0; } orionInfPageScroll.prototype.getScrollTop = function(num){ var asp = this.getMark(this, 'b', num, 0); if(asp.size()) return asp.offset().top; return 0; } orionInfPageScroll.prototype.jquery2html = function(d) { var a = []; for ( var i = 0; i < d.length; i++ ) { a.push(d[i].outerHTML); } return a.join(""); } orionInfPageScroll.prototype.ajax = function(){ if(this.max_page == this.limit_page) return false; if(this.on_ajax_before != null && typeof window[this.on_ajax_before] == 'function' ) { var ret = window[this.on_ajax_before](this.max_page + 1); if(ret == false) return false; } this.add_data(this.max_page + 1); if (this.list_mode == 'l') { this.ajaxLoader('on', this.getMark(this, 'e', this.max_page, 0)); } else { this.ajaxLoader('on', $('table.' + this.table_class_sp_name + this.nav_num)); } this.max_page++; var page_loc = location.href.split('?')[0]; var re = new RegExp('(' + this.pagen_name + '=)[\\s]*[\\d]*', 'i'); var loc = (this.self_ajax_url) ? this.self_ajax_url : location.href; var r = loc.match(re); if(r != null){ loc = loc.replace(re, '$1' + this.max_page); }else{ var glue = (loc.indexOf('?')>0)?'&':'?'; var arLoc = loc.split('#'); loc = arLoc[0] + glue + this.pagen_name + '=' + this.max_page; } if(this.max_page == this.limit_page) $(this.sp_btn_id + this.nav_num).hide(); var this1 = this; $.ajax({ 'url': loc, 'type': 'POST', 'data': {page_url:page_loc, nav_num:this1.nav_num}, 'success': function(d, textStatus){ if(this1.on_ajax_success != null && typeof window[this1.on_ajax_success] == 'function' ) window[this1.on_ajax_success](d, textStatus); this1.ajaxLoader('off'); if(!d) return ; var re = new RegExp('(?:<![-]{2}[\\s]*' + this1.begin_sp_name + this1.nav_num + '[^-]*[-]{2}>)([\\s\\S]*?)(?=<![-]{2}[\\s]*' + this1.end_sp_name + this1.nav_num + ')', 'i'); var r = d.match(re); if(r == null) return ; this1.data[this1.max_page].pagesData = r[1]; var newData; if (this1.list_mode == 'l') { re = new RegExp('(?:["\']+' + this1.begin_list_sp_name + this1.nav_num + '[\\s\\S]*?(?=span[\\s]*>)span[\\s]*>)([\\s\\S]*)(?=<[^"\']*["\']+' + this1.end_list_sp_name + this1.nav_num + ')', 'i'); r = d.match(re); if(r == null) return ; if(this1.on_ajax_data != null && typeof window[this1.on_ajax_data] == 'function' ) r[1] = window[this1.on_ajax_data](r[1]); s = '<a name="' + this1.begin_sp_name + this1.nav_num + '_' + this1.max_page + '"></a>' + r[1] + '<a name="' + this1.end_sp_name + this1.nav_num + '_' + this1.max_page + '"></a>'; newData = $(s); } else { re = new RegExp('<[\\s]*table.* (?=class[\\s]*=[\\s]*"[\\s\\S]*' + this1.table_class_sp_name + ')[\\s\\S]*?(?!<[\\s]*tr[\\s\\S]*<[\\s]*th)(<[\\s]*tr[\\s\\S]*?)(?=<[\\s]*\/[\\s]*table)', 'i'); r = d.match(re); if(r == null) return ; if(this1.on_ajax_data != null && typeof window[this1.on_ajax_data] == 'function' ) r[1] = window[this1.on_ajax_data](r[1]); var firstTRClass = this1.begin_sp_name + this1.nav_num + '_' + this1.max_page; var lastTRClass = this1.end_sp_name + this1.nav_num + '_' + this1.max_page; newData = $(r[1]).filter('tr:last').addClass(lastTRClass).end().filter('tr:first').addClass(firstTRClass).end(); } if(this1.on_ajax_data4insert != null && typeof window[this1.on_ajax_data4insert] == 'function' ) newData = $(window[this1.on_ajax_data4insert](this1.jquery2html(newData))); newData.insertAfter(this1.getMark(this1, 'e', (this1.max_page-1), 0)); }, 'error': function(XMLHttpRequest, textStatus, errorThrown){ if(this1.on_ajax_error != null && typeof window[this1.on_ajax_error] == 'function' ) window[this1.on_ajax_error](XMLHttpRequest, textStatus, errorThrown); }, 'complete': function(XMLHttpRequest, textStatus){ if(this1.on_ajax_complete != null && typeof window[this1.on_ajax_complete] == 'function' ) window[this1.on_ajax_complete](XMLHttpRequest, textStatus); } }); return true; } orionInfPageScroll.prototype.checkVisible = function(){ if(this.getScrollTop(this.min_page) != this.getScrollBtm(this.max_page)){ if(!this.is_visible) this.updatePaging(this.current_page, true); this.is_visible = 1; }else{ this.is_visible = 0; } return this.is_visible; } orionInfPageScroll.prototype.onScroll = function(e){ if(!this.checkVisible()) return false; if(this.window_bind == ''){ var wtop = Math.abs($(window).scrollTop()); var wheight = $(window).height(); }else{ var bw = $(this.window_bind); var wtop = Math.abs(bw.offset().top); var wheight = bw.height(); } var nbt = $('.' + this.bar_sp_name + this.nav_num).eq(0).offset().top; var sbt = this.getScrollTop(this.min_page); var pbtm = (nbt < sbt) ? nbt: sbt; var spf = $('.' + this.fbar_sp_name + this.nav_num).eq(0); if((-wtop + pbtm) < -this.margin_from_begin && this.float_bar_active == 0){ if(this.float_bar_show){ spf.fadeIn('fast'); } this.float_bar_active = 1; }else if((-wtop + pbtm) > -this.margin_from_begin && this.float_bar_active == 1){ if(this.float_bar_show){ spf.fadeOut('fast'); } this.float_bar_active = 0; } if(this.window_bind == ''){ this.direction = (wtop > this.last_wScroll)?1:-1; this.last_wScroll = wtop; }else{ var spt = this.getScrollTop(this.min_page); this.direction = (spt < this.last_wScroll)?1:-1; this.last_wScroll = spt; } if(this.limit_page > this.max_page){ var btmA = this.getMark(this, 'e', this.max_page, 0); if(btmA.size() > 0){ if((wheight + wtop) > (btmA.offset().top + this.margin_from_end)){ if($(this.sp_btn_id + this.nav_num).size() === 0) { this.ajax(); } } } } pScrollTop = this.getScrollTop(this.current_page); pScrollBtm = this.getScrollBtm(this.current_page); if(pScrollTop != null && pScrollBtm != null){ if((-wtop + pScrollBtm) < -this.margin_from_post && this.direction > 0 && this.current_page < this.limit_page){ var n = this.current_page, num = n; while(++n <= this.max_page){ num = n; if((-wtop + this.getScrollBtm(n-1)) > -this.margin_from_post) {num--; break;} } this.updatePaging(num); }else if((-wtop + pScrollBtm) > -this.margin_from_post && this.direction < 0 && this.current_page >= this.min_page){ var n = this.current_page, num = n; while(--n >= this.min_page){ num = n; if((-wtop + this.getScrollBtm(n)) < -this.margin_from_post) {num++; break;} } this.updatePaging(num); } if(this.direction > 0 && this.max_page == this.limit_page && this.current_page == this.limit_page-1){ pScrollBtmLmt = this.getScrollBtm(this.limit_page); if(pScrollBtmLmt != null && (-wtop - wheight + pScrollBtmLmt) < -this.margin_from_end){ this.updatePaging(this.limit_page); } } } if(this.on_scroll != null && typeof window[this.on_scroll] == 'function' ){ var st = (this.window_bind == '') ? this.last_wScroll: wtop - this.last_wScroll; window[this.on_scroll](st); } } orionInfPageScroll.prototype.getMark = function(this1, mark, page, idx){ var res; if (this1.list_mode == 'l') { if (mark == 'b') { res = $('a[name^="' + this1.begin_sp_name + this1.nav_num + '_' + page + '"]'); } else { res = $('a[name^="' + this1.end_sp_name + this1.nav_num + '_' + page + '"]'); } } else{ if (mark == 'b') { res = $('tr.' + this1.begin_sp_name + this1.nav_num + '_' + page); } else { res = $('tr.' + this1.end_sp_name + this1.nav_num + '_' + page); } } if(idx >= 0) return res.eq(idx); else return res; } orionInfPageScroll.prototype.init = function(){ var fn_on_init_name = 'orion_pgscroll' + this.nav_num + '_on_init'; if(typeof window[fn_on_init_name] == 'function') this.setParams(window[fn_on_init_name]()); this.window_sp_name = 'orion_isp_window'; this.sp_btn_id = '#orion-isp-btn'; this.begin_sp_name = 'orion_isp_begin'; this.end_sp_name = 'orion_isp_end'; this.table_class_sp_name = "orion_isp_table"; this.begin_list_sp_name = 'orion_isp_list_begin'; this.end_list_sp_name = 'orion_isp_list_end'; this.bar_sp_name = 'orion_isp'; this.fbar_sp_name = 'orion-isp-fixed'; this.float_bar_active = 0; this.min_page = 0; this.max_page = 0; this.direction = 0; this.last_wScroll = 0; this.is_visible = 0; this.list_mode = 'l'; this.data = {}; if(this.window_bind == ''){ var wsp_selector = '.' + this.window_sp_name + this.nav_num; if($(wsp_selector).size()) this.window_bind = wsp_selector; } if($('table.' + this.table_class_sp_name + this.nav_num).size()) this.list_mode = 't'; if(this.window_bind != ''){ this.float_bar_show = 0;} this.switchNavBar(this.nav_bar_show); var this1 = this; var ispf = this.fbar_sp_name + this.nav_num; var isp = this.bar_sp_name + this.nav_num; if($('.' + ispf).size() == 0) $('<div class="' + this.fbar_sp_name + ' ' + ispf + '"><div class="bg"></div><div class="txt"></div></div>').insertAfter($('.' + isp)); if(jQuery.fn.jquery.split('.').slice(0,2).join('') < 17){ $('.' + isp + ' a').live('click', function(e){return this1.onPageClick(e);}); $('.' + ispf + ' .txt a').live('click', function(e){return this1.onPageClick(e);}); }else{ $(document).on('click', '.' + isp + ' a', function(e){return this1.onPageClick(e);}); $(document).on('click', '.' + ispf + ' .txt a', function(e){return this1.onPageClick(e);}); } if(this.list_mode == 'l'){ $('.' + this.begin_list_sp_name + this.nav_num).replaceWith('<a name="' + this.begin_sp_name + this.nav_num + '_' + this.current_page + '"></a>'); $('.' + this.end_list_sp_name + this.nav_num).replaceWith('<a name="' + this.end_sp_name + this.nav_num + '_' + this.current_page + '"></a>'); } else { $('table.' + this.table_class_sp_name + this.nav_num + ' tr:first').addClass(this.begin_sp_name + this.nav_num + '_' + this.current_page); $('table.' + this.table_class_sp_name + this.nav_num + ' tr:last').addClass(this.end_sp_name + this.nav_num + '_' + this.current_page); } if(this.window_bind != ''){ $('.' + this.fbar_sp_name + this.nav_num).css('position', 'absolute'); } var btn = $(this.sp_btn_id + this.nav_num); if(btn.size()) btn.click(function(){ this1.ajax(); return false; }); this.max_page = this.current_page; this.min_page = this.current_page; this.add_data(this.current_page); var sp = $('.' + isp).eq(0); this.data[this.current_page].pagesData = sp.html(); this.checkVisible(); if(this.limit_page == this.current_page) $(this.sp_btn_id + this.nav_num).hide(); if($(this.sp_btn_id + this.nav_num).size() === 0) while(1){ var btmA = this.getMark(this, 'e', this.max_page, 0); var rpt = false; if(btmA.size() > 0){ if(this.window_bind == ''){ var wheight = $(window).height(); var btmAPos = btmA.offset().top; }else{ var wb = $(this.window_bind); var wheight = wb.height(); var btmAPos = btmA.offset().top - wb.offset().top; } if(wheight > btmAPos) if(this.ajax()) rpt = true; } if(!rpt) break; } if(this.window_bind == ''){ $(window).scroll(function(e){this1.onScroll(e);}); }else{ $(this.window_bind).scroll(function(e){this1.onScroll(e);}); } }