framework7
Version:
Full featured mobile HTML framework for building iOS & Android apps
2 lines (1 loc) • 5.03 kB
JavaScript
(function framework7ComponentLoader(t,e){void 0===e&&(e=!0);var o=t.$,a=t.utils,r=(t.getDevice,t.getSupport,t.Class,t.Modal,t.ConstructorMethods,t.ModalMethods,t.$jsx,a.serializeObject),n=a.extend;const i={store(t,e){let a=t;const r=o(t);r.length&&r.is("form")&&r.attr("id")&&(a=r.attr("id")),this.form.data[`form-${a}`]=e,window.localStorage[`f7form-${a}`]=JSON.stringify(e)},get(t){const e=this;let a=t;const r=o(t);return r.length&&r.is("form")&&r.attr("id")&&(a=r.attr("id")),window.localStorage[`f7form-${a}`]?JSON.parse(window.localStorage[`f7form-${a}`]):e.form.data[`form-${a}`]?e.form.data[`form-${a}`]:void 0},remove(t){const e=this;let a=t;const r=o(t);r.length&&r.is("form")&&r.attr("id")&&(a=r.attr("id")),e.form.data[`form-${a}`]&&(e.form.data[`form-${a}`]="",delete e.form.data[`form-${a}`]),window.localStorage[`f7form-${a}`]&&(window.localStorage[`f7form-${a}`]="",window.localStorage.removeItem(`f7form-${a}`))}},s={init(t){const e=this,a=o(t),r=a.attr("id");if(!r)return;const n=e.form.getFormData(r);n&&e.form.fillFromData(a,n),a.on("change submit",(function(){const t=e.form.convertToData(a);t&&(e.form.storeFormData(r,t),a.trigger("form:storedata",t),e.emit("formStoreData",a[0],t))}))},destroy(t){o(t).off("change submit")}};function f(t){const e=o(t).eq(0);if(0===e.length)return;const a={},r=["submit","image","button","file"],n=[];return e.find("input, select, textarea").each((t=>{const i=o(t);if(i.hasClass("ignore-store-data")||i.hasClass("no-store-data"))return;const s=i.attr("name"),f=i.attr("type"),m=t.nodeName.toLowerCase();if(!(r.indexOf(f)>=0)&&!(n.indexOf(s)>=0)&&s)if("select"===m&&i.prop("multiple"))n.push(s),a[s]=[],e.find(`select[name="${s}"] option`).each((t=>{t.selected&&a[s].push(t.value)}));else switch(f){case"checkbox":n.push(s),a[s]=[],e.find(`input[name="${s}"]`).each((t=>{t.checked&&a[s].push(t.value)}));break;case"radio":n.push(s),e.find(`input[name="${s}"]`).each((t=>{t.checked&&(a[s]=t.value)}));break;default:a[s]=i.val()}})),e.trigger("form:todata",a),this.emit("formToData",e[0],a),a}function m(t,e){const a=this,r=o(t).eq(0);if(!r.length)return;let n=e;const i=r.attr("id");if(!n&&i&&(n=a.form.getFormData(i)),!n)return;const s=["submit","image","button","file"],f=[];r.find("input, select, textarea").each((t=>{const e=o(t);if(e.hasClass("ignore-store-data")||e.hasClass("no-store-data"))return;const a=e.attr("name"),i=e.attr("type"),m=t.nodeName.toLowerCase();if(void 0!==n[a]&&null!==n[a]&&!(s.indexOf(i)>=0)&&!(f.indexOf(a)>=0)&&a){if("select"===m&&e.prop("multiple"))f.push(a),r.find(`select[name="${a}"] option`).each((t=>{const e=t;n[a].indexOf(t.value)>=0?e.selected=!0:e.selected=!1}));else switch(i){case"checkbox":f.push(a),r.find(`input[name="${a}"]`).each((t=>{const e=t;n[a].indexOf(t.value)>=0?e.checked=!0:e.checked=!1}));break;case"radio":f.push(a),r.find(`input[name="${a}"]`).each((t=>{const e=t;n[a]===t.value?e.checked=!0:e.checked=!1}));break;default:e.val(n[a])}"select"!==m&&"input"!==m&&"textarea"!==m||e.trigger("change","fromdata")}})),r.trigger("form:fromdata",n),a.emit("formFromData",r[0],n)}function c(){const t=this;o(document).on("submit change","form.form-ajax-submit, form.form-ajax-submit-onchange",(function(e,a){const n=o(this);if("change"===e.type&&!n.hasClass("form-ajax-submit-onchange"))return;if("submit"===e.type&&e.preventDefault(),"change"===e.type&&"fromdata"===a)return;const i=(n.attr("method")||"GET").toUpperCase(),s=n.prop("enctype")||n.attr("enctype"),f=n.attr("action");if(!f)return;let m;m="POST"===i?"application/x-www-form-urlencoded"===s?t.form.convertToData(n[0]):new window.FormData(n[0]):r(t.form.convertToData(n[0])),t.request({method:i,url:f,contentType:s,data:m,beforeSend(e){n.trigger("formajax:beforesend",{data:m,xhr:e}),t.emit("formAjaxBeforeSend",n[0],m,e)},error(e){n.trigger("formajax:error",{data:m,xhr:e}),t.emit("formAjaxError",n[0],m,e)},complete(e){n.trigger("formajax:complete",{data:m,xhr:e}),t.emit("formAjaxComplete",n[0],m,e)},success(e,o,a){n.trigger("formajax:success",{data:m,xhr:a}),t.emit("formAjaxSuccess",n[0],m,a)}})}))}var d={name:"form",create(){const t=this;n(t,{form:{data:{},storeFormData:i.store.bind(t),getFormData:i.get.bind(t),removeFormData:i.remove.bind(t),convertToData:f.bind(t),fillFromData:m.bind(t),storage:{init:s.init.bind(t),destroy:s.destroy.bind(t)}}})},on:{init(){c.call(this)},tabBeforeRemove(t){const e=this;o(t).find(".form-store-data").each((t=>{e.form.storage.destroy(t)}))},tabMounted(t){const e=this;o(t).find(".form-store-data").each((t=>{e.form.storage.init(t)}))},pageBeforeRemove(t){const e=this;t.$el.find(".form-store-data").each((t=>{e.form.storage.destroy(t)}))},pageInit(t){const e=this;t.$el.find(".form-store-data").each((t=>{e.form.storage.init(t)}))}}};if(e){if(t.prototype.modules&&t.prototype.modules[d.name])return;t.use(d),t.instance&&(t.instance.useModuleParams(d,t.instance.params),t.instance.useModule(d))}return d}(Framework7, typeof Framework7AutoInstallComponent === 'undefined' ? undefined : Framework7AutoInstallComponent))