OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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. |
| 4 |
| 5 cr.define('print_preview', function() { |
| 6 'use strict'; |
| 7 |
| 8 /** |
| 9 * A set of key parameters describing a destination used to determine |
| 10 * if two destinations are the same. |
| 11 * @param {!Array<!print_preview.DestinationOrigin>} origins Match |
| 12 * destinations from these origins. |
| 13 * @param {RegExp} idRegExp Match destination's id. |
| 14 * @param {RegExp} displayNameRegExp Match destination's displayName. |
| 15 * @param {boolean} skipVirtualDestinations Whether to ignore virtual |
| 16 * destinations, for example, Save as PDF. |
| 17 * @constructor |
| 18 */ |
| 19 function DestinationMatch( |
| 20 origins, idRegExp, displayNameRegExp, skipVirtualDestinations) { |
| 21 |
| 22 /** @private {!Array<!print_preview.DestinationOrigin>} */ |
| 23 this.origins_ = origins; |
| 24 |
| 25 /** @private {RegExp} */ |
| 26 this.idRegExp_ = idRegExp; |
| 27 |
| 28 /** @private {RegExp} */ |
| 29 this.displayNameRegExp_ = displayNameRegExp; |
| 30 |
| 31 /** @private {boolean} */ |
| 32 this.skipVirtualDestinations_ = skipVirtualDestinations; |
| 33 } |
| 34 |
| 35 DestinationMatch.prototype = { |
| 36 |
| 37 /** |
| 38 * @param {string} origin Origin to match. |
| 39 * @return {boolean} Whether the origin is one of the {@code origins_}. |
| 40 */ |
| 41 matchOrigin: function(origin) { |
| 42 return arrayContains(this.origins_, origin); |
| 43 }, |
| 44 |
| 45 /** |
| 46 * @param {string} id Id of the destination. |
| 47 * @param {string} origin Origin of the destination. |
| 48 * @return {boolean} Whether destination is the same as initial. |
| 49 */ |
| 50 matchIdAndOrigin: function(id, origin) { |
| 51 return this.matchOrigin(origin) && |
| 52 !!this.idRegExp_ && |
| 53 this.idRegExp_.test(id); |
| 54 }, |
| 55 |
| 56 /** |
| 57 * @param {!print_preview.Destination} destination Destination to match. |
| 58 * @return {boolean} Whether {@code destination} matches the last user |
| 59 * selected one. |
| 60 */ |
| 61 match: function(destination) { |
| 62 if (!this.matchOrigin(destination.origin)) { |
| 63 return false; |
| 64 } |
| 65 if (this.idRegExp_ && !this.idRegExp_.test(destination.id)) { |
| 66 return false; |
| 67 } |
| 68 if (this.displayNameRegExp_ && |
| 69 !this.displayNameRegExp_.test(destination.displayName)) { |
| 70 return false; |
| 71 } |
| 72 if (this.skipVirtualDestinations_ && |
| 73 this.isVirtualDestination_(destination)) { |
| 74 return false; |
| 75 } |
| 76 return true; |
| 77 }, |
| 78 |
| 79 /** |
| 80 * @param {!print_preview.Destination} destination Destination to check. |
| 81 * @return {boolean} Whether {@code destination} is virtual, in terms of |
| 82 * destination selection. |
| 83 * @private |
| 84 */ |
| 85 isVirtualDestination_: function(destination) { |
| 86 if (destination.origin == print_preview.DestinationOrigin.LOCAL) { |
| 87 return arrayContains( |
| 88 [print_preview.Destination.GooglePromotedId.SAVE_AS_PDF], |
| 89 destination.id); |
| 90 } |
| 91 return arrayContains( |
| 92 [print_preview.Destination.GooglePromotedId.DOCS], |
| 93 destination.id); |
| 94 } |
| 95 }; |
| 96 |
| 97 // Export |
| 98 return { |
| 99 DestinationMatch: DestinationMatch |
| 100 }; |
| 101 }); |
OLD | NEW |