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 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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | 22x 22x 22x 22x 22x 22x 88x 88x 88x 88x 88x 88x 88x 88x 88x 22x 22x 22x 22x 100x 2x 168x 168x 168x 168x 168x 168x 168x 50x 200x 50x 168x 168x 2x 2x 2x 1x 1x 5x 20x 5x 1x 168x 56x 56x 56x 56x 56x 56x 56x 56x 54x 2x 15x 15x 15x 15x 15x 15x 23310x 53x 15x 23310x 15x 15x 2x 15x 15x 15x 15x 15x 15x 15x 4x 29x 29x 29x 29x 22x 22x 22x 22x 22x 22x 170x 236x 236x 236x 9375x 9375x 9375x 9375x 9375x 66x 66x 66x 66x 66x 59x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import BaseController from "./BaseController";
import JSONModel from "sap/ui/model/json/JSONModel";
import MessageToast from "sap/m/MessageToast";
import SearchField from "sap/m/SearchField";
import Table from "sap/m/Table";
import ColumnListItem from "sap/m/ColumnListItem";
import SegmentedButton from "sap/m/SegmentedButton";
import Filter from "sap/ui/model/Filter";
import FilterOperator from "sap/ui/model/FilterOperator";
import FilterType from "sap/ui/model/FilterType";
import ListBinding from "sap/ui/model/ListBinding";
import { ListBase$SelectionChangeEvent } from "sap/m/ListBase";
import MessageBox from "sap/m/MessageBox";
import {
PamProduct,
PRODUCT_KEY_FIELDS,
getProductFieldValue,
createProductKey
} from "../model/productKey";
/**
* @namespace de.itsfullofstars.sap.pam.controller
*/
export default class MyProducts extends BaseController {
/** Product keys (see {@link createProductKey}) of the currently checked table rows. */
private selectedProductKeys: Set<string> = new Set();
private tableBinding?: ListBinding;
/** Sets up the "myProducts" title model and loads the initial selection/count state. */
public onInit(): void {
const titleModel = new JSONModel({
tableTitle: this.getText("myProducts.tableTitle", [0, 0])
});
this.setModel(
titleModel,
"myProducts"
);
this.loadSelectedProductsFromModel();
this.updateProductCount();
this.getRouter()
.getRoute("myproducts")
?.attachPatternMatched(this.onRouteMatched.bind(this));
}
/** Updates the table title with the current "N of M products selected" count. */
private updateProductCount(): void {
const pamModel = this.getOwnerComponent()
.getModel("pam") as JSONModel;
const ownProductsModel = this.getOwnerComponent()
.getModel("ownProducts") as JSONModel;
const pamData = pamModel?.getData() as PamProduct[] | undefined;
const ownProducts = ownProductsModel?.getData() as PamProduct[] | undefined;
const totalCount = Array.isArray(pamData)
? pamData.length
: 0;
const ownProductsCount = Array.isArray(ownProducts)
? ownProducts.length
: 0;
const titleModel = this.getView()
.getModel("myProducts") as JSONModel;
Iif (!titleModel) {
return;
}
titleModel.setProperty(
"/tableTitle",
this.getText("myProducts.tableTitle", [ownProductsCount, totalCount])
);
}
/** Attaches the table binding listener and refreshes count/filters/selection. */
public onAfterRendering(): void {
this.attachTableBinding();
this.updateProductCount();
this.applyFilters();
this.updateTableSelection();
}
/** Re-applies filters after the search field's value changes. */
public onSearch(): void {
this.applyFilters();
}
/** Re-applies filters after the "all"/"own" scope toggle changes. */
public onProductScopeFilterChange(): void {
this.applyFilters();
}
/** Rebuilds the table's filters from the search field and the "own products" scope toggle. */
private applyFilters(): void {
const table = this.byId("myProductsTable") as Table;
const binding = table.getBinding("items") as ListBinding | undefined;
Iif (!binding) {
return;
}
const filters: Filter[] = [];
const searchField = this.byId("myProductsSearch") as SearchField;
const searchValue = searchField.getValue().trim();
if (searchValue) {
const searchFilters = PRODUCT_KEY_FIELDS.map((field) => {
return new Filter(
field,
FilterOperator.Contains,
searchValue
);
});
filters.push(
new Filter({
filters: searchFilters,
and: false
})
);
}
const scopeFilter = this.byId(
"productScopeFilter"
) as SegmentedButton;
if (scopeFilter.getSelectedKey() === "own") {
const ownProductsModel =
this.getOwnerComponent().getModel(
"ownProducts"
) as JSONModel;
const ownProducts = ownProductsModel?.getData() as PamProduct[] | undefined;
if (!Array.isArray(ownProducts) || ownProducts.length === 0) {
/*
* No product can have this value, so this results in an
* empty table being shown.
*/
filters.push(
new Filter(
"version",
FilterOperator.EQ,
"__NO_OWN_PRODUCT__"
)
);
} else {
const ownProductFilters = ownProducts.map(
(product: PamProduct) => {
const productFields = PRODUCT_KEY_FIELDS.map(
(field) => {
return new Filter(
field,
FilterOperator.EQ,
getProductFieldValue(
product,
field
)
);
}
);
/*
* All four values must belong to one product.
*/
return new Filter({
filters: productFields,
and: true
});
}
);
/*
* One of the stored products must match.
*/
filters.push(
new Filter({
filters: ownProductFilters,
and: false
})
);
}
}
binding.filter(filters, FilterType.Application);
}
/** Tracks which products are checked in the table, independent of the current filter/scope. */
public onSelectionChange(event: ListBase$SelectionChangeEvent): void {
const item = event.getParameter("listItem") as ColumnListItem | undefined;
Iif (!item) {
return;
}
const context = item.getBindingContext("pam");
Iif (!context) {
return;
}
const product = context.getObject() as PamProduct;
const productKey = createProductKey(product);
const selected = event.getParameter("selected");
if (selected) {
this.selectedProductKeys.add(productKey);
} else {
this.selectedProductKeys.delete(productKey);
}
}
/** Persists the current table selection as the new "ownProducts" model content. */
public onAddProducts(): void {
const pamModel = this.getOwnerComponent().getModel("pam") as JSONModel;
const ownProductsModel =
this.getOwnerComponent().getModel("ownProducts") as JSONModel;
Iif (!pamModel || !ownProductsModel) {
return;
}
const pamData = pamModel.getData() as PamProduct[];
Iif (!Array.isArray(pamData)) {
return;
}
const selectedProducts = pamData
.filter((product: PamProduct) => {
return this.selectedProductKeys.has(
createProductKey(product)
);
})
.map((product: PamProduct) => {
return {
...product
};
});
/*
* Own products created directly via "myproducts/add" (see
* MyProductsAdd controller) have no matching row in the PAM table,
* so they can never be part of selectedProductKeys/selectedProducts
* above - carry them over unchanged, or this "declarative replace"
* would silently delete them.
*/
const pamProductKeys = new Set(
pamData.map((product: PamProduct) => createProductKey(product))
);
const existingOwnProducts =
(ownProductsModel.getData() as PamProduct[] | undefined) ?? [];
const customOwnProducts = existingOwnProducts.filter(
(product: PamProduct) => {
return !pamProductKeys.has(createProductKey(product));
}
);
const updatedOwnProducts = [...selectedProducts, ...customOwnProducts];
ownProductsModel.setData(updatedOwnProducts);
this.loadSelectedProductsFromModel();
this.updateProductCount();
this.updateTableSelection();
this.applyFilters();
MessageToast.show(
this.getText("msg.myProductsSaved", [updatedOwnProducts.length])
);
}
/** Navigates to the "create product" sub-page. */
public onNavigateToAddProduct(): void {
this.navTo("myproductsadd");
}
/** Re-syncs the selection/filters/count whenever this route is (re-)matched. */
private onRouteMatched(): void {
/*
* Unsaved checkbox changes are discarded when the page is
* revisited. The "ownProducts" model is authoritative.
*/
this.loadSelectedProductsFromModel();
this.updateProductCount();
this.applyFilters();
this.updateTableSelection();
}
/** Attaches the selection-sync listener to the table's current list binding, once. */
private attachTableBinding(): void {
const table = this.byId("myProductsTable") as Table;
Iif (!table) {
return;
}
const binding = table.getBinding("items") as ListBinding | undefined;
Iif (!binding || this.tableBinding === binding) {
return;
}
this.tableBinding = binding;
binding.attachChange(this.onTableBindingChange.bind(this));
}
/** Reacts to the table's list binding changing (e.g. after filtering) by re-applying the checkbox selection. */
private onTableBindingChange(): void {
this.updateTableSelection();
}
/** Re-applies {@link selectedProductKeys} as the table's checkbox selection state. */
private updateTableSelection(): void {
const table = this.byId("myProductsTable") as Table;
Iif (!table) {
return;
}
table.getItems().forEach((item) => {
const context = item.getBindingContext("pam");
Iif (!context) {
return;
}
const product = context.getObject() as PamProduct;
const productKey = createProductKey(product);
item.setSelected(
this.selectedProductKeys.has(productKey)
);
});
}
/** Rebuilds {@link selectedProductKeys} from the current "ownProducts" model content. */
private loadSelectedProductsFromModel(): void {
const ownProductsModel =
this.getOwnerComponent().getModel("ownProducts") as JSONModel;
Iif (!ownProductsModel) {
this.selectedProductKeys.clear();
return;
}
const ownProducts = ownProductsModel.getData() as PamProduct[];
Iif (!Array.isArray(ownProducts)) {
this.selectedProductKeys.clear();
return;
}
this.selectedProductKeys = new Set(
ownProducts.map((product: PamProduct) => {
return createProductKey(product);
})
);
}
/** Downloads the current "ownProducts" model content as an OwnProducts.json file. */
public onDownloadOwnProducts(): void {
const ownProductsModel =
this.getOwnerComponent().getModel("ownProducts") as JSONModel;
Iif (!ownProductsModel) {
MessageBox.error(
this.getText("msg.ownProductsModelUnavailable")
);
return;
}
const ownProducts = ownProductsModel.getData() as PamProduct[];
Iif (!Array.isArray(ownProducts) || ownProducts.length === 0) {
MessageToast.show(this.getText("msg.myProductsNoOwnProducts"));
return;
}
const jsonContent = JSON.stringify(ownProducts, null, 2);
const blob = new Blob(
[jsonContent],
{
type: "application/json;charset=utf-8"
}
);
const downloadUrl = URL.createObjectURL(blob);
const downloadLink = document.createElement("a");
downloadLink.href = downloadUrl;
downloadLink.download = "OwnProducts.json";
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
window.setTimeout(() => {
URL.revokeObjectURL(downloadUrl);
}, 100);
}
} |