Skip to content

Commit

Permalink
Make a template factory helper to handle all template cases
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoSot committed Aug 4, 2021
1 parent ea98ca4 commit 3881261
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
10 changes: 9 additions & 1 deletion js/src/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@

import * as Popper from '@popperjs/core'

import { defineJQueryPlugin, findShadowRoot, getElement, getUID, isRTL, noop, typeCheckConfig } from './util/index'
import {
defineJQueryPlugin,
findShadowRoot,
getElement,
getUID,
isRTL,
noop,
typeCheckConfig
} from './util/index'
import { DefaultAllowlist } from './util/sanitizer'
import Data from './dom/data'
import EventHandler from './dom/event-handler'
Expand Down
19 changes: 12 additions & 7 deletions js/src/util/template-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,16 @@ class TemplateFactory {
return Object.values(this._config.content).map(this._resolvePossibleFunction).filter(Boolean)
}

hasContent() {
return this.getContent().length > 0
}

changeContent(content) {
this._checkContent(content)
this._config.content = content
return this
}

hasContent() {
return this.getContent().length > 0
}

toHtml() {
const templateWrapper = document.createElement('div')
templateWrapper.innerHTML = this._maybeSanitize(this._config.template)
Expand All @@ -90,13 +91,17 @@ class TemplateFactory {

typeCheckConfig(NAME, config, DefaultType)

for (const [selector, content] of Object.entries(config.content)) {
typeCheckConfig(NAME, { selector, entry: content }, DefaultContentType)
}
this._checkContent(config.content)

return config
}

_checkContent(arg) {
for (const [selector, content] of Object.entries(arg)) {
typeCheckConfig(NAME, { selector, entry: content }, DefaultContentType)
}
}

_setContent(template, content, selector) {
const templateElement = SelectorEngine.findOne(selector, template)

Expand Down
7 changes: 4 additions & 3 deletions js/tests/unit/popover.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,21 @@ describe('Popover', () => {
const popover = new Popover(popoverEl, {
content: 'Popover content'
})

const spy = spyOn(popover, 'setContent').and.callThrough()
expect(popover._templateFactory).toBeNull()
let spy = null
let times = 1

popoverEl.addEventListener('hidden.bs.popover', () => {
popover.show()
})

popoverEl.addEventListener('shown.bs.popover', () => {
spy = spy || spyOn(popover._templateFactory, 'constructor').and.callThrough()
const popoverDisplayed = document.querySelector('.popover')

expect(popoverDisplayed).not.toBeNull()
expect(popoverDisplayed.querySelector('.popover-body').textContent).toEqual('Popover content')
expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledTimes(0)
if (times > 1) {
done()
}
Expand Down

0 comments on commit 3881261

Please sign in to comment.