Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit bdec35c

Browse files
docs(examples): use form POST to create Plunkers
The previous solution for opening Plunkers from the docs relied on tight coupling between the docs site and the plunkr site, in particular the URL to the example code on the docs server was hard coded in the Plunker site. This change goes back to the old POST method of creating a Plunker, but with a subtle difference: In the very old docs, the content was injected directly into the example HTML at build time. This was easy enough to do as the example actually ran in the current page but also increased the size of the doc page. The new examples are run in completely separate iframes. This new version of showing a Plunker loads the file content for the Plunker from the server by accessing the example's manifest.json file using $http requests. This also has the additional benefit that you can now generate plunkers from examples that are running locally or, frankly, in any folder on any server, such as personal builds on the Jenkins CI server. Closes #7186 Closes #7198
1 parent f0da1a3 commit bdec35c

File tree

4 files changed

+62
-248
lines changed

4 files changed

+62
-248
lines changed

docs/app/src/docs.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
angular.module('DocsController', [])
22

3-
.controller('DocsController', function($scope, $rootScope, $location, $window, $cookies, NG_PAGES, NG_NAVIGATION, NG_VERSION) {
3+
.controller('DocsController', [
4+
'$scope', '$rootScope', '$location', '$window', '$cookies', 'openPlunkr',
5+
'NG_PAGES', 'NG_NAVIGATION', 'NG_VERSION',
6+
function($scope, $rootScope, $location, $window, $cookies, openPlunkr,
7+
NG_PAGES, NG_NAVIGATION, NG_VERSION) {
8+
9+
10+
$scope.openPlunkr = openPlunkr;
411

512
$scope.docsVersion = NG_VERSION.isSnapshot ? 'snapshot' : NG_VERSION.version;
6-
13+
714
$scope.fold = function(url) {
815
if(url) {
916
$scope.docs_fold = '/notes/' + url;
@@ -120,4 +127,4 @@ angular.module('DocsController', [])
120127
});
121128
}
122129
});
123-
});
130+
}]);

docs/app/src/examples.js

+49-243
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,6 @@
11
angular.module('examples', [])
22

3-
.directive('sourceEdit', function(getEmbeddedTemplate) {
4-
return {
5-
template: '<div class="btn-group pull-right">' +
6-
'<a class="btn dropdown-toggle btn-primary" data-toggle="dropdown" href>' +
7-
' <i class="icon-pencil icon-white"></i> Edit<span class="caret"></span>' +
8-
'</a>' +
9-
'<ul class="dropdown-menu">' +
10-
' <li><a ng-click="plunkr($event)" href="">In Plunkr</a></li>' +
11-
' <li><a ng-click="fiddle($event)" href="">In JsFiddle</a></li>' +
12-
'</ul>' +
13-
'</div>',
14-
scope: true,
15-
controller: function($scope, $attrs, openJsFiddle, openPlunkr) {
16-
var sources = {
17-
module: $attrs.sourceEdit,
18-
deps: read($attrs.sourceEditDeps),
19-
html: read($attrs.sourceEditHtml),
20-
css: read($attrs.sourceEditCss),
21-
js: read($attrs.sourceEditJs),
22-
json: read($attrs.sourceEditJson),
23-
unit: read($attrs.sourceEditUnit),
24-
scenario: read($attrs.sourceEditScenario)
25-
};
26-
$scope.fiddle = function(e) {
27-
e.stopPropagation();
28-
openJsFiddle(sources);
29-
};
30-
$scope.plunkr = function(e) {
31-
e.stopPropagation();
32-
openPlunkr(sources);
33-
};
34-
}
35-
};
36-
37-
function read(text) {
38-
var files = [];
39-
angular.forEach(text ? text.split(' ') : [], function(refId) {
40-
// refId is index.html-343, so we need to strip the unique ID when exporting the name
41-
files.push({name: refId.replace(/-\d+$/, ''), content: getEmbeddedTemplate(refId)});
42-
});
43-
return files;
44-
}
45-
})
46-
47-
48-
.factory('angularUrls', function($document) {
49-
var urls = {};
50-
51-
angular.forEach($document.find('script'), function(script) {
52-
var match = script.src.match(/^.*\/(angular[^\/]*\.js)$/);
53-
if (match) {
54-
urls[match[1].replace(/(\-\d.*)?(\.min)?\.js$/, '.js')] = match[0];
55-
}
56-
});
57-
58-
return urls;
59-
})
60-
61-
62-
.factory('formPostData', function($document) {
3+
.factory('formPostData', ['$document', function($document) {
634
return function(url, fields) {
645
var form = angular.element('<form style="display: none;" method="post" action="' + url + '" target="_blank"></form>');
656
angular.forEach(fields, function(value, name) {
@@ -71,196 +12,61 @@ angular.module('examples', [])
7112
form[0].submit();
7213
form.remove();
7314
};
74-
})
75-
15+
}])
7616

77-
.factory('prepareDefaultAppModule', function() {
78-
return function(content) {
79-
var deps = [];
80-
angular.forEach(content.deps, function(file) {
81-
if(file.name == 'angular-animate.js') {
82-
deps.push('ngAnimate');
83-
}
84-
});
85-
86-
var moduleName = 'App';
87-
return {
88-
module : moduleName,
89-
script : "angular.module('" + moduleName + "', [" +
90-
(deps.length ? "'" + deps.join("','") + "'" : "") + "]);\n\n"
91-
};
92-
};
93-
})
9417

95-
.factory('prepareEditorAssetTags', function(angularUrls) {
96-
return function(content, options) {
97-
options = options || {};
98-
var includeLocalFiles = options.includeLocalFiles;
99-
var html = makeScriptTag(angularUrls['angular.js']);
100-
101-
var allFiles = [].concat(content.js, content.css, content.html, content.json);
102-
angular.forEach(content.deps, function(file) {
103-
if (file.name !== 'angular.js') {
104-
var isLocal = false;
105-
for(var i=0;i<allFiles.length;i++) {
106-
if(allFiles[i].name == file.name) {
107-
isLocal = true;
108-
break;
109-
}
110-
}
111-
if(!(isLocal && !includeLocalFiles)) {
112-
var assetUrl = angularUrls[file.name] || file.name;
113-
html += makeScriptTag(assetUrl);
114-
}
115-
}
116-
});
117-
118-
if(includeLocalFiles) {
119-
angular.forEach(content.css, function(file, index) {
120-
html += makeCssLinkTag(file.name);
121-
});
122-
}
123-
124-
return html;
125-
126-
127-
function makeScriptTag(src) {
128-
return '<script type="text/javascript" src="' + src + '"></script>\n';
129-
}
130-
131-
function makeCssLinkTag(src) {
132-
return '<link rel="stylesheet" type="text/css" href="' + src + '" />\n';
133-
}
134-
};
135-
})
136-
137-
138-
.factory('openPlunkr', function(templateMerge, formPostData, prepareEditorAssetTags, prepareDefaultAppModule) {
139-
return function(content) {
140-
var hasRouting = false;
141-
angular.forEach(content.deps, function(file) {
142-
hasRouting = hasRouting || file.name == 'angular-route.js';
143-
});
144-
var indexHtmlContent = '<!doctype html>\n' +
145-
'<html ng-app="{{module}}">\n' +
146-
' <head>\n' +
147-
'{{scriptDeps}}';
18+
.factory('openPlunkr', ['formPostData', '$http', '$q', function(formPostData, $http, $q) {
19+
return function(exampleFolder) {
14820

149-
if(hasRouting) {
150-
indexHtmlContent += '<script type="text/javascript">\n' +
151-
'//this is here to make plunkr work with AngularJS routing\n' +
152-
'angular.element(document.getElementsByTagName(\'head\')).append(' +
153-
'angular.element(\'<base href="\' + window.location.pathname + \'" />\')' +
154-
');\n' +
155-
'</script>\n';
156-
}
21+
var exampleName = 'AngularJS Example';
15722

158-
indexHtmlContent += '</head>\n' +
159-
' <body>\n\n' +
160-
'{{indexContents}}\n\n' +
161-
' </body>\n' +
162-
'</html>\n';
23+
// Load the manifest for the example
24+
$http.get(exampleFolder + '/manifest.json')
25+
.then(function(response) {
26+
return response.data;
27+
})
28+
.then(function(manifest) {
29+
var filePromises = [];
16330

164-
indexProp = {
165-
module: content.module,
166-
scriptDeps: prepareEditorAssetTags(content, { includeLocalFiles : true }),
167-
indexContents: content.html[0].content
168-
};
169-
170-
var allFiles = [].concat(content.js, content.css, content.html, content.json);
171-
172-
if(!content.module) {
173-
var moduleData = prepareDefaultAppModule(content);
174-
indexProp.module = moduleData.module;
175-
176-
var found = false;
177-
angular.forEach(content.js, function(file) {
178-
if(file.name == 'script.js') {
179-
file.content = moduleData.script + file.content;
180-
found = true;
181-
}
182-
});
183-
if(!found) {
184-
indexProp.scriptDeps += '<script type="text/javascript" src="script.js"></script>\n';
185-
allFiles.push({
186-
name : 'script.js',
187-
content : moduleData.script
31+
// Build a pretty title for the Plunkr
32+
var exampleNameParts = manifest.name.split('-');
33+
exampleNameParts.unshift('AngularJS');
34+
angular.forEach(exampleNameParts, function(part, index) {
35+
exampleNameParts[index] = part.charAt(0).toUpperCase() + part.substr(1);
18836
});
189-
}
190-
}
191-
192-
var postData = {};
193-
194-
angular.forEach(allFiles, function(file, index) {
195-
if (file.content && file.name != 'index.html') {
196-
postData['files[' + file.name + ']'] = file.content;
197-
}
198-
});
199-
200-
postData['files[index.html]'] = templateMerge(indexHtmlContent, indexProp);
201-
postData['tags[]'] = "angularjs";
202-
203-
postData.private = true;
204-
postData.description = 'AngularJS Example Plunkr';
205-
206-
formPostData('http://plnkr.co/edit/?p=preview', postData);
207-
};
208-
})
209-
210-
.factory('openJsFiddle', function(templateMerge, formPostData, prepareEditorAssetTags, prepareDefaultAppModule) {
211-
var HTML = '<div ng-app=\"{{module}}\">\n{{html:2}}</div>',
212-
CSS = '</style> <!-- Ugly Hack to make remote files preload in jsFiddle --> \n' +
213-
'{{head:0}}<style>{{css}}',
214-
SCRIPT = '{{script}}',
215-
SCRIPT_CACHE = '\n\n<!-- {{name}} -->\n<script type="text/ng-template" id="{{name}}">\n{{content:2}}</script>',
216-
BASE_HREF_TAG = '<!-- Ugly Hack to make AngularJS routing work inside of jsFiddle -->\n' +
217-
'<base href="/" />\n\n';
218-
219-
return function(content) {
220-
var prop = {
221-
module: content.module,
222-
html: '',
223-
css: '',
224-
script: ''
225-
};
226-
if(!prop.module) {
227-
var moduleData = prepareDefaultAppModule(content);
228-
prop.script = moduleData.script;
229-
prop.module = moduleData.module;
230-
}
231-
232-
angular.forEach(content.html, function(file, index) {
233-
if (index) {
234-
prop.html += templateMerge(SCRIPT_CACHE, file);
235-
} else {
236-
prop.html += file.content;
237-
}
238-
});
239-
240-
prop.head = prepareEditorAssetTags(content, { includeLocalFiles : false });
241-
242-
angular.forEach(content.js, function(file, index) {
243-
prop.script += file.content;
244-
});
37+
exampleName = exampleNameParts.join(' - ');
38+
39+
angular.forEach(manifest.files, function(filename) {
40+
filePromises.push($http.get(exampleFolder + '/' + filename)
41+
.then(function(response) {
42+
43+
// The manifests provide the production index file but Plunkr wants
44+
// a straight index.html
45+
if (filename === "index-production.html") {
46+
filename = "index.html"
47+
}
48+
49+
return {
50+
name: filename,
51+
content: response.data
52+
};
53+
}));
54+
});
55+
return $q.all(filePromises);
56+
})
57+
.then(function(files) {
58+
var postData = {};
24559

246-
angular.forEach(content.css, function(file, index) {
247-
prop.css += file.content;
248-
});
60+
angular.forEach(files, function(file) {
61+
postData['files[' + file.name + ']'] = file.content;
62+
});
24963

250-
var hasRouting = false;
251-
angular.forEach(content.deps, function(file) {
252-
hasRouting = hasRouting || file.name == 'angular-route.js';
253-
});
64+
postData['tags[0]'] = "angularjs";
65+
postData['tags[1]'] = "example";
66+
postData.private = true;
67+
postData.description = exampleName;
25468

255-
var compiledHTML = templateMerge(HTML, prop);
256-
if(hasRouting) {
257-
compiledHTML = BASE_HREF_TAG + compiledHTML;
258-
}
259-
formPostData("http://jsfiddle.net/api/post/library/pure/", {
260-
title: 'AngularJS Example',
261-
html: compiledHTML,
262-
js: templateMerge(SCRIPT, prop),
263-
css: templateMerge(CSS, prop)
264-
});
69+
formPostData('http://plnkr.co/edit/?p=preview', postData);
70+
});
26571
};
266-
});
72+
}]);

docs/app/test/docsSpec.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ describe("DocsController", function() {
33

44
angular.module('fake', [])
55
.value('$cookies', {})
6+
.value('openPlunkr', function() {})
67
.value('NG_PAGES', {})
78
.value('NG_NAVIGATION', {})
89
.value('NG_VERSION', {});

docs/config/templates/runnableExample.template.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
is HTML and wrap each line in a <p> - thus breaking the HTML #}
33

44
<div>
5-
<a ng-href="http://plnkr.co/edit/ngdoc:{$ doc.example.id $}@{{docsVersion}}?p=preview" class="btn pull-right" target="_blank">
5+
<a ng-click="openPlunkr('{$ doc.example.outputFolder $}')" class="btn pull-right">
66
<i class="glyphicon glyphicon-edit">&nbsp;</i>
77
Edit in Plunker</a>
88
<div class="runnable-example"
@@ -15,7 +15,7 @@
1515
{$ attrName $}="{$ attrValue $}"{% endfor %}>
1616
{% code -%}
1717
{$ file.fileContents $}
18-
{%- endcode %}
18+
{%- endcode %}
1919
</div>
2020
{% endfor %}
2121

0 commit comments

Comments
 (0)