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

Commit 431b748

Browse files
dandoyonIgorMinar
authored andcommitted
doc(sample): Add javascript sandbox integration (jsFiddle)
Change doc_widget.js to: - render "edit in jsfiddle" button next to all examples - make opt out certain examples by adding jsfiddle="false" attribute to doc:source element
1 parent de34ca0 commit 431b748

File tree

6 files changed

+78
-6
lines changed

6 files changed

+78
-6
lines changed

docs/src/templates/doc_widgets.css

+31
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,37 @@ ul.doc-example > li.doc-example-heading {
1919
margin-bottom: -10px;
2020
}
2121

22+
span.nojsfiddle {
23+
float: right;
24+
font-size: 14px;
25+
margin-right:10px;
26+
margin-top: 10px;
27+
}
28+
29+
form.jsfiddle {
30+
position: absolute;
31+
right: 0;
32+
z-index: 1;
33+
height: 14px;
34+
}
35+
36+
form.jsfiddle button {
37+
cursor: pointer;
38+
padding: 4px 10px;
39+
margin: 10px;
40+
background-color: #FFF;
41+
font-weight: bold;
42+
color: #7989D6;
43+
border-color: #7989D6;
44+
-moz-border-radius: 8px;
45+
-webkit-border-radius:8px;
46+
border-radius: 8px;
47+
}
48+
49+
form.jsfiddle textarea, form.jsfiddle input {
50+
display: none;
51+
}
52+
2253
li.doc-example-live {
2354
padding: 10px;
2455
font-size: 1.2em;

docs/src/templates/doc_widgets.js

+42-1
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@
2828
//jqlite instead. jqlite's find() method currently supports onlt getElementsByTagName!
2929
var example = element.find('pre').eq(0), //doc-source
3030
exampleSrc = example.text(),
31+
jsfiddle = element.find('doc:source').attr('jsfiddle') || true,
3132
scenario = element.find('pre').eq(1); //doc-scenario
3233

3334
var code = indent(exampleSrc);
3435
var tabHtml =
3536
'<ul class="doc-example">' +
3637
'<li class="doc-example-heading"><h3>Source</h3></li>' +
3738
'<li class="doc-example-source" ng:non-bindable>' +
38-
'<pre class="brush: js; html-script: true; highlight: [' +
39+
jsFiddleButton(jsfiddle) + // may or may not have value
40+
'<pre class="brush: js; html-script: true; highlight: [' +
3941
code.hilite + ']; toolbar: false;"></pre></li>' +
4042
'<li class="doc-example-heading"><h3>Live Preview</h3></li>' +
4143
'<li class="doc-example-live">' + exampleSrc +'</li>';
@@ -59,6 +61,45 @@
5961
} catch (e) {
6062
alert(e);
6163
}
64+
65+
function jsFiddleButton(jsfiddle) {
66+
if (jsfiddle !== 'false') {
67+
if(jsfiddle == true) {
68+
//dynamically generate a fiddle
69+
var fiddleUrl = 'http://jsfiddle.net/api/post/library/pure/',
70+
fiddleSrc = exampleSrc,
71+
stripIndent = fiddleSrc.match(/^(\s*)/)[1].length;
72+
73+
//escape closing textarea
74+
fiddleSrc = fiddleSrc.replace(/<\/textarea>/gi,'&lt;/textarea&gt;')
75+
//strip extra indentation
76+
fiddleSrc = fiddleSrc.replace(new RegExp('^\\s{' + stripIndent + '}', 'gm'), '');
77+
78+
return '<form class="jsfiddle" method="post" action="' + fiddleUrl + '" target="_blank">' +
79+
'<textarea name="css">' +
80+
'body { font-family: Arial,Helvetica,sans-serif; }\n' +
81+
'body, td, th { font-size: 14px; margin: 0; }\n' +
82+
'table { border-collapse: separate; border-spacing: 2px; display: table; margin-bottom: 0; margin-top: 0; -moz-box-sizing: border-box; text-indent: 0; }\n' +
83+
'a:link, a:visited, a:hover { color: #5D6DB6; text-decoration: none; }\n' +
84+
'</textarea>' +
85+
'<input type="text" name="title" value="AngularJS Live Example">' +
86+
'<textarea name="html">' +
87+
'<script src="' + angularJsUrl + '" ng:autobind></script>\n\n' +
88+
'<!-- AngularJS Example Code: -->\n\n' +
89+
fiddleSrc +
90+
'</textarea>' +
91+
'<button>edit at jsFiddle</button>' +
92+
'</form>';
93+
} else {
94+
//use existing fiddle
95+
fiddleUrl = "http://jsfiddle.net" + jsfiddle;
96+
return '<form class="jsfiddle" method="get" action="' + fiddleUrl + '" target="_blank">' +
97+
'<button>edit at jsFiddle</button>' +
98+
'</form>';
99+
}
100+
}
101+
return '';
102+
}
62103
});
63104

64105
function indent(text) {

src/service/resource.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157
158158
Let's look at what a buzz client created with the `$resource` service looks like:
159159
<doc:example>
160-
<doc:source>
160+
<doc:source jsfiddle="false">
161161
<script>
162162
function BuzzController($resource) {
163163
this.Activity = $resource(

src/service/route.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
Try changing the URL in the input box to see changes.
2323
2424
<doc:example>
25-
<doc:source>
25+
<doc:source jsfiddle="false">
2626
<script>
2727
function MainCntl($route, $location) {
2828
this.$route = $route;

src/service/xhr.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
*
108108
* @example
109109
<doc:example>
110-
<doc:source>
110+
<doc:source jsfiddle="false">
111111
<script>
112112
function FetchCntl($xhr) {
113113
var self = this;

src/widgets.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ angularWidget('select', function(element){
949949
*
950950
* @example
951951
<doc:example>
952-
<doc:source>
952+
<doc:source jsfiddle="false">
953953
<select name="url">
954954
<option value="examples/ng-include/template1.html">template1.html</option>
955955
<option value="examples/ng-include/template2.html">template2.html</option>
@@ -1375,7 +1375,7 @@ angularWidget("@ng:non-bindable", noop);
13751375
*
13761376
* @example
13771377
<doc:example>
1378-
<doc:source>
1378+
<doc:source jsfiddle="false">
13791379
<script>
13801380
function MyCtrl($route) {
13811381
$route.when('/overview',

0 commit comments

Comments
 (0)