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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This Polymer element contains the entire media router interface. It handles
// hiding and showing specific components.
Polymer({
is: 'media-router-container',
properties: {
/**
* The list of CastModes to show.
* @type {!Array<!media_router.CastMode>}
*/
castModeList: {
type: Array,
value: [],
},
/**
* The possible states of media-router-container. Used to determine which
* components of media-router-container to show.
* This is a property of media-router-container because it is used in
* tests.
*
* @enum {string}
* @private
*/
CONTAINER_VIEW_: {
type: Object,
readOnly: true,
value: {
CAST_MODE_LIST: 'cast-mode-list',
FILTER: 'filter',
ROUTE_DETAILS: 'route-details',
SINK_LIST: 'sink-list',
},
},
/**
* The current route.
* @private {?media_router.Route}
*/
currentRoute_: {
type: Object,
value: null,
},
/**
* The current view to be shown.
* @private {CONTAINER_VIEW_}
*/
currentView_: {
type: String,
value: '',
},
/**
* The text for when there are no devices.
* @private {string}
*/
deviceMissingText_: {
type: String,
value: loadTimeData.getString('deviceMissing'),
},
/**
* The URL to open when the device missing link is clicked.
* @private {string}
*/
deviceMissingUrl: {
type: String,
value: '',
},
/**
* The header text.
* @type {string}
*/
headerText: {
type: String,
value: '',
},
/**
* The header text tooltip. This would be descriptive of the
* source origin, whether a host name, tab URL, etc.
* @type {string}
*/
headerTextTooltip: {
type: String,
value: '',
},
/**
* The issue to show.
* @type {?media_router.Issue}
*/
issue: {
type: Object,
value: null,
},
/**
* Whether the MR UI was just opened.
* @private {boolean}
*/
justOpened_: {
type: Boolean,
value: true,
observer: 'computeSpinnerHidden_',
},
/**
* The number of current local routes.
* @private {number}
*/
localRouteCount_: {
type: Number,
value: 0,
},
/**
* The list of current routes.
* @type {!Array<!media_router.Route>}
*/
routeList: {
type: Array,
value: [],
observer: 'rebuildRouteMaps_',
},
/**
* Maps media_router.Route.id to corresponding media_router.Route.
* @private {!Object<!string, !media_router.Route>}
*/
routeMap_: {
type: Object,
value: {},
},
/**
* The header text when the cast mode list is shown.
* @private {string}
*/
selectCastModeHeaderText_: {
type: String,
value: loadTimeData.getString('selectCastModeHeader'),
},
/**
* The value of the selected cast mode in |castModeList|, or -1 if the
* user has not explicitly selected a cast mode.
* @private {number}
*/
selectedCastModeValue_: {
type: Number,
value: -1,
observer: 'showSinkList_',
},
/**
* The subheading text for the non-cast-enabled app cast mode list.
* @private {string}
*/
shareYourScreenSubheadingText_: {
type: String,
value: loadTimeData.getString('shareYourScreenSubheading'),
},
/**
* The list of available sinks.
* @type {!Array<!media_router.Sink>}
*/
sinkList: {
type: Array,
value: [],
observer: 'rebuildSinkMap_',
},
/**
* Maps media_router.Sink.id to corresponding media_router.Sink.
* @private {!Object<!string, !media_router.Sink>}
*/
sinkMap_: {
type: Object,
value: {},
},
/**
* Maps media_router.Sink.id to corresponding media_router.Route.
* @private {!Object<!string, ?media_router.Route>}
*/
sinkToRouteMap_: {
type: Object,
value: {},
},
},
ready: function() {
this.addEventListener('close-route-click', this.removeRoute);
this.showSinkList_();
},
attached: function() {
// Turn off the spinner after 3 seconds.
this.async(function() {
this.justOpened_ = false;
}, 3000 /* 3 seconds */);
},
/**
* @param {CONTAINER_VIEW_} view The current view.
* @return {string} The current arrow-drop-* icon to use.
* @private
*/
computeArrowDropIcon_: function(view) {
return view == this.CONTAINER_VIEW_.CAST_MODE_LIST ?
'arrow-drop-up' : 'arrow-drop-down';
},
/**
* @param {CONTAINER_VIEW_} view The current view.
* @return {boolean} Whether or not to hide the cast mode list.
* @private
*/
computeCastModeHidden_: function(view) {
return view != this.CONTAINER_VIEW_.CAST_MODE_LIST;
},
/**
* @param {!media_router.CastMode} castMode The cast mode to determine an
* icon for.
* @return {string} The Polymer <iron-icon> icon to use. The format is
* <iconset>:<icon>, where <iconset> is the set ID and <icon> is the name
* of the icon. <iconset>: may be ommitted if <icon> is from the default
* set.
* @private
*/
computeCastModeIcon_: function(castMode) {
switch (castMode.type) {
case media_router.CastModeType.DEFAULT:
return 'av:web';
case media_router.CastModeType.TAB_MIRROR:
return 'tab';
case media_router.CastModeType.DESKTOP_MIRROR:
return 'hardware:laptop';
default:
return '';
}
},
/**
* @param {!Array<!media_router.CastMode>} castModeList The current list of
* cast modes.
* @return {!Array<!media_router.CastMode>} The list of default cast modes.
* @private
*/
computeDefaultCastModeList_: function(castModeList) {
return castModeList.filter(function(mode) {
return mode.type == media_router.CastModeType.DEFAULT;
});
},
/**
* @param {CONTAINER_VIEW_} view The current view.
* @param {?media_router.Issue} issue The current issue.
* @return {boolean} Whether or not to hide the header.
* @private
*/
computeHeaderHidden_: function(view, issue) {
return view == this.CONTAINER_VIEW_.ROUTE_DETAILS ||
(view == this.CONTAINER_VIEW_.SINK_LIST &&
issue && issue.isBlocking);
},
/**
* @param {?media_router.Issue} issue The current issue.
* @return {string} The class for the issue banner.
* @private
*/
computeIssueBannerClass_: function(issue) {
return issue && !issue.isBlocking ? 'non-blocking' : '';
},
/**
* @param {CONTAINER_VIEW_} view The current view.
* @param {?media_router.Issue} issue The current issue.
* @return {boolean} Whether or not to hide the issue banner.
* @private
*/
computeIssueBannerHidden_: function(view, issue) {
return !issue || view == this.CONTAINER_VIEW_.CAST_MODE_LIST;
},
/**
* @param {!Array<!media_router.CastMode>} castModeList The current list of
* cast modes.
* @return {!Array<!media_router.CastMode>} The list of non-default cast
* modes.
* @private
*/
computeNonDefaultCastModeList_: function(castModeList) {
return castModeList.filter(function(mode) {
return mode.type != media_router.CastModeType.DEFAULT;
});
},
/**
* @param {CONTAINER_VIEW_} view The current view.
* @param {?media_router.Issue} issue The current issue.
* @return {boolean} Whether or not to hide the route details.
* @private
*/
computeRouteDetailsHidden_: function(view, issue) {
return view != this.CONTAINER_VIEW_.ROUTE_DETAILS ||
(issue && issue.isBlocking);
},
/**
* @param {!string} sinkId A sink ID.
* @return {boolean} Whether or not to hide the route info in the sink list
* that is associated with |sinkId|.
* @private
*/
computeRouteInSinkListHidden_: function(sinkId, sinkToRouteMap) {
return !sinkToRouteMap[sinkId];
},
/**
* @param {!string} sinkId A sink ID.
* @return {string} The description value of the route associated with
* |sinkId|.
* @private
*/
computeRouteInSinkListValue_: function(sinkId, sinkToRouteMap) {
var route = sinkToRouteMap[sinkId];
return route ? route.description : '';
},
/**
* @param {!Array<!media_router.CastMode>} castModeList The current list of
* cast modes.
* @return {boolean} Whether or not to hide the share screen subheading text.
* @private
*/
computeShareScreenSubheadingHidden_: function(castModeList) {
return this.computeNonDefaultCastModeList_(castModeList).length == 0;
},
/**
* @param {?media_router.Route} route The current route.
* @return {?media_router.Sink} The sink associated with |route|.
* @private
*/
computeSinkForCurrentRoute_: function(route) {
return route ? this.sinkMap_[route.sinkId] : null;
},
/**
* @param {!media_router.Sink} sink The sink to determine an icon for.
* @return {string} The Polymer <iron-icon> icon to use. The format is
* <iconset>:<icon>, where <iconset> is the set ID and <icon> is the name
* of the icon. <iconset>: may be ommitted if <icon> is from the default
* set.
* @private
*/
computeSinkIcon_: function(sink) {
switch (sink.iconType) {
case media_router.SinkIconType.CAST:
// TODO(apacible): Update icon after UX discussion.
return 'hardware:tv';
case media_router.SinkIconType.CAST_AUDIO:
return 'hardware:speaker';
case media_router.SinkIconType.GENERIC:
return 'hardware:tv';
case media_router.SinkIconType.HANGOUT:
return 'communication:message';
default:
return 'hardware:tv';
}
},
/**
* @param {!string} sinkId A sink ID.
* @param {!Object<!string, ?media_router.Route>} sinkToRouteMap
* Maps media_router.Sink.id to corresponding media_router.Route.
* @return {string} The class for the sink icon.
* @private
*/
computeSinkIconClass_: function(sinkId, sinkToRouteMap) {
return sinkToRouteMap[sinkId] ? 'sink-icon active-sink' : 'sink-icon';
},
/**
* @param {!Array<!media_router.Sink>} The list of sinks.
* @return {boolean} Whether or not to hide the sink list.
* @private
*/
computeSinkListHidden_: function(sinkList) {
return sinkList.length == 0;
},
/**
* @param {CONTAINER_VIEW_} view The current view.
* @param {?media_router.Issue} issue The current issue.
* @return {boolean} Whether or not to hide entire the sink list view.
* @private
*/
computeSinkListViewHidden_: function(view, issue) {
return view != this.CONTAINER_VIEW_.SINK_LIST ||
(issue && issue.isBlocking);
},
/**
* @param {boolean} justOpened Whether the MR UI was just opened.
* @return {boolean} Whether or not to hide the spinner.
* @private
*/
computeSpinnerHidden_: function(justOpened) {
return !justOpened;
},
/**
* Checks if there is a sink whose isLaunching is true.
*
* @param {!Array<!media_router.Sink>} sinks
* @return {boolean}
* @private
*/
isLaunching_: function(sinks) {
return sinks.some(function(sink) { return sink.isLaunching; });
},
/**
* Updates |currentView_| if the dialog had just opened and there's
* only one local route.
*
* @param {?media_router.Route} route A local route.
* @private
*/
maybeShowRouteDetailsOnOpen_: function(route) {
if (this.localRouteCount_ == 1 && this.justOpened_ && route)
this.showRouteDetails_(route);
},
/**
* Handles a cast mode selection. Updates |headerText|, |headerTextTooltip|,
* and |selectedCastModeValue_|.
*
* @param {!Event} event The event object.
* @private
*/
onCastModeClick_: function(event) {
// The clicked cast mode can come from one of two lists,
// defaultCastModeList and nonDefaultCastModeList.
var clickedMode =
this.$.defaultCastModeList.itemForElement(event.target) ||
this.$.nonDefaultCastModeList.itemForElement(event.target);
if (!clickedMode)
return;
this.headerText = clickedMode.description;
this.headerTextTooltip = clickedMode.host;
this.selectedCastModeValue_ = clickedMode.type;
},
/**
* Handles a click on the close button by firing a close-button-click event.
*
* @private
*/
onCloseButtonClick_: function() {
this.fire('close-button-click');
},
/**
* Handles response of previous create route attempt.
*
* @param {string} sinkId The ID of the sink to which the Media Route was
* creating a route.
* @param {?media_router.Route} route The newly created route to the sink
* if succeeded; null otherwise.
*/
onCreateRouteResponseReceived: function(sinkId, route) {
this.setLaunchState_(sinkId, false);
// TODO(apacible) Show launch failure.
if (!route)
return;
// Check if |route| already exists or if its associated sink
// does not exist.
if (this.routeMap_[route.id] || !this.sinkMap_[route.sinkId])
return;
// If there is an existing route associated with the same sink, its
// |sinkToRouteMap_| entry will be overwritten with that of the new route,
// which results in the correct sink to route mapping.
this.routeList.push(route);
this.showRouteDetails_(route);
},
/**
* Called when a sink is clicked.
*
* @param {!Event} event The event object.
* @private
*/
onSinkClick_: function(event) {
this.showOrCreateRoute_(this.$.sinkList.itemForElement(event.target));
},
/**
* Called when |routeList| is updated. Rebuilds |routeMap_| and
* |sinkToRouteMap_|.
*
* @private
*/
rebuildRouteMaps_: function() {
this.routeMap_ = {};
this.localRouteCount_ = 0;
// Keeps track of the last local route we find in |routeList|. If
// |localRouteCount_| is eventually equal to one, |localRoute| would be the
// only current local route.
var localRoute = null;
// Rebuild |sinkToRouteMap_| with a temporary map to avoid firing the
// computed functions prematurely.
var tempSinkToRouteMap = {};
// We expect that each route in |routeList| maps to a unique sink.
this.routeList.forEach(function(route) {
this.routeMap_[route.id] = route;
tempSinkToRouteMap[route.sinkId] = route;
if (route.isLocal) {
this.localRouteCount_++;
// It's OK if localRoute is updated multiple times; it is only used if
// |localRouteCount_| == 1, which implies it was only set once.
localRoute = route;
}
}, this);
// If |currentRoute_| is no longer active, clear |currentRoute_|. Also
// switch back to the SINK_PICKER view if the user is currently in the
// ROUTE_DETAILS view.
if (!this.currentRoute_ || !this.routeMap_[this.currentRoute_.id]) {
if (this.currentView_ == this.CONTAINER_VIEW_.ROUTE_DETAILS)
this.showSinkList_();
else
this.currentRoute_ = null;
}
this.sinkToRouteMap_ = tempSinkToRouteMap;
this.maybeShowRouteDetailsOnOpen_(localRoute);
},
/**
* Called when |sinkList| is updated. Rebuilds |sinkMap_|.
*
* @private
*/
rebuildSinkMap_: function() {
this.sinkMap_ = {};
this.sinkList.forEach(function(sink) {
this.sinkMap_[sink.id] = sink;
}, this);
},
/**
* Temporarily overrides the "isLaunching" bit for a sink.
*
* @param {string} sinkId The ID of the sink.
* @param {boolean} isLaunching Whether or not the media router is creating
* a route to the sink.
* @private
*/
setLaunchState_: function(sinkId, isLaunching) {
for (var index = 0; index < this.sinkList.length; index++) {
if (this.sinkList[index].id == sinkId) {
this.set(['sinkList', index, 'isLaunching'], isLaunching);
return;
}
}
},
/**
* Shows the cast mode list.
*
* @private
*/
showCastModeList_: function() {
this.currentRoute_ = null;
this.currentView_ = this.CONTAINER_VIEW_.CAST_MODE_LIST;
},
/**
* Creates a new route if there is no route to the |sink| . Otherwise,
* shows the route details.
*
* @param {!media_router.Sink} sink The sink to use.
* @private
*/
showOrCreateRoute_: function(sink) {
var route = this.sinkToRouteMap_[sink.id];
if (route) {
this.showRouteDetails_(route);
} else if (!this.isLaunching_(this.sinkList)) {
// Allow one launch at a time.
this.setLaunchState_(sink.id, true);
this.fire('create-route', {
sinkId: sink.id,
selectedCastModeValue: this.selectedCastModeValue_
});
}
},
/**
* Shows the route details.
*
* @param {!media_router.Route} route The route to show.
* @private
*/
showRouteDetails_: function(route) {
this.currentRoute_ = route;
this.currentView_ = this.CONTAINER_VIEW_.ROUTE_DETAILS;
},
/**
* Shows the sink list.
*
* @private
*/
showSinkList_: function() {
this.currentRoute_ = null;
this.currentView_ = this.CONTAINER_VIEW_.SINK_LIST;
},
/**
* Toggles |currentView_| between CAST_MODE_LIST and SINK_LIST.
*
* @private
*/
toggleCastModeHidden_: function() {
if (this.currentView_ == this.CONTAINER_VIEW_.CAST_MODE_LIST)
this.showSinkList_();
else
this.showCastModeList_();
},
});
|