ps-tabpage.js 3.84 KB
var $ = require('yoho.jquery'),
	Swiper = require('yoho.iswiper'),
	Mustache = require('yoho.mustache'),
	FetchBrand = require('./fetchbrand'),
	jgestures = require('yoho.jgestures');

var fetching = false,
	pagecount = 10,
	page = window.global_totalcount >= pagecount ? pagecount : window.global_totalcount,
	loaded = {},
	slideCache = [],
	slideMap = {},
	lastScrollY = window.pageYOffset,
	scrollY = window.pageYOffset,
	innerHeight,
	topViewPort,
	bottomViewPort;
/**
 *获得模板
 */
var template, global_timer;

function showLoadPreview() {
	document.querySelector('.loadmore').style.display = 'block';
}

function hideLoadPreview() {
	document.querySelector('.loadmore').style.display = 'none';
}
/**
 * 抓取数据
 * @return {[type]} [description]
 */
function fetchBrands() {
	if (fetching) {
		return;
	} else {
		fetching = true;
	}
	var senddata = {
		brandcat: global_brandcat,
		start: page,
		count: pagecount
	};
	var sendparams = {
		data: senddata,
		url: global_fetchdataurl
	};
	FetchBrand.FetchBrandDataInfo('brands', sendparams, function(data) {
		var frag = Mustache.render(template, {
			brandList: data.data.brandList
		});
		window.global_totalcount = data.data.total;
		$('#' + global_showpannel).append(frag)
		var swiper = $(frag).find('.swiper-container');
		swiper = swiper.prevObject;
		for (var i = 0; i < swiper.length; i++) {
			(function(id) {
				var swiperid = swiper[id].getAttribute('id');
				new Swiper('.swiper-container' + swiperid, {
					pagination: '.swiper-pagination' + swiperid,
					lazyLoading: false,
					lazyLoadingInPrevNext: false,
					loop: false,
					autoplay: false
				});
			})(i)
		}
		fetching = false;
		hideLoadPreview();
		page += pagecount;
	});
}
/**
 * 模拟滚动的函数
 * @param  {[type]} e     [description]
 * @param  {[type]} force [description]
 * @return {[type]}       [description]
 */
function handleScroll(e, force) {
	window.clearTimeout(global_timer);
	if (page >= window.global_totalcount) {
		return;
	}
	if (lastScrollY === window.scrollY) {
		global_timer = window.setTimeout(handleScroll, 100);
		return;
	} else {
		lastScrollY = window.scrollY;
	}
	scrollY = window.scrollY;
	innerHeight = window.innerHeight; //窗口高度
	topViewPort = scrollY - 6000;
	bottomViewPort = scrollY + innerHeight + 6000;
	if (window.scrollY + innerHeight >= document.body.offsetHeight) { //是否提前到底
		showLoadPreview();
		fetchBrands();
	}
	global_timer = window.setTimeout(handleScroll, 100);
}
/**抓取数据**/
function getBrand(catid) {
	page = 0;
	global_showpannel = 'showpannel' + catid;
	global_brandcat = catid;
	window.scrollTo(0, 0);
	fetchBrands();
	global_timer = window.setTimeout(handleScroll, 100);
}
/**
 * 初始化
 * @param  {[type]} ) {	window.setTimeout(handleScroll, 100);	fetchBrands();} [description]
 * @return {[type]}   [description]
 */
exports.init = function() {
	$.get('psTmplage', function(data) {
		template = data;
		Mustache.parse(template);
		lastScrollY = window.scrollY;
		global_timer = window.setTimeout(handleScroll, 500);
		$('.navitemwrapper').bind('tapone', function() {
			var nowActHeader = $(this),
				preActHeader = $('.newfestivalbrandheader').data('actheadtag') || $('.hasunderline'),
				tagId = nowActHeader.attr('tagid'),
				nowActContent = $('#showpannel' + tagId),
				preActContent = $('.newfestivalbrandheader').data('actcontenttag') || $('.newfestivalbrandwrapper .show');
			preActHeader.removeClass('hasunderline');
			nowActHeader.addClass('hasunderline');
			$('.newfestivalbrandheader').data('actheadtag', nowActHeader);
			preActContent.empty(); //清掉上个界面的数据
			parenttop = nowActContent.offset().top; //重置高度
			preActContent.removeClass('show').addClass('hide');
			nowActContent.removeClass('hide').addClass('show');
			$('.newfestivalbrandheader').data('actcontenttag', nowActContent);
			var catid = tagId;
			getBrand(catid);
		});
	});
}