ListSelector.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. sap.ui.define([
  2. "sap/ui/base/Object",
  3. "sap/base/Log"
  4. ], function (BaseObject, Log) {
  5. "use strict";
  6. return BaseObject.extend("sap.ui.demo.orderbrowser.controller.ListSelector", {
  7. /**
  8. * Provides a convenience API for selecting list items. All the functions will wait until the initial load of the a List passed to the instance by the setBoundMasterList
  9. * function.
  10. * @class
  11. * @public
  12. * @alias sap.ui.demo.orderbrowser.controller.ListSelector
  13. */
  14. constructor: function () {
  15. this._oWhenListHasBeenSet = new Promise(function (fnResolveListHasBeenSet) {
  16. this._fnResolveListHasBeenSet = fnResolveListHasBeenSet;
  17. }.bind(this));
  18. // This promise needs to be created in the constructor, since it is allowed to
  19. // invoke selectItem functions before calling setBoundMasterList
  20. this.oWhenListLoadingIsDone = new Promise(function (fnResolve, fnReject) {
  21. // Used to wait until the setBound masterList function is invoked
  22. this._oWhenListHasBeenSet
  23. .then(function (oList) {
  24. oList.getBinding("items").attachEventOnce("dataReceived",
  25. function () {
  26. if (this._oList.getItems().length) {
  27. fnResolve({
  28. list: oList
  29. });
  30. } else {
  31. // No items in the list
  32. fnReject({
  33. list: oList
  34. });
  35. }
  36. }.bind(this)
  37. );
  38. }.bind(this));
  39. }.bind(this));
  40. },
  41. /**
  42. * A bound list should be passed in here. Should be done, before the list has received its initial data from the server.
  43. * May only be invoked once per ListSelector instance.
  44. * @param {sap.m.List} oList The list all the select functions will be invoked on.
  45. * @public
  46. */
  47. setBoundMasterList: function (oList) {
  48. this._oList = oList;
  49. this._fnResolveListHasBeenSet(oList);
  50. },
  51. /**
  52. * Tries to select and scroll to a list item with a matching binding context. If there are no items matching the binding context or the ListMode is none,
  53. * no selection/scrolling will happen
  54. * @param {string} sBindingPath the binding path matching the binding path of a list item
  55. * @public
  56. */
  57. selectAListItem: function (sBindingPath) {
  58. this.oWhenListLoadingIsDone.then(
  59. function () {
  60. var oList = this._oList,
  61. oSelectedItem;
  62. if (oList.getMode() === "None") {
  63. return;
  64. }
  65. oSelectedItem = oList.getSelectedItem();
  66. // skip update if the current selection is already matching the object path
  67. if (oSelectedItem && oSelectedItem.getBindingContext().getPath() === sBindingPath) {
  68. return;
  69. }
  70. oList.getItems().some(function (oItem) {
  71. if (oItem.getBindingContext() && oItem.getBindingContext().getPath() === sBindingPath) {
  72. oList.setSelectedItem(oItem);
  73. return true;
  74. }
  75. });
  76. }.bind(this),
  77. function () {
  78. Log.warning("Could not select the list item with the path" + sBindingPath + " because the list encountered an error or had no items");
  79. }
  80. );
  81. },
  82. /**
  83. * Removes all selections from master list.
  84. * Does not trigger 'selectionChange' event on master list, though.
  85. * @public
  86. */
  87. clearMasterListSelection: function () {
  88. //use promise to make sure that 'this._oList' is available
  89. this._oWhenListHasBeenSet.then(function () {
  90. this._oList.removeSelections(true);
  91. }.bind(this));
  92. }
  93. });
  94. });