{"size": 2281, "ext": "js", "max_stars_repo_path": "spet/auth/helper/BaseDynamicActionHelper-dbg.js", "max_stars_repo_name": "serban-petrescu/packaged-ui5-auth", "max_stars_repo_stars_event_min_datetime": "2017-03-27T21:35:39.000Z", "max_stars_repo_stars_event_max_datetime": "2020-01-17T10:02:03.000Z", "max_issues_repo_path": "spet/auth/helper/BaseDynamicActionHelper-dbg.js", "max_issues_repo_name": "serban-petrescu/packaged-ui5-auth", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "spet/auth/helper/BaseDynamicActionHelper-dbg.js", "max_forks_repo_name": "serban-petrescu/packaged-ui5-auth", "max_forks_repo_forks_event_min_datetime": "2020-08-06T09:05:29.000Z", "max_forks_repo_forks_event_max_datetime": "2020-11-15T13:23:35.000Z"}
{"max_stars_count": 8, "max_issues_count": null, "max_forks_count": 2, "avg_line_length": 26.523255814, "max_line_length": 73, "alphanum_fraction": 0.6488382288}
sap.ui.define([
	"sap/ui/core/Control"
], function(Control) {

	/**
	 * Helper object for dynamic actions. Helper objects are designed
	 * to be injected inside the control tree in order to perform
	 * actions based on the binding context of the parent element.
	 * @class
	 * @abstract
	 * @name spet.auth.helper.BaseDynamicActionHelper
	 * @property	{boolean|Deferred}	role	The role value. If
	 * a deferred object is given, then the actions are executed only
	 * when the deferred is resolved.
	 * @property	{spet.auth.IAction} whenPresent	The action that 
	 * shoudl be executed when the role is present.
	 * @property	{spet.auth.IAction}	whenMissing	The action that 
	 * should be executed when the role is missing.
	 */
	return Control.extend("spet.auth.helper.BaseDynamicActionHelper", {
		metadata: {
			properties: {
				role: {
					type: "any"
				},
				whenPresent: {
					type: "spet.auth.IAction"
				},
				whenMissing: {
					type: "spet.auth.IAction"
				}
			},
			associations: {
				view: {
					type: "sap.ui.core.mvc.View",
					multiple: false
				}
			}
		},

		_counter: 0,

		setRole: function(oValue) {
			this.setProperty("role", oValue);
			jQuery.when(oValue).then(this.updateRole.bind(this, ++this._counter));
		},

		/**
		 * Called when the role is updated.
		 * @name spet.auth.helper.BaseDynamicActionHelper#updateRole
		 * @param	{integer}	iCounter	The request counter
		 * at the moment when the role update was received. This 
		 * is used to ensure that interleaving promises do not
		 * cause unexpected behavior.
		 * @returns {void}
		 */
		updateRole: function(iCounter, bRole) {
			if (iCounter === this._counter) {
				this.setProperty("role", bRole);
				this.refresh();
			}
		},
		
		/**
		 * Re-applies the actions to the control tree.
		 * @name spet.auth.helper.BaseDynamicActionHelper#refresh
		 * @returns {void}
		 */
		refresh: function() {
			var oAction = null,
				oView = sap.ui.getCore().byId(this.getView());
			if (this.getRole() === true) {
				oAction = this.getWhenPresent();
			} else if (this.getRole() === false) {
				oAction = this.getWhenMissing();
			}
			if (oAction) {
				this.getSelectedElements().forEach(function(oC) {
					oAction.process(oView, oC);
				});
			}
		}

	});

}, /* bExport = */ false);