Mercurial > repos > tabletprog
view mquery.js @ 101:5d15b91e738a
Fix paramerized types. Remove debug print calls.
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Thu, 09 Aug 2012 08:09:14 -0700 |
parents | 668f533e5284 |
children | 76a21129e8f5 |
line wrap: on
line source
function each(container, fun) { if (container instanceof Array) { 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 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; }