common/corpus-common-addon/addon/components/doc-created.js
author ymh <ymh.work@gmail.com>
Thu, 24 Nov 2016 19:08:36 +0100
changeset 446 2a93eb9f38f5
parent 445 b1e5ad6b2a29
child 447 38d5789e30d0
permissions -rw-r--r--
pb m3.22 - set correct filter when clicking on subject and language in notice
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
    console.log('SHORT DATE', this.get('value'));
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
    return this.formatDate(this.get('value'));
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
  }),
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
  periodMatches: Ember.computed('value', function () {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    let dateStr = this.get('value');
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
    console.log('PERIOD MATCH', dateStr);
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
    if (!dateStr) {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
      return null;
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
    }
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
    dateStr = dateStr.trim();
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
    const m = dateStr.match(/^(\d{4})-(\d{4})$/) ||
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
      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
    44
      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
    45
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
    if (!m) {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
      return null;
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
    }
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
    const [, dateStr1, dateStr2] = m;
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
    let date1 = new Date(dateStr1);
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
    let date2 = new Date(dateStr2);
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 (isNaN(date1.getTime()) || isNaN(date2.getTime())) {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
      return null;
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
    if (date2 < date1) {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
      [date1, date2] = [date2, date1];
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
    }
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
    return {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
      start: {str: dateStr1, date: date1},
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
      end: {str: dateStr2, date: date2}
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
    };
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
  }),
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
  isPeriod: Ember.computed('periodMatches', function () {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
    const periodMatches = this.get('periodMatches');
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
    console.log('ISPERIOD MATCH', periodMatches, Boolean(periodMatches));
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
    return Boolean(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
  shortDateStart: Ember.computed('periodMatches', function () {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
    const periodMatches = this.get('periodMatches');
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
    if (!periodMatches) {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
      return null;
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
    }
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
    // 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
    80
    return this.formatDate(periodMatches.start.str, periodMatches.start.date);
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
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
  shortDateEnd: Ember.computed('periodMatches', function () {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
    const periodMatches = this.get('periodMatches');
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
    if (!periodMatches) {
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
      return null;
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
    }
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
    // 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
    91
    return this.formatDate(periodMatches.end.str, periodMatches.end.date);
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
  })
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
b1e5ad6b2a29 Display created date instead of issued
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
});