diff --git a/modules/backend/assets/js/october.datetime.js b/modules/backend/assets/js/october.datetime.js new file mode 100644 index 000000000..0bf33b976 --- /dev/null +++ b/modules/backend/assets/js/october.datetime.js @@ -0,0 +1,166 @@ +/* + * Date time converter. + * See moment.js for format options. + * http://momentjs.com/docs/#/displaying/format/ + * + * Usage: + * + * + * + * Alias options: + * + * time -> 6:28 AM + * timeLong -> 6:28:01 AM + * date -> 04/23/2016 + * dateMin -> 4/23/2016 + * dateLong -> April 23, 2016 + * dateLongMin -> Apr 23, 2016 + * dateTime -> April 23, 2016 6:28 AM + * dateTimeMin -> Apr 23, 2016 6:28 AM + * dateTimeLong -> Saturday, April 23, 2016 6:28 AM + * dateTimeLongMin -> Sat, Apr 23, 2016 6:29 AM + * + */ ++function ($) { "use strict"; + var Base = $.oc.foundation.base, + BaseProto = Base.prototype + + var DateTimeConverter = function (element, options) { + this.$el = $(element) + this.options = options || {} + + $.oc.foundation.controlUtils.markDisposable(element) + Base.call(this) + this.init() + } + + DateTimeConverter.prototype = Object.create(BaseProto) + DateTimeConverter.prototype.constructor = DateTimeConverter + + DateTimeConverter.prototype.init = function() { + + this.initDefaults() + + this.$el.text(this.getDateTimeValue()) + + this.$el.one('dispose-control', this.proxy(this.dispose)) + } + + DateTimeConverter.prototype.initDefaults = function() { + if (!this.options.timezone) { + this.options.timezone = $('meta[name="backend-timezone"]').attr('content') + } + + if (!this.options.locale) { + this.options.locale = $('meta[name="backend-locale"]').attr('content') + } + + if (!this.options.format) { + this.options.format = 'llll' + } + + if (this.options.formatAlias) { + this.options.format = this.getFormatFromAlias(this.options.formatAlias) + } + } + + DateTimeConverter.prototype.getDateTimeValue = function() { + this.datetime = this.$el.attr('datetime') + var momentObj = moment(this.datetime), + result + + if (this.options.locale) { + moment.locale(this.options.locale) + } + + if (this.options.timezone) { + momentObj = momentObj.tz(this.options.timezone) + } + + if (this.options.timeSince) { + result = momentObj.fromNow() + } + else if (this.options.timeTense) { + result = momentObj.calendar() + } + else { + result = momentObj.format(this.options.format) + } + + return result + } + + DateTimeConverter.prototype.getFormatFromAlias = function(alias) { + var map = { + time: 'LT', + timeLong: 'LTS', + date: 'L', + dateMin: 'l', + dateLong: 'LL', + dateLongMin: 'll', + dateTime: 'LLL', + dateTimeMin: 'lll', + dateTimeLong: 'LLLL', + dateTimeLongMin: 'llll' + } + + return map[alias] ? map[alias] : 'llll' + } + + DateTimeConverter.prototype.dispose = function() { + this.$el.off('dispose-control', this.proxy(this.dispose)) + this.$el.removeData('oc.dateTimeConverter') + + this.$el = null + this.options = null + + BaseProto.dispose.call(this) + } + + DateTimeConverter.DEFAULTS = { + format: null, + formatAlias: null, + timezone: null, + locale: null, + timeTense: false, + timeSince: false + } + + // PLUGIN DEFINITION + // ============================ + + var old = $.fn.dateTimeConverter + + $.fn.dateTimeConverter = function (option) { + var args = Array.prototype.slice.call(arguments, 1), items, result + + items = this.each(function () { + var $this = $(this) + var data = $this.data('oc.dateTimeConverter') + var options = $.extend({}, DateTimeConverter.DEFAULTS, $this.data(), typeof option == 'object' && option) + if (!data) $this.data('oc.dateTimeConverter', (data = new DateTimeConverter(this, options))) + if (typeof option == 'string') result = data[option].apply(data, args) + if (typeof result != 'undefined') return false + }) + + return result ? result : items + } + + $.fn.dateTimeConverter.Constructor = DateTimeConverter + + $.fn.dateTimeConverter.noConflict = function () { + $.fn.dateTimeConverter = old + return this + } + + // Add this only if required + $(document).render(function (){ + $('time[data-datetime-control]').dateTimeConverter() + }) + +}(window.jQuery); diff --git a/modules/backend/helpers/Backend.php b/modules/backend/helpers/Backend.php index 7f4a66106..4b49516d1 100644 --- a/modules/backend/helpers/Backend.php +++ b/modules/backend/helpers/Backend.php @@ -1,10 +1,12 @@ uri() . '/' . $path, $status, $headers, $secure); } + + /** + * Returns the HTML for a date formatted in the backend. + */ + public function dateTime($dateTime, $value = '', $options = []) + { + extract(array_merge([ + 'format' => null, + 'formatAlias' => null, + 'jsFormat' => null, + 'timeTense' => false, + 'timeSince' => false, + ], $options)); + + $carbon = DateTimeHelper::makeCarbon($dateTime); + + if ($jsFormat !== null) { + $format = $jsFormat; + } + else { + $format = DateTimeHelper::momentFormat($format); + } + + $attributes = [ + 'datetime' => $carbon, + 'data-datetime-control' => 1, + ]; + + if ($timeTense) { + $attributes['data-time-tense'] = 1; + } + elseif ($timeSince) { + $attributes['data-time-since'] = 1; + } + elseif ($format) { + $attributes['data-format'] = $format; + } + elseif ($formatAlias) { + $attributes['data-format-alias'] = $formatAlias; + } + + return ''.PHP_EOL; + } + } diff --git a/modules/backend/layouts/_head.htm b/modules/backend/layouts/_head.htm index d11d5837b..c2be8adcf 100644 --- a/modules/backend/layouts/_head.htm +++ b/modules/backend/layouts/_head.htm @@ -2,6 +2,8 @@ + +