Comparing version 1.4.1 to 2.0.0
2208
dist/formjax.js
@@ -1,1366 +0,1424 @@ | ||
var Formjax = (function () { | ||
'use strict'; | ||
var app = (function () { | ||
'use strict'; | ||
function findParent$1(elem) { | ||
if (elem.parentNode) { | ||
// Accounting for https://bugs.webkit.org/show_bug.cgi?id=161454 | ||
var dataset = JSON.parse(JSON.stringify(elem.parentNode.dataset)); | ||
if (dataset.module) { | ||
return elem.parentNode; | ||
function _typeof(obj) { | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
} | ||
return findParent$1(elem.parentNode); | ||
return _typeof(obj); | ||
} | ||
return elem; | ||
} | ||
/* global window */ | ||
var attrObj = function (key, el) { | ||
var values = {}; | ||
Object.keys(el.dataset).forEach(function (data) { | ||
if (data.match(new RegExp('^' + key)) && data !== key) { | ||
var optionName = data.replace(key, ''); | ||
var isGlobal = false; | ||
if (optionName.match(/^Global/)) { | ||
optionName = optionName.replace('Global', ''); | ||
isGlobal = true; | ||
} | ||
optionName = '' + optionName[0].toLowerCase() + optionName.slice(1); | ||
if (isGlobal) { | ||
values[optionName] = window[el.dataset[data]]; | ||
} else { | ||
values[optionName] = el.dataset[data]; | ||
} | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}); | ||
return values; | ||
}; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { | ||
return typeof obj; | ||
} : function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
var asyncGenerator = function () { | ||
function AwaitValue(value) { | ||
this.value = value; | ||
} | ||
function AsyncGenerator(gen) { | ||
var front, back; | ||
function send(key, arg) { | ||
return new Promise(function (resolve, reject) { | ||
var request = { | ||
key: key, | ||
arg: arg, | ||
resolve: resolve, | ||
reject: reject, | ||
next: null | ||
}; | ||
if (back) { | ||
back = back.next = request; | ||
} else { | ||
front = back = request; | ||
resume(key, arg); | ||
} | ||
}); | ||
function _defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
function resume(key, arg) { | ||
try { | ||
var result = gen[key](arg); | ||
var value = result.value; | ||
function _createClass(Constructor, protoProps, staticProps) { | ||
if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) _defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
} | ||
if (value instanceof AwaitValue) { | ||
Promise.resolve(value.value).then(function (arg) { | ||
resume("next", arg); | ||
}, function (arg) { | ||
resume("throw", arg); | ||
}); | ||
} else { | ||
settle(result.done ? "return" : "normal", result.value); | ||
} | ||
} catch (err) { | ||
settle("throw", err); | ||
} | ||
function _inherits(subClass, superClass) { | ||
if (typeof superClass !== "function" && superClass !== null) { | ||
throw new TypeError("Super expression must either be null or a function"); | ||
} | ||
function settle(type, value) { | ||
switch (type) { | ||
case "return": | ||
front.resolve({ | ||
value: value, | ||
done: true | ||
}); | ||
break; | ||
case "throw": | ||
front.reject(value); | ||
break; | ||
default: | ||
front.resolve({ | ||
value: value, | ||
done: false | ||
}); | ||
break; | ||
subClass.prototype = Object.create(superClass && superClass.prototype, { | ||
constructor: { | ||
value: subClass, | ||
writable: true, | ||
configurable: true | ||
} | ||
front = front.next; | ||
if (front) { | ||
resume(front.key, front.arg); | ||
} else { | ||
back = null; | ||
} | ||
} | ||
this._invoke = send; | ||
if (typeof gen.return !== "function") { | ||
this.return = undefined; | ||
} | ||
}); | ||
if (superClass) _setPrototypeOf(subClass, superClass); | ||
} | ||
if (typeof Symbol === "function" && Symbol.asyncIterator) { | ||
AsyncGenerator.prototype[Symbol.asyncIterator] = function () { | ||
return this; | ||
function _getPrototypeOf(o) { | ||
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { | ||
return o.__proto__ || Object.getPrototypeOf(o); | ||
}; | ||
return _getPrototypeOf(o); | ||
} | ||
AsyncGenerator.prototype.next = function (arg) { | ||
return this._invoke("next", arg); | ||
}; | ||
function _setPrototypeOf(o, p) { | ||
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { | ||
o.__proto__ = p; | ||
return o; | ||
}; | ||
AsyncGenerator.prototype.throw = function (arg) { | ||
return this._invoke("throw", arg); | ||
}; | ||
return _setPrototypeOf(o, p); | ||
} | ||
AsyncGenerator.prototype.return = function (arg) { | ||
return this._invoke("return", arg); | ||
}; | ||
return { | ||
wrap: function (fn) { | ||
return function () { | ||
return new AsyncGenerator(fn.apply(this, arguments)); | ||
}; | ||
}, | ||
await: function (value) { | ||
return new AwaitValue(value); | ||
function _assertThisInitialized(self) { | ||
if (self === void 0) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
}; | ||
}(); | ||
return self; | ||
} | ||
function _possibleConstructorReturn(self, call) { | ||
if (call && (typeof call === "object" || typeof call === "function")) { | ||
return call; | ||
} | ||
return _assertThisInitialized(self); | ||
} | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
function _toConsumableArray(arr) { | ||
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); | ||
} | ||
}; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
function _arrayWithoutHoles(arr) { | ||
if (Array.isArray(arr)) { | ||
for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
function _iterableToArray(iter) { | ||
if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); | ||
} | ||
function _nonIterableSpread() { | ||
throw new TypeError("Invalid attempt to spread non-iterable instance"); | ||
} | ||
function findParent(elem) { | ||
if (elem.parentNode) { | ||
// Accounting for https://bugs.webkit.org/show_bug.cgi?id=161454 | ||
var dataset = JSON.parse(JSON.stringify(elem.parentNode.dataset)); | ||
if (dataset.module) { | ||
return elem.parentNode; | ||
} | ||
return findParent(elem.parentNode); | ||
} | ||
return elem; | ||
} | ||
/* global window */ | ||
function attrObj (key, el) { | ||
var values = {}; | ||
Object.keys(el.dataset).forEach(function (data) { | ||
if (data.match(new RegExp("^".concat(key))) && data !== key) { | ||
var optionName = data.replace(key, ''); | ||
var isGlobal = false; | ||
if (optionName.match(/^Global/)) { | ||
optionName = optionName.replace('Global', ''); | ||
isGlobal = true; | ||
} | ||
optionName = "".concat(optionName[0].toLowerCase()).concat(optionName.slice(1)); | ||
var inherits = function (subClass, superClass) { | ||
if (typeof superClass !== "function" && superClass !== null) { | ||
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); | ||
if (isGlobal) { | ||
values[optionName] = window[el.dataset[data]]; | ||
} else { | ||
values[optionName] = el.dataset[data]; | ||
} | ||
} | ||
}); | ||
return values; | ||
} | ||
subClass.prototype = Object.create(superClass && superClass.prototype, { | ||
constructor: { | ||
value: subClass, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; | ||
}; | ||
var aug = function aug() { | ||
var args = Array.prototype.slice.call(arguments); //eslint-disable-line prefer-rest-params | ||
var org = args.shift(); | ||
var type = ''; | ||
if (typeof org === 'string' || typeof org === 'boolean') { | ||
type = org === true ? 'deep' : org; | ||
org = args.shift(); | ||
if (type === 'defaults') { | ||
org = aug({}, org); //clone defaults into new object | ||
type = 'strict'; | ||
} | ||
} | ||
args.forEach(function (prop) { | ||
for (var propName in prop) { | ||
//eslint-disable-line | ||
var propValue = prop[propName]; // just overwrite arrays: | ||
if (Array.isArray(propValue)) { | ||
org[propName] = propValue; | ||
continue; | ||
} | ||
if (type === 'deep' && _typeof(propValue) === 'object' && typeof org[propName] !== 'undefined') { | ||
if (_typeof(org[propName]) !== 'object') { | ||
org[propName] = propValue; | ||
continue; | ||
} | ||
aug(type, org[propName], propValue); | ||
} else if (type !== 'strict' || type === 'strict' && typeof org[propName] !== 'undefined') { | ||
org[propName] = propValue; | ||
} | ||
} | ||
}); | ||
return org; | ||
}; | ||
var aug_1 = aug; | ||
var possibleConstructorReturn = function (self, call) { | ||
if (!self) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
function isWindow(obj) { | ||
return obj != null && obj === obj.window; | ||
} | ||
return call && (typeof call === "object" || typeof call === "function") ? call : self; | ||
}; | ||
function find(selector) { | ||
var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
if (selector instanceof HTMLElement || selector instanceof Node || isWindow(selector)) { | ||
return [selector]; | ||
} else if (selector instanceof NodeList) { | ||
return [].slice.call(selector); | ||
} else if (typeof selector === 'string') { | ||
var startElement = context ? find(context)[0] : document; | ||
return [].slice.call(startElement.querySelectorAll(selector)); | ||
} | ||
return []; | ||
} | ||
function on(selector, event, cb) { | ||
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false; | ||
if (Array.isArray(selector)) { | ||
selector.forEach(function (item) { | ||
return on(item, event, cb, capture); | ||
}); | ||
return; | ||
} | ||
var data = { | ||
cb: cb, | ||
capture: capture | ||
}; | ||
if (!window._domassistevents) { | ||
window._domassistevents = {}; | ||
} | ||
window._domassistevents["_".concat(event)] = data; | ||
var el = find(selector); | ||
if (el.length) { | ||
el.forEach(function (item) { | ||
item.addEventListener(event, cb, capture); | ||
}); | ||
} | ||
} | ||
function findOne(selector, el) { | ||
var found = find(selector, el); | ||
if (found.length) { | ||
return found[0]; | ||
} | ||
var toConsumableArray = function (arr) { | ||
if (Array.isArray(arr)) { | ||
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} else { | ||
return Array.from(arr); | ||
return null; | ||
} | ||
}; | ||
'use strict'; | ||
var NativeCustomEvent = window.CustomEvent; // | ||
// Check for the usage of native support for CustomEvents which is lacking | ||
// completely on IE. | ||
// | ||
var aug = function aug() { | ||
var args = Array.prototype.slice.call(arguments); //eslint-disable-line prefer-rest-params | ||
var org = args.shift(); | ||
var type = ''; | ||
if (typeof org === 'string' || typeof org === 'boolean') { | ||
type = org === true ? 'deep' : org; | ||
org = args.shift(); | ||
if (type === 'defaults') { | ||
org = aug({}, org); //clone defaults into new object | ||
type = 'strict'; | ||
} | ||
} | ||
args.forEach(function (prop) { | ||
for (var propName in prop) { | ||
//eslint-disable-line | ||
var propValue = prop[propName]; | ||
// just overwrite arrays: | ||
if (Array.isArray(propValue)) { | ||
org[propName] = propValue; | ||
continue; | ||
} | ||
if (type === 'deep' && (typeof propValue === 'undefined' ? 'undefined' : _typeof(propValue)) === 'object' && typeof org[propName] !== 'undefined') { | ||
if (_typeof(org[propName]) !== 'object') { | ||
org[propName] = propValue; | ||
continue; | ||
function canIuseNativeCustom() { | ||
try { | ||
var p = new NativeCustomEvent('t', { | ||
detail: { | ||
a: 'b' | ||
} | ||
aug(type, org[propName], propValue); | ||
} else if (type !== 'strict' || type === 'strict' && typeof org[propName] !== 'undefined') { | ||
org[propName] = propValue; | ||
} | ||
}); | ||
return p.type === 't' && p.detail.a === 'b'; | ||
} catch (e) { | ||
return false; | ||
} | ||
}); | ||
return org; | ||
}; | ||
var aug_1 = aug; | ||
} // Lousy polyfill for the Custom Event constructor for IE. | ||
function isWindow(obj) { | ||
return obj != null && obj === obj.window; | ||
} | ||
function find(selector) { | ||
var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
var IECustomEvent = function CustomEvent(type, params) { | ||
var e = document.createEvent('CustomEvent'); | ||
if (selector instanceof HTMLElement || selector instanceof Node || isWindow(selector)) { | ||
return [selector]; | ||
} else if (selector instanceof NodeList) { | ||
return [].slice.call(selector); | ||
} else if (typeof selector === 'string') { | ||
var startElement = context ? find(context)[0] : document; | ||
return [].slice.call(startElement.querySelectorAll(selector)); | ||
} | ||
return []; | ||
} | ||
if (params) { | ||
e.initCustomEvent(type, params.bubbles, params.cancelable, params.detail); | ||
} else { | ||
e.initCustomEvent(type, false, false, undefined); | ||
} | ||
function addClass(selector, cls) { | ||
if (Array.isArray(selector)) { | ||
return selector.forEach(function (item) { | ||
return addClass(item, cls); | ||
}); | ||
} | ||
var els = find(selector); | ||
if (els.length) { | ||
var clsArray = [].concat(cls); | ||
els.forEach(function (el) { | ||
clsArray.forEach(function (item) { | ||
el.classList.add(item); | ||
}); | ||
}); | ||
return els; | ||
} | ||
} | ||
function on(selector, event, cb) { | ||
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false; | ||
if (Array.isArray(selector)) { | ||
selector.forEach(function (item) { | ||
return on(item, event, cb, capture); | ||
}); | ||
return; | ||
} | ||
var data = { | ||
cb: cb, | ||
capture: capture | ||
return e; | ||
}; | ||
if (!window._domassistevents) { | ||
window._domassistevents = {}; | ||
} | ||
var DomassistCustomEvent = canIuseNativeCustom() ? NativeCustomEvent : IECustomEvent; | ||
window._domassistevents['_' + event] = data; | ||
var el = find(selector); | ||
if (el.length) { | ||
el.forEach(function (item) { | ||
item.addEventListener(event, cb, capture); | ||
}); | ||
} | ||
} | ||
var SCROLLABLE_CONTAINER; | ||
function findOne(selector, el) { | ||
var found = find(selector, el); | ||
function getScrollableContainer() { | ||
if (SCROLLABLE_CONTAINER) { | ||
return SCROLLABLE_CONTAINER; | ||
} | ||
if (found.length) { | ||
return found[0]; | ||
} | ||
var documentElement = window.document.documentElement; | ||
var scrollableContainer; | ||
documentElement.scrollTop = 1; | ||
return null; | ||
} | ||
if (documentElement.scrollTop === 1) { | ||
documentElement.scrollTop = 0; | ||
scrollableContainer = documentElement; | ||
} else { | ||
scrollableContainer = document.body; | ||
} | ||
var NativeCustomEvent = window.CustomEvent; | ||
// | ||
// Check for the usage of native support for CustomEvents which is lacking | ||
// completely on IE. | ||
// | ||
function canIuseNativeCustom() { | ||
try { | ||
var p = new NativeCustomEvent('t', { | ||
detail: { | ||
a: 'b' | ||
} | ||
}); | ||
return p.type === 't' && p.detail.a === 'b'; | ||
} catch (e) { | ||
return false; | ||
SCROLLABLE_CONTAINER = scrollableContainer; | ||
return scrollableContainer; | ||
} | ||
} | ||
// Lousy polyfill for the Custom Event constructor for IE. | ||
var IECustomEvent = function CustomEvent(type, params) { | ||
var e = document.createEvent('CustomEvent'); | ||
SCROLLABLE_CONTAINER = getScrollableContainer(); | ||
if (params) { | ||
e.initCustomEvent(type, params.bubbles, params.cancelable, params.detail); | ||
} else { | ||
e.initCustomEvent(type, false, false, undefined); | ||
} | ||
/* global DocumentTouch */ | ||
return e; | ||
}; | ||
var ACTION_SELECTOR = '[data-action]'; | ||
var DOMAssist = { | ||
find: find, | ||
findOne: findOne, | ||
on: on | ||
}; | ||
var DomassistCustomEvent = canIuseNativeCustom() ? NativeCustomEvent : IECustomEvent; | ||
var Domodule = | ||
/*#__PURE__*/ | ||
function () { | ||
function Domodule(el) { | ||
_classCallCheck(this, Domodule); | ||
function fire(selector, type) { | ||
var params = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
this.log('begin setup'); | ||
this.el = el; | ||
this.els = {}; | ||
this.options = aug_1({}, this.defaults, attrObj('module', this.el)); | ||
this.moduleName = this.el.dataset.module; | ||
this.setUps = { | ||
actions: [], | ||
named: [], | ||
options: [] | ||
}; | ||
this.boundActionRouter = this.actionRouter.bind(this); | ||
this.preInit(); | ||
this.storeRef(); | ||
this.setupActions(); | ||
this.setupNamed(); | ||
this.verifyRequired(); | ||
this.postInit(); | ||
this.log('initalized'); | ||
if (Array.isArray(selector)) { | ||
return selector.forEach(function (item) { | ||
return fire(item, type, params); | ||
}); | ||
} | ||
if (Domodule.debug) { | ||
this.el.module = this; | ||
} | ||
var els = find(selector); | ||
if (els.length) { | ||
if (params.bubbles !== false) { | ||
params.bubbles = true; | ||
return this; | ||
} | ||
els.forEach(function (el) { | ||
var event = new DomassistCustomEvent(type, params); | ||
el.dispatchEvent(event); | ||
}); | ||
_createClass(Domodule, [{ | ||
key: "preInit", | ||
value: function preInit() {} | ||
}, { | ||
key: "postInit", | ||
value: function postInit() {} | ||
}, { | ||
key: "verifyRequired", | ||
value: function verifyRequired() { | ||
var _this = this; | ||
return els; | ||
} | ||
} | ||
if (this.required === {}) { | ||
return this; | ||
} | ||
function removeClass(selector, cls) { | ||
if (Array.isArray(selector)) { | ||
return selector.forEach(function (item) { | ||
return removeClass(item, cls); | ||
}); | ||
} | ||
if (typeof this.required.options !== 'undefined') { | ||
this.setUps.options = Object.keys(this.options); | ||
} | ||
var els = find(selector); | ||
if (els.length) { | ||
var clsArray = [].concat(cls); | ||
els.forEach(function (el) { | ||
clsArray.forEach(function (item) { | ||
el.classList.remove(item); | ||
}); | ||
}); | ||
return els; | ||
} | ||
} | ||
Object.keys(this.required).forEach(function (required) { | ||
_this.required[required].forEach(function (value) { | ||
if (_this.setUps[required].indexOf(value) < 0) { | ||
throw new Error("".concat(value, " is required as ").concat(required, " for ").concat(_this.moduleName, ", but is missing!")); | ||
} | ||
}); | ||
}); | ||
return this; | ||
} | ||
}, { | ||
key: "setupActions", | ||
value: function setupActions() { | ||
var _this2 = this; | ||
var SCROLLABLE_CONTAINER = void 0; | ||
this.setupAction(this.el); | ||
this.find(ACTION_SELECTOR).forEach(function (action) { | ||
var parent = findParent(action); | ||
function getScrollableContainer() { | ||
if (SCROLLABLE_CONTAINER) { | ||
return SCROLLABLE_CONTAINER; | ||
} | ||
if (parent === _this2.el) { | ||
_this2.setupAction(action); | ||
} | ||
}); | ||
} | ||
}, { | ||
key: "setupAction", | ||
value: function setupAction(actionEl) { | ||
if (actionEl.dataset.domoduleActionProcessed === 'true') { | ||
return; | ||
} | ||
var documentElement = window.document.documentElement; | ||
var scrollableContainer = void 0; | ||
var _Domodule$parseAction = Domodule.parseAction(actionEl), | ||
actionName = _Domodule$parseAction.name, | ||
actionType = _Domodule$parseAction.type; | ||
documentElement.scrollTop = 1; | ||
if (!actionName) { | ||
return; | ||
} else if (typeof this[actionName] !== 'function') { | ||
this.log("".concat(actionName, " was registered, but there is no function set up")); | ||
return; | ||
} | ||
if (documentElement.scrollTop === 1) { | ||
documentElement.scrollTop = 0; | ||
scrollableContainer = documentElement; | ||
} else { | ||
scrollableContainer = document.body; | ||
} | ||
this.log("".concat(actionName, " bound")); | ||
this.storeSetUp(actionName, 'actions'); | ||
DOMAssist.on(actionEl, actionType, this.boundActionRouter); | ||
actionEl.dataset.domoduleActionProcessed = 'true'; | ||
} | ||
}, { | ||
key: "actionRouter", | ||
value: function actionRouter(event) { | ||
var actionEl = event.currentTarget; | ||
SCROLLABLE_CONTAINER = scrollableContainer; | ||
var _Domodule$parseAction2 = Domodule.parseAction(actionEl), | ||
actionName = _Domodule$parseAction2.name; | ||
return scrollableContainer; | ||
} | ||
var actionData = attrObj('action', actionEl); | ||
this[actionName].call(this, actionEl, event, actionData); | ||
} | ||
}, { | ||
key: "setupNamed", | ||
value: function setupNamed() { | ||
var _this3 = this; | ||
SCROLLABLE_CONTAINER = getScrollableContainer(); | ||
this.find('[data-name]').forEach(function (named) { | ||
var parent = findParent(named); | ||
/* global DocumentTouch */ | ||
if (parent !== _this3.el) { | ||
return; | ||
} | ||
function toArray$1(value) { | ||
if (!value) { | ||
return []; | ||
} | ||
if (Array.isArray(value)) { | ||
return value; | ||
} | ||
if (value instanceof Node) { | ||
return [value]; | ||
} | ||
return [].slice.call(value); | ||
} | ||
if (!named.dataset.domoduleNameProcessed) { | ||
_this3.els[named.dataset.name] = named; | ||
/* eslint no-new:0 */ | ||
var ACTION_SELECTOR = '[data-action]'; | ||
var DOMAssist = { find: find, findOne: findOne, on: on }; | ||
_this3.storeSetUp(named.dataset.name, 'named'); | ||
var Domodule = function () { | ||
function Domodule(el) { | ||
classCallCheck(this, Domodule); | ||
named.dataset.domoduleNameProcessed = 'true'; | ||
named.dataset.domoduleOwner = _this3.id; | ||
} | ||
}); | ||
} | ||
}, { | ||
key: "storeRef", | ||
value: function storeRef() { | ||
if (typeof window.domorefs === 'undefined') { | ||
window.domorefs = {}; | ||
} | ||
this.log('begin setup'); | ||
this.el = el; | ||
this.els = {}; | ||
this.options = aug_1({}, this.defaults, attrObj('module', this.el)); | ||
this.moduleName = this.el.dataset.module; | ||
this.setUps = { | ||
actions: [], | ||
named: [], | ||
options: [] | ||
}; | ||
this.boundActionRouter = this.actionRouter.bind(this); | ||
if (typeof window.domorefs[this.el.dataset.moduleUid] !== 'undefined') { | ||
return false; | ||
} | ||
this.preInit(); | ||
this.storeRef(); | ||
this.setupActions(); | ||
this.setupNamed(); | ||
this.verifyRequired(); | ||
this.postInit(); | ||
this.log('initalized'); | ||
this.id = this.uuid; | ||
this.el.dataset.moduleUid = this.id; | ||
window.domorefs[this.el.dataset.moduleUid] = this; | ||
} | ||
}, { | ||
key: "find", | ||
value: function find(selector) { | ||
return DOMAssist.find(selector, this.el); | ||
} | ||
}, { | ||
key: "findOne", | ||
value: function findOne(selector) { | ||
return DOMAssist.findOne(selector, this.el); | ||
} | ||
}, { | ||
key: "findByName", | ||
value: function findByName(name) { | ||
return this.els[name]; | ||
} | ||
}, { | ||
key: "getOption", | ||
value: function getOption(option) { | ||
return this.options[option]; | ||
} | ||
}, { | ||
key: "storeSetUp", | ||
value: function storeSetUp(name, dict) { | ||
if (this.setUps[dict].indexOf(name) < 0) { | ||
this.setUps[dict].push(name); | ||
} | ||
} | ||
}, { | ||
key: "destroy", | ||
value: function destroy() { | ||
var _this4 = this; | ||
if (Domodule.debug) { | ||
this.el.module = this; | ||
} | ||
DOMAssist.find(ACTION_SELECTOR, this.el.parentNode).forEach(function (el) { | ||
if (el.dataset.domoduleActionProcessed === 'true') { | ||
var _Domodule$parseAction3 = Domodule.parseAction(el), | ||
actionType = _Domodule$parseAction3.type; | ||
return this; | ||
} | ||
el.removeEventListener(actionType, _this4.boundActionRouter); | ||
el.dataset.domoduleActionProcessed = 'false'; | ||
} | ||
}); | ||
} // static methods can't access `this` so they go last | ||
createClass(Domodule, [{ | ||
key: 'preInit', | ||
value: function preInit() {} | ||
}, { | ||
key: 'postInit', | ||
value: function postInit() {} | ||
}, { | ||
key: 'verifyRequired', | ||
value: function verifyRequired() { | ||
var _this = this; | ||
if (this.required === {}) { | ||
return this; | ||
}, { | ||
key: "log", | ||
//used inside instance | ||
value: function log(msg) { | ||
Domodule.log("".concat(this.constructor.name, ": ").concat(msg)); | ||
} | ||
}, { | ||
key: "required", | ||
get: function get() { | ||
return {}; | ||
} | ||
}, { | ||
key: "defaults", | ||
get: function get() { | ||
return {}; | ||
} | ||
}, { | ||
key: "uuid", | ||
get: function get() { | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { | ||
var r = Math.random() * 16 | 0; | ||
var v = c === 'x' ? r : r & 0x3 | 0x8; | ||
return v.toString(16); | ||
}); | ||
} | ||
}], [{ | ||
key: "parseAction", | ||
value: function parseAction(el) { | ||
var _el$dataset = el.dataset, | ||
name = _el$dataset.action, | ||
_el$dataset$actionTyp = _el$dataset.actionType, | ||
type = _el$dataset$actionTyp === void 0 ? 'click' : _el$dataset$actionTyp; | ||
return { | ||
name: name, | ||
type: type | ||
}; | ||
} | ||
}, { | ||
key: "getInstance", | ||
value: function getInstance(element) { | ||
if (element instanceof Node) { | ||
return window.domorefs[element.dataset.moduleUid]; | ||
} | ||
if (typeof this.required.options !== 'undefined') { | ||
this.setUps.options = Object.keys(this.options); | ||
throw new Error('getInstance expects a dom node'); | ||
} | ||
}, { | ||
key: "register", | ||
value: function register(name, cls) { | ||
if (typeof name === 'function') { | ||
cls = name; | ||
name = cls.prototype.constructor.name; | ||
} | ||
Object.keys(this.required).forEach(function (required) { | ||
_this.required[required].forEach(function (value) { | ||
if (_this.setUps[required].indexOf(value) < 0) { | ||
throw new Error(value + ' is required as ' + required + ' for ' + _this.moduleName + ', but is missing!'); | ||
} | ||
}); | ||
}); | ||
if (!window.domodules) { | ||
window.domodules = {}; | ||
} | ||
return this; | ||
} | ||
}, { | ||
key: 'setupActions', | ||
value: function setupActions() { | ||
var _this2 = this; | ||
Domodule.log("Registering ".concat(name)); | ||
window.domodules[name] = cls; | ||
} | ||
}, { | ||
key: "discover", | ||
value: function discover() { | ||
var el = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'body'; | ||
Domodule.log('Discovering modules...'); | ||
this.setupAction(this.el); | ||
if (!window.domodules) { | ||
Domodule.log('No modules found'); | ||
return; | ||
} | ||
this.find(ACTION_SELECTOR).forEach(function (action) { | ||
var parent = findParent$1(action); | ||
var els; | ||
if (parent === _this2.el) { | ||
_this2.setupAction(action); | ||
if (el instanceof Node) { | ||
els = [el]; | ||
} else if (Array.isArray(el)) { | ||
els = el; | ||
} else { | ||
els = DOMAssist.find(el); | ||
} | ||
}); | ||
} | ||
}, { | ||
key: 'setupAction', | ||
value: function setupAction(actionEl) { | ||
if (actionEl.dataset.domoduleActionProcessed === 'true') { | ||
return; | ||
} | ||
var _Domodule$parseAction = Domodule.parseAction(actionEl), | ||
actionName = _Domodule$parseAction.name, | ||
actionType = _Domodule$parseAction.type; | ||
var instances = []; | ||
els.forEach(function (matched) { | ||
var foundModules = DOMAssist.find('[data-module]', matched); | ||
foundModules.forEach(function (moduleEl) { | ||
var moduleName = moduleEl.dataset.module; | ||
if (!actionName) { | ||
return; | ||
} else if (typeof this[actionName] !== 'function') { | ||
this.log(actionName + ' was registered, but there is no function set up'); | ||
return; | ||
if (moduleName && typeof window.domodules[moduleName] === 'function') { | ||
if (_typeof(window.domorefs) === 'object' && typeof window.domorefs[moduleEl.dataset.moduleUid] !== 'undefined') { | ||
return; | ||
} | ||
Domodule.log("".concat(moduleName, " found")); | ||
instances.push(new window.domodules[moduleName](moduleEl)); | ||
} | ||
}); | ||
}); | ||
return instances; | ||
} | ||
}, { | ||
key: "log", | ||
value: function log(msg) { | ||
if (Domodule.debug) { | ||
console.log("[DOMODULE] ".concat(msg)); //eslint-disable-line no-console | ||
} | ||
} | ||
}]); | ||
this.log(actionName + ' bound'); | ||
this.storeSetUp(actionName, 'actions'); | ||
return Domodule; | ||
}(); | ||
DOMAssist.on(actionEl, actionType, this.boundActionRouter); | ||
Domodule.debug = _typeof(window.localStorage) === 'object' && window.localStorage.getItem('DomoduleDebug'); | ||
Domodule.autoDiscover = true; | ||
window.addEventListener('DOMContentLoaded', function () { | ||
if (Domodule.autoDiscover) { | ||
Domodule.discover(); | ||
} | ||
}); | ||
actionEl.dataset.domoduleActionProcessed = 'true'; | ||
/* eslint-env browser */ | ||
var Ajax = | ||
/*#__PURE__*/ | ||
function () { | ||
function Ajax() { | ||
_classCallCheck(this, Ajax); | ||
} | ||
}, { | ||
key: 'actionRouter', | ||
value: function actionRouter(event) { | ||
var actionEl = event.currentTarget; | ||
var _Domodule$parseAction2 = Domodule.parseAction(actionEl), | ||
actionName = _Domodule$parseAction2.name; | ||
_createClass(Ajax, null, [{ | ||
key: "serialize", | ||
value: function serialize(data) { | ||
var queryString = ''; | ||
var actionData = attrObj('action', actionEl); | ||
var formatValue = function formatValue(k, v) { | ||
return "&".concat(encodeURIComponent(k), "=").concat(encodeURIComponent(v)); | ||
}; | ||
this[actionName].call(this, actionEl, event, actionData); | ||
} | ||
}, { | ||
key: 'setupNamed', | ||
value: function setupNamed() { | ||
var _this3 = this; | ||
Object.keys(data).forEach(function (key) { | ||
var value = data[key]; | ||
this.find('[data-name]').forEach(function (named) { | ||
var parent = findParent$1(named); | ||
if (Array.isArray(value)) { | ||
value.forEach(function (k) { | ||
queryString += formatValue(key, k); | ||
}); | ||
} else { | ||
queryString += formatValue(key, value); | ||
} | ||
}); | ||
return queryString.substring(1); | ||
} | ||
}, { | ||
key: "get", | ||
value: function get(url) { | ||
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
if (parent !== _this3.el) { | ||
return; | ||
return Ajax.request.apply(Ajax, [url, 'GET'].concat(args)); | ||
} | ||
}, { | ||
key: "post", | ||
value: function post(url) { | ||
for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
args[_key2 - 1] = arguments[_key2]; | ||
} | ||
if (!named.dataset.domoduleNameProcessed) { | ||
_this3.els[named.dataset.name] = named; | ||
return Ajax.request.apply(Ajax, [url, 'POST'].concat(args)); | ||
} | ||
}, { | ||
key: "patch", | ||
value: function patch(url) { | ||
for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { | ||
args[_key3 - 1] = arguments[_key3]; | ||
} | ||
_this3.storeSetUp(named.dataset.name, 'named'); | ||
named.dataset.domoduleNameProcessed = 'true'; | ||
named.dataset.domoduleOwner = _this3.id; | ||
return Ajax.request.apply(Ajax, [url, 'PATCH'].concat(args)); | ||
} | ||
}, { | ||
key: "put", | ||
value: function put(url) { | ||
for (var _len4 = arguments.length, args = new Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) { | ||
args[_key4 - 1] = arguments[_key4]; | ||
} | ||
}); | ||
} | ||
}, { | ||
key: 'storeRef', | ||
value: function storeRef() { | ||
if (typeof window.domorefs === 'undefined') { | ||
window.domorefs = {}; | ||
return Ajax.request.apply(Ajax, [url, 'PUT'].concat(args)); | ||
} | ||
}, { | ||
key: "head", | ||
value: function head(url) { | ||
for (var _len5 = arguments.length, args = new Array(_len5 > 1 ? _len5 - 1 : 0), _key5 = 1; _key5 < _len5; _key5++) { | ||
args[_key5 - 1] = arguments[_key5]; | ||
} | ||
if (typeof window.domorefs[this.el.dataset.moduleUid] !== 'undefined') { | ||
return false; | ||
return Ajax.request.apply(Ajax, [url, 'HEAD'].concat(args)); | ||
} | ||
}, { | ||
key: "del", | ||
value: function del(url) { | ||
for (var _len6 = arguments.length, args = new Array(_len6 > 1 ? _len6 - 1 : 0), _key6 = 1; _key6 < _len6; _key6++) { | ||
args[_key6 - 1] = arguments[_key6]; | ||
} | ||
this.id = this.uuid; | ||
this.el.dataset.moduleUid = this.id; | ||
window.domorefs[this.el.dataset.moduleUid] = this; | ||
} | ||
}, { | ||
key: 'find', | ||
value: function find(selector) { | ||
return DOMAssist.find(selector, this.el); | ||
} | ||
}, { | ||
key: 'findOne', | ||
value: function findOne(selector) { | ||
return DOMAssist.findOne(selector, this.el); | ||
} | ||
}, { | ||
key: 'findByName', | ||
value: function findByName(name) { | ||
return this.els[name]; | ||
} | ||
}, { | ||
key: 'getOption', | ||
value: function getOption(option) { | ||
return this.options[option]; | ||
} | ||
}, { | ||
key: 'storeSetUp', | ||
value: function storeSetUp(name, dict) { | ||
if (this.setUps[dict].indexOf(name) < 0) { | ||
this.setUps[dict].push(name); | ||
return Ajax.request.apply(Ajax, [url, 'DELETE'].concat(args)); | ||
} | ||
} | ||
}, { | ||
key: 'destroy', | ||
value: function destroy() { | ||
var _this4 = this; | ||
}, { | ||
key: "options", | ||
value: function options(url) { | ||
for (var _len7 = arguments.length, args = new Array(_len7 > 1 ? _len7 - 1 : 0), _key7 = 1; _key7 < _len7; _key7++) { | ||
args[_key7 - 1] = arguments[_key7]; | ||
} | ||
DOMAssist.find(ACTION_SELECTOR, this.el.parentNode).forEach(function (el) { | ||
if (el.dataset.domoduleActionProcessed === 'true') { | ||
var _Domodule$parseAction3 = Domodule.parseAction(el), | ||
actionType = _Domodule$parseAction3.type; | ||
return Ajax.request.apply(Ajax, [url, 'OPTIONS'].concat(args)); | ||
} | ||
/** | ||
* request - Makes a request to a remote server | ||
* | ||
* @param {String} url Url for the request | ||
* @param {String} method Request method: GET, POST, etc (default: GET) | ||
* @param {Object} data Data payload. Set to null to not send | ||
* anything. Data is sent as raw json (default: null) | ||
* @param {Function} callback Called con completion with an object containing | ||
* the status code and the data. If the response | ||
* has a content-type containing json data will | ||
* be parsed, otherwise it will be the raw data. | ||
* @return XMLHttpRequest Original XMLHttpRequest object to allow further | ||
* event binding. | ||
*/ | ||
el.removeEventListener(actionType, _this4.boundActionRouter); | ||
el.dataset.domoduleActionProcessed = 'false'; | ||
}, { | ||
key: "request", | ||
value: function request(url) { | ||
for (var _len8 = arguments.length, args = new Array(_len8 > 1 ? _len8 - 1 : 0), _key8 = 1; _key8 < _len8; _key8++) { | ||
args[_key8 - 1] = arguments[_key8]; | ||
} | ||
}); | ||
} | ||
// static methods can't access `this` so they go last | ||
var method = args[0], | ||
data = args[1], | ||
headers = args[2], | ||
callback = args[3]; | ||
}, { | ||
key: 'log', | ||
if (typeof method === 'function') { | ||
method = 'GET'; | ||
callback = method; | ||
data = null; | ||
headers = {}; | ||
} | ||
if (typeof data === 'function') { | ||
callback = data; | ||
data = null; | ||
headers = {}; | ||
} | ||
//used inside instance | ||
value: function log(msg) { | ||
Domodule.log(this.constructor.name + ': ' + msg); | ||
} | ||
}, { | ||
key: 'required', | ||
get: function get$$1() { | ||
return {}; | ||
} | ||
}, { | ||
key: 'defaults', | ||
get: function get$$1() { | ||
return {}; | ||
} | ||
}, { | ||
key: 'uuid', | ||
get: function get$$1() { | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { | ||
var r = Math.random() * 16 | 0; | ||
var v = c === 'x' ? r : r & 0x3 | 0x8; | ||
return v.toString(16); | ||
}); | ||
} | ||
}], [{ | ||
key: 'parseAction', | ||
value: function parseAction(el) { | ||
var _el$dataset = el.dataset, | ||
name = _el$dataset.action, | ||
_el$dataset$actionTyp = _el$dataset.actionType, | ||
type = _el$dataset$actionTyp === undefined ? 'click' : _el$dataset$actionTyp; | ||
if (typeof headers === 'function') { | ||
callback = headers; | ||
headers = {}; | ||
} | ||
return { name: name, type: type }; | ||
} | ||
}, { | ||
key: 'getInstance', | ||
value: function getInstance(element) { | ||
if (element instanceof Node) { | ||
return window.domorefs[element.dataset.moduleUid]; | ||
} | ||
if (headers === null || typeof headers === 'undefined') { | ||
headers = {}; | ||
} | ||
throw new Error('getInstance expects a dom node'); | ||
} | ||
}, { | ||
key: 'register', | ||
value: function register(name, cls) { | ||
if (typeof name === 'function') { | ||
cls = name; | ||
name = cls.prototype.constructor.name; | ||
} | ||
var useMethod = method.toUpperCase(); | ||
var validMethods = ['GET', 'POST', 'PUT', 'PATCH', 'HEAD', 'DELETE', 'OPTIONS']; | ||
if (!window.domodules) { | ||
window.domodules = {}; | ||
} | ||
if (validMethods.indexOf(useMethod) === -1) { | ||
var err = new TypeError("Method must be one of the following: ".concat(validMethods.join(', '))); | ||
return callback(err); | ||
} | ||
Domodule.log('Registering ' + name); | ||
window.domodules[name] = cls; | ||
} | ||
}, { | ||
key: 'discover', | ||
value: function discover() { | ||
var el = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'body'; | ||
if (_typeof(data) === 'object' && data !== null && method === 'GET') { | ||
var serializedData = data; | ||
Domodule.log('Discovering modules...'); | ||
if (_typeof(data) === 'object' && data !== null) { | ||
serializedData = Ajax.serialize(data); | ||
var join = url.indexOf('?') > -1 ? '&' : '?'; | ||
url = url + join + serializedData; | ||
data = null; | ||
} | ||
} | ||
if (!window.domodules) { | ||
Domodule.log('No modules found'); | ||
return; | ||
} | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open(useMethod, url); | ||
var els = void 0; | ||
xhr.onreadystatechange = function () { | ||
if (xhr.readyState > 3 && xhr.status > 0) { | ||
var contentType = xhr.getResponseHeader('content-type'); | ||
var parsedResponse = xhr.responseText; | ||
if (el instanceof Node) { | ||
els = [el]; | ||
} else if (Array.isArray(el)) { | ||
els = el; | ||
} else { | ||
els = DOMAssist.find(el); | ||
} | ||
if (contentType && contentType.toLowerCase().indexOf('json') > -1) { | ||
parsedResponse = JSON.parse(parsedResponse); | ||
} | ||
var instances = []; | ||
return callback(null, { | ||
headers: xhr.getAllResponseHeaders(), | ||
statusCode: xhr.status, | ||
data: parsedResponse | ||
}); | ||
} | ||
}; | ||
els.forEach(function (matched) { | ||
var foundModules = DOMAssist.find('[data-module]', matched); | ||
xhr.onerror = function (e) { | ||
return callback(e, null); | ||
}; | ||
foundModules.forEach(function (moduleEl) { | ||
var moduleName = moduleEl.dataset.module; | ||
Object.keys(headers).forEach(function (header) { | ||
xhr.setRequestHeader(header, headers[header]); | ||
}); | ||
if (moduleName && typeof window.domodules[moduleName] === 'function') { | ||
if (_typeof(window.domorefs) === 'object' && typeof window.domorefs[moduleEl.dataset.moduleUid] !== 'undefined') { | ||
return; | ||
} | ||
if (data !== null) { | ||
var dataString = data; | ||
var contentHeader = 'application/x-www-form-urlencoded'; | ||
Domodule.log(moduleName + ' found'); | ||
instances.push(new window.domodules[moduleName](moduleEl)); | ||
if (_typeof(data) === 'object') { | ||
dataString = JSON.stringify(data); | ||
contentHeader = 'application/json'; | ||
} | ||
}); | ||
}); | ||
return instances; | ||
} | ||
}, { | ||
key: 'log', | ||
value: function log(msg) { | ||
if (Domodule.debug) { | ||
console.log('[DOMODULE] ' + msg); //eslint-disable-line no-console | ||
xhr.setRequestHeader('Content-Type', contentHeader); | ||
xhr.send(dataString); | ||
} else { | ||
xhr.send(); | ||
} | ||
return xhr; | ||
} | ||
} | ||
}]); | ||
return Domodule; | ||
}(); | ||
}]); | ||
Domodule.debug = _typeof(window.localStorage) === 'object' && window.localStorage.getItem('DomoduleDebug'); | ||
return Ajax; | ||
}(); | ||
Domodule.autoDiscover = true; | ||
window.addEventListener('DOMContentLoaded', function () { | ||
if (Domodule.autoDiscover) { | ||
Domodule.discover(); | ||
function isWindow$1(obj) { | ||
return obj != null && obj === obj.window; | ||
} | ||
}); | ||
/* eslint-env browser */ | ||
var Ajax = function () { | ||
function Ajax() { | ||
classCallCheck(this, Ajax); | ||
} | ||
function find$1(selector) { | ||
var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
createClass(Ajax, null, [{ | ||
key: 'serialize', | ||
value: function serialize(data) { | ||
var queryString = ''; | ||
if (selector instanceof HTMLElement || selector instanceof Node || isWindow$1(selector)) { | ||
return [selector]; | ||
} else if (selector instanceof NodeList) { | ||
return [].slice.call(selector); | ||
} else if (typeof selector === 'string') { | ||
var startElement = context ? find$1(context)[0] : document; | ||
return [].slice.call(startElement.querySelectorAll(selector)); | ||
} | ||
var formatValue = function formatValue(k, v) { | ||
return '&' + encodeURIComponent(k) + '=' + encodeURIComponent(v); | ||
}; | ||
return []; | ||
} | ||
Object.keys(data).forEach(function (key) { | ||
var value = data[key]; | ||
var NativeCustomEvent$1 = window.CustomEvent; // | ||
// Check for the usage of native support for CustomEvents which is lacking | ||
// completely on IE. | ||
// | ||
if (Array.isArray(value)) { | ||
value.forEach(function (k) { | ||
queryString += formatValue(key, k); | ||
}); | ||
} else { | ||
queryString += formatValue(key, value); | ||
function canIuseNativeCustom$1() { | ||
try { | ||
var p = new NativeCustomEvent$1('t', { | ||
detail: { | ||
a: 'b' | ||
} | ||
}); | ||
return queryString.substring(1); | ||
return p.type === 't' && p.detail.a === 'b'; | ||
} catch (e) { | ||
return false; | ||
} | ||
}, { | ||
key: 'get', | ||
value: function get$$1(url) { | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
} // Lousy polyfill for the Custom Event constructor for IE. | ||
return Ajax.request.apply(Ajax, [url, 'GET'].concat(args)); | ||
} | ||
}, { | ||
key: 'post', | ||
value: function post(url) { | ||
for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
args[_key2 - 1] = arguments[_key2]; | ||
} | ||
return Ajax.request.apply(Ajax, [url, 'POST'].concat(args)); | ||
var IECustomEvent$1 = function CustomEvent(type, params) { | ||
var e = document.createEvent('CustomEvent'); | ||
if (params) { | ||
e.initCustomEvent(type, params.bubbles, params.cancelable, params.detail); | ||
} else { | ||
e.initCustomEvent(type, false, false, undefined); | ||
} | ||
}, { | ||
key: 'patch', | ||
value: function patch(url) { | ||
for (var _len3 = arguments.length, args = Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { | ||
args[_key3 - 1] = arguments[_key3]; | ||
} | ||
return Ajax.request.apply(Ajax, [url, 'PATCH'].concat(args)); | ||
return e; | ||
}; | ||
var DomassistCustomEvent$1 = canIuseNativeCustom$1() ? NativeCustomEvent$1 : IECustomEvent$1; | ||
var SCROLLABLE_CONTAINER$1; | ||
function getScrollableContainer$1() { | ||
if (SCROLLABLE_CONTAINER$1) { | ||
return SCROLLABLE_CONTAINER$1; | ||
} | ||
}, { | ||
key: 'put', | ||
value: function put(url) { | ||
for (var _len4 = arguments.length, args = Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) { | ||
args[_key4 - 1] = arguments[_key4]; | ||
} | ||
return Ajax.request.apply(Ajax, [url, 'PUT'].concat(args)); | ||
var documentElement = window.document.documentElement; | ||
var scrollableContainer; | ||
documentElement.scrollTop = 1; | ||
if (documentElement.scrollTop === 1) { | ||
documentElement.scrollTop = 0; | ||
scrollableContainer = documentElement; | ||
} else { | ||
scrollableContainer = document.body; | ||
} | ||
}, { | ||
key: 'head', | ||
value: function head(url) { | ||
for (var _len5 = arguments.length, args = Array(_len5 > 1 ? _len5 - 1 : 0), _key5 = 1; _key5 < _len5; _key5++) { | ||
args[_key5 - 1] = arguments[_key5]; | ||
} | ||
return Ajax.request.apply(Ajax, [url, 'HEAD'].concat(args)); | ||
SCROLLABLE_CONTAINER$1 = scrollableContainer; | ||
return scrollableContainer; | ||
} | ||
SCROLLABLE_CONTAINER$1 = getScrollableContainer$1(); | ||
/* global DocumentTouch */ | ||
function toArray(value) { | ||
if (!value) { | ||
return []; | ||
} | ||
}, { | ||
key: 'del', | ||
value: function del(url) { | ||
for (var _len6 = arguments.length, args = Array(_len6 > 1 ? _len6 - 1 : 0), _key6 = 1; _key6 < _len6; _key6++) { | ||
args[_key6 - 1] = arguments[_key6]; | ||
} | ||
return Ajax.request.apply(Ajax, [url, 'DELETE'].concat(args)); | ||
if (Array.isArray(value)) { | ||
return value; | ||
} | ||
}, { | ||
key: 'options', | ||
value: function options(url) { | ||
for (var _len7 = arguments.length, args = Array(_len7 > 1 ? _len7 - 1 : 0), _key7 = 1; _key7 < _len7; _key7++) { | ||
args[_key7 - 1] = arguments[_key7]; | ||
} | ||
return Ajax.request.apply(Ajax, [url, 'OPTIONS'].concat(args)); | ||
if (value instanceof Node) { | ||
return [value]; | ||
} | ||
/** | ||
* request - Makes a request to a remote server | ||
* | ||
* @param {String} url Url for the request | ||
* @param {String} method Request method: GET, POST, etc (default: GET) | ||
* @param {Object} data Data payload. Set to null to not send | ||
* anything. Data is sent as raw json (default: null) | ||
* @param {Function} callback Called con completion with an object containing | ||
* the status code and the data. If the response | ||
* has a content-type containing json data will | ||
* be parsed, otherwise it will be the raw data. | ||
* @return XMLHttpRequest Original XMLHttpRequest object to allow further | ||
* event binding. | ||
*/ | ||
return [].slice.call(value); | ||
} | ||
}, { | ||
key: 'request', | ||
value: function request(url) { | ||
for (var _len8 = arguments.length, args = Array(_len8 > 1 ? _len8 - 1 : 0), _key8 = 1; _key8 < _len8; _key8++) { | ||
args[_key8 - 1] = arguments[_key8]; | ||
} | ||
function deserialize(data, inputs) { | ||
var index = {}; | ||
var method = args[0], | ||
data = args[1], | ||
headers = args[2], | ||
callback = args[3]; | ||
if (!Array.isArray(inputs)) { | ||
inputs = toArray(inputs); | ||
} | ||
inputs.forEach(function (input) { | ||
var name = input.getAttribute('name'); | ||
var val = data[name]; | ||
if (typeof method === 'function') { | ||
method = 'GET'; | ||
callback = method; | ||
data = null; | ||
headers = {}; | ||
if (typeof index[name] === 'undefined') { | ||
index[name] = 0; | ||
} else { | ||
index[name] = index[name] + 1; | ||
} | ||
if (typeof data === 'function') { | ||
callback = data; | ||
data = null; | ||
headers = {}; | ||
if (Array.isArray(val) && input.tagName !== 'SELECT' && !input.multiple) { | ||
val = val[index[name]]; | ||
} | ||
if (typeof headers === 'function') { | ||
callback = headers; | ||
headers = {}; | ||
if (typeof val === 'undefined') { | ||
return; | ||
} | ||
if (headers === null || typeof headers === 'undefined') { | ||
headers = {}; | ||
} | ||
if (input.type === 'checkbox') { | ||
if (input.getAttribute('value')) { | ||
input.checked = val === input.value; | ||
} else { | ||
input.checked = val === true; | ||
} | ||
} else if (input.type === 'radio') { | ||
input.checked = input.value === val; | ||
} else if (input.tagName === 'SELECT') { | ||
var v = val; | ||
var useMethod = method.toUpperCase(); | ||
var validMethods = ['GET', 'POST', 'PUT', 'PATCH', 'HEAD', 'DELETE', 'OPTIONS']; | ||
if (!Array.isArray(val)) { | ||
v = [val]; | ||
} | ||
if (validMethods.indexOf(useMethod) === -1) { | ||
var err = new TypeError('Method must be one of the following: ' + validMethods.join(', ')); | ||
return callback(err); | ||
toArray(input.options).filter(function (option) { | ||
return v.indexOf(option.value) > -1; | ||
}).forEach(function (option) { | ||
option.selected = true; | ||
}); | ||
} else { | ||
input.value = val; | ||
} | ||
}); | ||
} | ||
if ((typeof data === 'undefined' ? 'undefined' : _typeof(data)) === 'object' && data !== null && method === 'GET') { | ||
var serializedData = data; | ||
function isForm(el) { | ||
if (!el || !el.tagName || el.tagName !== 'FORM') { | ||
throw new Error('Must pass in a form element'); | ||
} | ||
} | ||
if ((typeof data === 'undefined' ? 'undefined' : _typeof(data)) === 'object' && data !== null) { | ||
serializedData = Ajax.serialize(data); | ||
var join = url.indexOf('?') > -1 ? '&' : '?'; | ||
function getInputs(form) { | ||
var selector = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '[name]'; | ||
isForm(form); | ||
url = url + join + serializedData; | ||
data = null; | ||
} | ||
} | ||
if (typeof selector !== 'string') { | ||
throw new Error('Invalid selector'); | ||
} | ||
var xhr = new XMLHttpRequest(); | ||
return toArray(find$1(selector, form)); | ||
} | ||
xhr.open(useMethod, url); | ||
function getJSON(form, selector) { | ||
var inputs = getInputs(form, selector); | ||
var output = {}; | ||
inputs.forEach(function (input) { | ||
var name = input.getAttribute('name'); | ||
var value; | ||
xhr.onreadystatechange = function () { | ||
if (xhr.readyState > 3 && xhr.status > 0) { | ||
var contentType = xhr.getResponseHeader('content-type'); | ||
var parsedResponse = xhr.responseText; | ||
if (contentType && contentType.toLowerCase().indexOf('json') > -1) { | ||
parsedResponse = JSON.parse(parsedResponse); | ||
if (input.type === 'checkbox') { | ||
if (input.getAttribute('value')) { | ||
if (input.checked) { | ||
value = input.value; | ||
} else { | ||
return; | ||
} | ||
} else { | ||
value = input.checked; | ||
} | ||
} else if (input.type === 'radio') { | ||
if (input.checked) { | ||
value = input.value; | ||
} else { | ||
return; | ||
} | ||
} else if (input.tagName === 'SELECT' && input.multiple) { | ||
value = toArray(input.options).filter(function (option) { | ||
return option.selected; | ||
}).map(function (option) { | ||
return option.value; | ||
}); | ||
} else { | ||
value = input.value; | ||
} // Radio will have multiple matching `name` attributes and we don't want them all. | ||
return callback(null, { | ||
headers: xhr.getAllResponseHeaders(), | ||
statusCode: xhr.status, | ||
data: parsedResponse | ||
}); | ||
if (typeof output[name] !== 'undefined' && input.type !== 'radio') { | ||
if (Array.isArray(output[name])) { | ||
output[name].push(value); | ||
} else { | ||
output[name] = [output[name], value]; | ||
} | ||
}; | ||
} else { | ||
output[name] = value; | ||
} | ||
}); | ||
return output; | ||
} | ||
xhr.onerror = function (e) { | ||
return callback(e, null); | ||
}; | ||
function getQueryString(form) { | ||
var data; | ||
var queryString = ''; | ||
Object.keys(headers).forEach(function (header) { | ||
xhr.setRequestHeader(header, headers[header]); | ||
}); | ||
if (form instanceof HTMLElement) { | ||
data = getJSON(form); | ||
} else { | ||
data = form; | ||
} | ||
if (data !== null) { | ||
var dataString = data; | ||
var contentHeader = 'application/x-www-form-urlencoded'; | ||
if ((typeof data === 'undefined' ? 'undefined' : _typeof(data)) === 'object') { | ||
dataString = JSON.stringify(data); | ||
contentHeader = 'application/json'; | ||
} | ||
var formatValue = function formatValue(k, v) { | ||
return "&".concat(encodeURIComponent(k), "=").concat(encodeURIComponent(v)); | ||
}; | ||
xhr.setRequestHeader('Content-Type', contentHeader); | ||
xhr.send(dataString); | ||
Object.keys(data).forEach(function (key) { | ||
var value = data[key]; | ||
if (Array.isArray(value)) { | ||
value.forEach(function (k) { | ||
queryString += formatValue(key, k); | ||
}); | ||
} else { | ||
xhr.send(); | ||
queryString += formatValue(key, value); | ||
} | ||
}); | ||
return queryString.substring(1); | ||
} | ||
return xhr; | ||
} | ||
}]); | ||
return Ajax; | ||
}(); | ||
function formobj(form, selector) { | ||
isForm(form); | ||
var api = { | ||
getInputs: function getInputs$1() { | ||
return getInputs(form, selector); | ||
}, | ||
getJSON: function getJSON$1() { | ||
return getJSON(form, selector); | ||
}, | ||
deserialize: function deserialize$1(data) { | ||
return deserialize(data, getInputs(form, selector)); | ||
}, | ||
getQueryString: function getQueryString$1() { | ||
return getQueryString(api.getJSON(form)); | ||
} | ||
}; | ||
return api; | ||
} | ||
function deserialize(data, inputs) { | ||
var index = {}; | ||
/* eslint-env browser, node */ | ||
var tinytemplate = function tinyTemplate(template, data, fallback) { | ||
return template.replace(/\$\{[^}]+\}/g, function (match) { | ||
return match.slice(2, -1).trim().split('.').reduce(function (searchObject, key) { | ||
return searchObject[key] || fallback || match; | ||
}, data); | ||
}); | ||
}; | ||
if (!Array.isArray(inputs)) { | ||
inputs = toArray$1(inputs); | ||
function isWindow$2(obj) { | ||
return obj != null && obj === obj.window; | ||
} | ||
inputs.forEach(function (input) { | ||
var name = input.getAttribute('name'); | ||
var val = data[name]; | ||
function find$2(selector) { | ||
var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
if (typeof index[name] === 'undefined') { | ||
index[name] = 0; | ||
} else { | ||
index[name] = index[name] + 1; | ||
if (selector instanceof HTMLElement || selector instanceof Node || isWindow$2(selector)) { | ||
return [selector]; | ||
} else if (selector instanceof NodeList) { | ||
return [].slice.call(selector); | ||
} else if (typeof selector === 'string') { | ||
var startElement = context ? find$2(context)[0] : document; | ||
return [].slice.call(startElement.querySelectorAll(selector)); | ||
} | ||
if (Array.isArray(val) && input.tagName !== 'SELECT' && !input.multiple) { | ||
val = val[index[name]]; | ||
return []; | ||
} | ||
function addClass(selector, cls) { | ||
if (Array.isArray(selector)) { | ||
return selector.forEach(function (item) { | ||
return addClass(item, cls); | ||
}); | ||
} | ||
if (typeof val === 'undefined') { | ||
return; | ||
var els = find$2(selector); | ||
if (els.length) { | ||
var clsArray = [].concat(cls); | ||
els.forEach(function (el) { | ||
clsArray.forEach(function (item) { | ||
el.classList.add(item); | ||
}); | ||
}); | ||
return els; | ||
} | ||
} | ||
if (input.type === 'checkbox') { | ||
if (input.getAttribute('value')) { | ||
input.checked = val === input.value; | ||
} else { | ||
input.checked = val === true; | ||
} | ||
} else if (input.type === 'radio') { | ||
input.checked = input.value === val; | ||
} else if (input.tagName === 'SELECT') { | ||
var v = val; | ||
function on$1(selector, event, cb) { | ||
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false; | ||
if (!Array.isArray(val)) { | ||
v = [val]; | ||
} | ||
toArray$1(input.options).filter(function (option) { | ||
return v.indexOf(option.value) > -1; | ||
}).forEach(function (option) { | ||
option.selected = true; | ||
if (Array.isArray(selector)) { | ||
selector.forEach(function (item) { | ||
return on$1(item, event, cb, capture); | ||
}); | ||
} else { | ||
input.value = val; | ||
return; | ||
} | ||
}); | ||
} | ||
function isForm(el) { | ||
if (!el || !el.tagName || el.tagName !== 'FORM') { | ||
throw new Error('Must pass in a form element'); | ||
} | ||
} | ||
var data = { | ||
cb: cb, | ||
capture: capture | ||
}; | ||
function getInputs(form) { | ||
var selector = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '[name]'; | ||
if (!window._domassistevents) { | ||
window._domassistevents = {}; | ||
} | ||
isForm(form); | ||
window._domassistevents["_".concat(event)] = data; | ||
var el = find$2(selector); | ||
if (typeof selector !== 'string') { | ||
throw new Error('Invalid selector'); | ||
if (el.length) { | ||
el.forEach(function (item) { | ||
item.addEventListener(event, cb, capture); | ||
}); | ||
} | ||
} | ||
return toArray$1(find(selector, form)); | ||
} | ||
var NativeCustomEvent$2 = window.CustomEvent; // | ||
// Check for the usage of native support for CustomEvents which is lacking | ||
// completely on IE. | ||
// | ||
function getJSON(form, selector) { | ||
var inputs = getInputs(form, selector); | ||
var output = {}; | ||
inputs.forEach(function (input) { | ||
var name = input.getAttribute('name'); | ||
var value = void 0; | ||
if (input.type === 'checkbox') { | ||
if (input.getAttribute('value')) { | ||
if (input.checked) { | ||
value = input.value; | ||
} else { | ||
return; | ||
function canIuseNativeCustom$2() { | ||
try { | ||
var p = new NativeCustomEvent$2('t', { | ||
detail: { | ||
a: 'b' | ||
} | ||
} else { | ||
value = input.checked; | ||
} | ||
} else if (input.type === 'radio') { | ||
if (input.checked) { | ||
value = input.value; | ||
} else { | ||
return; | ||
} | ||
} else if (input.tagName === 'SELECT' && input.multiple) { | ||
value = toArray$1(input.options).filter(function (option) { | ||
return option.selected; | ||
}).map(function (option) { | ||
return option.value; | ||
}); | ||
} else { | ||
value = input.value; | ||
return p.type === 't' && p.detail.a === 'b'; | ||
} catch (e) { | ||
return false; | ||
} | ||
} // Lousy polyfill for the Custom Event constructor for IE. | ||
// Radio will have multiple matching `name` attributes and we don't want them all. | ||
if (typeof output[name] !== 'undefined' && input.type !== 'radio') { | ||
if (Array.isArray(output[name])) { | ||
output[name].push(value); | ||
} else { | ||
output[name] = [output[name], value]; | ||
} | ||
var IECustomEvent$2 = function CustomEvent(type, params) { | ||
var e = document.createEvent('CustomEvent'); | ||
if (params) { | ||
e.initCustomEvent(type, params.bubbles, params.cancelable, params.detail); | ||
} else { | ||
output[name] = value; | ||
e.initCustomEvent(type, false, false, undefined); | ||
} | ||
}); | ||
return output; | ||
} | ||
return e; | ||
}; | ||
function getQueryString(form) { | ||
var data = void 0; | ||
var queryString = ''; | ||
var DomassistCustomEvent$2 = canIuseNativeCustom$2() ? NativeCustomEvent$2 : IECustomEvent$2; | ||
if (form instanceof HTMLElement) { | ||
data = getJSON(form); | ||
} else { | ||
data = form; | ||
} | ||
function fire(selector, type) { | ||
var params = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var formatValue = function formatValue(k, v) { | ||
return '&' + encodeURIComponent(k) + '=' + encodeURIComponent(v); | ||
}; | ||
if (Array.isArray(selector)) { | ||
return selector.forEach(function (item) { | ||
return fire(item, type, params); | ||
}); | ||
} | ||
Object.keys(data).forEach(function (key) { | ||
var value = data[key]; | ||
var els = find$2(selector); | ||
if (Array.isArray(value)) { | ||
value.forEach(function (k) { | ||
queryString += formatValue(key, k); | ||
if (els.length) { | ||
if (params.bubbles !== false) { | ||
params.bubbles = true; | ||
} | ||
els.forEach(function (el) { | ||
var event = new DomassistCustomEvent$2(type, params); | ||
el.dispatchEvent(event); | ||
}); | ||
} else { | ||
queryString += formatValue(key, value); | ||
return els; | ||
} | ||
}); | ||
} | ||
return queryString.substring(1); | ||
} | ||
function removeClass(selector, cls) { | ||
if (Array.isArray(selector)) { | ||
return selector.forEach(function (item) { | ||
return removeClass(item, cls); | ||
}); | ||
} | ||
function formobj(form, selector) { | ||
isForm(form); | ||
var els = find$2(selector); | ||
var api = { | ||
getInputs: function getInputs$$1() { | ||
return getInputs(form, selector); | ||
}, | ||
getJSON: function getJSON$$1() { | ||
return getJSON(form, selector); | ||
}, | ||
deserialize: function deserialize$$1(data) { | ||
return deserialize(data, getInputs(form, selector)); | ||
}, | ||
getQueryString: function getQueryString$$1() { | ||
return getQueryString(api.getJSON(form)); | ||
if (els.length) { | ||
var clsArray = [].concat(cls); | ||
els.forEach(function (el) { | ||
clsArray.forEach(function (item) { | ||
el.classList.remove(item); | ||
}); | ||
}); | ||
return els; | ||
} | ||
}; | ||
} | ||
return api; | ||
} | ||
var SCROLLABLE_CONTAINER$2; | ||
/* eslint-env browser, node */ | ||
var tinytemplate = function tinyTemplate(template, data, fallback) { | ||
return template.replace(/\$\{[^}]+\}/g, function (match) { | ||
return match.slice(2, -1).trim().split('.').reduce(function (searchObject, key) { | ||
return searchObject[key] || fallback || match; | ||
}, data); | ||
}); | ||
}; | ||
function getScrollableContainer$2() { | ||
if (SCROLLABLE_CONTAINER$2) { | ||
return SCROLLABLE_CONTAINER$2; | ||
} | ||
var Events = { | ||
Error: 'formjax:error', | ||
Success: 'formjax:success' | ||
}; | ||
var documentElement = window.document.documentElement; | ||
var scrollableContainer; | ||
documentElement.scrollTop = 1; | ||
var Css = { | ||
Progress: 'formjax-progress', | ||
Error: 'formjax-error', | ||
Success: 'formjax-success' | ||
}; | ||
if (documentElement.scrollTop === 1) { | ||
documentElement.scrollTop = 0; | ||
scrollableContainer = documentElement; | ||
} else { | ||
scrollableContainer = document.body; | ||
} | ||
var Formjax = function (_Domodule) { | ||
inherits(Formjax, _Domodule); | ||
function Formjax() { | ||
classCallCheck(this, Formjax); | ||
return possibleConstructorReturn(this, (Formjax.__proto__ || Object.getPrototypeOf(Formjax)).apply(this, arguments)); | ||
SCROLLABLE_CONTAINER$2 = scrollableContainer; | ||
return scrollableContainer; | ||
} | ||
createClass(Formjax, [{ | ||
key: 'preInit', | ||
value: function preInit() { | ||
var _this2 = this; | ||
SCROLLABLE_CONTAINER$2 = getScrollableContainer$2(); | ||
if (this.el.tagName !== 'FORM') { | ||
throw new Error('Formjax need to be attached to a form'); | ||
} | ||
var Events = { | ||
Error: 'formjax:error', | ||
Success: 'formjax:success' | ||
}; | ||
var Css = { | ||
Progress: 'formjax-progress', | ||
Error: 'formjax-error', | ||
Success: 'formjax-success' | ||
}; | ||
this.method = this.el.getAttribute('method').toUpperCase(); | ||
this.url = this.el.getAttribute('action'); | ||
this.form = formobj(this.el); | ||
this.sending = false; | ||
var Formjax = | ||
/*#__PURE__*/ | ||
function (_Domodule) { | ||
_inherits(Formjax, _Domodule); | ||
on(this.el, 'submit', function (event) { | ||
_this2.submit(_this2.el, event); | ||
}); | ||
function Formjax() { | ||
_classCallCheck(this, Formjax); | ||
return _possibleConstructorReturn(this, _getPrototypeOf(Formjax).apply(this, arguments)); | ||
} | ||
}, { | ||
key: 'confirm', | ||
value: function confirm(sendForm) { | ||
if (window.confirm(this.options.confirmText)) { | ||
// eslint-disable-line no-alert | ||
sendForm(); | ||
} | ||
} | ||
}, { | ||
key: 'submit', | ||
value: function submit(el, event) { | ||
event.preventDefault(); | ||
if (this.options.confirm) { | ||
this.confirm(this.sendForm.bind(this)); | ||
} else { | ||
this.sendForm(); | ||
_createClass(Formjax, [{ | ||
key: "preInit", | ||
value: function preInit() { | ||
var _this = this; | ||
if (this.el.tagName !== 'FORM') { | ||
throw new Error('Formjax need to be attached to a form'); | ||
} | ||
this.method = this.el.getAttribute('method').toUpperCase(); | ||
this.url = this.el.getAttribute('action'); | ||
this.form = formobj(this.el); | ||
this.sending = false; | ||
on$1(this.el, 'submit', function (event) { | ||
_this.submit(_this.el, event); | ||
}); | ||
} | ||
} | ||
}, { | ||
key: 'sendForm', | ||
value: function sendForm() { | ||
if (this.sending) { | ||
return; | ||
}, { | ||
key: "confirm", | ||
value: function confirm(sendForm) { | ||
if (window.confirm(this.options.confirmText)) { | ||
// eslint-disable-line no-alert | ||
sendForm(); | ||
} | ||
} | ||
}, { | ||
key: "submit", | ||
value: function submit(el, event) { | ||
event.preventDefault(); | ||
removeClass(this.el, [Css.Error, Css.Success]); | ||
addClass(this.el, Css.Progress); | ||
if (this.options.confirm) { | ||
this.confirm(this.sendForm.bind(this)); | ||
} else { | ||
this.sendForm(); | ||
} | ||
} | ||
}, { | ||
key: "sendForm", | ||
value: function sendForm() { | ||
if (this.sending) { | ||
return; | ||
} | ||
this.sending = true; | ||
var args = [this.url, this.method]; | ||
removeClass(this.el, [Css.Error, Css.Success]); | ||
addClass(this.el, Css.Progress); | ||
this.sending = true; | ||
var args = [this.url, this.method]; | ||
if (this.method === 'GET') { | ||
var url = this.url; | ||
var uri = this.form.getQueryString(); | ||
if (this.method === 'GET') { | ||
var url = this.url; | ||
var uri = this.form.getQueryString(); | ||
if (url.indexOf('?') > -1) { | ||
url = url + '&' + uri; | ||
if (url.indexOf('?') > -1) { | ||
url = "".concat(url, "&").concat(uri); | ||
} else { | ||
url = "".concat(url, "?").concat(uri); | ||
} | ||
args[0] = url; | ||
} else { | ||
url = url + '?' + uri; | ||
args.push(this.form.getJSON()); | ||
} | ||
args[0] = url; | ||
} else { | ||
args.push(this.form.getJSON()); | ||
this.request(args); | ||
} | ||
}, { | ||
key: "request", | ||
value: function request(args) { | ||
var _this2 = this; | ||
this.request(args); | ||
} | ||
}, { | ||
key: 'request', | ||
value: function request(args) { | ||
var _this3 = this; | ||
Ajax.request.apply(Ajax, _toConsumableArray(args).concat([function (err, resp) { | ||
var eventName = ''; | ||
var className = ''; | ||
_this2.sending = false; | ||
Ajax.request.apply(Ajax, toConsumableArray(args).concat([function (err, resp) { | ||
var eventName = ''; | ||
var className = ''; | ||
_this3.sending = false; | ||
if (!err && resp.statusCode < 400) { | ||
eventName = Events.Success; | ||
className = Css.Success; | ||
if (!err && resp.statusCode < 400) { | ||
eventName = Events.Success; | ||
className = Css.Success; | ||
if (_this2.options.successReload) { | ||
Formjax.reload(); | ||
} else if (_this2.options.success) { | ||
try { | ||
var url = tinytemplate(_this2.options.success, resp.data); | ||
Formjax.goTo(url); | ||
} catch (e) { | ||
alert(e.message); // eslint-disable-line no-alert | ||
} | ||
} | ||
} else { | ||
eventName = Events.Error; | ||
className = Css.Error; | ||
if (_this3.options.successReload) { | ||
Formjax.reload(); | ||
} else if (_this3.options.success) { | ||
try { | ||
var url = tinytemplate(_this3.options.success, resp.data); | ||
Formjax.goTo(url); | ||
} catch (e) { | ||
alert(e.message); // eslint-disable-line no-alert | ||
if (resp.data && resp.data.message) { | ||
alert(resp.data.message); // eslint-disable-line no-alert | ||
} | ||
} | ||
} else { | ||
eventName = Events.Error; | ||
className = Css.Error; | ||
if (resp.data && resp.data.message) { | ||
alert(resp.data.message); // eslint-disable-line no-alert | ||
} | ||
} | ||
removeClass(_this2.el, Css.Progress); | ||
addClass(_this2.el, className); | ||
fire(_this2.el, eventName, { | ||
bubbles: true, | ||
detail: resp.data | ||
}); | ||
}])); | ||
} | ||
}, { | ||
key: "defaults", | ||
get: function get() { | ||
return { | ||
confirm: false, | ||
successReload: false, | ||
confirmText: 'Are you sure you want to submit?' | ||
}; | ||
} | ||
}], [{ | ||
key: "reload", | ||
value: function reload() { | ||
window.location.reload(); | ||
} | ||
}, { | ||
key: "goTo", | ||
value: function goTo(url) { | ||
window.location.href = url; | ||
} | ||
}]); | ||
removeClass(_this3.el, Css.Progress); | ||
addClass(_this3.el, className); | ||
return Formjax; | ||
}(Domodule); | ||
fire(_this3.el, eventName, { | ||
bubbles: true, | ||
detail: resp.data | ||
}); | ||
}])); | ||
} | ||
}, { | ||
key: 'defaults', | ||
get: function get$$1() { | ||
return { | ||
confirm: false, | ||
successReload: false, | ||
confirmText: 'Are you sure you want to submit?' | ||
}; | ||
} | ||
}], [{ | ||
key: 'reload', | ||
value: function reload() { | ||
window.location.reload(); | ||
} | ||
}, { | ||
key: 'goTo', | ||
value: function goTo(url) { | ||
window.location.href = url; | ||
} | ||
}]); | ||
Domodule.register('Formjax', Formjax); | ||
return Formjax; | ||
}(Domodule); | ||
Domodule.register('Formjax', Formjax); | ||
return Formjax; | ||
}()); | ||
//# sourceMappingURL=formjax.js.map | ||
//# sourceMappingURL=formjax.js.map |
{ | ||
"name": "formjax", | ||
"version": "1.4.1", | ||
"description": "", | ||
"main": "dist/formjax.js", | ||
"module": "index.js", | ||
"version": "2.0.0", | ||
"description": "Submit a form via ajax", | ||
"main": "index.js", | ||
"module": "dist/formjax.esm.js", | ||
"keywords": [], | ||
@@ -26,9 +26,9 @@ "scripts": { | ||
"devDependencies": { | ||
"eslint-config-firstandthird": "4.3.0", | ||
"eslint-plugin-import": "2.9.0", | ||
"phantomjs-prebuilt": "2.1.16", | ||
"scriptkit": "0.3.0", | ||
"tap-spec": "4.1.1", | ||
"tape-rollup": "4.6.4", | ||
"tape-run": "3.0.4" | ||
"eslint-config-firstandthird": "^4.3.0", | ||
"eslint-plugin-import": "^2.16.0", | ||
"phantomjs-prebuilt": "^2.1.16", | ||
"scriptkit": "^2.0.1", | ||
"tap-spec": "^5.0.0", | ||
"tape-rollup": "^4.6.4", | ||
"tape-run": "^5.0.0" | ||
}, | ||
@@ -39,2 +39,5 @@ "eslintConfig": { | ||
}, | ||
"serve": { | ||
"enabled": true | ||
}, | ||
"extends": "firstandthird" | ||
@@ -57,7 +60,7 @@ }, | ||
"bequest": "^1.4.2", | ||
"domassist": "^1.9.0", | ||
"domodule": "^5.1.3", | ||
"formobj": "^4.0.0", | ||
"domassist": "^2.0.1", | ||
"domodule": "^5.2.0", | ||
"formobj": "^4.0.1", | ||
"tinytemplate": "^2.0.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
0
81895
7
2484
+ Addeddomassist@2.3.2(transitive)
Updateddomassist@^2.0.1
Updateddomodule@^5.2.0
Updatedformobj@^4.0.1