blob: aa96a225b9d244adb07106f2d370bc01cb115293 [file] [edit]
/**
* common.js is a set of common functions used across all of skiaperf.
*
* Everything is scoped to 'sk' except $$ and $$$ which are global since they
* are used so often.
*
*/
/**
* $$ returns a real JS array of DOM elements that match the CSS query selector.
*
* A shortcut for jQuery-like $ behavior.
**/
function $$(query, ele) {
if (!ele) {
ele = document;
}
return Array.prototype.map.call(ele.querySelectorAll(query), function(e) { return e; });
}
/**
* $$$ returns the DOM element that match the CSS query selector.
*
* A shortcut for document.querySelector.
**/
function $$$(query, ele) {
if (!ele) {
ele = document;
}
return ele.querySelector(query);
}
this.sk = this.sk || function() {
"use strict";
var sk = {};
/**
* clearChildren removes all children of the passed in node.
*/
sk.clearChildren = function(ele) {
while (ele.firstChild) {
ele.removeChild(ele.firstChild);
}
}
/**
* Importer simplifies importing HTML Templates from HTML Imports.
*
* Just instantiate an instance in the HTML Import:
*
* importer = new sk.Importer();
*
* Then import templates via their id:
*
* var node = importer.import('#foo');
*/
sk.Importer = function() {
if ('currentScript' in document) {
this.importDoc_ = document.currentScript.ownerDocument;
} else {
this.importDoc_ = document._currentScript.ownerDocument;
}
}
sk.Importer.prototype.import = function(id) {
return document.importNode($$$(id, this.importDoc_).content, true);
}
// Returns a Promise that uses XMLHttpRequest to make a request to the given URL.
sk.get = function(url) {
// Return a new promise.
return new Promise(function(resolve, reject) {
// Do the usual XHR stuff
var req = new XMLHttpRequest();
req.open('GET', url);
req.onload = function() {
// This is called even on 404 etc
// so check the status
if (req.status == 200) {
// Resolve the promise with the response text
resolve(req.response);
} else {
// Otherwise reject with the status text
// which will hopefully be a meaningful error
reject(req.response);
}
};
// Handle network errors
req.onerror = function() {
reject(Error("Network Error"));
};
// Make the request
req.send();
});
}
// Returns a Promise that uses XMLHttpRequest to make a POST request to the
// given URL with the given JSON body.
sk.post = function(url, body) {
// Return a new promise.
return new Promise(function(resolve, reject) {
// Do the usual XHR stuff
var req = new XMLHttpRequest();
req.open('POST', url);
req.setRequestHeader("Content-Type", "application/json");
document.body.style.cursor = 'wait';
req.onload = function() {
// This is called even on 404 etc
// so check the status
document.body.style.cursor = 'auto';
if (req.status == 200) {
// Resolve the promise with the response text
resolve(req.response);
} else {
// Otherwise reject with the status text
// which will hopefully be a meaningful error
reject(req.response);
}
};
// Handle network errors
req.onerror = function() {
document.body.style.cursor = 'auto';
reject(Error("Network Error"));
};
// Make the request
req.send(body);
});
}
// A Promise the resolves when DOMContentLoaded has fired.
sk.DomReady = new Promise(function(resolve, reject) {
if (document.readyState != 'loading') {
// If readyState is already past loading then
// DOMContentLoaded has already fired, so just resolve.
resolve();
} else {
document.addEventListener('DOMContentLoaded', resolve);
}
});
// A Promise that resolves when Polymer has fired polymer-ready.
sk.WebComponentsReady = new Promise(function(resolve, reject) {
window.addEventListener('polymer-ready', resolve);
});
sk.array = {};
/**
* Returns true if the two arrays are equal.
*
* Notes:
* Presumes the arrays are already in the same order.
* Compares equality using ===.
*/
sk.array.equal = function(a, b) {
if (a.length != b.length) {
return false;
}
for (var i = 0, len = a.length; i < len; i++) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
}
return sk;
}();