Commit e4b2a6e0 authored by alekseys's avatar alekseys Committed by Commit bot

Export vendor capabilities to the CJT ticket.

BUG=397741

Review URL: https://codereview.chromium.org/540563002

Cr-Commit-Position: refs/heads/master@{#293265}
parent dc8c0c40
...@@ -40,11 +40,21 @@ cr.define('print_preview', function() { ...@@ -40,11 +40,21 @@ cr.define('print_preview', function() {
this.getElement().addEventListener('keydown', function f(e) { this.getElement().addEventListener('keydown', function f(e) {
// Escape pressed -> cancel the dialog. // Escape pressed -> cancel the dialog.
if (e.keyCode == 27 && !e.shiftKey && !e.ctrlKey && !e.altKey && if (!e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) {
!e.metaKey) { if (e.keyCode == 27) {
e.stopPropagation(); e.stopPropagation();
e.preventDefault(); e.preventDefault();
this.cancel(); this.cancel();
} else if (e.keyCode == 13) {
var activeElementTag = document.activeElement ?
document.activeElement.tagName.toUpperCase() : '';
if (activeElementTag != 'BUTTON' && activeElementTag != 'SELECT') {
if (this.onEnterPressedInternal()) {
e.stopPropagation();
e.preventDefault();
}
}
}
} }
}.bind(this)); }.bind(this));
...@@ -96,6 +106,14 @@ cr.define('print_preview', function() { ...@@ -96,6 +106,14 @@ cr.define('print_preview', function() {
/** @protected */ /** @protected */
onCancelInternal: function() {}, onCancelInternal: function() {},
/**
* @return {boolean} Whether the event was handled.
* @protected
*/
onEnterPressedInternal: function() {
return false;
},
/** /**
* Called when the overlay is clicked. Pulses the page. * Called when the overlay is clicked. Pulses the page.
* @param {Event} e Contains the element that was clicked. * @param {Event} e Contains the element that was clicked.
......
...@@ -48,7 +48,8 @@ cr.define('print_preview', function() { ...@@ -48,7 +48,8 @@ cr.define('print_preview', function() {
IS_HEADER_FOOTER_ENABLED: 'isHeaderFooterEnabled', IS_HEADER_FOOTER_ENABLED: 'isHeaderFooterEnabled',
IS_LANDSCAPE_ENABLED: 'isLandscapeEnabled', IS_LANDSCAPE_ENABLED: 'isLandscapeEnabled',
IS_COLLATE_ENABLED: 'isCollateEnabled', IS_COLLATE_ENABLED: 'isCollateEnabled',
IS_CSS_BACKGROUND_ENABLED: 'isCssBackgroundEnabled' IS_CSS_BACKGROUND_ENABLED: 'isCssBackgroundEnabled',
VENDOR_OPTIONS: 'vendorOptions'
}; };
/** /**
......
...@@ -176,6 +176,14 @@ cr.define('print_preview', function() { ...@@ -176,6 +176,14 @@ cr.define('print_preview', function() {
this.selectionOnly_ = this.selectionOnly_ =
new print_preview.ticket_items.SelectionOnly(this.documentInfo_); new print_preview.ticket_items.SelectionOnly(this.documentInfo_);
/**
* Vendor ticket items.
* @type {!print_preview.ticket_items.VendorItems}
* @private
*/
this.vendorItems_ = new print_preview.ticket_items.VendorItems(
this.appState_, this.destinationStore_);
/** /**
* Keeps track of event listeners for the print ticket store. * Keeps track of event listeners for the print ticket store.
* @type {!EventTracker} * @type {!EventTracker}
...@@ -267,6 +275,10 @@ cr.define('print_preview', function() { ...@@ -267,6 +275,10 @@ cr.define('print_preview', function() {
return this.selectionOnly_; return this.selectionOnly_;
}, },
get vendorItems() {
return this.vendorItems_;
},
/** /**
* @return {!print_preview.MeasurementSystem} Measurement system of the * @return {!print_preview.MeasurementSystem} Measurement system of the
* local system. * local system.
...@@ -335,6 +347,11 @@ cr.define('print_preview', function() { ...@@ -335,6 +347,11 @@ cr.define('print_preview', function() {
this.cssBackground_.updateValue(this.appState_.getField( this.cssBackground_.updateValue(this.appState_.getField(
print_preview.AppState.Field.IS_CSS_BACKGROUND_ENABLED)); print_preview.AppState.Field.IS_CSS_BACKGROUND_ENABLED));
} }
if (this.appState_.hasField(
print_preview.AppState.Field.VENDOR_OPTIONS)) {
this.vendorItems_.updateValue(this.appState_.getField(
print_preview.AppState.Field.VENDOR_OPTIONS));
}
}, },
/** /**
...@@ -408,6 +425,17 @@ cr.define('print_preview', function() { ...@@ -408,6 +425,17 @@ cr.define('print_preview', function() {
cjt.print.page_orientation = cjt.print.page_orientation =
{type: this.landscape.getValue() ? 'LANDSCAPE' : 'PORTRAIT'}; {type: this.landscape.getValue() ? 'LANDSCAPE' : 'PORTRAIT'};
} }
if (this.vendorItems.isCapabilityAvailable() &&
this.vendorItems.isUserEdited()) {
var items = this.vendorItems.ticketItems;
cjt.print.vendor_ticket_item = [];
for (var itemId in items) {
if (items.hasOwnProperty(itemId)) {
cjt.print.vendor_ticket_item.push(
{id: itemId, value: items[itemId]});
}
}
}
return JSON.stringify(cjt); return JSON.stringify(cjt);
}, },
...@@ -456,6 +484,7 @@ cr.define('print_preview', function() { ...@@ -456,6 +484,7 @@ cr.define('print_preview', function() {
this.marginsType_.updateValue( this.marginsType_.updateValue(
print_preview.ticket_items.MarginsType.Value.DEFAULT); print_preview.ticket_items.MarginsType.Value.DEFAULT);
} }
this.vendorItems_.updateValue({});
} }
}, },
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
cr.define('print_preview.ticket_items', function() {
'use strict';
/**
* An object that represents a user modifiable item in a print ticket. Each
* ticket item has a value which can be set by the user. Ticket items can also
* be unavailable for modifying if the print destination doesn't support it or
* if other ticket item constraints are not met.
* @param {print_preview.AppState} appState Application state model to update
* when ticket items update.
* @param {print_preview.DestinationStore} destinationStore Used listen for
* changes in the currently selected destination's capabilities. Since
* this is a common dependency of ticket items, it's handled in the base
* class.
* @constructor
* @extends {cr.EventTarget}
*/
function VendorItems(appState, destinationStore) {
cr.EventTarget.call(this);
/**
* Application state model to update when ticket items update.
* @private {print_preview.AppState}
*/
this.appState_ = appState || null;
/**
* Used listen for changes in the currently selected destination's
* capabilities.
* @private {print_preview.DestinationStore}
*/
this.destinationStore_ = destinationStore || null;
/**
* Vendor ticket items store, maps item id to the item value.
* @private {!Object.<string, string>}
*/
this.items_ = {};
};
VendorItems.prototype = {
__proto__: cr.EventTarget.prototype,
/** @return {boolean} Whether vendor capabilities are available. */
isCapabilityAvailable: function() {
return !!this.capability;
},
/** @return {boolean} Whether the ticket item was modified by the user. */
isUserEdited: function() {
// If there's at least one ticket item stored in values, it was edited.
for (var key in values) {
if (values.hasOwnProperty(key))
return true;
}
return false;
},
/** @return {Object} Media size capability of the selected destination. */
get capability() {
var destination = this.destinationStore_ ?
this.destinationStore_.selectedDestination : null;
return (destination &&
destination.capabilities &&
destination.capabilities.printer &&
destination.capabilities.printer.vendor_capability) ||
null;
},
/**
* Vendor ticket items store, maps item id to the item value.
* @return {!Object.<string, string>}
*/
get ticketItems() {
return this.items_;
},
/**
* @param {!Object.<string, string>} values Values to set as the values of
* vendor ticket items. Maps vendor item id to the value.
*/
updateValue: function(values) {
this.items_ = {};
if (typeof values == 'object') {
for (var key in values) {
if (values.hasOwnProperty(key) && typeof values[key] == 'string') {
// Let's empirically limit each value at 2K.
this.items_[key] = values[key].substring(0, 2048);
}
}
}
if (this.appState_) {
this.appState_.persistField(
print_preview.AppState.Field.VENDOR_OPTIONS, this.items_);
}
}
};
// Export
return {
VendorItems: VendorItems
};
});
...@@ -1243,6 +1243,7 @@ cr.define('print_preview', function() { ...@@ -1243,6 +1243,7 @@ cr.define('print_preview', function() {
<include src="data/ticket_items/fit_to_page.js"/> <include src="data/ticket_items/fit_to_page.js"/>
<include src="data/ticket_items/css_background.js"/> <include src="data/ticket_items/css_background.js"/>
<include src="data/ticket_items/selection_only.js"/> <include src="data/ticket_items/selection_only.js"/>
<include src="data/ticket_items/vendor_items.js"/>
<include src="native_layer.js"/> <include src="native_layer.js"/>
<include src="print_preview_animations.js"/> <include src="print_preview_animations.js"/>
......
...@@ -65,6 +65,11 @@ cr.define('print_preview', function() { ...@@ -65,6 +65,11 @@ cr.define('print_preview', function() {
'click', 'click',
this.cancel.bind(this)); this.cancel.bind(this));
this.tracker.add(
this.getChildElement('#done-button'),
'click',
this.onApplySettings_.bind(this));
this.tracker.add( this.tracker.add(
this.searchBox_, this.searchBox_,
print_preview.SearchBox.EventType.SEARCH, print_preview.SearchBox.EventType.SEARCH,
...@@ -95,6 +100,14 @@ cr.define('print_preview', function() { ...@@ -95,6 +100,14 @@ cr.define('print_preview', function() {
ADVANCED_SETTINGS_DIALOG_CANCELED); ADVANCED_SETTINGS_DIALOG_CANCELED);
}, },
/** @override */
onEnterPressedInternal: function() {
var doneButton = this.getChildElement('#done-button');
if (!doneButton.disabled)
doneButton.click();
return !doneButton.disabled;
},
/** /**
* @return {number} Height available for settings, in pixels. * @return {number} Height available for settings, in pixels.
* @private * @private
...@@ -136,11 +149,7 @@ cr.define('print_preview', function() { ...@@ -136,11 +149,7 @@ cr.define('print_preview', function() {
}.bind(this)); }.bind(this));
this.items_ = []; this.items_ = [];
var vendorCapabilities = var vendorCapabilities = this.printTicketStore_.vendorItems.capability;
this.destination_ &&
this.destination_.capabilities &&
this.destination_.capabilities.printer &&
this.destination_.capabilities.printer.vendor_capability;
if (!vendorCapabilities) if (!vendorCapabilities)
return; return;
...@@ -165,6 +174,22 @@ cr.define('print_preview', function() { ...@@ -165,6 +174,22 @@ cr.define('print_preview', function() {
*/ */
onSearch_: function(evt) { onSearch_: function(evt) {
this.filterLists_(evt.query); this.filterLists_(evt.query);
},
/**
* Called when current settings selection need to be stored in the ticket.
* @private
*/
onApplySettings_: function(evt) {
this.setIsVisible(false);
var values = {};
this.items_.forEach(function(item) {
if (item.isModified())
values[item.id] = item.selectedValue;
}.bind(this));
this.printTicketStore_.vendorItems.updateValue(values);
} }
}; };
......
...@@ -33,24 +33,27 @@ cr.define('print_preview', function() { ...@@ -33,24 +33,27 @@ cr.define('print_preview', function() {
this.printTicketStore_ = printTicketStore; this.printTicketStore_ = printTicketStore;
/** /**
* Capability that the list item renders. * Capability this component renders.
* @private {!Object} * @private {!Object}
*/ */
this.capability_ = capability; this.capability_ = capability;
/**
* Value selected by user. {@code null}, if user has not changed the default
* value yet (still, the value can be the default one, if it is what user
* selected).
* @private {?string}
*/
this.selectedValue_ = null;
/** /**
* Active filter query text. * Active filter query text.
* @private {RegExp} * @private {RegExp}
*/ */
this.query_ = null; this.query_ = null;
};
/** /** @private {!EventTracker} */
* Event types dispatched by this class. this.tracker_ = new EventTracker();
* @enum {string}
*/
AdvancedSettingsItem.EventType = {
CHANGED: 'print_preview.AdvancedSettingsItem.CHANGED'
}; };
AdvancedSettingsItem.prototype = { AdvancedSettingsItem.prototype = {
...@@ -69,15 +72,73 @@ cr.define('print_preview', function() { ...@@ -69,15 +72,73 @@ cr.define('print_preview', function() {
nameEl.textContent = textContent; nameEl.textContent = textContent;
nameEl.title = textContent; nameEl.title = textContent;
this.tracker_.add(
this.select_, 'change', this.onSelectChange_.bind(this));
this.tracker_.add(this.text_, 'input', this.onTextInput_.bind(this));
this.initializeValue_(); this.initializeValue_();
}, },
/**
* ID of the corresponding vendor capability.
* @return {string}
*/
get id() {
return this.capability_.id;
},
/**
* Currently selected value.
* @return {string}
*/
get selectedValue() {
return this.selectedValue_ || '';
},
/** /**
* Whether the corresponding ticket item was changed or not. * Whether the corresponding ticket item was changed or not.
* @return {boolean} * @return {boolean}
*/ */
isModified: function() { isModified: function() {
return false; return !!this.selectedValue_;
},
/**
* @return {HTMLSelectElement} Select element.
* @private
*/
get select_() {
return this.getChildElement(
'.advanced-settings-item-value-select-control');
},
/**
* @return {HTMLSelectElement} Text element.
* @private
*/
get text_() {
return this.getChildElement('.advanced-settings-item-value-text-control');
},
/**
* Called when the select element value is changed.
* @private
*/
onSelectChange_: function() {
this.selectedValue_ = this.select_.value;
this.capability_.select_cap.option.some(function(option) {
if (this.select_.value == option.value && option.is_default)
this.selectedValue_ = null;
return this.select_.value == option.value || option.is_default;
}.bind(this));
},
/**
* Called when the text element value is changed.
* @private
*/
onTextInput_: function() {
this.selectedValue_ = this.text_.value || null;
}, },
/** /**
...@@ -85,6 +146,9 @@ cr.define('print_preview', function() { ...@@ -85,6 +146,9 @@ cr.define('print_preview', function() {
* @private * @private
*/ */
initializeValue_: function() { initializeValue_: function() {
this.selectedValue_ =
this.printTicketStore_.vendorItems.ticketItems[this.id] || null;
if (this.capability_.type == 'SELECT') if (this.capability_.type == 'SELECT')
this.initializeSelectValue_(); this.initializeSelectValue_();
else else
...@@ -96,10 +160,9 @@ cr.define('print_preview', function() { ...@@ -96,10 +160,9 @@ cr.define('print_preview', function() {
* @private * @private
*/ */
initializeSelectValue_: function() { initializeSelectValue_: function() {
var selectEl = this.getChildElement(
'.advanced-settings-item-value-select-control');
setIsVisible( setIsVisible(
this.getChildElement('.advanced-settings-item-value-select'), true); this.getChildElement('.advanced-settings-item-value-select'), true);
var selectEl = this.select_;
var indexToSelect = 0; var indexToSelect = 0;
this.capability_.select_cap.option.forEach(function(option, index) { this.capability_.select_cap.option.forEach(function(option, index) {
var item = document.createElement('option'); var item = document.createElement('option');
...@@ -109,10 +172,8 @@ cr.define('print_preview', function() { ...@@ -109,10 +172,8 @@ cr.define('print_preview', function() {
indexToSelect = index; indexToSelect = index;
selectEl.add(item); selectEl.add(item);
}); });
// TODO: Try to select current ticket item.
var valueToSelect = '';
for (var i = 0, option; option = selectEl.options[i]; i++) { for (var i = 0, option; option = selectEl.options[i]; i++) {
if (option.value == valueToSelect) { if (option.value == this.selectedValue_) {
indexToSelect = i; indexToSelect = i;
break; break;
} }
...@@ -125,10 +186,9 @@ cr.define('print_preview', function() { ...@@ -125,10 +186,9 @@ cr.define('print_preview', function() {
* @private * @private
*/ */
initializeTextValue_: function() { initializeTextValue_: function() {
var textEl = this.getChildElement(
'.advanced-settings-item-value-text-control');
setIsVisible( setIsVisible(
this.getChildElement('.advanced-settings-item-value-text'), true); this.getChildElement('.advanced-settings-item-value-text'), true);
this.text_.value = this.selectedValue;
}, },
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment