This repository has been archived by the owner on Sep 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathgoogle-url-shortener.html
163 lines (134 loc) · 3.88 KB
/
google-url-shortener.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../google-apis/google-client-loader.html">
<!--
`google-url-shortener` is a web component that shortens URLs with the
<a href="https://developers.google.com/url-shortener/">Google URL Shortener API
</a>.
##### Example
<google-url-shortener id="shortener"></google-url-shortener>
<script>
var shortener = document.getElementById('shortener');
shortener.addEventListener('google-url-shorten', function(event) {
console.log(event.detail.shortUrl);
});
// Shorten the current page URL.
shortener.longUrl = document.URL;
shortener.shorten();
</script>
##### Example using `auto` and binding.
<google-url-shortener id="shortener" longUrl="{{longUrl}}" auto>
</google-url-shortener>
<script>
var shortener = document.getElementById('shortener');
shortener.addEventListener('google-url-shorten', function(event) {
// This will be called automatically every time `longUrl` changes.
console.log(event.detail.shortUrl);
});
</script>
@demo
-->
<dom-module id="google-url-shortener">
<template>
<google-client-loader id="urlshortener" name="urlshortener" version="v1"
on-google-api-load="_readyForAction"
on-google-api-load-error="_apiLoadError">
</google-client-loader>
</template>
</dom-module>
<script>
(function() {
'use strict';
// Stores whether the URL Shortener API is done loading.
var apiLoaded_ = false;
Polymer({
is: 'google-url-shortener',
/**
* Fired when the component is ready for shortening.
*
* @event google-url-shortener-ready
*/
/**
* Fired when the URL gets successfully shortened.
*
* @event google-url-shorten
*/
/**
* Fired if an attempt to shorten a URL results in an error.
*
* @event google-url-shorten-error
*/
properties: {
/**
* The URL to be shortened.
*/
longUrl: {
type: String,
value: '',
observer: '_longUrlChanged'
},
/**
* Shortened URL
*/
shortUrl: {
type: String,
value: '',
notify: true
},
/**
* Error when url was shortened
*/
error: {
type: String,
value: '',
notify: true
},
/**
* If true, automatically performs the shortening request when `longUrl`
* changes.
*/
auto: {
type: Boolean,
value: false
}
},
_readyForAction: function() {
apiLoaded_ = true;
this.fire('google-url-shortener-ready');
},
_apiLoadError: function(event) {
this.fire('api-error', {
'error': {
'message': 'Error loading URL Shortener API',
'innerError': event.detail
}
});
},
_longUrlChanged: function() {
if (this.auto) {
this.shorten();
}
},
/**
* Shortens the URL in `longUrl`. Use if `auto` is not set.
*/
shorten: function() {
if (apiLoaded_) {
if (this.longUrl) {
var request = this.$.urlshortener.api.url.insert(
{resource: {longUrl: this.longUrl}});
request.execute(function(response) {
if (response && response.id) {
this.shortUrl = response.id;
this.error = '';
this.fire('google-url-shorten', {shortUrl: response.id});
} else {
this.error = response && response.error ? response.error.message : 'Unknown error';
this.fire('google-url-shorten-error', {error: this.error});
}
}.bind(this));
}
}
}
});
})();
</script>