Permalink
Browse files

add display zeros options

  • Loading branch information...
1 parent f5a2c31 commit 7076e3391b2187be7f3611f30ae6e26b6549145c @patrickarlt committed Oct 19, 2011
Showing with 32 additions and 10 deletions.
  1. +21 −4 jquery-relative.js
  2. +11 −6 test.html
View
@@ -3,6 +3,7 @@
var defaults = {
format: "%dd:%hh:%mm:%ss",
+ displayZeros: true,
tick: 990
};
@@ -63,7 +64,7 @@
};
- function formatTime(secondsRemaining, format){
+ function formatTime(secondsRemaining, format, displayZeros){
var remaining = {
days: calcage(secondsRemaining, 86400, 100000),
@@ -90,8 +91,24 @@
var text = format;
for(i = 0; i <= patterns.length-1; i++ ){
-
- tmp = text.replace(patterns[i], replacements[i]);
+
+ //If display zeros is true replacement should always happen no matter what
+ //if display zeros is false replacement should only happen if the remaining is not zero
+ //if display zeros is false and the replacement is a string render it only if remaining[string] > 0
+ //else remove the pattern from the string
+
+ notZero = !!(parseInt(replacements[i]));
+ isString = (typeof replacements[i] == "string");
+
+ if(displayZeros){
+ tmp = text.replace(patterns[i], replacements[i]);
+ } else if(notZero) {
+ tmp = text.replace(patterns[i], replacements[i]);
+ } else if(isString && remaining[replacements[i]] > 0) {
+ tmp = text.replace(patterns[i], replacements[i]);
+ } else {
+ tmp = text.replace(patterns[i], "");
+ };
text = tmp;
@@ -158,7 +175,7 @@
if(data.options.format == "human"){
$(this).text(humanTime(seconds));
} else {
- $(this).text(formatTime(seconds, data.options.format));
+ $(this).text(formatTime(seconds, data.options.format, data.options.displayZeros));
}
});
View
@@ -9,18 +9,23 @@
Relative: march 25th was <time id="relative" datetime="Fri Mar 13 2011"></time><br>
Timer: <time id="timer" datetime="Fri Oct 12 2011 06:0:00 -07:00"></time> since i woke up at 6am oct 12st
<h1>Via Data</h1>
- Countdown: <time data-relativize datetime="Fri Oct 12 2011 12:00:00 -07:00"></time> until noon oct 12st<br>
+ <!--Countdown: <time data-relativize datetime="Fri Oct 12 2011 12:00:00 -07:00"></time> until noon oct 12st<br>
Relative: march 25th was <time data-relativize="human" datetime="Fri Mar 13 2011"></time><br>
- Timer: <time data-relativize="%hh:%mm:%ss" datetime="Fri Oct 12 2011 06:0:00 -07:00"></time> since i woke up at 6am oct 12st
-
+ Timer: <time data-relativize="%hh:%mm:%ss" datetime="Fri Oct 12 2011 06:0:00 -07:00"></time> since i woke up at 6am oct 12st-->
+<h1>No Zero</h1>
+ Countdown: <time id="countdown-nozero" datetime="Fri Oct 19 2011 12:00:00 -07:00"></time> until noon oct 12st<br>
+ Timer: <time id="timer-nozero" datetime="Fri Oct 19 2011 06:0:00 -07:00"></time> since i woke up at 6am oct 12st
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery-relative.js"></script>
<script type="text/javascript">
$(document).ready(function(){
- $('#countdown').relative();
- $("#relative").relative({format:"human"});
- $('#timer').relative();
+ //$('#countdown').relative();
+ //$("#relative").relative({format:"human"});
+ //$('#timer').relative();
+
+ $('#countdown-nozero').relative({format:"%dd %DAYS %hh %HOURS %mm %MINUTES %ss %SECONDS",displayZeros:false, tick:0});
+ $('#timer-nozero').relative({displayZeros:false, tick:0});
});
</script>
</html>

0 comments on commit 7076e33

Please sign in to comment.