Skip to content

Commit

Permalink
Merge 9a6da9c into abea4df
Browse files Browse the repository at this point in the history
  • Loading branch information
gkchestertron committed Apr 5, 2018
2 parents abea4df + 9a6da9c commit 5b8b6e4
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
23 changes: 23 additions & 0 deletions addon/utils/details-page-title-factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Ember from 'ember'

const {String: EmberString, get} = Ember

/**
* factory for a details page title handler
* will get a dynamic title from a route's controller
* based on a path to the data in its model
* @param {string} modelPath - path in model to dynamic title data
* @returns {array} - array of title sections
*/
export default function pageTitleFactory (modelPath) {
return function (sections, defaultTitle) {
const name = get(this.controller.model, modelPath)
let tab = get(this.controller, 'selectedTabId')

if (tab) {
tab = EmberString.capitalize(tab.replace(/-/g, ' '))
}

return [name, tab]
}
}
1 change: 1 addition & 0 deletions app/utils/details-page-title-factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {default, initialize} from 'ember-frost-page-title/utils/details-page-title-factory'
24 changes: 24 additions & 0 deletions docs/frost-page-title.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,30 @@ The `defaultHandler` method takes a url (defaulted to `window.location.hash` or
### #updateTitle()
The `updateTitle` function runs runs the `defaultHandler` and the hanslers built up by routes using the mixin and joins them with either a "|" or whatever you set in your config for `APP['frost-page-title'].delimiter` (see example above).

## Details page title factory util
Provided with this addon is a util that handles the default behavior for our details pages. They typically get the title from a display attribute in the model, and append the name of the currently selected tab (automatically capitalized and de-deasherized). For example:

```
// some route
import pageTitleFactory from 'ember-frost-page-title/utils/details-page-title-factory'
...
pageTitleHandler: pageTitleFactory('displayData.displayTitle')
...
```

So if your url is /somethings-details/selectedTabId=secret-details, and your data looks like this:

```
{
displayData: {
'Some thing'
}
}
```

Your title will be `Some thing | Secret details`

## Examples

### Default
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/utils/details-page-title-factory-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {expect} from 'chai'
import pageTitleFactory from 'ember-frost-page-title/utils/details-page-title-factory'
import {beforeEach, describe, it} from 'mocha'

describe('Unit / utils / details-page-title-factory /', function () {
let dummyRoute

beforeEach(function () {
dummyRoute = {
pageTitleHandler: pageTitleFactory('name'),

controller: {
model: {
name: 'Details page title'
},

selectedTabId: 'some-tab'
}
}
})

it('should return a function', function () {
expect(typeof dummyRoute.pageTitleHandler).to.equal('function')
})

describe('the generated handler', function () {
it('should return an array', function () {
let handlerOutput = dummyRoute.pageTitleHandler([], 'defaultTitle')
expect(Array.isArray(handlerOutput)).to.equal(true)
})

it('should get the data from the model via the modelPath', function () {
let handlerOutput = dummyRoute.pageTitleHandler([], 'defaultTitle')
expect(handlerOutput[0]).to.equal('Details page title')
})

it('should get the tab name and properly capitalize/de-dasherize it', function () {
let handlerOutput = dummyRoute.pageTitleHandler([], 'defaultTitle')
expect(handlerOutput[1]).to.equal('Some tab')
})
})
})

0 comments on commit 5b8b6e4

Please sign in to comment.