仰望星辰工作室

better-staridc-MNBT

better-staridc-MNBT/ imsetes/js/bootstrap-table/extensions/mobile/bootstrap-table-mobile.js 3.0 KB · 123 行 原始文件
Z zfhsh first commit 2 个月前
1/**
2 * @author: Dennis Hernández
3 * @webSite: http://djhvscf.github.io/Blog
4 * @update zhixin wen <wenzhixin2010@gmail.com>
5 */
6
7const debounce = (func, wait) => {
8 let timeout = 0
9 return (...args) => {
10 const later = () => {
11 timeout = 0
12 func(...args)
13 }
14 clearTimeout(timeout)
15 timeout = setTimeout(later, wait)
16 }
17}
18
19$.extend($.fn.bootstrapTable.defaults, {
20 mobileResponsive: false,
21 minWidth: 562,
22 minHeight: undefined,
23 heightThreshold: 100, // just slightly larger than mobile chrome's auto-hiding toolbar
24 checkOnInit: true,
25 columnsHidden: []
26})
27
28$.BootstrapTable = class extends $.BootstrapTable {
29 init (...args) {
30 super.init(...args)
31
32 if (!this.options.mobileResponsive || !this.options.minWidth) {
33 return
34 }
35
36 if (this.options.minWidth < 100 && this.options.resizable) {
37 console.info('The minWidth when the resizable extension is active should be greater or equal than 100')
38 this.options.minWidth = 100
39 }
40
41 let old = {
42 width: $(window).width(),
43 height: $(window).height()
44 }
45
46 $(window).on('resize orientationchange', debounce(() => {
47 // reset view if height has only changed by at least the threshold.
48 const width = $(window).width()
49 const height = $(window).height()
50 const $activeElement = $(document.activeElement)
51
52 if ($activeElement.length && ['INPUT', 'SELECT', 'TEXTAREA'].includes($activeElement.prop('nodeName'))) {
53 return
54 }
55
56 if (
57 Math.abs(old.height - height) > this.options.heightThreshold ||
58 old.width !== width
59 ) {
60 this.changeView(width, height)
61 old = {
62 width,
63 height
64 }
65 }
66 }, 200))
67
68 if (this.options.checkOnInit) {
69 const width = $(window).width()
70 const height = $(window).height()
71 this.changeView(width, height)
72 old = {
73 width,
74 height
75 }
76 }
77 }
78
79 conditionCardView () {
80 this.changeTableView(false)
81 this.showHideColumns(false)
82 }
83
84 conditionFullView () {
85 this.changeTableView(true)
86 this.showHideColumns(true)
87 }
88
89 changeTableView (cardViewState) {
90 this.options.cardView = cardViewState
91 this.toggleView()
92 }
93
94 showHideColumns (checked) {
95 if (this.options.columnsHidden.length > 0) {
96 this.columns.forEach(column => {
97 if (this.options.columnsHidden.includes(column.field)) {
98 if (column.visible !== checked) {
99 this._toggleColumn(this.fieldsColumnsIndex[column.field], checked, true)
100 }
101 }
102 })
103 }
104 }
105
106 changeView (width, height) {
107 if (this.options.minHeight) {
108 if ((width <= this.options.minWidth) && (height <= this.options.minHeight)) {
109 this.conditionCardView()
110 } else if ((width > this.options.minWidth) && (height > this.options.minHeight)) {
111 this.conditionFullView()
112 }
113 } else {
114 if (width <= this.options.minWidth) {
115 this.conditionCardView()
116 } else if (width > this.options.minWidth) {
117 this.conditionFullView()
118 }
119 }
120
121 this.resetView()
122 }
123}