-
Notifications
You must be signed in to change notification settings - Fork 31
/
backgrid-select-all.min.js
97 lines (89 loc) · 6.48 KB
/
backgrid-select-all.min.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
/*
backgrid-select-all
http://github.com/wyuenho/backgrid
Copyright (c) 2013-present Cloudflare, Inc and contributors
Licensed under the MIT @license.
*/
!function(a,b){"function"==typeof define&&define.amd?
// AMD. Register as an anonymous module.
define(["backbone","backgrid","underscore"],b):"object"==typeof exports?
// CommonJS
module.exports=b(require("backbone"),require("backgrid"),require("underscore")):b(a.Backbone,a.Backgrid,a._)}(this,function(a,b,c){"use strict";/**
Renders a checkbox for row selection.
@class Backgrid.Extension.SelectRowCell
@extends Backbone.View
*/
var d=b.Extension.SelectRowCell=a.View.extend({/** @property */
className:"select-row-cell",/** @property */
tagName:"td",/** @property */
events:{"keydown input[type=checkbox]":"onKeydown","change input[type=checkbox]":"onChange","click input[type=checkbox]":"enterEditMode"},/**
Initializer. If the underlying model triggers a `select` event, this cell
will change its checked value according to the event's `selected` value.
@param {Object} options
@param {Backgrid.Column} options.column
@param {Backbone.Model} options.model
*/
initialize:function(a){this.column=a.column,this.column instanceof b.Column||(this.column=new b.Column(this.column));var c=this.column,d=this.model,e=this.$el;this.listenTo(c,"change:renderable",function(a,b){e.toggleClass("renderable",b)}),b.callByNeed(c.renderable(),c,d)&&e.addClass("renderable"),this.listenTo(d,"backgrid:select",function(a,b){this.checkbox().prop("checked",b).change()})},/**
Returns the checkbox.
*/
checkbox:function(){return this.$el.find("input[type=checkbox]")},/**
Focuses the checkbox.
*/
enterEditMode:function(){this.checkbox().focus()},/**
Unfocuses the checkbox.
*/
exitEditMode:function(){this.checkbox().blur()},/**
Process keyboard navigation.
*/
onKeydown:function(a){var c=new b.Command(a);// skip ahead to `change`
return!!c.passThru()||void(c.cancel()?(a.stopPropagation(),this.checkbox().blur()):(c.save()||c.moveLeft()||c.moveRight()||c.moveUp()||c.moveDown())&&(a.preventDefault(),a.stopPropagation(),this.model.trigger("backgrid:edited",this.model,this.column,c)))},/**
When the checkbox's value changes, this method will trigger a Backbone
`backgrid:selected` event with a reference of the model and the
checkbox's `checked` value.
*/
onChange:function(){var a=this.checkbox().prop("checked");this.$el.parent().toggleClass("selected",a),this.model.trigger("backgrid:selected",this.model,a)},/**
Renders a checkbox in a table cell.
*/
render:function(){return this.$el.empty().append('<input tabindex="-1" type="checkbox" />'),this.delegateEvents(),this}}),e=b.Extension.SelectAllHeaderCell=d.extend({/** @property */
className:"select-all-header-cell",/** @property */
tagName:"th",/**
Initializer. When this cell's checkbox is checked, a Backbone
`backgrid:select` event will be triggered for each model for the current
page in the underlying collection. If a `SelectRowCell` instance exists
for the rows representing the models, they will check themselves. If any
of the SelectRowCell instances trigger a Backbone `backgrid:selected`
event with a `false` value, this cell will uncheck its checkbox. In the
event of a Backbone `backgrid:refresh` event, which is triggered when the
body refreshes its rows, which can happen under a number of conditions
such as paging or the columns were reset, this cell will still remember
the previously selected models and trigger a Backbone `backgrid:select`
event on them such that the SelectRowCells can recheck themselves upon
refreshing.
@param {Object} options
@param {Backgrid.Column} options.column
@param {Backbone.Collection} options.collection
*/
initialize:function(a){this.column=a.column,this.column instanceof b.Column||(this.column=new b.Column(this.column));var d=this.collection,e=this.selectedModels={};this.listenTo(d.fullCollection||d,"backgrid:selected",function(a,b){b?e[a.id||a.cid]=1:(delete e[a.id||a.cid],this.checkbox().prop("checked",!1)),c.keys(e).length===(d.fullCollection||d).length&&this.checkbox().prop("checked",!0)}),this.listenTo(d.fullCollection||d,"remove",function(a){delete e[a.id||a.cid],0===(d.fullCollection||d).length&&this.checkbox().prop("checked",!1)}),this.listenTo(d,"backgrid:refresh",function(){if(0===(d.fullCollection||d).length)this.checkbox().prop("checked",!1);else for(var a=this.checkbox().prop("checked"),b=0;b<d.length;b++){var c=d.at(b);(a||e[c.id||c.cid])&&c.trigger("backgrid:select",c,!0)}});var f=this.column,g=this.$el;this.listenTo(f,"change:renderable",function(a,b){g.toggleClass("renderable",b)}),b.callByNeed(f.renderable(),f,d)&&g.addClass("renderable")},/**
Propagates the checked value of this checkbox to all the models of the
underlying collection by triggering a Backbone `backgrid:select` event on
the models on the current page, passing each model and the current
`checked` value of the checkbox in each event.
A `backgrid:selected` event will also be triggered with the current
`checked` value on all the models regardless of whether they are on the
current page.
This method triggers a 'backgrid:select-all' event on the collection
afterwards.
*/
onChange:function(){var a=this.checkbox().prop("checked"),b=this.collection;b.each(function(b){b.trigger("backgrid:select",b,a)}),b.fullCollection&&b.fullCollection.each(function(c){b.get(c.cid)||c.trigger("backgrid:selected",c,a)}),this.collection.trigger("backgrid:select-all",this.collection,a)}});/**
Convenient method to retrieve a list of selected models. This method only
exists when the `SelectAll` extension has been included. Selected models
are retained across pagination.
@member Backgrid.Grid
@return {Array.<Backbone.Model>}
*/
b.Grid.prototype.getSelectedModels=function(){for(var a,b=this.header.row.cells,c=0,d=b.length;c<d;c++){var f=b[c];if(f instanceof e){a=f;break}}var g=[];if(a){var h=a.selectedModels,i=this.collection.fullCollection||this.collection;for(var j in h)g.push(i.get(j))}return g},/**
Convenient method to deselect the selected models. This method is only
available when the `SelectAll` extension has been included.
@member Backgrid.Grid
*/
b.Grid.prototype.clearSelectedModels=function(){for(var a=this.getSelectedModels(),b=0,c=a.length;b<c;b++){var d=a[b];d.trigger("backgrid:select",d,!1)}}});