This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathiconview.js
126 lines (107 loc) · 2.37 KB
/
iconview.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/* global DOMParser */
/**
* @module ui/icon/iconview
*/
import View from '../view';
import '../../theme/components/icon/icon.css';
/**
* The icon view class.
*
* @extends module:ui/view~View
*/
export default class IconView extends View {
/**
* @inheritDoc
*/
constructor() {
super();
const bind = this.bindTemplate;
/**
* The SVG source of the icon.
*
* @observable
* @member {String} #content
*/
this.set( 'content', '' );
/**
* This attribute specifies the boundaries to which the
* icon content should stretch.
*
* @observable
* @default '0 0 20 20'
* @member {String} #viewBox
*/
this.set( 'viewBox', '0 0 20 20' );
/**
* The fill color of the child `path.ck-icon__fill`.
*
* @observable
* @default ''
* @member {String} #fillColor
*/
this.set( 'fillColor', '' );
this.setTemplate( {
tag: 'svg',
ns: 'http://www.w3.org/2000/svg',
attributes: {
class: [
'ck',
'ck-icon'
],
viewBox: bind.to( 'viewBox' )
}
} );
}
/**
* @inheritDoc
*/
render() {
super.render();
this._updateXMLContent();
this._colorFillPaths();
// This is a hack for lack of innerHTML binding.
// See: https://github.com/ckeditor/ckeditor5-ui/issues/99.
this.on( 'change:content', () => {
this._updateXMLContent();
this._colorFillPaths();
} );
this.on( 'change:fillColor', () => {
this._colorFillPaths();
} );
}
/**
* Updates the {@link #element} with the value of {@link #content}.
*
* @private
*/
_updateXMLContent() {
if ( this.content ) {
const parsed = new DOMParser().parseFromString( this.content.trim(), 'image/svg+xml' );
const svg = parsed.querySelector( 'svg' );
const viewBox = svg.getAttribute( 'viewBox' );
if ( viewBox ) {
this.viewBox = viewBox;
}
this.element.innerHTML = '';
while ( svg.childNodes.length > 0 ) {
this.element.appendChild( svg.childNodes[ 0 ] );
}
}
}
/**
* Fills all child `path.ck-icon__fill` with the `#fillColor`.
*
* @private
*/
_colorFillPaths() {
if ( this.fillColor ) {
this.element.querySelectorAll( '.ck-icon__fill' ).forEach( path => {
path.style.fill = this.fillColor;
} );
}
}
}