Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/apps/system/js/activities.js
blob: f4db8615ffc2b32c9a2a86c0c4330f51e0625e59 (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
/* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */

'use strict';

var Activities = {
  init: function act_init() {
    window.addEventListener('mozChromeEvent', this);
  },

  handleEvent: function act_handleEvent(evt) {
    switch (evt.type) {
      case 'mozChromeEvent':
        var detail = evt.detail;
        switch (detail.type) {
          case 'activity-choice':
            this.chooseActivity(detail);
            break;
        }
        break;
    }
  },

  chooseActivity: function chooseActivity(detail) {
    this._id = detail.id;

    var choices = detail.choices;
    if (choices.length === 1) {
      this.choose('0');
    } else {
      // Since the mozChromeEvent could be triggered by a 'click', and gecko
      // event are synchronous make sure to exit the event loop before
      // showing the list.
      setTimeout((function nextTick() {
        var activityName = navigator.mozL10n.get('activity-' + detail.name);
        ListMenu.request(this._listItems(choices), activityName,
                         this.choose.bind(this), this.cancel.bind(this));
      }).bind(this));
    }
  },

  choose: function act_choose(choice) {
    var returnedChoice = {
      id: this._id,
      type: 'activity-choice',
      value: choice
    };

    this._sendEvent(returnedChoice);
    delete this._id;
  },

  cancel: function act_cancel(value) {
    var returnedChoice = {
      id: this._id,
      type: 'activity-choice',
      value: -1
    };

    this._sendEvent(returnedChoice);
    delete this._id;
  },

  _sendEvent: function act_sendEvent(value) {
    var event = document.createEvent('CustomEvent');
    event.initCustomEvent('mozContentEvent', true, true, value);
    window.dispatchEvent(event);
  },

  _listItems: function act_listItems(choices) {
    var items = [];

    choices.forEach(function(choice, index) {
      var app = Applications.getByManifestURL(choice.manifest);
      items.push({
        label: new ManifestHelper(app.manifest).name,
        icon: choice.icon,
        value: index
      });
    });

    return items;
  }
};

Activities.init();