navbar.js 5.2 KB
Newer Older
M
Minwe 已提交
1
define(function(require, exports, module) {
L
LUO Minhui 已提交
2 3 4
    require('core');

    var $ = window.Zepto,
M
Minwe 已提交
5 6 7 8
        UI = $.AMUI,
        QRCode = require('util.qrcode'),
        modal = require('ui.modal'),
        share = require('ui.share');
L
LUO Minhui 已提交
9 10

    var navbarInit = function() {
M
Minwe 已提交
11 12 13 14
        var $navBar = $('[data-am-widget="navbar"]');

        if (!$navBar.length) return;

M
Minwe 已提交
15 16 17 18 19
        var $win = $(window),
            $body = $('body'),
            $navBarNav = $navBar.find('.am-navbar-nav'),
            $navItems = $navBar.find('li'),
            navItemsCounter = $navItems.length;
M
Minwe 已提交
20
            configItems = $navBarNav.attr('class') && parseInt($navBarNav.attr('class').match(/sm-block-grid-(\d+)/)[1]) || 3,
M
Minwe 已提交
21
            navMinWidth = 60, //每个li最小宽度
M
Minwe 已提交
22 23 24 25 26 27 28 29 30 31 32
            offsetWidth = 16,
            $share = $navItems.filter('[data-am-navbar-share]'),
            $qrcode = $navItems.filter('[data-am-navbar-qrcode]'),
            activeStatus = 'am-active',
            $moreActions = $('<ul class="am-navbar-actions"></ul>', {
                id: UI.utils.generateGUID('am-navbar-actions')
            }),
            $moreLink = $('<li class="am-navbar-labels am-navbar-more"><a href="javascript: void(0);"><span class="am-icon-angle-up"></span><span class="am-navbar-label">更多</span></a></li>');

        // 如果有 Fix 的工具栏则设置 body 的 padding-bottom
        if ($navBar.css('position') == 'fixed') {
M
Minwe 已提交
33
            $body.addClass('am-with-fixed-navbar');
L
LUO Minhui 已提交
34 35 36
        }

        if ($qrcode.length) {
M
Minwe 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
            var qrId = 'am-navbar-qrcode';
            $qrModal = $('#' + qrId);

            if (!$qrModal.length) {
                var qrImg = $qrcode.attr('data-am-navbar-qrcode'),
                    $qrModal = $('<div class="am-modal am-modal-no-btn" id="">' +
                    '<div class="am-modal-dialog"><div class="am-modal-bd"></div></div>' +
                    '</div>', {
                        id: qrId
                    }),
                    $qrContainer = $qrModal.find('.am-modal-bd');

                // 判断上传自定义的二维码没有,否则生成二维码
                if (qrImg) {
                    $qrContainer.html('<img src="' + qrImg + '"/>');
                } else {
                    var qrnode = new QRCode({
                        render: 'canvas',
                        correctLevel: 0,
M
Minwe 已提交
56
                        text: window.location.href,
M
Minwe 已提交
57 58
                        width: 200,
                        height: 200,
M
Minwe 已提交
59 60 61 62 63
                        background: '#fff',
                        foreground: '#000'
                    });
                    $qrContainer.html(qrnode);
                }
L
LUO Minhui 已提交
64

M
Minwe 已提交
65
                $body.append($qrModal);
L
LUO Minhui 已提交
66 67
            }

M
Minwe 已提交
68 69
            $qrcode.on('click', function(e) {
                e.preventDefault();
M
Minwe 已提交
70
                $qrModal.modal();
L
LUO Minhui 已提交
71 72 73
            });
        }

M
Minwe 已提交
74 75
        if (navItemsCounter > configItems && navItemsCounter > calcSuiteItems()) {
            initActions();
L
LUO Minhui 已提交
76 77
        }

M
Minwe 已提交
78 79
        // console.log('NavItems: %d, config: %d, best: %d', navItemsCounter, configItems, calcSuiteItems());

M
Minwe 已提交
80 81
        function initActions() {
            $navBarNav.append($moreLink);
L
LUO Minhui 已提交
82

M
Minwe 已提交
83
            $navBarNav.find('li').not('.am-navbar-more').slice(calcSuiteItems() - 1).appendTo($moreActions);
L
LUO Minhui 已提交
84

M
Minwe 已提交
85 86
            // Append more actions
            $navBar.append($moreActions);
L
LUO Minhui 已提交
87 88
        }

M
Minwe 已提交
89 90 91 92 93 94 95 96 97
        function checkNavBarItems() {
            if (calcSuiteItems() >= navItemsCounter) {
                // 显示所有链接,隐藏 more
                $moreLink.hide();
                $moreActions.find('li').insertBefore($moreLink);
                return;
            }

            !$navBar.find('.am-navbar-actions').length && initActions();
L
LUO Minhui 已提交
98

M
Minwe 已提交
99 100 101 102 103 104 105 106 107
            $moreLink.show();

            if ($navBarNav.find('li').length < calcSuiteItems()) {
                $moreActions.find('li').slice(0, calcSuiteItems() - $navBarNav.find('li').length).insertBefore($moreLink);
            } else if ($navBarNav.find('li').length > calcSuiteItems()) {
                if ($moreActions.find('li').length) {
                    $navBarNav.find('li').not($moreLink).slice(calcSuiteItems() - 1).insertBefore($moreActions.find('li').first());
                } else {
                    $navBarNav.find('li').not($moreLink).slice(calcSuiteItems() - 1).appendTo($moreActions);
L
LUO Minhui 已提交
108
                }
M
Minwe 已提交
109
            }
L
LUO Minhui 已提交
110 111
        }

M
Minwe 已提交
112 113 114 115 116
        /**
         * 计算最适合显示的条目个数
         * @returns {number}
         */
        function calcSuiteItems() {
M
Minwe 已提交
117
            return Math.floor(($win.width() - offsetWidth) / navMinWidth);
L
LUO Minhui 已提交
118
        }
M
Minwe 已提交
119
        
M
Minwe 已提交
120
        $navBar.on('click.navbar.amui', '.am-navbar-more', function(e) {
M
Minwe 已提交
121
            e.preventDefault();
L
LUO Minhui 已提交
122

M
Minwe 已提交
123 124 125 126 127 128
            $moreLink[$moreActions.hasClass(activeStatus) ? 'removeClass' : 'addClass'](activeStatus);

            $moreActions.toggleClass(activeStatus);
        });

        if ($share.length) {
M
Minwe 已提交
129 130
            $share.on('click.navbar.amui', function(e) {
                e.preventDefault();
M
Minwe 已提交
131 132
                share.toggle();
            });
L
LUO Minhui 已提交
133
        }
M
Minwe 已提交
134

M
Minwe 已提交
135
        $win.on('resize.navbar.amui orientationchange.navbar.amui', UI.utils.debounce(checkNavBarItems, 150));
L
LUO Minhui 已提交
136 137
    };

M
Minwe 已提交
138 139

    // DOMContent ready
M
Minwe 已提交
140
    $(function() {
L
LUO Minhui 已提交
141 142 143 144 145
        navbarInit();
    });

    exports.init = navbarInit;
});