Skip to content

Commit

Permalink
Update the time range.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobytripp committed Mar 17, 2012
1 parent c402114 commit f9528a0
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 42 deletions.
119 changes: 78 additions & 41 deletions js/meeting-ticker.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,51 @@
(function() {
var Locale, MeetingTicker, SECONDS_PER_HOUR, Time, UPDATE_INTERVAL, root;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
var $, Locale, MeetingTicker, SECONDS_PER_HOUR, Time, UPDATE_INTERVAL, root;

$ = jQuery;

UPDATE_INTERVAL = 125;

SECONDS_PER_HOUR = 60.0 * 60.0;

MeetingTicker = (function() {

function MeetingTicker(form, settings) {
this.options = settings;
this.form = $(form);
this.display = $(this.options.displaySelector);
this.odometerElement = $(".odometer");
this.display.hide();
if (this.startTime() == null) {
this.startTime(Time.now());
}
if (this.startTime() == null) this.startTime(Time.now());
this._initForm();
this._bindFormEvents();
this._detectCurrency();
}

MeetingTicker.prototype.start = function() {
if (!this.valid()) {
return;
}
var _this = this;
if (!this.valid()) return;
this.startTime(this._formElement("start_time").val());
this.display.show();
this.form.parent().hide();
$("#started_at").text("(we began at " + (this.startTime().toString()) + ")");
this.odometerElement.odometer({
prefix: this.currencyLabel()
});
return this.timer = setInterval((__bind(function() {
return this.odometerElement.trigger("update", this.cost());
}, this)), UPDATE_INTERVAL);
return this.timer = setInterval((function() {
return _this.odometerElement.trigger("update", _this.cost());
}), UPDATE_INTERVAL);
};

MeetingTicker.prototype.stop = function() {
console.log("Stopped...");
clearInterval(this.timer);
return this.timer = null;
};

MeetingTicker.prototype.isRunning = function() {
return this.timer != null;
};

MeetingTicker.prototype.cost = function() {
try {
return this.perSecondBurn() * this.elapsedSeconds();
Expand All @@ -48,6 +54,7 @@
throw e;
}
};

MeetingTicker.prototype.hourlyRate = function(rate) {
var input;
input = this._formElement("hourly_rate");
Expand All @@ -62,6 +69,7 @@
}
return this._rate;
};

MeetingTicker.prototype.attendeeCount = function(count) {
var input;
input = this._formElement("attendees");
Expand All @@ -76,27 +84,30 @@
}
return this._attendees;
};

MeetingTicker.prototype.startTime = function(time) {
var input;
if (this.isRunning()) {
return this._startTime;
}
if (this.isRunning()) return this._startTime;
input = this._formElement("start_time");
if (time != null) {
this._startTime = new Time(time);
input.val(this._startTime.toString());
}
return this._startTime;
};

MeetingTicker.prototype.elapsedSeconds = function() {
return Time.now().secondsSince(this.startTime());
};

MeetingTicker.prototype.hourlyBurn = function() {
return this.hourlyRate() * this.attendeeCount();
};

MeetingTicker.prototype.perSecondBurn = function() {
return this.hourlyBurn() / SECONDS_PER_HOUR;
};

MeetingTicker.prototype.currency = function(newCurrency) {
var view;
view = this.form.find("select[name=units]");
Expand All @@ -105,22 +116,25 @@
}
return view.val();
};

MeetingTicker.prototype.currencyLabel = function() {
return this.form.find("select[name=units] option:selected").text();
};

MeetingTicker.prototype.valid = function() {
return this.form.valid();
};

MeetingTicker.prototype._initForm = function() {
if (this.startTime() == null) {
this.startTime(Time.now());
}
if (this.startTime() == null) this.startTime(Time.now());
$("input.watermark").each(function() {
return $(this).watermark($(this).attr("title"));
});
$("#start_time").clockpick({
military: true,
layout: "horizontal"
starthour: 0,
endhour: 23,
layout: "vertical"
});
return this.form.validate({
rules: {
Expand All @@ -142,40 +156,49 @@
}
});
};

MeetingTicker.prototype._bindFormEvents = function() {
this._formElement("start_time").change(__bind(function(event) {
var _this = this;
this._formElement("start_time").change(function(event) {
event.preventDefault();
return this.startTime($(event.target).val());
}, this));
this._formElement("hourly_rate").change(__bind(function(event) {
return _this.startTime($(event.target).val());
});
this._formElement("hourly_rate").change(function(event) {
event.preventDefault();
return this.hourlyRate($(event.target).val());
}, this));
this._formElement("attendees").change(__bind(function(event) {
return _this.hourlyRate($(event.target).val());
});
this._formElement("attendees").change(function(event) {
event.preventDefault();
return this.attendeeCount($(event.target).val());
}, this));
this.form.submit(__bind(function(event) {
return _this.attendeeCount($(event.target).val());
});
this.form.submit(function(event) {
event.preventDefault();
return this.start();
}, this));
return $("form.stop").submit(__bind(function(event) {
return _this.start();
});
return $("form.stop").submit(function(event) {
event.preventDefault();
return this.stop();
}, this));
return _this.stop();
});
};

MeetingTicker.prototype._detectCurrency = function() {
return this.currency(Locale.current().currency());
};

MeetingTicker.prototype._formElement = function(name) {
return this.form.find("input[name=" + name + "]");
};

return MeetingTicker;

})();

Time = (function() {

Time.now = function() {
return new Time();
};

function Time(time) {
var hours, minutes, _ref;
if ((time != null) && (time.getMinutes != null)) {
Expand All @@ -194,32 +217,37 @@
this.time = new Date();
}
}

Time.prototype.secondsSince = function(past) {
var diff;
diff = this.time.getTime() - past.time.getTime();
return diff / 1000.00;
};

Time.prototype.toString = function() {
var minutes;
minutes = this.time.getMinutes();
if (minutes < 10) {
minutes = "0" + minutes;
}
if (minutes < 10) minutes = "0" + minutes;
return this.time.getHours() + ":" + minutes;
};

return Time;

})();

Locale = (function() {

Locale.current = function() {
return new Locale();
};

function Locale(language) {
var lang, _ref;
if (language != null) {
this.language = language;
return;
}
if (typeof navigator != "undefined" && navigator !== null) {
if (typeof navigator !== "undefined" && navigator !== null) {
lang = (_ref = navigator.language) != null ? _ref : navigator.userLanguage;
} else {
lang = "en-us";
Expand All @@ -231,6 +259,7 @@
this.language = lang[0];
}
}

Locale.prototype.currency = function() {
switch (this.language) {
case "us":
Expand Down Expand Up @@ -259,8 +288,13 @@
return "Kr";
}
};

return Locale;

})();

console.log("installing plugin…");

$.fn.meetingTicker = function(options) {
var settings;
settings = {
Expand All @@ -269,9 +303,7 @@
return this.each(function() {
var $this, data;
$this = $(this);
if (options) {
$.extend(settings, options);
}
if (options) $.extend(settings, options);
data = $this.data('meeting-ticker');
if (!data) {
return $this.data("meeting-ticker", {
Expand All @@ -281,8 +313,13 @@
}
});
};
root = typeof exports != "undefined" && exports !== null ? exports : this;

root = typeof exports !== "undefined" && exports !== null ? exports : this;

root.MeetingTicker = MeetingTicker;

root.MeetingTicker.Time = Time;

root.MeetingTicker.Locale = Locale;

}).call(this);
8 changes: 7 additions & 1 deletion src/meeting-ticker.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
$ = jQuery

UPDATE_INTERVAL = 125
SECONDS_PER_HOUR = 60.0 * 60.0

Expand Down Expand Up @@ -102,7 +104,10 @@ class MeetingTicker
$("input.watermark").each ->
$(this).watermark( $(this).attr( "title" ) )
$("#start_time").clockpick({
military: true, layout: "horizontal"
military: true,
starthour: 0,
endhour: 23,
layout: "vertical"
})
@form.validate
rules:
Expand Down Expand Up @@ -205,6 +210,7 @@ class Locale
when 'sv' then "Kr"


console.log( "installing plugin…" )
$.fn.meetingTicker = (options) ->
settings =
displaySelector: "#display"
Expand Down

0 comments on commit f9528a0

Please sign in to comment.