1
0
Fork 0
fgdata/Phi/topics/Environment/DateTime.js

98 lines
3.3 KiB
JavaScript
Raw Normal View History

2015-01-25 12:02:20 +00:00
define([
'jquery', 'knockout', 'text!./DateTime.html', 'fgcommand', 'kojqui/datepicker', 'kojqui/spinner', 'clockpicker'
], function(jquery, ko, htmlString, fgcommand) {
2015-01-25 12:02:20 +00:00
function ViewModel(params) {
var self = this;
2015-01-25 12:02:20 +00:00
self.timesOfToday = [
'Clock Time', 'Dawn', 'Morning', 'Noon', 'Afternoon', 'Dusk', 'Evening', 'Night',
];
self.setTimeOfToday = function(type) {
var offsetTypes = {
"Clock Time" : "real",
"Dawn" : "dawn",
"Morning" : "morning",
"Noon" : "noon",
"Afternoon" : "afternoon",
"Dusk" : "dusk",
"Evening" : "evening",
"Night" : "night",
2015-01-25 12:02:20 +00:00
}
offsetType = offsetTypes[type] || null;
if (!offsetType) {
console.log("unknown time offset type ", type);
2015-01-25 12:02:20 +00:00
return;
}
fgcommand.timeofday(offsetType);
}
self.gmtProp = ko.observable().extend({
fgprop : 'gmt'
});
//TODO: bind this to gmtProp?
// self.clockpickerInput.val(d.getUTCHours() + ':' + d.getUTCMinutes());
2015-01-25 12:02:20 +00:00
self.warp = ko.observable().extend({
fgprop : 'timeWarp'
});
2015-01-25 12:02:20 +00:00
self.simTimeUTC = ko.pureComputed(function() {
// make a Date object holding the UTC time
var d = new Date(self.gmtProp() + "Z");
return d.getTime();
2015-01-25 12:02:20 +00:00
});
self.timeAsString = ko.pureComputed(function() {
var d = new Date();
d.setTime( self.simTimeUTC() );
return d.toUTCString()
});
self.simTimeAsLocalTime = ko.pureComputed(function() {
// jqui datepicker displays local (browser) time, so fake it by adding timezoneOffset
var d = new Date();
d.setTime( self.simTimeUTC() + 60000 * d.getTimezoneOffset() );
return d;
2015-01-25 12:02:20 +00:00
});
self.onDateSelect = function(dateText, inst) {
var utc = new Date(self.simTimeUTC());
utc.setFullYear(inst.selectedYear, inst.selectedMonth, inst.selectedDay);
self.setWarpFor( utc.getTime() );
}
self.setWarpFor = function( newDateTime ) {
var warp = (newDateTime - self.simTimeUTC())/1000;
console.log("warp=", warp, self.warp());
ko.utils.knockprops.write( "timeWarp", self.warp() + warp );
}
// clockpicker: see http://weareoutman.github.io/clockpicker/
self.clockpicker = jquery('.clockpicker').clockpicker({
placement : 'top',
align : 'left',
autoclose : true,
afterDone : function() {
var utc = new Date();
utc.setTime( self.simTimeUTC() );
var hm = self.clockpickerInput.val().toString().split(":");
utc.setUTCHours(hm[0]);
utc.setUTCMinutes(hm[1]);
self.setWarpFor( utc.getTime() );
2015-01-25 12:02:20 +00:00
},
});
self.clockpickerInput = jquery('.clockpicker input');
2015-01-25 12:02:20 +00:00
}
ViewModel.prototype.dispose = function() {
}
// Return component definition
return {
viewModel : ViewModel,
template : htmlString
};
});