@@ -357,32 +357,85 @@ docsApp.serviceFactory.formPostData = function($document) {
357
357
} ;
358
358
} ;
359
359
360
- docsApp . serviceFactory . openPlunkr = function ( templateMerge , formPostData , angularUrls ) {
361
- return function ( content ) {
360
+
361
+
362
+ docsApp . serviceFactory . prepareEditorAssetTags = function ( angularUrls ) {
363
+ return function ( content , options ) {
364
+ options = options || { } ;
365
+ var includeLocalFiles = options . includeLocalFiles ;
366
+ var html = makeScriptTag ( angularUrls [ 'angular.js' ] ) ;
367
+
362
368
var allFiles = [ ] . concat ( content . js , content . css , content . html , content . json ) ;
363
- var indexHtmlContent = '<!doctype html>\n' +
364
- '<html ng-app="{{module}}">\n' +
365
- ' <head>\n' +
366
- ' <script src="{{angularJSUrl}}"></script>\n' +
367
- '{{scriptDeps}}\n' +
368
- ' </head>\n' +
369
- ' <body>\n\n' +
370
- '{{indexContents}}' +
371
- '\n\n </body>\n' +
372
- '</html>\n' ;
373
- var scriptDeps = '' ;
374
369
angular . forEach ( content . deps , function ( file ) {
375
370
if ( file . name !== 'angular.js' ) {
376
- scriptDeps += ' <script src="' + file . name + '"></script>\n' ;
371
+ var isLocal = false ;
372
+ for ( var i = 0 ; i < allFiles . length ; i ++ ) {
373
+ if ( allFiles [ i ] . name == file . name ) {
374
+ isLocal = true ;
375
+ break ;
376
+ }
377
+ }
378
+ if ( ! ( isLocal && ! includeLocalFiles ) ) {
379
+ var assetUrl = angularUrls [ file . name ] || file . name ;
380
+ html += makeScriptTag ( assetUrl ) ;
381
+ }
377
382
}
378
383
} ) ;
384
+
385
+ if ( includeLocalFiles ) {
386
+ angular . forEach ( content . css , function ( file , index ) {
387
+ html += makeCssLinkTag ( file . name ) ;
388
+ } ) ;
389
+ }
390
+
391
+ return html ;
392
+
393
+
394
+ function makeScriptTag ( src ) {
395
+ return '<script type="text/javascript" src="' + src + '"></script>\n' ;
396
+ } ;
397
+
398
+ function makeCssLinkTag ( src ) {
399
+ return '<link rel="stylesheet" type="text/css" href="' + src + '" />\n' ;
400
+ } ;
401
+ } ;
402
+ } ;
403
+
404
+
405
+ docsApp . serviceFactory . openPlunkr = function ( templateMerge , formPostData , prepareEditorAssetTags ) {
406
+ return function ( content ) {
407
+ var hasRouting = false ;
408
+ angular . forEach ( content . deps , function ( file ) {
409
+ hasRouting = hasRouting || file . name == 'angular-route.js' ;
410
+ } ) ;
411
+ var indexHtmlContent = '<!doctype html>\n' +
412
+ '<html ng-app="{{module}}">\n' +
413
+ ' <head>\n' +
414
+ '{{scriptDeps}}' ;
415
+
416
+ if ( hasRouting ) {
417
+ indexHtmlContent += '<script type="text/javascript">\n' +
418
+ '//this is here to make plunkr work with AngularJS routing\n' +
419
+ 'angular.element(document.getElementsByTagName(\'head\')).append(' +
420
+ 'angular.element(\'<base href="\' + window.location.pathname + \'" />\')' +
421
+ ');\n' +
422
+ '</script>\n' ;
423
+ }
424
+
425
+ indexHtmlContent += '</head>\n' +
426
+ ' <body>\n\n' +
427
+ '{{indexContents}}\n\n' +
428
+ ' </body>\n' +
429
+ '</html>\n' ;
430
+
379
431
indexProp = {
380
432
module : content . module ,
381
- angularJSUrl : angularUrls [ 'angular.js' ] ,
382
- scriptDeps : scriptDeps ,
433
+ scriptDeps : prepareEditorAssetTags ( content , { includeLocalFiles : true } ) ,
383
434
indexContents : content . html [ 0 ] . content
384
435
} ;
385
436
var postData = { } ;
437
+
438
+ var allFiles = [ ] . concat ( content . js , content . css , content . html , content . json ) ;
386
439
angular . forEach ( allFiles , function ( file , index ) {
387
440
if ( file . content && file . name != 'index.html' ) {
388
441
postData [ 'files[' + file . name + ']' ] = file . content ;
@@ -399,13 +452,14 @@ docsApp.serviceFactory.openPlunkr = function(templateMerge, formPostData, angula
399
452
} ;
400
453
} ;
401
454
402
- docsApp . serviceFactory . openJsFiddle = function ( templateMerge , formPostData , angularUrls ) {
403
-
455
+ docsApp . serviceFactory . openJsFiddle = function ( templateMerge , formPostData , prepareEditorAssetTags ) {
404
456
var HTML = '<div ng-app=\"{{module}}\">\n{{html:2}}</div>' ,
405
- CSS = '</style> <!-- Ugly Hack due to jsFiddle issue: http://goo.gl/BUfGZ --> \n' +
457
+ CSS = '</style> <!-- Ugly Hack to make remote files preload in jsFiddle --> \n' +
406
458
'{{head:0}}<style>\n.ng-invalid { border: 1px solid red; }\n{{css}}' ,
407
459
SCRIPT = '{{script}}' ,
408
- SCRIPT_CACHE = '\n\n<!-- {{name}} -->\n<script type="text/ng-template" id="{{name}}">\n{{content:2}}</script>' ;
460
+ SCRIPT_CACHE = '\n\n<!-- {{name}} -->\n<script type="text/ng-template" id="{{name}}">\n{{content:2}}</script>' ,
461
+ BASE_HREF_TAG = '<!-- Ugly Hack to make AngularJS routing work inside of jsFiddle -->\n' +
462
+ '<base href="/" />\n\n' ;
409
463
410
464
return function ( content ) {
411
465
var prop = {
@@ -415,8 +469,6 @@ docsApp.serviceFactory.openJsFiddle = function(templateMerge, formPostData, angu
415
469
script : ''
416
470
} ;
417
471
418
- prop . head = templateMerge ( '<script src="{{url}}"></script>' , { url : angularUrls [ 'angular.js' ] } ) ;
419
-
420
472
angular . forEach ( content . html , function ( file , index ) {
421
473
if ( index ) {
422
474
prop . html += templateMerge ( SCRIPT_CACHE , file ) ;
@@ -425,6 +477,8 @@ docsApp.serviceFactory.openJsFiddle = function(templateMerge, formPostData, angu
425
477
}
426
478
} ) ;
427
479
480
+ prop . head = prepareEditorAssetTags ( content , { includeLocalFiles : false } ) ;
481
+
428
482
angular . forEach ( content . js , function ( file , index ) {
429
483
prop . script += file . content ;
430
484
} ) ;
@@ -433,9 +487,18 @@ docsApp.serviceFactory.openJsFiddle = function(templateMerge, formPostData, angu
433
487
prop . css += file . content ;
434
488
} ) ;
435
489
490
+ var hasRouting = false ;
491
+ angular . forEach ( content . deps , function ( file ) {
492
+ hasRouting = hasRouting || file . name == 'angular-route.js' ;
493
+ } ) ;
494
+
495
+ var compiledHTML = templateMerge ( HTML , prop ) ;
496
+ if ( hasRouting ) {
497
+ compiledHTML = BASE_HREF_TAG + compiledHTML ;
498
+ }
436
499
formPostData ( "http://jsfiddle.net/api/post/library/pure/" , {
437
500
title : 'AngularJS Example' ,
438
- html : templateMerge ( HTML , prop ) ,
501
+ html : compiledHTML ,
439
502
js : templateMerge ( SCRIPT , prop ) ,
440
503
css : templateMerge ( CSS , prop )
441
504
} ) ;
0 commit comments