In the last step of our Walkthrough tutorial, we adjust the content density based on the user’s device. SAPUI5 contains different content densities allowing you to display larger controls for touch-enabled devices and a smaller, more compact design for devices that are operated by mouse. In our app, we will detect the device and adjust the density accordingly.
You can view and download all files in the Explored app in the Demo Kit under Walkthrough - Step 37.
...
init: function () …
},
getContentDensityClass : function() {
if (!this._sContentDensityClass) {
if (!sap.ui.Device.support.touch) {
this._sContentDensityClass = "sapUiSizeCompact";
} else {
this._sContentDensityClass = "sapUiSizeCozy";
}
}
return this._sContentDensityClass;
}
});
});
To prepare the content density feature we will also add a helper method getContentDensityClass. SAPUI5 controls can be displayed in multiple sizes, for example in a compact size that is optimized for desktop and non-touch devices, and in a cozy mode that is optimized for touch interaction. The controls look for a specific CSS class in the HTML structure of the application to adjust their size.
This helper method queries the sap.ui.Device API directly for touch support of the client and returns the CSS class sapUiSizeCompact if touch interaction is not supported and sapUiSizeCozy for all other cases. We will use it throughout the application coding to set the proper content density CSS class.
sap.ui.define([
"sap/ui/core/mvc/Controller"
], function (Controller) {
"use strict";
return Controller.extend("sap.ui.demo.wt.controller.App", {
onInit: function () {
this.getView().addStyleClass(this.getOwnerComponent().getContentDensityClass());
},
onOpenDialog: function () {
this.getOwnerComponent().helloDialog.open(this.getView());
}
});
});
sap.ui.define([
"sap/ui/base/Object"
], function (Object) {
"use strict";
return Object.extend("sap.ui.demo.wt.controller.HelloDialog", {
_getDialog: function () {
// create dialog lazily
if (!this._oDialog) {
// create dialog via fragment factory
this._oDialog = sap.ui.xmlfragment("sap.ui.demo.wt.view.HelloDialog", this);
}
return this._oDialog;
},
open: function (oView) {
var oDialog = this._getDialog();
// forward compact/cozy style into Dialog
jQuery.sap.syncStyleClass(oView.getController().getOwnerComponent().getContentDensityClass(), oView, oDialog);
// connect dialog to view (models, lifecycle)
oView.addDependent(oDialog);
// open dialog
oDialog.open();
},
onCloseDialog: function () {
this._getDialog().close();
}
});
});
This was the last step, you have successfully completed the Walkthrough!