Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / print_preview / data / ticket_items / media_size.js
1 // Copyright 2014 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    * Media size ticket item.
10    * @param {!print_preview.AppState} appState App state used to persist media
11    *     size selection.
12    * @param {!print_preview.DestinationStore} destinationStore Destination store
13    *     used to determine if a destination has the media size capability.
14    * @param {!print_preview.DocumentInfo} documentInfo Information about the
15    *     document to print.
16    * @param {!print_preview.ticket_items.MarginsType} marginsType Reset when
17    *     landscape value changes.
18    * @param {!print_preview.ticket_items.CustomMargins} customMargins Reset when
19    *     landscape value changes.
20    * @constructor
21    * @extends {print_preview.ticket_items.TicketItem}
22    */
23   function MediaSize(
24       appState, destinationStore, documentInfo, marginsType, customMargins) {
25     print_preview.ticket_items.TicketItem.call(
26         this,
27         appState,
28         print_preview.AppState.Field.MEDIA_SIZE,
29         destinationStore,
30         documentInfo);
31
32     /**
33      * Margins ticket item. Reset when this item changes.
34      * @private {!print_preview.ticket_items.MarginsType}
35      */
36     this.marginsType_ = marginsType;
37
38     /**
39      * Custom margins ticket item. Reset when this item changes.
40      * @private {!print_preview.ticket_items.CustomMargins}
41      */
42     this.customMargins_ = customMargins;
43   };
44
45   MediaSize.prototype = {
46     __proto__: print_preview.ticket_items.TicketItem.prototype,
47
48     /** @override */
49     wouldValueBeValid: function(value) {
50       if (!this.isCapabilityAvailable()) {
51         return false;
52       }
53       return this.capability.option.some(function(option) {
54         return option.width_microns == value.width_microns &&
55                option.height_microns == value.height_microns &&
56                option.is_continuous_feed == value.is_continuous_feed &&
57                option.vendor_id == value.vendor_id;
58       });
59     },
60
61     /** @override */
62     isCapabilityAvailable: function() {
63       var knownSizeToSaveAsPdf =
64           (!this.getDocumentInfoInternal().isModifiable ||
65            this.getDocumentInfoInternal().hasCssMediaStyles) &&
66           this.getSelectedDestInternal() &&
67           this.getSelectedDestInternal().id ==
68               print_preview.Destination.GooglePromotedId.SAVE_AS_PDF;
69       return !knownSizeToSaveAsPdf && !!this.capability;
70     },
71
72     /** @override */
73     isValueEqual: function(value) {
74       var myValue = this.getValue();
75       return myValue.width_microns == value.width_microns &&
76              myValue.height_microns == value.height_microns &&
77              myValue.is_continuous_feed == value.is_continuous_feed &&
78              myValue.vendor_id == value.vendor_id;
79     },
80
81     /** @return {Object} Media size capability of the selected destination. */
82     get capability() {
83       var destination = this.getSelectedDestInternal();
84       return (destination &&
85               destination.capabilities &&
86               destination.capabilities.printer &&
87               destination.capabilities.printer.media_size) ||
88              null;
89     },
90
91     /** @override */
92     getDefaultValueInternal: function() {
93       var defaultOptions = this.capability.option.filter(function(option) {
94         return option.is_default;
95       });
96       return defaultOptions.length > 0 ? defaultOptions[0] : null;
97     },
98
99     /** @override */
100     getCapabilityNotAvailableValueInternal: function() {
101       return {};
102     },
103
104     /** @override */
105     updateValueInternal: function(value) {
106       var updateMargins = !this.isValueEqual(value);
107       print_preview.ticket_items.TicketItem.prototype.updateValueInternal.call(
108           this, value);
109       if (updateMargins) {
110         // Reset the user set margins when media size changes.
111         this.marginsType_.updateValue(
112             print_preview.ticket_items.MarginsType.Value.DEFAULT);
113         this.customMargins_.updateValue(null);
114       }
115     }
116   };
117
118   // Export
119   return {
120     MediaSize: MediaSize
121   };
122 });