chengkun
2025-04-18 1bb985f32f2efe0f9dd69f3cf29a1c809b1cf96d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
 * IndexedList
 * 类似联系人应用中的联系人列表,可以按首字母分组
 * 右侧的字母定位工具条,可以快速定位列表位置
 * varstion 1.0.0
 * by Houfeng
 * Houfeng@DCloud.io
 **/
 
(function($, window, document) {
 
    var classSelector = function(name) {
        return '.' + $.className(name);
    }
 
    var IndexedList = $.IndexedList = $.Class.extend({
        /**
         * 通过 element 和 options 构造 IndexedList 实例
         **/
        init: function(holder, options) {
            var self = this;
            self.options = options || {};
            self.box = holder;
            if (!self.box) {
                throw "实例 IndexedList 时需要指定 element";
            }
            self.createDom();
            self.findElements();
            self.caleLayout();
            self.bindEvent();
        },
        createDom: function() {
            var self = this;
            self.el = self.el || {};
            //styleForSearch 用于搜索,此方式能在数据较多时获取很好的性能
            self.el.styleForSearch = document.createElement('style');
            (document.head || document.body).appendChild(self.el.styleForSearch);
        },
        findElements: function() {
            var self = this;
            self.el = self.el || {};
            self.el.search = self.box.querySelector(classSelector('indexed-list-search'));
            self.el.searchInput = self.box.querySelector(classSelector('indexed-list-search-input'));
            self.el.searchClear = self.box.querySelector(classSelector('indexed-list-search') + ' ' + classSelector('icon-clear'));
            self.el.bar = self.box.querySelector(classSelector('indexed-list-bar'));
            self.el.barItems = [].slice.call(self.box.querySelectorAll(classSelector('indexed-list-bar') + ' a'));
            self.el.inner = self.box.querySelector(classSelector('indexed-list-inner'));
            self.el.items = [].slice.call(self.box.querySelectorAll(classSelector('indexed-list-item')));
            self.el.liArray = [].slice.call(self.box.querySelectorAll(classSelector('indexed-list-inner') + ' li'));
            self.el.alert = self.box.querySelector(classSelector('indexed-list-alert'));
        },
        caleLayout: function() {
            var self = this;
            var withoutSearchHeight = (self.box.offsetHeight - self.el.search.offsetHeight) + 'px';
            self.el.bar.style.height = withoutSearchHeight;
            self.el.inner.style.height = withoutSearchHeight;
            var barItemHeight = ((self.el.bar.offsetHeight - 40) / self.el.barItems.length) + 'px';
            self.el.barItems.forEach(function(item) {
                item.style.height = barItemHeight;
                item.style.lineHeight = barItemHeight;
            });
        },
        scrollTo: function(group) {
            var self = this;
            var groupElement = self.el.inner.querySelector('[data-group="' + group + '"]');
            if (!groupElement || (self.hiddenGroups && self.hiddenGroups.indexOf(groupElement) > -1)) {
                return;
            }
            self.el.inner.scrollTop = groupElement.offsetTop;
        },
        bindBarEvent: function() {
            var self = this;
            var pointElement = null;
            var findStart = function(event) {
                if (pointElement) {
                    pointElement.classList.remove('active');
                    pointElement = null;
                }
                self.el.bar.classList.add('active');
                var point = event.changedTouches ? event.changedTouches[0] : event;
                pointElement = document.elementFromPoint(point.pageX, point.pageY);
                if (pointElement) {
                    var group = pointElement.innerText;
                    if (group && group.length == 1) {
                        pointElement.classList.add('active');
                        self.el.alert.innerText = group;
                        self.el.alert.classList.add('active');
                        self.scrollTo(group);
                    }
                }
                event.preventDefault();
            };
            var findEnd = function(event) {
                self.el.alert.classList.remove('active');
                self.el.bar.classList.remove('active');
                if (pointElement) {
                    pointElement.classList.remove('active');
                    pointElement = null;
                }
            };
            self.el.bar.addEventListener($.EVENT_MOVE, function(event) {
                findStart(event);
            }, false);
            self.el.bar.addEventListener($.EVENT_START, function(event) {
                findStart(event);
            }, false);
            document.body.addEventListener($.EVENT_END, function(event) {
                findEnd(event);
            }, false);
            document.body.addEventListener($.EVENT_CANCEL, function(event) {
                findEnd(event);
            }, false);
        },
        search: function(keyword) {
            var self = this;
            keyword = (keyword || '').toLowerCase();
            var selectorBuffer = [];
            var groupIndex = -1;
            var itemCount = 0;
            var liArray = self.el.liArray;
            var itemTotal = liArray.length;
            self.hiddenGroups = [];
            var checkGroup = function(currentIndex, last) {
                if (itemCount >= currentIndex - groupIndex - (last ? 0 : 1)) {
                    selectorBuffer.push(classSelector('indexed-list-inner li') + ':nth-child(' + (groupIndex + 1) + ')');
                    self.hiddenGroups.push(liArray[groupIndex]);
                };
                groupIndex = currentIndex;
                itemCount = 0;
            }
            liArray.forEach(function(item) {
                var currentIndex = liArray.indexOf(item);
                if (item.classList.contains($.className('indexed-list-group'))) {
                    checkGroup(currentIndex, false);
                } else {
                    var text = (item.innerText || '').toLowerCase();
                    var value = (item.getAttribute('data-value') || '').toLowerCase();
                    var tags = (item.getAttribute('data-tags') || '').toLowerCase();
                    if (keyword && text.indexOf(keyword) < 0 &&
                        value.indexOf(keyword) < 0 &&
                        tags.indexOf(keyword) < 0) {
                        selectorBuffer.push(classSelector('indexed-list-inner li') + ':nth-child(' + (currentIndex + 1) + ')');
                        itemCount++;
                    }
                    if (currentIndex >= itemTotal - 1) {
                        checkGroup(currentIndex, true);
                    }
                }
            });
            if (selectorBuffer.length >= itemTotal) {
                self.el.inner.classList.add('empty');
            } else if (selectorBuffer.length > 0) {
                self.el.inner.classList.remove('empty');
                self.el.styleForSearch.innerText = selectorBuffer.join(', ') + "{display:none;}";
            } else {
                self.el.inner.classList.remove('empty');
                self.el.styleForSearch.innerText = "";
            }
        },
        bindSearchEvent: function() {
            var self = this;
            self.el.searchInput.addEventListener('input', function() {
                var keyword = this.value;
                self.search(keyword);
            }, false);
            $(self.el.search).on('tap', classSelector('icon-clear'), function() {
                self.search('');
            }, false);
        },
        bindEvent: function() {
            var self = this;
            self.bindBarEvent();
            self.bindSearchEvent();
        }
    });
 
    //mui(selector).indexedList 方式
    $.fn.indexedList = function(options) {
        //遍历选择的元素
        this.each(function(i, element) {
            if (element.indexedList) return;
            element.indexedList = new IndexedList(element, options);
        });
        return this[0] ? this[0].indexedList : null;
    };
 
})(mui, window, document);