123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- /**
- * @api
- */
- define([
- 'underscore',
- 'moment',
- 'uiLayout',
- 'Magento_Ui/js/grid/listing'
- ], function (_, moment, layout, Listing) {
- 'use strict';
- var ONE_DAY = 86400000;
- return Listing.extend({
- defaults: {
- recordTmpl: 'ui/timeline/record',
- dateFormat: 'YYYY-MM-DD HH:mm:ss',
- headerFormat: 'ddd MM/DD',
- detailsFormat: 'DD/MM/YYYY HH:mm:ss',
- scale: 7,
- scaleStep: 1,
- minScale: 7,
- maxScale: 28,
- minDays: 28,
- displayMode: 'timeline',
- displayModes: {
- timeline: {
- label: 'Timeline',
- value: 'timeline',
- template: 'ui/timeline/timeline'
- }
- },
- viewConfig: {
- component: 'Magento_Ui/js/timeline/timeline-view',
- name: '${ $.name }_view',
- model: '${ $.name }'
- },
- tracks: {
- scale: true
- },
- statefull: {
- scale: true
- },
- range: {}
- },
- /**
- * Initializes Timeline component.
- *
- * @returns {Timeline} Chainable.
- */
- initialize: function () {
- this._super()
- .initView()
- .updateRange();
- return this;
- },
- /**
- * Initializes components configuration.
- *
- * @returns {Timeline} Chainable.
- */
- initConfig: function () {
- this._super();
- this.maxScale = Math.min(this.minDays, this.maxScale);
- this.minScale = Math.min(this.maxScale, this.minScale);
- return this;
- },
- /**
- * Initializes observable properties.
- *
- * @returns {Timeline} Chainable.
- */
- initObservable: function () {
- this._super()
- .observe.call(this.range, true, 'hasToday');
- return this;
- },
- /**
- * Initializes TimelineView component.
- *
- * @returns {Timeline} Chainable.
- */
- initView: function () {
- layout([this.viewConfig]);
- return this;
- },
- /**
- * Checks if provided event record is active,
- * i.e. it has already started.
- *
- * @param {Object} record
- * @returns {Boolean}
- */
- isActive: function (record) {
- return Number(record.status) === 1;
- },
- /**
- * Checks if provided event record is upcoming,
- * i.e. it will start later on.
- *
- * @param {Object} record
- * @returns {Boolean}
- */
- isUpcoming: function (record) {
- return Number(record.status) === 2;
- },
- /**
- * Checks if provided event record is permanent,
- * i.e. it has no ending time.
- *
- * @param {Object} record
- * @returns {Boolean}
- */
- isPermanent: function (record) {
- return !this.getEndDate(record);
- },
- /**
- * Checks if provided date indicates current day.
- *
- * @param {(Number|Moment)} date
- * @returns {Boolenan}
- */
- isToday: function (date) {
- return moment().isSame(date, 'day');
- },
- /**
- * Checks if range object contains todays date.
- *
- * @returns {Boolean}
- */
- hasToday: function () {
- return this.range.hasToday;
- },
- /**
- * Returns start date of provided record.
- *
- * @param {Object} record
- * @returns {String}
- */
- getStartDate: function (record) {
- return record['start_time'];
- },
- /**
- * Returns end date of provided record.
- *
- * @param {Object} record
- * @returns {String}
- */
- getEndDate: function (record) {
- return record['end_time'];
- },
- /**
- * Returns difference in days between records' start date
- * and a first day of a range.
- *
- * @param {Object} record
- * @returns {Number}
- */
- getStartDelta: function (record) {
- var start = this.createDate(this.getStartDate(record)),
- firstDay = this.range.firstDay;
- return start.diff(firstDay, 'days', true);
- },
- /**
- * Calculates the amount of days that provided event lasts.
- *
- * @param {Object} record
- * @returns {Number}
- */
- getDaysLength: function (record) {
- var start = this.createDate(this.getStartDate(record)),
- end = this.createDate(this.getEndDate(record));
- if (!end.isValid()) {
- end = this.range.lastDay.endOf('day');
- }
- return end.diff(start, 'days', true);
- },
- /**
- * Creates new date object based on provided date string value.
- *
- * @param {String} dateStr
- * @returns {Moment}
- */
- createDate: function (dateStr) {
- return moment(dateStr, this.dateFormat);
- },
- /**
- * Converts days to weeks.
- *
- * @param {Number} days
- * @returns {Number}
- */
- daysToWeeks: function (days) {
- var weeks = days / 7;
- if (weeks % 1) {
- weeks = weeks.toFixed(1);
- }
- return weeks;
- },
- /**
- * Updates data of a range object,
- * e.g. total days, first day and last day, etc.
- *
- * @returns {Object} Range instance.
- */
- updateRange: function () {
- var firstDay = this._getFirstDay(),
- lastDay = this._getLastDay(),
- totalDays = lastDay.diff(firstDay, 'days'),
- days = [],
- i = -1;
- if (totalDays < this.minDays) {
- totalDays += this.minDays - totalDays - 1;
- }
- while (++i <= totalDays) {
- days.push(+firstDay + ONE_DAY * i);
- }
- return _.extend(this.range, {
- days: days,
- totalDays: totalDays,
- firstDay: firstDay,
- lastDay: moment(_.last(days)),
- hasToday: this.isToday(firstDay)
- });
- },
- /**
- *
- * @private
- * @param {String} key
- * @returns {Array<Moment>}
- */
- _getDates: function (key) {
- var dates = [];
- this.rows.forEach(function (record) {
- if (record[key]) {
- dates.push(this.createDate(record[key]));
- }
- }, this);
- return dates;
- },
- /**
- * Returns date which is closest to the current day.
- *
- * @private
- * @returns {Moment}
- */
- _getFirstDay: function () {
- var dates = this._getDates('start_time'),
- first = moment.min(dates).subtract(1, 'day'),
- today = moment();
- if (!first.isValid() || first < today) {
- first = today;
- }
- return first.startOf('day');
- },
- /**
- * Returns the most distant date
- * specified in available records.
- *
- * @private
- * @returns {Moment}
- */
- _getLastDay: function () {
- var startDates = this._getDates('start_time'),
- endDates = this._getDates('end_time'),
- last = moment.max(startDates.concat(endDates));
- return last.add(1, 'day').startOf('day');
- },
- /**
- * TODO: remove after integration with date binding.
- *
- * @param {Number} timestamp
- * @returns {String}
- */
- formatHeader: function (timestamp) {
- return moment(timestamp).format(this.headerFormat);
- },
- /**
- * TODO: remove after integration with date binding.
- *
- * @param {String} date
- * @returns {String}
- */
- formatDetails: function (date) {
- return moment(date).format(this.detailsFormat);
- }
- });
- });
|