|
| 1 | +var abp = abp || {}; |
| 2 | +(function ($) { |
| 3 | + |
| 4 | + if (typeof timeago === 'undefined') { |
| 5 | + throw "abp/timeago library requires the timeago.js library included to the page!"; |
| 6 | + } |
| 7 | + |
| 8 | + abp.timeago = abp.timeago || {}; |
| 9 | + |
| 10 | + // ABP culture name -> timeago.js locale name (only the ones that don't match the two-letter language code) |
| 11 | + abp.timeago.localeMap = { |
| 12 | + 'en': 'en_US', |
| 13 | + 'zh': 'zh_CN', |
| 14 | + 'zh-Hans': 'zh_CN', |
| 15 | + 'zh-Hant': 'zh_TW', |
| 16 | + 'zh-CN': 'zh_CN', |
| 17 | + 'zh-TW': 'zh_TW', |
| 18 | + 'pt': 'pt_BR', |
| 19 | + 'nb': 'nb_NO', |
| 20 | + 'no': 'nb_NO', |
| 21 | + 'nn': 'nn_NO', |
| 22 | + 'hi': 'hi_IN', |
| 23 | + 'id': 'id_ID', |
| 24 | + 'bn': 'bn_IN' |
| 25 | + }; |
| 26 | + |
| 27 | + abp.timeago.getLocale = function (cultureName) { |
| 28 | + cultureName = cultureName || (abp.localization && abp.localization.currentCulture && abp.localization.currentCulture.cultureName) || 'en'; |
| 29 | + |
| 30 | + if (abp.timeago.localeMap[cultureName]) { |
| 31 | + return abp.timeago.localeMap[cultureName]; |
| 32 | + } |
| 33 | + |
| 34 | + var language = cultureName.split('-')[0]; |
| 35 | + return abp.timeago.localeMap[language] || language; |
| 36 | + }; |
| 37 | + |
| 38 | + abp.timeago.format = function (date, options) { |
| 39 | + return timeago.format(date, abp.timeago.getLocale(), options); |
| 40 | + }; |
| 41 | + |
| 42 | + var toNodeList = function (nodes) { |
| 43 | + if (!nodes) { |
| 44 | + return []; |
| 45 | + } |
| 46 | + |
| 47 | + return Array.prototype.filter.call(nodes.nodeType ? [nodes] : nodes, function (node) { |
| 48 | + return node && node.getAttribute; |
| 49 | + }); |
| 50 | + }; |
| 51 | + |
| 52 | + // <time> elements carry the date in the "datetime" attribute, other elements may carry it in "title" |
| 53 | + var getDateAttribute = function (node) { |
| 54 | + var datetime = node.getAttribute('datetime'); |
| 55 | + if (datetime || node.tagName === 'TIME') { |
| 56 | + return datetime; |
| 57 | + } |
| 58 | + |
| 59 | + return node.getAttribute('title'); |
| 60 | + }; |
| 61 | + |
| 62 | + abp.timeago.render = function (nodes, options) { |
| 63 | + var nodeList = toNodeList(nodes).filter(function (node) { |
| 64 | + var datetime = getDateAttribute(node); |
| 65 | + if (!datetime) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + node.setAttribute('datetime', datetime); |
| 70 | + return true; |
| 71 | + }); |
| 72 | + |
| 73 | + if (!nodeList.length) { |
| 74 | + return nodeList; |
| 75 | + } |
| 76 | + |
| 77 | + return timeago.render(nodeList, abp.timeago.getLocale(), options); |
| 78 | + }; |
| 79 | + |
| 80 | + abp.timeago.cancel = function (nodes) { |
| 81 | + if (nodes === undefined) { |
| 82 | + timeago.cancel(); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + toNodeList(nodes).forEach(function (node) { |
| 87 | + timeago.cancel(node); |
| 88 | + }); |
| 89 | + }; |
| 90 | + |
| 91 | + // Locales that ship with ABP's default languages but are missing in timeago.full.min.js |
| 92 | + var slavicIndex = function (number, index) { |
| 93 | + return (index % 2 === 1 && number >= 5) ? 1 : 0; |
| 94 | + }; |
| 95 | + |
| 96 | + timeago.register('cs', function (number, index) { |
| 97 | + return [ |
| 98 | + [['právě teď', 'právě teď']], |
| 99 | + [['před %s vteřinami', 'za %s vteřiny'], ['před %s vteřinami', 'za %s vteřin']], |
| 100 | + [['před minutou', 'za minutu']], |
| 101 | + [['před %s minutami', 'za %s minuty'], ['před %s minutami', 'za %s minut']], |
| 102 | + [['před hodinou', 'za hodinu']], |
| 103 | + [['před %s hodinami', 'za %s hodiny'], ['před %s hodinami', 'za %s hodin']], |
| 104 | + [['včera', 'zítra']], |
| 105 | + [['před %s dny', 'za %s dny'], ['před %s dny', 'za %s dnů']], |
| 106 | + [['minulý týden', 'příští týden']], |
| 107 | + [['před %s týdny', 'za %s týdny'], ['před %s týdny', 'za %s týdnů']], |
| 108 | + [['minulý měsíc', 'příští měsíc']], |
| 109 | + [['před %s měsíci', 'za %s měsíce'], ['před %s měsíci', 'za %s měsíců']], |
| 110 | + [['před rokem', 'příští rok']], |
| 111 | + [['před %s lety', 'za %s roky'], ['před %s lety', 'za %s let']] |
| 112 | + ][index][slavicIndex(number, index)]; |
| 113 | + }); |
| 114 | + |
| 115 | + timeago.register('sk', function (number, index) { |
| 116 | + return [ |
| 117 | + [['práve teraz', 'práve teraz']], |
| 118 | + [['pred %s sekundami', 'o %s sekundy'], ['pred %s sekundami', 'o %s sekúnd']], |
| 119 | + [['pred minútou', 'o minútu']], |
| 120 | + [['pred %s minútami', 'o %s minúty'], ['pred %s minútami', 'o %s minút']], |
| 121 | + [['pred hodinou', 'o hodinu']], |
| 122 | + [['pred %s hodinami', 'o %s hodiny'], ['pred %s hodinami', 'o %s hodín']], |
| 123 | + [['pred %s dňom', 'o %s deň']], |
| 124 | + [['pred %s dňami', 'o %s dni'], ['pred %s dňami', 'o %s dní']], |
| 125 | + [['pred %s týždňom', 'o %s týždeň']], |
| 126 | + [['pred %s týždňami', 'o %s týždne'], ['pred %s týždňami', 'o %s týždňov']], |
| 127 | + [['pred %s mesiacom', 'o %s mesiac']], |
| 128 | + [['pred %s mesiacmi', 'o %s mesiace'], ['pred %s mesiacmi', 'o %s mesiacov']], |
| 129 | + [['pred %s rokom', 'o %s rok']], |
| 130 | + [['pred %s rokmi', 'o %s roky'], ['pred %s rokmi', 'o %s rokov']] |
| 131 | + ][index][slavicIndex(number, index)]; |
| 132 | + }); |
| 133 | + |
| 134 | + if (!$) { |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + $.timeago = function (date) { |
| 139 | + if (date && date.jquery) { |
| 140 | + date = date[0]; |
| 141 | + } |
| 142 | + |
| 143 | + if (date && date.nodeType === 1) { |
| 144 | + date = getDateAttribute(date); |
| 145 | + } |
| 146 | + |
| 147 | + return abp.timeago.format(date); |
| 148 | + }; |
| 149 | + |
| 150 | + $.fn.timeago = function (action, options) { |
| 151 | + if (action === 'dispose') { |
| 152 | + abp.timeago.cancel(this.toArray()); |
| 153 | + return this; |
| 154 | + } |
| 155 | + |
| 156 | + if (action === 'update' && options !== undefined && options !== null) { |
| 157 | + this.attr('datetime', options instanceof Date ? options.toISOString() : options); |
| 158 | + } |
| 159 | + |
| 160 | + abp.timeago.render(this.toArray()); |
| 161 | + return this; |
| 162 | + }; |
| 163 | + |
| 164 | +})(window.jQuery); |
0 commit comments