|
| 1 | +(function (module) { |
| 2 | + |
| 3 | +var DEFAULT_CACHE_KEY = 'pc035860'; |
| 4 | + |
| 5 | +var getCache = function ($cacheFactory) { |
| 6 | + var ID = 'to-plunker'; |
| 7 | + return $cacheFactory.get(ID) || $cacheFactory(ID); |
| 8 | +}; |
| 9 | + |
| 10 | +module |
| 11 | + |
| 12 | +.directive('toPlunker', function ($cacheFactory) { |
| 13 | + return { |
| 14 | + link: function postLink(scope, iElm, iAttrs) { |
| 15 | + var scriptPathsCache, cache, cacheKey; |
| 16 | + |
| 17 | + if (iElm[0].tagName === 'SCRIPT' && angular.isDefined(iAttrs.toPlunker)) { |
| 18 | + scriptPathsCache = getCache($cacheFactory); |
| 19 | + cacheKey = iAttrs.toPlunker || DEFAULT_CACHE_KEY; |
| 20 | + |
| 21 | + cache = scriptPathsCache.get(cacheKey); |
| 22 | + if (!cache) { |
| 23 | + cache = []; |
| 24 | + } |
| 25 | + cache.push(iAttrs.src); |
| 26 | + |
| 27 | + scriptPathsCache.put(cacheKey, cache); |
| 28 | + } |
| 29 | + } |
| 30 | + }; |
| 31 | +}) |
| 32 | + |
| 33 | +.directive('editOnPlunker', function (openPlunker, $document, $q, $http, $cacheFactory) { |
| 34 | + return { |
| 35 | + restrict: 'EA', |
| 36 | + template: '<button class="btn btn-default" style="position:fixed; z-index: 65535; top: 8px; left: 50%; margin-left: -61px;" ng-click="edit()">Edit on Plunker</button>', |
| 37 | + replace: true, |
| 38 | + scope: { |
| 39 | + id: '@', |
| 40 | + getFiles: '&files', |
| 41 | + description: '@', |
| 42 | + getTags: '&tags' |
| 43 | + }, |
| 44 | + link: function postLink(scope, iElm, iAttrs) { |
| 45 | + var preparePromise = prepareFiles(); |
| 46 | + |
| 47 | + scope.edit = function () { |
| 48 | + var description = scope.description || $document[0].title, |
| 49 | + tags = (scope.getTags || angular.noop)() || []; |
| 50 | + |
| 51 | + preparePromise.then(function (fileObjs) { |
| 52 | + openPlunker(fileObjs, description, tags); |
| 53 | + }); |
| 54 | + }; |
| 55 | + |
| 56 | + function prepareFiles () { |
| 57 | + var scriptPathsCache = getCache($cacheFactory), |
| 58 | + cacheKey = scope.id || DEFAULT_CACHE_KEY, |
| 59 | + scripts = scriptPathsCache.get(cacheKey), |
| 60 | + files = scope.getFiles(), |
| 61 | + promises = []; |
| 62 | + |
| 63 | + if (scripts && angular.isArray(scripts)) { |
| 64 | + files = files.concat(scripts); |
| 65 | + } |
| 66 | + |
| 67 | + angular.forEach(files, function (path) { |
| 68 | + var promise = $http.get(path, {transformResponse: transformResponse}) |
| 69 | + .then(function (res) { |
| 70 | + var content = res.data, |
| 71 | + filename = getFilename(path); |
| 72 | + |
| 73 | + if (filename === 'index.html') { |
| 74 | + angular.forEach(scripts, function (scriptPath) { |
| 75 | + content = content.replace(scriptPath, getFilename(scriptPath)); |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + return { |
| 80 | + filename: filename, |
| 81 | + content: content |
| 82 | + }; |
| 83 | + }); |
| 84 | + promises.push(promise); |
| 85 | + }); |
| 86 | + |
| 87 | + return $q.all(promises); |
| 88 | + } |
| 89 | + |
| 90 | + function transformResponse(data, headerGetter) { |
| 91 | + return data; |
| 92 | + } |
| 93 | + |
| 94 | + function getFilename(path) { |
| 95 | + return path.substring(path.lastIndexOf('/') + 1); |
| 96 | + } |
| 97 | + } |
| 98 | + }; |
| 99 | +}) |
| 100 | + |
| 101 | +.factory('formPostData', function ($document) { |
| 102 | + return function(url, fields) { |
| 103 | + var form = angular.element('<form style="display: none;" method="post" action="' + url + '" target="_blank"></form>'); |
| 104 | + angular.forEach(fields, function(field) { |
| 105 | + var name = field.name, |
| 106 | + value = field.value; |
| 107 | + |
| 108 | + var input = angular.element('<input type="hidden" name="' + name + '">'); |
| 109 | + input.attr('value', value); |
| 110 | + form.append(input); |
| 111 | + }); |
| 112 | + $document.find('body').append(form); |
| 113 | + form[0].submit(); |
| 114 | + form.remove(); |
| 115 | + }; |
| 116 | +}) |
| 117 | + |
| 118 | +.factory('openPlunker', function (formPostData) { |
| 119 | + return function (files, description, tags) { |
| 120 | + var postData = []; |
| 121 | + |
| 122 | + description = description || ''; |
| 123 | + tags = tags || []; |
| 124 | + |
| 125 | + angular.forEach(files, function (file) { |
| 126 | + postData.push({ |
| 127 | + name: 'files[' + file.filename + ']', |
| 128 | + value: file.content |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + angular.forEach(tags, function (tag) { |
| 133 | + postData.push({ |
| 134 | + name: 'tags[]', |
| 135 | + value: tag |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + postData.push({ |
| 140 | + name: 'private', |
| 141 | + value: true |
| 142 | + }); |
| 143 | + postData.push({ |
| 144 | + name: 'description', |
| 145 | + value: description |
| 146 | + }); |
| 147 | + |
| 148 | + formPostData('http://plnkr.co/edit/?p=preview', postData); |
| 149 | + }; |
| 150 | +}); |
| 151 | + |
| 152 | + |
| 153 | +})(angular.module('app.edit-on-plunker', [])); |
0 commit comments