Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to pass in boolean to noDirtyCheck directive #8638

Merged
merged 16 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

function onInit() {
vm.inputId = vm.inputId || "umb-check_" + String.CreateGuid();

vm.disableDirtyCheck = Object.toBoolean(vm.disableDirtyCheck) === true;
vm.icon = vm.icon || vm.iconClass || null;

// If a labelKey is passed let's update the returned text if it's does not contain an opening square bracket [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

function onInit() {
vm.inputId = vm.inputId || "umb-radio_" + String.CreateGuid();

vm.disableDirtyCheck = Object.toBoolean(vm.disableDirtyCheck) === true;
vm.icon = vm.icon || vm.iconClass || null;

// If a labelKey is passed let's update the returned text if it's does not contain an opening square bracket [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ function noDirtyCheck() {
require: 'ngModel',
link: function (scope, elm, attrs, ctrl) {

var alwaysFalse = {
get: function () { return false; },
set: function () { }
};
Object.defineProperty(ctrl, '$pristine', alwaysFalse);
Object.defineProperty(ctrl, '$dirty', alwaysFalse);
// If no attribute value is "false", then skip and use default behaviour.
var dirtyCheck = scope.$eval(attrs.noDirtyCheck) === false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be a native JS function to evaluate a boolean instead of depending on AngularJS to do it for us?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, probably. However I have noticed the $eval function is used several other places to parse attribute values.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the above changes in mind, shouldn't this be var dirtyCheck = Object.toBoolean(attrs.noDirtyCheck) === false; ?

if (dirtyCheck)
return;

ctrl.$setDirty = Utilities.noop;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I'm sure this works, I'm a tad worried it's too good to be true. The quite intricate solution with the alwaysFalse binding to $pristine and $dirty must be there for a reason... this is just a hunch, but I'm afraid your solution over simplifies things. Wouldn't it be prudent to keep the old solution here, just after the conditional dirty check handling?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if there is a specific reason it change $pristine. In other alternatives of no-dirty-check directives I have seen in only change $dirty. Regarding the "Unsaved changes" dialog it only rely on $dirty property as far as I know.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep I agree with you. As I tried to explain, it should work fine, but the current solution seems quite explicitly done as-is. There can be two reasons for this: Either the current solution is over complicated and implemented as such due to lack of knowledge/understanding, or else the current solution covers edge cases we can't right now. My point is it can't hurt to retain the current solution from a better-safe-than-sorry standpoint?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, not sure if $pristine has any effect on the "Unsaved changes" dialog though. The reason we have the no-dirty-check directive is to prevent the "Unsaved changes" dialog to show in certain circumstances.

It seems it only rely on $dirty before opening the overlay here:

if (!formCtrl.$dirty && infiniteEditors.length === 0 || isSavingNewItem && infiniteEditors.length === 0) {
return;
}

}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
<label class="checkbox umb-form-check umb-form-check--checkbox {{vm.cssClass}}" ng-class="{ 'umb-form-check--disabled': vm.disabled }">
<span class="umb-form-check__symbol">
<input ng-if="vm.disableDirtyCheck"
type="checkbox"
id="{{vm.inputId}}"
name="{{vm.name}}"
value="{{vm.value}}"
class="umb-form-check__input"
val-server-field="{{vm.serverValidationField}}"
ng-model="vm.model"
ng-disabled="vm.disabled"
ng-required="vm.required"
ng-change="vm.change()"
no-dirty-check />

<input ng-if="!vm.disableDirtyCheck"
type="checkbox"
id="{{vm.inputId}}"
name="{{vm.name}}"
value="{{vm.value}}"
class="umb-form-check__input"
val-server-field="{{vm.serverValidationField}}"
ng-model="vm.model"
ng-disabled="vm.disabled"
ng-required="vm.required"
ng-change="vm.change()"/>
<input type="checkbox"
id="{{vm.inputId}}"
name="{{vm.name}}"
value="{{vm.value}}"
class="umb-form-check__input"
val-server-field="{{vm.serverValidationField}}"
ng-model="vm.model"
ng-disabled="vm.disabled"
ng-required="vm.required"
ng-change="vm.change()"
no-dirty-check="{{vm.disableDirtyCheck}}" />

<span class="umb-form-check__state" aria-hidden="true">
<span class="umb-form-check__check">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
<label class="radio umb-form-check umb-form-check--radiobutton {{vm.cssClass}}" ng-class="{ 'umb-form-check--disabled': vm.disabled }">
<span class="umb-form-check__symbol">
<input ng-if="vm.disableDirtyCheck"
type="radio"
id="{{vm.inputId}}"
name="{{vm.name}}"
value="{{vm.value}}"
class="umb-form-check__input"
val-server-field="{{vm.serverValidationField}}"
ng-model="vm.model"
ng-disabled="vm.disabled"
ng-required="vm.required"
ng-change="vm.change()"
no-dirty-check />

<input ng-if="!vm.disableDirtyCheck"
type="radio"
id="{{vm.inputId}}"
name="{{vm.name}}"
value="{{vm.value}}"
class="umb-form-check__input"
val-server-field="{{vm.serverValidationField}}"
ng-model="vm.model"
ng-disabled="vm.disabled"
ng-required="vm.required"
ng-change="vm.change()" />
<input type="radio"
id="{{vm.inputId}}"
name="{{vm.name}}"
value="{{vm.value}}"
class="umb-form-check__input"
val-server-field="{{vm.serverValidationField}}"
ng-model="vm.model"
ng-disabled="vm.disabled"
ng-required="vm.required"
ng-change="vm.change()"
no-dirty-check="{{vm.disableDirtyCheck}}" />

<span class="umb-form-check__state" aria-hidden="true">
<span class="umb-form-check__check"></span>
Expand Down