Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / print_preview / data / ticket_items / copies.js
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.
4
5 cr.define('print_preview.ticket_items', function() {
6   'use strict';
7
8   /**
9    * Copies ticket item whose value is a {@code string} that indicates how many
10    * copies of the document should be printed. The ticket item is backed by a
11    * string since the user can textually input the copies value.
12    * @param {!print_preview.DestinationStore} destinationStore Destination store
13    *     used determine if a destination has the copies capability.
14    * @constructor
15    * @extends {print_preview.ticket_items.TicketItem}
16    */
17   function Copies(destinationStore) {
18     print_preview.ticket_items.TicketItem.call(
19         this, null /*appState*/, null /*field*/, destinationStore);
20   };
21
22   Copies.prototype = {
23     __proto__: print_preview.ticket_items.TicketItem.prototype,
24
25     /** @override */
26     wouldValueBeValid: function(value) {
27       if (/[^\d]+/.test(value)) {
28         return false;
29       }
30       var copies = parseInt(value);
31       if (copies > 999 || copies < 1) {
32         return false;
33       }
34       return true;
35     },
36
37     /** @override */
38     isCapabilityAvailable: function() {
39       return !!this.getCopiesCapability_();
40     },
41
42     /** @return {number} The number of copies indicated by the ticket item. */
43     getValueAsNumber: function() {
44       return parseInt(this.getValue());
45     },
46
47     /** @override */
48     getDefaultValueInternal: function() {
49       var cap = this.getCopiesCapability_();
50       return cap.hasOwnProperty('default') ? cap.default : '1';
51     },
52
53     /** @override */
54     getCapabilityNotAvailableValueInternal: function() {
55       return '1';
56     },
57
58     /**
59      * @return {Object} Copies capability of the selected destination.
60      * @private
61      */
62     getCopiesCapability_: function() {
63       var dest = this.getSelectedDestInternal();
64       return (dest &&
65               dest.capabilities &&
66               dest.capabilities.printer &&
67               dest.capabilities.printer.copies) ||
68              null;
69     }
70   };
71
72   // Export
73   return {
74     Copies: Copies
75   };
76 });