Comparing version 0.2.1 to 0.2.2
@@ -1,2 +0,2 @@ | ||
/*! ajaxable - 0.2.1 */ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.ajaxable=t():e.ajaxable=t()}(this,function(){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return e[r].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{"default":e}}var o=n(1),i=r(o);e.exports=function(e,t){return new i["default"](e,t)}},function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{"default":e}}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function s(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}Object.defineProperty(t,"__esModule",{value:!0});var a=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),u=n(2),f=r(u),c=function(e){function t(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";o(this,t);var r=i(this,(t.__proto__||Object.getPrototypeOf(t)).call(this));if(!e)throw new Error("The element is empty");var s={responseType:"json",headers:{}},a=n||{};for(var u in s)u in a||(a[u]=s[u]);var f="X-Requested-With";""!=a.headers[f]&&(a.headers[f]="XMLHttpRequest"),r.els=r.parseEl(e),r.opts=a;for(var c=0;c<r.els.length;c++)r.bindForm(r.els[c]);return r}return s(t,e),a(t,[{key:"onStart",value:function(e){return this.on("start",e)}},{key:"onEnd",value:function(e){return this.on("end",e)}},{key:"onResponse",value:function(e){return this.on("response",e)}},{key:"onError",value:function(e){return this.on("error",e)}},{key:"submit",value:function(){for(var e=0;e<this.els.length;e++)this.els[e].dispatchEvent(new window.Event("submit"))}},{key:"parseEl",value:function(e){return"string"==typeof e&&(e=document.querySelectorAll(e)),e.length||(e=[e]),e}},{key:"bindForm",value:function(e){var t=this;this.checkForm(e);var n="submit",r=function(n){if(!e.checkValidity())throw new Error("Validation failed");n.preventDefault(),t.sendForm(e)};this.removeListeners(e,n),this.addListener(e,n,r)}},{key:"sendForm",value:function(e){var t=this,n=this.fetchData(e),r=new XMLHttpRequest,o=this.opts.headers;this._ar++;var i={el:e,req:r,activeRequests:this._ar,requestData:this.fetchFormData(n)};this.emit("start",i),r.addEventListener("progress",function(n){return t.emit("progress",n,e,r)}),r.addEventListener("load",function(e){var n="json"==t.opts.responseType,o="";try{o=n?JSON.parse(r.responseText):r.response}catch(s){return void t.emit("error",s,i)}t.emit("response",o,i)}),r.addEventListener("error",function(e){return t.emit("error",e,i)}),r.addEventListener("loadend",function(e){t._ar--,i.activeRequests=t._ar,t.emit("end",i)}),r.open(e.method,e.action);for(var s in o)r.setRequestHeader(s,o[s]);r.send(n)}},{key:"fetchData",value:function(e){this.checkForm(e);var t=new window.FormData(e);return t}},{key:"fetchFormData",value:function(e){var t={};if(e.entries){var n=!0,r=!1,o=void 0;try{for(var i,s=e.entries()[Symbol.iterator]();!(n=(i=s.next()).done);n=!0){var a=i.value;t[a[0]]=a[1]}}catch(u){r=!0,o=u}finally{try{!n&&s["return"]&&s["return"]()}finally{if(r)throw o}}}return t}},{key:"checkForm",value:function(e){if(!(e&&e instanceof window.HTMLFormElement))throw new Error("The element is not a valid form")}},{key:"addListener",value:function(e,t,n){e in this._eh||(this._eh[e]={}),t in this._eh[e]||(this._eh[e][t]=[]),this._eh[e][t].push(n),e.addEventListener(t,n)}},{key:"removeListeners",value:function(e,t){if(e in this._eh){var n=this._eh[e];if(t in n)for(var r=n[t],o=r.length;o--;){var i=r[o];e.removeEventListener(t,i)}}}}]),t}(f["default"]);c.prototype._eh={},c.prototype._ar=0,t["default"]=c},function(e,t){function n(){}n.prototype={on:function(e,t,n){var r=this.e||(this.e={});return(r[e]||(r[e]=[])).push({fn:t,ctx:n}),this},once:function(e,t,n){function r(){o.off(e,r),t.apply(n,arguments)}var o=this;return r._=t,this.on(e,r,n)},emit:function(e){var t=[].slice.call(arguments,1),n=((this.e||(this.e={}))[e]||[]).slice(),r=0,o=n.length;for(r;r<o;r++)n[r].fn.apply(n[r].ctx,t);return this},off:function(e,t){var n=this.e||(this.e={}),r=n[e],o=[];if(r&&t)for(var i=0,s=r.length;i<s;i++)r[i].fn!==t&&r[i].fn._!==t&&o.push(r[i]);return o.length?n[e]=o:delete n[e],this}},e.exports=n}])}); | ||
/*! ajaxable - 0.2.2 */ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.ajaxable=t():e.ajaxable=t()}(this,function(){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return e[r].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{"default":e}}var o=n(1),i=r(o);e.exports=function(e,t){return new i["default"](e,t)}},function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{"default":e}}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function s(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}Object.defineProperty(t,"__esModule",{value:!0});var a=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),u=n(2),f=r(u),c=function(e){function t(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";o(this,t);var r=i(this,(t.__proto__||Object.getPrototypeOf(t)).call(this));if(!e)throw new Error("The element is empty");var s={responseType:"json",headers:{}},a=n||{};for(var u in s)u in a||(a[u]=s[u]);var f="X-Requested-With";""!=a.headers[f]&&(a.headers[f]="XMLHttpRequest"),r.els=r.parseEl(e),r.opts=a;for(var c=0;c<r.els.length;c++)r.bindForm(r.els[c]);return r}return s(t,e),a(t,[{key:"onStart",value:function(e){return this.on("start",e)}},{key:"onEnd",value:function(e){return this.on("end",e)}},{key:"onResponse",value:function(e){return this.on("response",e)}},{key:"onError",value:function(e){return this.on("error",e)}},{key:"submit",value:function(){for(var e=0;e<this.els.length;e++)this.send(this.els[e])}},{key:"send",value:function(e){var t="_aj_btn",n=e.querySelector("#"+t);n||(n=e.appendChild(document.createElement("button")),n.id=t,n.style.display="none"),n.click()}},{key:"parseEl",value:function(e){return"string"==typeof e&&(e=document.querySelectorAll(e)),e.length||(e=[e]),e}},{key:"bindForm",value:function(e){var t=this;this.checkForm(e);var n="submit",r=function(n){e.checkValidity()&&(n.preventDefault(),t.sendForm(e))};this.removeListeners(e,n),this.addListener(e,n,r)}},{key:"sendForm",value:function(e){var t=this,n=this.fetchData(e),r=new XMLHttpRequest,o=this.opts.headers;this._ar++;var i={el:e,req:r,activeRequests:this._ar,requestData:this.fetchFormData(n)};this.emit("start",i),r.addEventListener("progress",function(n){return t.emit("progress",n,e,r)}),r.addEventListener("load",function(e){var n="json"==t.opts.responseType,o="";try{o=n?JSON.parse(r.responseText):r.response}catch(s){return void t.emit("error",s,i)}t.emit("response",o,i)}),r.addEventListener("error",function(e){return t.emit("error",e,i)}),r.addEventListener("loadend",function(e){t._ar--,i.activeRequests=t._ar,t.emit("end",i)}),r.open(e.method,e.action);for(var s in o)r.setRequestHeader(s,o[s]);r.send(n)}},{key:"fetchData",value:function(e){this.checkForm(e);var t=new window.FormData(e);return t}},{key:"fetchFormData",value:function(e){var t={};if(e.entries){var n=!0,r=!1,o=void 0;try{for(var i,s=e.entries()[Symbol.iterator]();!(n=(i=s.next()).done);n=!0){var a=i.value;t[a[0]]=a[1]}}catch(u){r=!0,o=u}finally{try{!n&&s["return"]&&s["return"]()}finally{if(r)throw o}}}return t}},{key:"checkForm",value:function(e){if(!(e&&e instanceof window.HTMLFormElement))throw new Error("The element is not a valid form")}},{key:"addListener",value:function(e,t,n){e in this._eh||(this._eh[e]={}),t in this._eh[e]||(this._eh[e][t]=[]),this._eh[e][t].push(n),e.addEventListener(t,n)}},{key:"removeListeners",value:function(e,t){if(e in this._eh){var n=this._eh[e];if(t in n)for(var r=n[t],o=r.length;o--;){var i=r[o];e.removeEventListener(t,i)}}}}]),t}(f["default"]);c.prototype._eh={},c.prototype._ar=0,t["default"]=c},function(e,t){function n(){}n.prototype={on:function(e,t,n){var r=this.e||(this.e={});return(r[e]||(r[e]=[])).push({fn:t,ctx:n}),this},once:function(e,t,n){function r(){o.off(e,r),t.apply(n,arguments)}var o=this;return r._=t,this.on(e,r,n)},emit:function(e){var t=[].slice.call(arguments,1),n=((this.e||(this.e={}))[e]||[]).slice(),r=0,o=n.length;for(r;r<o;r++)n[r].fn.apply(n[r].ctx,t);return this},off:function(e,t){var n=this.e||(this.e={}),r=n[e],o=[];if(r&&t)for(var i=0,s=r.length;i<s;i++)r[i].fn!==t&&r[i].fn._!==t&&o.push(r[i]);return o.length?n[e]=o:delete n[e],this}},e.exports=n}])}); |
{ | ||
"name": "ajaxable", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Make your form instantly ajaxable", | ||
@@ -5,0 +5,0 @@ "main": "dist/ajaxable.min.js", |
@@ -108,3 +108,3 @@ import EventEmitter from 'tiny-emitter'; | ||
for (let i = 0; i < this.els.length; i++) { | ||
this.els[i].dispatchEvent(new window.Event('submit')); | ||
this.send(this.els[i]); | ||
} | ||
@@ -114,2 +114,21 @@ } | ||
/** | ||
* Trigger form submit. | ||
* If I need to submit form programmatically and trigger | ||
* HTML5 Validation .submit() doesn't work, it's necessary | ||
* to 'click()' on a submitable element. | ||
* @param {HTMLFormElement} el Form element | ||
* @private | ||
*/ | ||
send(el) { | ||
const id = '_aj_btn'; | ||
let subEl = el.querySelector('#' + id); | ||
if(!subEl){ | ||
subEl = el.appendChild(document.createElement('button')); | ||
subEl.id = id; | ||
subEl.style.display = 'none'; | ||
} | ||
subEl.click(); | ||
} | ||
/** | ||
* Parse element data and return iterable object | ||
@@ -141,13 +160,3 @@ * @param {string|HTMLFormElement} el Form/s element | ||
this.sendForm(el); | ||
}else{ | ||
throw new Error('Validation failed'); | ||
/* | ||
// If I need to submit form programmatically and trigger | ||
// HTML5 Validation .submit() doesn't work, it's necessary to .click() | ||
// on submitable element. | ||
// Solution: As some forms could not have a submit button I need to create | ||
// one, hide it and execute .click() | ||
el.querySelector('button').click(); | ||
*/ | ||
} | ||
} | ||
}; | ||
@@ -154,0 +163,0 @@ this.removeListeners(el, ev); |
Sorry, the diff of this file is not supported yet
26576
488