-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender-to-string.js
108 lines (96 loc) · 2.35 KB
/
render-to-string.js
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
import Component from '@ember/component';
import layout from 'ember-render-to-string/templates/components/render-to-string';
import { get, computed } from '@ember/object';
import { assert } from '@ember/debug';
import { getOwner } from '@ember/application';
export default Component.extend({
tagName: '',
layout,
/**
* Type of result to return:
* – 'html' – node.innerHTML
* – 'text' – node.innerText
* – 'dom' – node
*
* @default 'html'
* @type {String}
* @public
*/
content: 'html',
/**
* Tag name for HTML node. Accept any valid for document.createElement() method tag.
* Usefull only with `content='dom'`
*
* @default 'div'
* @type {String}
* @public
*/
destElTag: 'div',
/**
* Action to call after yielded template has been rendered. Hook accepts one
* argument which content depends on `content` property.
*
* @default NoOp
* @public
*/
afterRender() {},
// Fastboot service
fastboot: computed({
get() {
let owner = getOwner(this);
return owner.lookup('service:fastboot');
}
}),
isNotFastboot: computed({
get() {
let fastboot = get(this, 'fastboot');
return !(fastboot && get(fastboot, 'isFastBoot'));
}
}),
// Create element based on `destElTag`
destEl: computed('destElTag', {
get() {
let tag = get(this, 'destElTag');
return document.createElement(tag);
}
}),
didRender() {
this._super(...arguments);
// Trigger afterRender hook only in non-fastboot mode.
// Because we cannot create destEl in fastboot.
if (get(this, 'isNotFastboot')){
this._extractHTML();
}
},
/**
* Validate passed action and call it with content
*
* @private
*/
_extractHTML() {
let afterRender = get(this, 'afterRender');
assert(
`afterRender must be a function. You provided: ${typeof afterRender}`,
typeof afterRender === 'function'
);
afterRender(this._getContent());
},
/**
* Get content from `destEl` based on `content` property.
*
* @return {String|HTMLNode}
* @private
*/
_getContent() {
let destEl = get(this, 'destEl');
let content = get(this, 'content');
switch (content) {
case 'dom':
return destEl;
case 'text':
return destEl.innerText;
default:
return destEl.innerHTML;
}
}
});