-
Notifications
You must be signed in to change notification settings - Fork 0
/
pnsk-structured-editor-soundcloud.html
137 lines (113 loc) · 4.95 KB
/
pnsk-structured-editor-soundcloud.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<link rel="import" href="../polymer/polymer-micro.html">
<style>
pnsk-structured-editor-soundcloud .preview {
position: relative;
}
</style>
<template id="pnsk-structured-editor-soundcloud" noshadowroot>
<div class="preview"></div>
<span class="inputswrapper">
<input class="trackID" type="hidden">
<input class="color" type="hidden">
<input class="visual" type="hidden">
<input class="height" type="hidden">
<label>
Embed code from SoundCloud
<input class="embed" type="text">
</label>
</span>
<content></content>
</template>
<script>
Polymer({
is: 'pnsk-structured-editor-soundcloud',
behaviors: [MinimalComponent],
template: currentImport.querySelector('#pnsk-structured-editor-soundcloud'),
properties: {
oembedProxyUrl: String
},
dataType: "soundcloud",
getState: function () {
return {
'trackID': this.qs(".trackID").value,
'visual': this.qs(".visual").value,
'height': this.qs(".height").value,
'color': this.qs(".color").value
};
},
setState: function (state) {
this.qs(".trackID").value = state.trackID;
this.qs(".visual").value = state.visual;
this.qs(".height").value = state.height;
this.qs(".color").value = state.color;
this._updatePreview();
},
_onChange: function () {
window.setTimeout(function () {
var params = this._getParams(this.qs(".embed").value);
this.qs(".trackID").value = params['trackID'];
this.qs(".visual").value = params['visual'];
this.qs(".height").value = params['height'];
this.qs(".color").value = params['color'];
var event = new Event('pnsk-component-stateChanged');
this.dispatchEvent(event);
this._updatePreview();
}.bind(this),
40);
},
_getParams: function (embedCode) {
var ret = {};
var tmp;
tmp = embedCode.match(/tracks\/([0-9]+)/i);
ret['trackID'] = ( tmp != null ) ? tmp[1] : '';
tmp = embedCode.match(/visual=(true|false)/i);
ret['visual'] = ( tmp != null ) ? tmp[1] : '';
tmp = embedCode.match(/height="([a-f0-9]+)"/i);
ret['height'] = ( tmp != null ) ? tmp[1] : '';
tmp = embedCode.match(/color=([a-f0-9]+)/i);
ret['color'] = ( tmp != null ) ? tmp[1] : '';
return ret;
},
_updatePreview: function () {
var trackID = this.qs(".trackID").value;
var visual = this.qs(".visual").value;
var height = this.qs(".height").value;
var color = this.qs(".color").value;
var preview = this.qs(".preview");
var iframe = preview.querySelector("iframe");
if (iframe !== null) {
preview.removeChild(iframe);
}
if (trackID == "") {
preview.style.display = "none";
return;
}
var visualStr = '';
if (visual == "true") {
visualStr = "&visual=" + visual;
}
var colorStr = '';
if (color != "") {
colorStr = "&color=" + color;
}
iframe = document.createElement("iframe");
iframe.width = "100%";
iframe.height = height;
iframe.frameBorder = 0;
iframe.webkitallowfullscreen = true;
iframe.mozallowfullscreen = true;
iframe.allowfullscreen = true;
iframe.src = "https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/" + trackID + visualStr + colorStr;
preview.appendChild(iframe);
preview.style.display = "block";
//<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/257722300&color=ff5500&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false"></iframe>
//<iframe width="100%" height="450" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/257722300&visual=true"></iframe>
},
// Element Lifecycle
created: function () {
this.qs(".inputswrapper").addEventListener("change", this._onChange.bind(this));
this.qs(".inputswrapper").addEventListener("cut", this._onChange.bind(this));
this.qs(".inputswrapper").addEventListener("paste", this._onChange.bind(this));
},
});
</script>