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
This commit is contained in:
dandoyon 2011-08-10 20:53:56 -07:00 committed by Igor Minar
parent de34ca0b64
commit 431b748cac
6 changed files with 78 additions and 6 deletions

View file

@ -19,6 +19,37 @@ ul.doc-example > li.doc-example-heading {
margin-bottom: -10px;
}
span.nojsfiddle {
float: right;
font-size: 14px;
margin-right:10px;
margin-top: 10px;
}
form.jsfiddle {
position: absolute;
right: 0;
z-index: 1;
height: 14px;
}
form.jsfiddle button {
cursor: pointer;
padding: 4px 10px;
margin: 10px;
background-color: #FFF;
font-weight: bold;
color: #7989D6;
border-color: #7989D6;
-moz-border-radius: 8px;
-webkit-border-radius:8px;
border-radius: 8px;
}
form.jsfiddle textarea, form.jsfiddle input {
display: none;
}
li.doc-example-live {
padding: 10px;
font-size: 1.2em;

View file

@ -28,6 +28,7 @@
//jqlite instead. jqlite's find() method currently supports onlt getElementsByTagName!
var example = element.find('pre').eq(0), //doc-source
exampleSrc = example.text(),
jsfiddle = element.find('doc:source').attr('jsfiddle') || true,
scenario = element.find('pre').eq(1); //doc-scenario
var code = indent(exampleSrc);
@ -35,7 +36,8 @@
'<ul class="doc-example">' +
'<li class="doc-example-heading"><h3>Source</h3></li>' +
'<li class="doc-example-source" ng:non-bindable>' +
'<pre class="brush: js; html-script: true; highlight: [' +
jsFiddleButton(jsfiddle) + // may or may not have value
'<pre class="brush: js; html-script: true; highlight: [' +
code.hilite + ']; toolbar: false;"></pre></li>' +
'<li class="doc-example-heading"><h3>Live Preview</h3></li>' +
'<li class="doc-example-live">' + exampleSrc +'</li>';
@ -59,6 +61,45 @@
} catch (e) {
alert(e);
}
function jsFiddleButton(jsfiddle) {
if (jsfiddle !== 'false') {
if(jsfiddle == true) {
//dynamically generate a fiddle
var fiddleUrl = 'http://jsfiddle.net/api/post/library/pure/',
fiddleSrc = exampleSrc,
stripIndent = fiddleSrc.match(/^(\s*)/)[1].length;
//escape closing textarea
fiddleSrc = fiddleSrc.replace(/<\/textarea>/gi,'&lt;/textarea&gt;')
//strip extra indentation
fiddleSrc = fiddleSrc.replace(new RegExp('^\\s{' + stripIndent + '}', 'gm'), '');
return '<form class="jsfiddle" method="post" action="' + fiddleUrl + '" target="_blank">' +
'<textarea name="css">' +
'body { font-family: Arial,Helvetica,sans-serif; }\n' +
'body, td, th { font-size: 14px; margin: 0; }\n' +
'table { border-collapse: separate; border-spacing: 2px; display: table; margin-bottom: 0; margin-top: 0; -moz-box-sizing: border-box; text-indent: 0; }\n' +
'a:link, a:visited, a:hover { color: #5D6DB6; text-decoration: none; }\n' +
'</textarea>' +
'<input type="text" name="title" value="AngularJS Live Example">' +
'<textarea name="html">' +
'<script src="' + angularJsUrl + '" ng:autobind></script>\n\n' +
'<!-- AngularJS Example Code: -->\n\n' +
fiddleSrc +
'</textarea>' +
'<button>edit at jsFiddle</button>' +
'</form>';
} else {
//use existing fiddle
fiddleUrl = "http://jsfiddle.net" + jsfiddle;
return '<form class="jsfiddle" method="get" action="' + fiddleUrl + '" target="_blank">' +
'<button>edit at jsFiddle</button>' +
'</form>';
}
}
return '';
}
});
function indent(text) {

View file

@ -157,7 +157,7 @@
Let's look at what a buzz client created with the `$resource` service looks like:
<doc:example>
<doc:source>
<doc:source jsfiddle="false">
<script>
function BuzzController($resource) {
this.Activity = $resource(

View file

@ -22,7 +22,7 @@
Try changing the URL in the input box to see changes.
<doc:example>
<doc:source>
<doc:source jsfiddle="false">
<script>
function MainCntl($route, $location) {
this.$route = $route;

View file

@ -107,7 +107,7 @@
*
* @example
<doc:example>
<doc:source>
<doc:source jsfiddle="false">
<script>
function FetchCntl($xhr) {
var self = this;

View file

@ -949,7 +949,7 @@ angularWidget('select', function(element){
*
* @example
<doc:example>
<doc:source>
<doc:source jsfiddle="false">
<select name="url">
<option value="examples/ng-include/template1.html">template1.html</option>
<option value="examples/ng-include/template2.html">template2.html</option>
@ -1375,7 +1375,7 @@ angularWidget("@ng:non-bindable", noop);
*
* @example
<doc:example>
<doc:source>
<doc:source jsfiddle="false">
<script>
function MyCtrl($route) {
$route.when('/overview',