浏览代码

Added $.fn.bootstrapTable.utils tools.

zhixin 10 年之前
父节点
当前提交
6410da3640

+ 6 - 0
src/bootstrap-table.js

@@ -2546,6 +2546,12 @@
     $.fn.bootstrapTable.columnDefaults = BootstrapTable.COLUMN_DEFAULTS;
     $.fn.bootstrapTable.locales = BootstrapTable.LOCALES;
     $.fn.bootstrapTable.methods = allowedMethods;
+    $.fn.bootstrapTable.utils = {
+        sprintf: sprintf,
+        getFieldIndex: getFieldIndex,
+        compareObjects: compareObjects,
+        calculateObjectValue: calculateObjectValue
+    };
 
     // BOOTSTRAP TABLE INIT
     // =======================

+ 3 - 44
src/extensions/accent-neutralise/bootstrap-table-accent-neutralise.js

@@ -1,5 +1,5 @@
 /**
- * @author: Dennis Hernández
+ * @author: Dennis Hernández
  * @webSite: http://djhvscf.github.io/Blog
  * @version: v1.0.0
  */
@@ -113,47 +113,6 @@
         });
     };
 
-    var getFieldIndex = function (columns, field) {
-        var index = -1;
-
-        $.each(columns, function (i, column) {
-            if (column.field === field) {
-                index = i;
-                return false;
-            }
-            return true;
-        });
-        return index;
-    };
-
-    var calculateObjectValue = function (self, name, args, defaultValue) {
-        var func = name;
-
-        if (typeof name === 'string') {
-            // support obj.func1.func2
-            var names = name.split('.');
-
-            if (names.length > 1) {
-                func = window;
-                $.each(names, function (i, f) {
-                    func = func[f];
-                });
-            } else {
-                func = window[name];
-            }
-        }
-        if (typeof func === 'object') {
-            return func;
-        }
-        if (typeof func === 'function') {
-            return func.apply(self, args);
-        }
-        if (!func && typeof name === 'string' && sprintf.apply(this, [name].concat(args))) {
-            return sprintf.apply(this, [name].concat(args));
-        }
-        return defaultValue;
-    };
-
     $.extend($.fn.bootstrapTable.defaults, {
         searchAccentNeutralise: false
     });
@@ -190,11 +149,11 @@
                 for (var key in item) {
                     key = $.isNumeric(key) ? parseInt(key, 10) : key;
                     var value = item[key],
-                        column = that.columns[getFieldIndex(that.columns, key)],
+                        column = that.columns[$.fn.bootstrapTable.utils.getFieldIndex(that.columns, key)],
                         j = $.inArray(key, that.header.fields);
 
                     if (column && column.searchFormatter) {
-                        value = calculateObjectValue(column,
+                        value = $.fn.bootstrapTable.utils.calculateObjectValue(column,
                             that.header.formatters[j], [value, item, i], value);
                     }
 

+ 4 - 55
src/extensions/filter-control/bootstrap-table-filter-control.js

@@ -8,58 +8,7 @@
 
     'use strict';
 
-    var sprintf = function (str) {
-        var args = arguments,
-            flag = true,
-            i = 1;
-
-        str = str.replace(/%s/g, function () {
-            var arg = args[i++];
-
-            if (typeof arg === 'undefined') {
-                flag = false;
-                return '';
-            }
-            return arg;
-        });
-        return flag ? str : '';
-    };
-
-    var getFieldIndex = function (columns, field) {
-        var index = -1;
-
-        $.each(columns, function (i, column) {
-            if (column.field === field) {
-                index = i;
-                return false;
-            }
-            return true;
-        });
-        return index;
-    };
-
-    var calculateObjectValue = function (self, name, args, defaultValue) {
-        if (typeof name === 'string') {
-            // support obj.func1.func2
-            var names = name.split('.');
-
-            if (names.length > 1) {
-                name = window;
-                $.each(names, function (i, f) {
-                    name = name[f];
-                });
-            } else {
-                name = window[name];
-            }
-        }
-        if (typeof name === 'object') {
-            return name;
-        }
-        if (typeof name === 'function') {
-            return name.apply(self, args);
-        }
-        return defaultValue;
-    };
+    var sprintf = $.fn.bootstrapTable.utils.sprintf;
 
     var addOptionToSelectControl = function (selectControl, value, text) {
         selectControl = $(selectControl.get(selectControl.length - 1));
@@ -384,9 +333,9 @@
 
             $.each(this.header.fields, function (j, field) {
                 var value = item[field],
-                    column = that.columns[getFieldIndex(that.columns, field)];
+                    column = that.columns[$.fn.bootstrapTable.utils.getFieldIndex(that.columns, field)];
 
-                value = calculateObjectValue(that.header, that.header.formatters[j], [value, item, i], value);
+                value = $.fn.bootstrapTable.utils.calculateObjectValue(that.header, that.header.formatters[j], [value, item, i], value);
 
                 if ((!column.checkbox) || (!column.radio)) {
                     if (column.filterControl !== undefined && column.filterControl.toLowerCase() === 'select' && column.searchable) {
@@ -419,7 +368,7 @@
             for (var key in fp) {
                 var fval = fp[key].toLowerCase();
                 var value = item[key];
-                value = calculateObjectValue(that.header,
+                value = $.fn.bootstrapTable.utils.calculateObjectValue(that.header,
                     that.header.formatters[$.inArray(key, that.header.fields)],
                     [value, item, i], value);
 

+ 30 - 72
src/extensions/group-by/bootstrap-table-group-by.js

@@ -14,48 +14,6 @@
         obj = {},
         parentId = undefined;
 
-    var compareObjects = function (objectA, objectB, compareLength) {
-        // Create arrays of property names
-        var objectAProperties = Object.getOwnPropertyNames(objectA),
-            objectBProperties = Object.getOwnPropertyNames(objectB),
-            propName = '';
-
-        if (compareLength) {
-            // If number of properties is different, objects are not equivalent
-            if (objectAProperties.length !== objectBProperties.length) {
-                return false;
-            }
-        }
-
-        for (var i = 0; i < objectAProperties.length; i++) {
-            propName = objectAProperties[i];
-
-            // If the property is not in the object B properties, continue with the next property
-            if ($.inArray(propName, objectBProperties) > -1) {
-                // If values of same property are not equal, objects are not equivalent
-                if (objectA[propName] !== objectB[propName]) {
-                    return false;
-                }
-            }
-        }
-
-        // If we made it this far, objects are considered equivalent
-        return true;
-    };
-
-    var getFieldIndex = function (columns, field) {
-        var index = -1;
-
-        $.each(columns, function (i, column) {
-            if (column.field === field) {
-                index = i;
-                return false;
-            }
-            return true;
-        });
-        return index;
-    };
-
     var getParentRowId = function (that, id) {
         var parentRows = that.$body.find('tr').not('[' + 'data-tt-parent-id]');
 
@@ -72,15 +30,15 @@
         var sumRow = {};
         $.each(data, function (i, row) {
             if (!row.IsParent) {
-                for(var prop in row) {
-                        if (!isNaN(parseFloat(row[prop]))) {
-                            if (that.columns[getFieldIndex(that.columns, prop)].groupBySumGroup) {
-                                if (sumRow[prop] === undefined) {
-                                    sumRow[prop] = 0;
-                                }
-                                sumRow[prop] += +row[prop];
+                for (var prop in row) {
+                    if (!isNaN(parseFloat(row[prop]))) {
+                        if (that.columns[$.fn.bootstrapTable.utils.getFieldIndex(that.columns, prop)].groupBySumGroup) {
+                            if (sumRow[prop] === undefined) {
+                                sumRow[prop] = 0;
                             }
+                            sumRow[prop] += +row[prop];
                         }
+                    }
                 }
             }
         });
@@ -105,19 +63,19 @@
 
     var setObjectKeys = function () {
         // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
-       Object.keys = function (o) {
-           if (o !== Object(o)) {
-               throw new TypeError('Object.keys called on a non-object');
-           }
-           var k = [],
-               p;
-           for (p in o) {
-               if (Object.prototype.hasOwnProperty.call(o, p)) {
-                   k.push(p);
-               }
-           }
-           return k;
-       }
+        Object.keys = function (o) {
+            if (o !== Object(o)) {
+                throw new TypeError('Object.keys called on a non-object');
+            }
+            var k = [],
+                p;
+            for (p in o) {
+                if (Object.prototype.hasOwnProperty.call(o, p)) {
+                    k.push(p);
+                }
+            }
+            return k;
+        }
     };
 
     var getDataArrayFromItem = function (that, item) {
@@ -140,16 +98,16 @@
         return newRow;
     };
 
-    var groupBy = function (array , f) {
-       var groups = {};
-       $.each(array, function(i, o) {
-           var group = JSON.stringify(f(o));
-           groups[group] = groups[group] || [];
-           groups[group].push(o);
-       });
-       return Object.keys(groups).map(function (group) {
+    var groupBy = function (array, f) {
+        var groups = {};
+        $.each(array, function (i, o) {
+            var group = JSON.stringify(f(o));
+            groups[group] = groups[group] || [];
+            groups[group].push(o);
+        });
+        return Object.keys(groups).map(function (group) {
             return groups[group];
-       });
+        });
     };
 
     var makeGrouped = function (that, data) {
@@ -277,7 +235,7 @@
     };
 
     BootstrapTable.prototype.refreshGroupByField = function (groupByFields) {
-        if (!compareObjects(this.options.groupByField, groupByFields)) {
+        if (!$.fn.bootstrapTable.utils.compareObjects(this.options.groupByField, groupByFields)) {
             this.options.groupByField = groupByFields;
             this.load(this.options.originalData);
         }

+ 1 - 14
src/extensions/mobile/bootstrap-table-mobile.js

@@ -8,25 +8,12 @@
 
     'use strict';
 
-    var getFieldIndex = function (columns, field) {
-        var index = -1;
-
-        $.each(columns, function (i, column) {
-            if (column.field === field) {
-                index = i;
-                return false;
-            }
-            return true;
-        });
-        return index;
-    };
-
     var showHideColumns = function (that, checked) {
         if (that.options.columnsHidden.length > 0 ) {
             $.each(that.columns, function (i, column) {
                 if (that.options.columnsHidden.indexOf(column.field) !== -1) {
                     if (column.visible !== checked) {
-                        that.toggleColumn(getFieldIndex(that.columns, column.field), checked, true);
+                        that.toggleColumn($.fn.bootstrapTable.utils.getFieldIndex(that.columns, column.field), checked, true);
                     }
                 }
             });

+ 2 - 60
src/extensions/multiple-search/bootstrap-table-multiple-search.js

@@ -8,64 +8,6 @@
 
     'use strict';
 
-    var sprintf = function (str) {
-        var args = arguments,
-            flag = true,
-            i = 1;
-
-        str = str.replace(/%s/g, function () {
-            var arg = args[i++];
-
-            if (typeof arg === 'undefined') {
-                flag = false;
-                return '';
-            }
-            return arg;
-        });
-        return flag ? str : '';
-    };
-
-    var getFieldIndex = function (columns, field) {
-        var index = -1;
-
-        $.each(columns, function (i, column) {
-            if (column.field === field) {
-                index = i;
-                return false;
-            }
-            return true;
-        });
-        return index;
-    };
-
-    var calculateObjectValue = function (self, name, args, defaultValue) {
-        var func = name;
-
-        if (typeof name === 'string') {
-            // support obj.func1.func2
-            var names = name.split('.');
-
-            if (names.length > 1) {
-                func = window;
-                $.each(names, function (i, f) {
-                    func = func[f];
-                });
-            } else {
-                func = window[name];
-            }
-        }
-        if (typeof func === 'object') {
-            return func;
-        }
-        if (typeof func === 'function') {
-            return func.apply(self, args);
-        }
-        if (!func && typeof name === 'string' && sprintf.apply(this, [name].concat(args))) {
-            return sprintf.apply(this, [name].concat(args));
-        }
-        return defaultValue;
-    };
-
     $.extend($.fn.bootstrapTable.defaults, {
         multipleSearch: false
     });
@@ -89,12 +31,12 @@
                         for (var key in item) {
                             key = $.isNumeric(key) ? parseInt(key, 10) : key;
                             var value = item[key],
-                                column = that.columns[getFieldIndex(that.columns, key)],
+                                column = that.columns[$.fn.bootstrapTable.utils.getFieldIndex(that.columns, key)],
                                 j = $.inArray(key, that.header.fields);
 
                             // Fix #142: search use formated data
                             if (column && column.searchFormatter) {
-                                value = calculateObjectValue(column,
+                                value = $.fn.bootstrapTable.utils.calculateObjectValue(column,
                                     that.header.formatters[j], [value, item, i], value);
                             }
 

+ 1 - 14
src/extensions/reorder-columns/bootstrap-table-reorder-columns.js

@@ -8,19 +8,6 @@
 
     'use strict';
 
-    var getFieldIndex = function (columns, field) {
-        var index = -1;
-
-        $.each(columns, function (i, column) {
-            if (column.field === field) {
-                index = i;
-                return false;
-            }
-            return true;
-        });
-        return index;
-    };
-
     $.extend($.fn.bootstrapTable.defaults, {
         reorderableColumns: false,
         maxMovingRows: 10,
@@ -116,7 +103,7 @@
                 }
 
                 for (var i = 0; i < ths.length; i++ ) {
-                    columnIndex = getFieldIndex(that.columns, ths[i]);
+                    columnIndex = $.fn.bootstrapTable.utils.getFieldIndex(that.columns, ths[i]);
                     if (columnIndex !== -1) {
                         columns.push(that.columns[columnIndex]);
                         that.columns.splice(columnIndex, 1);

+ 2 - 40
src/extensions/toolbar/bootstrap-table-toolbar.js

@@ -10,45 +10,7 @@
 
     var firstLoad = false;
 
-    var sprintf = function(str) {
-        var args = arguments,
-            flag = true,
-            i = 1;
-
-        str = str.replace(/%s/g, function() {
-            var arg = args[i++];
-
-            if (typeof arg === 'undefined') {
-                flag = false;
-                return '';
-            }
-            return arg;
-        });
-        return flag ? str : '';
-    };
-
-    var calculateObjectValue = function (self, name, args, defaultValue) {
-        if (typeof name === 'string') {
-            // support obj.func1.func2
-            var names = name.split('.');
-
-            if (names.length > 1) {
-                name = window;
-                $.each(names, function (i, f) {
-                    name = name[f];
-                });
-            } else {
-                name = window[name];
-            }
-        }
-        if (typeof name === 'object') {
-            return name;
-        }
-        if (typeof name === 'function') {
-            return name.apply(self, args);
-        }
-        return defaultValue;
-    };
+    var sprintf = $.fn.bootstrapTable.utils.sprintf;
 
     var showAvdSearch = function(pColumns, searchTitle, searchText, that) {
         if (!$("#avdSearchModal" + "_" + that.options.idTable).hasClass("modal")) {
@@ -214,7 +176,7 @@
             for (var key in fp) {
                 var fval = fp[key].toLowerCase();
                 var value = item[key];
-                value = calculateObjectValue(that.header,
+                value = $.fn.bootstrapTable.utils.calculateObjectValue(that.header,
                     that.header.formatters[$.inArray(key, that.header.fields)],
                     [value, item, i], value);