分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-01-16 5884c9023393061afbe6d3d6e709e53e672ddde8
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/**
 * bootstrap-paginator.js v0.5
 * --
 * Copyright 2013 Yun Lai <lyonlai1984@gmail.com>
 * --
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
(function ($) {
 
    "use strict"; // jshint ;_;
 
 
    /* Paginator PUBLIC CLASS DEFINITION
     * ================================= */
 
    /**
     * Bootstrap分页构造函数
     *
     * @param 分页的容器对象
     * @param 分页的相关配置
     *
     * */
    var BootstrapPaginator = function (element, options) {
        this.init(element, options);
    },
        old = null;
 
    BootstrapPaginator.prototype = {
 
        /**
         * 分页初始化, 把element和options作为参数
         *
         * @param element 分页的容器对象
         * @param options 分页的相关配置
         *
         * */
        init: function (element, options) {
 
            this.$element = $(element);
 
            var version = (options && options.bootstrapMajorVersion) ? options.bootstrapMajorVersion : $.fn.bootstrapPaginator.defaults.bootstrapMajorVersion,
                id = this.$element.attr("id");
 
            if (version === 2 && !this.$element.is("div")) {
 
                throw "在Bootstrap2中,必须使用div作为分页的容器";
            } else if (version > 2 && !this.$element.is("ul")) {
                throw "在Bootstrap3中,必须使用ul作为分页的容器"
            }
 
 
 
            this.currentPage = 1;
 
            this.lastPage = 1;
 
            this.setOptions(options);
 
            this.initialized = true;
        },
 
        /**
         * 更新分页element的属性
         *
         * @param 分页的相关配置
         * */
        setOptions: function (options) {
 
            this.options = $.extend({}, (this.options || $.fn.bootstrapPaginator.defaults), options);
 
            this.totalPages = parseInt(this.options.totalPages, 10);  //设置总页数
            this.numberOfPages = parseInt(this.options.numberOfPages, 10); //设置要显示的页数
 
            //设置总页数后移除当前页设置,否则会导致页面异常
            if (options && typeof (options.currentPage)  !== 'undefined') {
 
                this.setCurrentPage(options.currentPage);
            }
 
            this.listen();
 
            //呈现分页
            this.render();
 
            if (!this.initialized && this.lastPage !== this.currentPage) {
                this.$element.trigger("page-changed", [this.lastPage, this.currentPage]);
            }
 
        },
 
        /**
         * 设置时间监听器,如果可用的话,页面点击和切换事件是关联的
         *
         * */
        listen: function () {
 
            this.$element.off("page-clicked");
 
            this.$element.off("page-changed");// 卸载元素事件
 
            if (typeof (this.options.onPageClicked) === "function") {
                this.$element.bind("page-clicked", this.options.onPageClicked);
            }
 
            if (typeof (this.options.onPageChanged) === "function") {
                this.$element.on("page-changed", this.options.onPageChanged);
            }
 
            this.$element.bind("page-clicked", this.onPageClicked);
        },
 
 
        /**
         *
         *  销毁paginator元素,首先卸载事件,然后清空里面的内容。
         *
         * */
        destroy: function () {
 
            this.$element.off("page-clicked");
 
            this.$element.off("page-changed");
 
            this.$element.removeData('bootstrapPaginator');
 
            this.$element.empty();
 
        },
 
        /**
         * 显示分页
         *
         * */
        show: function (page) {
 
            this.setCurrentPage(page);
 
            this.render();
 
            if (this.lastPage !== this.currentPage) {
                this.$element.trigger("page-changed", [this.lastPage, this.currentPage]);
            }
        },
 
        /**
         * 显示下一页
         *
         * */
        showNext: function () {
            var pages = this.getPages();
 
            if (pages.next) {
                this.show(pages.next);
            }
 
        },
 
        /**
         * 显示上一页
         *
         * */
        showPrevious: function () {
            var pages = this.getPages();
 
            if (pages.prev) {
                this.show(pages.prev);
            }
 
        },
 
        /**
         * 显示第一页
         *
         * */
        showFirst: function () {
            var pages = this.getPages();
 
            if (pages.first) {
                this.show(pages.first);
            }
 
        },
 
        /**
         * 显示最后一页
         *
         * */
        showLast: function () {
            var pages = this.getPages();
 
            if (pages.last) {
                this.show(pages.last);
            }
 
        },
 
        /**
         * 页码单击处理程序,单击页码时,将当前页更改为相应的页面,并触发监听器的pageClick事件
         *
         *
         * */
        onPageItemClicked: function (event) {
 
            var type = event.data.type,
                page = event.data.page;
 
            this.$element.trigger("page-clicked", [event, type, page]);
 
        },
        /**
         * 点击跳转事件
         * @param event
         */
        onPageItemJumped : function (event) {
            var type = event.data.type;
            var value = this.$element.find('input').val();
            var page = parseInt(value,10);
            if(!isNaN(value) && page > 0 && page != this.currentPage && page < this.totalPages){
                this.$element.trigger("page-clicked", [event, type, page]);
            }
        },
        onPageClicked: function (event, originalEvent, type, page) {
 
            //显示相应页面,并在事件返回之前检查新建的页面
 
            var currentTarget = $(event.currentTarget);
 
            switch (type) {
            case "first":
                currentTarget.bootstrapPaginator("showFirst");
                break;
            case "prev":
                currentTarget.bootstrapPaginator("showPrevious");
                break;
            case "next":
                currentTarget.bootstrapPaginator("showNext");
                break;
            case "last":
                currentTarget.bootstrapPaginator("showLast");
                break;
            case "page":
                currentTarget.bootstrapPaginator("show", page);
                break;
            case "jump":
                currentTarget.bootstrapPaginator("show", page);
                break;
            }
 
        },
 
        /**
         * 根据内部属性和设置呈现分页(生成分页主函数,要修改分页的生成主要就在这里)
         *
         *
         * */
        render: function () {
 
            //获取容器class并将其添加到容器中
            var containerClass = this.getValueFromOption(this.options.containerClass, this.$element),
                size = this.options.size || "normal",
                alignment = this.options.alignment || "left",
                pages = this.getPages(),
                listContainer = this.options.bootstrapMajorVersion === 2 ? $("<ul></ul>") : this.$element,
                listContainerClass = this.options.bootstrapMajorVersion === 2 ? this.getValueFromOption(this.options.listContainerClass, listContainer) : null,
                first = null,
                prev = null,
                next = null,
                last = null,
                p = null,
                i = 0;
 
 
            this.$element.prop("class", "");
 
            this.$element.addClass("pagination");
 
            switch (size.toLowerCase()) {
            case "large":
            case "small":
            case "mini":
                this.$element.addClass($.fn.bootstrapPaginator.sizeArray[this.options.bootstrapMajorVersion][size.toLowerCase()]);
                break;
            default:
                break;
            }
 
            if (this.options.bootstrapMajorVersion === 2) {
                switch (alignment.toLowerCase()) {
                case "center":
                    this.$element.addClass("pagination-centered");
                    break;
                case "right":
                    this.$element.addClass("pagination-right");
                    break;
                default:
                    break;
                }
            }
 
 
            this.$element.addClass(containerClass);
 
            //清空最外面的容器,然后把列表添加进容器中。
            this.$element.empty();
 
            if (this.options.bootstrapMajorVersion === 2) {
                this.$element.append(listContainer);
 
                listContainer.addClass(listContainerClass);
            }
 
            //更新页面元素引用
            this.pageRef = [];
 
            if (pages.first) {//如果是第1页
                first = this.buildPageItem("first", pages.first);
 
                if (first) {
                    listContainer.append(first);
                }
 
            }
 
            if (pages.prev) {//如果是上一页
 
                prev = this.buildPageItem("prev", pages.prev);
 
                if (prev) {
                    listContainer.append(prev);
                }
 
            }
 
 
            for (= 0; i < pages.length; i = i + 1) {//填写数字
 
                p = this.buildPageItem("page", pages[i]);
 
                if (p) {
                    listContainer.append(p);
                }
            }
 
            if (pages.next) {//如果是下一页
 
                next = this.buildPageItem("next", pages.next);
 
                if (next) {
                    listContainer.append(next);
                }
            }
 
            if (pages.last) {//如果是最后一页
 
                last = this.buildPageItem("last", pages.last);
 
                if (last) {
                    listContainer.append(last);
                }
            }
            //新加的
            var itemCustom = $("<li></li>");//创建一个容器
            var itemHtml = "<span><input type='text' style='width: 30px;height: 20px;' value='"+this.currentPage+"'/>/"+this.totalPages+"</span>";
            itemCustom.append(itemHtml);
            listContainer.append(itemCustom);
            var itemlast = $("<li></li>");//创建一个‘跳转’的span
            var itemTz = $("<span>跳转</span>").on("click",null,{type: 'jump'},$.proxy(this.onPageItemJumped, this));//绑定点击事件
            itemlast.append(itemTz);
            listContainer.append(itemlast);
        },
 
        /**
         *
         * 根据给定的类型和页码创建page
         *
         * @param page 页码
         * @param type 类型(first, prev, page, next, last)
         *
         * @return 构造的页面元素
         * */
        buildPageItem: function (type, page) {
 
            var itemContainer = $("<li></li>"),//创建一个容器
                itemContent = $("<a></a>"),//创建一个内容
                text = "",
                title = "",
                itemContainerClass = this.options.itemContainerClass(type, page, this.currentPage),
                itemContentClass = this.getValueFromOption(this.options.itemContentClass, type, page, this.currentPage),
                tooltipOpts = null;
 
 
            switch (type) {
 
            case "first":
                if (!this.getValueFromOption(this.options.shouldShowPage, type, page, this.currentPage)) { return; }
                text = this.options.itemTexts(type, page, this.currentPage);
                title = this.options.tooltipTitles(type, page, this.currentPage);
                break;
            case "last":
                if (!this.getValueFromOption(this.options.shouldShowPage, type, page, this.currentPage)) { return; }
                text = this.options.itemTexts(type, page, this.currentPage);
                title = this.options.tooltipTitles(type, page, this.currentPage);
                break;
            case "prev":
                if (!this.getValueFromOption(this.options.shouldShowPage, type, page, this.currentPage)) { return; }
                text = this.options.itemTexts(type, page, this.currentPage);
                title = this.options.tooltipTitles(type, page, this.currentPage);
                break;
            case "next":
                if (!this.getValueFromOption(this.options.shouldShowPage, type, page, this.currentPage)) { return; }
                text = this.options.itemTexts(type, page, this.currentPage);
                title = this.options.tooltipTitles(type, page, this.currentPage);
                break;
            case "page":
                if (!this.getValueFromOption(this.options.shouldShowPage, type, page, this.currentPage)) { return; }
                text = this.options.itemTexts(type, page, this.currentPage);
                title = this.options.tooltipTitles(type, page, this.currentPage);
                break;
            }
 
            itemContainer.addClass(itemContainerClass).append(itemContent);
 
            itemContent.addClass(itemContentClass).html(text).on("click", null, {type: type, page: page}, $.proxy(this.onPageItemClicked, this));
 
            if (this.options.pageUrl) {
                itemContent.attr("href", this.getValueFromOption(this.options.pageUrl, type, page, this.currentPage));
            }
 
            if (this.options.useBootstrapTooltip) {
                tooltipOpts = $.extend({}, this.options.bootstrapTooltipOptions, {title: title});
 
                itemContent.tooltip(tooltipOpts);
            } else {
                itemContent.attr("title", title);
            }
 
            return itemContainer;
 
        },
 
        setCurrentPage: function (page) {
            if (page > this.totalPages || page < 1) {// 如果当前页码超出范围,则抛出异常
 
                throw "页码超出范围";
 
            }
 
            this.lastPage = this.currentPage;
 
            this.currentPage = parseInt(page, 10);
 
        },
 
        /**
         * 获取表示页面对象当前状态的数组。
         *
         * @return 输出具有first, prev, next, last和中间页码的对象.
         * */
        getPages: function () {
 
            var totalPages = this.totalPages,//通过总记录获取或计算总页数
                pageStart = (this.currentPage % this.numberOfPages === 0) ? (parseInt(this.currentPage / this.numberOfPages, 10) - 1) * this.numberOfPages + 1 : parseInt(this.currentPage / this.numberOfPages, 10) * this.numberOfPages + 1,//calculates the start page.
                output = [],
                i = 0,
                counter = 0;
 
            pageStart = pageStart < 1 ? 1 : pageStart;//检查页面的开始范围,看它是否小于1。
 
            for (= pageStart, counter = 0; counter < this.numberOfPages && i <= totalPages; i = i + 1, counter = counter + 1) {//填写页面
                output.push(i);
            }
 
            output.first = 1;//在当前页面离开第一页时添加first
 
            if (this.currentPage > 1) {// 在当前页面离开第一页时添加prev
                output.prev = this.currentPage - 1;
            } else {
                output.prev = 1;
            }
 
            if (this.currentPage < totalPages) {// 当前页不是最后一页时添加next
                output.next = this.currentPage + 1;
            } else {
                output.next = totalPages;
            }
 
            output.last = totalPages;// 在当前页面没有达到最后一页时添加last
 
            output.current = this.currentPage;//标记当前页面
 
            output.total = totalPages;
 
            output.numberOfPages = this.options.numberOfPages;
 
            return output;
 
        },
 
        /**
         * 从选项中获取值,这是为了处理其中value是函数的返回值的情况。
         *
         * @return 混合值取决于参数的类型,如果给定参数是函数,则返回评估结果。 否则参数本身将返回。
         * */
        getValueFromOption: function (value) {
 
            var output = null,
                args = Array.prototype.slice.call(arguments, 1);
 
            if (typeof value === 'function') {
                output = value.apply(this, args);
            } else {
                output = value;
            }
 
            return output;
 
        }
 
    };
 
 
    /* TYPEAHEAD PLUGIN DEFINITION
     * =========================== */
 
    old = $.fn.bootstrapPaginator;
 
    $.fn.bootstrapPaginator = function (option) {
 
        var args = arguments,
            result = null;
 
        $(this).each(function (index, item) {
            var $this = $(item),
                data = $this.data('bootstrapPaginator'),
                options = (typeof option !== 'object') ? null : option;
 
            if (!data) {
                data = new BootstrapPaginator(this, options);
 
                $this = $(data.$element);
 
                $this.data('bootstrapPaginator', data);
 
                return;
            }
 
            if (typeof option === 'string') {
 
                if (data[option]) {
                    result = data[option].apply(data, Array.prototype.slice.call(args, 1));
                } else {
                    throw "Method " + option + " does not exist";
                }
 
            } else {
                result = data.setOptions(option);
            }
        });
 
        return result;
 
    };
 
    $.fn.bootstrapPaginator.sizeArray = {
 
        "2": {
            "large": "pagination-large",
            "small": "pagination-small",
            "mini": "pagination-mini"
        },
        "3": {
            "large": "pagination-lg",
            "small": "pagination-sm",
            "mini": ""
        }
 
    };
 
    $.fn.bootstrapPaginator.defaults = {
        containerClass: "",
        size: "normal",
        alignment: "left",
        bootstrapMajorVersion: 2,
        listContainerClass: "",
        itemContainerClass: function (type, page, current) {
            return (page === current) ? "active" : "";
        },
        itemContentClass: function (type, page, current) {
            return "";
        },
        currentPage: 1,
        numberOfPages: 5,
        totalPages: 1,
        pageUrl: function (type, page, current) {
            return null;
        },
        onPageClicked: null,
        onPageChanged: null,
        useBootstrapTooltip: false,
        shouldShowPage: function (type, page, current) {
 
            var result = true;
 
            switch (type) {
            case "first":
                result = (current !== 1);
                break;
            case "prev":
                result = (current !== 1);
                break;
            case "next":
                result = (current !== this.totalPages);
                break;
            case "last":
                result = (current !== this.totalPages);
                break;
            case "page":
                result = true;
                break;
            }
 
            return result;
 
        },
        itemTexts: function (type, page, current) {
            switch (type) {
            case "first":
                return "首页";
            case "prev":
                return "上一页";
            case "next":
                return "下一页";
            case "last":
                return "末页";
            case "page":
                return page;
            }
        },
        tooltipTitles: function (type, page, current) {
 
            switch (type) {
            case "first":
                return "首页";
            case "prev":
                return "上一页";
            case "next":
                return "下一页";
            case "last":
                return "末页";
            case "page":
                return (page === current) ? "当前是第" + page + "页": "第" + page + "页";
            }
        },
        bootstrapTooltipOptions: {
            animation: true,
            html: true,
            placement: 'top',
            selector: false,
            title: "",
            container: false
        }
    };
 
    $.fn.bootstrapPaginator.Constructor = BootstrapPaginator;
 
 
 
}(window.jQuery));