-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumbers.min.js
1 lines (1 loc) · 4.97 KB
/
numbers.min.js
1
!function(t,i,n){"use strict";var o=t.Model=function(t){i.call(this),this.set(t)};o.prototype=Object.create(i.prototype),o.prototype.constructor=o,o.prototype.set=function(t){this.n=t,this.signal("n",t)};var e=t.RegularView=function(t,i){this.model=t,this.container=i,this.model.on("n",this.update.bind(this)),this.update(this.model.n)};e.prototype.update=function(t){this.container.innerText=t},t.multiplication=function(t,i){return[Math.pow(t,i)]},t.power=function(t,i){return[t,"<sup>",i,"</sup>"]};var s=n.extend,h=t.RepresentationView=function(i,n,o){this.options=s(o||{},{base:10,representation:t.power}),this.model=i,this.container=n,this.model.on("n",this.update.bind(this)),this.update(this.model.n)};h.prototype.update=function(t){var i=math.digits(t,this.options.base),n=i.map(function(t,n){var o=i.length-1-n;return[t,o]}).map(function(t){return[t[0],"×"].concat(this.options.representation(this.options.base,t[1])).join("")}.bind(this)).join("+");this.container.innerHTML=n};var r=t.EditableView=function(t,n,o){i.call(this),this.options=s(o||{},{editing:!1}),this.model=t,this.container=n,this.editing=!1,this.on("mode changed",this.visibility.bind(this)),this.model.on("n",this.update.bind(this)),this.visibility(),this.update(this.model.n)};r.prototype=Object.create(i.prototype),r.prototype.constructor=r,r.prototype.toggleEditing=function(){this.setEditing(!this.editing)},r.prototype.startEditing=function(){this.setEditing(!0)},r.prototype.stopEditing=function(){this.setEditing(!1)},r.prototype.setEditing=function(t){this.editing=t,this.signal("mode changed",this.editing)},r.prototype.visibility=function(){this.view().setAttribute("style","display: "+(this.editing?"none":"inline")),this.input().setAttribute("style","display: "+(this.editing?"inline":"none"))},r.prototype.update=function(t){this.view().innerText=t,this.input().value=t},r.prototype.view=function(){if(!this._view){var t=this._view=document.createElement("span");this.container.appendChild(t),t.addEventListener("click",this.startEditing.bind(this))}return this._view},r.prototype.input=function(){if(!this._input){var t=this._input=document.createElement("input");t.setAttribute("size",3),t.setAttribute("class","editable number input"),this.container.appendChild(t);var i=this.stopEditing.bind(this);["blur","change"].forEach(function(n){t.addEventListener(n,i)}),t.addEventListener("change",function(){var i=this.model.n;try{this.model.set(parseInt(t.value))}catch(n){this.model.set(i)}}.bind(this))}return this._input};var a=t.PunchcardView=function(t,i,n){this.options=s(n||{},{base:10,width:640,height:480,columns:5,backgroundColor:"white",gridColor:"black",gridWidth:2,gridHeaderWidth:4,headerFont:"20px monospace",headerDrop:.6,holeColor:"black",holeSize:.75}),this.model=t,this.container=i,this.model.on("n",this.update.bind(this)),this.update(this.model.n)};a.prototype.update=function(t){this.blankCard(t)},a.prototype.blankCard=function(t){this.background(),this.grid(),this.legenda(),this.number(t)},a.prototype.background=function(){var t=this.context();t.save(),t.fillStyle=this.options.backgroundColor,t.fillRect(0,0,this.options.width,this.options.height),t.restore()},a.prototype.grid=function(){var t=this.context();t.save(),t.lineWidth=this.options.gridWidth,t.strokeStyle=this.options.gridColor;var i=this.cellDimensions(),n=i.dx;t.beginPath();for(var o=1;o<=this.options.columns;o++)t.moveTo(o*n,0),t.lineTo(o*n,this.options.height);for(var e=i.dy,s=1;s<=this.options.base;s++)t.moveTo(0,s*e),t.lineTo(this.options.width,s*e);t.stroke(),t.lineWidth*=this.options.gridHeaderWidth,t.beginPath(),t.moveTo(this.options.columns*n,0),t.lineTo(this.options.columns*n,this.options.height),t.moveTo(0,e),t.lineTo(this.options.width,e),t.stroke(),t.restore()},a.prototype.cellDimensions=function(){return{dx:this.options.width/(this.options.columns+1),dy:this.options.height/(this.options.base+1)}},a.prototype.legenda=function(){var t=this.context();t.save(),t.font=this.options.headerFont;for(var i=this.cellDimensions(),n=0;n<this.options.columns;n++){var o=t.measureText(n);t.fillText(n,i.dx*(this.options.columns-1-n+.5)-o.width/2,i.dy*this.options.headerDrop)}for(var e=0;e<this.options.base;e++){var s=t.measureText(e);t.fillText(e,i.dx*(this.options.columns+.5)-s.width/2,i.dy*(e+1+this.options.headerDrop))}t.restore()},a.prototype.number=function(t){var i=this.context(),n=this.cellDimensions(),o=this.options.holeSize*Math.min(n.dx,n.dy)/2;i.save(),i.fillStyle=this.options.holeColor,i.beginPath(),math.digits(t,this.options.base).reverse().forEach(function(t,e){var s=n.dx*(this.options.columns-1-e+.5),h=n.dy*(t+1+.5);i.moveTo(s,h),i.arc(s,h,o,0,2*Math.PI)}.bind(this)),i.fill(),i.restore()},a.prototype.context=function(){return this._context||(this._context=this.canvas().getContext("2d")),this._context},a.prototype.canvas=function(){if(!this._canvas){var t=this._canvas=document.createElement("canvas");t.width=this.options.width,t.height=this.options.height,this.container.appendChild(t)}return this._canvas}}(window.math=window.math||{},ns.Observable,util);