Skip to content

Instantly share code, notes, and snippets.

@juliangruber
Created August 18, 2013 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save juliangruber/6264704 to your computer and use it in GitHub Desktop.
Save juliangruber/6264704 to your computer and use it in GitHub Desktop.
requirebin sketch
var Rating = require('rating');
var insertCSS = require('insert-css');
insertCSS('.star { color: gray } .star.glow { color: brown }');
var rating = new Rating([1, 2, 3, 4, 5]);
document.body.appendChild(rating.el);
var current = document.createElement('p');
document.body.appendChild(current);
var rate = document.createElement('p');
document.body.appendChild(rate);
rating.on('rate', function(weight) {
rate.innerHTML = 'rated: ' + weight;
});
rating.on('select', function(weight) {
current.innerHTML = 'current: ' + weight;
});
require=function(e,t,n){function r(n,o){if(!t[n]){if(!e[n]){var s="function"==typeof require&&require;if(!o&&s)return s(n,!0);if(i)return i(n,!0);throw Error("Cannot find module '"+n+"'")}var a=t[n]={exports:{}};e[n][0].call(a.exports,function(t){var i=e[n][1][t];return r(i?i:t)},a,a.exports)}return t[n].exports}for(var i="function"==typeof require&&require,o=0;n.length>o;o++)r(n[o]);return r}({1:[function(e,t){var n=t.exports={};n.nextTick=function(){var e="undefined"!=typeof window&&window.setImmediate,t="undefined"!=typeof window&&window.postMessage&&window.addEventListener;if(e)return function(e){return window.setImmediate(e)};if(t){var n=[];return window.addEventListener("message",function(e){if(e.source===window&&"process-tick"===e.data&&(e.stopPropagation(),n.length>0)){var t=n.shift();t()}},!0),function(e){n.push(e),window.postMessage("process-tick","*")}}return function(e){setTimeout(e,0)}}(),n.title="browser",n.browser=!0,n.env={},n.argv=[],n.binding=function(){throw Error("process.binding is not supported")},n.cwd=function(){return"/"},n.chdir=function(){throw Error("process.chdir is not supported")}},{}],2:[function(e,t,n){(function(e){function t(e,t){if(e.indexOf)return e.indexOf(t);for(var n=0;e.length>n;n++)if(t===e[n])return n;return-1}e.EventEmitter||(e.EventEmitter=function(){});var r=n.EventEmitter=e.EventEmitter,i="function"==typeof Array.isArray?Array.isArray:function(e){return"[object Array]"===Object.prototype.toString.call(e)},o=10;r.prototype.setMaxListeners=function(e){this._events||(this._events={}),this._events.maxListeners=e},r.prototype.emit=function(e){if("error"===e&&(!this._events||!this._events.error||i(this._events.error)&&!this._events.error.length))throw arguments[1]instanceof Error?arguments[1]:Error("Uncaught, unspecified 'error' event.");if(!this._events)return!1;var t=this._events[e];if(!t)return!1;if("function"==typeof t){switch(arguments.length){case 1:t.call(this);break;case 2:t.call(this,arguments[1]);break;case 3:t.call(this,arguments[1],arguments[2]);break;default:var n=Array.prototype.slice.call(arguments,1);t.apply(this,n)}return!0}if(i(t)){for(var n=Array.prototype.slice.call(arguments,1),r=t.slice(),o=0,s=r.length;s>o;o++)r[o].apply(this,n);return!0}return!1},r.prototype.addListener=function(e,t){if("function"!=typeof t)throw Error("addListener only takes instances of Function");if(this._events||(this._events={}),this.emit("newListener",e,t),this._events[e])if(i(this._events[e])){if(!this._events[e].warned){var n;n=void 0!==this._events.maxListeners?this._events.maxListeners:o,n&&n>0&&this._events[e].length>n&&(this._events[e].warned=!0,console.error("(node) warning: possible EventEmitter memory leak detected. %d listeners added. Use emitter.setMaxListeners() to increase limit.",this._events[e].length),console.trace())}this._events[e].push(t)}else this._events[e]=[this._events[e],t];else this._events[e]=t;return this},r.prototype.on=r.prototype.addListener,r.prototype.once=function(e,t){var n=this;return n.on(e,function r(){n.removeListener(e,r),t.apply(this,arguments)}),this},r.prototype.removeListener=function(e,n){if("function"!=typeof n)throw Error("removeListener only takes instances of Function");if(!this._events||!this._events[e])return this;var r=this._events[e];if(i(r)){var o=t(r,n);if(0>o)return this;r.splice(o,1),0==r.length&&delete this._events[e]}else this._events[e]===n&&delete this._events[e];return this},r.prototype.removeAllListeners=function(e){return 0===arguments.length?(this._events={},this):(e&&this._events&&this._events[e]&&(this._events[e]=null),this)},r.prototype.listeners=function(e){return this._events||(this._events={}),this._events[e]||(this._events[e]=[]),i(this._events[e])||(this._events[e]=[this._events[e]]),this._events[e]}})(e("__browserify_process"))},{__browserify_process:1}],3:[function(e,t,n){function r(e){return e instanceof Array||Array.isArray(e)||e&&e!==Object.prototype&&r(e.__proto__)}function i(e){return e instanceof RegExp||"object"==typeof e&&"[object RegExp]"===Object.prototype.toString.call(e)}function o(e){if(e instanceof Date)return!0;if("object"!=typeof e)return!1;var t=Date.prototype&&a(Date.prototype),n=e.__proto__&&a(e.__proto__);return JSON.stringify(n)===JSON.stringify(t)}e("events"),n.isArray=r,n.isDate=function(e){return"[object Date]"===Object.prototype.toString.call(e)},n.isRegExp=function(e){return"[object RegExp]"===Object.prototype.toString.call(e)},n.print=function(){},n.puts=function(){},n.debug=function(){},n.inspect=function(e,t,u,c){function f(e,u){if(e&&"function"==typeof e.inspect&&e!==n&&(!e.constructor||e.constructor.prototype!==e))return e.inspect(u);switch(typeof e){case"undefined":return l("undefined","undefined");case"string":var c="'"+JSON.stringify(e).replace(/^"|"$/g,"").replace(/'/g,"\\'").replace(/\\"/g,'"')+"'";return l(c,"string");case"number":return l(""+e,"number");case"boolean":return l(""+e,"boolean")}if(null===e)return l("null","null");var h=s(e),d=t?a(e):h;if("function"==typeof e&&0===d.length){if(i(e))return l(""+e,"regexp");var v=e.name?": "+e.name:"";return l("[Function"+v+"]","special")}if(o(e)&&0===d.length)return l(e.toUTCString(),"date");var g,m,y;if(r(e)?(m="Array",y=["[","]"]):(m="Object",y=["{","}"]),"function"==typeof e){var _=e.name?": "+e.name:"";g=i(e)?" "+e:" [Function"+_+"]"}else g="";if(o(e)&&(g=" "+e.toUTCString()),0===d.length)return y[0]+g+y[1];if(0>u)return i(e)?l(""+e,"regexp"):l("[Object]","special");p.push(e);var b=d.map(function(t){var n,i;if(e.__lookupGetter__&&(e.__lookupGetter__(t)?i=e.__lookupSetter__(t)?l("[Getter/Setter]","special"):l("[Getter]","special"):e.__lookupSetter__(t)&&(i=l("[Setter]","special"))),0>h.indexOf(t)&&(n="["+t+"]"),i||(0>p.indexOf(e[t])?(i=null===u?f(e[t]):f(e[t],u-1),i.indexOf("\n")>-1&&(i=r(e)?i.split("\n").map(function(e){return" "+e}).join("\n").substr(2):"\n"+i.split("\n").map(function(e){return" "+e}).join("\n"))):i=l("[Circular]","special")),n===void 0){if("Array"===m&&t.match(/^\d+$/))return i;n=JSON.stringify(""+t),n.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)?(n=n.substr(1,n.length-2),n=l(n,"name")):(n=n.replace(/'/g,"\\'").replace(/\\"/g,'"').replace(/(^"|"$)/g,"'"),n=l(n,"string"))}return n+": "+i});p.pop();var w=0,x=b.reduce(function(e,t){return w++,t.indexOf("\n")>=0&&w++,e+t.length+1},0);return b=x>50?y[0]+(""===g?"":g+"\n ")+" "+b.join(",\n ")+" "+y[1]:y[0]+g+" "+b.join(", ")+" "+y[1]}var p=[],l=function(e,t){var n={bold:[1,22],italic:[3,23],underline:[4,24],inverse:[7,27],white:[37,39],grey:[90,39],black:[30,39],blue:[34,39],cyan:[36,39],green:[32,39],magenta:[35,39],red:[31,39],yellow:[33,39]},r={special:"cyan",number:"blue","boolean":"yellow",undefined:"grey","null":"bold",string:"green",date:"magenta",regexp:"red"}[t];return r?"["+n[r][0]+"m"+e+"["+n[r][1]+"m":e};return c||(l=function(e){return e}),f(e,u===void 0?2:u)},n.log=function(){},n.pump=null;var s=Object.keys||function(e){var t=[];for(var n in e)t.push(n);return t},a=Object.getOwnPropertyNames||function(e){var t=[];for(var n in e)Object.hasOwnProperty.call(e,n)&&t.push(n);return t},u=Object.create||function(e,t){var n;if(null===e)n={__proto__:null};else{if("object"!=typeof e)throw new TypeError("typeof prototype["+typeof e+"] != 'object'");var r=function(){};r.prototype=e,n=new r,n.__proto__=e}return t!==void 0&&Object.defineProperties&&Object.defineProperties(n,t),n};n.inherits=function(e,t){e.super_=t,e.prototype=u(t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}})};var c=/%[sdj%]/g;n.format=function(e){if("string"!=typeof e){for(var t=[],r=0;arguments.length>r;r++)t.push(n.inspect(arguments[r]));return t.join(" ")}for(var r=1,i=arguments,o=i.length,s=(e+"").replace(c,function(e){if("%%"===e)return"%";if(r>=o)return e;switch(e){case"%s":return i[r++]+"";case"%d":return Number(i[r++]);case"%j":return JSON.stringify(i[r++]);default:return e}}),a=i[r];o>r;a=i[++r])s+=null===a||"object"!=typeof a?" "+a:" "+n.inspect(a);return s}},{events:2}],4:[function(e,t){function n(e){r.call(this),this.el=e,this.el.addEventListener("mouseover",this.emit.bind(this,"hover")),this.el.addEventListener("mousedown",this.emit.bind(this,"click"))}var r=e("events").EventEmitter,i=e("util").inherits;t.exports=n,i(n,r),n.prototype.bright=function(){this.el.classList.add("glow")},n.prototype.dark=function(){this.el.classList.remove("glow")}},{events:2,util:3}],rating:[function(e,t){t.exports=e("G+vKN3")},{}],"G+vKN3":[function(e,t){(function(n){function r(e,t){return this instanceof r?(o.call(this),t&&t.container?this.el=t.container:(this.el=document.createElement("span"),this.el.className="rating"),t&&t.star?this.tmpl=t.star:(this.tmpl=document.createElement("span"),this.tmpl.className="star",this.tmpl.innerHTML="★"),this.weights=e,this.stars=[],this.ratingIdx=null,this.build(),void 0):new r(e,t)}function i(e){return function(t){t[e]()}}var o=e("events").EventEmitter,s=e("util").inherits,a=e("./lib/star"),u=e("contains");t.exports=r,s(r,o),r.prototype.build=function(){var e=this;e.weights.forEach(function(t,n){var r=new a(e.tmpl.cloneNode(!0));e.el.appendChild(r.el),e.stars.push(r),r.on("click",function(){e.ratingIdx=n,e.emit("rate",e.weights[e.ratingIdx])}),r.on("hover",function(){e.update(n+1),e.emit("select",e.weights[n])})}),e.el.addEventListener("mouseout",function(t){if(!u(this,t.relatedTarget)){var n=null!==e.ratingIdx?e.ratingIdx+1:0;e.update(n),e.emit("select",e.weights[e.ratingIdx])}}),n.nextTick(function(){e.emit("select",e.weights[e.ratingIdx])})},r.prototype.set=function(e){this.ratingIdx=this.weights.indexOf(e),this.update(this.ratingIdx+1),this.emit("select",e),this.emit("rate",e)},r.prototype.update=function(e){this.stars.slice(0,e).forEach(i("bright")),this.stars.slice(e).forEach(i("dark"))}})(e("__browserify_process"))},{events:2,util:3,"./lib/star":4,contains:5,__browserify_process:1}],5:[function(e,t){function n(e,t){if(e.contains)return e.contains(t);var n=e.compareDocumentPosition(t);return 0===n||n&r}var r=16;t.exports=n},{}]},{},[]),require=function(e,t,n){function r(n,o){if(!t[n]){if(!e[n]){var s="function"==typeof require&&require;if(!o&&s)return s(n,!0);if(i)return i(n,!0);throw Error("Cannot find module '"+n+"'")}var a=t[n]={exports:{}};e[n][0].call(a.exports,function(t){var i=e[n][1][t];return r(i?i:t)},a,a.exports)}return t[n].exports}for(var i="function"==typeof require&&require,o=0;n.length>o;o++)r(n[o]);return r}({"insert-css":[function(e,t){t.exports=e("Hhx+uZ")},{}],"Hhx+uZ":[function(e,t){var n=[];t.exports=function(e){if(!(n.indexOf(e)>=0)){n.push(e);var t=document.createElement("style"),r=document.createTextNode(e);t.appendChild(r),document.head.childNodes.length?document.head.insertBefore(t,document.head.childNodes[0]):document.head.appendChild(t)}}},{}]},{},[]);var Rating=require("rating"),insertCSS=require("insert-css");insertCSS(".star { color: gray } .star.glow { color: brown }");var rating=new Rating([1,2,3,4,5]);document.body.appendChild(rating.el);var current=document.createElement("p");document.body.appendChild(current);var rate=document.createElement("p");document.body.appendChild(rate),rating.on("rate",function(e){rate.innerHTML="rated: "+e}),rating.on("select",function(e){current.innerHTML="current: "+e});
<style type='text/css'>html, body { margin: 0; padding: 0; border: 0; }
body, html { height: 100%; width: 100%; }</style>
@19h
Copy link

19h commented Aug 19, 2013

This is cool. gj

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment