1/*
2  Copyright 2010 Google
3
4  Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  use this file except in compliance with the License. You may obtain a copy of
6  the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  License for the specific language governing permissions and limitations under
14  the License.
15
16  Brian Kennish <byoogle@google.com>
17*/
18const ELLIPSIS = ['   ', '   ', '  .', '  .', ' ..', ' ..', '...', '...'];
19const SPINNER = ['|', '/', '-', '\\'];
20const BOUNCING_BALL = [
21  '    0', '   o ', '  _  ', ' o   ', '0    ', ' o   ', '  _  ', '   o '
22];
23const SINE_WAVE = ['.::.', '::..', ':..:', '..::'];
24const TRIANGLE_WAVE = ['/\\/\\', '\\/\\/'];
25
26function Prettyload(
27  frames,
28  backgroundColor,
29  successBackgroundColor,
30  timeoutBackgroundColor,
31  frameRate,
32  minDuration,
33  maxDuration
34) {
35  const BROWSER_ACTION = chrome.browserAction;
36  var startTime;
37  const MS_PER_SEC = 1000;
38  var id;
39  this.frames = frames !== undefined ? frames : ELLIPSIS;
40  this.backgroundColor = backgroundColor !== undefined ? backgroundColor : null;
41  this.successBackgroundColor =
42    successBackgroundColor !== undefined ? successBackgroundColor : null;
43  this.timeoutBackgroundColor =
44    timeoutBackgroundColor !== undefined ? timeoutBackgroundColor : null;
45  this.frameRate = frameRate !== undefined ? frameRate : 12;
46  this.minDuration = minDuration !== undefined ? minDuration : 1;
47  this.maxDuration = maxDuration !== undefined ? maxDuration : 10;
48
49  function finish(frame, backgroundColor) {
50    clearInterval(id);
51
52    if (backgroundColor) {
53      BROWSER_ACTION.setBadgeBackgroundColor({color: backgroundColor});
54    }
55
56    BROWSER_ACTION.setBadgeText({text: frame});
57  }
58
59  this.start = function(timeoutFrame) {
60    function timeout() {
61      finish(
62        timeoutFrame !== undefined ? timeoutFrame : '', TIMEOUT_BACKGROUND_COLOR
63      );
64    }
65
66    if (this.backgroundColor) {
67      BROWSER_ACTION.setBadgeBackgroundColor({color: this.backgroundColor});
68    }
69
70    startTime = Date.now();
71    const MAX_DURATION = this.maxDuration * MS_PER_SEC;
72    const FRAMES = this.frames;
73    var i = 0;
74    const FRAME_COUNT = FRAMES.length;
75    const TIMEOUT_BACKGROUND_COLOR = this.timeoutBackgroundColor;
76
77    if (0 < MAX_DURATION) {
78      BROWSER_ACTION.setBadgeText({text: FRAMES[i]});
79
80      id = setInterval(function() {
81        if (Date.now() - startTime < MAX_DURATION) {
82          BROWSER_ACTION.setBadgeText({text: FRAMES[++i % FRAME_COUNT]});
83        } else { timeout(); }
84      }, MS_PER_SEC / this.frameRate);
85    } else { timeout(); }
86
87    return this;
88  };
89
90  this.finish = function(successFrame) {
91    const SUCCESS_BACKGROUND_COLOR = this.successBackgroundColor;
92
93    setTimeout(function() {
94      finish(
95        successFrame !== undefined ? successFrame : '', SUCCESS_BACKGROUND_COLOR
96      );
97    }, this.minDuration * MS_PER_SEC - (Date.now() - startTime));
98
99    return this;
100  };
101}
102