-
Notifications
You must be signed in to change notification settings - Fork 2
/
px-demo-component-snippet.html
182 lines (157 loc) · 5.25 KB
/
px-demo-component-snippet.html
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
<!--
Copyright (c) 2018, General Electric
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!--
Relative paths assume component is being run from inside an app or another component, where dependencies are flat
siblings. When this component is run from its own repo (e.g. tests, examples), we assume the server is started with
'gulp serve' (or similar server setup) to enable correct finding of bower dependencies for local runs.
-->
<link rel="import" href="../polymer/polymer.html"/>
<link rel="import" href="../px-demo-snippet/px-demo-snippet.html">
<!-- Import style module -->
<link rel="import" href="css/px-demo-styles.html">
<!--
Wraps around `px-demo-snippet` to show code sample.
##### Usage
<px-demo-component-snippet></px-demo-component-snippet>
@element px-demo-component-snippet
@blurb Wraps around `px-demo-snippet` to show code sample.
@homepage index.html
@demo index.html
-->
<dom-module id="px-demo-component-snippet">
<template>
<style include="px-demo-styles"></style>
<px-demo-snippet
element-properties="{{_shallowProps}}"
element-name="{{elementName}}"
links-includes="[[linksIncludes]]"
scripts-includes="[[scriptsIncludes]]"
polygit-includes="[[polygitIncludes]]"
code-link="[[codepenLink]]"
suppress-property-values="[[suppressPropertyValues]]"
no-indentation="[[noIndentation]]"
hide-codepen="[[hideCodepen]]">
</px-demo-snippet>
</template>
</dom-module>
<script>
Polymer({
is: 'px-demo-component-snippet',
properties: {
/**
* Name of the Predix UI element.
*
* @property elementName
*/
elementName: {
type: String,
value: ''
},
/**
* Description of the Predix UI module.
*
* @property elementProperties
*/
elementProperties: {
type: Object,
value: function(){ return {}; }
},
/**
* An array of <link> include URLs required to make the CodePen work.
*
* @property linksIncludes
*/
linksIncludes: {
type: Array,
value: function(){ return {}; }
},
/**
* An array of <script> include URLs required to make the CodePen work.
*
* @property scriptsIncludes
*/
scriptsIncludes: {
type: Array,
value: function(){ return {}; }
},
/**
* An array of additional PolyGit includes required to make the CodePen work.
*
* @property polygitIncludes
*/
polygitIncludes: {
type: Array,
value: function(){ return {}; }
},
/**
* Link to a pre-created Codepen that we do not need to dynamically build.
*
* @property codepenLink
*/
codepenLink: {
type: String
},
/**
* Props computed to the format `px-demo-snippet` accepts.
*
* @property _shallowProps
*/
_shallowProps: {
type: Object,
value: function(){ return {}; }
},
suppressPropertyValues: {
type: Array,
value: function() { return []; }
},
noIndentation: {
type: Boolean,
value: false
},
hideCodepen: {
type: Boolean,
value: false
}
},
observers: ['_assignShallowProps(elementProperties, elementProperties.*)'],
/**
* Converts the complex `props` object into a shallow format of
* `propName`:`propValue` for `px-demo-snippet`.
*
* @return {Object}
*/
_assignShallowProps: function(props) {
if (props && (typeof props === 'object') && Object.keys(props).length) {
var propKeys = Object.keys(props);
var shallow = {};
for (var i = 0; i < propKeys.length; i++) {
// Don't check the property directly because it might be a boolean value, check its existence on the object
var val = props[propKeys[i]].hasOwnProperty('value') ? props[propKeys[i]].value : "";
// @TODO: px-demo-snippet silently fails if it gets a string for an attribute with double quotes.
// For now, we clean them out manually. Future improvement is to fix this bug in px-demo-snippet.
if (typeof val === 'string') {
// Replace double quotes on the outside with single quotes. Example:
// In: <tag foo="bar" array="['key':'val']"></tag>
// Out: <tag foo='bar' array='["key":"val"]'></tag>
val = val.replace(/([a-zA-Z]+=)"([^"]*)"/g, function(fullMatch, attrName, attrVal) {
return attrName + "'" + attrVal.replace(/\'/g, '"') + "'";
});
}
shallow[propKeys[i]] = val;
}
this.set('_shallowProps', {});
this.set('_shallowProps', shallow);
}
}
});
</script>