umbrellajs
Advanced tools
Comparing version 2.10.3 to 3.0.0
@@ -72,20 +72,3 @@ # Documentation | ||
It plays well with other libraries, including jquery. For example, with [pagex.js](http://github.com/franciscop/pagex): | ||
```js | ||
// When we are on the page "/login" | ||
page(/^login/, function(){ | ||
function done(err, res){ | ||
if (err) return alert("There was an error"); | ||
window.location.href = "/user/" + res.id; | ||
}; | ||
// Find the form and handle it through ajax when it's submitted | ||
u("form.login").ajax(done); | ||
}); | ||
``` | ||
### Native methods | ||
@@ -298,90 +281,2 @@ | ||
## .ajax() | ||
Make all of the matched forms to be submitted by ajax with the same action, method and values when the user submits the form. | ||
> Note: this method does NOT submit the form, it just handles it when it's submitted (from the user or with .trigger()) | ||
```js | ||
.ajax(done, before); | ||
``` | ||
### Parameters | ||
`done` [optional]: A function to be called when the request ends. The first argument is the error, if any. The second is the body, which is parsed to JSON if it's a JSON string or just the body as a string if it's not JSON. The third is the request object itself. | ||
```js | ||
var done = function(err, body, xhr){}; | ||
``` | ||
`before` [optional]: A function to be called before the request is sent. Useful to manipulate some data in real-time. | ||
```js | ||
var before = function(xhr){}; | ||
``` | ||
### Return | ||
**Undefined**. Please don't use the returned value for anything (it might be a promise in the future). | ||
### Examples | ||
Handle the newsletter through ajax | ||
```js | ||
u('.newsletter').ajax(function(err){ | ||
if (err) return alert("Error"); | ||
alert("Thank you for subscribing, awesome!"); | ||
}); | ||
``` | ||
Actually send a form through ajax: | ||
```js | ||
u('form.edit').ajax(function(){ console.log('Sent!'); }).trigger('submit'); | ||
``` | ||
### Why not jquery? | ||
This was created because this pattern is quite common in jquery: | ||
```js | ||
$('form').on('submit', function(e){ | ||
e.preventDefault(); | ||
$.post($(this).attr('action'), $(this).serialize(), function(data){ | ||
alert("Done! Thanks, " + data.name); | ||
}, 'json'); | ||
}); | ||
``` | ||
After repeating that many times, I found out that it's better if we just make that the default. The same code on Umbrella JS: | ||
```js | ||
u('form').ajax(function(err, data){ | ||
if (!err) alert('Done! Thanks, ' + data.name); | ||
}); | ||
``` | ||
Of course you have freedom and you can use a similar method to jquery, but I think it's a bit pointless for this specific situation: | ||
```js | ||
u('form').on('submit', function(e){ | ||
e.preventDefault(); | ||
var options = { method: u(this).attr('method'), body: u(this).serialize() }; | ||
ajax(u(this).attr('action'), options, function(err, data){ | ||
if (!err) alert("Done! Thanks, " + data.name); | ||
}); | ||
}); | ||
``` | ||
### Related | ||
[ajax()](#ajaxfn): perform ajax requests | ||
## .append() | ||
@@ -1101,45 +996,2 @@ | ||
[.last()](#last) retrieve the last matched element | ||
## ajax() fn | ||
Function (not method) that allows performing ajax requests. The implementation is somewhat similar to [nanoajax](https://github.com/yanatan16/nanoajax): | ||
```js | ||
var action = '/save'; | ||
var options = { body: 'a=b' }; | ||
var after = function(err, data){ console.log(data); }; | ||
var before = function(xhr){}; | ||
ajax(action, options, after, before); | ||
``` | ||
### Parameters | ||
`action`: the place where to send the ajax request | ||
`options`: an object that sets the options to be passed. These are: | ||
- `method = 'GET'`: the way to send the request. It can be GET or POST | ||
- `body = ''`: a string on the `a=b&c=d` format or a simple object that will be converted | ||
- `headers = {}`: an object with `{ key: value }` headers to be manually set | ||
`after`: the callback to be called when the request has been sent and parsed. The first parameter is an error that can be null, and the second one the parsed data in JSON or the unparsed data as an string. | ||
`before`: a callback that can be called just before sending the request. It receives the XHR object as the first parameter. | ||
### Return | ||
Returns the already sent XHR object. | ||
### Tips | ||
You can modify the XHR object straight by using the *before* callback. It is called just before sending the request, after setting all its parameters: | ||
```js | ||
ajax('/save', {}, after, function(xhr){ | ||
xhr.responseType = 'json'; | ||
}); | ||
``` | ||
## .handle() | ||
@@ -1329,8 +1181,10 @@ | ||
```js | ||
u('form.subscribe').ajax(false, function() { | ||
u('form.subscribe').on('submit', function(e) { | ||
// Same as u('form.subscribe').hasClass('validate') | ||
if (u('form.subscribe').is('.validate')) { | ||
if (u(e.target).is('.validate')) { | ||
validate(); | ||
} | ||
// ... | ||
}); | ||
@@ -1346,2 +1200,3 @@ ``` | ||
[.not()](#not) remove all the nodes that match the criteria | ||
## .last() | ||
@@ -1591,3 +1446,3 @@ | ||
// This example is very similar to .ajax() implementation | ||
// Submit a form through Ajax | ||
u('form.test').on('submit', function(e){ | ||
@@ -1599,3 +1454,3 @@ | ||
// Submit the form through ajax | ||
ajax(u(this).attr('action'), u(this).serialize()); | ||
fetch(u(this).attr('action'), { body: u(this).serialize(), ... }); | ||
}); | ||
@@ -1907,2 +1762,30 @@ | ||
### Example | ||
For this form: | ||
```html | ||
<form action="/contact" method="POST"> | ||
Email: | ||
<input type="email" name="email" value="test@example.com" /> | ||
Message: | ||
<textarea type="email" name="message">Hello world</textarea> | ||
<button>Send</button> | ||
</form> | ||
``` | ||
When the user clicks on the "Send" button, the following handler can be used to send the data through Ajax: | ||
```js | ||
// .handle() == .on() + preventDefault() | ||
u('form.contact').handle('submit', async e => { | ||
const body = u(e.target).serialize(); | ||
console.log(body); // email=test@example.com&message=Hello+world | ||
const res = await fetch('/contact', { method: 'POST', body }); | ||
const data = await res.json(); | ||
console.log('Response data:', data); | ||
}); | ||
``` | ||
## .siblings() | ||
@@ -2135,8 +2018,5 @@ | ||
```js | ||
// Make the form to submit through ajax | ||
u('form.edit').ajax(); | ||
// Submit it every 10s | ||
setInterval(function(){ | ||
u('form.edit').trigger('submit'); | ||
u('button.save').trigger('click'); | ||
}, 10000); | ||
@@ -2143,0 +2023,0 @@ ``` |
@@ -9,6 +9,11 @@ // This builds the library itself | ||
}, | ||
my_target: { | ||
umbrella: { | ||
files: { | ||
'umbrella.min.js': 'umbrella.js' | ||
} | ||
}, | ||
web: { | ||
files: { | ||
'docs/umbrella.min.js': 'umbrella.js' | ||
} | ||
} | ||
@@ -80,3 +85,3 @@ }, | ||
files: { | ||
'test/test.js': ['src/test.js', 'src/plugins/**/test.js'] | ||
'docs/test.js': ['src/test.js', 'src/plugins/**/test.js'] | ||
} | ||
@@ -83,0 +88,0 @@ } |
{ | ||
"name": "umbrellajs", | ||
"version": "2.10.3", | ||
"version": "3.0.0", | ||
"homepage": "http://umbrellajs.com/", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -1,4 +0,4 @@ | ||
# Umbrella JS [![Circle CI](https://circleci.com/gh/franciscop/umbrella/tree/master.svg?style=shield)](https://circleci.com/gh/franciscop/umbrella/tree/master) [![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg)](https://github.com/Flet/semistandard) [![License MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/franciscop/umbrella/blob/master/LICENSE) | ||
# Umbrella JS [![Circle CI](https://circleci.com/gh/franciscop/umbrella/tree/master.svg?style=shield)](https://circleci.com/gh/franciscop/umbrella/tree/master) [![stats](https://data.jsdelivr.com/v1/package/npm/umbrellajs/badge?style=rounded)](https://www.jsdelivr.com/package/npm/umbrellajs) [![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg)](https://github.com/Flet/semistandard) [![License MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/franciscop/umbrella/blob/master/LICENSE) | ||
> [**Library Documentation**](http://umbrellajs.com/documentation) | [**Migrating from jQuery guide**](https://github.com/franciscop/umbrella/blob/master/jquery.md) | ||
> [**Library Documentation**](http://umbrellajs.com/documentation) | [**Migrate from 2.0 to 3.0**](https://github.com/franciscop/umbrella/blob/master/migrate-2-to-3.md) | [**Migrating from jQuery guide**](https://github.com/franciscop/umbrella/blob/master/jquery.md) | ||
@@ -12,14 +12,15 @@ Covers your javascript needs for those rainy days. A <3kb performant jquery-like library born from the question: [You might not need jquery](http://youmightnotneedjquery.com/), then what do you need? | ||
- Event handling | ||
- Ajax | ||
A simple example: | ||
A couple of simple examples: | ||
```js | ||
// Simple events like jquery | ||
u("button").on('click', function(){ | ||
u("button").on('click', e => { | ||
alert("Hello world"); | ||
}); | ||
// Send form through ajax when submitted | ||
u('form.login').ajax(function(err, res){ | ||
// Handle form submissions | ||
u('form.login').handle('submit', async e => { | ||
const body = u(e.target).serialize(); | ||
const user = await fetch('/login', { method: 'POST', body }); | ||
window.href = '/user/' + res.id; | ||
@@ -26,0 +27,0 @@ }); |
// Export it for webpack | ||
if (typeof module === 'object' && module.exports) { | ||
module.exports = { | ||
u: u, | ||
ajax: ajax | ||
}; | ||
module.exports = { u: u }; | ||
} |
@@ -33,8 +33,10 @@ ## .is() | ||
```js | ||
u('form.subscribe').ajax(false, function() { | ||
u('form.subscribe').on('submit', function(e) { | ||
// Same as u('form.subscribe').hasClass('validate') | ||
if (u('form.subscribe').is('.validate')) { | ||
if (u(e.target).is('.validate')) { | ||
validate(); | ||
} | ||
// ... | ||
}); | ||
@@ -49,2 +51,2 @@ ``` | ||
[.not()](#not) remove all the nodes that match the criteria | ||
[.not()](#not) remove all the nodes that match the criteria |
@@ -54,3 +54,3 @@ ## .on() | ||
// This example is very similar to .ajax() implementation | ||
// Submit a form through Ajax | ||
u('form.test').on('submit', function(e){ | ||
@@ -62,3 +62,3 @@ | ||
// Submit the form through ajax | ||
ajax(u(this).attr('action'), u(this).serialize()); | ||
fetch(u(this).attr('action'), { body: u(this).serialize(), ... }); | ||
}); | ||
@@ -65,0 +65,0 @@ |
@@ -7,44 +7,7 @@ // [INTERNAL USE ONLY] | ||
if (context) { | ||
return this.select.byCss(parameter, context); | ||
if (/^</.test(parameter)) { | ||
return u().generate(parameter); | ||
} | ||
for (var key in this.selectors) { | ||
// Reusing it to save space | ||
context = key.split('/'); | ||
if ((new RegExp(context[1], context[2])).test(parameter)) { | ||
return this.selectors[key](parameter); | ||
} | ||
} | ||
return this.select.byCss(parameter); | ||
}; | ||
// Select some elements using a css Selector | ||
u.prototype.select.byCss = function (parameter, context) { | ||
return (context || document).querySelectorAll(parameter); | ||
}; | ||
// Allow for adding/removing regexes and parsing functions | ||
// It stores a regex: function pair to process the parameter and context | ||
u.prototype.selectors = {}; | ||
// Find some html nodes using an Id | ||
u.prototype.selectors[/^\.[\w\-]+$/] = function (param) { | ||
return document.getElementsByClassName(param.substring(1)); | ||
}; | ||
// The tag nodes | ||
u.prototype.selectors[/^\w+$/] = function (param) { | ||
return document.getElementsByTagName(param); | ||
}; | ||
// Find some html nodes using an Id | ||
u.prototype.selectors[/^\#[\w\-]+$/] = function (param) { | ||
return document.getElementById(param.substring(1)); | ||
}; | ||
// Create a new element for the DOM | ||
u.prototype.selectors[/^</] = function (param) { | ||
return u().generate(param); | ||
}; |
@@ -8,8 +8,2 @@ // Testing the main file | ||
it("is fine-tuned for context (use css with that)", function() { | ||
var withContext = u().select('a', u('.brand').first())[0]; | ||
var withCss = u().select.byCss('.brand a')[0]; | ||
expect(withContext).to.equal(withCss); | ||
}); | ||
it("can select by class", function(){ | ||
@@ -26,4 +20,4 @@ expect(u().select('.base').length).to.equal(1); | ||
it("can select by id", function(){ | ||
expect(u().select('#base')).to.not.equal(null); | ||
expect(u().select('#base').nodeName).to.equal('DIV'); | ||
expect(u().select('#base')[0]).to.not.equal(null); | ||
expect(u().select('#base')[0].nodeName).to.equal('DIV'); | ||
}); | ||
@@ -30,0 +24,0 @@ |
@@ -10,1 +10,29 @@ ## .serialize() | ||
> Note: multiple-select are not supported in Internet Explorer, [similarly to jQuery](https://github.com/jquery/jquery-mobile/issues/3947) | ||
### Example | ||
For this form: | ||
```html | ||
<form action="/contact" method="POST"> | ||
Email: | ||
<input type="email" name="email" value="test@example.com" /> | ||
Message: | ||
<textarea type="email" name="message">Hello world</textarea> | ||
<button>Send</button> | ||
</form> | ||
``` | ||
When the user clicks on the "Send" button, the following handler can be used to send the data through Ajax: | ||
```js | ||
// .handle() == .on() + preventDefault() | ||
u('form.contact').handle('submit', async e => { | ||
const body = u(e.target).serialize(); | ||
console.log(body); // email=test@example.com&message=Hello+world | ||
const res = await fetch('/contact', { method: 'POST', body }); | ||
const data = await res.json(); | ||
console.log('Response data:', data); | ||
}); | ||
``` |
@@ -32,8 +32,5 @@ ## .trigger() | ||
```js | ||
// Make the form to submit through ajax | ||
u('form.edit').ajax(); | ||
// Submit it every 10s | ||
setInterval(function(){ | ||
u('form.edit').trigger('submit'); | ||
u('button.save').trigger('click'); | ||
}, 10000); | ||
@@ -40,0 +37,0 @@ ``` |
@@ -72,20 +72,3 @@ # Documentation | ||
It plays well with other libraries, including jquery. For example, with [pagex.js](http://github.com/franciscop/pagex): | ||
```js | ||
// When we are on the page "/login" | ||
page(/^login/, function(){ | ||
function done(err, res){ | ||
if (err) return alert("There was an error"); | ||
window.location.href = "/user/" + res.id; | ||
}; | ||
// Find the form and handle it through ajax when it's submitted | ||
u("form.login").ajax(done); | ||
}); | ||
``` | ||
### Native methods | ||
@@ -92,0 +75,0 @@ |
218
src/test.js
@@ -172,111 +172,111 @@ var expect = chai.expect; | ||
describe("performance tests", function(){ | ||
function performance(callback, times){ | ||
var init = new Date().getTime(); | ||
for (var i = 0; i < times; i++) { | ||
callback(i); | ||
} | ||
return new Date().getTime() - init; | ||
} | ||
// Generate a big and varied 100 element table | ||
before(function(){ | ||
performance(function(i){ | ||
u('.performance').append('<tr class="ro"><td id="idn' + i + '"></td><td class="tabletest"></td><td></td><td></td></tr>'); | ||
}, 1000); | ||
}); | ||
after(function(){ | ||
u('.performance').remove(); | ||
}); | ||
it("simple select by class 10.000/second", function() { | ||
uTime = performance(function(){ | ||
u('.demo'); | ||
}, 1000); | ||
console.log(' - u: ' + uTime + 'ms'); | ||
expect(uTime).to.be.below(100, uTime + ' ms'); | ||
}); | ||
it("select by class is comparable to jquery (50% margin)", function() { | ||
size('.demo', 1); | ||
var uTime = performance(function(){ | ||
u('.demo'); | ||
}, 5000); | ||
var $Time = performance(function(){ | ||
$('.demo'); | ||
}, 5000); | ||
console.log(' - u: ' + uTime + 'ms $: ' + $Time + 'ms'); | ||
expect(uTime).to.be.below($Time * 1.5, uTime + ' ms'); | ||
}); | ||
it("vs jquery: class selector (50% margin)", function() { | ||
size('.tabletest', 1000); | ||
var uTime = performance(function(){ | ||
u('.tabletest'); | ||
}, 100); | ||
var $Time = performance(function(){ | ||
$('.tabletest'); | ||
}, 100); | ||
console.log(' - u: ' + uTime + 'ms $: ' + $Time + 'ms'); | ||
expect(uTime).to.be.below($Time * 1.5, uTime + ' ms'); | ||
}); | ||
it("vs jquery: complex selector (50% margin)", function() { | ||
size('table td:first-child', 1000); | ||
var uTime = performance(function(){ | ||
u('table td:first-child'); | ||
}, 50); | ||
var $Time = performance(function(){ | ||
$('table td:first-child'); | ||
}, 50); | ||
console.log(' - u: ' + uTime + 'ms $: ' + $Time + 'ms'); | ||
expect(uTime).to.be.below($Time * 1.5, uTime + ' ms'); | ||
}); | ||
it("vs jquery: jquery optimized vs raw umbrella (50% margin)", function() { | ||
size(".ro > *", 4000); | ||
var uTime = performance(function(){ | ||
u(".ro > *"); | ||
}, 50); | ||
var $Time = performance(function(){ | ||
$(".ro > *"); | ||
}, 50); | ||
console.log(' - u: ' + uTime + 'ms $: ' + $Time + 'ms'); | ||
expect(uTime).to.be.below($Time * 1.5, uTime + ' ms'); | ||
}); | ||
}); | ||
// describe("performance tests", function(){ | ||
// | ||
// function performance(callback, times){ | ||
// var init = new Date().getTime(); | ||
// for (var i = 0; i < times; i++) { | ||
// callback(i); | ||
// } | ||
// return new Date().getTime() - init; | ||
// } | ||
// | ||
// // Generate a big and varied 100 element table | ||
// before(function(){ | ||
// performance(function(i){ | ||
// u('.performance').append('<tr class="ro"><td id="idn' + i + '"></td><td class="tabletest"></td><td></td><td></td></tr>'); | ||
// }, 1000); | ||
// }); | ||
// | ||
// after(function(){ | ||
// u('.performance').remove(); | ||
// }); | ||
// | ||
// | ||
// | ||
// it("simple select by class 10.000/second", function() { | ||
// | ||
// uTime = performance(function(){ | ||
// u('.demo'); | ||
// }, 1000); | ||
// | ||
// console.log(' - u: ' + uTime + 'ms'); | ||
// expect(uTime).to.be.below(100, uTime + ' ms'); | ||
// }); | ||
// | ||
// | ||
// | ||
// it("select by class is comparable to jquery (50% margin)", function() { | ||
// | ||
// size('.demo', 1); | ||
// | ||
// var uTime = performance(function(){ | ||
// u('.demo'); | ||
// }, 5000); | ||
// | ||
// var $Time = performance(function(){ | ||
// $('.demo'); | ||
// }, 5000); | ||
// | ||
// console.log(' - u: ' + uTime + 'ms $: ' + $Time + 'ms'); | ||
// | ||
// expect(uTime).to.be.below($Time * 1.5, uTime + ' ms'); | ||
// }); | ||
// | ||
// | ||
// | ||
// it("vs jquery: class selector (50% margin)", function() { | ||
// | ||
// size('.tabletest', 1000); | ||
// | ||
// var uTime = performance(function(){ | ||
// u('.tabletest'); | ||
// }, 100); | ||
// | ||
// var $Time = performance(function(){ | ||
// $('.tabletest'); | ||
// }, 100); | ||
// | ||
// console.log(' - u: ' + uTime + 'ms $: ' + $Time + 'ms'); | ||
// | ||
// expect(uTime).to.be.below($Time * 1.5, uTime + ' ms'); | ||
// }); | ||
// | ||
// | ||
// | ||
// it("vs jquery: complex selector (50% margin)", function() { | ||
// | ||
// size('table td:first-child', 1000); | ||
// | ||
// var uTime = performance(function(){ | ||
// u('table td:first-child'); | ||
// }, 50); | ||
// | ||
// var $Time = performance(function(){ | ||
// $('table td:first-child'); | ||
// }, 50); | ||
// | ||
// console.log(' - u: ' + uTime + 'ms $: ' + $Time + 'ms'); | ||
// | ||
// expect(uTime).to.be.below($Time * 1.5, uTime + ' ms'); | ||
// }); | ||
// | ||
// | ||
// | ||
// it("vs jquery: jquery optimized vs raw umbrella (50% margin)", function() { | ||
// | ||
// size(".ro > *", 4000); | ||
// | ||
// var uTime = performance(function(){ | ||
// u(".ro > *"); | ||
// }, 50); | ||
// | ||
// var $Time = performance(function(){ | ||
// $(".ro > *"); | ||
// }, 50); | ||
// | ||
// console.log(' - u: ' + uTime + 'ms $: ' + $Time + 'ms'); | ||
// | ||
// expect(uTime).to.be.below($Time * 1.5, uTime + ' ms'); | ||
// }); | ||
// }); | ||
}); |
146
umbrella.js
@@ -100,15 +100,2 @@ // Umbrella JS http://umbrellajs.com/ | ||
// Create a HTTP request for whenever the matched form submits | ||
u.prototype.ajax = function (done, before) { | ||
return this.handle('submit', function (e) { | ||
ajax( | ||
u(this).attr('action'), | ||
{ body: u(this).serialize(), method: u(this).attr('method') }, | ||
done && done.bind(this), | ||
before && before.bind(this) | ||
); | ||
}); | ||
}; | ||
// Add some html as a child at the end of each of the matched elements. | ||
@@ -363,90 +350,3 @@ u.prototype.append = function (html, data) { | ||
// Perform ajax calls | ||
/* eslint-disable no-unused-vars*/ | ||
function ajax (action, opt, done, before) { | ||
done = done || function () {}; | ||
// A bunch of options and defaults | ||
opt = opt || {}; | ||
opt.body = opt.body || {}; | ||
opt.method = (opt.method || 'GET').toUpperCase(); | ||
opt.headers = opt.headers || {}; | ||
// Tell the back-end it's an AJAX request | ||
opt.headers['X-Requested-With'] = opt.headers['X-Requested-With'] || 'XMLHttpRequest'; | ||
if (typeof window.FormData === 'undefined' || !(opt.body instanceof window.FormData)) { | ||
opt.headers['Content-Type'] = opt.headers['Content-Type'] || 'application/x-www-form-urlencoded'; | ||
} | ||
// If it's of type JSON, encode it as such | ||
if (/json/.test(opt.headers['Content-Type'])) { | ||
opt.body = JSON.stringify(opt.body); | ||
} | ||
if ((typeof opt.body === 'object') && !(opt.body instanceof window.FormData)) { | ||
opt.body = u().param(opt.body); | ||
} | ||
// Create and send the actual request | ||
var request = new window.XMLHttpRequest(); | ||
// An error is just an error | ||
// This uses a little hack of passing an array to u() so it handles it as | ||
// an array of nodes, hence we can use 'on'. However a single element wouldn't | ||
// work since it a) doesn't have nodeName and b) it will be sliced, failing | ||
u(request).on('error timeout abort', function () { | ||
done(new Error(), null, request); | ||
}).on('load', function () { | ||
// Also an error if it doesn't start by 2 or 3... | ||
// This is valid as there's no code 2x nor 2, nor 3x nor 3, only 2xx and 3xx | ||
// We don't want to return yet though as there might be some content | ||
var err = /^(4|5)/.test(request.status) ? new Error(request.status) : null; | ||
// Attempt to parse the body into JSON | ||
var body = parseJson(request.response) || request.response; | ||
return done(err, body, request); | ||
}); | ||
// Create a request of the specified type to the URL and ASYNC | ||
request.open(opt.method, action); | ||
request.withCredentials = true; | ||
// Set the corresponding headers | ||
for (var name in opt.headers) { | ||
request.setRequestHeader(name, opt.headers[name]); | ||
} | ||
// Load the before callback before sending the data | ||
if (before) before(request); | ||
request.send(opt.body); | ||
return request; | ||
} | ||
/* eslint-enable no-unused-vars*/ | ||
// [INTERNAL USE ONLY] | ||
// Parse JSON without throwing an error | ||
/* eslint-disable no-unused-vars*/ | ||
function parseJson (jsonString) { | ||
try { | ||
var o = JSON.parse(jsonString); | ||
// Handle non-exception-throwing cases: | ||
// Neither JSON.parse(false) or JSON.parse(1234) throw errors, hence the type-checking | ||
// so we must check for that, too. | ||
if (o && typeof o === 'object') { | ||
return o; | ||
} | ||
} catch (e) {} | ||
return false; | ||
} | ||
/* eslint-enable no-unused-vars*/ | ||
// [INTERNAL USE ONLY] | ||
// Generate a fragment of HTML. This irons out the inconsistences | ||
@@ -684,47 +584,10 @@ u.prototype.generate = function (html) { | ||
if (context) { | ||
return this.select.byCss(parameter, context); | ||
if (/^</.test(parameter)) { | ||
return u().generate(parameter); | ||
} | ||
for (var key in this.selectors) { | ||
// Reusing it to save space | ||
context = key.split('/'); | ||
if ((new RegExp(context[1], context[2])).test(parameter)) { | ||
return this.selectors[key](parameter); | ||
} | ||
} | ||
return this.select.byCss(parameter); | ||
}; | ||
// Select some elements using a css Selector | ||
u.prototype.select.byCss = function (parameter, context) { | ||
return (context || document).querySelectorAll(parameter); | ||
}; | ||
// Allow for adding/removing regexes and parsing functions | ||
// It stores a regex: function pair to process the parameter and context | ||
u.prototype.selectors = {}; | ||
// Find some html nodes using an Id | ||
u.prototype.selectors[/^\.[\w\-]+$/] = function (param) { | ||
return document.getElementsByClassName(param.substring(1)); | ||
}; | ||
// The tag nodes | ||
u.prototype.selectors[/^\w+$/] = function (param) { | ||
return document.getElementsByTagName(param); | ||
}; | ||
// Find some html nodes using an Id | ||
u.prototype.selectors[/^\#[\w\-]+$/] = function (param) { | ||
return document.getElementById(param.substring(1)); | ||
}; | ||
// Create a new element for the DOM | ||
u.prototype.selectors[/^</] = function (param) { | ||
return u().generate(param); | ||
}; | ||
// Convert forms into a string able to be submitted | ||
@@ -900,6 +763,3 @@ // Original source: http://stackoverflow.com/q/11661187 | ||
if (typeof module === 'object' && module.exports) { | ||
module.exports = { | ||
u: u, | ||
ajax: ajax | ||
}; | ||
module.exports = { u: u }; | ||
} |
@@ -1,2 +0,2 @@ | ||
/* Umbrella JS 2.10.2 umbrellajs.com */ | ||
function ajax(a,b,c,d){c=c||function(){},b=b||{},b.body=b.body||{},b.method=(b.method||"GET").toUpperCase(),b.headers=b.headers||{},b.headers["X-Requested-With"]=b.headers["X-Requested-With"]||"XMLHttpRequest","undefined"!=typeof window.FormData&&b.body instanceof window.FormData||(b.headers["Content-Type"]=b.headers["Content-Type"]||"application/x-www-form-urlencoded"),/json/.test(b.headers["Content-Type"])&&(b.body=JSON.stringify(b.body)),"object"!=typeof b.body||b.body instanceof window.FormData||(b.body=u().param(b.body));var e=new window.XMLHttpRequest;u(e).on("error timeout abort",function(){c(new Error,null,e)}).on("load",function(){var a=/^(4|5)/.test(e.status)?new Error(e.status):null,b=parseJson(e.response)||e.response;return c(a,b,e)}),e.open(b.method,a),e.withCredentials=!0;for(var f in b.headers)e.setRequestHeader(f,b.headers[f]);return d&&d(e),e.send(b.body),e}function parseJson(a){try{var b=JSON.parse(a);if(b&&"object"==typeof b)return b}catch(c){}return!1}var u=function(a,b){return this instanceof u?a instanceof u?a:("string"==typeof a&&(a=this.select(a,b)),a&&a.nodeName&&(a=[a]),void(this.nodes=this.slice(a))):new u(a,b)};u.prototype={get length(){return this.nodes.length}},u.prototype.nodes=[],u.prototype.addClass=function(){return this.eacharg(arguments,function(a,b){a.classList.add(b)})},u.prototype.adjacent=function(a,b,c){return"number"==typeof b&&(b=0===b?[]:new Array(b).join().split(",").map(Number.call,Number)),this.each(function(d,e){var f=document.createDocumentFragment();u(b||{}).map(function(b,c){var f="function"==typeof a?a.call(this,b,c,d,e):a;return"string"==typeof f?this.generate(f):u(f)}).each(function(a){this.isInPage(a)?f.appendChild(u(a).clone().first()):f.appendChild(a)}),c.call(this,d,f)})},u.prototype.after=function(a,b){return this.adjacent(a,b,function(a,b){a.parentNode.insertBefore(b,a.nextSibling)})},u.prototype.ajax=function(a,b){return this.handle("submit",function(c){ajax(u(this).attr("action"),{body:u(this).serialize(),method:u(this).attr("method")},a&&a.bind(this),b&&b.bind(this))})},u.prototype.append=function(a,b){return this.adjacent(a,b,function(a,b){a.appendChild(b)})},u.prototype.args=function(a,b,c){return"function"==typeof a&&(a=a(b,c)),"string"!=typeof a&&(a=this.slice(a).map(this.str(b,c))),a.toString().split(/[\s,]+/).filter(function(a){return a.length})},u.prototype.array=function(a){a=a;var b=this;return this.nodes.reduce(function(c,d,e){var f;return a?(f=a.call(b,d,e),f||(f=!1),"string"==typeof f&&(f=u(f)),f instanceof u&&(f=f.nodes)):f=d.innerHTML,c.concat(f!==!1?f:[])},[])},u.prototype.attr=function(a,b,c){return c=c?"data-":"",this.pairs(a,b,function(a,b){return a.getAttribute(c+b)},function(a,b,d){a.setAttribute(c+b,d)})},u.prototype.before=function(a,b){return this.adjacent(a,b,function(a,b){a.parentNode.insertBefore(b,a)})},u.prototype.children=function(a){return this.map(function(a){return this.slice(a.children)}).filter(a)},u.prototype.clone=function(){return this.map(function(a,b){var c=a.cloneNode(!0),d=this.getAll(c);return this.getAll(a).each(function(a,b){for(var c in this.mirror)this.mirror[c](a,d.nodes[b])}),c})},u.prototype.getAll=function(a){return u([a].concat(u("*",a).nodes))},u.prototype.mirror={},u.prototype.mirror.events=function(a,b){if(a._e)for(var c in a._e)a._e[c].forEach(function(a){u(b).on(c,a)})},u.prototype.mirror.select=function(a,b){u(a).is("select")&&(b.value=a.value)},u.prototype.mirror.textarea=function(a,b){u(a).is("textarea")&&(b.value=a.value)},u.prototype.closest=function(a){return this.map(function(b){do if(u(b).is(a))return b;while((b=b.parentNode)&&b!==document)})},u.prototype.data=function(a,b){return this.attr(a,b,!0)},u.prototype.each=function(a){return this.nodes.forEach(a.bind(this)),this},u.prototype.eacharg=function(a,b){return this.each(function(c,d){this.args(a,c,d).forEach(function(a){b.call(this,c,a)},this)})},u.prototype.empty=function(){return this.each(function(a){for(;a.firstChild;)a.removeChild(a.firstChild)})},u.prototype.filter=function(a){var b=function(b){return b.matches=b.matches||b.msMatchesSelector||b.webkitMatchesSelector,b.matches(a||"*")};return"function"==typeof a&&(b=a),a instanceof u&&(b=function(b){return a.nodes.indexOf(b)!==-1}),u(this.nodes.filter(b))},u.prototype.find=function(a){return this.map(function(b){return u(a||"*",b)})},u.prototype.first=function(){return this.nodes[0]||!1},u.prototype.generate=function(a){return/^\s*<tr[> ]/.test(a)?u(document.createElement("table")).html(a).children().children().nodes:/^\s*<t(h|d)[> ]/.test(a)?u(document.createElement("table")).html(a).children().children().children().nodes:/^\s*</.test(a)?u(document.createElement("div")).html(a).children().nodes:document.createTextNode(a)},u.prototype.handle=function(){var a=this.slice(arguments).map(function(a){return"function"==typeof a?function(b){b.preventDefault(),a.apply(this,arguments)}:a},this);return this.on.apply(this,a)},u.prototype.hasClass=function(){return this.is("."+this.args(arguments).join("."))},u.prototype.html=function(a){return void 0===a?this.first().innerHTML||"":this.each(function(b){b.innerHTML=a})},u.prototype.is=function(a){return this.filter(a).length>0},u.prototype.isInPage=function(a){return a!==document.body&&document.body.contains(a)},u.prototype.last=function(){return this.nodes[this.length-1]||!1},u.prototype.map=function(a){return a?u(this.array(a)).unique():this},u.prototype.not=function(a){return this.filter(function(b){return!u(b).is(a||!0)})},u.prototype.off=function(a){return this.eacharg(a,function(a,b){u(a._e?a._e[b]:[]).each(function(c){a.removeEventListener(b,c)})})},u.prototype.on=function(a,b,c){if("string"==typeof b){var d=b;b=function(a){var b=arguments;u(a.currentTarget).find(d).each(function(d){if(d===a.target||d.contains(a.target)){try{Object.defineProperty(a,"currentTarget",{get:function(){return d}})}catch(e){}c.apply(d,b)}})}}var e=function(a){return b.apply(this,[a].concat(a.detail||[]))};return this.eacharg(a,function(a,b){a.addEventListener(b,e),a._e=a._e||{},a._e[b]=a._e[b]||[],a._e[b].push(e)})},u.prototype.pairs=function(a,b,c,d){if("undefined"!=typeof b){var e=a;a={},a[e]=b}return"object"==typeof a?this.each(function(b){for(var c in a)d(b,c,a[c])}):this.length?c(this.first(),a):""},u.prototype.param=function(a){return Object.keys(a).map(function(b){return this.uri(b)+"="+this.uri(a[b])}.bind(this)).join("&")},u.prototype.parent=function(a){return this.map(function(a){return a.parentNode}).filter(a)},u.prototype.prepend=function(a,b){return this.adjacent(a,b,function(a,b){a.insertBefore(b,a.firstChild)})},u.prototype.remove=function(){return this.each(function(a){a.parentNode&&a.parentNode.removeChild(a)})},u.prototype.removeClass=function(){return this.eacharg(arguments,function(a,b){a.classList.remove(b)})},u.prototype.replace=function(a,b){var c=[];return this.adjacent(a,b,function(a,b){c=c.concat(this.slice(b.children)),a.parentNode.replaceChild(b,a)}),u(c)},u.prototype.scroll=function(){return this.first().scrollIntoView({behavior:"smooth"}),this},u.prototype.select=function(a,b){if(a=a.replace(/^\s*/,"").replace(/\s*$/,""),b)return this.select.byCss(a,b);for(var c in this.selectors)if(b=c.split("/"),new RegExp(b[1],b[2]).test(a))return this.selectors[c](a);return this.select.byCss(a)},u.prototype.select.byCss=function(a,b){return(b||document).querySelectorAll(a)},u.prototype.selectors={},u.prototype.selectors[/^\.[\w\-]+$/]=function(a){return document.getElementsByClassName(a.substring(1))},u.prototype.selectors[/^\w+$/]=function(a){return document.getElementsByTagName(a)},u.prototype.selectors[/^\#[\w\-]+$/]=function(a){return document.getElementById(a.substring(1))},u.prototype.selectors[/^</]=function(a){return u().generate(a)},u.prototype.serialize=function(){var a=this;return this.slice(this.first().elements).reduce(function(b,c){return!c.name||c.disabled||"file"===c.type?b:/(checkbox|radio)/.test(c.type)&&!c.checked?b:"select-multiple"===c.type?(u(c.options).each(function(d){d.selected&&(b+="&"+a.uri(c.name)+"="+a.uri(d.value))}),b):b+"&"+a.uri(c.name)+"="+a.uri(c.value)},"").slice(1)},u.prototype.siblings=function(a){return this.parent().children(a).not(this)},u.prototype.size=function(){return this.first().getBoundingClientRect()},u.prototype.slice=function(a){return a&&0!==a.length&&"string"!=typeof a&&"[object Function]"!==a.toString()?a.length?[].slice.call(a.nodes||a):[a]:[]},u.prototype.str=function(a,b){return function(c){return"function"==typeof c?c.call(this,a,b):c.toString()}},u.prototype.text=function(a){return void 0===a?this.first().textContent||"":this.each(function(b){b.textContent=a})},u.prototype.toggleClass=function(a,b){return!!b===b?this[b?"addClass":"removeClass"](a):this.eacharg(a,function(a,b){a.classList.toggle(b)})},u.prototype.trigger=function(a){var b=this.slice(arguments).slice(1);return this.eacharg(a,function(a,c){var d,e={bubbles:!0,cancelable:!0,detail:b};try{d=new window.CustomEvent(c,e)}catch(f){d=document.createEvent("CustomEvent"),d.initCustomEvent(c,!0,!0,b)}a.dispatchEvent(d)})},u.prototype.unique=function(){return u(this.nodes.reduce(function(a,b){var c=null!==b&&void 0!==b&&b!==!1;return c&&a.indexOf(b)===-1?a.concat(b):a},[]))},u.prototype.uri=function(a){return encodeURIComponent(a).replace(/!/g,"%21").replace(/'/g,"%27").replace(/\(/g,"%28").replace(/\)/g,"%29").replace(/\*/g,"%2A").replace(/%20/g,"+")},u.prototype.wrap=function(a){function b(a){for(;a.firstElementChild;)a=a.firstElementChild;return u(a)}return this.map(function(c){return u(a).each(function(a){b(a).append(c.cloneNode(!0)),c.parentNode.replaceChild(a,c)})})},"object"==typeof module&&module.exports&&(module.exports={u:u,ajax:ajax}); | ||
/* Umbrella JS 3.0.0 umbrellajs.com */ | ||
var u=function(a,b){return this instanceof u?a instanceof u?a:("string"==typeof a&&(a=this.select(a,b)),a&&a.nodeName&&(a=[a]),void(this.nodes=this.slice(a))):new u(a,b)};u.prototype={get length(){return this.nodes.length}},u.prototype.nodes=[],u.prototype.addClass=function(){return this.eacharg(arguments,function(a,b){a.classList.add(b)})},u.prototype.adjacent=function(a,b,c){return"number"==typeof b&&(b=0===b?[]:new Array(b).join().split(",").map(Number.call,Number)),this.each(function(d,e){var f=document.createDocumentFragment();u(b||{}).map(function(b,c){var f="function"==typeof a?a.call(this,b,c,d,e):a;return"string"==typeof f?this.generate(f):u(f)}).each(function(a){this.isInPage(a)?f.appendChild(u(a).clone().first()):f.appendChild(a)}),c.call(this,d,f)})},u.prototype.after=function(a,b){return this.adjacent(a,b,function(a,b){a.parentNode.insertBefore(b,a.nextSibling)})},u.prototype.append=function(a,b){return this.adjacent(a,b,function(a,b){a.appendChild(b)})},u.prototype.args=function(a,b,c){return"function"==typeof a&&(a=a(b,c)),"string"!=typeof a&&(a=this.slice(a).map(this.str(b,c))),a.toString().split(/[\s,]+/).filter(function(a){return a.length})},u.prototype.array=function(a){a=a;var b=this;return this.nodes.reduce(function(c,d,e){var f;return a?(f=a.call(b,d,e),f||(f=!1),"string"==typeof f&&(f=u(f)),f instanceof u&&(f=f.nodes)):f=d.innerHTML,c.concat(f!==!1?f:[])},[])},u.prototype.attr=function(a,b,c){return c=c?"data-":"",this.pairs(a,b,function(a,b){return a.getAttribute(c+b)},function(a,b,d){a.setAttribute(c+b,d)})},u.prototype.before=function(a,b){return this.adjacent(a,b,function(a,b){a.parentNode.insertBefore(b,a)})},u.prototype.children=function(a){return this.map(function(a){return this.slice(a.children)}).filter(a)},u.prototype.clone=function(){return this.map(function(a,b){var c=a.cloneNode(!0),d=this.getAll(c);return this.getAll(a).each(function(a,b){for(var c in this.mirror)this.mirror[c](a,d.nodes[b])}),c})},u.prototype.getAll=function(a){return u([a].concat(u("*",a).nodes))},u.prototype.mirror={},u.prototype.mirror.events=function(a,b){if(a._e)for(var c in a._e)a._e[c].forEach(function(a){u(b).on(c,a)})},u.prototype.mirror.select=function(a,b){u(a).is("select")&&(b.value=a.value)},u.prototype.mirror.textarea=function(a,b){u(a).is("textarea")&&(b.value=a.value)},u.prototype.closest=function(a){return this.map(function(b){do if(u(b).is(a))return b;while((b=b.parentNode)&&b!==document)})},u.prototype.data=function(a,b){return this.attr(a,b,!0)},u.prototype.each=function(a){return this.nodes.forEach(a.bind(this)),this},u.prototype.eacharg=function(a,b){return this.each(function(c,d){this.args(a,c,d).forEach(function(a){b.call(this,c,a)},this)})},u.prototype.empty=function(){return this.each(function(a){for(;a.firstChild;)a.removeChild(a.firstChild)})},u.prototype.filter=function(a){var b=function(b){return b.matches=b.matches||b.msMatchesSelector||b.webkitMatchesSelector,b.matches(a||"*")};return"function"==typeof a&&(b=a),a instanceof u&&(b=function(b){return a.nodes.indexOf(b)!==-1}),u(this.nodes.filter(b))},u.prototype.find=function(a){return this.map(function(b){return u(a||"*",b)})},u.prototype.first=function(){return this.nodes[0]||!1},u.prototype.generate=function(a){return/^\s*<tr[> ]/.test(a)?u(document.createElement("table")).html(a).children().children().nodes:/^\s*<t(h|d)[> ]/.test(a)?u(document.createElement("table")).html(a).children().children().children().nodes:/^\s*</.test(a)?u(document.createElement("div")).html(a).children().nodes:document.createTextNode(a)},u.prototype.handle=function(){var a=this.slice(arguments).map(function(a){return"function"==typeof a?function(b){b.preventDefault(),a.apply(this,arguments)}:a},this);return this.on.apply(this,a)},u.prototype.hasClass=function(){return this.is("."+this.args(arguments).join("."))},u.prototype.html=function(a){return void 0===a?this.first().innerHTML||"":this.each(function(b){b.innerHTML=a})},u.prototype.is=function(a){return this.filter(a).length>0},u.prototype.isInPage=function(a){return a!==document.body&&document.body.contains(a)},u.prototype.last=function(){return this.nodes[this.length-1]||!1},u.prototype.map=function(a){return a?u(this.array(a)).unique():this},u.prototype.not=function(a){return this.filter(function(b){return!u(b).is(a||!0)})},u.prototype.off=function(a){return this.eacharg(a,function(a,b){u(a._e?a._e[b]:[]).each(function(c){a.removeEventListener(b,c)})})},u.prototype.on=function(a,b,c){if("string"==typeof b){var d=b;b=function(a){var b=arguments;u(a.currentTarget).find(d).each(function(d){if(d===a.target||d.contains(a.target)){try{Object.defineProperty(a,"currentTarget",{get:function(){return d}})}catch(e){}c.apply(d,b)}})}}var e=function(a){return b.apply(this,[a].concat(a.detail||[]))};return this.eacharg(a,function(a,b){a.addEventListener(b,e),a._e=a._e||{},a._e[b]=a._e[b]||[],a._e[b].push(e)})},u.prototype.pairs=function(a,b,c,d){if("undefined"!=typeof b){var e=a;a={},a[e]=b}return"object"==typeof a?this.each(function(b){for(var c in a)d(b,c,a[c])}):this.length?c(this.first(),a):""},u.prototype.param=function(a){return Object.keys(a).map(function(b){return this.uri(b)+"="+this.uri(a[b])}.bind(this)).join("&")},u.prototype.parent=function(a){return this.map(function(a){return a.parentNode}).filter(a)},u.prototype.prepend=function(a,b){return this.adjacent(a,b,function(a,b){a.insertBefore(b,a.firstChild)})},u.prototype.remove=function(){return this.each(function(a){a.parentNode&&a.parentNode.removeChild(a)})},u.prototype.removeClass=function(){return this.eacharg(arguments,function(a,b){a.classList.remove(b)})},u.prototype.replace=function(a,b){var c=[];return this.adjacent(a,b,function(a,b){c=c.concat(this.slice(b.children)),a.parentNode.replaceChild(b,a)}),u(c)},u.prototype.scroll=function(){return this.first().scrollIntoView({behavior:"smooth"}),this},u.prototype.select=function(a,b){return a=a.replace(/^\s*/,"").replace(/\s*$/,""),/^</.test(a)?u().generate(a):(b||document).querySelectorAll(a)},u.prototype.serialize=function(){var a=this;return this.slice(this.first().elements).reduce(function(b,c){return!c.name||c.disabled||"file"===c.type?b:/(checkbox|radio)/.test(c.type)&&!c.checked?b:"select-multiple"===c.type?(u(c.options).each(function(d){d.selected&&(b+="&"+a.uri(c.name)+"="+a.uri(d.value))}),b):b+"&"+a.uri(c.name)+"="+a.uri(c.value)},"").slice(1)},u.prototype.siblings=function(a){return this.parent().children(a).not(this)},u.prototype.size=function(){return this.first().getBoundingClientRect()},u.prototype.slice=function(a){return a&&0!==a.length&&"string"!=typeof a&&"[object Function]"!==a.toString()?a.length?[].slice.call(a.nodes||a):[a]:[]},u.prototype.str=function(a,b){return function(c){return"function"==typeof c?c.call(this,a,b):c.toString()}},u.prototype.text=function(a){return void 0===a?this.first().textContent||"":this.each(function(b){b.textContent=a})},u.prototype.toggleClass=function(a,b){return!!b===b?this[b?"addClass":"removeClass"](a):this.eacharg(a,function(a,b){a.classList.toggle(b)})},u.prototype.trigger=function(a){var b=this.slice(arguments).slice(1);return this.eacharg(a,function(a,c){var d,e={bubbles:!0,cancelable:!0,detail:b};try{d=new window.CustomEvent(c,e)}catch(f){d=document.createEvent("CustomEvent"),d.initCustomEvent(c,!0,!0,b)}a.dispatchEvent(d)})},u.prototype.unique=function(){return u(this.nodes.reduce(function(a,b){var c=null!==b&&void 0!==b&&b!==!1;return c&&a.indexOf(b)===-1?a.concat(b):a},[]))},u.prototype.uri=function(a){return encodeURIComponent(a).replace(/!/g,"%21").replace(/'/g,"%27").replace(/\(/g,"%28").replace(/\)/g,"%29").replace(/\*/g,"%2A").replace(/%20/g,"+")},u.prototype.wrap=function(a){function b(a){for(;a.firstElementChild;)a=a.firstElementChild;return u(a)}return this.map(function(c){return u(a).each(function(a){b(a).append(c.cloneNode(!0)),c.parentNode.replaceChild(a,c)})})},"object"==typeof module&&module.exports&&(module.exports={u:u}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
142
1335841
177
28409
6