-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mgr/dashboard v2: Add cdRequired directive to mark a form field label…
… as required. Signed-off-by: Volker Theile <vtheile@suse.com>
- Loading branch information
Showing
4 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
src/pybind/mgr/dashboard_v2/frontend/src/app/shared/directives/required.directive.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { RequiredDirective } from './required.directive'; | ||
|
||
describe('RequiredDirective', () => { | ||
it('should create an instance', () => { | ||
const directive = new RequiredDirective(null, null); | ||
expect(directive).toBeTruthy(); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
src/pybind/mgr/dashboard_v2/frontend/src/app/shared/directives/required.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Directive, ElementRef, Input, OnInit, Renderer2 } from '@angular/core'; | ||
|
||
/** | ||
* This directive adds a '*' to the label of a form field to indicate | ||
* that this field is required. | ||
* | ||
* Example: | ||
* <div class="form-group"> | ||
* <label i18n | ||
* class="control-label col-sm-3" | ||
* for="bucket">Name</label> | ||
* <div class="col-sm-9"> | ||
* <input id="bucket" | ||
* name="bucket" | ||
* class="form-control" | ||
* type="text" | ||
* placeholder="Name..." | ||
* [(ngModel)]="bucket.bucket" | ||
* autofocus | ||
* cdRequired="bucket"> | ||
* </div> | ||
* </div> | ||
*/ | ||
@Directive({ | ||
selector: '[cdRequired]' | ||
}) | ||
export class RequiredDirective implements OnInit { | ||
|
||
constructor(private el: ElementRef, private renderer: Renderer2) { } | ||
|
||
ngOnInit() { | ||
const selector = `label[for='${this.el.nativeElement.id}']`; | ||
const labelElement = document.body.querySelector(selector); | ||
if (labelElement) { | ||
const spanElement = this.renderer.createElement('span'); | ||
this.renderer.addClass(spanElement, 'fa'); | ||
this.renderer.addClass(spanElement, 'fa-asterisk'); | ||
this.renderer.addClass(spanElement, 'required'); | ||
this.renderer.appendChild(labelElement, spanElement); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters