From c1a0542b9d253201f797b8e6f07f966091386e48 Mon Sep 17 00:00:00 2001 From: zhixin Date: Sun, 20 Sep 2020 01:03:34 +0800 Subject: [PATCH 01/39] Update eslint --- .eslintrc.js | 137 ++++---- src/bootstrap-table.js | 297 +++++++++++------- src/constants/index.js | 7 +- .../addrbar/bootstrap-table-addrbar.js | 14 +- .../bootstrap-table-auto-refresh.js | 8 +- .../cookie/bootstrap-table-cookie.js | 41 ++- .../copy-rows/bootstrap-table-copy-rows.js | 16 +- .../bootstrap-table-custom-view.js | 11 +- .../editable/bootstrap-table-editable.js | 13 +- .../export/bootstrap-table-export.js | 29 +- .../bootstrap-table-filter-control.js | 41 ++- src/extensions/filter-control/utils.js | 56 +++- .../bootstrap-table-fixed-columns.js | 8 +- .../group-by-v2/bootstrap-table-group-by.js | 29 +- .../key-events/bootstrap-table-key-events.js | 2 +- .../mobile/bootstrap-table-mobile.js | 15 +- .../bootstrap-table-multiple-sort.js | 46 +-- .../bootstrap-table-page-jump-to.js | 2 +- .../pipeline/bootstrap-table-pipeline.js | 31 +- src/extensions/print/bootstrap-table-print.js | 33 +- .../bootstrap-table-reorder-columns.js | 15 +- .../bootstrap-table-reorder-rows.js | 7 + .../resizable/bootstrap-table-resizable.js | 1 + .../bootstrap-table-sticky-header.js | 12 +- .../toolbar/bootstrap-table-toolbar.js | 23 +- .../treegrid/bootstrap-table-treegrid.js | 5 + src/themes/bootstrap-table/bootstrap-table.js | 9 +- src/themes/bulma/bootstrap-table-bulma.js | 1 + .../foundation/bootstrap-table-foundation.js | 4 + src/utils/index.js | 6 + src/virtual-scroll/index.js | 4 + 31 files changed, 588 insertions(+), 335 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 2ccf8ce519..9884773275 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,75 +1,100 @@ module.exports = { - 'root': true, - 'env': { - 'browser': true, - 'node': true + root: true, + env: { + browser: true, + node: true }, - 'parserOptions': { - 'parser': 'babel-eslint', - 'ecmaVersion': 2017, - 'sourceType': 'module' + parserOptions: { + parser: 'babel-eslint', + ecmaVersion: 2017, + sourceType: 'module' }, - 'extends': [ + extends: [ 'eslint:recommended' ], - 'rules': { - 'indent': ['error', + rules: { + 'array-bracket-newline': ['error', 'consistent'], + 'array-bracket-spacing': ['error', 'never'], + 'arrow-parens': ['error', 'as-needed'], + 'arrow-spacing': ['error', { after: true, before: true }], + 'block-spacing': 'error', + camelcase: 'off', + 'comma-dangle': ['error', 'never'], + 'comma-spacing': ['error', { after: true, before: false }], + 'default-case': 'error', + 'dot-location': ['error', 'property'], + 'eol-last': ['error', 'always'], + eqeqeq: 'error', + 'guard-for-in': 'warn', + indent: ['error', 2, { - 'SwitchCase': 1, - 'MemberExpression': 1, - 'ArrayExpression': 1, - 'FunctionDeclaration': {'parameters': 'first'}, - 'CallExpression': {'arguments': 1}, - 'ImportDeclaration': 'first', - 'ObjectExpression': 1 - } - ], + ArrayExpression: 1, + CallExpression: { arguments: 1 }, + FunctionDeclaration: { parameters: 'first' }, + ImportDeclaration: 'first', + MemberExpression: 1, + ObjectExpression: 1, + SwitchCase: 1 + }], + 'jsdoc/require-jsdoc': 0, + 'key-spacing': ['error', { afterColon: true, beforeColon: false, mode: 'strict' }], + 'keyword-spacing': ['error', { after: true, before: true }], 'linebreak-style': 'off', - 'quotes': ['error', 'single'], - 'semi': ['error', 'never'], - 'semi-style': ['error', 'last'], - 'semi-spacing': ['error', {'before': false, 'after': true}], - 'camelcase': 'off', - 'default-case': 'error', - 'no-new-func': 'error', - 'no-void': 'error', - 'array-bracket-spacing': ['error', 'never'], - 'no-tabs': 'error', - 'one-var': ['error', 'never'], - 'prefer-const': 'error', - 'no-trailing-spaces': 'error', - 'operator-assignment': ['error', 'always'], - 'dot-location': ['error', 'property'], - 'no-console': ['error', { allow: ['log', 'info', 'warn', 'error'] }], - 'no-else-return': ['error', {allowElseIf: false}], + 'lines-between-class-members': ['error', 'always'], + 'no-alert': 'error', 'no-case-declarations': 'off', - 'no-unused-vars': 'off', + 'no-console': ['warn', { allow: ['warn', 'error', 'trace'] }], + 'no-duplicate-imports': 'error', + 'no-else-return': ['error', { allowElseIf: false }], + 'no-lonely-if': 'error', 'no-multi-spaces': 'error', - 'valid-jsdoc': 'warn', - 'eqeqeq': 'error', - 'guard-for-in': 'off', 'no-multi-str': 'error', - 'no-return-await': 'error', + 'no-multiple-empty-lines': 'error', + 'no-new-func': 'error', + 'no-prototype-builtins': 'off', 'no-return-assign': 'error', + 'no-return-await': 'error', + 'no-sequences': 'error', + 'no-tabs': 'error', 'no-throw-literal': 'error', + 'no-trailing-spaces': 'error', 'no-undef-init': 'error', + 'no-unused-vars': 'error', 'no-use-before-define': 'warn', - 'key-spacing': ['error', {'beforeColon': false, 'afterColon': true, 'mode': 'strict'}], - 'keyword-spacing': ['error', {'before': true, 'after': true}], - 'space-before-blocks': ['error', {'functions': 'always', 'keywords': 'always', 'classes': 'always'}], - 'spaced-comment': ['error', 'always'], - 'space-infix-ops': 'error', - 'arrow-spacing': ['error', { 'before': true, 'after': true }], 'no-useless-constructor': 'warn', - 'comma-dangle': ['error', 'never'], - 'no-param-reassign': 'off', - 'space-before-function-paren': ["error", "always"], - 'no-prototype-builtins': 'off' + 'no-var': 'error', + 'no-void': 'error', + 'object-curly-spacing': ['error', 'always'], + 'object-shorthand': 'error', + 'one-var': ['error', 'never'], + 'operator-assignment': ['error', 'always'], + 'operator-linebreak': ['error', 'after'], + 'padding-line-between-statements': [ + 'error', + { blankLine: 'always', next: '*', prev: ['const', 'let', 'var'] }, + { blankLine: 'any', next: ['const', 'let', 'var'], prev: ['const', 'let', 'var'] }, + { blankLine: 'always', next: 'export', prev: '*' } + ], + 'prefer-const': 'error', + 'prefer-spread': 'error', + 'prefer-template': 'error', + 'quote-props': ['error', 'as-needed'], + quotes: ['error', 'single'], + semi: ['error', 'never'], + 'semi-spacing': ['error', { after: true, before: false }], + 'semi-style': ['error', 'last'], + 'sort-imports': 'off', + 'space-before-blocks': ['error', { classes: 'always', functions: 'always', keywords: 'always' }], + 'space-before-function-paren': ['error', 'always'], + 'space-in-parens': [2, 'never'], + 'space-infix-ops': 'error', + 'spaced-comment': ['error', 'always'], + 'switch-colon-spacing': 'error' }, - 'globals': { - '$': true, - 'jQuery': true, - 'adsbygoogle': true + globals: { + $: true, + jQuery: true, + adsbygoogle: true } } diff --git a/src/bootstrap-table.js b/src/bootstrap-table.js index bc2f2ec21d..80a43a1434 100644 --- a/src/bootstrap-table.js +++ b/src/bootstrap-table.js @@ -34,10 +34,12 @@ class BootstrapTable { initConstants () { const opts = this.options + this.constants = Constants.CONSTANTS this.constants.theme = $.fn.bootstrapTable.theme const buttonsPrefix = opts.buttonsPrefix ? `${opts.buttonsPrefix}-` : '' + this.constants.buttonsClass = [ opts.buttonsPrefix, buttonsPrefix + opts.buttonsClass, @@ -68,10 +70,10 @@ class BootstrapTable { } initContainer () { - const topPagination = ['top', 'both'].includes(this.options.paginationVAlign) - ? '
' : '' - const bottomPagination = ['bottom', 'both'].includes(this.options.paginationVAlign) - ? '
' : '' + const topPagination = ['top', 'both'].includes(this.options.paginationVAlign) ? + '
' : '' + const bottomPagination = ['bottom', 'both'].includes(this.options.paginationVAlign) ? + '
' : '' const loadingTemplate = Utils.calculateObjectValue(this.options, this.options.loadingTemplate, [this.options.formatLoadingMessage()]) @@ -131,7 +133,6 @@ class BootstrapTable { initTable () { const columns = [] - const data = [] this.$header = this.$el.find('>thead') if (!this.$header.length) { @@ -155,7 +156,7 @@ class BootstrapTable { } column.push($.extend({}, { title: $th.html(), - 'class': $th.attr('class'), + class: $th.attr('class'), titleTooltip: $th.attr('title'), rowspan: $th.attr('rowspan') ? +$th.attr('rowspan') : undefined, colspan: $th.attr('colspan') ? +$th.attr('colspan') : undefined @@ -197,6 +198,7 @@ class BootstrapTable { // if options.data is setting, do not process tbody and tfoot data if (!this.options.data.length) { const htmlData = Utils.trToData(this.columns, this.$el.find('>tbody>tr')) + if (htmlData.length) { this.options.data = htmlData this.fromHtml = true @@ -260,9 +262,10 @@ class BootstrapTable { const halign = Utils.sprintf('text-align: %s; ', column.halign ? column.halign : column.align) const align = Utils.sprintf('text-align: %s; ', column.align) let style = Utils.sprintf('vertical-align: %s; ', column.valign) - style += Utils.sprintf('width: %s; ', (column.checkbox || column.radio) && !width - ? (!column.showSelectTitle ? '36px' : undefined) - : (width ? width + unitWidth : undefined)) + + style += Utils.sprintf('width: %s; ', (column.checkbox || column.radio) && !width ? + (!column.showSelectTitle ? '36px' : undefined) : + (width ? width + unitWidth : undefined)) if (typeof column.fieldIndex === 'undefined' && !column.visible) { return @@ -306,9 +309,9 @@ class BootstrapTable { } html.push(` 0 ? ' data-not-first-th' : '', '>') - html.push(Utils.sprintf('
', this.options.sortable && column.sortable - ? 'sortable both' : '')) + html.push(Utils.sprintf('
', this.options.sortable && column.sortable ? + 'sortable both' : '')) let text = this.options.escape ? Utils.escapeHTML(column.title) : column.title const title = text + if (column.checkbox) { text = '' if (!this.options.singleSelect && this.options.checkboxHeader) { @@ -373,6 +377,7 @@ class BootstrapTable { this.$header.children().children().off('keypress').on('keypress', e => { if (this.options.sortable && $(e.currentTarget).data().sortable) { const code = e.keyCode || e.which + if (code === 13) { // Enter keycode this.onSort(e) } @@ -380,6 +385,7 @@ class BootstrapTable { }) const resizeEvent = Utils.getEventName('resize.bootstrap-table', this.$el.attr('id')) + $(window).off(resizeEvent) if (!this.options.showHeader || this.options.cardView) { this.$header.hide() @@ -398,6 +404,7 @@ class BootstrapTable { this.$selectAll.off('click').on('click', e => { e.stopPropagation() const checked = $(e.currentTarget).prop('checked') + this[checked ? 'checkAll' : 'uncheckAll']() this.updateSelected() }) @@ -472,6 +479,7 @@ class BootstrapTable { timeoutId = setTimeout(() => { this.$el.removeClass(this.options.sortClass) const index = this.$header.find(`[data-field="${this.options.sortName}"]`).index() + this.$el.find(`tr td:nth-child(${index + 1})`).addClass(this.options.sortClass) }, 250) } @@ -480,7 +488,7 @@ class BootstrapTable { } } - onSort ({type, currentTarget}) { + onSort ({ type, currentTarget }) { const $this = type === 'keypress' ? $(currentTarget) : $(currentTarget).parent() const $this_ = this.$header.find('th').eq($this.index()) @@ -605,6 +613,7 @@ class BootstrapTable { render: false, html: (() => { const html = [] + html.push(`
- ${this.constants.html.pageDropdown[0]}`] + ${this.constants.html.pageDropdown[0]}` + ] pageList.forEach((page, i) => { if (!opts.smartDisplay || i === 0 || pageList[i - 1] < opts.totalRows) { let active + if (allSelected) { active = page === opts.formatAllRows() ? this.constants.classes.dropdownActive : '' } else { @@ -1197,6 +1231,7 @@ class BootstrapTable { if (from > 1) { let max = opts.paginationPagesBySide + if (max >= from) max = from - 1 for (i = 1; i <= max; i++) { html.push(pageItem(i)) @@ -1204,18 +1239,16 @@ class BootstrapTable { if ((from - 1) === max + 1) { i = from - 1 html.push(pageItem(i)) - } else { - if ((from - 1) > max) { - if ( - (from - opts.paginationPagesBySide * 2) > opts.paginationPagesBySide && + } else if ((from - 1) > max) { + if ( + (from - opts.paginationPagesBySide * 2) > opts.paginationPagesBySide && opts.paginationUseIntermediate - ) { - i = Math.round(((from - middleSize) / 2) + middleSize) - html.push(pageItem(i, ' page-intermediate')) - } else { - html.push(Utils.sprintf(this.constants.html.paginationItem, - ' page-first-separator disabled', '', '...')) - } + ) { + i = Math.round(((from - middleSize) / 2) + middleSize) + html.push(pageItem(i, ' page-intermediate')) + } else { + html.push(Utils.sprintf(this.constants.html.paginationItem, + ' page-first-separator disabled', '', '...')) } } } @@ -1226,22 +1259,21 @@ class BootstrapTable { if (this.totalPages > to) { let min = this.totalPages - (opts.paginationPagesBySide - 1) + if (to >= min) min = to + 1 if ((to + 1) === min - 1) { i = to + 1 html.push(pageItem(i)) - } else { - if (min > (to + 1)) { - if ( - (this.totalPages - to) > opts.paginationPagesBySide * 2 && + } else if (min > (to + 1)) { + if ( + (this.totalPages - to) > opts.paginationPagesBySide * 2 && opts.paginationUseIntermediate - ) { - i = Math.round(((this.totalPages - middleSize - to) / 2) + to) - html.push(pageItem(i, ' page-intermediate')) - } else { - html.push(Utils.sprintf(this.constants.html.paginationItem, - ' page-last-separator disabled', '', '...')) - } + ) { + i = Math.round(((this.totalPages - middleSize - to) / 2) + to) + html.push(pageItem(i, ' page-intermediate')) + } else { + html.push(Utils.sprintf(this.constants.html.paginationItem, + ' page-last-separator disabled', '', '...')) } } @@ -1257,6 +1289,7 @@ class BootstrapTable { const dropupClass = ['bottom', 'both'].includes(opts.paginationVAlign) ? ` ${this.constants.classes.dropup}` : '' + this.$pagination.last().find('.page-list > span').addClass(dropupClass) if (!opts.onlyInfoPagination) { @@ -1325,8 +1358,8 @@ class BootstrapTable { $this.parent().addClass(this.constants.classes.dropdownActive) .siblings().removeClass(this.constants.classes.dropdownActive) - this.options.pageSize = $this.text().toUpperCase() === this.options.formatAllRows().toUpperCase() - ? this.options.formatAllRows() : +$this.text() + this.options.pageSize = $this.text().toUpperCase() === this.options.formatAllRows().toUpperCase() ? + this.options.formatAllRows() : +$this.text() this.$toolbar.find('.page-size').text(this.options.pageSize) this.updatePagination(event) @@ -1365,6 +1398,7 @@ class BootstrapTable { return false } + // eslint-disable-next-line no-unused-vars initRow (item, i, data, trFragments) { const html = [] let style = {} @@ -1510,6 +1544,7 @@ class BootstrapTable { } if (cellStyle.css) { const csses_ = [] + for (const [key, value] of Object.entries(cellStyle.css)) { csses_.push(`${key}: ${value}`) } @@ -1520,7 +1555,7 @@ class BootstrapTable { this.header.formatters[j], [value_, item, i, field], value_) if (this.searchText !== '' && this.options.searchHighlight) { - value = Utils.calculateObjectValue(column, column.searchHighlightFormatter, [value, this.searchText], value.replace(new RegExp('(' + this.searchText + ')', 'gim'), '$1')) + value = Utils.calculateObjectValue(column, column.searchHighlightFormatter, [value, this.searchText], value.replace(new RegExp(`(${ this.searchText })`, 'gim'), '$1')) } if (item[`_${field}_data`] && !Utils.isEmptyObject(item[`_${field}_data`])) { @@ -1543,9 +1578,9 @@ class BootstrapTable { const isDisabled = !column.checkboxEnabled || (value && value.disabled) text = [ - this.options.cardView - ? `
` - : ``, + this.options.cardView ? + `
` : + ``, `