OLD | NEW |
(Empty) | |
| 1 <link rel="import" href="../polymer/polymer.html"> |
| 2 <link rel="import" href="../google-signin/google-signin-aware.html"> |
| 3 <link rel="import" href="../google-apis/google-client-loader.html"> |
| 4 |
| 5 |
| 6 <!-- |
| 7 Element providing a list of Google Calendars for a signed in user. |
| 8 Needs a google-signin element included somewhere on the same page |
| 9 that handles authentication. |
| 10 |
| 11 ##### Example |
| 12 |
| 13 <google-calendar-list title="What I'm up to"></google-calendar-list> |
| 14 |
| 15 @demo |
| 16 --> |
| 17 <dom-module id="google-calendar-list"> |
| 18 <style> |
| 19 :host, span { |
| 20 display: inline-block; |
| 21 } |
| 22 ul { |
| 23 list-style: none; |
| 24 padding: 0; |
| 25 } |
| 26 |
| 27 li { |
| 28 font-family: Arial, sans-serif; |
| 29 display: block; |
| 30 list-style: none; |
| 31 width: 300px; |
| 32 border-radius: .2em; |
| 33 padding: .2em; |
| 34 margin: .2em; |
| 35 overflow: hidden; |
| 36 } |
| 37 |
| 38 li a { |
| 39 color: inherit; |
| 40 display: block; |
| 41 text-decoration: none; |
| 42 } |
| 43 </style> |
| 44 <template> |
| 45 <google-client-loader id="calendar" name="calendar" version="v3" |
| 46 on-google-api-load="displayCalendars"></google-client-loader> |
| 47 <google-signin-aware |
| 48 scopes="https://www.googleapis.com/auth/calendar.readonly" |
| 49 is-authorized="{{_signedIn}}"></google-signin-aware> |
| 50 |
| 51 <ul id="calendars"> |
| 52 <li>{{title}}</li> |
| 53 <template is="dom-repeat" items="{{calendars}}"> |
| 54 <li style$="{{_computeCalStyle(item.backgroundColor)}}"> |
| 55 <a href="{{_computeCalHref(item.id, item.timeZone)}}" target="_blank">
{{item.summary}}</a> |
| 56 </li> |
| 57 </template> |
| 58 </ul> |
| 59 </template> |
| 60 </dom-module> |
| 61 |
| 62 <script> |
| 63 Polymer({ |
| 64 |
| 65 is: 'google-calendar-list', |
| 66 |
| 67 properties: { |
| 68 _signedIn: { |
| 69 type: Boolean, |
| 70 value: false, |
| 71 observer: '_signInChanged' |
| 72 }, |
| 73 /** |
| 74 * A title to be displayed on top of the calendar list. |
| 75 */ |
| 76 title: { |
| 77 type: String, |
| 78 value: 'My calendars' |
| 79 }, |
| 80 /** |
| 81 * List of calendars |
| 82 */ |
| 83 calendars: { |
| 84 type: Array, |
| 85 value: function() { return []; }, |
| 86 readOnly: true |
| 87 } |
| 88 }, |
| 89 |
| 90 _signInChanged: function(val) { |
| 91 if (val) { |
| 92 this.displayCalendars(); |
| 93 } |
| 94 else { |
| 95 this._setCalendars([]); |
| 96 } |
| 97 }, |
| 98 |
| 99 _computeCalStyle: function(backgroundColor) { |
| 100 return 'background-color:' + (backgroundColor || 'gray'); |
| 101 }, |
| 102 _computeCalHref: function(calId, calTimeZone) { |
| 103 return 'https://www.google.com/calendar/embed?src=' + calId + '&ctz=' + ca
lTimeZone; |
| 104 }, |
| 105 /** |
| 106 * Displays the calendar list if the user is signed in to Google. |
| 107 */ |
| 108 displayCalendars: function() { |
| 109 if (this._signedIn && this.$.calendar.api) { |
| 110 var request = this.$.calendar.api.calendarList.list({"key": ""}); |
| 111 |
| 112 // var request = this.$.calendar.api.calendarList.list({"key": ""}); |
| 113 request.execute(function(resp) { |
| 114 if (resp.error) { |
| 115 console.error("Error with calendarList.list", resp.message) |
| 116 } else { |
| 117 this._setCalendars(resp.items); |
| 118 } |
| 119 }.bind(this)); |
| 120 } |
| 121 } |
| 122 }); |
| 123 </script> |
| 124 |
| 125 |
| 126 <dom-module id="google-calendar-busy-now"> |
| 127 <style> |
| 128 span { |
| 129 font-family: Arial, sans-serif; |
| 130 display: inline-block; |
| 131 border-radius: .2em; |
| 132 padding: .2em; |
| 133 margin: .2em; |
| 134 overflow: hidden; |
| 135 } |
| 136 |
| 137 .busy { |
| 138 background-color: #FA573C; |
| 139 } |
| 140 |
| 141 .free { |
| 142 background-color: #7BD148; |
| 143 } |
| 144 |
| 145 .na { |
| 146 background-color: #999; |
| 147 } |
| 148 </style> |
| 149 <template> |
| 150 <google-client-loader id="calendar" name="calendar" version="v3" |
| 151 on-google-api-load="displayBusy"></google-client-loader> |
| 152 <google-signin-aware |
| 153 scopes="https://www.googleapis.com/auth/calendar.readonly" |
| 154 is-authorized="{{_isAuthorized}}"></google-signin-aware> |
| 155 |
| 156 <span class$="{{_labelClass}}">{{_label}}</span> |
| 157 </template> |
| 158 </dom-template> |
| 159 <script> |
| 160 (function() { |
| 161 var MS_PER_MINUTE = 60000; |
| 162 var TIME_SPAN = 30; |
| 163 |
| 164 /** |
| 165 A badge showing the free/busy status based on the events in a given calendar. |
| 166 |
| 167 ##### Example |
| 168 |
| 169 <google-calendar-busy-now |
| 170 calendarId="YOUR_CAL_ID" |
| 171 apiKey="YOUR_API_KEY" |
| 172 busyLabel="Do not disturb" |
| 173 freeLabel="I'm free, talk to me!"> |
| 174 </google-calendar-busy-now> |
| 175 |
| 176 */ |
| 177 Polymer({ |
| 178 is: 'google-calendar-busy-now', |
| 179 |
| 180 properties: { |
| 181 /** |
| 182 * Event from this calendar decide whether the status is free/busy. |
| 183 */ |
| 184 calendarId: { |
| 185 type: String, |
| 186 value: null, |
| 187 observer: '_calendarIdChanged' |
| 188 }, |
| 189 /** |
| 190 * API key to use with Calendar API requests. |
| 191 */ |
| 192 apiKey: { |
| 193 type: String, |
| 194 value: null |
| 195 }, |
| 196 /** |
| 197 * Label to be displayed if the status is busy. |
| 198 */ |
| 199 busyLabel: { |
| 200 type: String, |
| 201 value: "I'm busy" |
| 202 }, |
| 203 /** |
| 204 * Label to be displayed if the status is free. |
| 205 */ |
| 206 freeLabel: { |
| 207 type: String, |
| 208 value: "I'm free" |
| 209 }, |
| 210 |
| 211 _labelClass: { |
| 212 type: String, |
| 213 value: '' |
| 214 }, |
| 215 |
| 216 _label: { |
| 217 type: String, |
| 218 value: '' |
| 219 }, |
| 220 |
| 221 _isAuthorized: { |
| 222 type: Boolean, |
| 223 value: false, |
| 224 observer: '_isAuthorizedChanged' |
| 225 } |
| 226 }, |
| 227 |
| 228 _calendarIdChanged: function() { |
| 229 this.displayBusy(); |
| 230 }, |
| 231 |
| 232 _isAuthorizedChanged: function() { |
| 233 this.displayBusy(); |
| 234 }, |
| 235 |
| 236 _setState: function(state) { |
| 237 switch(state) { |
| 238 case 'free': |
| 239 this._label = this.freeLabel; |
| 240 this._labelClass = 'free'; |
| 241 break; |
| 242 case 'busy': |
| 243 this._label = this.busyLabel; |
| 244 this._labelClass = 'busy'; |
| 245 break; |
| 246 case 'na': |
| 247 this._label = 'n/a'; |
| 248 this._labelClass = 'na'; |
| 249 break; |
| 250 }; |
| 251 }, |
| 252 /** |
| 253 * Displays the busy/free status. Use it to refresh label state |
| 254 */ |
| 255 displayBusy: function() { |
| 256 if (!this.calendarId) { |
| 257 console.log('CalendarId is required for this component'); |
| 258 return; |
| 259 } |
| 260 if (!this._isAuthorized) { |
| 261 this._setState('na'); |
| 262 return; |
| 263 } |
| 264 if (this.$.calendar.api) { |
| 265 if (this.apiKey) { |
| 266 gapi.client.setApiKey(this.apiKey); |
| 267 } |
| 268 var now = new Date(); |
| 269 var query = { |
| 270 timeMin: now.toISOString(), |
| 271 timeMax: new Date(now.valueOf() + TIME_SPAN * MS_PER_MINUTE).toISOSt
ring(), |
| 272 items: [ |
| 273 { |
| 274 id: this.calendarId |
| 275 } |
| 276 ] |
| 277 } |
| 278 var request = this.$.calendar.api.freebusy.query(query); |
| 279 request.execute(function(resp) { |
| 280 if (!resp.calendars) { |
| 281 if (resp.error) { |
| 282 this._setState('na'); |
| 283 } else { |
| 284 this._setState('free'); |
| 285 } |
| 286 return; |
| 287 } |
| 288 if (resp.calendars[this.calendarId].errors) { |
| 289 this._setState('na'); |
| 290 return; |
| 291 } |
| 292 var now = new Date(); |
| 293 var busyTimes = resp.calendars[this.calendarId]; |
| 294 for (var i = 0, busyTime; busyTime = busyTimes.busy[i]; i++) { |
| 295 var start = new Date(busyTime.start); |
| 296 var end = new Date(busyTime.end); |
| 297 var busy = start < now && now < end; |
| 298 this._setState( busy ? 'busy': 'free'); |
| 299 if (busy) { |
| 300 break; |
| 301 } |
| 302 } |
| 303 }.bind(this)); |
| 304 } |
| 305 } |
| 306 }); |
| 307 })(); |
| 308 </script> |
| 309 |
OLD | NEW |