Skip to content

Commit

Permalink
Added ruler scale tick formatter event to allow custom formatting of …
Browse files Browse the repository at this point in the history
…scale ticks, so that application specific data can be reflected within the scale display itself
  • Loading branch information
Crispin Botticelli committed Mar 6, 2013
1 parent ff10abe commit 661b97d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions jQRuler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
next: function(value){
return value + 1;
},
format: function (tick, startValue, endValue) {
return;
},
label: function(tick){
return Math.round(tick);
},
Expand Down Expand Up @@ -115,6 +118,7 @@
label = $("<span class='ui-ruler-tick-label' />").appendTo(inner);

label.text(scaleOptions.label(start, end));
scaleOptions.format(container, start, end);

return container;
}
Expand Down
46 changes: 46 additions & 0 deletions tests/unit/rulerTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@
}
};

var formattedScale = {
first: function(min){
return Math.floor(min / 10) * 10;
},
next: function(previous){
return previous + 10;
},
format: function(tick, startValue, endValue){
if (startValue % (2 * 10)) {
tick.addClass("formatterTestClass");
}
},
label: function(value){
return value;
}
};

var el = function(){return $("#test") };

var ctorTest = new TestCase(
Expand Down Expand Up @@ -78,6 +95,34 @@
}
);

var formattedScaleWith4Ticks = new TestCase(
"Formatting ticks with one scale of 4 ticks",
function(){
ruled = $("<div />").appendTo(el());
ruled.ruler({
min: 0,
max: 40,
scales: [formattedScale]
});
},
function(){
var scale = ruled.find(".ui-ruler-scale"),
tick = scale.find(".ui-ruler-tick"),
label = tick.find(".ui-ruler-tick-label"),
formattedTick = scale.find(".formatterTestClass");

QUnit.equal(scale.length, 1, "Should have created one scale");
QUnit.equal(tick.length, 4, "Should have created four ticks");
QUnit.equal(label.length, 4, "Should have created four labels");
QUnit.equal(formattedTick.length, 2, "Should have created two labels");
QUnit.ok(scale.hasClass("ui-ruler-scale0"), "Class should have been initialized for first ruler");
QUnit.ok($(tick[0]).attr("style") && $(tick[0]).attr("style").indexOf("width: 25%") >= 0, "Width should be 25%: " + $(tick[0]).attr("style"));

ruled.ruler("destroy");
ruled.detach();
}
);

var scaleStartingAfter = new TestCase(
"Shifted scale starting after min",
function(){
Expand Down Expand Up @@ -254,6 +299,7 @@

testRunner.add("ruler", [ctorTest, dtorTest,
ctorWithOneScale,
formattedScaleWith4Ticks,
scaleStartingAfter,
lasttick,
tickStop,
Expand Down

0 comments on commit 661b97d

Please sign in to comment.