forked from minj/foxtrick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnote.js
278 lines (243 loc) · 7.19 KB
/
note.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
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
/**
* note.js
* Foxtrick.util.note used for creating Foxtrick notes.
* @author ryanli
*/
'use strict';
/* eslint-disable */
if (!this.Foxtrick)
// @ts-ignore
var Foxtrick = {};
/* eslint-enable */
if (!Foxtrick.util)
Foxtrick.util = {};
Foxtrick.util.note = {};
/** @type {Object<string, NoteButtonType>} */
Foxtrick.util.note.BUTTONS = {};
Foxtrick.util.note.BUTTONS.OK = { name: 'button.ok' };
// BUTTON_CANCEL has a default event listener to remove the note if
// listener/onClick parameters are not passed
Foxtrick.util.note.BUTTONS.CANCEL = { name: 'button.cancel' };
/**
* @typedef NoteButtonType
* @prop {string} name
* @typedef NoteButton
* @prop {NoteButtonType} type
* @prop {Listener<HTMLInputElement, MouseEvent>} [listener]
* @typedef NoteOptions
* @prop {HTMLElement} [at]
* @prop {HTMLElement} [to]
* @prop {boolean} [focus]
* @prop {boolean} [closable]
* @prop {number} [timeout]
* @prop {NoteButton[]} [buttons]
*/
/**
* Creates a note container with a given message and ID, inserts it and returns it.
* Note is inserted before options.at, or appended to options.to, or at a default location.
* Additional options:
* focus: scrollIntoView (false)
* timeout: close automatically in X ms (null)
* closable: adds the close button (true)
* buttons: additional buttons if any:
* Array<{type: Foxtrick.util.note.BUTTONS.XXX, listener: function}>
*
* @param {document} doc
* @param {string|Node} msg message to display
* @param {string?} [noteId] note ID
* @param {NoteOptions} [options] additional options
* @return {HTMLElement} the constructed note
*/
Foxtrick.util.note.add = function(doc, msg, noteId, options) {
var note = this._create(doc, msg, noteId, options);
var id = note.id; // this may change (if id was null)
/** @type {NoteOptions} */
var opts = {
at: null,
to: null,
focus: false,
timeout: null,
};
if (options && typeof options === 'object')
Foxtrick.mergeValid(opts, options);
if (opts.at && opts.at.parentNode) {
opts.at.parentNode.insertBefore(note, opts.at);
}
else if (opts.to) {
opts.to.appendChild(note);
}
else {
// default insert position
let appendTo = Foxtrick.Pages.All.getNotes(doc);
appendTo.appendChild(note);
}
// ensure the note is visible
if (opts.focus)
note.scrollIntoView(false);
if (opts.timeout) {
let win = doc.defaultView;
win.setTimeout(function() {
try {
// eslint-disable-next-line no-invalid-this
let container = this.document.getElementById(id);
if (container)
container.parentNode.removeChild(container);
}
catch (e) {
Foxtrick.log(e);
}
}, opts.timeout);
}
// // add copy instructions for safari to copy notes
// if (Foxtrick.platform == 'Safari' && id.indexOf('copy-note') !== -1) {
// Foxtrick.sessionGet('clipboard', (string) => {
// let msgP = note.querySelector('p');
// msgP.textContent = Foxtrick.L10n.getString('specialCopy.hint');
// let textarea = doc.createElement('textarea');
// msgP.parentNode.insertBefore(textarea, msgP.nextSibling);
// textarea.value = string;
// textarea.select();
// });
// }
return note;
};
/**
* Returns a note with a loading spinner.
* Loading text can optionally be customized (defaults to Loading...).
* inline: true returns a basic span
*
* TODO check if all calls test for xmlLoad pref
*
* @param {document} doc
* @param {string} [loadingText]
* @param {boolean} [inline]
* @return {HTMLElement}
*/
Foxtrick.util.note.createLoading = function(doc, loadingText, inline) {
var text = loadingText || Foxtrick.L10n.getString('status.loading');
var container;
if (inline) {
// if the note is inline, return a span with nothing special
container = doc.createElement('span');
container.className = 'hidden';
container.textContent = text;
return container;
}
container = doc.createElement('div');
let img = doc.createElement('img');
img.src = '/Img/Icons/loading.gif';
img.alt = text;
container.appendChild(img);
container.appendChild(doc.createTextNode(' '));
container.appendChild(doc.createTextNode(text));
let id = 'ft-loading-' + Math.random().toString().slice(2);
let note = this._create(doc, container, id, { closable: false });
// delay showing
Foxtrick.addClass(note, 'hidden');
let win = doc.defaultView;
win.setTimeout(function() {
// eslint-disable-next-line no-invalid-this
Foxtrick.removeClass(this.document.getElementById(id), 'hidden');
// eslint-disable-next-line no-magic-numbers
}, 500);
return note;
};
/**
* @param {document} doc
* @param {string|Node} msg
* @param {string} [noteId]
* @param {NoteOptions} [options]
* @return {HTMLElement}
*/
// eslint-disable-next-line complexity
Foxtrick.util.note._create = function(doc, msg, noteId, options) {
/** @type {NoteOptions} */
var opts = {
buttons: null,
closable: true,
};
if (options && typeof options === 'object')
Foxtrick.mergeValid(opts, options);
try {
let id = noteId ||
'ft-note-' + Foxtrick.hash(typeof msg == 'string' ? msg : msg.textContent);
// first we remove the old notes with same name
let old = doc.getElementById(id);
if (old)
old.parentNode.removeChild(old);
var container = doc.createElement('div');
container.className = 'ft-note';
container.id = id;
// msg could be either a string or an HTML node
if (typeof msg == 'string') {
let par = doc.createElement('p');
par.textContent = msg;
container.appendChild(par);
}
else {
container.appendChild(msg);
}
if (opts.buttons && opts.buttons.length) {
/**
* default event listener for BUTTON_CANCEL
*
* @type {Listener<HTMLInputElement, MouseEvent>}
* */
let cancel = function() {
try {
// eslint-disable-next-line no-invalid-this, consistent-this
let button = this;
let doc = button.ownerDocument;
let containerId = button.getAttribute('container');
let container = doc.getElementById(containerId);
if (container)
container.parentNode.removeChild(container);
}
catch (e) {
Foxtrick.log(e);
}
};
let buttonContainer = doc.createElement('div');
for (let btn of opts.buttons) {
let button = doc.createElement('input');
button.type = 'button';
button.value = Foxtrick.L10n.getString(btn.type.name);
if (btn.listener) {
Foxtrick.onClick(button, btn.listener);
}
else if (btn.type == this.BUTTONS.CANCEL) {
button.setAttribute('container', id);
Foxtrick.onClick(button, cancel);
}
buttonContainer.appendChild(button);
}
container.appendChild(buttonContainer);
}
if (opts.closable) {
let close = doc.createElement('a');
close.className = 'close';
close.textContent = Foxtrick.L10n.getString('button.close');
Foxtrick.onClick(close, function() {
try {
// eslint-disable-next-line no-invalid-this
let container = this.parentNode;
if (container)
container.parentNode.removeChild(container);
}
catch (e) {
Foxtrick.log(e);
}
});
container.appendChild(close);
// we need to add clear since close is floated to the right
let clear = doc.createElement('div');
clear.className = 'clear';
container.appendChild(clear);
}
return container;
}
catch (e) {
Foxtrick.log(e);
}
return null;
};