ソースを参照

Improve bootstrap-table.js, editable and export

zhixin 7 年 前
コミット
71d70e33db

+ 2 - 1
.eslintrc.js

@@ -63,7 +63,8 @@ module.exports = {
     'arrow-spacing': ['error', { 'before': true, 'after': true }],
     'no-useless-constructor': 'warn',
     'comma-dangle': ['error', 'never'],
-    'no-param-reassign': 'warn'
+    'no-param-reassign': 'warn',
+    "space-before-function-paren": ["error", "always"]
   },
   'globals': {
     '$': true,

+ 1 - 0
CHANGELOG.md

@@ -4,6 +4,7 @@ ChangeLog
 ### 1.13.1
 
 - feat(js): add `theadClasses` option to supoort bootstrap v4
+- feat(locale): rewrite all locale to ES6
 - feat(editable extension): rewrite bootstrap-table-editable to ES6
 - feat(filter-control extension): rewrite bootstrap-table-filter-control to ES6
 - fix(js): fix #4066, `getOptions` method remove data property

+ 19 - 20
src/bootstrap-table.js

@@ -117,8 +117,7 @@
       }
 
       for (let i = 0; i < columns.length; i++) {
-        for (let j = 0; j < columns[i].length; j++) {
-          const r = columns[i][j]
+        for (const r of columns[i]) {
           const rowspan = r.rowspan || 1
           const colspan = r.colspan || 1
           const index = flag[i].indexOf(false)
@@ -352,8 +351,8 @@
     icons: bootstrap.icons,
     customSearch: $.noop,
     customSort: $.noop,
-    ignoreClickToSelectOn (element) {
-      return ['A', 'BUTTON'].includes(element.tagName)
+    ignoreClickToSelectOn ({tagName}) {
+      return ['A', 'BUTTON'].includes(tagName)
     },
     rowStyle (row, index) {
       return {}
@@ -912,8 +911,8 @@
       }
 
       this.$selectAll = this.$header.find('[name="btSelectAll"]')
-      this.$selectAll.off('click').on('click', e => {
-        const checked = $(e.currentTarget).prop('checked')
+      this.$selectAll.off('click').on('click', ({currentTarget}) => {
+        const checked = $(currentTarget).prop('checked')
         this[checked ? 'checkAll' : 'uncheckAll']()
         this.updateSelected()
       })
@@ -1186,8 +1185,8 @@
         $keepOpen.find('li').off('click').on('click', e => {
           e.stopImmediatePropagation()
         })
-        $keepOpen.find('input').off('click').on('click', e => {
-          const $this = $(e.currentTarget)
+        $keepOpen.find('input').off('click').on('click', ({currentTarget}) => {
+          const $this = $(currentTarget)
 
           this.toggleColumn($this.val(), $this.prop('checked'), false)
           this.trigger('column-switch', $this.data('field'), $this.prop('checked'))
@@ -1872,8 +1871,8 @@
       }
 
       // click to select by column
-      this.$body.find('> tr[data-index] > td').off('click dblclick').on('click dblclick', (e) => {
-        const $td = $(e.currentTarget)
+      this.$body.find('> tr[data-index] > td').off('click dblclick').on('click dblclick', ({currentTarget, type, target}) => {
+        const $td = $(currentTarget)
         const $tr = $td.parent()
         const item = this.data[$tr.data('index')]
         const index = $td[0].cellIndex
@@ -1886,15 +1885,15 @@
           return
         }
 
-        this.trigger(e.type === 'click' ? 'click-cell' : 'dbl-click-cell', field, value, item, $td)
-        this.trigger(e.type === 'click' ? 'click-row' : 'dbl-click-row', item, $tr, field)
+        this.trigger(type === 'click' ? 'click-cell' : 'dbl-click-cell', field, value, item, $td)
+        this.trigger(type === 'click' ? 'click-row' : 'dbl-click-row', item, $tr, field)
 
         // if click to select - then trigger the checkbox/radio click
         if (
-          e.type === 'click' &&
+          type === 'click' &&
           this.options.clickToSelect &&
           column.clickToSelect &&
-          !this.options.ignoreClickToSelectOn(e.target)
+          !this.options.ignoreClickToSelectOn(target)
         ) {
           const $selectItem = $tr.find(Utils.sprintf('[name="%s"]', this.options.selectItemName))
           if ($selectItem.length) {
@@ -2131,7 +2130,7 @@
 
     trigger (_name, ...args) {
       const name = `${_name}.bs.table`
-      this.options[BootstrapTable.EVENTS[name]].apply(this.options, args)
+      this.options[BootstrapTable.EVENTS[name]](...args)
       this.$el.trigger($.Event(name), args)
 
       this.options.onAll(name, args)
@@ -2310,13 +2309,13 @@
       // TODO: it's probably better improving the layout than binding to scroll event
 
       this.trigger('scroll-body')
-      this.$tableBody.off('scroll').on('scroll', e => {
+      this.$tableBody.off('scroll').on('scroll', ({currentTarget}) => {
         if (this.options.showHeader && this.options.height) {
-          this.$tableHeader.scrollLeft($(e.currentTarget).scrollLeft())
+          this.$tableHeader.scrollLeft($(currentTarget).scrollLeft())
         }
 
         if (this.options.showFooter && !this.options.cardView) {
-          this.$tableFooter.scrollLeft($(e.currentTarget).scrollLeft())
+          this.$tableFooter.scrollLeft($(currentTarget).scrollLeft())
         }
       })
     }
@@ -3149,14 +3148,14 @@
 
       if (typeof option === 'string') {
         if (!allowedMethods.includes(option)) {
-          throw new Error('Unknown method: ' + option)
+          throw new Error(`Unknown method: ${option}`)
         }
 
         if (!data) {
           return
         }
 
-        value = data[option].apply(data, args)
+        value = data[option](...args)
 
         if (option === 'destroy') {
           $(el).removeData('bootstrap.table')

+ 9 - 11
src/extensions/editable/bootstrap-table-editable.js

@@ -47,9 +47,7 @@
         const editableDataPrefix = 'editable-'
         const processDataOptions = (key, value) => {
           // Replace camel case with dashes.
-          const dashKey = key.replace(/([A-Z])/g, $1 => {
-            return '-' + $1.toLowerCase()
-          })
+          const dashKey = key.replace(/([A-Z])/g, $1 => `-${$1.toLowerCase()}`)
           if (dashKey.indexOf(editableDataPrefix) === 0) {
             editableOptions[dashKey.replace(editableDataPrefix, 'data-')] = value
           }
@@ -113,21 +111,21 @@
           $element.editable(editableOpts)
         })
 
-        $field.off('save').on('save', (e, params) => {
-          const $this = $(e.currentTarget)
+        $field.off('save').on('save', ({currentTarget}, {submitValue}) => {
+          const $this = $(currentTarget)
           const data = this.getData()
           const index = $this.parents('tr[data-index]').data('index')
           const row = data[index]
           const oldValue = row[column.field]
 
-          $this.data('value', params.submitValue)
-          row[column.field] = params.submitValue
+          $this.data('value', submitValue)
+          row[column.field] = submitValue
           this.trigger('editable-save', column.field, row, oldValue, $this)
           this.resetFooter()
         })
 
-        $field.off('shown').on('shown', (e, editable) => {
-          const $this = $(e.currentTarget)
+        $field.off('shown').on('shown', ({currentTarget}, editable) => {
+          const $this = $(currentTarget)
           const data = this.getData()
           const index = $this.parents('tr[data-index]').data('index')
           const row = data[index]
@@ -135,8 +133,8 @@
           this.trigger('editable-shown', column.field, row, $this, editable)
         })
 
-        $field.off('hidden').on('hidden', (e, reason) => {
-          const $this = $(e.currentTarget)
+        $field.off('hidden').on('hidden', ({currentTarget}, reason) => {
+          const $this = $(currentTarget)
           const data = this.getData()
           const index = $this.parents('tr[data-index]').data('index')
           const row = data[index]

+ 7 - 7
src/extensions/export/bootstrap-table-export.js

@@ -106,10 +106,10 @@
         }
       }
 
-      $menu.find('>li, >a').click(e => {
-        const type = $(e.currentTarget).data('type')
+      $menu.find('>li, >a').click(({currentTarget}) => {
+        const type = $(currentTarget).data('type')
         const exportOptions = {
-          type: type,
+          type,
           escape: false
         }
 
@@ -128,8 +128,8 @@
           const footerData = {}
           const footerHtml = []
 
-          $.each($footerRow.children(), function (index, footerCell) {
-            var footerCellHtml = $(footerCell).children('.th-inner').first().html()
+          $.each($footerRow.children(), (index, footerCell) => {
+            const footerCellHtml = $(footerCell).children('.th-inner').first().html()
             footerData[that.columns[index].field] = footerCellHtml === '&nbsp;' ? null : footerCellHtml
 
             // grab footer cell text into cell index-based array
@@ -138,9 +138,9 @@
 
           this.append(footerData)
 
-          var $lastTableRow = this.$body.children().last()
+          const $lastTableRow = this.$body.children().last()
 
-          $.each($lastTableRow.children(), function (index, lastTableRowCell) {
+          $.each($lastTableRow.children(), (index, lastTableRowCell) => {
             $(lastTableRowCell).html(footerHtml[index])
           })
         }