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 */ 12var 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 */ 29var 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 */ 41var 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 opeartion. 47 DELETE: 'delete', 48 // The item is file zip operation. 49 ZIP: 'zip', 50 // The item is general file transfer operation. 51 // This is used for the mixed operation of summarized item. 52 TRANSFER: 'transfer' 53}); 54 55/** 56 * Item of the progress center. 57 * @constructor 58 */ 59var ProgressCenterItem = function() { 60 /** 61 * Item ID. 62 * @type {string} 63 * @private 64 */ 65 this.id_ = null; 66 67 /** 68 * State of the progress item. 69 * @type {ProgressItemState} 70 */ 71 this.state = ProgressItemState.PROGRESSING; 72 73 /** 74 * Message of the progress item. 75 * @type {string} 76 */ 77 this.message = ''; 78 79 /** 80 * Max value of the progress. 81 * @type {number} 82 */ 83 this.progressMax = 0; 84 85 /** 86 * Current value of the progress. 87 * @type {number} 88 */ 89 this.progressValue = 0; 90 91 /** 92 * Type of progress item. 93 * @type {ProgressItemType} 94 */ 95 this.type = null; 96 97 /** 98 * Whether the item is summarized item or not. 99 * @type {boolean} 100 */ 101 this.summarized = false; 102 103 /** 104 * Callback function to cancel the item. 105 * @type {function()} 106 */ 107 this.cancelCallback = null; 108 109 Object.seal(this); 110}; 111 112ProgressCenterItem.prototype = { 113 /** 114 * Setter of Item ID. 115 * @param {string} value New value of ID. 116 */ 117 set id(value) { 118 if (!this.id_) 119 this.id_ = value; 120 else 121 console.error('The ID is already set. (current ID: ' + this.id_ + ')'); 122 }, 123 124 /** 125 * Getter of Item ID. 126 * @return {string} Item ID. 127 */ 128 get id() { 129 return this.id_; 130 }, 131 132 /** 133 * Gets progress rate in percent. 134 * @return {number} Progress rate in percent. 135 */ 136 get progressRateInPercent() { 137 return ~~(100 * this.progressValue / this.progressMax); 138 }, 139 140 /** 141 * Whether the item can be canceled or not. 142 * @return {boolean} True if the item can be canceled. 143 */ 144 get cancelable() { 145 return !!(this.state == ProgressItemState.PROGRESSING && 146 this.cancelCallback && 147 !this.summarized); 148 } 149}; 150