OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @typedef {{device: string, | 6 * @typedef {{device: string, |
7 * lastUpdateTime: string, | 7 * lastUpdateTime: string, |
| 8 * separatorIndexes: !Array<number>, |
8 * timestamp: number, | 9 * timestamp: number, |
9 * tabs: !Array<!ForeignSessionTab>, | 10 * tabs: !Array<!ForeignSessionTab>, |
10 * tag: string}} | 11 * tag: string}} |
11 */ | 12 */ |
12 var ForeignDeviceInternal; | 13 var ForeignDeviceInternal; |
13 | 14 |
14 Polymer({ | 15 Polymer({ |
15 is: 'history-synced-device-manager', | 16 is: 'history-synced-device-manager', |
16 | 17 |
17 properties: { | 18 properties: { |
18 /** | 19 /** |
19 * @type {?Array<!ForeignSession>} | 20 * @type {?Array<!ForeignSession>} |
20 */ | 21 */ |
21 sessionList: { | 22 sessionList: { |
22 type: Array, | 23 type: Array, |
23 observer: 'setSyncedHistory', | 24 observer: 'updateSyncedDevices' |
| 25 }, |
| 26 |
| 27 searchedTerm: { |
| 28 type: String, |
| 29 observer: 'searchTermChanged' |
24 }, | 30 }, |
25 | 31 |
26 /** | 32 /** |
27 * An array of synced devices with synced tab data. | 33 * An array of synced devices with synced tab data. |
28 * @type {!Array<!ForeignDeviceInternal>} | 34 * @type {!Array<!ForeignDeviceInternal>} |
29 */ | 35 */ |
30 syncedDevices_: { | 36 syncedDevices_: { |
31 type: Array, | 37 type: Array, |
32 value: function() { return []; } | 38 value: function() { return []; } |
33 } | 39 } |
34 }, | 40 }, |
35 | 41 |
36 /** | 42 /** |
37 * @param {!ForeignSession} session | 43 * @param {!ForeignSession} session |
38 * @return {!ForeignDeviceInternal} | 44 * @return {!ForeignDeviceInternal} |
39 */ | 45 */ |
40 createInternalDevice_: function(session) { | 46 createInternalDevice_: function(session) { |
41 var tabs = []; | 47 var tabs = []; |
42 for (var j = 0; j < session.windows.length; j++) { | 48 var separatorIndexes = []; |
43 var newTabs = session.windows[j].tabs; | 49 for (var i = 0; i < session.windows.length; i++) { |
| 50 var newTabs = session.windows[i].tabs; |
44 if (newTabs.length == 0) | 51 if (newTabs.length == 0) |
45 continue; | 52 continue; |
46 | 53 |
47 tabs = tabs.concat(newTabs); | 54 |
48 tabs[tabs.length - 1].needsWindowSeparator = true; | 55 if (!this.searchedTerm) { |
| 56 // Add all the tabs if there is no search term. |
| 57 tabs = tabs.concat(newTabs); |
| 58 separatorIndexes.push(tabs.length - 1); |
| 59 } else { |
| 60 var searchText = this.searchedTerm.toLowerCase(); |
| 61 var windowAdded = false; |
| 62 for (var j = 0; j < newTabs.length; j++) { |
| 63 var tab = newTabs[j]; |
| 64 if (tab.title.toLowerCase().indexOf(searchText) != -1) { |
| 65 tabs.push(tab); |
| 66 windowAdded = true; |
| 67 } |
| 68 } |
| 69 if (windowAdded) |
| 70 separatorIndexes.push(tabs.length - 1); |
| 71 } |
| 72 |
49 } | 73 } |
50 return { | 74 return { |
51 device: session.name, | 75 device: session.name, |
52 lastUpdateTime: '– ' + session.modifiedTime, | 76 lastUpdateTime: '– ' + session.modifiedTime, |
| 77 separatorIndexes: separatorIndexes, |
53 timestamp: session.timestamp, | 78 timestamp: session.timestamp, |
54 tabs: tabs, | 79 tabs: tabs, |
55 tag: session.tag | 80 tag: session.tag, |
56 }; | 81 }; |
57 }, | 82 }, |
58 | 83 |
59 /** | 84 /** |
60 * Replaces the currently displayed synced tabs with |sessionList|. It is | 85 * Replaces the currently displayed synced tabs with |sessionList|. It is |
61 * common for only a single session within the list to have changed, We try to | 86 * common for only a single session within the list to have changed, We try to |
62 * avoid doing extra work in this case. The logic could be more intelligent | 87 * avoid doing extra work in this case. The logic could be more intelligent |
63 * about updating individual tabs rather than replacing whole sessions, but | 88 * about updating individual tabs rather than replacing whole sessions, but |
64 * this approach seems to have acceptable performance. | 89 * this approach seems to have acceptable performance. |
65 * @param {!Array<!ForeignSession>} sessionList | 90 * @param {?Array<!ForeignSession>} sessionList |
66 */ | 91 */ |
67 setSyncedHistory: function(sessionList) { | 92 updateSyncedDevices: function(sessionList) { |
68 if (!sessionList) | 93 if (!sessionList) |
69 return; | 94 return; |
70 | 95 |
71 // First, update any existing devices that have changed. | 96 // First, update any existing devices that have changed. |
72 var updateCount = Math.min(sessionList.length, this.syncedDevices_.length); | 97 var updateCount = Math.min(sessionList.length, this.syncedDevices_.length); |
73 for (var i = 0; i < updateCount; i++) { | 98 for (var i = 0; i < updateCount; i++) { |
74 var oldDevice = this.syncedDevices_[i]; | 99 var oldDevice = this.syncedDevices_[i]; |
75 if (oldDevice.tag != sessionList[i].tag || | 100 if (oldDevice.tag != sessionList[i].tag || |
76 oldDevice.timestamp != sessionList[i].timestamp) { | 101 oldDevice.timestamp != sessionList[i].timestamp) { |
77 this.splice( | 102 this.splice( |
78 'syncedDevices_', i, 1, this.createInternalDevice_(sessionList[i])); | 103 'syncedDevices_', i, 1, this.createInternalDevice_(sessionList[i])); |
79 } | 104 } |
80 } | 105 } |
81 | 106 |
82 // Then, append any new devices. | 107 // Then, append any new devices. |
83 for (var i = updateCount; i < sessionList.length; i++) { | 108 for (var i = updateCount; i < sessionList.length; i++) { |
84 this.push('syncedDevices_', this.createInternalDevice_(sessionList[i])); | 109 this.push('syncedDevices_', this.createInternalDevice_(sessionList[i])); |
85 } | 110 } |
| 111 }, |
| 112 |
| 113 searchTermChanged: function(searchedTerm) { |
| 114 this.syncedDevices_ = []; |
| 115 this.updateSyncedDevices(this.sessionList); |
86 } | 116 } |
87 }); | 117 }); |
OLD | NEW |