@quadrel-enterprise-ui/framework 21.12.15 → 21.12.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9110,6 +9110,12 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.17", ngImpo
9110
9110
  * view; the guard is released once the focused element blurs, so a newly opened view can
9111
9111
  * autofocus again. Using it on more than one active element at a time logs a warning and is
9112
9112
  * ignored for the surplus elements.
9113
+ *
9114
+ * An element that is not in the document does not take part, e.g. a field on a page tab that is
9115
+ * not shown. So every tab can have its own autofocus field, and the shown tab gets the focus.
9116
+ * This happens only when the view opens. A tab that the user opens later does not take the focus.
9117
+ *
9118
+ * An element that cannot take the focus, e.g. because it is hidden, frees the guard again.
9113
9119
  */
9114
9120
  class QdAutofocusDirective {
9115
9121
  autofocusService = inject(QdAutofocusService);
@@ -9117,34 +9123,52 @@ class QdAutofocusDirective {
9117
9123
  qdAutofocus = false;
9118
9124
  _timeoutId;
9119
9125
  _blurHandler;
9126
+ _hasClaimedAutofocus = false;
9120
9127
  ngAfterViewInit() {
9121
9128
  if (!this.qdAutofocus)
9122
9129
  return;
9123
- if (this.autofocusService.isAutofocusActivated) {
9124
- console.warn('Quadrel Framework | QdAutofocus - "hasAutofocus" can be used only once.');
9130
+ if (this.elementRef.nativeElement.isConnected && !this.claimAutofocus())
9125
9131
  return;
9126
- }
9127
- this.autofocusService.isAutofocusActivated = true;
9128
- this._timeoutId = setTimeout(() => {
9129
- const element = this.elementRef.nativeElement;
9130
- if (!element.isConnected) {
9131
- this.autofocusService.isAutofocusActivated = false;
9132
- return;
9133
- }
9134
- element.focus();
9135
- this._blurHandler = () => {
9136
- this.autofocusService.isAutofocusActivated = false;
9137
- };
9138
- element.addEventListener('blur', this._blurHandler, { once: true });
9139
- }, 0);
9132
+ this._timeoutId = setTimeout(() => this.focusIfShown(), 0);
9140
9133
  }
9141
9134
  ngOnDestroy() {
9142
9135
  if (this._timeoutId)
9143
9136
  clearTimeout(this._timeoutId);
9144
9137
  if (this._blurHandler)
9145
9138
  this.elementRef.nativeElement.removeEventListener('blur', this._blurHandler);
9139
+ this.releaseAutofocus();
9140
+ }
9141
+ claimAutofocus() {
9142
+ if (this.autofocusService.isAutofocusActivated) {
9143
+ console.warn('Quadrel Framework | QdAutofocus - "hasAutofocus" can be used only once.');
9144
+ return false;
9145
+ }
9146
+ this.autofocusService.isAutofocusActivated = true;
9147
+ this._hasClaimedAutofocus = true;
9148
+ return true;
9149
+ }
9150
+ releaseAutofocus() {
9151
+ if (!this._hasClaimedAutofocus)
9152
+ return;
9153
+ this._hasClaimedAutofocus = false;
9146
9154
  this.autofocusService.isAutofocusActivated = false;
9147
9155
  }
9156
+ focusIfShown() {
9157
+ const element = this.elementRef.nativeElement;
9158
+ if (!element.isConnected) {
9159
+ this.releaseAutofocus();
9160
+ return;
9161
+ }
9162
+ if (!this._hasClaimedAutofocus && !this.claimAutofocus())
9163
+ return;
9164
+ element.focus();
9165
+ if (element.ownerDocument.activeElement !== element) {
9166
+ this.releaseAutofocus();
9167
+ return;
9168
+ }
9169
+ this._blurHandler = () => this.releaseAutofocus();
9170
+ element.addEventListener('blur', this._blurHandler, { once: true });
9171
+ }
9148
9172
  static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.17", ngImport: i0, type: QdAutofocusDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
9149
9173
  static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "21.2.17", type: QdAutofocusDirective, isStandalone: false, selector: "[qdAutofocus]", inputs: { qdAutofocus: "qdAutofocus" }, ngImport: i0 });
9150
9174
  }