Skip to content
This repository was archived by the owner on Jan 22, 2018. It is now read-only.

Commit 741d2cd

Browse files
author
Kamil Kisiela
committed
demo: add codepen service
1 parent 626a483 commit 741d2cd

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

demo/client/data/codepen.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
const { SetModule, Service, Inject } = angular2now;
2+
3+
SetModule('demo');
4+
5+
@Service({
6+
name: 'Codepen'
7+
})
8+
@Inject(['$document'])
9+
class Codepen {
10+
constructor($document, Examples) {
11+
this.$document = $document;
12+
this.Examples = Examples;
13+
this.address = 'http://codepen.io/pen/define/';
14+
}
15+
16+
open(exampleId) {
17+
// const data = codepenDataAdapter.translate(demo, $demoAngularScripts.all());
18+
// const example = this.Examples.get(exampleId);
19+
const data = {
20+
css_external: [
21+
'//npmcdn.com/[email protected]/angular-material.min.css'
22+
].join(';'),
23+
js_external: [
24+
'//cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js',
25+
'//npmcdn.com/[email protected]/angular-animate.min.js',
26+
'//npmcdn.com/[email protected]/angular-aria.min.js',
27+
'//npmcdn.com/angular-material@latest/angular-material.min.js',
28+
'//npmcdn.com/[email protected]/angular-messages.min.js',
29+
'//npmcdn.com/api-check@latest/dist/api-check.js',
30+
'//npmcdn.com/angular-formly@latest/dist/formly.js',
31+
'//npmcdn.com/angular-formly-material@latest/dist/formly-material.js'
32+
].join(';'),
33+
html: this._getHTML().trim()
34+
};
35+
const form = this._buildForm(data);
36+
37+
this.$document.find('body').append(form);
38+
form[0].submit();
39+
form.remove();
40+
}
41+
42+
_buildForm(data) {
43+
const form = angular.element(
44+
`<form style="display: none;" method="post" target="_blank" action="${this.address}"></form>`
45+
);
46+
const input = `<input type="hidden" name="data" value="${this._escapeJsonQuotes(data)}" />`;
47+
form.append(input);
48+
return form;
49+
}
50+
51+
_escapeJsonQuotes(json) {
52+
return JSON.stringify(json)
53+
.replace(/'/g, '&amp;apos;')
54+
.replace(/"/g, '&amp;quot;');
55+
}
56+
57+
_getHTML() {
58+
return `
59+
<div ng-controller="DemoCtrl as vm" ng-cloak="" ng-app="demo">
60+
<form flex name="vm.form" ng-submit="vm.submit()">
61+
<formly-form fields="vm.fields" model="vm.model" form="vm.form" options="vm.options">
62+
<md-button type="submit" class="md-primary">Submit</md-button>
63+
</formly-form>
64+
</form>
65+
</div>
66+
`;
67+
}
68+
69+
_getJS() {
70+
return `
71+
(function () {
72+
'use strict';
73+
angular
74+
.module('demo', ['formlyMaterial'])
75+
.controller('DemoCtrl', DemoCtrl);
76+
77+
function DemoCtrl () {
78+
var self = this;
79+
80+
self.fields = ::field;
81+
self.model = ::model;
82+
self.form = ::form;
83+
self.options = ::options;
84+
self.submit = function() {}
85+
}
86+
})();
87+
`;
88+
}
89+
}

0 commit comments

Comments
 (0)