Improved Disabled template function to make it work with more complex conditions.
This commit is contained in:
@ -1203,24 +1203,45 @@ class IgniteTemplate {
|
|||||||
* @returns {IgniteTemplate} This ignite template so function calls can be chained.
|
* @returns {IgniteTemplate} This ignite template so function calls can be chained.
|
||||||
*/
|
*/
|
||||||
disabled(value, converter = null) {
|
disabled(value, converter = null) {
|
||||||
if (value instanceof IgniteProperty) {
|
IgniteRendering.push();
|
||||||
this.attribute("disabled", value, convert => {
|
|
||||||
convert = (converter != null ? converter(convert) : convert);
|
|
||||||
if (convert) {
|
|
||||||
return "disabled";
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
this.class(value, convert => {
|
let updateDisabled = (...newValues) => {
|
||||||
convert = (converter != null ? converter(convert) : convert);
|
var converted = (converter != null ? converter(...newValues) : newValues[0]);
|
||||||
if (convert) {
|
|
||||||
return "disabled";
|
if (converted) {
|
||||||
} else {
|
this._attributes["disabled"] = "disabled";
|
||||||
return null;
|
|
||||||
|
if (this._classes.indexOf("disabled") == -1) {
|
||||||
|
this._classes.push("disabled");
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
if (this.element) {
|
||||||
|
this.element.setAttribute("disabled", "disabled");
|
||||||
|
|
||||||
|
if (!this.element.classList.contains("disabled")) {
|
||||||
|
this.element.classList.add("disabled");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this._attributes["disabled"] = null;
|
||||||
|
this._classes.splice(this._classes.indexOf("disabled"), 1);
|
||||||
|
|
||||||
|
if (this.element) {
|
||||||
|
this.element.removeAttribute("disabled");
|
||||||
|
this.element.classList.remove("disabled");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (value instanceof IgniteProperty) {
|
||||||
|
this._callbacks.push(value.attachOnChange((oldValue, newValue) => updateDisabled(newValue)));
|
||||||
|
this._callbacks.push(value.attachOnPush((list, items) => updateDisabled(list)));
|
||||||
|
this._callbacks.push(value.attachOnUnshift((list, items) => updateDisabled(list)));
|
||||||
|
this._callbacks.push(value.attachOnPop(list => updateDisabled(list)));
|
||||||
|
this._callbacks.push(value.attachOnShift(list => updateDisabled(list)));
|
||||||
|
this._callbacks.push(value.attachOnSplice((list, start, deleteCount, items) => updateDisabled(list)));
|
||||||
|
|
||||||
|
updateDisabled(value.value);
|
||||||
} else if (Array.isArray(value) && value.length > 0 && value[0] instanceof IgniteProperty) {
|
} else if (Array.isArray(value) && value.length > 0 && value[0] instanceof IgniteProperty) {
|
||||||
//There must be a converter for this to work correctly
|
//There must be a converter for this to work correctly
|
||||||
if (!converter) {
|
if (!converter) {
|
||||||
@ -1230,33 +1251,22 @@ class IgniteTemplate {
|
|||||||
//Attack a callback for all the properties
|
//Attack a callback for all the properties
|
||||||
value.forEach(prop => {
|
value.forEach(prop => {
|
||||||
if (prop instanceof IgniteProperty) {
|
if (prop instanceof IgniteProperty) {
|
||||||
let updateDisabled = () => {
|
this._callbacks.push(prop.attachOnChange((oldValue, newValue) => updateDisabled(...value.getPropertyValues())));
|
||||||
this.onAttributeChanged("disabled", converter(...value.getPropertyValues()) ? "disabled" : null);
|
this._callbacks.push(prop.attachOnPush((list, items) => updateDisabled(...value.getPropertyValues())));
|
||||||
this.onClassChanged("disabled", converter(...value.getPropertyValues()) ? "disabled" : null);
|
this._callbacks.push(prop.attachOnUnshift((list, items) => updateDisabled(...value.getPropertyValues())));
|
||||||
}
|
this._callbacks.push(prop.attachOnPop((list) => updateDisabled(...value.getPropertyValues())));
|
||||||
|
this._callbacks.push(prop.attachOnShift((list) => updateDisabled(...value.getPropertyValues())));
|
||||||
this._callbacks.push(prop.attachOnChange((oldValue, newValue) => updateDisabled()));
|
this._callbacks.push(prop.attachOnSplice((list, start, deleteCount, items) => updateDisabled(...value.getPropertyValues())));
|
||||||
this._callbacks.push(prop.attachOnPush((list, items) => updateDisabled()));
|
|
||||||
this._callbacks.push(prop.attachOnUnshift((list, items) => updateDisabled()));
|
|
||||||
this._callbacks.push(prop.attachOnPop((list) => updateDisabled()));
|
|
||||||
this._callbacks.push(prop.attachOnShift((list) => updateDisabled()));
|
|
||||||
this._callbacks.push(prop.attachOnSplice((list, start, deleteCount, items) => updateDisabled()));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (converter(...value.getPropertyValues())) {
|
updateDisabled(...value.getPropertyValues());
|
||||||
this.attribute("disabled", "disabled");
|
|
||||||
this.class("disabled");
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
value = converter ? converter(value) : value;
|
updateDisabled(value);
|
||||||
|
|
||||||
if (value) {
|
|
||||||
this.attribute("disabled", "disabled");
|
|
||||||
this.class("disabled");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
IgniteRendering.pop();
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user