๐Ÿ“ฆ directus / v6-archive

๐Ÿ“„ date.js ยท 191 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191/* global _ */
define([
  'app',
  'underscore',
  'core/UIComponent',
  'core/UIView',
  'moment',
  'helpers/ui',
  'core/t'
], function (app, _, UIComponent, UIView, moment, UIHelper, __t) {
  'use strict';

  function removeTimeFromFormat(format) {
    return format.replace(/(A|a|H|h|m|s|S|z|Z|x|X)/g, '');
  }

  var dateFormat = 'YYYY-MM-DD';

  var Input = UIView.extend({
    template: 'datetime/input',

    events: {
      'blur input.date': 'updateValue',
      'blur input.time': 'updateValue',
      'change input.date': 'updateValue',
      'change input.time': 'updateValue',
      'click .now': 'makeNow'
    },

    getFormat: function () {
      return dateFormat;
    },

    unsavedChange: function () {
      // NOTE: Only set the new value (mark changed) if the value has changed
      if (this.value.isValid() && (this.model.isNew() || this.model.hasChanges(this.name))) {
        return this.value.format(this.getFormat());
      }
    },

    supportsTime: function (type) {
      type = type || this.columnSchema.get('type');

      return UIHelper.supportsTime(type);
    },

    makeNow: function () {
      this.value = moment();
      this.render();
      this.$('input.date, input.time').trigger('change');
    },

    getDate: function () {
      return {
        value: this.$('input[type=date]').val(),
        format: dateFormat
      };
    },

    updateValue: function () {
      var date = this.getDate();
      var value = date.value;
      var format = date.format;
      var newValue = '';

      if (moment(value).isValid()) {
        this.value = moment(value);
        newValue = this.value.format(format);
      }

      this.$('#' + this.name).val(newValue);
      this.model.set(this.name, newValue);
    },

    serialize: function () {
      var settings = this.options.settings;
      var isValid = this.value.isValid();
      var dateValue = isValid ? this.value.format(dateFormat) : null;

      return {
        hasValue: isValid,
        useDate: true,
        useTime: false,
        dateValue: dateValue,
        value: dateValue,
        name: this.name,
        readOnly: settings.get('read_only') || !this.options.canWrite
      };
    },

    initialize: function () {
      var value = this.model.get(this.name);
      var settings = this.options.settings;

      if (value === undefined) {
        value = moment('0000-00-00');
      } else {
        value = moment(value);
      }

      if (settings.get('auto_populate') === true && value.isValid() === false) {
        value = moment();
      }

      this.value = value;
    }
  });

  var Component = UIComponent.extend({
    id: 'date',
    dataTypes: ['DATE'],
    options: [
      {
        id: 'read_only',
        ui: 'toggle',
        type: 'Boolean',
        comment: 'Force this interface to be read only',
        default_value: false
      },
      {
        id: 'format',
        ui: 'text_input',
        type: 'String',
        comment: '<a href="http://momentjs.com/docs/#/displaying/format/" target="_blank">Formatting Rules</a>',
        char_length: 255,
        default_value: 'YYYY-MM-DD',
        options: {
          placeholder: 'eg: YYYY-MM-DD HH:mm:ss'
        }
      },
      {
        id: 'contextual_date_in_listview',
        type: 'Boolean',
        ui: 'toggle',
        comment: 'Show dates in relatively to now (eg: 3 days ago)',
        default_value: false
      },
      {
        id: 'auto_populate',
        type: 'Boolean',
        ui: 'toggle',
        comment: 'Automatically fill this field with the current date if it\'s empty',
        default_value: true
      }
    ],
    Input: Input,
    validate: function (value, interfaceOptions) {
      if (interfaceOptions.schema.isRequired() && _.isEmpty(value)) {
        return __t('this_field_is_required');
      }

      var date = moment(value);
      if (!value || date.isValid()) {
        return;
      }

      return 'Not a valid date';
    },
    list: function (interfaceOptions) {
      var value = interfaceOptions.value;
      var format = this.getFormat(interfaceOptions);

      if (interfaceOptions.settings.get('contextual_date_in_listview') === true) {
        var momentDate = moment(interfaceOptions.value);

        value = '-';

        // make sure the value is also valid
        // otherwise the date will be "now"
        if (interfaceOptions.value && momentDate.isValid()) {
          value = momentDate.fromNow();
        }
      } else if (format) {
        value = moment(value).format(format);
      }

      return value;
    },
    getFormat: function (interfaceOptions) {
      var format = interfaceOptions.settings.get('format');

      return removeTimeFromFormat(format);
    },
    sort: function (interfaceOptions) {
      return interfaceOptions.value;
    }
  });

  return Component;
});