Subversion-Projekte lars-tiefland.ci

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
776 lars 1
/**
2
 * @author: Dennis Hernández
3
 * @webSite: http://djhvscf.github.io/Blog
4
 * @version: v1.2.0
5
 *
6
 * @update zhixin wen <wenzhixin2010@gmail.com>
7
 */
8
 
9
(function ($) {
10
    'use strict';
11
 
12
    var cookieIds = {
13
        sortOrder: 'bs.table.sortOrder',
14
        sortName: 'bs.table.sortName',
15
        pageNumber: 'bs.table.pageNumber',
16
        pageList: 'bs.table.pageList',
17
        columns: 'bs.table.columns',
18
        searchText: 'bs.table.searchText',
19
        filterControl: 'bs.table.filterControl'
20
    };
21
 
22
    var getCurrentHeader = function (that) {
23
        var header = that.$header;
24
        if (that.options.height) {
25
            header = that.$tableHeader;
26
        }
27
 
28
        return header;
29
    };
30
 
31
    var getCurrentSearchControls = function (that) {
32
        var searchControls = 'select, input';
33
        if (that.options.height) {
34
            searchControls = 'table select, table input';
35
        }
36
 
37
        return searchControls;
38
    };
39
 
40
    var cookieEnabled = function () {
41
        return !!(navigator.cookieEnabled);
42
    };
43
 
44
    var inArrayCookiesEnabled = function (cookieName, cookiesEnabled) {
45
        var index = -1;
46
 
47
        for (var i = 0; i < cookiesEnabled.length; i++) {
48
            if (cookieName.toLowerCase() === cookiesEnabled[i].toLowerCase()) {
49
                index = i;
50
                break;
51
            }
52
        }
53
 
54
        return index;
55
    };
56
 
57
    var setCookie = function (that, cookieName, cookieValue) {
58
        if ((!that.options.cookie) || (!cookieEnabled()) || (that.options.cookieIdTable === '')) {
59
            return;
60
        }
61
 
62
        if (inArrayCookiesEnabled(cookieName, that.options.cookiesEnabled) === -1) {
63
            return;
64
        }
65
 
66
        cookieName = that.options.cookieIdTable + '.' + cookieName;
67
        if (!cookieName || /^(?:expires|max\-age|path|domain|secure)$/i.test(cookieName)) {
68
            return false;
69
        }
70
 
71
        document.cookie = encodeURIComponent(cookieName) + '=' + encodeURIComponent(cookieValue) + calculateExpiration(that.options.cookieExpire) + (that.options.cookieDomain ? '; domain=' + that.options.cookieDomain : '') + (that.options.cookiePath ? '; path=' + that.options.cookiePath : '') + (that.cookieSecure ? '; secure' : '');
72
        return true;
73
    };
74
 
75
    var getCookie = function (that, tableName, cookieName) {
76
        if (!cookieName) {
77
            return null;
78
        }
79
 
80
        if (inArrayCookiesEnabled(cookieName, that.options.cookiesEnabled) === -1) {
81
            return null;
82
        }
83
 
84
        cookieName = tableName + '.' + cookieName;
85
 
86
        return decodeURIComponent(document.cookie.replace(new RegExp('(?:(?:^|.*;)\\s*' + encodeURIComponent(cookieName).replace(/[\-\.\+\*]/g, '\\$&') + '\\s*\\=\\s*([^;]*).*$)|^.*$'), '$1')) || null;
87
    };
88
 
89
    var hasCookie = function (cookieName) {
90
        if (!cookieName) {
91
            return false;
92
        }
93
        return (new RegExp('(?:^|;\\s*)' + encodeURIComponent(cookieName).replace(/[\-\.\+\*]/g, '\\$&') + '\\s*\\=')).test(document.cookie);
94
    };
95
 
96
    var deleteCookie = function (tableName, cookieName, sPath, sDomain) {
97
        cookieName = tableName + '.' + cookieName;
98
        if (!hasCookie(cookieName)) {
99
            return false;
100
        }
101
        document.cookie = encodeURIComponent(cookieName) + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT' + (sDomain ? '; domain=' + sDomain : '') + (sPath ? '; path=' + sPath : '');
102
        return true;
103
    };
104
 
105
    var calculateExpiration = function(cookieExpire) {
106
        var time = cookieExpire.replace(/[0-9]*/, ''); //s,mi,h,d,m,y
107
        cookieExpire = cookieExpire.replace(/[A-Za-z]/, ''); //number
108
 
109
        switch (time.toLowerCase()) {
110
            case 's':
111
                cookieExpire = +cookieExpire;
112
                break;
113
            case 'mi':
114
                cookieExpire = cookieExpire * 60;
115
                break;
116
            case 'h':
117
                cookieExpire = cookieExpire * 60 * 60;
118
                break;
119
            case 'd':
120
                cookieExpire = cookieExpire * 24 * 60 * 60;
121
                break;
122
            case 'm':
123
                cookieExpire = cookieExpire * 30 * 24 * 60 * 60;
124
                break;
125
            case 'y':
126
                cookieExpire = cookieExpire * 365 * 30 * 24 * 60 * 60;
127
                break;
128
            default:
129
                cookieExpire = undefined;
130
                break;
131
        }
132
 
133
        return cookieExpire === undefined ? '' : '; max-age=' + cookieExpire;
134
    };
135
 
136
    $.extend($.fn.bootstrapTable.defaults, {
137
        cookie: false,
138
        cookieExpire: '2h',
139
        cookiePath: null,
140
        cookieDomain: null,
141
        cookieSecure: null,
142
        cookieIdTable: '',
143
        cookiesEnabled: ['bs.table.sortOrder', 'bs.table.sortName', 'bs.table.pageNumber', 'bs.table.pageList', 'bs.table.columns', 'bs.table.searchText', 'bs.table.filterControl'],
144
        //internal variable
145
        filterControls: [],
146
        filterControlValuesLoaded: false
147
    });
148
 
149
    $.fn.bootstrapTable.methods.push('deleteCookie');
150
 
151
    var BootstrapTable = $.fn.bootstrapTable.Constructor,
152
        _init = BootstrapTable.prototype.init,
153
        _initTable = BootstrapTable.prototype.initTable,
154
        _onSort = BootstrapTable.prototype.onSort,
155
        _onPageNumber = BootstrapTable.prototype.onPageNumber,
156
        _onPageListChange = BootstrapTable.prototype.onPageListChange,
157
        _onPageFirst = BootstrapTable.prototype.onPageFirst,
158
        _onPagePre = BootstrapTable.prototype.onPagePre,
159
        _onPageNext = BootstrapTable.prototype.onPageNext,
160
        _onPageLast = BootstrapTable.prototype.onPageLast,
161
        _toggleColumn = BootstrapTable.prototype.toggleColumn,
162
        _selectPage = BootstrapTable.prototype.selectPage,
163
        _onSearch = BootstrapTable.prototype.onSearch;
164
 
165
    BootstrapTable.prototype.init = function () {
166
        var timeoutId = 0;
167
        this.options.filterControls = [];
168
        this.options.filterControlValuesLoaded = false;
169
 
170
 
171
        this.options.cookiesEnabled = typeof this.options.cookiesEnabled === 'string' ?
172
            this.options.cookiesEnabled.replace('[', '').replace(']', '').replace(/ /g, '').toLowerCase().split(',') : this.options.cookiesEnabled;
173
 
174
        if (this.options.filterControl) {
175
            var that = this;
176
            this.$el.on('column-search.bs.table', function (e, field, text) {
177
                var isNewField = true;
178
 
179
                for (var i = 0; i < that.options.filterControls.length; i++) {
180
                    if (that.options.filterControls[i].field === field) {
181
                        that.options.filterControls[i].text = text;
182
                        isNewField = false;
183
                        break;
184
                    }
185
                }
186
                if (isNewField) {
187
                    that.options.filterControls.push({
188
                        field: field,
189
                        text: text
190
                    });
191
                }
192
 
193
                setCookie(that, cookieIds.filterControl, JSON.stringify(that.options.filterControls));
194
            }).on('post-body.bs.table', function () {
195
                setTimeout(function () {
196
                    if (!that.options.filterControlValuesLoaded) {
197
                        that.options.filterControlValuesLoaded = true;
198
                        var filterControl = JSON.parse(getCookie(that, that.options.cookieIdTable, cookieIds.filterControl));
199
                        if (filterControl) {
200
                            var field = null,
201
                                result = [],
202
                                header = getCurrentHeader(that),
203
                                searchControls = getCurrentSearchControls(that);
204
 
205
                            header.find(searchControls).each(function (index, ele) {
206
                                field = $(this).parent().parent().parent().data('field');
207
                                result = $.grep(filterControl, function (valueObj) {
208
                                    return valueObj.field === field;
209
                                });
210
 
211
                                if (result.length > 0) {
212
                                    $(this).val(result[0].text);
213
                                    that.onColumnSearch({currentTarget: $(this)});
214
                                }
215
                            });
216
                        }
217
                    }
218
                }, 250);
219
            });
220
        }
221
        _init.apply(this, Array.prototype.slice.apply(arguments));
222
    };
223
 
224
    BootstrapTable.prototype.initTable = function () {
225
        _initTable.apply(this, Array.prototype.slice.apply(arguments));
226
        this.initCookie();
227
    };
228
 
229
    BootstrapTable.prototype.initCookie = function () {
230
        if (!this.options.cookie) {
231
            return;
232
        }
233
 
234
        if ((this.options.cookieIdTable === '') || (this.options.cookieExpire === '') || (!cookieEnabled())) {
235
            throw new Error("Configuration error. Please review the cookieIdTable, cookieExpire properties, if those properties are ok, then this browser does not support the cookies");
236
            return;
237
        }
238
 
239
        var sortOrderCookie = getCookie(this, this.options.cookieIdTable, cookieIds.sortOrder),
240
            sortOrderNameCookie = getCookie(this, this.options.cookieIdTable, cookieIds.sortName),
241
            pageNumberCookie = getCookie(this, this.options.cookieIdTable, cookieIds.pageNumber),
242
            pageListCookie = getCookie(this, this.options.cookieIdTable, cookieIds.pageList),
243
            columnsCookie = JSON.parse(getCookie(this, this.options.cookieIdTable, cookieIds.columns)),
244
            searchTextCookie = getCookie(this, this.options.cookieIdTable, cookieIds.searchText);
245
 
246
        //sortOrder
247
        this.options.sortOrder = sortOrderCookie ? sortOrderCookie : this.options.sortOrder;
248
        //sortName
249
        this.options.sortName = sortOrderNameCookie ? sortOrderNameCookie : this.options.sortName;
250
        //pageNumber
251
        this.options.pageNumber = pageNumberCookie ? +pageNumberCookie : this.options.pageNumber;
252
        //pageSize
253
        this.options.pageSize = pageListCookie ? pageListCookie === this.options.formatAllRows() ? pageListCookie : +pageListCookie : this.options.pageSize;
254
        //searchText
255
        this.options.searchText = searchTextCookie ? searchTextCookie : '';
256
 
257
        if (columnsCookie) {
258
            $.each(this.columns, function (i, column) {
259
                column.visible = $.inArray(column.field, columnsCookie) !== -1;
260
            });
261
        }
262
    };
263
 
264
    BootstrapTable.prototype.onSort = function () {
265
        _onSort.apply(this, Array.prototype.slice.apply(arguments));
266
        setCookie(this, cookieIds.sortOrder, this.options.sortOrder);
267
        setCookie(this, cookieIds.sortName, this.options.sortName);
268
    };
269
 
270
    BootstrapTable.prototype.onPageNumber = function () {
271
        _onPageNumber.apply(this, Array.prototype.slice.apply(arguments));
272
        setCookie(this, cookieIds.pageNumber, this.options.pageNumber);
273
    };
274
 
275
    BootstrapTable.prototype.onPageListChange = function () {
276
        _onPageListChange.apply(this, Array.prototype.slice.apply(arguments));
277
        setCookie(this, cookieIds.pageList, this.options.pageSize);
278
    };
279
 
280
    BootstrapTable.prototype.onPageFirst = function () {
281
        _onPageFirst.apply(this, Array.prototype.slice.apply(arguments));
282
        setCookie(this, cookieIds.pageNumber, this.options.pageNumber);
283
    };
284
 
285
    BootstrapTable.prototype.onPagePre = function () {
286
        _onPagePre.apply(this, Array.prototype.slice.apply(arguments));
287
        setCookie(this, cookieIds.pageNumber, this.options.pageNumber);
288
    };
289
 
290
    BootstrapTable.prototype.onPageNext = function () {
291
        _onPageNext.apply(this, Array.prototype.slice.apply(arguments));
292
        setCookie(this, cookieIds.pageNumber, this.options.pageNumber);
293
    };
294
 
295
    BootstrapTable.prototype.onPageLast = function () {
296
        _onPageLast.apply(this, Array.prototype.slice.apply(arguments));
297
        setCookie(this, cookieIds.pageNumber, this.options.pageNumber);
298
    };
299
 
300
    BootstrapTable.prototype.toggleColumn = function () {
301
        _toggleColumn.apply(this, Array.prototype.slice.apply(arguments));
302
 
303
        var visibleColumns = [];
304
 
305
        $.each(this.columns, function (i, column) {
306
            if (column.visible) {
307
                visibleColumns.push(column.field);
308
            }
309
        });
310
 
311
        setCookie(this, cookieIds.columns, JSON.stringify(visibleColumns));
312
    };
313
 
314
    BootstrapTable.prototype.selectPage = function (page) {
315
        _selectPage.apply(this, Array.prototype.slice.apply(arguments));
316
        setCookie(this, cookieIds.pageNumber, page);
317
    };
318
 
319
    BootstrapTable.prototype.onSearch = function () {
320
        _onSearch.apply(this, Array.prototype.slice.apply(arguments));
321
        setCookie(this, cookieIds.searchText, this.searchText);
322
    };
323
 
324
    BootstrapTable.prototype.deleteCookie = function (cookieName) {
325
        if ((cookieName === '') || (!cookieEnabled())) {
326
            return;
327
        }
328
 
329
        deleteCookie(this.options.cookieIdTable, cookieIds[cookieName], this.options.cookiePath, this.options.cookieDomain);
330
    };
331
})(jQuery);