/**
* A FORM COMPONENT
* <BR><BR><img src=../images/form.png width=40% style="border:1px lime dashed";>
* <BR><BR><a href="../html/form.html">DEMO</a>
*/
class Form extends HTMLElement {
constructor() {
console.group("Form.constructor")
super();
console.groupEnd();
};
/**
* Set observable values here. When Changed, attributeChangedCallback is invoked
* @observedAttributes
*/
static get observedAttributes() {
console.group("Form.observedAttributes");
this.observables = [];
console.log(this.observables);
console.groupEnd();
return this.observables;
};
/**
* This function is called when this is attached to DOM
* @connectedCallback.
*/
connectedCallback() {
console.group("Form.connectedCallback")
let self = this;
// FETCH ALL INTERESTING ELEMENTS
this._fetchElements();
// FETCH ALL ATTRIBUTES
this._fetchAttributes();
// REPLACE CONTENT IF NECESSARY WITH NEW STUFF
this.innerHTML = `
<form id='${this.id}-actual' class="${this.properties.class}" name='${this.properties.name}' method='${this.properties.method}' action='${this.properties.action}'>
${this.innerHTML}
<div id='${this.id}-results'></div>
</form>`
if (this.properties.action !== "undefined") {
let form = this.querySelector("form");
$(form).validator().on('submit', function (e) {
if (e.isDefaultPrevented()) {
// handle the invalid form...
} else {
e.preventDefault();
$(form).ajaxSubmit({
target: "#" + form.id + "-results"
});
}
})
}
// TRANSFER ALL ATTRIBUTES NOW
let widget = this.querySelector("form");
for (var key in this.propertiesW) {
this.removeAttribute(key);
if (key != "class") {
widget.setAttribute(key, this.properties[key]);
}
}
// REPLACE ALL PATTERNS WITH ACTUAL REGEX STRINGS
let patterns = this.querySelectorAll("[pattern]");
$(patterns).each(function() {
let pattern = this.getAttribute("pattern");
console.log("---", "fpattern." + pattern);
this.setAttribute("pattern", eval("fpattern." + pattern));
});
this._finalize();
// SHOW IT NOW (NO FLICKERS)
this.style.visibility = "visible";
console.groupEnd();
};
/**
* Invoked When component is removed. Usually with a .remove() function call
* @disconnectedCallback
*/
disconnectedCallback() {
console.group("Form.disconnectedCallback")
/* CLEAN UP NOW */
console.groupEnd();
};
/**
* Called with .setAttribute(...) function call
* @attributeChangedCallback
*/
attributeChangedCallback(attr, oldval, newval) {
console.group("Form.attributeChangedCallback:", attr, oldval, newval);
this.properties = this.properties || [];
let obs = Form.observedAttributes;
for (let i = 0; i < obs.length; i++) {
this.properties[obs[i]] = newval;
console.log(obs[i] + ": " + this.properties.background);
// YOUR CODE FOR CHANGES GO HERE
}
console.groupEnd();
};
/**
* Stores DOM elements of interest for future use
* @_fetchElements
*/
_fetchElements() {
console.group("Form._fetchElements");
this.dom = this.dom || [];
this.dom.content = this.innerHTML;
console.groupEnd();
};
/**
* Component attributes are _fetched and defaults are set if undefined
* @_fetchAttributes
* @param {string} [method=POST] form method
* @param {string} [name="NOTSET"] form action
*/
_fetchAttributes() {
console.group("Form._fetchAttributes");
this.properties = {
"cname" : "Form",
"author" : "Mel Heravi",
"version" : "1.0",
"method" : "POST",
"name" : "NOTSET"
};
// SAVE WIDGET SPECIFIC PROPERTIES
this.propertiesW = [];
// SAVE ALL OTHER PROPERTIES
let attrs = wc.getAttributes(this)
for (var key in attrs) {
this.properties[key] = this.getAttribute(key);
this.propertiesW[key] = this.getAttribute(key);
console.log(key + ": " + attrs[key]);
}
console.log("attributes: ", this.properties);
console.groupEnd();
};
/**
* Destroy the instance object and artifacts
* @_destroy
*/
destroy() {
console.group("Form.destroy:", this.id);
// FREE POINTER
delete this;
// REMOVE ITEM FROM DOM
this.parentNode.removeChild(this);
console.groupEnd();
};
/**
* SAVE DATA FOR ANALYTICS
* @__finalize
*/
_finalize() {
console.group("Form._finalize:", this.id);
this.classList.add("wc");
// ADD ANALYTICS HERE
wc.getStats(this, this.properties.cname, this.properties.version);
// TURN ON VALIDATOR
setTimeout(function() {
$("form").validator("update");
}, 100);
console.groupEnd();
};
/**
* FOR TESTING PURPOSES
* @test
*/
static test() {
console.group("Form.test");
console.log("testing results will be printed here...");
console.groupEnd();
return true;
}
}
window.customElements.define('wc-form', Form);