Feature #35: Livestamp implemented

Humanized dates now update automatically when a page is left open for
some time.
This commit is contained in:
Gary Sharp
2014-02-11 16:50:03 +11:00
parent c4cc54d316
commit 7bdbeb6a82
40 changed files with 1395 additions and 1022 deletions
+152 -2
View File
@@ -41888,6 +41888,150 @@ if ( $.watermark.runOnce ) {
///#source 1 1 /ClientSource/Scripts/Core/disco.moment.extensions.js
moment.lang('en-au');
///#source 1 1 /ClientSource/Scripts/Core/livestamp.js
// Livestamp.js / v1.1.2 / (c) 2012 Matt Bradley / MIT License
(function (plugin) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery', 'moment'], plugin);
} else {
// Browser globals
plugin(jQuery, moment);
}
}(function ($, moment) {
// DISCO: Update Interval = 30 seconds (not 1 second)
var updateInterval = 30e3,
paused = false,
$livestamps = $([]),
init = function () {
livestampGlobal.resume();
},
prep = function ($el, timestamp) {
var oldData = $el.data('livestampdata');
// DISCO: Use milliseconds, not seconds
//if (typeof timestamp == 'number')
// timestamp *= 1e3;
$el.removeAttr('data-livestamp')
.removeData('livestamp');
timestamp = moment(timestamp);
if (moment.isMoment(timestamp) && !isNaN(+timestamp)) {
var newData = $.extend({}, { 'original': $el.contents() }, oldData);
newData.moment = moment(timestamp);
// DISCO: Add 'WithoutSuffix' support
newData.fromNowWithoutSuffix = $el.hasClass('noMomentSuffix');
$el.data('livestampdata', newData).empty();
$livestamps.push($el[0]);
}
},
run = function () {
if (paused) return;
livestampGlobal.update();
setTimeout(run, updateInterval);
},
livestampGlobal = {
update: function () {
$('[data-livestamp]').each(function () {
var $this = $(this);
prep($this, $this.data('livestamp'));
});
var toRemove = [];
$livestamps.each(function () {
var $this = $(this),
data = $this.data('livestampdata');
if (data === undefined)
toRemove.push(this);
else if (moment.isMoment(data.moment)) {
var from = $this.html(),
to = data.moment.fromNow(data.fromNowWithoutSuffix); // DISCO: Implement 'WithoutSuffix' support
if (from != to) {
var e = $.Event('change.livestamp');
$this.trigger(e, [from, to]);
if (!e.isDefaultPrevented())
$this.html(to);
}
}
});
$livestamps = $livestamps.not(toRemove);
delete $livestamps.prevObject
},
pause: function () {
paused = true;
},
resume: function () {
paused = false;
run();
},
interval: function (interval) {
if (interval === undefined)
return updateInterval;
updateInterval = interval;
}
},
livestampLocal = {
add: function ($el, timestamp) {
// DISCO: Use milliseconds, not seconds
//if (typeof timestamp == 'number')
// timestamp *= 1e3;
timestamp = moment(timestamp);
if (moment.isMoment(timestamp) && !isNaN(+timestamp)) {
$el.each(function () {
prep($(this), timestamp);
});
livestampGlobal.update();
}
return $el;
},
destroy: function ($el) {
$livestamps = $livestamps.not($el);
$el.each(function () {
var $this = $(this),
data = $this.data('livestampdata');
if (data === undefined)
return $el;
$this
.html(data.original ? data.original : '')
.removeData('livestampdata');
});
return $el;
},
isLivestamp: function ($el) {
return $el.data('livestampdata') !== undefined;
}
};
$.livestamp = livestampGlobal;
$(init);
$.fn.livestamp = function (method, options) {
if (!livestampLocal[method]) {
options = method;
method = 'add';
}
return livestampLocal[method](this, options);
};
}));
///#source 1 1 /ClientSource/Scripts/Core/disco.dataTables.extensions.js
jQuery.fn.dataTableExt.afnSortData['text'] = function (oSettings, iColumn) {
var aData = [];
@@ -41899,9 +42043,15 @@ jQuery.fn.dataTableExt.afnSortData['text'] = function (oSettings, iColumn) {
jQuery.fn.dataTableExt.afnSortData['disco_datetime'] = function (oSettings, iColumn) {
var aData = [];
$('td:eq(' + iColumn + ')', oSettings.oApi._fnGetTrNodes(oSettings)).each(function () {
var d = $(this).children('span.date[data-discodatetime]');
var d = $(this).children('span.date');
if (d.length > 0)
aData.push((d.attr('data-discodatetime')) * 1);
if (d.is('[data-livestamp]')){
aData.push((d.attr('data-livestamp')) * 1);
} else if (d.data('livestampdata') !== undefined) {
aData.push(d.data('livestampdata').moment.valueOf());
}else{
aData.push(-1);
}
else
aData.push(-1);
});
@@ -11,6 +11,7 @@
<file>/ClientSource/Scripts/Core/moment.js</file>
<file>/ClientSource/Scripts/Core/moment.en-au.js</file>
<file>/ClientSource/Scripts/Core/disco.moment.extensions.js</file>
<file>/ClientSource/Scripts/Core/livestamp.js</file>
<file>/ClientSource/Scripts/Core/disco.dataTables.extensions.js</file>
<file>/ClientSource/Scripts/Core/disco.uicore.js</file>
</bundle>
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -9,5 +9,6 @@
/// <reference path="moment.js" />
/// <reference path="moment.en-au.js" />
/// <reference path="disco.moment.extensions.js" />
/// <reference path="livestamp.js" />
/// <reference path="disco.dataTables.extensions.js" />
/// <reference path="disco.uicore.js" />
@@ -8,9 +8,15 @@
jQuery.fn.dataTableExt.afnSortData['disco_datetime'] = function (oSettings, iColumn) {
var aData = [];
$('td:eq(' + iColumn + ')', oSettings.oApi._fnGetTrNodes(oSettings)).each(function () {
var d = $(this).children('span.date[data-discodatetime]');
var d = $(this).children('span.date');
if (d.length > 0)
aData.push((d.attr('data-discodatetime')) * 1);
if (d.is('[data-livestamp]')){
aData.push((d.attr('data-livestamp')) * 1);
} else if (d.data('livestampdata') !== undefined) {
aData.push(d.data('livestampdata').moment.valueOf());
}else{
aData.push(-1);
}
else
aData.push(-1);
});
@@ -0,0 +1,143 @@
// Livestamp.js / v1.1.2 / (c) 2012 Matt Bradley / MIT License
(function (plugin) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery', 'moment'], plugin);
} else {
// Browser globals
plugin(jQuery, moment);
}
}(function ($, moment) {
// DISCO: Update Interval = 30 seconds (not 1 second)
var updateInterval = 30e3,
paused = false,
$livestamps = $([]),
init = function () {
livestampGlobal.resume();
},
prep = function ($el, timestamp) {
var oldData = $el.data('livestampdata');
// DISCO: Use milliseconds, not seconds
//if (typeof timestamp == 'number')
// timestamp *= 1e3;
$el.removeAttr('data-livestamp')
.removeData('livestamp');
timestamp = moment(timestamp);
if (moment.isMoment(timestamp) && !isNaN(+timestamp)) {
var newData = $.extend({}, { 'original': $el.contents() }, oldData);
newData.moment = moment(timestamp);
// DISCO: Add 'WithoutSuffix' support
newData.fromNowWithoutSuffix = $el.hasClass('noMomentSuffix');
$el.data('livestampdata', newData).empty();
$livestamps.push($el[0]);
}
},
run = function () {
if (paused) return;
livestampGlobal.update();
setTimeout(run, updateInterval);
},
livestampGlobal = {
update: function () {
$('[data-livestamp]').each(function () {
var $this = $(this);
prep($this, $this.data('livestamp'));
});
var toRemove = [];
$livestamps.each(function () {
var $this = $(this),
data = $this.data('livestampdata');
if (data === undefined)
toRemove.push(this);
else if (moment.isMoment(data.moment)) {
var from = $this.html(),
to = data.moment.fromNow(data.fromNowWithoutSuffix); // DISCO: Implement 'WithoutSuffix' support
if (from != to) {
var e = $.Event('change.livestamp');
$this.trigger(e, [from, to]);
if (!e.isDefaultPrevented())
$this.html(to);
}
}
});
$livestamps = $livestamps.not(toRemove);
delete $livestamps.prevObject
},
pause: function () {
paused = true;
},
resume: function () {
paused = false;
run();
},
interval: function (interval) {
if (interval === undefined)
return updateInterval;
updateInterval = interval;
}
},
livestampLocal = {
add: function ($el, timestamp) {
// DISCO: Use milliseconds, not seconds
//if (typeof timestamp == 'number')
// timestamp *= 1e3;
timestamp = moment(timestamp);
if (moment.isMoment(timestamp) && !isNaN(+timestamp)) {
$el.each(function () {
prep($(this), timestamp);
});
livestampGlobal.update();
}
return $el;
},
destroy: function ($el) {
$livestamps = $livestamps.not($el);
$el.each(function () {
var $this = $(this),
data = $this.data('livestampdata');
if (data === undefined)
return $el;
$this
.html(data.original ? data.original : '')
.removeData('livestampdata');
});
return $el;
},
isLivestamp: function ($el) {
return $el.data('livestampdata') !== undefined;
}
};
$.livestamp = livestampGlobal;
$(init);
$.fn.livestamp = function (method, options) {
if (!livestampLocal[method]) {
options = method;
method = 'add';
}
return livestampLocal[method](this, options);
};
}));
@@ -1,4 +1,4 @@
///#source 1 1 /ClientSource/Scripts/Modules/Disco-PropertyChangeHelpers/disco.propertychangehelpers.js
///#source 1 1 /ClientSource/Scripts/Modules/Disco-PropertyChangeHelpers/disco.propertychangehelpers.js
if (!document.DiscoFunctions) {
document.DiscoFunctions = {};
}
@@ -260,13 +260,13 @@ if (!document.DiscoFunctions.DateDialogCreateUpdater)
$ajaxLoading.hide();
} else {
if (response.DateTimeFull) {
$dateField.attr('data-datetimeformatted', response.DateTimeJavascript)
.attr('data-discodatetime', response.DateTimeSortable)
$dateField.attr('data-isodate', response.DateTimeISO8601)
.attr('data-livestamp', response.DateTimeUnixEpoc)
.attr('title', response.DateTimeFull)
.text(response.DateTimeFriendly);
} else {
$dateField.attr('data-datetimeformatted', '')
.attr('data-discodatetime', '-1')
$dateField.attr('data-isodate', '')
.attr('data-livestamp', '-1')
.attr('title', notSetDisplay)
.text(notSetDisplay);
}
@@ -301,7 +301,7 @@ if (!document.DiscoFunctions.DateDialogCreateUpdater)
d.dialog('option', 'title', friendlyName);
dialogHeader.text(friendlyName + ' Date');
var dfVal = $('#' + DateField).attr('data-datetimeformatted');
var dfVal = $('#' + DateField).attr('data-isodate');
if (dfVal)
dialogDateBox.datetimepicker('setDate', new Date(dfVal));
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -259,13 +259,13 @@ if (!document.DiscoFunctions.DateDialogCreateUpdater)
$ajaxLoading.hide();
} else {
if (response.DateTimeFull) {
$dateField.attr('data-datetimeformatted', response.DateTimeJavascript)
.attr('data-discodatetime', response.DateTimeSortable)
$dateField.attr('data-isodate', response.DateTimeISO8601)
.attr('data-livestamp', response.DateTimeUnixEpoc)
.attr('title', response.DateTimeFull)
.text(response.DateTimeFriendly);
} else {
$dateField.attr('data-datetimeformatted', '')
.attr('data-discodatetime', '-1')
$dateField.attr('data-isodate', '')
.attr('data-livestamp', '-1')
.attr('title', notSetDisplay)
.text(notSetDisplay);
}
@@ -300,7 +300,7 @@ if (!document.DiscoFunctions.DateDialogCreateUpdater)
d.dialog('option', 'title', friendlyName);
dialogHeader.text(friendlyName + ' Date');
var dfVal = $('#' + DateField).attr('data-datetimeformatted');
var dfVal = $('#' + DateField).attr('data-isodate');
if (dfVal)
dialogDateBox.datetimepicker('setDate', new Date(dfVal));