Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

functions.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // @flow
  2. import moment from 'moment';
  3. // MomentJS uses static language bundle loading, so in order to support dynamic
  4. // language selection in the app we need to load all bundles that we support in
  5. // the app.
  6. // FIXME: If we decide to support MomentJS in other features as well we may need
  7. // to move this import and the lenient matcher to the i18n feature.
  8. require('moment/locale/bg');
  9. require('moment/locale/de');
  10. require('moment/locale/eo');
  11. require('moment/locale/es');
  12. require('moment/locale/fr');
  13. require('moment/locale/hy-am');
  14. require('moment/locale/it');
  15. require('moment/locale/nb');
  16. // OC is not available. Please submit OC translation to the MomentJS project.
  17. require('moment/locale/pl');
  18. require('moment/locale/pt');
  19. require('moment/locale/pt-br');
  20. require('moment/locale/ru');
  21. require('moment/locale/sk');
  22. require('moment/locale/sl');
  23. require('moment/locale/sv');
  24. require('moment/locale/tr');
  25. require('moment/locale/zh-cn');
  26. import { i18next } from '../base/i18n';
  27. import { parseURIString } from '../base/util';
  28. /**
  29. * Retrieves the recent room list and generates all the data needed to be
  30. * displayed.
  31. *
  32. * @param {Array<Object>} list - The stored recent list retrieved from redux.
  33. * @returns {Array}
  34. */
  35. export function getRecentRooms(list: Array<Object>): Array<Object> {
  36. const recentRoomDS = [];
  37. if (list.length) {
  38. // We init the locale on every list render, so then it changes
  39. // immediately if a language change happens in the app.
  40. const locale = _getSupportedLocale();
  41. for (const e of list) {
  42. const location = parseURIString(e.conference);
  43. if (location && location.room && location.hostname) {
  44. recentRoomDS.push({
  45. baseURL: `${location.protocol}//${location.host}`,
  46. conference: e.conference,
  47. conferenceDuration: e.conferenceDuration,
  48. conferenceDurationString:
  49. _getDurationString(
  50. e.conferenceDuration,
  51. locale),
  52. dateString: _getDateString(e.date, locale),
  53. dateTimeStamp: e.date,
  54. initials: _getInitials(location.room),
  55. room: location.room,
  56. serverName: location.hostname
  57. });
  58. }
  59. }
  60. }
  61. return recentRoomDS.reverse();
  62. }
  63. /**
  64. * Returns a well formatted date string to be displayed in the list.
  65. *
  66. * @param {number} dateTimeStamp - The UTC timestamp to be converted to String.
  67. * @param {string} locale - The locale to init the formatter with. Note: This
  68. * locale must be supported by the formatter so ensure this prerequisite before
  69. * invoking the function.
  70. * @private
  71. * @returns {string}
  72. */
  73. function _getDateString(dateTimeStamp: number, locale: string) {
  74. const date = new Date(dateTimeStamp);
  75. const m = _getLocalizedFormatter(date, locale);
  76. if (date.toDateString() === new Date().toDateString()) {
  77. // The date is today, we use fromNow format.
  78. return m.fromNow();
  79. }
  80. return m.format('lll');
  81. }
  82. /**
  83. * Returns a well formatted duration string to be displayed as the conference
  84. * length.
  85. *
  86. * @param {number} duration - The duration in MS.
  87. * @param {string} locale - The locale to init the formatter with. Note: This
  88. * locale must be supported by the formatter so ensure this prerequisite before
  89. * invoking the function.
  90. * @private
  91. * @returns {string}
  92. */
  93. function _getDurationString(duration: number, locale: string) {
  94. return _getLocalizedFormatter(duration, locale).humanize();
  95. }
  96. /**
  97. * Returns the initials supposed to be used based on the room name.
  98. *
  99. * @param {string} room - The room name.
  100. * @private
  101. * @returns {string}
  102. */
  103. function _getInitials(room: string) {
  104. return room && room.charAt(0) ? room.charAt(0).toUpperCase() : '?';
  105. }
  106. /**
  107. * Returns a localized date formatter initialized with a specific {@code Date}
  108. * or duration ({@code number}).
  109. *
  110. * @private
  111. * @param {Date | number} dateOrDuration - The date or duration to format.
  112. * @param {string} locale - The locale to init the formatter with. Note: The
  113. * specified locale must be supported by the formatter so ensure the
  114. * prerequisite is met before invoking the function.
  115. * @returns {Object}
  116. */
  117. function _getLocalizedFormatter(dateOrDuration: Date | number, locale: string) {
  118. const m
  119. = typeof dateOrDuration === 'number'
  120. ? moment.duration(dateOrDuration)
  121. : moment(dateOrDuration);
  122. return m.locale(locale);
  123. }
  124. /**
  125. * A lenient locale matcher to match language and dialect if possible.
  126. *
  127. * @private
  128. * @returns {string}
  129. */
  130. function _getSupportedLocale() {
  131. const i18nLocale = i18next.language;
  132. let supportedLocale;
  133. if (i18nLocale) {
  134. const localeRegexp = new RegExp('^([a-z]{2,2})(-)*([a-z]{2,2})*$');
  135. const localeResult = localeRegexp.exec(i18nLocale.toLowerCase());
  136. if (localeResult) {
  137. const currentLocaleRegexp
  138. = new RegExp(
  139. `^${localeResult[1]}(-)*${`(${localeResult[3]})*` || ''}`);
  140. supportedLocale
  141. = moment.locales().find(lang => currentLocaleRegexp.exec(lang));
  142. }
  143. }
  144. return supportedLocale || 'en';
  145. }