Better argument handling

- Throw error in case of no input
- call querySelectorAll in case of string
- convert to array in case of single object
This commit is contained in:
Mahdi Dibaiee 2015-09-25 19:36:25 +03:30
parent 34560b85e8
commit a4c2cd0b70

View File

@ -3,13 +3,27 @@
var context = canvas.getContext('2d'); var context = canvas.getContext('2d');
var Snaphash = function(els) { var Snaphash = function(els) {
if (typeof els === 'string') els = document.querySelectorAll(els); if (!els) {
this.els = els.length ? els : [els]; throw Error('You must provide an element/selector');
}
// selector provided
if (typeof els === 'string') {
els = document.querySelectorAll(els);
}
// single element provided
if (typeof els.length === 'undefined') {
els = [els];
}
// loop over elements
for (var i = 0, len = els.length; i < len; i++) { for (var i = 0, len = els.length; i < len; i++) {
var el = els[i]; var el = els[i];
// Let's find out target selector and time
var src = parseSource(el.dataset.snap); var src = parseSource(el.dataset.snap);
// Target video, clone it to avoid flickering
var target = document.querySelector(src.selector).cloneNode(); var target = document.querySelector(src.selector).cloneNode();
function listener() { function listener() {