This repository was archived by the owner on Dec 4, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 877
/
Copy pathlive-example.js
131 lines (118 loc) · 5.15 KB
/
live-example.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
/**
* Angular.io Live Example Directive
*
* Renders a link to a live/host example of the doc page
* app this directive is contained in.
*
* Usage:
* <live-example
* [name="..."]
* [plnkr='...']
* [noSource]
* [embedded]
* [embedded-style | flat-style]
* [srcText="..."]
* [title="..."]>text</live-example>
* Example:
* <p>Run <live-example>Try the live example</live-example></p>.
* // ~/resources/live-examples/{page}/ts/plnkr.html
*
* <p>Run <live-example name="toh-1">this example</live-example></p>.
* // ~/resources/live-examples/toh-1/ts/plnkr.html
*
* // Link to the default plunker in the toh-1 sample
* // The title overrides default ("live example") with "Tour of Heroes - Part 1"
* <p>Run <live-example name="toh-1" title="Tour of Heroes - Part 1"></live-example></p>.
* // ~/resources/live-examples/toh-1/ts/plnkr.html
*
* <p>Run <live-example plnkr="minimal"></live-example></p>.
* // ~/resources/live-examples/{page}/ts/minimal.plnkr.html
*
* // Embed the current page's default plunker
* // Text within tag is "live example"
* // No title (no tooltip)
* <live-example embedded title=""></live-example>
* // ~/resources/live-examples/{page}/ts/eplnkr.html
*
* // Links to a *new* tab as an embedded style plunker editor
* <live-example embedded-style>this example</live-example>
* // ~/resources/live-examples/{page}//ts/eplnkr.html
*
* // Links to a *new* tab in the flat (original editor) style plunker editor
* <live-example flat-style>this example</live-example>
* // ~/resources/live-examples/{page}//ts/eplnkr.html
*
* // Displays in *same* tab as an embedded style plunker editor
* <live-example name="toh-1" embedded plnkr="minimal" img="toh>Tour of Heroes - Part 1</live-example>
* // ~/resources/live-examples/toh-1/ts/minimal.eplnkr.html
*/
angularIO.directive('liveExample', ['$location', function ($location) {
function a(text, attrs) {
var attr = (attrs.href ? ' href="' + attrs.href + '"' : '') +
(attrs.target ? ' target="' + attrs.target + '"' : '');
return '<a' + attr + '>' + text + '</a>';
}
function span(text) { return '<span>' + text + '</span>'; }
function embeddedTemplate(src, img, zipHref) {
return '<div ng-if="embeddedShow">' +
'<iframe frameborder="0" width="100%" height="100%" src="' + src + '"></iframe>' +
'</div>' +
'<img ng-click="toggleEmbedded()" ng-if="!embeddedShow" src="' + img + '" alt="plunker">' +
'<p>You can also <a href="' + zipHref +'">download this example.</p>';
}
return {
restrict: 'E',
scope: true,
compile: function (tElement, attrs) {
var href, template;
var text = tElement.text() || 'live example';
if (attrs['title'] == undefined) { tElement[0].setAttribute('title', text); } // set default title (tooltip)
var ex = attrs.name || NgIoUtil.getExampleName($location);
var embedded = attrs.hasOwnProperty('embedded');
var noDownload = attrs.hasOwnProperty('nodownload') || attrs.hasOwnProperty('noDownload');
var flatStyle = attrs.hasOwnProperty('flatstyle') || attrs.hasOwnProperty('flatStyle');
var embeddedStyle = embedded || attrs.hasOwnProperty('embeddedstyle') || attrs.hasOwnProperty('embeddedStyle');
var plnkr = (embeddedStyle || !flatStyle) ? 'eplnkr' : 'plnkr';
var zipHref = ex;
var imageBase = '/resources/images/';
var defaultImg = 'plunker/placeholder.png';
if (attrs.plnkr) {
plnkr = attrs.plnkr + '.' + plnkr;
zipHref = attrs.plnkr + '.' + zipHref;
}
var isForDart = attrs.lang === 'dart' || NgIoUtil.isDoc($location, 'dart');
var isForJs = attrs.lang === 'js' || NgIoUtil.isDoc($location, 'js');
var exLang = isForDart ? 'dart' : isForJs ? 'js' : 'ts';
zipHref = '/resources/zips/' + ex + '/' + zipHref + '.zip';
if (embedded && !isForDart) {
href = '/resources/live-examples/' + ex + '/' + exLang + '/' + plnkr + '.html';
img = imageBase + (attrs.img || defaultImg);
template = embeddedTemplate(href, img, zipHref);
} else {
var href = isForDart
? 'http://angular-examples.github.io/' + ex
: '/resources/live-examples/' + ex + '/' + exLang + '/' + plnkr + '.html'
// Link to live example.
var template = a(text, { href: href, target: '_blank' });
if (!noDownload) {
template += ' / ' + a('downloadable example', { href: zipHref, target: '_blank' });
}
// The hosted example and sources are in different locations for Dart.
// Also show link to sources for Dart, unless noSource is specified.
if (isForDart && !attrs.hasOwnProperty('nosource')) {
var srcText = attrs.srcText || 'view source';
var srcHref = 'http://github.com/angular-examples/' + ex;
template = span(template + ' (' + a(srcText, { href: srcHref, target: '_blank' }) + ')');
}
}
// UPDATE ELEMENT WITH NEW TEMPLATE
tElement.html(template);
// RETURN ELEMENT
return function (scope, element, attrs) {
scope.toggleEmbedded = function() {
scope.embeddedShow = !scope.embeddedShow;
}
};
}
};
}]);