1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | 10× 10× 10× 2× 10× 10× 10× 2× 9× 9× 9× 8× 4× 5× 10× 8× 8× 10× 1× 9× 1× 8× 8× 8× 430× 142× 32× 32× 28× 7× 7× 21× 20× 20× 246× 246× 246× 20× 8× 7× 7× 228× 228× 253× 253× 253× 274× 1258× 917× 228× 7× 7× 12190× 228× 7× 253× 1169× 859× 442× 417× 417× 417× 32× 118× 30× 46× 46× 1× 45× 45× 45× 45× 46× 46× 46× 46× 271× 45× 45× 271× 271× 25× 25× 25× 171× 25× 246× 246× 45× | import Ember from 'ember'; export default Ember.Service.extend({ sitemapEntryFilter: null, dynamicSegmentResolver: null, rootUrl: null, allRoutes: null, /** * Initialize the service. Try to add the sitemap settings and the routes automatically. */ init() { this._super(...arguments); const envSettings = Ember.getOwner(this).resolveRegistration('config:environment'); if (envSettings) { this.setSettings(envSettings.sitemap); } const router = Ember.getOwner(this).lookup('router:main'); const allRoutes = router.get('_routerMicrolib.recognizer.names'); if (allRoutes) { this.setRoutes(allRoutes); } }, /** * Set the sitemap settings. * * @param {object} settings */ setSettings: function(settings) { Eif (settings) { Eif ('rootUrl' in settings) { this.set('rootUrl', settings.rootUrl); } } }, /** * Set the routes. * * @param {object} allRoutes An ember routes object. */ setRoutes: function(allRoutes) { this.set('allRoutes', allRoutes); }, /** * Optionally, can be used to filter all sitemap entries through a custom function. * * @param {func} sitemapEntryFilter */ setSitemapEntryFilter: function(sitemapEntryFilter) { this.set('sitemapEntryFilter', sitemapEntryFilter); }, /** * Set a function that will resolve possible dynamic segment values * * @param {func} dynamicSegmentResolver */ setDynamicSegmentResolver: function(dynamicSegmentResolver) { this.set('dynamicSegmentResolver', dynamicSegmentResolver); }, /** * Get the model for sitemap routes. * * @return {object} Promise returning an array of sitemap entry objects */ getModel() { this._validate(); const sitemapEntriesPromise = this._routesToSitemapEntries(this.get('allRoutes')); return sitemapEntriesPromise; }, /** * Make sure all the required settings are set. */ _validate() { if (!this.get('rootUrl')) { throw new Error(`sitemap.rootUrl is required`); } if (!this.get('allRoutes')) { throw new Error(`allRoutes are required`); } }, /** * Transform an Ember routes object into an array of sitemap entry objects * * @param {object} routes Ember routes object * @return {object} Promise returning sitemap entry objects */ _routesToSitemapEntries(routes) { const ignore = [ 'error', 'loading', 'application', 'sitemap-txt', 'sitemap-xml', ]; let sitemapEntriesPromise = Promise.resolve([]); Object.keys(routes).forEach((key) => { if (ignore.find(ignoredKey => key.indexOf(ignoredKey) > -1)) { return; } const dynamicSegmentsKeys = this._routeToDynamicSegments(routes[key]); sitemapEntriesPromise = sitemapEntriesPromise.then((entries) => { if (dynamicSegmentsKeys.length === 0) { // No dynamic segments in route return this._filterSitemapEntry( { loc: this._routeToPath(routes[key]) }, this._routeToSegments(routes[key]) ) .then(entry => entry ? [...entries, entry] : entries); } else { return this._dynamicSegmentsToPermutations( dynamicSegmentsKeys, this._routeToSegments(routes[key]) ).then((permutations) => { let permutationsPromise = Promise.resolve(entries); permutations.forEach((permutation) => { permutationsPromise = permutationsPromise.then((entries) => this._filterSitemapEntry( { loc: this._routeToPath(routes[key], permutation) }, this._routeToSegments(routes[key]), permutation ) .then(entry => entry ? [...entries, entry] : entries) ); }); return permutationsPromise; }); } }); }); return sitemapEntriesPromise .then(entries => // Remove duplications caused by indexes this._removeDuplicateEntries(entries) ) .then(entries => // Map entry.loc from relative paths to absolute URLs entries.map((entry) => { entry.loc = this._relativeToAbsoluteUrl(entry.loc); return entry; }) ); }, /** * If sitemapEntryFilter is set, uses it to filter a sitemap entry. * * @param {object} entry * @param {array} segments * @param {object} dynamicSegments={} * @return {object} Promise returning the filtered sitemap entry */ _filterSitemapEntry(entry, segments, dynamicSegments = {}) { const sitemapEntryFilter = this.get('sitemapEntryFilter'); const result = sitemapEntryFilter ? sitemapEntryFilter(entry, segments, dynamicSegments) : entry; return Promise.resolve(result); }, /** * Transform an Ember route object into a segments array. * * @param {object} route * @return {object} An array of segments in the route */ _routeToSegments(route) { return route.segments .filter(segment => [0, 1].includes(segment.type)) .map(({type, value}) => (type === 1) ? `:${value}` : value); // Prefix dynamic segments with colon }, /** * Converts a relative URL to an absolute URL. * * @param {string} relativeUrl * @return {string} Absolute URL */ _relativeToAbsoluteUrl(relativeUrl) { return this.get('rootUrl') + relativeUrl; }, /** * Removes duplicate sitemap array entries. Duplicates exist because of index routes. * * @param {array} entries * @return {array} Entries with duplicates removed */ _removeDuplicateEntries(entries) { const newEntries = []; entries.forEach((entry) => { if (!newEntries.find(newEntry => newEntry.loc === entry.loc)) { newEntries.push(entry); } }); return newEntries; }, /** * Transforms a route object into a path string. * * @param {object} route An Ember route object * @param {object} dynamicSegments An object containing values for dynamic segments in the route * @return {string} Path (Ex: /photos) */ _routeToPath(route, dynamicSegments) { return route.segments .filter(segment => segment.type === 0 || segment.type === 1) .map(segment => { if (segment.type === 0) { // static return segment.value; } else Eif (segment.type === 1) { // dynamic Eif (segment.value in dynamicSegments) { return dynamicSegments[segment.value]; } else { throw new Error(`The value for dynamic segment '${segment.value}' is not set.`); } } }) .join('/'); }, /** * Extracts dynamic segments from an Ember route object. * * @param {object} route An Ember route object * @return {array} An array of dynamic segment keys */ _routeToDynamicSegments(route) { return route.segments .filter(segment => segment.type === 1) .map(segment => segment.value); }, /** * Resolves possible values for a dynamic segment using the dynamicSegmentResolver function. * * @param {string} dynamicSegmentKey * @param {array} allSegments * @param {object} otherDynamicSegments * @return {array} Possible values for the dynamic segment. */ _resolveDynamicSegment(dynamicSegmentKey, allSegments, otherDynamicSegments) { const dynamicSegmentResolver = this.get('dynamicSegmentResolver'); if (!dynamicSegmentResolver) { throw new Error('dynamicSegmentResolver is required but is not set.'); } const result = dynamicSegmentResolver(dynamicSegmentKey, allSegments, otherDynamicSegments); return Promise.resolve(result) .then(values => { Iif (!Array.isArray(values)) { throw new Error(`The dynamic segment resolver returned a '${typeof values}'` + ` instead of an array for '${dynamicSegmentKey}'.`); } return values; }); }, /** * Given a list of dynamic segments, returns an array of all the possible * permutations for the combination of those dynamic segments. * Since the number of dynamic segments is not limited or fixed, this function * is designed recursive. * * @param {array} dynamicSegmentsKeys An array of dynamic segment key strings * @param {array} segments An array of all segments in the route * @param {permutation} permutation={} Used for passing the permutation in recursive calls * @return {array} An array of all the possible permutations (dynamic segment objects) */ _dynamicSegmentsToPermutations(dynamicSegmentsKeys, segments, permutation = {}) { Iif (dynamicSegmentsKeys.length === 0) { return Promise.resolve([]); } const firstDynamicSegmentKey = dynamicSegmentsKeys[0]; const remainingDynamicSegmentsKeys = dynamicSegmentsKeys.slice(1); return this._resolveDynamicSegment(firstDynamicSegmentKey, segments, permutation) .then((firstDynamicSegmentValues) => { const firstDynamicSegmentPermutations = firstDynamicSegmentValues.map(val => ({ [firstDynamicSegmentKey]: val, })); let permutationsPromise = Promise.resolve([]); firstDynamicSegmentPermutations.forEach((firstDynamicSegmentPermutation) => { permutationsPromise = permutationsPromise.then(permutations => { if (remainingDynamicSegmentsKeys.length > 0) { return this._dynamicSegmentsToPermutations( remainingDynamicSegmentsKeys, segments, Object.assign({}, permutation, firstDynamicSegmentPermutation) ).then((remainingDynamicSegmentsPermutations) => { Eif (remainingDynamicSegmentsPermutations.length > 0) { remainingDynamicSegmentsPermutations.forEach(remainingDynamicSegmentsPermutation => { permutations.push(Object.assign({}, firstDynamicSegmentPermutation, remainingDynamicSegmentsPermutation)); }); } else { permutations = []; } return permutations; }) } else { permutations.push(firstDynamicSegmentPermutation); return permutations; } }) }); return permutationsPromise; }); }, }); |