OLD | NEW |
| (Empty) |
1 // Copyright 2013 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 'use strict'; | |
6 | |
7 /** | |
8 * Event of the ProgressCenter class. | |
9 * @enum {string} | |
10 * @const | |
11 */ | |
12 var ProgressCenterEvent = Object.freeze({ | |
13 /** | |
14 * Background page notifies item update to application windows. | |
15 */ | |
16 ITEM_UPDATED: 'itemUpdated', | |
17 | |
18 /** | |
19 * Background page notifies all the items are cleared. | |
20 */ | |
21 RESET: 'reset' | |
22 }); | |
23 | |
24 /** | |
25 * State of progress items. | |
26 * @enum {string} | |
27 * @const | |
28 */ | |
29 var ProgressItemState = Object.freeze({ | |
30 PROGRESSING: 'progressing', | |
31 COMPLETED: 'completed', | |
32 ERROR: 'error', | |
33 CANCELED: 'canceled' | |
34 }); | |
35 | |
36 /** | |
37 * Type of progress items. | |
38 * @enum {string} | |
39 * @const | |
40 */ | |
41 var ProgressItemType = Object.freeze({ | |
42 // The item is file copy operation. | |
43 COPY: 'copy', | |
44 // The item is file move operation. | |
45 MOVE: 'move', | |
46 // The item is file delete operation. | |
47 DELETE: 'delete', | |
48 // The item is file zip operation. | |
49 ZIP: 'zip', | |
50 // The item is drive sync operation. | |
51 SYNC: 'sync', | |
52 // The item is general file transfer operation. | |
53 // This is used for the mixed operation of summarized item. | |
54 TRANSFER: 'transfer' | |
55 }); | |
56 | |
57 /** | |
58 * Item of the progress center. | |
59 * @constructor | |
60 */ | |
61 var ProgressCenterItem = function() { | |
62 /** | |
63 * Item ID. | |
64 * @type {string} | |
65 * @private | |
66 */ | |
67 this.id_ = null; | |
68 | |
69 /** | |
70 * State of the progress item. | |
71 * @type {ProgressItemState} | |
72 */ | |
73 this.state = ProgressItemState.PROGRESSING; | |
74 | |
75 /** | |
76 * Message of the progress item. | |
77 * @type {string} | |
78 */ | |
79 this.message = ''; | |
80 | |
81 /** | |
82 * Max value of the progress. | |
83 * @type {number} | |
84 */ | |
85 this.progressMax = 0; | |
86 | |
87 /** | |
88 * Current value of the progress. | |
89 * @type {number} | |
90 */ | |
91 this.progressValue = 0; | |
92 | |
93 /** | |
94 * Type of progress item. | |
95 * @type {ProgressItemType} | |
96 */ | |
97 this.type = null; | |
98 | |
99 /** | |
100 * Whether the item represents a single item or not. | |
101 * @type {boolean} | |
102 */ | |
103 this.single = true; | |
104 | |
105 /** | |
106 * If the property is true, only the message of item shown in the progress | |
107 * center and the notification of the item is created as priority = -1. | |
108 * @type {boolean} | |
109 */ | |
110 this.quiet = false; | |
111 | |
112 /** | |
113 * Callback function to cancel the item. | |
114 * @type {function()} | |
115 */ | |
116 this.cancelCallback = null; | |
117 | |
118 Object.seal(this); | |
119 }; | |
120 | |
121 ProgressCenterItem.prototype = { | |
122 /** | |
123 * Setter of Item ID. | |
124 * @param {string} value New value of ID. | |
125 */ | |
126 set id(value) { | |
127 if (!this.id_) | |
128 this.id_ = value; | |
129 else | |
130 console.error('The ID is already set. (current ID: ' + this.id_ + ')'); | |
131 }, | |
132 | |
133 /** | |
134 * Getter of Item ID. | |
135 * @return {string} Item ID. | |
136 */ | |
137 get id() { | |
138 return this.id_; | |
139 }, | |
140 | |
141 /** | |
142 * Gets progress rate in percent. | |
143 * | |
144 * If the current state is canceled or completed, it always returns 0 or 100 | |
145 * respectively. | |
146 * | |
147 * @return {number} Progress rate in percent. | |
148 */ | |
149 get progressRateInPercent() { | |
150 switch (this.state) { | |
151 case ProgressItemState.CANCELED: return 0; | |
152 case ProgressItemState.COMPLETED: return 100; | |
153 default: return ~~(100 * this.progressValue / this.progressMax); | |
154 } | |
155 }, | |
156 | |
157 /** | |
158 * Whether the item can be canceled or not. | |
159 * @return {boolean} True if the item can be canceled. | |
160 */ | |
161 get cancelable() { | |
162 return !!(this.state == ProgressItemState.PROGRESSING && | |
163 this.cancelCallback && | |
164 this.single); | |
165 } | |
166 }; | |
167 | |
168 /** | |
169 * Clones the item. | |
170 * @return {ProgressCenterItem} New item having the same properties with this. | |
171 */ | |
172 ProgressCenterItem.prototype.clone = function() { | |
173 var newItem = new ProgressCenterItem(); | |
174 newItem.id = this.id; | |
175 newItem.state = this.state; | |
176 newItem.message = this.message; | |
177 newItem.progressMax = this.progressMax; | |
178 newItem.progressValue = this.progressValue; | |
179 newItem.type = this.type; | |
180 newItem.single = this.single; | |
181 newItem.quiet = this.quiet; | |
182 newItem.cancelCallback = this.cancelCallback; | |
183 return newItem; | |
184 }; | |
OLD | NEW |