Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / print_preview / settings / advanced_settings / advanced_settings.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', function() {
6   'use strict';
7
8   /**
9    * Modal dialog for print destination's advanced settings.
10    * @param {!print_preview.PrintTicketStore} printTicketStore Contains the
11    *     print ticket to print.
12    * @constructor
13    * @extends {print_preview.Overlay}
14    */
15   function AdvancedSettings(printTicketStore) {
16     print_preview.Overlay.call(this);
17
18     /**
19      * Contains the print ticket to print.
20      * @private {!print_preview.PrintTicketStore}
21      */
22     this.printTicketStore_ = printTicketStore;
23
24     /**
25      * Used to record usage statistics.
26      * @private {!print_preview.PrintSettingsUiMetricsContext}
27      */
28     this.metrics_ = new print_preview.PrintSettingsUiMetricsContext();
29
30     /** @private {!print_preview.SearchBox} */
31     this.searchBox_ = new print_preview.SearchBox(
32         localStrings.getString('advancedSettingsSearchBoxPlaceholder'));
33     this.addChild(this.searchBox_);
34
35     /** @private {print_preview.Destination} */
36     this.destination_ = null;
37
38     /** @private {!Array.<!print_preview.AdvancedSettingsItem>} */
39     this.items_ = [];
40   };
41
42   AdvancedSettings.prototype = {
43     __proto__: print_preview.Overlay.prototype,
44
45     /**
46      * @param {!print_preview.Destination} destination Destination to show
47      *     advanced settings for.
48      */
49     showForDestination: function(destination) {
50       assert(!this.destination_);
51       this.destination_ = destination;
52       this.getChildElement('.advanced-settings-title').textContent =
53           localStrings.getStringF('advancedSettingsDialogTitle',
54                                   this.destination_.displayName);
55       this.setIsVisible(true);
56       this.renderSettings_();
57     },
58
59     /** @override */
60     enterDocument: function() {
61       print_preview.Overlay.prototype.enterDocument.call(this);
62
63       this.tracker.add(
64           this.getChildElement('#cancel-button'),
65           'click',
66           this.cancel.bind(this));
67
68       this.tracker.add(
69           this.searchBox_,
70           print_preview.SearchBox.EventType.SEARCH,
71           this.onSearch_.bind(this));
72     },
73
74     /** @override */
75     decorateInternal: function() {
76       this.searchBox_.render(this.getChildElement('.search-box-area'));
77     },
78
79     /** @override */
80     onSetVisibleInternal: function(isVisible) {
81       if (isVisible) {
82         this.searchBox_.focus();
83         this.metrics_.record(print_preview.Metrics.PrintSettingsUiBucket.
84             ADVANCED_SETTINGS_DIALOG_SHOWN);
85       } else {
86         this.searchBox_.setQuery(null);
87         this.filterLists_(null);
88         this.destination_ = null;
89       }
90     },
91
92     /** @override */
93     onCancelInternal: function() {
94       this.metrics_.record(print_preview.Metrics.PrintSettingsUiBucket.
95           ADVANCED_SETTINGS_DIALOG_CANCELED);
96     },
97
98     /**
99      * @return {number} Height available for settings, in pixels.
100      * @private
101      */
102     getAvailableContentHeight_: function() {
103       var elStyle = window.getComputedStyle(this.getElement());
104       return this.getElement().offsetHeight -
105           parseInt(elStyle.getPropertyValue('padding-top')) -
106           parseInt(elStyle.getPropertyValue('padding-bottom')) -
107           this.getChildElement('.settings-area').offsetTop -
108           this.getChildElement('.action-area').offsetHeight;
109     },
110
111     /**
112      * Filters displayed settings with the given query.
113      * @param {?string} query Query to filter settings by.
114      * @private
115      */
116     filterLists_: function(query) {
117     },
118
119     /**
120      * Resets the filter query.
121      * @private
122      */
123     resetSearch_: function() {
124       this.searchBox_.setQuery(null);
125       this.filterLists_(null);
126     },
127
128     /**
129      * Renders all of the available settings.
130      * @private
131      */
132     renderSettings_: function() {
133       // Remove all children settings elements.
134       this.items_.forEach(function(item) {
135         this.removeChild(item);
136       }.bind(this));
137       this.items_ = [];
138
139       var vendorCapabilities =
140           this.destination_ &&
141           this.destination_.capabilities &&
142           this.destination_.capabilities.printer &&
143           this.destination_.capabilities.printer.vendor_capability;
144       if (!vendorCapabilities)
145         return;
146
147       var availableHeight = this.getAvailableContentHeight_();
148       var containerEl = this.getChildElement('.settings-area');
149       containerEl.style.maxHeight = availableHeight + 'px';
150
151       vendorCapabilities.forEach(function(capability) {
152         var item = new print_preview.AdvancedSettingsItem(
153             this.eventTarget_, this.printTicketStore_, capability);
154         this.addChild(item);
155         item.render(this.getChildElement('.settings'));
156         this.items_.push(item);
157       }.bind(this));
158     },
159
160     /**
161      * Called when settings search query changes. Filters displayed settings
162      * with the given query.
163      * @param {Event} evt Contains search query.
164      * @private
165      */
166     onSearch_: function(evt) {
167       this.filterLists_(evt.query);
168     }
169   };
170
171   // Export
172   return {
173     AdvancedSettings: AdvancedSettings
174   };
175 });