-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlucid_footable_bookmarkable.js
169 lines (151 loc) · 7.28 KB
/
lucid_footable_bookmarkable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
(function ($, w, undefined) {
if (w.footable === undefined || w.foobox === null)
throw new Error('Please check and make sure footable.js is included in the page and is loaded prior to this script.');
var defaults = {
lucidBookmarkable: {
enabled: false
}
};
// see http://www.onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/
function $_HASH(q,s) {
s = s ? s : location.hash;
var re = new RegExp('&'+q+'(?:=([^&]*))?(?=&|$)','i');
return (s=s.replace(/^\#/,'&').match(re)) ? (typeof s[1] == 'undefined' ? '' : decodeURIComponent(s[1])) : undefined;
}
function addFootableStatusData(ft, event) {
var tbl_total = $(ft.table).find("tbody").find("tr:not(.footable-row-detail, .footable-filtered)").length;
$(ft.table).data("status_num_total", tbl_total);
var tbl_num = $(ft.table).find("tbody").find("tr:not(.footable-row-detail)").filter(":visible").length;
$(ft.table).data("status_num_shown", tbl_num);
var sort_colnum = $(ft.table).data("sorted");
var sort_col = $(ft.table).find("th")[sort_colnum];
var descending = $(sort_col).hasClass("footable-sorted-desc");
$(ft.table).data("status_descending", descending);
if (ft.pageInfo) {
var pagenum = ft.pageInfo.currentPage;
$(ft.table).data("status_pagenum", pagenum);
}
var filter_val = '';
var filter_field_id = $(ft.table).data('filter');
if ( $(filter_field_id).length ) {
filter_val = $(filter_field_id).val();
}
$(ft.table).data("status_filter_val", filter_val);
// manage expanded or collapsed rows:
if (event.type == 'footable_row_expanded') {
var row = event.row;
if (row) {
var rowlist = $(ft.table).data('expanded_rows');
var expanded_rows = [];
if (rowlist) {
expanded_rows = rowlist.split(',');
}
expanded_rows.push(row.rowIndex);
$(ft.table).data('expanded_rows', expanded_rows.join(','));
}
}
if (event.type == 'footable_row_collapsed') {
var row = event.row;
if (row) {
var rowlist = $(ft.table).data('expanded_rows');
var expanded_rows = [];
if (rowlist) {
expanded_rows = rowlist.split(',');
}
new_expanded_rows = [];
for (i in expanded_rows) {
if (expanded_rows[i] == row.rowIndex) {
new_expanded_rows = expanded_rows.splice(i, 1);
break;
}
}
$(ft.table).data('expanded_rows', new_expanded_rows.join(','));
}
}
}
function LucidBookmarkable() {
var p = this;
p.name = 'Footable LucidBookmarkable';
p.init = function(ft) {
if (ft.options.lucidBookmarkable.enabled) {
$(ft.table).bind({
'footable_initialized': function(){
var tbl_id = ft.table.id;
var q_filter = $_HASH(tbl_id + '_f');
var q_page_num = $_HASH(tbl_id + '_p');
var q_sorted = $_HASH(tbl_id + '_s');
var q_desc = $_HASH(tbl_id + '_d');
var q_expanded = $_HASH(tbl_id + '_e');
if (q_filter) {
var filter_field_id = $(ft.table).data('filter');
$(filter_field_id).val(q_filter);
$(ft.table).trigger('footable_filter', {filter: q_filter});
}
if (q_page_num) {
$(ft.table).data('currentPage', q_page_num);
// we'll check for sort before triggering pagination, since
// sorting triggers pagination.
}
if (typeof q_sorted !== 'undefined') {
var footableSort = $(ft.table).data('footable-sort');
var ascending = true;
if (q_desc == 'true') {
ascending = false;
}
footableSort.doSort(q_sorted, ascending);
}
else {
$(ft.table).trigger('footable_setup_paging');
}
if (q_expanded) {
var expanded_rows = q_expanded.split(',');
for (i in expanded_rows) {
row = $(ft.table.rows[expanded_rows[i]]);
row.find('> td:first').trigger('footable_toggle_row');
}
}
ft.lucid_bookmark_read = true;
},
'footable_page_filled footable_redrawn footable_filtered footable_sorted footable_row_expanded footable_row_collapsed': function(e) {
addFootableStatusData(ft, e);
// update the URL hash
// lucid_bookmark_read guards against running this logic before
// the "first read" of the location bookmark hash.
if (ft.lucid_bookmark_read) {
var tbl_id = ft.table.id;
var filter = tbl_id + '_f';
var page_num = tbl_id + '_p';
var sorted = tbl_id + '_s';
var descending = tbl_id + '_d';
var expanded = tbl_id + '_e';
var hash = location.hash.replace(/^\#/, '&');
var hashkeys = [filter, page_num, sorted, descending, expanded];
// trim existing elements out of the hash.
for (i in hashkeys) {
var re = new RegExp('&' + hashkeys[i]+'=([^&]*)', 'g');
hash = hash.replace(re, '');
}
var foostate = {};
foostate[filter] = $(ft.table).data('status_filter_val');
foostate[page_num] = $(ft.table).data('status_pagenum');
foostate[sorted] = $(ft.table).data('sorted');
foostate[descending] = $(ft.table).data('status_descending');
foostate[expanded] = $(ft.table).data('expanded_rows');
var pairs = [];
for (elt in foostate) {
if (foostate[elt] !== undefined) {
pairs.push(elt + '=' + encodeURIComponent(foostate[elt]));
}
}
if (hash.length) {
pairs.push(hash);
}
location.hash = pairs.join('&');
}
}
});
}
};
}
w.footable.plugins.register(LucidBookmarkable, defaults);
})(jQuery, window);