ui.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. // date picker timezone conversion for display
  2. QUnit.test("date picker", assert => {
  3. Date.prototype.getTimezoneOffset2 = Date.prototype.getTimezoneOffset
  4. Date.prototype.getTimezoneOffset = function() { return -360 }
  5. obj_date_time = {
  6. name: 'test',
  7. value: '2018-04-06T20:45:00.000Z'
  8. }
  9. el_date_time = App.UiElement.datetime.render(obj_date_time)
  10. date_time_parsed = new Date(Date.parse(obj_date_time.value))
  11. date_time_input = el_date_time.find('.js-datepicker').datepicker('getDate')
  12. assert.equal(date_time_parsed.getDate(), date_time_input.getDate(), 'datetime matching day')
  13. obj_date = {
  14. name: 'test',
  15. value: '2018-06-06'
  16. }
  17. el_date = App.UiElement.date.render(obj_date)
  18. date_parsed = new Date(Date.parse(obj_date.value))
  19. date_input = el_date.find('.js-datepicker').datepicker('getUTCDate')
  20. assert.equal(date_parsed.getDate(), date_input.getDate(), 'date matching day')
  21. Date.prototype.getTimezoneOffset = Date.prototype.getTimezoneOffset2
  22. Date.prototype.getTimezoneOffset2 = undefined
  23. })
  24. // pretty date
  25. QUnit.test("check pretty date", assert => {
  26. var current = new Date('1995-12-17T03:24:00')
  27. clock = sinon.useFakeTimers({now: current})
  28. // use date formatting as functions to make it more flexible
  29. prettyDateRelative(current, '', true, 'relative');
  30. prettyDateAbsolute(current, '', true, 'absolute');
  31. prettyDateTimestamp(current, '', true, 'timestamp');
  32. // past
  33. function prettyDateRelative(current, escalation, long, type) {
  34. var result = App.PrettyDate.humanTime(current, escalation, long, type);
  35. assert.equal(result, 'just now', 'just now')
  36. result = App.PrettyDate.humanTime(current - 15000, escalation, long, type);
  37. assert.equal(result, 'just now', 'just now')
  38. result = App.PrettyDate.humanTime(current - 60000, escalation, long, type);
  39. assert.equal(result, '1 minute ago', '1 min ago')
  40. result = App.PrettyDate.humanTime(current - (2 * 60000), escalation, long, type);
  41. assert.equal(result, '2 minutes ago', '2 min ago')
  42. result = App.PrettyDate.humanTime(current - (60000 * 60), escalation, long, type);
  43. assert.equal(result, '1 hour ago', '1 hour')
  44. result = App.PrettyDate.humanTime(current - (60000 * 60 * 2), escalation, long, type);
  45. assert.equal(result, '2 hours ago', '2 hours')
  46. result = App.PrettyDate.humanTime(current - (60000 * 60 * 2.5), escalation, long, type);
  47. assert.equal(result, '2 hours 30 minutes ago', '2.5 hours')
  48. result = App.PrettyDate.humanTime(current - (60000 * 60 * 12.5), escalation, long, type);
  49. assert.equal(result, '12 hours ago', '12.5 hours')
  50. result = App.PrettyDate.humanTime(current - (60000 * 60 * 24), escalation, long, type);
  51. assert.equal(result, '1 day ago', '1 day')
  52. result = App.PrettyDate.humanTime(current - (60000 * 60 * 24 * 2), escalation, long, type);
  53. assert.equal(result, '2 days ago', '2 days')
  54. result = App.PrettyDate.humanTime(current - (60000 * 60 * 24 * 2) - (60000 * 5), escalation, long, type);
  55. assert.equal(result, '2 days ago', '2 days')
  56. result = App.PrettyDate.humanTime(current - (60000 * 60 * 24 * 2.5), escalation, long, type);
  57. assert.equal(result, '2 days 12 hours ago', '2.5 days')
  58. result = App.PrettyDate.humanTime(current - (60000 * 60 * 24 * 2.5) - (60000 * 5), escalation, long, type);
  59. assert.equal(result, '2 days 12 hours ago', '2.5 days')
  60. result = App.PrettyDate.humanTime(current - (60000 * 60 * 24 * 10.5), escalation, long, type);
  61. var pastDate = new Date(current - (60000 * 60 * 24 * 10.5))
  62. var dd = pastDate.getDate();
  63. if(dd<10) {
  64. dd = '0' + dd
  65. }
  66. var mm = pastDate.getMonth() + 1;
  67. if(mm<10) {
  68. mm = '0' + mm
  69. }
  70. var yyyy = pastDate.getFullYear();
  71. // mm/dd/yyyy
  72. assert.equal(result, mm+'/'+dd+'/'+yyyy, '10.5 days')
  73. result = App.PrettyDate.humanTime(current - (60000 * 60 * 24 * 30), escalation, long, type);
  74. var pastDate = new Date(current - (60000 * 60 * 24 * 30))
  75. var dd = pastDate.getDate();
  76. if(dd<10) {
  77. dd = '0' + dd
  78. }
  79. var mm = pastDate.getMonth() + 1;
  80. if(mm<10) {
  81. mm = '0' + mm
  82. }
  83. var yyyy = pastDate.getFullYear();
  84. // mm/dd/yyyy
  85. assert.equal(result, mm+'/'+dd+'/'+yyyy, '30 days')
  86. // future
  87. current = new Date('1995-12-17T03:24:00')
  88. result = App.PrettyDate.humanTime(current, escalation, long, type);
  89. assert.equal(result, 'just now', 'just now')
  90. result = App.PrettyDate.humanTime(current.getTime() + 55000, escalation, long, type);
  91. assert.equal(result, 'just now', 'just now')
  92. result = App.PrettyDate.humanTime(current.getTime() + 65000, escalation, long, type);
  93. assert.equal(result, 'in 1 minute', 'in 1 min')
  94. result = App.PrettyDate.humanTime(current.getTime() + (2 * 65000), escalation, long, type);
  95. assert.equal(result, 'in 2 minutes', 'in 2 min')
  96. result = App.PrettyDate.humanTime(current.getTime() + (60500 * 60), escalation, long, type);
  97. assert.equal(result, 'in 1 hour', 'in 1 hour')
  98. result = App.PrettyDate.humanTime(current.getTime() + (60050 * 60 * 2), escalation, long, type);
  99. assert.equal(result, 'in 2 hours', 'in 2 hours')
  100. result = App.PrettyDate.humanTime(current.getTime() + (60050 * 60 * 2.5), escalation, long, type);
  101. assert.equal(result, 'in 2 hours 30 minutes', 'in 2.5 hours')
  102. result = App.PrettyDate.humanTime(current.getTime() + (60050 * 60 * 24), escalation, long, type) ;
  103. assert.equal(result, 'in 1 day', 'in 1 day')
  104. result = App.PrettyDate.humanTime(current.getTime() + (60050 * 60 * 24 * 2), escalation, long, type);
  105. assert.equal(result, 'in 2 days', 'in 2 days')
  106. result = App.PrettyDate.humanTime(current.getTime() + (60050 * 60 * 24 * 2.5), escalation, long, type);
  107. assert.equal(result, 'in 2 days 12 hours', 'in 2.5 days')
  108. result = App.PrettyDate.humanTime(current.getTime() + (60050 * 60 * 24 * 5.5), escalation, long, type);
  109. assert.equal(result, 'in 5 days 12 hours', 'in 30.5 days')
  110. result = App.PrettyDate.humanTime(current.getTime() + (60050 * 60 * 24 * 30.5), escalation, long, type);
  111. assert.equal(result, 'in 30 days', 'in 30.5 days')
  112. };
  113. function prettyDateAbsolute(current, escalation, long, type) {
  114. var result = App.PrettyDate.humanTime(current, escalation, long, type);
  115. assert.equal(result, 'just now', 'just now') // by defaul < 1 min is just now
  116. result = App.PrettyDate.humanTime(current - 15000, escalation, long, type);
  117. assert.equal(result, 'just now', 'just now') // by default < 1 min is just now
  118. result = App.PrettyDate.humanTime(current - 60000, escalation, long, type);
  119. diff = 60
  120. assert.equal(result, getAbsolute(new Date(current - 60000), diff), '1 min ago')
  121. result = App.PrettyDate.humanTime(current - (2 * 60000), escalation, long, type);
  122. diff = 2 * 60
  123. assert.equal(result, getAbsolute(new Date(current - (2 * 60000)), diff), '2 min ago')
  124. result = App.PrettyDate.humanTime(current - (60000 * 60), escalation, long, type);
  125. diff = 60 * 60
  126. assert.equal(result, getAbsolute(new Date(current - (60000 * 60)), diff), '1 hour')
  127. result = App.PrettyDate.humanTime(current - (60000 * 60 * 2), escalation, long, type);
  128. diff = 60 * 60 * 2
  129. assert.equal(result, getAbsolute(new Date(current - (60000 * 60 * 2)), diff), '2 hours')
  130. result = App.PrettyDate.humanTime(current - (60000 * 60 * 2.5), escalation, long, type);
  131. diff = 60 * 60 * 2.5
  132. assert.equal(result, getAbsolute(new Date(current - (60000 * 60 * 2.5)), diff), '2.5 hours')
  133. result = App.PrettyDate.humanTime(current - (60000 * 60 * 12.5), escalation, long, type);
  134. diff = 60 * 60 * 12.5
  135. assert.equal(result, getAbsolute(new Date(current - (60000 * 60 * 12.5)), diff), '12.5 hours')
  136. result = App.PrettyDate.humanTime(current - (60000 * 60 * 24), escalation, long, type);
  137. diff = 60 * 60 * 25
  138. assert.equal(result, getAbsolute(new Date(current - (60000 * 60 * 24)), diff), '1 day')
  139. result = App.PrettyDate.humanTime(current - (60000 * 60 * 24 * 2), escalation, long, type);
  140. diff = 60 * 60 * 25 * 2
  141. assert.equal(result, getAbsolute(new Date(current - (60000 * 60 * 24 * 2)), diff), '2 days')
  142. result = App.PrettyDate.humanTime(current - (60000 * 60 * 24 * 2) - (60000 * 5), escalation, long, type);
  143. diff = (60 * 60 * 24 * 2) - (60 * 5)
  144. assert.equal(result, getAbsolute(new Date(current - (60000 * 60 * 24 * 2) - (60000 * 5)), diff), '2 days')
  145. result = App.PrettyDate.humanTime(current - (60000 * 60 * 24 * 2.5), escalation, long, type);
  146. diff = (60 * 60 * 24 * 2.5)
  147. assert.equal(result, getAbsolute(new Date(current - (60000 * 60 * 24 * 2.5)), diff), '2.5 days')
  148. result = App.PrettyDate.humanTime(current - (60000 * 60 * 24 * 2.5) - (60000 * 5), escalation, long, type);
  149. diff = (60 * 60 * 24 * 2.5) - (60 * 5)
  150. assert.equal(result, getAbsolute(new Date(current - (60000 * 60 * 24 * 2.5) - (60000 * 5)), diff), '2.5 days')
  151. // future
  152. current = new Date('1995-12-17T03:24:00')
  153. result = App.PrettyDate.humanTime(current, escalation, long, type);
  154. assert.equal(result, 'just now', 'just now') // no change, because < 1 min = just now
  155. result = App.PrettyDate.humanTime(current.getTime() + 55000, escalation, long, type);
  156. assert.equal(result, 'just now', 'just now') // no change, because < 1 min = just now
  157. result = App.PrettyDate.humanTime(current.getTime() + 65000, escalation, long, type);
  158. diff = 60
  159. assert.equal(result, getAbsolute(new Date(current.getTime() + 65000), diff), 'in 1 min')
  160. result = App.PrettyDate.humanTime(current.getTime() + (2 * 65000), escalation, long, type);
  161. diff = 2 * 60
  162. assert.equal(result, getAbsolute(new Date(current.getTime() + (2 * 65000)), diff), 'in 2 min')
  163. result = App.PrettyDate.humanTime(current.getTime() + (60500 * 60), escalation, long, type) ;
  164. diff = 60 * 60
  165. assert.equal(result, getAbsolute(new Date(current.getTime() + (60500 * 60)), diff), 'in 1 hour')
  166. result = App.PrettyDate.humanTime(current.getTime() + (60050 * 60 * 2), escalation, long, type);
  167. diff = 60 * 60 * 2
  168. assert.equal(result, getAbsolute(new Date(current.getTime() + (60050 * 60 * 2)), diff), 'in 2 hours')
  169. result = App.PrettyDate.humanTime(current.getTime() + (60050 * 60 * 2.5), escalation, long, type);
  170. diff = 60 * 60 * 2.5
  171. assert.equal(result, getAbsolute(new Date(current.getTime() + (60050 * 60 * 2.5)), diff), 'in 2.5 hours')
  172. result = App.PrettyDate.humanTime(current.getTime() + (60050 * 60 * 24), escalation, long, type);
  173. diff = 60 * 60 * 24
  174. assert.equal(result, getAbsolute(new Date(current.getTime() + (60050 * 60 * 24)), diff), 'in 1 day')
  175. result = App.PrettyDate.humanTime(current.getTime() + (60050 * 60 * 24 * 2), escalation, long, type);
  176. diff = 60 * 60 * 24 * 2
  177. assert.equal(result, getAbsolute(new Date(current.getTime() + (60050 * 60 * 24 * 2)), diff), 'in 2 days')
  178. result = App.PrettyDate.humanTime(current.getTime() + (60050 * 60 * 24 * 2.5), escalation, long, type);
  179. diff = 60 * 60 * 24 * 2.5
  180. assert.equal(result, getAbsolute(new Date(current.getTime() + (60050 * 60 * 24 * 2.5)), diff), 'in 2.5 days')
  181. result = App.PrettyDate.humanTime(current.getTime() + (60050 * 60 * 24 * 5.5), escalation, long, type);
  182. diff = 60 * 60 * 24 * 5.5
  183. assert.equal(result, getAbsolute(new Date(current.getTime() + (60050 * 60 * 24 * 5.5)), diff), 'in 5.5 days')
  184. result = App.PrettyDate.humanTime(current.getTime() + (60050 * 60 * 24 * 30.5), escalation, long, type);
  185. diff = (60 * 60 * 24 * 30.5);
  186. assert.equal(result, getAbsolute(new Date(current.getTime() + 60050 * 60 * 24 * 30.5), diff), 'in 30.5 days')
  187. };
  188. function prettyDateTimestamp(current, escalation, long, type) {
  189. var result = App.PrettyDate.humanTime(current, escalation, long, type);
  190. assert.equal(result, 'just now', 'just now') // by defaul < 1 min is just now
  191. result = App.PrettyDate.humanTime(current - 15000, escalation, long, type);
  192. assert.equal(result, 'just now', 'just now') // by default < 1 min is just now
  193. result = App.PrettyDate.humanTime(current - 60000, escalation, long, type);
  194. assert.equal(result, getTimestamp(new Date(current - 60000)), '1 min ago')
  195. result = App.PrettyDate.humanTime(current - (2 * 60000), escalation, long, type);
  196. assert.equal(result, getTimestamp(new Date(current - (2 * 60000))), '2 min ago')
  197. result = App.PrettyDate.humanTime(current - (60000 * 60), escalation, long, type);
  198. assert.equal(result, getTimestamp(new Date(current - (60000 * 60))), '1 hour')
  199. result = App.PrettyDate.humanTime(current - (60000 * 60 * 2), escalation, long, type);
  200. assert.equal(result, getTimestamp(new Date(current - (60000 * 60 * 2))), '2 hours')
  201. result = App.PrettyDate.humanTime(current - (60000 * 60 * 2.5), escalation, long, type);
  202. assert.equal(result, getTimestamp(new Date(current - (60000 * 60 * 2.5))), '2.5 hours')
  203. result = App.PrettyDate.humanTime(current - (60000 * 60 * 12.5), escalation, long, type);
  204. assert.equal(result, getTimestamp(new Date(current - (60000 * 60 * 12.5))), '12.5 hours')
  205. result = App.PrettyDate.humanTime(current - (60000 * 60 * 24), escalation, long, type);
  206. assert.equal(result, getTimestamp(new Date(current - (60000 * 60 * 24))), '1 day')
  207. result = App.PrettyDate.humanTime(current - (60000 * 60 * 24 * 2), escalation, long, type);
  208. assert.equal(result, getTimestamp(new Date(current - (60000 * 60 * 24 * 2))), '2 days')
  209. result = App.PrettyDate.humanTime(current - (60000 * 60 * 24 * 2) - (60000 * 5), escalation, long, type);
  210. assert.equal(result, getTimestamp(new Date(current - (60000 * 60 * 24 * 2) - (60000 * 5))), '2 days')
  211. result = App.PrettyDate.humanTime(current - (60000 * 60 * 24 * 2.5), escalation, long, type);
  212. assert.equal(result, getTimestamp(new Date(current - (60000 * 60 * 24 * 2.5))), '2.5 days')
  213. result = App.PrettyDate.humanTime(current - (60000 * 60 * 24 * 2.5) - (60000 * 5), escalation, long, type);
  214. assert.equal(result, getTimestamp(new Date(current - (60000 * 60 * 24 * 2.5) - (60000 * 5))), '2.5 days')
  215. // future
  216. current = new Date('1995-12-17T03:24:00')
  217. result = App.PrettyDate.humanTime(current, escalation, long, type);
  218. assert.equal(result, 'just now', 'just now') // no change, because < 1 min = just now
  219. result = App.PrettyDate.humanTime(current.getTime() + 55000, escalation, long, type);
  220. assert.equal(result, 'just now', 'just now') // no change, because < 1 min = just now
  221. result = App.PrettyDate.humanTime(current.getTime() + 65000, escalation, long, type);
  222. assert.equal(result, getTimestamp(new Date(current.getTime() + 65000)), 'in 1 min')
  223. result = App.PrettyDate.humanTime(current.getTime() + (2 * 65000), escalation, long, type);
  224. assert.equal(result, getTimestamp(new Date(current.getTime() + (2 * 65000))), 'in 2 min')
  225. result = App.PrettyDate.humanTime(current.getTime() + (60500 * 60), escalation, long, type) ;
  226. assert.equal(result, getTimestamp(new Date(current.getTime() + (60500 * 60))), 'in 1 hour')
  227. result = App.PrettyDate.humanTime(current.getTime() + (60050 * 60 * 2), escalation, long, type);
  228. assert.equal(result, getTimestamp(new Date(current.getTime() + (60050 * 60 * 2))), 'in 2 hours')
  229. result = App.PrettyDate.humanTime(current.getTime() + (60050 * 60 * 2.5), escalation, long, type);
  230. assert.equal(result, getTimestamp(new Date(current.getTime() + (60050 * 60 * 2.5))), 'in 2.5 hours')
  231. result = App.PrettyDate.humanTime(current.getTime() + (60050 * 60 * 24), escalation, long, type);
  232. assert.equal(result, getTimestamp(new Date(current.getTime() + (60050 * 60 * 24))), 'in 1 day')
  233. result = App.PrettyDate.humanTime(current.getTime() + (60050 * 60 * 24 * 2), escalation, long, type);
  234. assert.equal(result, getTimestamp(new Date(current.getTime() + (60050 * 60 * 24 * 2))), 'in 2 days')
  235. result = App.PrettyDate.humanTime(current.getTime() + (60050 * 60 * 24 * 2.5), escalation, long, type);
  236. assert.equal(result, getTimestamp(new Date(current.getTime() + (60050 * 60 * 24 * 2.5))), 'in 2.5 days')
  237. result = App.PrettyDate.humanTime(current.getTime() + (60050 * 60 * 24 * 5.5), escalation, long, type);
  238. assert.equal(result, getTimestamp(new Date(current.getTime() + (60050 * 60 * 24 * 5.5))), 'in 5.5 days')
  239. result = App.PrettyDate.humanTime(current.getTime() + (60050 * 60 * 24 * 30.5), escalation, long, type);
  240. assert.equal(result, getTimestamp(new Date(current.getTime() + 60050 * 60 * 24 * 30.5)), 'in 30.5 days')
  241. };
  242. function getAbsolute(date, diff) {
  243. weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  244. weekday = weekdays[date.getDay()];
  245. months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  246. month = months[date.getMonth()];
  247. d = date.getDate()
  248. m = date.getMonth() + 1
  249. yfull = date.getFullYear()
  250. M = date.getMinutes()
  251. H = date.getHours()
  252. l = (H + 11) % 12 + 1
  253. if (l < 10) {
  254. l = ' ' + l
  255. }
  256. P = H >= 12 ? 'pm' : 'am'
  257. // for less than 6 days
  258. // weekday HH::MM
  259. if (diff < (60 * 60 * 24 * 6))
  260. string = weekday + ' ' + H + ':' + (M < 10 ? '0':'') + M
  261. else if (current.getYear() == date.getYear())
  262. string = weekday + ' ' + date.getDate() + '. ' + month + ' ' + H + ':' + (M < 10 ? '0':'') + M
  263. else
  264. string = weekday + ' ' + (m < 10 ? '0':'') + m + '/' + (d < 10 ? '0':'') + d + '/' + (yfull) + ' ' + l + ':' + (M < 10 ? '0':'') + M + ' ' + P
  265. return string;
  266. }
  267. function getTimestamp(date) {
  268. d = date.getDate()
  269. m = date.getMonth() + 1
  270. yfull = date.getFullYear()
  271. yshort = date.getYear()-100
  272. M = date.getMinutes()
  273. H = date.getHours()
  274. l = (H + 11) % 12 + 1
  275. if (l < 10) {
  276. l = ' ' + l
  277. }
  278. P = H >= 12 ? 'pm' : 'am'
  279. // YYYY-MM-DD HH::MM
  280. return (m < 10 ? '0':'') + m + '/' + (d < 10 ? '0':'') + d + '/' + (yfull) + ' ' + l + ':' + (M < 10 ? '0':'') + M + ' ' + P
  281. }
  282. });
  283. QUnit.test("PrettyDate getISOWeeks", assert => {
  284. assert.equal(App.PrettyDate.getISOWeeks(2026), 53, '53 weeks')
  285. assert.equal(App.PrettyDate.getISOWeeks(2020), 53, 'leap year and 53 weeks')
  286. assert.equal(App.PrettyDate.getISOWeeks(2019), 52, '52 weeks')
  287. assert.equal(App.PrettyDate.getISOWeeks(2024), 52, 'leap year and 52 weeks')
  288. })