Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimeAgo plugin #933

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions symphony/assets/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,15 @@
Symphony.Message.fade('silence', 10000);

// Relative times in system messages
$('abbr.timeago').each(function() {
var time = $(this).parent();
time.html(time.html().replace(Symphony.Language.get('at') + ' ', ''));
$('header p.notice').symphonyTimeAgo().each(function() {
var notice = $(this);
notice.html(notice.html().replace(Symphony.Language.get('at') + ' ', ''));
});
Symphony.Message.timer();

// Update relative times in system messages
setInterval(function() {
$('header p.notice').symphonyTimeAgo();
}, 60000);

// XSLT utilities
$('textarea').blur(function() {
Expand Down
57 changes: 0 additions & 57 deletions symphony/assets/symphony.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,63 +366,6 @@ var Symphony = {};
});
}
};

/**
* Convert absolute message time to relative time and update continuously
*/
this.timer = function() {
var time = Date.parse($('abbr.timeago').attr('title')),
to = new Date(),
from = new Date();

// Set time
from.setTime(time);

// Set relative time
$('abbr.timeago').text(this.distance(from, to));

// Update continuously
window.setTimeout("Symphony.Message.timer()", 60000);
};

/**
* Calculate relative time.
*
* @param {Date} from
* Starting date
* @param {Date} to
* Current date
*/
this.distance = function(from, to) {

// Calculate time difference
var distance = to - from,

// Convert time to minutes
time = Math.floor(distance / 60000);

// Return relative date based on passed time
if(time < 1) {
return Symphony.Language.get('just now');
}
if(time < 2) {
return Symphony.Language.get('a minute ago');
}
if(time < 45) {
return Symphony.Language.get('{$minutes} minutes ago', {
'minutes': time
});
}
if(time < 90) {
return Symphony.Language.get('about 1 hour ago');
}
else {
return Symphony.Language.get('about {$hours} hours ago', {
'hours': Math.floor(time / 60)
});
}
};

}),

/**
Expand Down
109 changes: 109 additions & 0 deletions symphony/assets/symphony.timeago.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* @package assets
*/

(function($) {

/**
* @todo: documentation
*/
$.fn.symphonyTimeAgo = function(options) {
var objects = this,
settings = {
items: 'time',
timestamp: 'datetime'
};

$.extend(settings, options);

/*-----------------------------------------------------------------------*/

function parse(item) {
var timestamp = item.data('timestamp'),
datetime, now;

// Fetch stored timestamp
if($.isNumeric(timestamp)) {
return timestamp;
}

// Parse date
else {
datetime = item.attr(settings.timestamp);

// Defined date and time
if(datetime) {

// Browsers that understand ISO 8601
timestamp = Date.parse(datetime);

// Browsers that understand ISO 8601 without timezone
if(isNaN(timestamp)) {
timestamp = Date.parse(datetime.substring(0, 19));
}

// Browsers that don't understand ISO 8601
if(isNaN(timestamp)) {
timestamp = Date.parse(datetime.replace(/-/g, '/').replace(/T/g, ' ').replace(/Z/, ' UTC'));
}
}

// Undefined date and time
if(!$.isNumeric(timestamp)) {
now = new Date();
timestamp = now.getTime();
}

// Store and return timestamp
item.data('timestamp', timestamp);
return timestamp;
}
}

function say(from, to) {

// Calculate time difference
var distance = to - from,

// Convert time to minutes
time = Math.floor(distance / 60000);

// Return relative date based on passed time
if(time < 1) {
return Symphony.Language.get('just now');
}
if(time < 2) {
return Symphony.Language.get('a minute ago');
}
if(time < 45) {
return Symphony.Language.get('{$minutes} minutes ago', {
'minutes': time
});
}
if(time < 90) {
return Symphony.Language.get('about 1 hour ago');
}
else {
return Symphony.Language.get('about {$hours} hours ago', {
'hours': Math.floor(time / 60)
});
}
};

/*-----------------------------------------------------------------------*/

objects.find(settings.items).each(function() {
var item = $(this),
from = parse(item),
to = new Date();

// Set relative time
item.text(say(from, to));
});

/*-----------------------------------------------------------------------*/

return objects;
};

})(jQuery.noConflict());
6 changes: 3 additions & 3 deletions symphony/content/content.blueprintsdatasources.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function __form(){

case 'saved':
$this->pageAlert(
__('Data source updated at %s.', array(DateTimeObj::getTimeAgo(__SYM_TIME_FORMAT__)))
__('Data source updated at %s.', array(DateTimeObj::getTimeAgo()))
. ' <a href="' . SYMPHONY_URL . '/blueprints/datasources/new/" accesskey="c">'
. __('Create another?')
. '</a> <a href="' . SYMPHONY_URL . '/blueprints/datasources/" accesskey="a">'
Expand All @@ -63,7 +63,7 @@ public function __form(){

case 'created':
$this->pageAlert(
__('Data source created at %s.', array(DateTimeObj::getTimeAgo(__SYM_TIME_FORMAT__)))
__('Data source created at %s.', array(DateTimeObj::getTimeAgo()))
. ' <a href="' . SYMPHONY_URL . '/blueprints/datasources/new/" accesskey="c">'
. __('Create another?')
. '</a> <a href="' . SYMPHONY_URL . '/blueprints/datasources/" accesskey="a">'
Expand Down Expand Up @@ -170,7 +170,7 @@ public function __form(){
if(isset($cache_id) && in_array('clear_cache', $this->_context)) {
$cache->forceExpiry($cache_id);
$this->pageAlert(
__('Data source cache cleared at %s.', array(DateTimeObj::getTimeAgo(__SYM_TIME_FORMAT__)))
__('Data source cache cleared at %s.', array(DateTimeObj::getTimeAgo()))
. '<a href="' . SYMPHONY_URL . '/blueprints/datasources" accesskey="a">'
. __('View all Data sources')
. '</a>'
Expand Down
4 changes: 2 additions & 2 deletions symphony/content/content.blueprintsevents.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function __form($readonly=false){

case 'saved':
$this->pageAlert(
__('Event updated at %s.', array(DateTimeObj::getTimeAgo(__SYM_TIME_FORMAT__)))
__('Event updated at %s.', array(DateTimeObj::getTimeAgo()))
. ' <a href="' . SYMPHONY_URL . '/blueprints/events/new/" accesskey="c">'
. __('Create another?')
. '</a> <a href="' . SYMPHONY_URL . '/blueprints/events/" accesskey="a">'
Expand All @@ -66,7 +66,7 @@ public function __form($readonly=false){

case 'created':
$this->pageAlert(
__('Event created at %s.', array(DateTimeObj::getTimeAgo(__SYM_TIME_FORMAT__)))
__('Event created at %s.', array(DateTimeObj::getTimeAgo()))
. ' <a href="' . SYMPHONY_URL . '/blueprints/events/new/" accesskey="c">'
. __('Create another?')
. '</a> <a href="' . SYMPHONY_URL . '/blueprints/events/" accesskey="a">'
Expand Down
6 changes: 3 additions & 3 deletions symphony/content/content.blueprintspages.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public function __viewTemplate() {
// Status message:
if(isset($this->_context[2])) {
$this->pageAlert(
__('Page updated at %s.', array(DateTimeObj::getTimeAgo(__SYM_TIME_FORMAT__)))
__('Page updated at %s.', array(DateTimeObj::getTimeAgo()))
. ' <a href="' . SYMPHONY_URL . '/blueprints/pages/new/" accesskey="c">'
. __('Create another?')
. '</a> <a href="' . SYMPHONY_URL . '/blueprints/pages/" accesskey="a">'
Expand Down Expand Up @@ -339,7 +339,7 @@ public function __viewEdit() {

case 'saved':
$this->pageAlert(
__('Page updated at %s.', array(DateTimeObj::getTimeAgo(__SYM_TIME_FORMAT__)))
__('Page updated at %s.', array(DateTimeObj::getTimeAgo()))
. ' <a href="' . SYMPHONY_URL . '/blueprints/pages/new/" accesskey="c">'
. __('Create another?')
. '</a> <a href="' . SYMPHONY_URL . '/blueprints/pages/" accesskey="a">'
Expand All @@ -351,7 +351,7 @@ public function __viewEdit() {

case 'created':
$this->pageAlert(
__('Page created at %s.', array(DateTimeObj::getTimeAgo(__SYM_TIME_FORMAT__)))
__('Page created at %s.', array(DateTimeObj::getTimeAgo()))
. ' <a href="' . SYMPHONY_URL . '/blueprints/pages/new/" accesskey="c">'
. __('Create another?')
. '</a> <a href="' . SYMPHONY_URL . '/blueprints/pages/" accesskey="a">'
Expand Down
4 changes: 2 additions & 2 deletions symphony/content/content.blueprintssections.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public function __viewEdit(){

case 'saved':
$this->pageAlert(
__('Section updated at %s.', array(DateTimeObj::getTimeAgo(__SYM_TIME_FORMAT__)))
__('Section updated at %s.', array(DateTimeObj::getTimeAgo()))
. ' <a href="' . SYMPHONY_URL . '/blueprints/sections/new/" accesskey="c">'
. __('Create another?')
. '</a> <a href="' . SYMPHONY_URL . '/blueprints/sections/" accesskey="a">'
Expand All @@ -299,7 +299,7 @@ public function __viewEdit(){

case 'created':
$this->pageAlert(
__('Section created at %s.', array(DateTimeObj::getTimeAgo(__SYM_TIME_FORMAT__)))
__('Section created at %s.', array(DateTimeObj::getTimeAgo()))
. ' <a href="' . SYMPHONY_URL . '/blueprints/sections/new/" accesskey="c">'
. __('Create another?')
. '</a> <a href="' . SYMPHONY_URL . '/blueprints/sections/" accesskey="a">'
Expand Down
4 changes: 2 additions & 2 deletions symphony/content/content.blueprintsutilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function __form(){

case 'saved':
$this->pageAlert(
__('Utility updated at %s.', array(DateTimeObj::getTimeAgo(__SYM_TIME_FORMAT__)))
__('Utility updated at %s.', array(DateTimeObj::getTimeAgo()))
. ' <a href="' . SYMPHONY_URL . '/blueprints/utilities/new/" accesskey="c">'
. __('Create another?')
. '</a> <a href="' . SYMPHONY_URL . '/blueprints/utilities/" accesskey="a">'
Expand All @@ -140,7 +140,7 @@ public function __form(){

case 'created':
$this->pageAlert(
__('Utility created at %s.', array(DateTimeObj::getTimeAgo(__SYM_TIME_FORMAT__)))
__('Utility created at %s.', array(DateTimeObj::getTimeAgo()))
. ' <a href="' . SYMPHONY_URL . '/blueprints/utilities/new/" accesskey="c">'
. __('Create another?')
. '</a> <a href="' . SYMPHONY_URL . '/blueprints/utilities/" accesskey="a">'
Expand Down
4 changes: 2 additions & 2 deletions symphony/content/content.publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ public function __viewEdit() {

case 'saved':
$this->pageAlert(
__('Entry updated at %s.', array(DateTimeObj::getTimeAgo(__SYM_TIME_FORMAT__)))
__('Entry updated at %s.', array(DateTimeObj::getTimeAgo()))
. ' <a href="' . SYMPHONY_URL . '/' . $link . '" accesskey="c">'
. __('Create another?')
. '</a> <a href="' . SYMPHONY_URL . '/publish/'.$this->_context['section_handle'].'/" accesskey="a">'
Expand All @@ -762,7 +762,7 @@ public function __viewEdit() {

case 'created':
$this->pageAlert(
__('Entry created at %s.', array(DateTimeObj::getTimeAgo(__SYM_TIME_FORMAT__)))
__('Entry created at %s.', array(DateTimeObj::getTimeAgo()))
. ' <a href="' . SYMPHONY_URL . '/' . $link . '" accesskey="c">'
. __('Create another?')
. '</a> <a href="' . SYMPHONY_URL . '/publish/'.$this->_context['section_handle'].'/" accesskey="a">'
Expand Down
4 changes: 2 additions & 2 deletions symphony/content/content.systemauthors.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function __form(){
switch($this->_context[2]){
case 'saved':
$this->pageAlert(
__('Author updated at %s.', array(DateTimeObj::getTimeAgo(__SYM_TIME_FORMAT__)))
__('Author updated at %s.', array(DateTimeObj::getTimeAgo()))
. ' <a href="' . SYMPHONY_URL . '/system/authors/new/" accesskey="c">'
. __('Create another?')
. '</a> <a href="' . SYMPHONY_URL . '/system/authors/" accesskey="a">'
Expand All @@ -216,7 +216,7 @@ public function __form(){

case 'created':
$this->pageAlert(
__('Author created at %s.', array(DateTimeObj::getTimeAgo(__SYM_TIME_FORMAT__)))
__('Author created at %s.', array(DateTimeObj::getTimeAgo()))
. ' <a href="' . SYMPHONY_URL . '/system/authors/new/" accesskey="c">'
. __('Create another?')
. '</a> <a href="' . SYMPHONY_URL . '/system/authors/" accesskey="a">'
Expand Down
6 changes: 3 additions & 3 deletions symphony/lib/core/class.datetimeobj.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,13 @@ public static function getGMT($format, $timestamp = 'now'){
* date (RFC 2822) as the title element. The value is the current time as
* specified by the `$format`.
*/
public static function getTimeAgo($format){
return '<abbr class="timeago" title="' . self::get(DateTime::RFC2822) . '">' . self::get($format) . '</abbr>';
public static function getTimeAgo($format = __SYM_TIME_FORMAT__){
$time = Widget::Time($string, $format);
return $time->generate();
}

public static function getTimezones(){
include_once(TEMPLATE . '/date.timezones.php'); // $timezones

return $timezones;
}

Expand Down
1 change: 1 addition & 0 deletions symphony/lib/toolkit/class.administrationpage.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ public function build(Array $context = array()){
$this->addScriptToHead(SYMPHONY_URL . '/assets/symphony.duplicator.js', 64);
$this->addScriptToHead(SYMPHONY_URL . '/assets/symphony.tags.js', 65);
$this->addScriptToHead(SYMPHONY_URL . '/assets/symphony.pickable.js', 66);
$this->addScriptToHead(SYMPHONY_URL . '/assets/symphony.timeago.js', 67);
$this->addScriptToHead(SYMPHONY_URL . '/assets/admin.js', 70);

$this->addElementToHead(
Expand Down