summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/chromeos/first_run/step.js
blob: 213ea27c02e97919b48249f7274502df1b721ffd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright 2013 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.

/**
 * Prototype for first-run tutorial steps.
 */

cr.define('cr.FirstRun', function() {
  var Step = cr.ui.define('div');

  Step.prototype = {
    __proto__: HTMLDivElement.prototype,

    // Name of step.
    name_: null,

    // Button leading to next tutorial step.
    nextButton_: null,

    // Default control for this step.
    defaultControl_: null,

    decorate: function() {
      this.name_ = this.getAttribute('id');
      var controlsContainer = this.getElementsByClassName('controls')[0];
      if (!controlsContainer)
          throw Error('Controls not found.');
      this.nextButton_ =
          controlsContainer.getElementsByClassName('next-button')[0];
      if (!this.nextButton_)
        throw Error('Next button not found.');
      this.nextButton_.addEventListener('click', (function(e) {
        chrome.send('nextButtonClicked', [this.getName()]);
        e.stopPropagation();
      }).bind(this));
      this.defaultControl_ = controlsContainer.children[0];
    },

    /**
     * Returns name of the string.
     */
    getName: function() {
      return this.name_;
    },

    /**
     * Hides the step.
     * @param {boolean} animated Whether transition should be animated.
     * @param {function()=} opt_onHidden Called after step has been hidden.
     */
    hide: function(animated, opt_onHidden) {
      var transitionDuration =
          animated ? cr.FirstRun.getDefaultTransitionDuration() : 0;
      changeVisibility(this,
                       false,
                       transitionDuration,
                       function() {
                         this.classList.add('hidden');
                         if (opt_onHidden)
                            opt_onHidden();
                       }.bind(this));
    },

    /**
     * Shows the step.
     * @param {boolean} animated Whether transition should be animated.
     * @param {function(Step)=} opt_onShown Called after step has been shown.
     */
    show: function(animated, opt_onShown) {
      var transitionDuration =
          animated ? cr.FirstRun.getDefaultTransitionDuration() : 0;
      this.classList.remove('hidden');
      changeVisibility(this,
                       true,
                       transitionDuration,
                       function() {
                         if (opt_onShown)
                           opt_onShown(this);
                       }.bind(this));
    },

    /**
     * Sets position of the step.
     * @param {object} position Parameter with optional fields |top|,
     *     |right|, |bottom|, |left| holding corresponding offsets.
     */
    setPosition: function(position) {
      var style = this.style;
      ['top', 'right', 'bottom', 'left'].forEach(function(property) {
        if (position.hasOwnProperty(property))
          style.setProperty(property, position[property] + 'px');
      });
    },

    /**
     * Makes default control focused. Default control is a first control in
     * current implementation.
     */
    focusDefaultControl: function() {
      this.defaultControl_.focus();
    },
  };

  var Bubble = cr.ui.define('div');

  // List of rules declaring bubble's arrow position depending on text direction
  // and shelf alignment. Every rule has required field |position| with list
  // of classes that should be applied to arrow element if this rule choosen.
  // The rule is suitable if its |shelf| and |dir| fields are correspond
  // to current shelf alignment and text direction. Missing fields behaves like
  // '*' wildcard. The last suitable rule in list is choosen for arrow style.
  var ARROW_POSITION = {
    'app-list': [
      {
        position: ['points-down', 'left']
      },
      {
        dir: 'rtl',
        position: ['points-down', 'right']
      },
      {
        shelf: 'left',
        position: ['points-left', 'top']
      },
      {
        shelf: 'right',
        position: ['points-right', 'top']
      }
    ],
    'tray': [
      {
        position: ['points-right', 'top']
      },
      {
        dir: 'rtl',
        shelf: 'bottom',
        position: ['points-left', 'top']
      },
      {
        shelf: 'left',
        position: ['points-left', 'top']
      }
    ],
    'help': [
      {
        position: ['points-right', 'bottom']
      },
      {
        dir: 'rtl',
        shelf: 'bottom',
        position: ['points-left', 'bottom']
      },
      {
        shelf: 'left',
        position: ['points-left', 'bottom']
      }
    ]
  };

  var DISTANCE_TO_POINTEE = 10;
  var MINIMAL_SCREEN_OFFSET = 10;
  var ARROW_LENGTH = 6; // Keep synced with .arrow border-width.

  Bubble.prototype = {
    __proto__: Step.prototype,

    // Element displaying arrow.
    arrow_: null,

    // Unit vector directed along the bubble arrow.
    direction_: null,

    /**
     * In addition to base class 'decorate' this method creates arrow and
     * sets some properties related to arrow.
     */
    decorate: function() {
      Step.prototype.decorate.call(this);
      this.arrow_ = document.createElement('div');
      this.arrow_.classList.add('arrow');
      this.appendChild(this.arrow_);
      var inputDirection = document.documentElement.getAttribute('dir');
      var shelfAlignment = document.documentElement.getAttribute('shelf');
      var isSuitable = function(rule) {
        var inputDirectionMatch = !rule.hasOwnProperty('dir') ||
                                  rule.dir === inputDirection;
        var shelfAlignmentMatch = !rule.hasOwnProperty('shelf') ||
                                  rule.shelf === shelfAlignment;
        return inputDirectionMatch && shelfAlignmentMatch;
      };
      var lastSuitableRule = null;
      var rules = ARROW_POSITION[this.getName()];
      rules.forEach(function(rule) {
        if (isSuitable(rule))
          lastSuitableRule = rule;
      });
      assert(lastSuitableRule);
      lastSuitableRule.position.forEach(function(cls) {
        this.arrow_.classList.add(cls);
      }.bind(this));
      var list = this.arrow_.classList;
      if (list.contains('points-up'))
        this.direction_ = [0, -1];
      else if (list.contains('points-right'))
        this.direction_ = [1, 0];
      else if (list.contains('points-down'))
        this.direction_ = [0, 1];
      else // list.contains('points-left')
        this.direction_ = [-1, 0];
    },

    /**
     * Sets position of bubble in such a maner that bubble's arrow points to
     * given point.
     * @param {Array} point Bubble arrow should point to this point after
     *     positioning. |point| has format [x, y].
     * @param {offset} number Additional offset from |point|.
     */
    setPointsTo: function(point, offset) {
      var shouldShowBefore = this.hidden;
      // "Showing" bubble in order to make offset* methods work.
      if (shouldShowBefore) {
        this.style.setProperty('opacity', '0');
        this.show(false);
      }
      var arrow = [this.arrow_.offsetLeft + this.arrow_.offsetWidth / 2,
                   this.arrow_.offsetTop + this.arrow_.offsetHeight / 2];
      var totalOffset = DISTANCE_TO_POINTEE + offset;
      var left = point[0] - totalOffset * this.direction_[0] - arrow[0];
      var top = point[1] - totalOffset * this.direction_[1] - arrow[1];
      // Force bubble to be inside screen.
      if (this.arrow_.classList.contains('points-up') ||
          this.arrow_.classList.contains('points-down')) {
        left = Math.max(left, MINIMAL_SCREEN_OFFSET);
        left = Math.min(left, document.body.offsetWidth - this.offsetWidth -
            MINIMAL_SCREEN_OFFSET);
      }
      if (this.arrow_.classList.contains('points-left') ||
          this.arrow_.classList.contains('points-right')) {
        top = Math.max(top, MINIMAL_SCREEN_OFFSET);
        top = Math.min(top, document.body.offsetHeight - this.offsetHeight -
            MINIMAL_SCREEN_OFFSET);
      }
      this.style.setProperty('left', left + 'px');
      this.style.setProperty('top', top + 'px');
      if (shouldShowBefore) {
        this.hide(false);
        this.style.removeProperty('opacity');
      }
    },

    /**
     * Sets position of bubble. Overrides Step.setPosition to adjust offsets
     * in case if its direction is the same as arrow's direction.
     * @param {object} position Parameter with optional fields |top|,
     *     |right|, |bottom|, |left| holding corresponding offsets.
     */
    setPosition: function(position) {
      var arrow = this.arrow_;
      // Increasing offset if it's from side where bubble points to.
      [['top', 'points-up'],
       ['right', 'points-right'],
       ['bottom', 'points-down'],
       ['left', 'points-left']].forEach(function(mapping) {
          if (position.hasOwnProperty(mapping[0]) &&
              arrow.classList.contains(mapping[1])) {
            position[mapping[0]] += ARROW_LENGTH + DISTANCE_TO_POINTEE;
          }
        });
      Step.prototype.setPosition.call(this, position);
    },
  };

  var HelpStep = cr.ui.define('div');

  HelpStep.prototype = {
    __proto__: Bubble.prototype,

    decorate: function() {
      Bubble.prototype.decorate.call(this);
      var helpButton = this.getElementsByClassName('help-button')[0];
      helpButton.addEventListener('click', function(e) {
        chrome.send('helpButtonClicked');
        e.stopPropagation();
      });
    },
  };

  var DecorateStep = function(el) {
    if (el.id == 'help')
      HelpStep.decorate(el);
    else if (el.classList.contains('bubble'))
      Bubble.decorate(el);
    else
      Step.decorate(el);
  };

  return {DecorateStep: DecorateStep};
});