Mercurial > repos > tabletprog
view mquery.js @ 182:ab7c142090a0
Make method names available at runtime so they can be included in method not implemented error messages
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 24 Aug 2013 19:02:59 -0700 |
parents | c0bfff39abe3 |
children |
line wrap: on
line source
function each(container, fun) { if (container instanceof Array || container instanceof HTMLCollection || container instanceof NodeList) { for (var i = 0; i < container.length; i++) { fun(i, container[i]); } } else { for (var i in container) { if (container.hasOwnProperty(i)) { fun(i, container[i]); } } } } function q(query, el) { return el === undefined ? document.querySelector(query) : el.querySelector(query); } function qall(query, el) { return el === undefined ? document.querySelectorAll(query) : el.querySelectorAll(query); } function hide(el) { el.style.display = 'none'; } function show(el, dtype) { if (dtype === undefined) { dtype = 'block'; } el.style.display = dtype; } function onReady(fun) { if (document.readyState == 'complete') { fun(); } else { document.onreadystatechange = function() { if (document.readyState == 'complete') { fun(); document.onreadystatechange = null; } } } } function addClass(el, classname) { var classes = el.className.split(' '); if (classes.indexOf(classname) == -1) { classes.push(classname); el.className = classes.join(' '); } } function removeClass(el, classname) { var classes = el.className.split(' '); var idx = classes.indexOf(classname); if (idx > -1) { classes.splice(idx, 1); el.className = classes.join(' '); } } function hasClass(el, classname) { return el.className == classname || el.className.split(' ').indexOf(classname) > -1 } function ajax(method, url, data, onSuccess, onFail, onOthers) { var req; try { req = new XMLHttpRequest(); } catch (e) { req = new ActiveXObject("Microsoft.XMLHTTP"); } req.onreadystatechange = function() { if (req.readyState == 4) { if ((req.status >= 200 && req.status <= 299) || req.status == 0) { onSuccess(req); } else if(onFail) { onFail(req); } else { console.log('request failed:', req); } } else if(onOthers) { onOthers(req); } } req.open(method, url); if (data && 'mime' in data) { req.setRequestHeader('Content-Type', data.mime); req.send(data); } else { req.send(data); } } function get(url, onSuccess, onFail, onOthers) { ajax('GET', url, undefined, onSuccess, onFail, onOthers); } function post(url, data, onSuccess, onFail, onOthers) { ajax('POST', url, data, onSuccess, onFail, onOthers); } function newEl(tagname, props) { var el = document.createElement(tagname); if (typeof props == 'object') { each(props, function (key, val) { el[key] = val; }); } return el; } function setText(parent, text) { parent.innerHTML = ''; parent.appendChild(document.createTextNode(text)); return parent; } function bubble(el, event, handler) { el.addEventListener(event, handler, false); } function capture(el, event, handler) { el.addEventListener(event, handler, true); }