Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

set value method

  • Loading branch information...
commit 09209cc13152d9b213db8ae349b9e275c7b3777d 1 parent 7a808ab
@pascalopitz authored
Showing with 42 additions and 22 deletions.
  1. +8 −1 index.html
  2. +33 −20 percentage.js
  3. +1 −1  percentage.min.js
View
9 index.html
@@ -40,8 +40,12 @@
<p class="percent">70%</p>
+ <p>
+ <input type="button" value="update above with 50%" id="update-p">
+ </p>
+
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
- <script type="text/javascript" src="./percentage.js"></script>
+ <script type="text/javascript" src="./percentage.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('input.percent').percentage();
@@ -61,6 +65,9 @@
$('#remove-span').click(function() {
$('span.percent').percentage('destroy');
});
+ $('#update-p').click(function() {
+ $('p.percent').percentage('set', 50);
+ });
$('p.percent').percentage({
color: 'pink',
View
53 percentage.js
@@ -1,7 +1,7 @@
(function(){
var _self = this;
- function render(conf) {
+ function render(conf, val) {
var elems = this;
if(conf == 'destroy') {
@@ -12,7 +12,7 @@
$(this.percentage).remove();
}
})
- return;
+ return elems;
}
var defaults = {
@@ -35,6 +35,34 @@
}
}
}
+
+ function getLeft(newVal) {
+ return Math.floor((conf.width * -1) + (conf.width / 100 * newVal))
+ }
+
+ function updateElem(elem, val) {
+ if($(elem).is('textarea,input,select')) {
+ $(elem)
+ .attr('value', val)
+ .val(val)
+ .attr('title', val + '%')
+ ;
+ } else {
+ $(elem)
+ .text(val + '%')
+ ;
+ }
+ }
+
+ if(conf == 'set') {
+ $(elems).each(function() {
+ var elem = this;
+ conf = elem.percentage_conf;
+ updateElem(elem, val);
+ $(elem.percentage).find('div').css('left', getLeft(val));
+ });
+ return elems;
+ }
$(elems).each(function() {
var elem = this;
@@ -52,28 +80,12 @@
hasValue = false;
}
-
- function getLeft(val) {
- return Math.floor((conf.width * -1) + (conf.width / 100 * val))
- }
-
function handleClick(e) {
var p = $(this).offset();
var percent = Math.ceil((e.clientX - p.left) / (conf.width / 100));
- if(hasValue) {
- $(elem)
- .attr('value', percent)
- .val(percent)
- .attr('title', percent + '%')
- .trigger('clickupdate')
- ;
- } else {
- $(elem)
- .text(percent + '%')
- .trigger('clickupdate')
- ;
- }
+ updateElem(elem, percent);
+ $(elem).trigger('clickupdate');
$(inner)
.css('left', getLeft(percent))
@@ -117,6 +129,7 @@
;
this.percentage = bar;
+ this.percentage_conf = conf;
});
return elems;
View
2  percentage.min.js
@@ -1 +1 @@
-(function(){function j(a){if(a=="destroy")$(this).each(function(){$(this).show();this.percentage&&$(this.percentage).remove()});else{var f={width:200,height:15,border:"1px solid #000000",color:"#cc0000",background:"#ffffff",clickable:true,classname:"percentage",display:"inline-block"};if(a==undefined)a=f;else for(i in f)if(a[i]==undefined)a[i]=f[i];$(this).each(function(){function k(b){var l=$(this).offset();b=Math.ceil((b.clientX-l.left)/(a.width/100));g?$(c).attr("value",b).val(b).attr("title",b+"%").trigger("clickupdate"):$(c).text(b+"%").trigger("clickupdate");$(h).css("left",Math.floor(a.width*-1+a.width/100*b));return false}var c=this,d=$("<div></div>"),h=$("<div></div>"),e,g;if($(c).is("textarea,input,select")){e=$(c).val();g=true}else{e=parseInt($(c).text().replace("%",""));g=false}$(d).attr("title",e+"%").css("display",a.display).css("border",a.border).css("background",a.background).css("position","relative").css("overflow","hidden").css("width",a.width).css("height",a.height);a.clickable&&$(d).click(k);a.classname&&$(d).addClass(a.classname);$(h).css("background",a.color).css("position","absolute").css("top",0).css("left",Math.floor(a.width*-1+a.width/100*e)).css("width",a.width).css("height",a.height).appendTo(d);$(c).hide().after(d);this.percentage=d});return this}}jQuery.fn.percentage=function(){j.apply(this,arguments)}})();
+(function(){function m(a,j){function f(c){return Math.floor(a.width*-1+a.width/100*c)}function k(c,b){$(c).is("textarea,input,select")?$(c).attr("value",b).val(b).attr("title",b+"%"):$(c).text(b+"%")}if(a=="destroy"){$(this).each(function(){$(this).show();this.p&&$(this.p).remove()});return this}var g={width:200,height:15,border:"1px solid #000000",color:"#cc0000",background:"#ffffff",clickable:true,classname:"percentage",display:"inline-block"};if(a==undefined)a=g;else for(i in g)if(a[i]==undefined)a[i]=g[i];if(a=="set"){$(this).each(function(){a=this.pc;k(this,j);$(this.p).find("div").css("left",f(j))});return this}$(this).each(function(){function c(e){var n=$(this).offset();e=Math.ceil((e.clientX-n.left)/(a.width/100));k(b,e);$(b).trigger("clickupdate");$(l).css("left",f(e));return false}var b=this,d=$("<div></div>"),l=$("<div></div>"),h;h=$(b).is("textarea,input,select")?$(b).val():parseInt($(b).text().replace("%",""));$(d).attr("title",h+"%").css("display",a.display).css("border",a.border).css("background",a.background).css("position","relative").css("overflow","hidden").css("width",a.width).css("height",a.height);a.clickable&&$(d).click(c);a.classname&&$(d).addClass(a.classname);$(l).css("background",a.color).css("position","absolute").css("top",0).css("left",f(h)).css("width",a.width).css("height",a.height).appendTo(d);$(b).hide().after(d);this.p=d;this.pc=a});return this}jQuery.fn.percentage=function(){m.apply(this,arguments)}})();
Please sign in to comment.
Something went wrong with that request. Please try again.