-
Notifications
You must be signed in to change notification settings - Fork 0
/
adom-demo-helper-properties.js
666 lines (645 loc) · 19.4 KB
/
adom-demo-helper-properties.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
(function () {
'use strict';
Polymer({
is: 'adom-demo-helper-properties',
properties: {
/**
* Component name, is a obligatory property
*/
componentName: {
type: String,
observer: '_setComponentName'
},
_componentDescription: {
type: String
},
/**
* Is the route of bower.json component by default is ../../bower.json
*/
bower: {
type: String,
value: '../../bower.json'
},
/**
* Is the route of package.json component by default is ../../bower.json
*/
package: {
type: String,
value: '../../package.json'
},
/**
* Is the route of analysis.json component by default is '../../analysis.json'
*/
descriptorUrl: {
type: String,
value: '../../analysis.json'
},
/**
* Is the route of css component by default is '../../component-name-styles.html'
*/
cssUrl: {
type: String
},
_metadata: {
type: Object,
value: function () {
return {};
}
},
_package: {
type: Object,
value: function () {
return {};
}
},
_analysis: {
type: Object,
value: function () {
return {};
},
},
_cssStyles: {
type: String
},
_cssVars: {
type: Array,
value: function () {
return [];
},
},
_cssMixins: {
type: Array,
value: function () {
return [];
},
},
_propertiesComponent: {
type: Array,
value: function () {
return [];
}
},
_eventsComponent: {
type: Array,
value: function () {
return [];
}
},
_methodsComponent: {
type: Array,
value: function () {
return [];
}
},
_slotsComponent: {
type: Array,
value: function () {
return [];
}
},
/**
* If it´s true show the demo page
*/
showDemo: {
type: Boolean,
value: false,
observer: '_setDemo'
},
/**
* The number of demo property is selected
* component responsive: 0
* component properties: 1
* component css vars: 2
* component methods: 3
* component slot: 4
* component data: 5
*/
optionSelected: {
type: Number,
value: 0
},
/**
* If it´s true show the demo page
*/
device: {
type: Array,
value: function () {
return {
'width': '360px',
'height': '480px'
};
}
},
/**
* This show the built of component
*/
padding: {
type: Number,
value: 0
},
/**
* This show the built of component
*/
_builtComponent: {
type: String
},
/**
* This show the built of component
*/
allowSlot: {
type: Boolean,
value: false
},
/**
* Save the slots content
*/
_slotted: {
type: String
},
/**
* This property built the iframe
*/
_builtIframe: {
type: String
}
},
ready: function() {
Polymer.RenderStatus.afterNextRender(this, function() {
var self = this;
var docView = this.shadowRoot.querySelectorAll('.main__content__wrap__views__doc__right-panel__doc-view');
window.addEventListener('scroll', function() {
self.$.installation
if(pageYOffset === 0) {
self.$.menuDoc.style.top = '300px';
}
if(pageYOffset > 0 && pageYOffset <= 300) {
self.$.menuDoc.style.top = 300 - pageYOffset + 'px';
}
if(pageYOffset > 300) {
self.$.menuDoc.style.top = 0;
}
});
this._getResize(550);
var self = this;
window.addEventListener('resize', function() {
self._getResize(550);
})
})
},
_setEvents: function () {
var events = '';
this._eventsComponent.forEach(function(element) {
events += `
this.addEventListener('${element.name}', function(e) {
showToast('${element.name}' , e);
});
`
});
events += `
function showToast(event, e) {
document.querySelector('#toast').classList.add('show');
document.querySelector('#toastEvent').innerHTML = '<span class="text">event:</span> ' + event;
if(e.detail) {
document.querySelector('#detail').innerHTML = '<span class="text">detail:</span> ' + e.detail;
}
setTimeout(function() {
removeToast();
}, 10000);
}
function removeToast() {
document.querySelector('#toast').classList.remove('show');
}
`
return events;
},
_getBower: function () {
this.$.clone.querySelector('code').innerHTML = '$ git clone ' + this._metadata.repository.url
},
_getBowerError: function () {
console.error('This component has problem to show the bower, please define bower property with your route');
},
_getPackage: function() {
},
_getPackageError: function () {
console.error('This component has problem to show the bower, please define package property with your route');
},
_getCss: function (e) {
this._cssVarsformated();
this._cssMixinsformated();
this._buildIframe();
},
_getCssError: function () {
console.error('This component has problem to show the css, please define cssFile property with your route');
},
_cssVarsformated: function () {
var arrVars = [];
var array = [];
// Find the first word
var reg1 = /([^\s]+)/;
// Find the second word
var reg2 = /\s+([^\s]+)/;
this._cssStyles.split('var(').forEach(function (element, i) {
if (i > 0) {
var item = this._cssStyles.split('var(')[i].match(reg1)[0].slice(0, -1);
var value = this._cssStyles.split('var(')[i].match(reg2)[0].slice(1, -2);
if (array.indexOf(item) === -1) {
if (this._cssStyles.split('var(')[i].match(reg2)[0].slice(-3) === '));') {
value = this._cssStyles.split('var(')[i].match(reg2)[0].slice(1, -3);
}
arrVars.push({
"varName": item,
"description": item.slice(this.componentName.length + 3),
"value": value,
"special": item.slice(-5) === 'color' ? 'color' : ''
});
array.push(item);
}
}
}, this);
this._cssVars = arrVars;
},
_cssMixinsformated: function () {
var arrMixins = [];
var mixins = this._cssStyles.toString().match(new RegExp(`@apply --${this.componentName}(-*\\w*)*`, 'g'));
mixins.forEach(function (element) {
if (element.slice(this.componentName.length + 10) !== "") {
arrMixins.push({
"mixin": element.slice(7),
"description": "empty mixin for ." + element.slice(this.componentName.length + 10)
});
} else {
arrMixins.push({
"mixin": element.slice(7),
"value": "empty mixin for :host"
});
}
}, this);
this._cssMixins = arrMixins;
},
_getAnalysis: function () {
var properties = [];
var slots = [];
var methods = [];
var self = this;
//Formated Array for _propertiesComponent
this._analysis.elements[0].properties.forEach(function (element) {
properties.push({
"name": element.name,
"nameHtml": element.name.replace(/(?:^|\.?)([A-Z])/g, function (x,y){return "-" + y.toLowerCase()}).replace(/^_/, ""),
"description": element.description,
"defaultValue": self._getDefaultValue(element),
"privacy": element.privacy,
"type": element.type,
"special": element.name.slice(-5) === 'color' ? 'color' : ''
});
});
//Formated Array for _eventsComponent
this.set('_componentDescription', this._analysis.elements[0].description);
//Formated Array for _eventsComponent
this.set('_eventsComponent', this._analysis.elements[0].events);
//Formated Array for _methodsComponent
this._analysis.elements[0].methods.forEach(function(element) {
methods.push({
"name": element.name,
"description": element.description,
"privacy": element.privacy,
"params": element.params
});
});
this._methodsComponent = methods;
//Formated Array for _slotsComponent
this._analysis.elements[0].slots.forEach(function(element) {
slots.push({
"name": element.name,
"description": element.description
});
});
this._slotsComponent = slots;
/**
* Format the slotted property
*/
var slotted = '';
this._slotsComponent.forEach(function(element) {
slotted += `
<div slot="${element.name}">
${element.name} slot
</div>
`
});
this._slotted = slotted;
this._propertiesComponent = properties;
},
_getAnalysisError: function () {
console.error('This component has problem to show the analysis, please define descriptorUrl property with your route');
},
_computeLibrary(library) {
switch (library) {
case 'Polymer 1':
case 'Polymer hybrid':
case 'Polymer 2':
case 'Polymer 3':
return '../adom-demo-helper-properties/images/polymer.svg';
break;
case 'VueJs':
return '../adom-demo-helper-properties/images/vuejs.svg';
break;
case 'Angular 1':
case 'Angular 2':
return '../adom-demo-helper-properties/images/angular.svg';
break;
default:
return;
break;
}
},
_computeProperties: function (property) {
return property.privacy === 'public' ? false : true;
},
_computeMethods: function (property) {
return property.privacy === 'protected' ? true : false;
},
_computeType: function(property) {
if (property.type === "boolean") {
return true;
} else {
return false;
}
},
_computeColor: function(item) {
return item.special === 'color' ? true : false;
},
_setDevice(e) {
var device;
switch (e.currentTarget) {
case this.$.mobile:
this.$.phone.classList.remove('main__content__wrap__views__demo__fullsize');
this.$.phone.classList.add('main__content__wrap__views__demo__phone');
device = {
'width': '360px',
'height': '480px'
}
break;
case this.$.tablet:
this.$.phone.classList.remove('main__content__wrap__views__demo__fullsize');
this.$.phone.classList.add('main__content__wrap__views__demo__phone');
device = {
'width': '768px',
'height': '1024px'
}
break;
case this.$.desktop:
this.$.phone.classList.remove('main__content__wrap__views__demo__fullsize');
this.$.phone.classList.add('main__content__wrap__views__demo__phone');
device = {
'width': '1024px',
'height': '769px'
}
break;
case this.$.fullsize:
device = {
'width': '100%',
'height': '769px'
}
this.async(function () {
this.$.phone.classList.remove('main__content__wrap__views__demo__phone');
this.$.phone.classList.add('main__content__wrap__views__demo__fullsize');
}, 600);
break;
}
this.device = device;
},
_setComponentName: function (value) {
this.cssUrl = '../../' + value + '-styles.html'
},
_setView: function (e) {
if (e.currentTarget === this.$.buttonDemo) {
this.showDemo = true;
} else {
this.showDemo = false;
}
e.currentTarget.blur();
},
_setDemo: function (value) {
if (value) {
this.$.selector.classList.add('transition');
this.$.viewDemo.hidden = false;
this.$.titleView.innerHTML = 'Demo';
this.$.menuDoc.hidden = true;
this.$.viewDoc.hidden = true;
} else {
this.$.selector.classList.remove('transition');
this.$.menuDoc.hidden = false;
this.$.viewDoc.hidden = false;
this.$.titleView.innerHTML = 'Documentation';
this.$.viewDemo.hidden = true;
}
},
/**
* Copy the component on click in the button copy
*/
copyContent: function() {
var iconCopy = this.$.iconCopy;
this._contentCopy(this.$.code);
iconCopy.icon = "vaadin:copy";
setTimeout(function() {
iconCopy.icon = "vaadin:file-o";
}, 3000);
},
_buildComponent: function () {
var properties = '';
var styles = '';
/**
* Format the properties of component
*/
this._propertiesComponent.forEach(function (element) {
if (element.type === 'string' && element.defaultValue !== "") {
properties += element.nameHtml + '="' + element.defaultValue + '" ';
}
if (element.type === 'boolean' && element.defaultValue === true) {
properties += element.nameHtml + ' ';
}
if (element.type === 'Array' && element.defaultValue !== '[]') {
properties += element.nameHtml + "='" + element.defaultValue + "'"
}
});
/**
* Format the styles of component
*/
this._cssVars.forEach(function(element) {
styles += `${element.varName}: ${element.value};
`
});
this.allowSlot = this.$.checkSlot.checked;
this._builtComponent = `
<style is="custom-style">
${this.componentName} {
${styles}
}
</style>
<${this.componentName} id="component" ${properties}>
${this.allowSlot ? this._slotted : ''}
<${this.componentName}/>`
},
_buildIframe: function () {
this._buildComponent();
var methods = '';
var functions = '';
this._builtIframe = `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes">
<title>${this.componentName} demo</title>
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<script>
window.Polymer = {
dom: 'shadow',
lazyRegister: 'true',
useNativeCSSProperties: 'true'
}
</script>
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Roboto+Mono:400,700|Roboto:400,300,300italic,400italic,500,500italic,700,700italic" crossorigin="anonymous">
<link rel="import" href="../${this.componentName}.html">
<style is="custom-style" include="demo-pages-shared-styles">
/*Common styles*/
body {
font-family: Roboto;
margin: 0;
background-color: #fff;
padding: ${this.padding}px;
box-sizing: border-box;
}
.toast {
outline: none;
position: fixed;
box-sizing: border-box;
left: 10px;
top: 0;
transform: translateY(-100px);
background-color: rgba(0, 0, 0, 0.8);
color: #d32f2f;
border-radius: 5px;
transition: transform ease 200ms;
padding: 8px;
font-weight: 100;
}
.show {
transform: translateY(10px);
outline: none;
z-index: 103;
display: block;
}
.detail {
color: #0097a7;
}
.text {
color: white;
font-weight: bolder;
}
</style>
</head>
<body>
<dom-bind>
<template is="dom-bind" id="demo">
${this._builtComponent}
${this._methodsComponent.forEach(function(element) {
methods += `
<input type="hidden" id="${element.name}" on-click="${element.name}">`
})}
${methods}
<script>
document.addEventListener('WebComponentsReady', function() {
var demo = document.querySelector(Polymer.Element ? 'dom-bind' : '#demo');
${this._methodsComponent.forEach(function(element) {
functions +=`
demo.${element.name} = function() {
demo.$.component.${element.name}();
}
`
})}
${functions}
});
</script>
</template>
</dom-bind>
<div id="toast" class="toast" onClick="removeToast()">
<div class="toastEvent" id="toastEvent">
</div>
<div class="detail" id="detail">
</div>
</div>
<script type="text/javascript">
${this._setEvents()}
</script>
</body>
</html>
`
},
_contentCopy: function(element) {
var snipRange = document.createRange();
snipRange.selectNodeContents(element);
var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(snipRange);
var result = false;
try {
result = document.execCommand('copy');
this.dispatchEvent(new CustomEvent('content-copied', {
bubbles: true,
composed: true,
detail: result
}));
} catch (err) {
// Copy command is not available
Polymer.Base._error(err);
}
selection.removeAllRanges();
return result;
},
_computePackage: function() {
return this._package ? false : true
},
_computeBower: function() {
return this._metadata ? false : true
},
_computeSlotDoc: function() {
return this._slotsComponent !== [] ? false : true;
},
_setActiveDoc: function(e) {
var buttons = this.shadowRoot.querySelectorAll('.menuDoc__button');
var doc = this.shadowRoot.querySelector('#' +e.currentTarget.name);
doc.scrollIntoView();
buttons.forEach(function(element) {
element.classList.remove('active');
});
e.currentTarget.classList.add('active')
},
_getResize: function(resize) {
var self = this;
if(window.innerWidth < resize) {
self.$.fullsize.click();
}
},
_getDefaultValue: function(value) {
if(value.defaultValue !== "null" && value.type === "string") {
return value.defaultValue.slice(1, -1);
} else {
if(value.defaultValue === "null") {
return "";
}
if(value.type === "boolean" && value.defaultValue === "true") {
return true;
} else {
return false;
}
return value.defaultValue;
}
element.defaultValue !== "null" && element.type === "string" ? element.defaultValue.slice(1, -1) : element.defaultValue === "null" ? '' : element.defaultValue
return value === 'false' ? false : true;
},
_setEventMethod: function(e) {
var iframe = this.$.frame;
iframe.contentWindow.document.querySelector('#' + e.model.item.name).click();
}
});
}());