common/corpus-common-addon/addon/components/doc-created.js
author ymh <ymh.work@gmail.com>
Sun, 27 Nov 2016 15:12:30 +0100
changeset 447 38d5789e30d0
parent 445 b1e5ad6b2a29
child 513 dad9471f0d63
permissions -rw-r--r--
Improce notice data display, correct m3.23
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
445
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
import Ember from 'ember';
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
import layout from '../templates/components/doc-created';
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
export default Ember.Component.extend({
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
  layout: layout,
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
  tagName: 'span',
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
  value: null,
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
  formatDate: function (dateStr, dateParsed) {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
    if (!dateStr) {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
      return Ember.String.htmlSafe('');
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
    }
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
    if (/^\d{4}$/.test(dateStr)) {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
      return Ember.String.htmlSafe(dateStr);
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
    }
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
    const date = dateParsed || new Date(dateStr);
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
    if (isNaN(date.getTime())) {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
      return Ember.String.htmlSafe('Date inconnue');
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    }
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    if (/^\d{4}-\d{2}$/.test(dateStr)) {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
      return Ember.String.htmlSafe((date.getMonth() + 1) + '-' + date.getFullYear());
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
    }
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
    return Ember.String.htmlSafe(date.getDate() + '-' + (date.getMonth() + 1) + '-' + date.getFullYear());
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
  },
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
  shortDateDate: Ember.computed('value', function () {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
    return this.formatDate(this.get('value'));
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
  }),
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
  periodMatches: Ember.computed('value', function () {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
    let dateStr = this.get('value');
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
    if (!dateStr) {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
      return null;
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
    }
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    dateStr = dateStr.trim();
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
    const m = dateStr.match(/^(\d{4})-(\d{4})$/) ||
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
      dateStr.match(/^start\s*=\s*([^\s]+)\s*;\s*end\s*=\s*([^\s]+)$/) ||
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
      dateStr.match(/^end\s*=\s*([^\s]+)\s*;\s*start\s*=\s*([^\s]+)$/);
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
    if (!m) {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
      return null;
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
    }
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    const [, dateStr1, dateStr2] = m;
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
    let date1 = new Date(dateStr1);
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
    let date2 = new Date(dateStr2);
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
    if (isNaN(date1.getTime()) || isNaN(date2.getTime())) {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
      return null;
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
    }
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
    if (date2 < date1) {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
      [date1, date2] = [date2, date1];
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
    }
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
    return {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
      start: {str: dateStr1, date: date1},
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
      end: {str: dateStr2, date: date2}
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
    };
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
  }),
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
  isPeriod: Ember.computed('periodMatches', function () {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
    const periodMatches = this.get('periodMatches');
447
38d5789e30d0 Improce notice data display, correct m3.23
ymh <ymh.work@gmail.com>
parents: 445
diff changeset
    66
445
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
    return Boolean(periodMatches);
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
  }),
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
  shortDateStart: Ember.computed('periodMatches', function () {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
    const periodMatches = this.get('periodMatches');
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
    if (!periodMatches) {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
      return null;
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
    }
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
    // from the periodMatches function we know that we have a valid start date
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
    return this.formatDate(periodMatches.start.str, periodMatches.start.date);
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
  }),
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
  shortDateEnd: Ember.computed('periodMatches', function () {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
    const periodMatches = this.get('periodMatches');
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
    if (!periodMatches) {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
      return null;
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
    }
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
    // from the periodMatches function we know that we have a valid end date
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
    return this.formatDate(periodMatches.end.str, periodMatches.end.date);
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
  })
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
});