From f062b052f54ae4b32d229cbe9b87f8c30cdf8c98 Mon Sep 17 00:00:00 2001 From: Nathan Phillip Brink Date: Tue, 19 Apr 2011 23:00:00 -0400 Subject: [PATCH] Add an option `use_html_attribute' which can be set to false to disable creating and trying to access a data-cutetime attribute. Most uses of cuteTime do not need to use the extra attribute. jQuery's .data() function is intended for storing information such as the original time and works perfectly well with cuteTime. --- js/jquery.cuteTime.js | 27 ++++++++++++++++++++++----- js/jquery.cuteTime.min.js | 6 +++--- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/js/jquery.cuteTime.js b/js/jquery.cuteTime.js index dbd514e..3c76426 100644 --- a/js/jquery.cuteTime.js +++ b/js/jquery.cuteTime.js @@ -74,6 +74,9 @@ If neither cutetime attibute nor valid object text exist then the timestamp is assumed to by 'now'. + However, CuteTime can operate without using the data-timestamp attribute (and by instead + using jQuery().data()). + stop_cuteness() stops all automatic updates of refresh enabled timestamps @@ -94,6 +97,8 @@ e.g. $('.ts2').cuteTime({ refresh: 60000 }); refresh: time in milliseconds before next refresh of page data; -1 == no refresh + use_html_attribute: Whether the data-cutetime HTML attribute should be used. Defaults to true. + Set to false to disable this behavior and use jQuery().data() instead. time_ranges: array of bound_structures definining the cute descriptions associated with time ranges @@ -277,6 +282,7 @@ **********************************************************************************/ $.fn.cuteTime.settings = { refresh: -1, // time in milliseconds before next refresh of page data; -1 == no refresh + use_html_attribute: true, // Whether or not to set/access the data-cutetime attribute. time_ranges: [ {bound: NEG_INF, // IMPORANT: bounds MUST be in ascending order, from negative infinity to positive infinity cuteness: 'the future!', unit_size: 0}, @@ -604,9 +610,10 @@ get_time_value DESCRIPTION - get the time value specified either in the text or the cuteime attribute + get the time value specified either in the text or the cutetime attribute of the object and update the cutetime attribute whether initially present - or not + or not. Or, if options.use_html_attribute is set to false, use jQuery().data() + to store/access the value. If the cutetime attribute already exists within the provided object, then the text within the object is ignored in the cutification @@ -658,7 +665,13 @@ **********************************************************************************/ function get_cutetime_attr(obj) { - var return_value = obj.attr(TS_ATTR); + var use_html_attribute = $.fn.cuteTime.c_settings.use_html_attribute; + var return_value = null; + + if (use_html_attribute) + return_value = obj.attr(TS_ATTR); + else + return_value = obj.data('cutetime'); if (return_value != undefined) { return return_value; @@ -681,8 +694,12 @@ **********************************************************************************/ function set_cutetime_attr(attr, obj) { - // assume valid attr(ibute) value - obj.attr(TS_ATTR, attr); + var use_html_attribute = $.fn.cuteTime.c_settings.use_html_attribute; + if (use_html_attribute) + // assume valid attr(ibute) value + obj.attr(TS_ATTR, attr); + else + obj.data('cutetime', attr); } diff --git a/js/jquery.cuteTime.min.js b/js/jquery.cuteTime.min.js index facc1e2..afc41ac 100644 --- a/js/jquery.cuteTime.min.js +++ b/js/jquery.cuteTime.min.js @@ -1,3 +1,3 @@ -/* jQuery.CuteTime v 1.1.3 (4/6/2010) http://tpgblog.com/cutetime/ -compiled by http://yui.2clics.net/ */ -(function(f){var b=Number.NEGATIVE_INFINITY;var d=Number.POSITIVE_INFINITY;var a="data-timestamp";f.fn.cuteTime=function(n){var m=new Date().getTime();var o;var p;if((typeof n=="object")||(n==undefined)){f.fn.cuteTime.c_settings=f.extend({},f.fn.cuteTime.settings,n);f.fn.cuteTime.the_selected=this;this.each(function(){p=f(this);o=j(p);p.html(c(m-o))});f.fn.cuteTime.start_cuteness()}return this};f.cuteTime=function(n,q){var m=new Date().getTime();var p;var r;var o=null;if(typeof n=="object"){f.fn.cuteTime.c_settings=f.extend({},f.fn.cuteTime.settings,n)}if(typeof n=="string"){o=n}else{if(typeof q=="string"){o=q}}if(o!=null){p=l(o);if(!isNaN(p)){return c(m-p)}else{return"INVALID_DATETIME_FORMAT"}}return this};f.fn.cuteTime.settings={refresh:-1,time_ranges:[{bound:b,cuteness:"the future!",unit_size:0},{bound:0,cuteness:"just now",unit_size:0},{bound:20*1000,cuteness:"a few seconds ago",unit_size:0},{bound:60*1000,cuteness:"a minute ago",unit_size:0},{bound:60*1000*2,cuteness:" minutes ago",unit_size:60*1000},{bound:60*1000*60,cuteness:"an hour ago",unit_size:0},{bound:60*1000*60*2,cuteness:" hours ago",unit_size:60*1000*60},{bound:60*1000*60*24,cuteness:"yesterday",unit_size:0},{bound:60*1000*60*24*2,cuteness:" days ago",unit_size:60*1000*60*24},{bound:60*1000*60*24*30,cuteness:"last month",unit_size:0},{bound:60*1000*60*24*30*2,cuteness:" months ago",unit_size:60*1000*60*24*30},{bound:60*1000*60*24*30*12,cuteness:"last year",unit_size:0},{bound:60*1000*60*24*30*12*2,cuteness:" years ago",unit_size:60*1000*60*24*30*12},{bound:d,cuteness:"a blinkle ago",unit_size:0}]};f.fn.cuteTime.start_cuteness=function(){var m=f.fn.cuteTime.c_settings.refresh;if(f.fn.cuteTime.process_tracker==null){if(m>0){f.fn.cuteTime.process_tracker=setInterval("$.fn.cuteTime.update_cuteness()",m)}}else{}return this};f.fn.cuteTime.update_cuteness=function(){var m=new Date().getTime();var o;var n;f.fn.cuteTime.the_selected.each(function(){o=f(this);n=j(o);o.html(c(m-n))})};f.fn.cuteTime.stop_cuteness=function(){if(f.fn.cuteTime.process_tracker!=null){clearInterval(f.fn.cuteTime.process_tracker);f.fn.cuteTime.process_tracker=null}else{}return this};function c(q){var o=f.fn.cuteTime.c_settings.time_ranges;var n,p;var m="";jQuery.each(o,function(r,s){if(r=s.bound)&&(q0){p=Math.floor(q/s.unit_size)}else{p=""}n=s.cuteness.replace(/%CT%/,p);if(n==s.cuteness){m=p+s.cuteness}else{m=n}return false}}else{return false}});if(m==""){m="2 pookies ago"}return m}function l(n){var m;if((new_date=g(n))!=null){m=new_date.valueOf()}else{m=(new Date(n)).valueOf();if(isNaN(m)){m=new Date(n.replace(/-/g," "))}}return m}function g(q){var m=q.match(/^(\d{4})((-(\d{2})(-(\d{2})(T(\d{2}):(\d{2})(:(\d{2})(.(\d+))?)?(Z|(([+-])((\d{2}):(\d{2})))))?)?)?)$/);if(m!=null){var n=new Date();var p=0;var o=0;n.setUTCFullYear(m[1]);if(!e(m[4])){n.setUTCMonth(m[4]-1);if(!e(m[6])){n.setUTCDate(m[6]);if(!e(m[16])){p=m[18];o=m[19];if(m[16]=="-"){p*=-1;o*=-1}}if(!e(m[8])){n.setUTCHours(m[8]-p);n.setUTCMinutes(m[9]-o);if(!e(m[11])){n.setUTCSeconds(m[11]);if(!e(m[13])){n.setUTCMilliseconds(m[13]*1000)}}}}}return n}else{return null}}function e(m){if(null==m||""==m){return true}return false}function j(o){var n=Number.NaN;var m=h(o);if(m!=null){n=l(m)}if(isNaN(n)){m=i(o);if(m!=null){n=l(m)}}if(isNaN(n)){m=new Date().toString();n=l(m)}k(m,o);return n}function h(n){var m=n.attr(a);if(m!=undefined){return m}else{return null}}function k(m,n){n.attr(a,m)}function i(m){return m.text()}})(jQuery); \ No newline at end of file +/* jQuery.CuteTime v 1.1.3 (4/6/2010) http://tpgblog.com/cutetime/ +compiled by http://yui.2clics.net/ */ +(function(f){var b=Number.NEGATIVE_INFINITY;var d=Number.POSITIVE_INFINITY;var a="data-timestamp";f.fn.cuteTime=function(n){var m=new Date().getTime();var o;var p;if((typeof n=="object")||(n==undefined)){f.fn.cuteTime.c_settings=f.extend({},f.fn.cuteTime.settings,n);f.fn.cuteTime.the_selected=this;this.each(function(){p=f(this);o=j(p);p.html(c(m-o))});f.fn.cuteTime.start_cuteness()}return this};f.cuteTime=function(n,q){var m=new Date().getTime();var p;var r;var o=null;if(typeof n=="object"){f.fn.cuteTime.c_settings=f.extend({},f.fn.cuteTime.settings,n)}if(typeof n=="string"){o=n}else{if(typeof q=="string"){o=q}}if(o!=null){p=l(o);if(!isNaN(p)){return c(m-p)}else{return"INVALID_DATETIME_FORMAT"}}return this};f.fn.cuteTime.settings={refresh:-1,use_html_attribute:true,time_ranges:[{bound:b,cuteness:"the future!",unit_size:0},{bound:0,cuteness:"just now",unit_size:0},{bound:20*1000,cuteness:"a few seconds ago",unit_size:0},{bound:60*1000,cuteness:"a minute ago",unit_size:0},{bound:60*1000*2,cuteness:" minutes ago",unit_size:60*1000},{bound:60*1000*60,cuteness:"an hour ago",unit_size:0},{bound:60*1000*60*2,cuteness:" hours ago",unit_size:60*1000*60},{bound:60*1000*60*24,cuteness:"yesterday",unit_size:0},{bound:60*1000*60*24*2,cuteness:" days ago",unit_size:60*1000*60*24},{bound:60*1000*60*24*30,cuteness:"last month",unit_size:0},{bound:60*1000*60*24*30*2,cuteness:" months ago",unit_size:60*1000*60*24*30},{bound:60*1000*60*24*30*12,cuteness:"last year",unit_size:0},{bound:60*1000*60*24*30*12*2,cuteness:" years ago",unit_size:60*1000*60*24*30*12},{bound:d,cuteness:"a blinkle ago",unit_size:0}]};f.fn.cuteTime.start_cuteness=function(){var m=f.fn.cuteTime.c_settings.refresh;if(f.fn.cuteTime.process_tracker==null){if(m>0){f.fn.cuteTime.process_tracker=setInterval("$.fn.cuteTime.update_cuteness()",m)}}else{}return this};f.fn.cuteTime.update_cuteness=function(){var m=new Date().getTime();var o;var n;f.fn.cuteTime.the_selected.each(function(){o=f(this);n=j(o);o.html(c(m-n))})};f.fn.cuteTime.stop_cuteness=function(){if(f.fn.cuteTime.process_tracker!=null){clearInterval(f.fn.cuteTime.process_tracker);f.fn.cuteTime.process_tracker=null}else{}return this};function c(q){var o=f.fn.cuteTime.c_settings.time_ranges;var n,p;var m="";jQuery.each(o,function(r,s){if(r=s.bound)&&(q0){p=Math.floor(q/s.unit_size)}else{p=""}n=s.cuteness.replace(/%CT%/,p);if(n==s.cuteness){m=p+s.cuteness}else{m=n}return false}}else{return false}});if(m==""){m="2 pookies ago"}return m}function l(n){var m;if((new_date=g(n))!=null){m=new_date.valueOf()}else{m=(new Date(n)).valueOf();if(isNaN(m)){m=new Date(n.replace(/-/g," "))}}return m}function g(q){var m=q.match(/^(\d{4})((-(\d{2})(-(\d{2})(T(\d{2}):(\d{2})(:(\d{2})(.(\d+))?)?(Z|(([+-])((\d{2}):(\d{2})))))?)?)?)$/);if(m!=null){var n=new Date();var p=0;var o=0;n.setUTCFullYear(m[1]);if(!e(m[4])){n.setUTCMonth(m[4]-1);if(!e(m[6])){n.setUTCDate(m[6]);if(!e(m[16])){p=m[18];o=m[19];if(m[16]=="-"){p*=-1;o*=-1}}if(!e(m[8])){n.setUTCHours(m[8]-p);n.setUTCMinutes(m[9]-o);if(!e(m[11])){n.setUTCSeconds(m[11]);if(!e(m[13])){n.setUTCMilliseconds(m[13]*1000)}}}}}return n}else{return null}}function e(m){if(null==m||""==m){return true}return false}function j(o){var n=Number.NaN;var m=h(o);if(m!=null){n=l(m)}if(isNaN(n)){m=i(o);if(m!=null){n=l(m)}}if(isNaN(n)){m=new Date().toString();n=l(m)}k(m,o);return n}function h(o){var m=f.fn.cuteTime.c_settings.use_html_attribute;var n=null;if(m){n=o.attr(a)}else{n=o.data("cutetime")}if(n!=undefined){return n}else{return null}}function k(n,o){var m=f.fn.cuteTime.c_settings.use_html_attribute;if(m){o.attr(a,n)}else{o.data("cutetime",n)}}function i(m){return m.text()}})(jQuery);