1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 * @param {string} toTest The string to be tested.
7 * @return {boolean} True if |toTest| contains only digits. Leading and trailing
8 * whitespace is allowed.
10 function isInteger(toTest
) {
11 var numericExp
= /^\s*[0-9]+\s*$/;
12 return numericExp
.test(toTest
);
16 * Returns true if |value| is a valid non zero positive integer.
17 * @param {string} value The string to be tested.
18 * @return {boolean} true if the |value| is valid non zero positive integer.
20 function isPositiveInteger(value
) {
21 return isInteger(value
) && parseInt(value
, 10) > 0;
25 * Returns true if the contents of the two arrays are equal.
26 * @param {Array<{from: number, to: number}>} array1 The first array.
27 * @param {Array<{from: number, to: number}>} array2 The second array.
28 * @return {boolean} true if the arrays are equal.
30 function areArraysEqual(array1
, array2
) {
31 if (array1
.length
!= array2
.length
)
33 for (var i
= 0; i
< array1
.length
; i
++)
34 if (array1
[i
] !== array2
[i
])
40 * Returns true if the contents of the two page ranges are equal.
41 * @param {Array} array1 The first array.
42 * @param {Array} array2 The second array.
43 * @return {boolean} true if the arrays are equal.
45 function areRangesEqual(array1
, array2
) {
46 if (array1
.length
!= array2
.length
)
48 for (var i
= 0; i
< array1
.length
; i
++)
49 if (array1
[i
].from != array2
[i
].from ||
50 array1
[i
].to
!= array2
[i
].to
) {
57 * Removes duplicate elements from |inArray| and returns a new array.
58 * |inArray| is not affected. It assumes that |inArray| is already sorted.
59 * @param {!Array<number>} inArray The array to be processed.
60 * @return {!Array<number>} The array after processing.
62 function removeDuplicates(inArray
) {
65 if (inArray
.length
== 0)
69 for (var i
= 1; i
< inArray
.length
; ++i
)
70 if (inArray
[i
] != inArray
[i
- 1])
76 * Returns a list of ranges in |pageRangeText|. The ranges are
77 * listed in the order they appear in |pageRangeText| and duplicates are not
78 * eliminated. If |pageRangeText| is not valid null is returned.
79 * A valid selection has a parsable format and every page identifier is
80 * greater the 0 and less or equal to |totalPageCount| unless wildcards are
82 * If |totalPageCount| is 0 or undefined function uses impossibly large number
84 * Wildcard the first number must be larger then 0 and less or equal then
85 * |totalPageCount|. If it's missed then 1 is used as the first number.
86 * Wildcard the second number must be larger then the first number. If it's
87 * missed then |totalPageCount| is used as the second number.
88 * Example: "1-4, 9, 3-6, 10, 11" is valid, assuming |totalPageCount| >= 11.
89 * Example: "1-4, -6" is valid, assuming |totalPageCount| >= 6.
90 * Example: "2-" is valid, assuming |totalPageCount| >= 2, means from 2 to the
92 * Example: "4-2, 11, -6" is invalid.
93 * Example: "-" is valid, assuming |totalPageCount| >= 1.
94 * Example: "1-4dsf, 11" is invalid regardless of |totalPageCount|.
95 * @param {string} pageRangeText The text to be checked.
96 * @param {number=} opt_totalPageCount The total number of pages.
97 * @return {Array<{from: number, to: number}>} An array of page range objects.
99 function pageRangeTextToPageRanges(pageRangeText
, opt_totalPageCount
) {
100 if (pageRangeText
== '') {
104 var MAX_PAGE_NUMBER
= 1000000000;
105 var totalPageCount
= opt_totalPageCount
? opt_totalPageCount
:
108 var regex
= /^\s*([0-9]*)\s*-\s*([0-9]*)\s*$/;
109 var parts
= pageRangeText
.split(/,/);
112 for (var i
= 0; i
< parts
.length
; ++i
) {
113 var match
= parts
[i
].match(regex
);
115 if (!isPositiveInteger(match
[1]) && match
[1] !== '')
117 if (!isPositiveInteger(match
[2]) && match
[2] !== '')
119 var from = match
[1] ? parseInt(match
[1], 10) : 1;
120 var to
= match
[2] ? parseInt(match
[2], 10) : totalPageCount
;
121 if (from > to
|| from > totalPageCount
)
123 pageRanges
.push({'from': from, 'to': to
});
125 if (!isPositiveInteger(parts
[i
]))
127 var singlePageNumber
= parseInt(parts
[i
], 10);
128 if (singlePageNumber
> totalPageCount
)
130 pageRanges
.push({'from': singlePageNumber
, 'to': singlePageNumber
});
137 * Returns a list of pages defined by |pagesRangeText|. The pages are
138 * listed in the order they appear in |pageRangeText| and duplicates are not
139 * eliminated. If |pageRangeText| is not valid according or
140 * |totalPageCount| undefined [1,2,...,totalPageCount] is returned.
141 * See pageRangeTextToPageRanges for details.
142 * @param {string} pageRangeText The text to be checked.
143 * @param {number} totalPageCount The total number of pages.
144 * @return {Array<number>} A list of all pages.
146 function pageRangeTextToPageList(pageRangeText
, totalPageCount
) {
147 var pageRanges
= pageRangeTextToPageRanges(pageRangeText
, totalPageCount
);
150 for (var i
= 0; i
< pageRanges
.length
; ++i
) {
151 for (var j
= pageRanges
[i
].from; j
<= Math
.min(pageRanges
[i
].to
,
152 totalPageCount
); ++j
) {
157 if (pageList
.length
== 0) {
158 for (var j
= 1; j
<= totalPageCount
; ++j
)
165 * @param {!Array<number>} pageList The list to be processed.
166 * @return {!Array<number>} The contents of |pageList| in ascending order and
167 * without any duplicates. |pageList| is not affected.
169 function pageListToPageSet(pageList
) {
171 if (pageList
.length
== 0)
173 pageSet
= pageList
.slice(0);
174 pageSet
.sort(function(a
, b
) {
175 return /** @type {number} */(a
) - /** @type {number} */(b
);
177 pageSet
= removeDuplicates(pageSet
);
182 * @param {!HTMLElement} element Element to check for visibility.
183 * @return {boolean} Whether the given element is visible.
185 function getIsVisible(element
) {
186 return !element
.hidden
;
190 * Shows or hides an element.
191 * @param {!HTMLElement} element Element to show or hide.
192 * @param {boolean} isVisible Whether the element should be visible or not.
194 function setIsVisible(element
, isVisible
) {
195 element
.hidden
= !isVisible
;
199 * @param {!Array} array Array to check for item.
200 * @param {*} item Item to look for in array.
201 * @return {boolean} Whether the item is in the array.
203 function arrayContains(array
, item
) {
204 return array
.indexOf(item
) != -1;
208 * @param {!goog.array.ArrayLike<!{locale: string, value: string}>}
209 * localizedStrings An array of strings with corresponding locales.
210 * @param {string} locale Locale to look the string up for.
211 * @return {string} A string for the requested {@code locale}. An empty string
212 * if there's no string for the specified locale found.
214 function getStringForLocale(localizedStrings
, locale
) {
215 locale
= locale
.toLowerCase();
216 for (var i
= 0; i
< localizedStrings
.length
; i
++) {
217 if (localizedStrings
[i
].locale
.toLowerCase() == locale
)
218 return localizedStrings
[i
].value
;
224 * @param {!goog.array.ArrayLike<!{locale: string, value: string}>}
225 * localizedStrings An array of strings with corresponding locales.
226 * @return {string} A string for the current locale. An empty string if there's
227 * no string for the current locale found.
229 function getStringForCurrentLocale(localizedStrings
) {
230 // First try to find an exact match and then look for the language only.
231 return getStringForLocale(localizedStrings
, navigator
.language
) ||
232 getStringForLocale(localizedStrings
,
233 navigator
.language
.split('-')[0]);