This repository has been archived by the owner on Feb 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raml-docs-resource-viewer.html
974 lines (896 loc) · 32.7 KB
/
raml-docs-resource-viewer.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
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
<!--
@license
Copyright 2016 The Advanced REST client authors <arc@mulesoft.com>
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.
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../raml-behaviors/raml-behavior.html">
<link rel="import" href="../marked-element/marked-element.html">
<link rel="import" href="../paper-styles/paper-styles.html">
<link rel="import" href="../paper-tabs/paper-tabs.html">
<link rel="import" href="../paper-tabs/paper-tab.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
<link rel="import" href="../docs-parameters-table/docs-body-parameters-table.html">
<link rel="import" href="../markdown-styles/markdown-styles.html">
<link rel="import" href="../iron-media-query/iron-media-query.html">
<link rel="import" href="../docs-parameters-table/docs-parameters-table-shared-styles.html">
<link rel="import" href="../iron-collapse/iron-collapse.html">
<link rel="import" href="../paper-button/paper-button.html">
<link rel="import" href="../iron-icon/iron-icon.html">
<link rel="import" href="../arc-icons/arc-icons.html">
<link rel="import" href="../iron-scroll-target-behavior/iron-scroll-target-behavior.html">
<link rel="import" href="../paper-tooltip/paper-tooltip.html">
<link rel="import" href="../arc-polyfills/arc-polyfills.html">
<link rel="import" href="../raml-annotations-display/raml-annotations-display.html">
<!--
`<raml-docs-resource-viewer>` An element that displays a documentation for a RAML's resource object
**Note: This element requires `currentPath` property to be set** in order to compute methods and
sub-resources list. It is required for navigation.
### Example
```
<raml-docs-resource-viewer current-path="{{path}}" raml="{{resource}}"></raml-docs-resource-viewer>
```
## Requesting navigation to method / sub-property
When the user click on the name of a method or a sub-resource then the `raml-path-changed` event
will be fired with the `path` property in event detail object.
Parent element(s) should use this event to handle the change and navigate to sub-resource.
Method 1) assumes that Polymer's data binding model is used and parent element listens to change
in `currentPath`:
```
<raml-docs-resource-viewer current-path="{{path}}"></raml-docs-resource-viewer>
```
Note use of `{{}}` instead of `[[]]` ([more about that](https://www.polymer-project.org/1.0/docs/devguide/data-binding)).
Observers can observe change to the `path` property and change current object.
Second method uses event handling to handle path change:
```
<raml-docs-resource-viewer id="resourceViewer" current-path="[[path]]"></raml-docs-resource-viewer>
```
```
init: function() {
document.querySelector('#resourceViewer').addEventListener('raml-path-changed', this._navigate.bind(this));
},
_navigate: function(e) {
var path = e.detail.path;
// Do somethind with path, like use `<raml-path-to-object>` element.
}
```
### Styling
The internal elemnts uses as uch width as they need. The code view displays
code snippets without text wrapping by defaul. To set proper width
set the `width` or `max-width` on this element so any code snippet will respect
this limitation.
```
<raml-docs-resource-viewer
style="max-width: 900px"></raml-docs-resource-viewer>
```
This is also can be dome by setting `max-width` on any other parent elements.
`<raml-docs-resource-viewer>` provides the following custom properties and mixins for styling:
Custom property | Description | Default
----------------|-------------|----------
`--raml-docs-resource-viewer` | Mixin applied to the element | `{}` |
`--raml-docs-h1` | Mixin applied to H1 | `{}` |
`--raml-docs-h2` | Mixin applied to H2 | `{}` |
`--raml-docs-h3` | Mixin applied to H3 | `{}` |
`--raml-docs-item-description` | Mixin applied to the description field. | `{}` |
`--raml-docs-table-max-width` | Max width applied to the tables. Suggested to not exceed 800px, otherwise it may cause usability issues. | `800px` |
`--raml-docs-link` | Mixin applied to the links elements. | `{}` |
`--raml-docs-link-color` | Color of the link elements | `#00A1DF` |
`--docs-parameters-table` | Mixin applied to all parameter table elements | `{}`
`--docs-parameters-url-table` | Mixin applied to this elements | `{}`
`--params-table-header-background-color` | Background color of table header | `#00A1DF`
`--params-table-header-color` | Font color of table header | `rgba(255, 255, 255, 0.87)`
`--params-table-subheader-background-color` | Background color of table subheader | `rgba(0, 161, 223, 0.24)`
`--params-table-subheader-color` | Font color of table subheader | `rgba(0, 0, 0, 0.87)`
`--params-table-row-border-color` | Border color of table's each row | `#00A1DF`
`--params-table-row-background-color` | Background color of table's each row | `#fff`
`--params-table-row-color` | Font color of table's each row | `#fff`
`--docs-parameters-table-cell` | Mixin applied to each cell | `{}`
`--docs-parameters-table-meta` | Mixin applied to property's metadata (example, pattern, etc) | `{}`
`--raml-docs-resource-viewer-title-border-color` | Border color applied to the border of the section headline elements | `#e5e5e5`
`--raml-docs-resource-viewer-narrow-container-width` | width of the main container in the narrow view | `calc(100vw - 32px)`
`--raml-docs-resource-viewer-navigation` | Mixin applied to the links section | `{}`
`--raml-docs-resource-viewer-navigation-wide-layout` | Mixin applied to the links section in wide layout | `{}`
`--raml-docs-resource-viewer-content` | Mixin applied to the main content section. | `{}`
`--raml-docs-resource-viewer-container` | Mixin applied to the container that is holding main content and navigation containers | `{}`
`--arc-font-headline` | | Mixin applied to all headline (h1) elements in ARC ecosystem. | `{}`
`--arc-font-title` | Mixin applied to all title elements in ARC ecosystem. | `{}`
`--arc-font-body1` | Mixin applied to a ARC's body element | `{}`
`--sidebar-list` | Mixin applied to sidebar list of links | `{}`
`--sidebar-list-link` | Mixin applied to each link in sidebar list of links | `{}`
`--sidebar-list-link-hover` | Mixin applied to :hover state for links in sidebar list of links | `{}`
`--toggle-button` | Mixin applied to toggling button (show/hide) | `{}`
`--toggle-button-hover` | Mixin applied to toggling button on hover (show/hide) | `{}`
`--raml-docs-method-viewer-title-area-actions` | Mixin applied to toggling actions area | `{}`
`--raml-docs-method-viewer-traits-list` | Mixin applied to the list of traits container | `{}`
`--raml-docs-method-viewer-traits-list-values` | Mixin applied to the list of traits values (names) | `{}`
`--raml-docs-method-viewer-traits-list-label` | Mixin applied to the list of traits list label | `{}`
`--raml-docs-method-viewer-traits-list-values-color` | Color of the traits list values (names) | `rgba(0,0,0,0.94)`
`--raml-docs-method-viewer-traits-list-color` | Color of the traits list section | `rgba(0,0,0,0.74)`
`--raml-docs-method-viewer-traits-list-values-color` | Color of the traits list values (names) | `rgba(0,0,0,0.94)`
`--raml-docs-method-viewer-traits-list-label-color` | Color of the traits list label | `inherit`
`--raml-docs-method-viewer-traits-list-help-icon` | A mixin applied to the "more info" icon | `{}`
`--raml-docs-method-viewer-traits-list-padding` | Padding applied to the traits and resource types container | `0px 12px`
`--raml-docs-resource-viewer-annotations-color` | Color of the annotations lable | `rgba(0,0,0,0.74)`
`--raml-docs-resource-viewer-annotations` | Mixin applied to the annotations label | `{}`
@group RAML Elements
@element raml-docs-resource-viewer
@demo demo/index.html
-->
<dom-module id="raml-docs-resource-viewer">
<template>
<style include="markdown-styles"></style>
<style include="docs-parameters-table-shared-styles"></style>
<style>
:host {
display: block;
@apply(--raml-docs-resource-viewer);
--paper-tabs-selection-bar-color: var(--params-table-header-background-color, #00A1DF);
--paper-tab-ink: var(--params-table-header-background-color, #00A1DF);
--paper-tab: {
flex: initial;
}
--content-section: {
padding: 8px 12px;
@apply(--raml-docs-resource-viewer-content-section);
}
}
:host([hidden]) {
display: none !important;
}
h1 {
@apply(--arc-font-headline);
@apply(--content-section);
@apply(--raml-docs-h1);
}
h2 {
@apply(--arc-font-title);
@apply(--content-section);
@apply(--raml-docs-h2);
}
h3 {
@apply(--paper-font-subhead);
@apply(--content-section);
@apply(--raml-docs-h3);
}
.resource-name {
font-weight: 500;
}
.resource-desc {
@apply(--arc-font-body1);
@apply(--content-section);
@apply(--raml-docs-item-description);
}
.type-block {
@apply(--content-section);
}
.params-table,
docs-body-parameters-table {
max-width: var(--raml-docs-table-max-width, 800px);
}
.sub-resources-list {
@apply(--content-section);
}
a {
color: var(--raml-docs-link-color, #00A1DF);
@apply(--raml-docs-link);
}
.container {
@apply(--layout-horizontal);
@apply(--raml-docs-resource-viewer-container);
}
.main {
@apply(--layout-flex);
overflow: auto;
@apply(--raml-docs-resource-viewer-content);
}
nav {
width: 210px;
min-height: 160px;
border-left: 1px #e5e5e5 solid;
padding-left: 12px;
margin: 12px 0;
position: relative;
display: none;
@apply(--raml-docs-resource-viewer-navigation);
}
[wide-layout] nav {
display: block;
@apply(--raml-docs-resource-viewer-navigation-wide-layout);
}
#nav-links {
position: absolute;
@apply(--sidebar-list);
}
#nav-links a {
display: block;
padding: 8px 12px;
margin: 8px 0;
@apply(--sidebar-list-link);
}
#nav-links a:hover {
@apply(--sidebar-list-link-hover);
}
:host([narrow]) .container {
max-width: var(--raml-docs-resource-viewer-narrow-container-width, calc(100vw - 32px));
overflow: auto;
}
.section-title-area {
@apply(--layout-horizontal);
@apply(--layout-center);
border-bottom: 1px var(--raml-docs-resource-viewer-title-border-color, #e5e5e5) solid;
}
.section-title-area h2 {
@apply(--layout-flex);
}
.toggle-button {
color: var(--response-status-view-toggle-icon-color, rgba(0, 0, 0, 0.54));
transition: color 0.25s linear;
@apply(--toggle-button);
}
.toggle-icon {
transform: rotateZ(0deg);
transition: transform 0.3s linear;
}
.toggle-icon.opened {
transform: rotateZ(-180deg);
}
.toggle-button:hover {
color: var(--response-status-view-toggle-icon-hover-color, rgba(0, 0, 0, 0.78));
@apply(--toggle-button-hover);
}
.title-area-actions {
@apply(--raml-docs-method-viewer-title-area-actions);
}
.traits-list,
.resource-types-list {
margin-bottom: 8px;
color: var(--raml-docs-method-viewer-traits-list-color, rgba(0,0,0,0.74));
padding: var(--raml-docs-method-viewer-traits-list-padding, 0px 12px);
@apply(--raml-docs-method-viewer-traits-list);
}
.trait-value,
.resource-types-value {
color: var(--raml-docs-method-viewer-traits-list-values-color, rgba(0,0,0,0.94));
@apply(--raml-docs-method-viewer-traits-list-values);
}
.trait-label,
.resource-types-label {
color: var(--raml-docs-method-viewer-traits-list-label-color, inherit);
@apply(--raml-docs-method-viewer-traits-list-label);
}
.help-icon-link-external {
width: 16px;
height: 16px;
vertical-align: text-bottom;
@apply(--raml-docs-method-viewer-traits-list-help-icon);
}
.title-area-actions {
@apply(--title-area-actions);
}
raml-annotations-display {
color: var(--raml-docs-resource-viewer-annotations-color, rgba(0,0,0,0.74));
@apply(--content-section);
@apply(--raml-docs-resource-viewer-annotations);
}
</style>
<iron-media-query query="(min-width: 800px)" query-matches="{{wide}}"></iron-media-query>
<iron-media-query query="(max-width: [[narrowWidth]])" query-matches="{{narrow}}"></iron-media-query>
<div class="container" wide-layout$="[[isWideLayout]]">
<div class="main">
<div class="title-area">
<h1 id="overview" class="title">
<span>Resource: </span>
<span class="resource-name">[[_computeResourceName(raml.*)]]</span>
</h1>
</div>
<template is="dom-if" if="[[hasTraits]]">
<div class="traits-list">
<span class="trait-label">Traits:</span>
<span class="trait-value">[[_computeTraitsList(raml.is)]]</span>
<a href="https://github.com/raml-org/raml-spec/blob/master/versions/raml-10/raml-10.md/#resource-types-and-traits" target="_blank"><iron-icon icon="arc:help-outline" class="help-icon-link-external"></iron-icon></a>
<paper-tooltip>A trait provids a method-level nodes such as description, headers, query string parameters, and responses.</paper-tooltip>
</div>
</template>
<template is="dom-if" if="[[hasResourceTypes]]">
<div class="resource-types-list">
<span class="resource-types-label">Resource types:</span>
<span class="resource-types-value">[[_computeResourceTypesList(raml.type)]]</span>
<a href="https://github.com/raml-org/raml-spec/blob/master/versions/raml-10/raml-10.md/#resource-types-and-traits" target="_blank"><iron-icon icon="arc:help-outline" class="help-icon-link-external"></iron-icon></a>
<paper-tooltip>A resource type can specify security schemes, methods, and other nodes that can be applied to a resource as a reusable asset in RAML.</paper-tooltip>
</div>
</template>
<template is="dom-if" if="[[hasAnnotations]]">
<raml-annotations-display annotations="[[raml.annotations]]"></raml-annotations-display>
</template>
<template is="dom-if" if="[[raml.description]]">
<marked-element smartypants markdown="[[raml.description]]">
<div class="markdown-html resource-desc"></div>
</marked-element>
</template>
<section hidden$="[[!hasTypes]]">
<div class="section-title-area">
<h2 id="resources">Types</h2>
<div class="title-area-actions">
<paper-button on-tap="toggleTypesCollapse" class="toggle-button">
[[_computeToggleActionLabel(typesOpened)]]
<iron-icon icon="arc:expand-more" class$="[[_computeToggleIconClass(typesOpened)]]"></iron-icon>
</paper-button>
<paper-tooltip animation-delay="200">Toogle types details</paper-tooltip>
</div>
</div>
<iron-collapse opened="[[typesOpened]]">
<template is="dom-repeat" items="[[types]]">
<div class="type-block">
<docs-body-parameters-table narrow="[[narrow]]" body="[[item]]" auto-hide></docs-body-parameters-table>
</div>
</template>
</iron-collapse>
</section>
<section hidden$="[[!hasSubproperties]]">
<div class="section-title-area">
<h2 id="methods">Methods and sub-resources</h2>
<div class="title-area-actions">
<paper-button on-tap="toggleMethodsCollapse" class="toggle-button">
[[_computeToggleActionLabel(methodsOpened)]]
<iron-icon icon="arc:expand-more" class$="[[_computeToggleIconClass(methodsOpened)]]"></iron-icon>
</paper-button>
<paper-tooltip animation-delay="200">Toogle methods and sub-resources details</paper-tooltip>
</div>
</div>
<iron-collapse opened="[[methodsOpened]]">
<div class="sub-resources-list">
<div class="params-table" id="substable">
<div class="params-table-header thead">
<section class="tr">
<div class="td">Name</div>
<div class="td">Description</div>
</section>
</div>
<div class="tbody">
<section hidden$="[[!methods]]" class="tr params-table-header subheader">
<div class="td" colspan="3">Methods</div>
<div class="td"></div>
</section>
<template is="dom-repeat" items="[[methods]]">
<section class="tr">
<div class="td param-name"><a href="#" data-path$="[[item.path]]">[[item.meta.name]]</a></div>
<div class="td param-desc">[[item.meta.description]]</div>
</section>
</template>
<section hidden$="[[!resources]]" class="tr params-table-header subheader">
<div class="td" colspan="3">Sub-resources</div>
<div class="td"></div>
</section>
<template is="dom-repeat" items="[[resources]]">
<section class="tr">
<div class="td param-name"><a href="#" data-path$="[[item.path]]">[[item.meta.name]]</a></div>
<div class="td param-desc">[[item.meta.description]]</div>
</section>
</template>
</div>
</div>
</div>
</iron-collapse>
</section>
</div>
<nav>
<div id="nav-links">
<a href="#overview">Overview</a>
<a hidden$="[[!hasTypes]]" href="#resources">Resources</a>
<a href="#methods">Methods</a>
</div>
</nav>
</div>
</template>
<script>
Polymer({
is: 'raml-docs-resource-viewer',
behaviors: [
Polymer.RamlBehavior,
Polymer.IronScrollTargetBehavior
],
/**
* An event fired when the user cliecked on a method/sub-resource link.
* Before the event is fired the `currentPath` propery is set. If parent
* element is listening for change on this property then it's redundand to
* listen for this event.
*
* @event raml-path-changed
* @property {String} path Path requested by the user.
*/
properties: {
/**
* Computed value. List of types used by this endpoint.
* The elements looks for types inside methods of this resource.
*
* It will not search for types in nested resources.
*/
types: {
type: Array,
readOnly: true
},
// Computed value. True when the resource has types defined in it.
hasTypes: {
type: Boolean,
value: false,
computed: '_computeHasTypes(types.length)'
},
// If true, the method has traits applied to it.
hasTraits: {
type: Boolean,
readOnly: true
},
// If true, the method has traits applied to it.
hasResourceTypes: {
type: Boolean,
readOnly: true
},
// Computed value, true if raml object has annotations
hasAnnotations: {
type: Boolean,
readOnly: true
},
/**
* Curently selected path in the RAML JSON structure. Required to compute
* sub-resources.
*/
currentPath: {
type: String
},
// Computed list of methods. It's the element's internal structure.
methods: Array,
// Computed list of resources. It's the element's internal structure.
resources: Array,
// Computed value, True if the resource has methods or sub-resources.
hasSubproperties: {
type: Boolean,
computed: '_computeHasSubproperties(methods, resources)'
},
/**
* If true then the element will start listen for the
* `raml-is-resource-changed`, `raml-selected-object-changed` and
* `raml-selected-path-changed` event and set corresponding properties
* when needed. It's a replacement for the Polymer's data biding
* for apps that are not Polymer powered.
*/
autoHide: {
type: Boolean,
value: false,
observer: '_autoHideChanged'
},
// If true then the element will be hidden ui the UI.
hidden: {
type: Boolean,
value: false,
reflectToAttribute: true
},
// If true then there's enough space to display right column
wide: {
type: Boolean,
value: false
},
/**
* If set it will renders the view in the narrow layout.
*/
narrow: {
type: Boolean,
notify: true,
reflectToAttribute: true
},
// A widith below which the `narrow` property will be set to true.
narrowWidth: {
type: String,
value: '768px'
},
// If true then the types section is visible.
typesOpened: {
type: Boolean,
value: true
},
// If true then the methods and sub-resources section is visible.
methodsOpened: {
type: Boolean,
value: true
},
// Computed value, true if renders wide layout (with nav links)
isWideLayout: {
type: Boolean,
computed: '_computeIsWideLayout(narrow, wide)'
}
},
observers: [
'_objectChanged(raml.*)',
'_computeMethods(currentPath, raml.*)',
'_computeResources(currentPath, raml.*)'
],
listeners: {
'substable.tap': '_subResourceTap',
'nav-links.tap': '_navLinksTap'
},
attached: function() {
this._eventTarget = Polymer.dom(this).host || document;
if (this.autoHide) {
this._attacheListeners();
}
this.listen(window, 'scroll', '_scrollHandler');
},
detached: function() {
this._detachListeners();
this.unlisten(window, 'scroll', '_scrollHandler');
},
_autoHideChanged: function(state) {
if (state) {
this._attacheListeners();
} else {
this._detachListeners();
}
},
_attacheListeners: function() {
if (!this._eventTarget) {
this.hidden = true;
return;
}
this._detachListeners();
this.listen(this._eventTarget, 'raml-is-resource-changed',
'_isResourceHandler');
this.listen(this._eventTarget, 'raml-selected-object-changed',
'_selectedChangeHandler');
this.listen(this._eventTarget, 'raml-selected-path-changed',
'_selectedPathChangeHandler');
},
_detachListeners: function() {
if (!this._eventTarget) {
return;
}
this.unlisten(this._eventTarget, 'raml-is-resource-changed',
'_isResourceHandler');
this.unlisten(this._eventTarget, 'raml-selected-object-changed',
'_selectedChangeHandler');
this.unlisten(this._eventTarget, 'raml-selected-path-changed',
'_selectedPathChangeHandler');
},
/**
* It's a handler for the `raml-is-resource-changed` event fired
* by other element. It sets the `hidden` value depending on the
* event's `state` parameter.
*/
_isResourceHandler: function(e, detail) {
var state = detail.state;
this.hidden = !state;
},
/**
* Handler for the `selectedObject` (from the `<raml-path-to-object>`).
*/
_selectedChangeHandler: function(e, detail) {
if (this.hidden) {
this.raml = undefined;
return;
}
this.raml = detail.selectedObject;
},
_selectedPathChangeHandler: function(e, detail) {
if (this.hidden) {
this.currentPath = undefined;
return;
}
this.currentPath = detail.path;
},
// Computes the name of the resource.
_computeResourceName: function() {
var r = this.raml;
if (!r) {
return '';
}
return r.displayName || r.relativeUri;
},
_objectChanged: function(record) {
var raml = record && record.base;
if (!raml) {
this._setHasTraits(false);
this._setHasResourceTypes(false);
this._setHasAnnotations(false);
this._setTypes(undefined);
return;
}
if (raml.is && raml.is.length) {
this._setHasTraits(true);
} else {
this._setHasTraits(false);
}
if (raml.type && (typeof raml.type === 'string' || raml.type.length)) {
this._setHasResourceTypes(true);
} else {
this._setHasResourceTypes(false);
}
if (raml.annotations && raml.annotations.length) {
this._setHasAnnotations(true);
} else {
this._setHasAnnotations(false);
}
this._computeTypes(raml);
},
// Computes list of the types in current resource. Called automatically when `raml` change.
_computeTypes: function(raml) {
var types = [];
var self = this;
if (raml.methods && raml.methods.length) {
raml.methods.forEach(function(method) {
var type = self._listTypes(method);
if (type) {
types = types.concat(type);
}
});
}
this._filterDuplicates(types);
this._setTypes(types);
},
/**
* Filters out duplicates of types so the list contain only one instance of
* a type.
*
* @param {Array} types List of computed types.
* @return {Array} list without duplicates.
*/
_filterDuplicates: function(types) {
if (!types || !types.length) {
return types;
}
for (var i = 0; i < types.length; i++) {
var current = types[i][0];
for (var j = types.length - 1; j > i; j--) {
var compare = types[j][0];
if (this.deepEqual(current, compare)) {
types.splice(j, 1);
}
}
}
},
_listTypes: function(obj) {
var self = this;
var types = [];
var type = self._extractType(obj);
if (type) {
types[types.length] = type;
}
if (obj.responses) {
var keys = Object.keys(obj.responses);
keys.forEach(function(status) {
var response = obj.responses[status];
var type = self._extractType(response);
if (type) {
types[types.length] = type;
}
});
}
return types;
},
_extractType: function(obj) {
if (!obj || !obj.body || !obj.body.length) {
return;
}
var body = obj.body;
if (body instanceof Array) {
body = body.filter(function(type) {
if (!type.description && !type.properties && !type.items) {
return false;
}
return true;
});
if (!body.length) {
return;
}
} else {
if (!body.description && !body.properties && !body.items) {
return;
}
}
return body;
},
// Computes if the resource has defined types. It will revalidate when `types` array change.
_computeHasTypes: function() {
return !!(this.types && this.types.length);
},
_computeMethods: function(currentPath) {
var r = this.raml;
if (!currentPath || !r || !r.methods || !r.methods.length) {
this.methods = null;
return;
}
var methods = [];
r.methods.forEach(function(method, i) {
methods[methods.length] = {
path: currentPath + '.methods.' + i,
meta: {
name: method.displayName || method.method,
description: method.description
}
};
});
if (methods.length === 0) {
methods = null;
}
this.methods = methods;
},
_computeResources: function(currentPath) {
var r = this.raml;
if (!currentPath || !r || !r.resources || !r.resources.length) {
this.resources = null;
return;
}
var resources = [];
r.resources.forEach(function(resource, i) {
resources[resources.length] = {
path: currentPath + '.resources.' + i,
meta: {
name: resource.displayName || resource.relativeUri,
description: resource.description
}
};
});
if (resources.length === 0) {
resources = null;
}
this.resources = resources;
},
// Computes the value for the hasSubproperties property.
_computeHasSubproperties: function(methods, resources) {
return !!((methods && methods.length) || (resources && resources.length));
},
_subResourceTap: function(e) {
e.preventDefault();
e.stopPropagation();
var path = e.target.dataset.path;
if (!path) {
return;
}
// this.set('currentPath', path);
this.fire('raml-path-changed', {
path: path
});
},
_navLinksTap: function(e) {
e.preventDefault();
e.stopPropagation();
var targetId = e.target.getAttribute('href');
if (!targetId) {
return;
}
var target = this.$$(targetId);
this.scroll(0, target.offsetTop);
},
_scrollHandler: function() {
var elm = this.$$('#nav-links');
var parent = elm.parentNode;
var top = parent.getBoundingClientRect().top;
if (top < 24) {
var _top = Math.abs(top) + 24;
elm.style.top = _top + 'px';
} else {
elm.style.removeProperty('top');
}
},
// Computes class for the toggle's button icon.
_computeToggleIconClass: function(opened) {
var clazz = 'toggle-icon';
if (opened) {
clazz += ' opened';
}
return clazz;
},
// Computes a label for the section toggle buttons.
_computeToggleActionLabel: function(opened) {
return opened ? 'Hide' : 'Show';
},
/**
* Toogles types section opened / closed.
*/
toggleTypesCollapse: function() {
this.typesOpened = !this.typesOpened;
},
/**
* Toogles types section opened / closed.
*/
toggleMethodsCollapse: function() {
this.methodsOpened = !this.methodsOpened;
},
/**
* Computes the sate of the `isWideLayout` property.
*/
_computeIsWideLayout: function(narrow, wide) {
return !narrow && wide;
},
// Computes list of traits names to display in the view.
_computeTraitsList: function(traits) {
if (!traits || !traits.length) {
return;
}
return traits.map(function(trait) {
if (typeof trait === 'string') {
return trait;
}
if (trait.name) {
return trait.name;
}
try {
var keys = Object.keys(trait);
return keys[0];
} catch (e) {}
}).join(', ');
},
// Computes list of resource type names to display in the view.
_computeResourceTypesList: function(type) {
if (!type) {
return;
}
if (typeof type === 'string') {
return type;
}
return type.map(function(item) {
if (typeof item === 'string') {
return item;
}
if (item.name) {
return item.name;
}
try {
var keys = Object.keys(item);
return keys[0];
} catch (e) {}
}).join(', ');
},
/**
* Checks if an objects deeply equals other object
*
* @param {Object} a First object to compare
* @param {object} b Second object to compare
* @return {Boolean} True if both objects have the same list of properties
* and have the same values in properties.
*/
deepEqual: function(a, b) {
var i;
if ((typeof a === 'object' && a) && (typeof b === 'object' && b)) {
var keysA = Object.keys(a);
var keysB = Object.keys(b);
var keyAlength = keysA.length;
var keyBlength = keysB.length;
if (keyAlength !== keyBlength) {
return false;
}
for (i = 0; i < keyAlength; i++) {
var prop = keysA[i];
if (!b.hasOwnProperty(prop)) {
return false;
}
if (!this.deepEqual(a[prop], b[prop])) {
return false;
}
}
return true;
} else if (a instanceof Array && b instanceof Array) {
var aLength = a.length;
var bLength = b.length;
if (aLength !== bLength) {
return false;
}
for (i = 0; i < aLength; i++) {
if (!this.deepEqual(a[i], b[i])) {
return false;
}
}
return true;
} else if (a !== b) {
return false;
}
return true;
}
});
</script>
</dom-module>