Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 5x 5x 6x 5x 2x 4x 2x 1x 1x 3x 3x 2x 1x 3x 3x 8x 8x 3x 5x 4x 6x 3x 3x 1x 1x 2x 2x 3x 3x 3x 2x 1x | import FieldIteratorMap from '../util/field/base/FieldIteratorMap';
import DefineMap from 'can-define/map/map';
import DefineList from 'can-define/list/list';
/**
* A `<sp-list-table />` component's ViewModel.
*
* @class ViewModel
* @memberof sp-list-table
*/
export default FieldIteratorMap.extend('ListTable', {seal: false}, {
/** @lends sp-list-table.ViewModel.prototype */
/**
* A string referencing a field type that will exclude that field
* from this classes fields. The default is 'list'.
* @type {String}
* @instance
* @memberof sp-list-table.ViewModel
*/
excludeFieldKey: {
default: 'list'
},
/**
* Optional promise or deferred object that will resolve to an object. Once
* the promise resolves, the objects list will be replaced with the promise result
* @memberof sp-list-table.ViewModel
* @type {Promise}
* @instance
*/
promise: {
set (newVal) {
Iif (!newVal) {
return newVal;
}
newVal.then((objects) => {
this.objects.replace(objects);
});
return newVal;
}
},
/**
* A list of objects to display. These objects should generally be can.Model
* objects but may be an javascript object.
* @memberof sp-list-table.ViewModel
* @type {Array<DefineMap>}
* @instance
*/
objects: {
Default: DefineList
},
/**
* A virtual type that retrieves this table's first object
* @memberof sp-list-table.ViewModel
* @type {Array<Object>}
* @instance
*
*/
object: {
get () {
// if length changes, update this getter
this.objects.get('length');
// return the first object
return this.objects[0] || {};
}
},
/**
* Id type name for the rows of objects. The default is `id`. This value
* is used to determine whether objects are selected or not. For a built in
* unique ID, `_cid` may be used. _cid is automatically generatted by `can-define`
* and should be guaranteed to be unique accross all DefineMaps
* @memberof sp-list-table.ViewModel
* @type {String}
* @instance
*/
idProp: {
default: 'id'
},
/**
* Whether rows can be selectable using a checkbox
* @memberof sp-list-table.ViewModel
* @type {Boolean}
* @instance
*/
selectable: {
type: 'boolean',
default: true
},
/**
* A list of the currently selected objects in the table
* @memberof sp-list-table.ViewModel
* @type {Array.<Object>}
* @instance
*/
selectedObjects: {
Type: DefineList,
Default: DefineList
},
/**
* An array of ids for the selected objects. This is a virtual type
* and cannot be set.
* @memberof sp-list-table.ViewModel
* @type {Array<Number>}
* @instance
*/
selectedIds: {
get () {
return this.selectedObjects.map((obj) => {
return obj[this.idProp];
});
}
},
/**
* A virtual type that helps the template determine whether all objects are selected
* @memberof sp-list-table.ViewModel
* @type {Boolean}
* @instance
*/
_allSelected: {
type: 'boolean',
get () {
for (let i = 0; i < this.objects.length; i ++) {
Iif (this.selectedObjects.indexOf(this.objects[i]) < 0) {
return false;
}
}
return true;
}
},
/**
* The current sort field
* @memberof sp-list-table.ViewModel
* @instance
* @type {can.List}
*/
currentSort: {
Type: DefineMap,
default () {
return {
field: null,
type: 'asc'
};
}
},
/**
* Dispatches an event with the name of the passed argument. Any additional
* arguments will be passed to the event handler
* @memberof sp-list-table.ViewModel
* @instance
* @param {String} event The name of the event to dispatch
*/
dispatchEvent (event) {
this.dispatch(event, Array.from(arguments));
},
/**
* Helps the template the currentSort value
* @memberof sp-list-table.ViewModel
* @instance
* @param {String} field the field to set the sort on
* @param {Event} event the event to cancel
*/
setSort (field, event) {
Iif (!field) {
if (event) {
event.stopPropagation();
}
this.currentSort = {};
return;
}
if (this.currentSort.field !== field) {
this.currentSort = {
field: field,
type: 'desc'
};
} else {
this.currentSort.type = this.currentSort.type === 'asc' ? 'desc' : 'asc';
}
this.sort(this.currentSort);
this.dispatch('sort', [this.currentSort]);
},
/**
* Toggles a row as selected or not selected
* @memberof sp-list-table.ViewModel
* @instance
* @param {Object} obj The row to toggle
*/
toggleSelected (obj) {
const index = this.selectedObjects.indexOf(obj);
if (index > -1) {
this.selectedObjects.splice(index, 1);
} else {
this.selectedObjects.push(obj);
}
},
/**
* Selects or unselects all of the objects in the table
* @memberof sp-list-table.ViewModel
* @instance
*/
toggleSelectAll () {
this.objects.forEach((obj) => this.toggleSelected(obj));
},
/**
* Determines whether or not the provided object is selected by comparing
* it to the list of currently selected objects
* @memberof sp-list-table.ViewModel
* @instance
* @param {Object} obj The object to check if is selected
* @return {Boolean} Whether or not it is selected
*/
isSelected (obj) {
return this.selectedIds.indexOf(obj[this.idProp]) > -1;
},
/**
* A function that sorts the list. It is called with the scope of the
* view model.
* @memberof sp-list-table.ViewModel
* @instance
* @param {Object} sortInfo The sorting object which contains `field` and `type`
*/
sort (sortInfo) {
const field = sortInfo.field;
let gt, lt;
if (sortInfo.type === 'asc') {
gt = 1;
lt = -1;
} else {
gt = -1;
lt = 1;
}
this.objects.sort((a, b) => {
Iif (a[field] === b[field]) {
return 0;
}
if (a[field] > b[field]) {
return gt;
} else {
return lt;
}
});
}
}); |