Blame view

public/js/channel/cate.page.js 2.88 KB
ccbikai authored
1 2 3 4 5 6
/**
 * 分类
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/10/14
 */
lijing authored
7 8
let $ = require('yoho-jquery');
let FastClick = require('yoho-fastclick');
ccbikai authored
9
lijing authored
10
let $nav = $('.category-nav'),
沈志敏 authored
11
    $search = $('#search-input'),
ccbikai authored
12 13 14 15 16
    $categoryContainer = $('.category-container'),
    $contents = $categoryContainer.children('.content'),
    $subLevelItem = $categoryContainer.find('.sub-level li'),
    $primaryItem = $categoryContainer.find('.primary-level li');
17
require('common');
ccbikai authored
18
19 20
$(function() {
    FastClick.attach(document.body);
21
});
22
沈志敏 authored
23
function resetHeight() {
lijing authored
24
    let h = document.body.scrollHeight - $search.outerHeight() - $nav.outerHeight();
25 26

    $contents.height(h);
沈志敏 authored
27 28 29 30
}
resetHeight();

$search.focus(function() {
ccbikai authored
31 32
    $(this).blur();
});
ccbikai authored
33
$nav.on('contextmenu', function() {
ccbikai authored
34 35
    return false;
});
ccbikai authored
36
$('.category-container').on('contextmenu', function() {
ccbikai authored
37 38 39 40
    return false;
});

$nav.on('touchend touchcancel', function(e) {
lijing authored
41
    let $this = $(e.target).closest('li'),
ccbikai authored
42
        selector = '.' + $this.data('channel');
ccbikai authored
43 44 45 46 47 48 49 50

    if ($this.hasClass('focus')) {
        return;
    }

    $nav.find('li.focus').removeClass('focus');
    $this.addClass('focus');
ccbikai authored
51 52
    $contents.addClass('hide');
    $contents.filter(selector).removeClass('hide');
ccbikai authored
53 54
});
55
$categoryContainer.on('click', function(e) {
lijing authored
56
    let $this = $(e.target),
ccbikai authored
57 58 59 60
        $subLevel,
        $cur, index;

    $cur = $this.closest('.p-level-item');
郭成尧 authored
61
郭成尧 authored
62 63
    // 第一次单击右则空白处,出现冒泡
    if ($(e.target).closest('.sub-level').length <= 0) {
郭成尧 authored
64 65 66
        e.preventDefault();
    }
67 68 69 70 71 72 73 74
    if ($this[0] && $this[0].tagName === 'LI' && $this.find('a').attr('href')) {
        location.href = $this.find('a').attr('href');
    }

    if ($this[0] && $this[0].tagName === 'A' && $this.attr('href')) {
        location.href = $this.attr('href');
    }
ccbikai authored
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    if ($cur.length > 0) {
        index = $cur.index();
        $subLevel = $this.closest('.content').find('.sub-level');

        if ($this.hasClass('focus')) {
            return;
        }

        $this.closest('.primary-level').children('.focus').removeClass('focus');
        $this.addClass('focus');

        $subLevel.not('.hide').addClass('hide');
        $subLevel.eq(index).removeClass('hide');
    }
});

$categoryContainer.find('.primary-level').on('touchstart touchend touchcancel', 'li', function() {
    $primaryItem.removeClass('highlight');
    $(this).addClass('highlight');
}).on('touchend touchcancel', 'li', function() {
    $(this).removeClass('highlight');
});

$categoryContainer.find('.sub-level').on('touchstart', 'li', function() {
    $subLevelItem.removeClass('highlight');
    $(this).addClass('highlight');
}).on('touchend touchcancel', 'li', function() {
    $(this).removeClass('highlight');
});

$nav.on('touchstart', 'li', function() {
    $nav.find('li').removeClass('bytouch');
    $(this).addClass('bytouch');
}).on('touchend touchcancel', 'li', function() {
    $nav.find('li').removeClass('bytouch');
});