plunkr.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. var plunkr = angular.module("plunkr", []);
  2. plunkr.service('Plnkr', function() {
  3. var urlToHtmlTag = function(url) {
  4. var linkTag = '<link rel="stylesheet" href="' + url + '"/>';
  5. var scriptTag = '<script src="' + url +'"></'+'script>';
  6. return url.match(/\.css$/) ? linkTag : scriptTag;
  7. };
  8. this.html = null;
  9. this.js = null;
  10. this.css = null;
  11. this.libs = [
  12. "http://maps.google.com/maps/api/js?sensor=false",
  13. "http://code.angularjs.org/1.2.5/angular.js",
  14. "http://rawgithub.com/allenhwkim/angularjs-google-maps" +
  15. "/master/build/scripts/ng-map.min.js",
  16. "script.js",
  17. "style.css"
  18. ];
  19. this.submitToPlnkr = function() {
  20. var postData = this.getPostData();
  21. var form = document.createElement('form');
  22. form.style.display = 'none';
  23. form.target = '_blank';
  24. form.method = 'post';
  25. form.action = 'http://plnkr.co/edit/?p=preview';
  26. for(var key in postData) {
  27. if (key) {
  28. var input = document.createElement('input');
  29. input.type = "hidden";
  30. input.name = key;
  31. input.value = postData[key];
  32. form.appendChild(input);
  33. }
  34. }
  35. form.submit();
  36. form.remove();
  37. };
  38. this.getPostData = function() {
  39. var contents = this.html;
  40. var js = this.js||'';
  41. var css = this.css;
  42. var appName = this.moduleName || "ngMap";
  43. js = js.replace(/,[ '"]*plunkr['"]?/,'');
  44. var headTags = [];
  45. for (var i=0; i<this.libs.length; i++) {
  46. var url = this.libs[i];
  47. headTags.push(urlToHtmlTag(url));
  48. }
  49. var postData = {};
  50. postData.description = "AngularJS Google Maps Directive";
  51. postData.private = true;
  52. postData['tags[]'] = 'auglarjs';
  53. postData['files[script.js]'] = js;
  54. postData['files[style.css]'] = css;
  55. postData['files[index.html]'] =
  56. '<!doctype html>\n' +
  57. '<html ng-app="' + appName + '">\n' +
  58. ' <head>\n' +
  59. headTags.join('\n') + '\n' +
  60. ' </head>\n' +
  61. ' <body>\n' +
  62. contents + '\n' +
  63. ' </body>\n' +
  64. '</html>\n';
  65. return postData;
  66. };
  67. return this;
  68. }); // service
  69. plunkr.directive('plnkrCode', function(Plnkr) {
  70. return {
  71. compile: function(el, attrs) {
  72. var key = attrs.plnkrCode;
  73. var code = el.html();
  74. Plnkr[key] = code;
  75. }
  76. };
  77. });
  78. plunkr.directive('plnkrLib', function(Plnkr) {
  79. return {
  80. compile: function(el, attrs) {
  81. var url = attrs.src || attrs.href;
  82. Plnkr.libs.push(url);
  83. }
  84. };
  85. });
  86. plunkr.directive('plnkrShow', function(Plnkr) {
  87. return {
  88. templateUrl: 'source_code.html',
  89. link: function(scope, el, attrs) {
  90. // so that we can access Plnkr js, css, html and method
  91. scope.Plnkr = Plnkr;
  92. Plnkr.moduleName = attrs.moduleName;
  93. }
  94. };
  95. });