dojo.provide("dojoc.demos.faces.src"); dojo.require("dojox.fx.flip"); dojo.require("demos.faces.src.block"); dojo.require("dijit._base.sniff"); dojo.require("dojox.image._base"); dojo.require("dojox.image.LightboxNano"); dojo.require("dojox.analytics.Urchin"); (function(d){ // to reuse d.NodeList.prototype.makeNano = function(){ return this.forEach(function(n){ new dojox.image.LightboxNano({ href: d.attr(n, "href") }, n); }); } var v = "flip", x = 0; var base64Img = ""; var sortDivsByLengthOfFirstChildUl = function(id){ // don't ever let me see you doing this outside of a demo situation. there has // got to be a better way. id = d.byId(id); d.query("> div", id).sort(function(a,b){ var q = "ul > li", al = d.query(q, a).length, bl = d.query(q, b).length; return al > bl ? 0 : al < bl ? 1 : -1; }).forEach(function(n){ id.appendChild(n); }); } var nameset = function(){ // quick, generate a list of names from top to bottom var byid = d.byId; return "" + byid("hair").parentNode.className + ", " + byid("eyes").parentNode.className + ", and " + byid("mouth").parentNode.className + " make: " ; }; var demo = function(){ // each region has an id, and this order is convenient. keep it: var pieceId = ["hair", "eyes", "mouth"], // cache a ref to this node, and always make it 'off' by default checknode = d.byId("random"); checknode.checked = false; var nextImage = function(n){ // FIXME: there is no unity to this. should keep an index // of each region individually. This is more "random" though. if(++x > people.length - 1 ){ x = 0; } if(people[x] == "webcam"){ // if we loaded webcam.js, this applies n.parentNode.className = ""; var image = new Image(); image.src = base64Img; d.style(n, "backgroundImage", "url(" + image.src + ")"); }else{ // otherwise, just set the image d.style(n, "backgroundImage", ""); n.parentNode.className = people[x]; } }; var flip = function(e){ // flip one of the pieceId nodes: var n = e.target, // create the flip animation: fl = dojox.fx[v]({ node: n }), // and determine which image to show after flip is over: c = d.connect(fl, "onEnd", function(){ nextImage(n); d.disconnect(c); }) ; fl.play(); } // random logic, could move out into module: var randomInterval; var stopRandom = function(){ checknode.checked = false; clearTimeout(randomInterval); randomInterval = null; } var randomFunc = function(){ // pseudo-emulate (duck) the event object randomly: flip({ target: d.byId(pieceId[(Math.floor(Math.random() * 3))]) }); } var startRandom = function(){ randomInterval = setInterval(randomFunc, 4500); randomFunc(); } d.query("#flipper").onclick(function(e){ if(randomInterval){ stopRandom(); } flip(e); }); d.connect(checknode, "onchange", function(e){ // check if we should be looping or stop it: if(!e.target.checked){ stopRandom(); }else{ startRandom(); } }); // end random logic // click handling for "hall of shame" link: var blocker; d.query("#saveAs").onclick(function(e){ var target = d.query("#faceContainer .container")[0]; blocker = d.block(target, { opacity:0.6, keepPosition:true, backgroundColor:"#000" }); // onclick, create a data object from the selected faces and send to backend var data = {}; d.forEach(pieceId, function(piece){ data[piece] = d.byId(piece).parentNode.className || ""; }); setTimeout(d.partial(saveFace, data), 50); stopRandom(); }); var saveFace = function(pay){ // a function to send the list of currently selected users to the backend var url; d.xhrPost({ url: "resources/imageMaker.php", handleAs: "json", handle: function(response){ // try{ url = response["file"]; if(response["name"]){ d.byId("savedName").innerHTML = "
" + nameset() + "
" + "