Browse Source

Merge pull request #1033 from tmorehouse/master

Add option for specifying locale as an option
文翼 10 years ago
parent
commit
7e656d8f4e

+ 20 - 1
docs/_i18n/en/documentation/table-options.md

@@ -489,5 +489,24 @@ The table options are defined in `jQuery.fn.bootstrapTable.defaults`.
         Support all custom attributes.
         </td>
     </tr>
-    </tbody>
+     <tr>
+        <td>locale</td>
+        <td>data-locale</td>
+        <td>string</td>
+        <td>undefined</td>
+        <td>
+        Sets the locale to use (i.e. <code>'fr-CA'</code>). Locale files must be pre-loaded.
+        Allows for fallback locales, if loaded, in the following order:<br>
+        <ol>
+        <li>First tries for the locale as specified,</li>
+        <li>Then tries the locale with <code>'_'</code> translated to 
+        <code>'-'</code> and the region code upper cased,</li>
+        <li>Then tries the the short locale code (i.e. <code>'fr'</code> instead of <code>'fr-CA'</code>),</li>
+        <li>And finally will use the last locale file loaded (or the default locale if no locales loaded).</li>
+        </ol>
+        If left undfined or an empty string, uses the last locale loaded (or <code>'en-US'</code>
+        if no locale files loaded).
+        </td>
+    </tr>
+   </tbody>
 </table>

+ 21 - 1
src/bootstrap-table.js

@@ -185,6 +185,7 @@
 
     BootstrapTable.DEFAULTS = {
         classes: 'table table-hover',
+        locale: undefined,
         height: undefined,
         undefinedText: '-',
         sortName: undefined,
@@ -347,7 +348,7 @@
 
     BootstrapTable.LOCALES = [];
 
-    BootstrapTable.LOCALES['en-US'] = {
+    BootstrapTable.LOCALES['en-US'] = BootstrapTable.LOCALES['en'] = {
         formatLoadingMessage: function () {
             return 'Loading, please wait...';
         },
@@ -439,6 +440,7 @@
     };
 
     BootstrapTable.prototype.init = function () {
+        this.initLocale();
         this.initContainer();
         this.initTable();
         this.initHeader();
@@ -450,6 +452,24 @@
         this.initServer();
     };
 
+    BootstrapTable.prototype.initLocale = function () {
+        if (this.options.locale) {
+            var parts = this.options.locale.split(/-|_/);
+            parts[0].toLowerCase();
+            parts[1] && parts[1].toUpperCase();
+            if ($.fn.bootstrapTable.locales[this.options.locale]) {
+                // locale as requested
+                $.extend(this.options, $.fn.bootstrapTable.locales[this.options.locale]);
+            } else if ($.fn.bootstrapTable.locales[parts.join('-')]) {
+                // locale with sep set to - (in case original was specified with _)
+                $.extend(this.options, $.fn.bootstrapTable.locales[parts.join('-')]);
+            } else if ($.fn.bootstrapTable.locales[parts[0]]) {
+                // short locale language code (i.e. 'en')
+                $.extend(this.options, $.fn.bootstrapTable.locales[parts[0]]);
+            }
+        }
+    };
+    
     BootstrapTable.prototype.initContainer = function () {
         this.$container = $([
             '<div class="bootstrap-table">',

+ 27 - 5
src/extensions/mobile/bootstrap-table-mobile.js

@@ -53,10 +53,24 @@
         that.toggleView();
     };
 
+    var debounce = function(func,wait) {
+        var timeout;
+        return function() {
+            var context = this, args = arguments;
+            var later = function() {
+                timeout = null;
+                func.apply(context,args);
+            };
+            clearTimeout(timeout);
+            timeout = setTimeout(later,wait);
+        };
+    };
+
     $.extend($.fn.bootstrapTable.defaults, {
         mobileResponsive: false,
         minWidth: 562,
         minHeight: undefined,
+        heightThreshold: 100, // just slightly larger than mobile chrome's auto-hiding toolbar
         checkOnInit: true,
         toggled: false
     });
@@ -75,13 +89,21 @@
             return;
         }
 
-        var that = this;
-        $(window).resize(function () {
-            changeView(that, $(this).width(), $(this).height())
-        });
+        var that = this, old = { w: $(window).width(), h: $(window).height() };
+
+        $(window).on('resize orientationchange',debounce(function (evt) {
+            // reset view if height has only changed by at least the threshold.
+            var h = $(this).height(), w = $(this).width();
+            if (Math.abs(old.h - h) > that.options.heightThreshold || old.w != w) {
+                changeView(that, w, h);
+                old = { w: w, h: h };
+            }
+        },200));
 
         if (this.options.checkOnInit) {
-            changeView(this, $(window).width(), $(window).height());
+            var h = $(window).height(), w = $(window).width();
+            changeView(this, w, h);
+            old = { w: w, h: h };
         }
     };
 }(jQuery);