Index: trunk/parsers/wikidom/lib/hype/bases/es.AggregateArray.js |
— | — | @@ -1,117 +0,0 @@ |
2 | | -/** |
3 | | - * Creates an es.AggregateArray object. |
4 | | - * |
5 | | - * A content series is an array of items which have a getLength method. |
6 | | - */ |
7 | | -es.AggregateArray = function( items ) { |
8 | | - var items = $.isArray( items ) ? items : []; |
9 | | - // Extend native array with method and properties of this |
10 | | - return $.extend( items, this ); |
11 | | -}; |
12 | | - |
13 | | -es.AggregateArray.prototype.lookup = function( offset ) { |
14 | | - if ( this.length ) { |
15 | | - var i = 0, |
16 | | - length = this.length, |
17 | | - left = 0, |
18 | | - right; |
19 | | - while ( i < length ) { |
20 | | - right = left + this[i].getLength() + 1; |
21 | | - if ( offset >= left && offset < right ) { |
22 | | - return this[i]; |
23 | | - } |
24 | | - left = right; |
25 | | - i++; |
26 | | - } |
27 | | - } |
28 | | - return null; |
29 | | -}; |
30 | | - |
31 | | -es.AggregateArray.prototype.rangeOf = function( item ) { |
32 | | - if ( this.length ) { |
33 | | - var i = 0, |
34 | | - length = this.length, |
35 | | - left = 0; |
36 | | - while ( i < length ) { |
37 | | - if ( this[i] === item ) { |
38 | | - return new es.Range( left, left + this[i].getLength() ); |
39 | | - } |
40 | | - left += this[i].getLength() + 1; |
41 | | - i++; |
42 | | - } |
43 | | - } |
44 | | - return null; |
45 | | -}; |
46 | | - |
47 | | -es.AggregateArray.prototype.getLengthOfItems = function() { |
48 | | - var sum = 0; |
49 | | - for ( var i = 0, length = this.length; i < length; i++ ) { |
50 | | - sum += this[i].getLength(); |
51 | | - } |
52 | | - return Math.max( 0, sum + this.length - 1 ); |
53 | | -}; |
54 | | - |
55 | | -es.AggregateArray.prototype.getCoverage = function( start, end ) { |
56 | | - var result = { 'on': [], 'off': [] }, |
57 | | - sum = 0, |
58 | | - len; |
59 | | - for ( var i = 0, length = this.length; i < length; i++ ) { |
60 | | - len = this[i].getLength(); |
61 | | - if ( sum >= start && sum + len < end ) { |
62 | | - result.on.push( this[i] ); |
63 | | - } else { |
64 | | - result.off.push( this[i] ); |
65 | | - } |
66 | | - sum += len |
67 | | - } |
68 | | - return result; |
69 | | -}; |
70 | | - |
71 | | -es.AggregateArray.prototype.select = function( start, end ) { |
72 | | - // Support es.Range object as first argument |
73 | | - if ( typeof start.from === 'number' && typeof start.to === 'number') { |
74 | | - start.normalize(); |
75 | | - end = start.end; |
76 | | - start = start.start; |
77 | | - } |
78 | | - var items = []; |
79 | | - if ( this.length ) { |
80 | | - var i = 0, |
81 | | - length = this.length, |
82 | | - left = 0, |
83 | | - right, |
84 | | - inside = false, |
85 | | - from, |
86 | | - to; |
87 | | - while ( i < length ) { |
88 | | - right = left + this[i].getLength() + 1; |
89 | | - if ( inside ) { |
90 | | - // Append items until we reach the end |
91 | | - from = 0; |
92 | | - to = Math.min( right - left - 1, end - left ); |
93 | | - |
94 | | - if ( from !== to ) { |
95 | | - items.push( { 'item': this[i], 'from': from, 'to': to } ); |
96 | | - } |
97 | | - if ( end >= left && end < right ) { |
98 | | - break; |
99 | | - } |
100 | | - } else if ( start >= left && start < right ) { |
101 | | - inside = true; |
102 | | - // Append first item |
103 | | - from = start - left; |
104 | | - //to = Math.min( right - 1, end - left ); |
105 | | - to = Math.min( right - left - 1, end - left ); |
106 | | - if ( from !== to ) { |
107 | | - items.push( { 'item': this[i], 'from': from, 'to': to } ); |
108 | | - } |
109 | | - if ( right >= end ) { |
110 | | - break; |
111 | | - } |
112 | | - } |
113 | | - left = right; |
114 | | - i++; |
115 | | - } |
116 | | - } |
117 | | - return items; |
118 | | -}; |