/
listing.js
299 lines (272 loc) · 8.72 KB
/
listing.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* Return an instance of Listing.
*
* @constructor
* @param {Object} options - instance options
*/
var Listing = function(options) {
this.initialize(options);
};
Listing.prototype = {
constructor: Listing,
listing_defaults: {
sortable_selector: '.sortable',
with_searchform: true,
load_page_url: null,
navigation_params: ["sort_order", "searchquery"],
scroll_container: null,
main_table_id: "objects_table",
eor_message: gettext("No more entry to show")
},
initialize: function(options) {
this.options = $.extend({}, this.listing_defaults, options);
this.tag_handlers = {};
this.navobj = new History(this.options);
$(document).on("click", "a.filter", $.proxy(this.filter_by_tag, this));
if (this.options.with_searchform) {
this.init_searchform();
}
},
/**
* Initialize the search form.
*/
init_searchform: function() {
$("#searchquery").focus(function() {
$(this).val("");
}).blur($.proxy(function(e) {
var $this = $(e.target);
if ($this.val() === "") {
if (this.navobj.getparam("searchquery")) {
$this.val(this.navobj.getparam("searchquery"));
} else {
$this.val(gettext("Search"));
}
}
}, this));
if (this.navobj.getparam("searchquery") !== undefined) {
$("#searchquery").val(this.navobj.getparam("searchquery"));
}
$("#searchform").submit($.proxy(this.do_search, this));
},
/**
* Apply the current search pattern.
*/
do_search: function(e) {
e.preventDefault();
var squery = $("#searchquery").val();
if (squery !== "") {
this.navobj.setparam("searchquery", squery);
} else {
this.navobj.delparam("searchquery");
}
this.navobj.update();
},
load_page: function(e) {
var $link = get_target(e, "a");
e.preventDefault();
this.navobj.updateparams($link.attr("href")).update();
},
/**
* Return extra arguments used to fetch a page.
*
* @this Listing
*/
get_load_page_args: function() {
var $this = this;
var args = {};
$.each(this.options.navigation_params, function(pos, param) {
if ($this.navobj.hasparam(param)) {
args[param] = $this.navobj.getparam(param);
}
});
return args;
},
/**
* Calculate the bottom position of the scroll container.
*
* @param {Object} $element - scroll container object
*/
calculate_bottom: function($element) {
var $last_row = $("#" + this.options.main_table_id)
.find("tr").last();
return $last_row.offset().top - $element.height();
},
/**
* A new page has been received, inject it.
*
* @param {Object} data - page content
* @param {string} direction - scroll direction (up or down)
*/
add_new_page: function(data, direction) {
var $container = $("#{0} tbody".format(this.options.main_table_id));
if (direction == "down") {
$container.html(function(pos, oldhtml) {
return oldhtml + data.rows;
});
} else {
$container.html(function(pos, oldhtml) {
return data.rows + oldhtml;
});
}
},
/**
* Callback called when no more page can be loaded.
*
* @this Listing
*/
end_of_list_reached: function(element) {
$("#" + this.options.main_table_id).after(
$("<div id='eor_message' class='alert alert-info text-center'>{0}</div>".format(
this.options.eor_message
))
);
},
/**
* Update the listing with the received data.
*
* @this Listing
* @param {Object} data - new content
*/
update_listing: function(data, with_infinite_scroll) {
var $this = this;
var $scroll_container = (this.options.scroll_container) ?
$(this.options.scroll_container) : $(window);
$scroll_container.scrollTop(0);
if (with_infinite_scroll || with_infinite_scroll === undefined) {
if ($scroll_container.data("infinite-scroll") !== undefined) {
$scroll_container.infinite_scroll("reset_loaded_pages", data.pages);
$scroll_container.infinite_scroll("resume");
$("#eor_message").remove();
} else {
$scroll_container.infinite_scroll({
initial_pages: data.pages,
url: this.options.load_page_url,
get_args: $.proxy(this.get_load_page_args, this),
calculate_bottom: $.proxy(this.calculate_bottom, this),
process_results: $.proxy(this.add_new_page, this),
end_of_list_reached: $.proxy(this.end_of_list_reached, this)
});
}
} else if ($scroll_container.data("infinite-scroll") !== undefined) {
$scroll_container.infinite_scroll("pause");
}
var $sortables = $(this.options.sortable_selector);
if ($sortables.length) {
$(this.options.sortable_selector).sortable({
onSortOrderChange: $.proxy(this.change_sort_order, this)
});
this.set_sort_order();
}
},
/**
* Set current sort order.
*
* @this Listing
*/
set_sort_order: function() {
var sort_order = this.navobj.getparam("sort_order");
var sort_dir;
if (!sort_order) {
return;
}
if (sort_order[0] == '-') {
sort_dir = "desc";
sort_order = sort_order.substr(1);
} else {
sort_dir = 'asc';
}
$("th[data-sort_order=" + sort_order + "]").sortable('select', sort_dir);
},
/**
* Change current sort order.
*
* @this Listing
*/
change_sort_order: function(sort_order, dir) {
if (dir == "desc") {
sort_order = "-" + sort_order;
}
this.navobj.setparam("sort_order", sort_order).update();
},
/**
* Register a new tag handler.
*
* @this Listing
* @param {string} name - name of the tag
* @param {function} handler - reference to a function (optional)
*/
register_tag_handler: function(name, handler) {
if (handler === undefined) {
handler = this.generic_tag_handler;
}
this.tag_handlers[name] = handler;
if (this.navobj.getparam(name + "filter") !== undefined) {
var text = this.navobj.getparam(name + "filter");
$("#taglist").append(this.make_tag(text, name));
}
},
/**
* Default tag handler.
*
* @this Listing
* @param {string} tag - name of the tag
* @param {Object} $link - tag that was clicked
* @return {boolean} - return true if handler has been executed
*/
generic_tag_handler: function(tag, $link) {
if (this.navobj.getparam(tag + "filter") === undefined && $link.hasClass(tag)) {
var text = $link.attr("name");
this.navobj.setparam(tag + "filter", text).update();
$("#taglist").append(this.make_tag(text, tag));
return true;
}
return false;
},
/**
* Build a button to remove a specific filter.
*
* @this Listing
* @param {string} text - text of the button
* @param {string} type - tag type
* @return {Object} - new button object
*/
make_tag: function(text, type) {
var $tag = $("<a />", {
"name": type, "class" : "btn btn-default btn-xs",
"html": " " + htmlEncode(text)
});
$("<span />", {"class" : "fa fa-remove"}).prependTo($tag);
$tag.click($.proxy(this.remove_tag, this));
return $tag;
},
/**
* Click event : remove a tag button.
*
* @this Listing
* @param {Object} e - event object
*/
remove_tag: function(e) {
var $tag = $(e.target);
if ($tag.is("i")) {
$tag = $tag.parent();
}
e.preventDefault();
this.navobj.delparam($tag.attr("name") + "filter").update();
$tag.remove();
},
/**
* Click event: filter the listing when a tag is clicked.
*
* @this Listing
* @param {Object} e - event object
*/
filter_by_tag: function(e) {
var $link = $(e.target);
e.preventDefault();
for (var name in this.tag_handlers) {
if (this.tag_handlers[name].apply(this, [name, $link])) {
break;
}
}
}
};