1 angular.module('plunkr', [])
2 .factory('formPostData', ['$document', function ($document) {
3 return function (url, newWindow, fields) {
5 * If the form posts to target="_blank", pop-up blockers can cause it not to work.
6 * If a user choses to bypass pop-up blocker one time and click the link, they will arrive at
7 * a new default plnkr, not a plnkr with the desired template. Given this undesired behavior,
8 * some may still want to open the plnk in a new window by opting-in via ctrl+click. The
9 * newWindow param allows for this possibility.
11 var target = newWindow ? '_blank' : '_self';
12 var form = angular.element('<form style="display: none;" method="post" action="' + url + '" target="' + target + '"></form>');
13 angular.forEach(fields, function (value, name) {
14 var input = angular.element('<input type="hidden" name="' + name + '">');
15 input.attr('value', value);
18 $document.find('body').append(form);
25 .directive('plnkrOpener', ['$q', 'getExampleData', 'formPostData', function ($q, getExampleData, formPostData) {
31 controllerAs: 'plnkr',
32 template: '<button ng-click="plnkr.open($event)" class="btn btn-info btn-sm plunk-btn"> <i class="glyphicon glyphicon-edit"> </i> Edit in Plunker</button> ',
33 controller: [function () {
36 ctrl.prepareExampleData = function (examplePath) {
38 return $q.when(ctrl.prepared);
40 return getExampleData(examplePath).then(function (data) {
46 ctrl.open = function (clickEvent) {
48 var newWindow = clickEvent.ctrlKey || clickEvent.metaKey;
50 'tags[0]': "angularjs",
51 'tags[1]': "ui-select",
55 // Make sure the example data is available.
56 // If an XHR must be made, this might break some pop-up blockers when
57 // new window is requested
58 ctrl.prepareExampleData(ctrl.examplePath)
60 angular.forEach(ctrl.prepared, function (file) {
61 postData['files[' + file.name + ']'] = file.content;
64 postData.description = "Angular ui-select http://github.com/angular-ui/ui-select/";
66 formPostData('http://plnkr.co/edit/?p=preview', newWindow, postData);
70 // Initialize the example data, so it's ready when clicking the open button.
71 // Otherwise pop-up blockers will prevent a new window from opening
72 ctrl.prepareExampleData(ctrl.examplePath);
77 .factory('getExampleData', ['$http', '$q', function ($http, $q) {
78 return function (exampleFile) {
79 // Load the manifest for the example
81 'demo.js': './assets/',
82 'select.css': './dist',
85 files = angular.copy(defaultFiles);
86 files[exampleFile] = './';
88 var filePromises = [];
90 angular.forEach(files, function (folder, filename) {
91 filePromises.push($http.get(folder + '/' + filename, { transformResponse: [], cache: true })
92 .then(function (response) {
94 var content = response.data;
95 // Should only be one html (the example)
96 if (filename.match(/.html$/)) {
97 filename = "index.html";
98 content = content.replace(/.\/assets\//g, './').replace(/.\/dist\//g, './');
108 return $q.all(filePromises);