OLD | NEW |
| (Empty) |
1 <!-- | |
2 // Copyright 2014 The Chromium Authors. All rights reserved. | |
3 // Use of this source code is governed by a BSD-style license that can be | |
4 // found in the LICENSE file. | |
5 --> | |
6 <script> | |
7 function StateHeader(state) { | |
8 this.state = state; | |
9 this.headerOrder = 1; | |
10 }; | |
11 | |
12 function LetterHeader(letter) { | |
13 this.letter = letter; | |
14 this.headerOrder = 2; | |
15 } | |
16 | |
17 function CitySequence(cities) | |
18 { | |
19 this.items = []; | |
20 this.cursor = 0; | |
21 | |
22 var lastState; | |
23 var lastLetter; | |
24 | |
25 for (var i = 0; i < cities.length; i++) { | |
26 var city = cities[i]; | |
27 if (!lastState || lastState.state != city.state) { | |
28 lastState = new StateHeader(city.state); | |
29 this.items.push(lastState); | |
30 lastLetter = undefined; | |
31 } | |
32 if (!lastLetter || lastLetter.letter != city.name[0]) { | |
33 lastLetter = new LetterHeader(city.name[0]); | |
34 this.items.push(lastLetter); | |
35 } | |
36 this.items.push(city); | |
37 } | |
38 }; | |
39 | |
40 CitySequence.prototype = { | |
41 append: function(other) { | |
42 var lastCity = this.items[this.items.length - 1]; | |
43 var firstOtherCity = other.items[2]; | |
44 | |
45 var index = 0; | |
46 if (firstOtherCity.state == lastCity.state) { | |
47 // skip StateHeader | |
48 if (firstOtherCity.name[0] == lastCity.name[0]) { | |
49 // skip LetterHeader | |
50 index = 2; | |
51 } else { | |
52 index = 1; | |
53 } | |
54 } | |
55 | |
56 for (; index < other.items.length; index++) { | |
57 this.items.push(other.items[index]); | |
58 } | |
59 } | |
60 }; | |
61 | |
62 module.exports = CitySequence; | |
63 | |
64 </script> | |
OLD | NEW |