Merge pull request #3484 from vikas5914/master

add Jcalculator
This commit is contained in:
Peter Dave Hello
2014-06-29 03:24:15 +08:00
5 changed files with 182 additions and 0 deletions
@@ -0,0 +1,38 @@
.jcalculator_wrap {
position: relative;
}
.jcalculator {
background: #232323;
width: 200px;
display: none;
position: absolute;
top: 82px;
right: 0;
font-family: Helvetica, Arial, Verdana, sans-serif;
border-top: 1px solid #000;
border-left: 1px solid #000;
}
.jcalculator span {
cursor: pointer;
color: #fff;
float: left;
display: block;
width: 50px;
height: 50px;
line-height: 50px;
font-size: 12px;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
border-right: 1px solid #000;
border-bottom: 1px solid #000;
}
.jcalculator span:active {
line-height: 52px;
}
.jcalculator .operation {
background: #191919;
}
.jcalculator .operation.equal {
background: #eb0934;
}
@@ -0,0 +1,129 @@
var template =
'<div class="jcalculator">' +
'<span>7</span>' +
'<span>8</span>' +
'<span>9</span>' +
'<span class="plus operation">+</span>' +
'<span>4</span>' +
'<span>5</span>' +
'<span>6</span>' +
'<span class="minus operation">-</span>' +
'<span>1</span>' +
'<span>2</span>' +
'<span>3</span>' +
'<span class="multiplication operation">x</span>' +
'<span class="clear operation">C</span>' +
'<span>0</span>' +
'<span class="equal operation">=</span>' +
'<span class="divide operation">&divide;</span>' +
'</div>';
(function($) {
$.fn.calculator = function(theme) {
function Controller(el) {
var self = this;
el.wrap("<div class='jcalculator_wrap'></div>");
el.after(template);
this.display = el;
this.element = el.next();
if (theme) {
this.element.addClass(theme);
}
this.value = this.load();
this.stack = null;
this.stackOp = null;
this.clearStack = true;
$("span", this.element).on('click', function() {
var code = $(this).text().trim();
if (isNaN(code)) {
if (code == "C") {
self.digit;
} else if (code.charCodeAt(0) == 247) {
self.op = "/";
} else {
self.op = code;
}
} else {
self.digit = code;
}
});
}
Controller.prototype = {
load: function() {
return this.display.val() || this.display.text();
},
save: function() {
if (this.display.is("input")) this.display.val(this.value);
else this.display.text(this.value);
},
get v() {
return this.value;
},
set v(val) {
this.clearStack = false;
this.value = val;
this.save();
},
get op() {
return this.stackOp;
},
set op(value) {
switch (this.stackOp) {
case "+":
this.v = this.stack + this.v;
break;
case "-":
this.v = this.stack - this.v;
break;
case "x":
this.v = this.stack * this.v;
break;
case "/":
this.v = this.stack / this.v;
break;
}
this.stack = this.v;
this.stackOp = value;
this.clearStack = true;
},
set digit(d) {
d = parseInt(d);
if (this.clearStack) return this.v = d;
return this.v = this.v * 10 + d;
},
get digit() {
if (this.clearStack) return this.v = 0;
return this.v = Math.floor(this.v / 10);
}
};
var controller;
this.each(function() {
controller = new Controller($(this));
$(this).on('focus', function() {
$(".jcalculator").show();
});
$("body").click(function() {
$(".jcalculator").hide();
});
$(".jcalculator_wrap").click(function(event) {
event.stopPropagation();
});
$(document).keyup(function(e) {
if (e.keyCode == 27) {
$(".jcalculator").hide();
}
});
});
return controller;
};
})(jQuery);
+1
View File
@@ -0,0 +1 @@
.jcalculator_wrap{position:relative}.jcalculator{background:#232323;width:200px;display:none;position:absolute;top:82px;right:0;font-family:Helvetica,Arial,Verdana,sans-serif;border-top:1px solid #000;border-left:1px solid #000}.jcalculator span{cursor:pointer;color:#fff;float:left;display:block;width:50px;height:50px;line-height:50px;font-size:12px;box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;border-right:1px solid #000;border-bottom:1px solid #000}.jcalculator span:active{line-height:52px}.jcalculator .operation{background:#191919}.jcalculator .operation.equal{background:#eb0934}
+1
View File
@@ -0,0 +1 @@
var template='<div class="jcalculator"><span>7</span><span>8</span><span>9</span><span class="plus operation">+</span><span>4</span><span>5</span><span>6</span><span class="minus operation">-</span><span>1</span><span>2</span><span>3</span><span class="multiplication operation">x</span><span class="clear operation">C</span><span>0</span><span class="equal operation">=</span><span class="divide operation">&divide;</span></div>';!function(t){t.fn.calculator=function(s){function a(a){var i=this;a.wrap("<div class='jcalculator_wrap'></div>"),a.after(template),this.display=a,this.element=a.next(),s&&this.element.addClass(s),this.value=this.load(),this.stack=null,this.stackOp=null,this.clearStack=!0,t("span",this.element).on("click",function(){var s=t(this).text().trim();isNaN(s)?"C"==s?i.digit:i.op=247==s.charCodeAt(0)?"/":s:i.digit=s})}a.prototype={load:function(){return this.display.val()||this.display.text()},save:function(){this.display.is("input")?this.display.val(this.value):this.display.text(this.value)},get v(){return this.value},set v(t){this.clearStack=!1,this.value=t,this.save()},get op(){return this.stackOp},set op(t){switch(this.stackOp){case"+":this.v=this.stack+this.v;break;case"-":this.v=this.stack-this.v;break;case"x":this.v=this.stack*this.v;break;case"/":this.v=this.stack/this.v}this.stack=this.v,this.stackOp=t,this.clearStack=!0},set digit(t){return t=parseInt(t),this.v=this.clearStack?t:10*this.v+t},get digit(){return this.v=this.clearStack?0:Math.floor(this.v/10)}};var i;return this.each(function(){i=new a(t(this)),t(this).on("focus",function(){t(".jcalculator").show()}),t("body").click(function(){t(".jcalculator").hide()}),t(".jcalculator_wrap").click(function(t){t.stopPropagation()}),t(document).keyup(function(s){27==s.keyCode&&t(".jcalculator").hide()})}),i}}(jQuery);
+13
View File
@@ -0,0 +1,13 @@
{
"name": "jcalculator",
"filename": "jcalculator.min.js",
"version": "1403955268",
"description": "Awesome jQuery plugin for calculator inputs",
"homepage": "http://mariusbalaj.com/dev/jcalculator/",
"keywords": [
"calculator",
"jquery",
"input"
],
"author": "balajmarius"
}