This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
actions_menu.js
157 lines (143 loc) · 3.95 KB
/
actions_menu.js
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
/* global CustomDialog, MozActivity, Toaster */
'use strict';
/**
* Create an actions menu to allow managing individual tones. Tones can be
* shared with other apps or (in the case of user-created tones) deleted from
* the system.
*
* @param {Node} menuElement The DOM node for the menu.
*/
function ActionsMenu(menuElement) {
this._menuElement = menuElement;
function getButton(action) {
return menuElement.querySelector('button[data-action="' + action + '"]');
}
this._cancelButton = getButton('cancel');
this._shareButton = getButton('share');
this._deleteButton = getButton('delete');
this._cancelButton.addEventListener('click', this._cancel.bind(this));
this._shareButton.addEventListener('click', this._share.bind(this));
this._deleteButton.addEventListener('click', this._delete.bind(this));
}
ActionsMenu.prototype = {
/**
* Open the actions menu.
*
* @param {Tone} tone The tone to perform actions on.
* @param {Array} inUseAs An array representing the places using this tone.
* @param {Function} callback A callback to call when the actions menu is
* closed. Takes one argument: the action that was performed.
*/
open: function(tone, inUseAs, callback) {
if (!this._menuElement.hidden) {
throw new Error('actions menu is already open');
}
this._shareButton.hidden = !tone.shareable;
this._deleteButton.hidden = !tone.deletable;
if (tone.shareable || tone.deletable) {
this._tone = tone;
this._callback = callback;
this._menuElement.hidden = false;
this._inUseAs = inUseAs;
}
return !this._menuElement.hidden;
},
/**
* Close the actions menu.
*/
close: function() {
this._menuElement.hidden = true;
},
/**
* Handle clicking the "cancel" button.
*
* @param {Event} event The event.
*/
_cancel: function(event) {
this.close();
this._finish('cancel');
},
/**
* Handle clicking the "share" button.
*
* @param {Event} event The event.
*/
_share: function(event) {
var self = this;
this.close();
this._tone.getBlob().then(function(blob) {
var activity = new MozActivity({
name: 'share',
data: {
type: 'audio/*',
// Make sure we can't share with ourselves!
__bug1015513_hide_from_self__: true,
number: 1,
blobs: [blob],
filenames: [self._tone.name],
metadata: [{
title: self._tone.name
}]
}
});
activity.onerror = function(e) {
console.warn('share activity error:', activity.error.name);
self._finish('share');
};
activity.onsuccess = function(e) {
self._finish('share');
};
});
},
/**
* Handle clicking the "delete" button.
*
* @param {Event} event The event.
*/
_delete: function(event) {
var _ = navigator.mozL10n.get;
var self = this;
this.close();
var descKey = 'delete-desc';
if (self._inUseAs.length) {
descKey += '-default-' + self._inUseAs[0];
}
var cancelButton = {
title: _('delete-cancel'),
callback: function() {
CustomDialog.hide();
self._finish('cancel');
}
};
var confirmButton = {
title: _('delete-confirm'),
callback: function() {
CustomDialog.hide();
Toaster.showToast({
messageL10nId: 'deleted-tone',
latency: 3000,
useTransition: true
});
self._tone.remove();
self._finish('delete');
}
};
CustomDialog.show(
_('delete-title'), _(descKey, {tone: self._tone.name}),
cancelButton, confirmButton
);
},
/**
* Fire our callback function and clean up the internal state of the menu.
*
* @param {String} command The command that was selected.
*/
_finish: function(command) {
if (this._callback) {
this._callback(command);
}
this._tone = null;
this._callback = null;
this._inUseAs = null;
}
};