Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: chrome/browser/resources/print_preview/data/destination_store.js

Issue 10108001: Refactor print preview web ui (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove new widget Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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', function() {
6 'use strict';
7
8 /**
9 * A data store that stores destinations and dispatches events when the data
10 * store changes.
11 *
12 * @constructor
13 * @extends {cr.EventTarget}
14 */
15 function DestinationStore() {
16 cr.EventTarget.call(this);
17
18 /**
19 * Internal backing store for the data store.
20 * @type {Array.<print_preview.Destination>}
21 * @private
22 */
23 this.destinations_ = [];
24
25 /**
26 * Currently selected destination.
27 * @type {print_preview.Destination}
28 * @private
29 */
30 this.selectedDestination_ = null;
31
32 /**
33 * Initial destination ID used to auto-select the first inserted destination
34 * that matches.
35 * @type {string?}
36 * @private
37 */
38 this.initialDestinationId_ = null;
39
40 /**
41 * Initial destination ID used to auto-select the first inserted destination
42 * that matches. This will be reset when match occurs.
43 * @type {string?}
44 * @private
45 */
46 this.autoSelectDestinationId_ = null;
47 };
48
49 /**
50 * Events dispatched by the data store.
51 * @enum {string}
52 */
53 DestinationStore.Event = {
54 DESTINATIONS_INSERTED:
55 'print_preview.DestinationStore.DESTINATIONS_INSERTED',
56 DESTINATION_SELECT: 'print_preview.DestinationStore.DESTINATION_SELECT'
57 };
58
59 DestinationStore.prototype = {
60 __proto__: cr.EventTarget.prototype,
61
62 get destinations() {
63 return this.destinations_.slice(0);
64 },
65
66 get selectedDestination() {
67 return this.selectedDestination_;
68 },
69
70 set initialDestinationId(initialDestinationId) {
71 this.initialDestinationId_ = initialDestinationId;
72 this.autoSelectDestinationId_ = initialDestinationId;
73 },
74
75 /** @param {print_preview.Destination} Destination to select. */
76 selectDestination: function(destination) {
77 if (!destination) {
78 throw Error('Selected destination must not be null nor undefined');
79 }
80 this.selectedDestination_ = destination;
81 this.selectedDestination_.isRecent = true;
82 this.autoSelectDestinationId_ = null;
83 cr.dispatchSimpleEvent(this, DestinationStore.Event.DESTINATION_SELECT);
84 },
85
86 /**
87 * Inserts a print destination to the data store and dispatches a
88 * DESTINATIONS_INSERTED event. If the destination matches the initial
89 * destination ID, then the destination will be automatically selected.
90 * @param {print_preview.Destination} destination Print destination to
91 * insert.
92 */
93 insertDestination: function(destination) {
94 this.destinations_.push(destination);
95 cr.dispatchSimpleEvent(
96 this, DestinationStore.Event.DESTINATIONS_INSERTED);
97 if (this.autoSelectDestinationId_ != null &&
98 destination.id == this.autoSelectDestinationId_) {
99 this.autoSelectDestinationId_ = null;
100 this.selectDestination(destination);
101 }
102 },
103
104 /**
105 * Inserts multiple print destinations to the data store and dispatches one
106 * DESTINATIONS_INSERTED event. If any of the destinations match the initial
107 * destination ID, then that destination will be automatically selected.
108 * @param {Array.<print_preview.Destination>} destinations Print
109 * destinations to insert.
110 */
111 insertDestinations: function(destinations) {
112 this.destinations_ = this.destinations_.concat(destinations);
113 cr.dispatchSimpleEvent(
114 this, DestinationStore.Event.DESTINATIONS_INSERTED);
115 if (this.autoSelectDestinationId_ != null) {
116 for (var dest, i = 0; dest = destinations[i]; i++) {
117 if (dest.id == this.autoSelectDestinationId_) {
118 this.autoSelectDestinationId_ = null;
119 this.selectDestination(dest);
120 break;
121 }
122 }
123 }
124 },
125
126 /**
127 * Updates an existing print destination with capabilities information.
128 * @param {print_preview.Destination} destination Destination to update.
129 * @return {print_preview.Destination} The existing destination that was
130 * updated.
131 */
132 updateDestination: function(destination) {
133 var existingDestination = null;
134 for (var d, i = 0; d = this.destinations_[i]; i++) {
135 if (destination.id == d.id) {
136 existingDestination = d;
137 break;
138 }
139 }
140 if (existingDestination) {
141 existingDestination.capabilities = destination.capabilities;
142 return existingDestination;
143 } else {
144 throw Error('Print destination not found: ' + destination.id);
145 }
146 },
147
148 /** Clears all print destinations. */
149 clear: function() {
150 this.destinations_ = [];
151 this.autoSelectDestinationId_ = this.initialDestinationId_;
152 }
153 };
154
155 // Export
156 return {
157 DestinationStore: DestinationStore
158 };
159 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698