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
| (function(F/*fossil object*/){
/**
A very basic tooltip-like widget. It's intended to be popped up
to display basic information or basic user interaction
components, e.g. a copy-to-clipboard button.
Requires: fossil.bootstrap, fossil.dom
*/
const D = F.dom;
/**
Creates a new tooltip-like widget using the given options object.
Options:
.refresh: callback which is called just before the tooltip is
revealed or moved. It must refresh the contents of the tooltip,
if needed, by applying the content to/within this.e, which is the
base DOM element for the tooltip (and is a child of
document.body). If the contents are static and set up via the
.init option then this callback is not needed.
.adjustX: an optional callback which is called when the tooltip
is to be displayed at a given position and passed the X
viewport-relative coordinate. This routine must either return its
argument as-is or return an adjusted value. The intent is to
allow a given tooltip may be positioned more appropriately for a
given context, if needed (noting that the desired position can,
and probably should, be passed to the show() method
instead). This class's API assumes that clients give it
viewport-relative coordinates, and it will take care to translate
those to page-relative, so this callback should not do so.
.adjustY: the Y counterpart of adjustX.
.init: optional callback called one time to initialize the state
of the tooltip. This is called after the this.e has been created
and added (initially hidden) to the DOM. If this is called, it is
removed from the object immediately after it is called.
All callback options are called with the PopupWidget object as
their "this".
.cssClass: optional CSS class, or list of classes, to apply to
the new element.
.style: optional object of properties to copy directly into
the element's style object.
The options passed to this constructor get normalized into a
separate object which includes any default values for options not
provided by the caller. That object is available this the
resulting PopupWidget's options property. Default values for any
options not provided by the caller are pulled from
PopupWidget.defaultOptions, and modifying those affects all
future calls to this method but has no effect on existing
instances.
Example:
const tip = new fossil.PopupWidget({
init: function(){
// optionally populate DOM element this.e with the widget's
// content.
},
refresh: function(){
// (re)populate/refresh the contents of the main
// wrapper element, this.e.
}
});
tip.show(50, 100);
// ^^^ viewport-relative coordinates. See show() for other options.
*/
F.PopupWidget = function f(opt){
opt = F.mergeLastWins(f.defaultOptions,opt);
this.options = opt;
const e = this.e = D.addClass(D.div(), opt.cssClass);
this.show(false);
if(opt.style){
let k;
for(k in opt.style){
if(opt.style.hasOwnProperty(k)) e.style[k] = opt.style[k];
}
}
D.append(document.body, e/*must be in the DOM for size calc. to work*/);
D.copyStyle(e, opt.style);
if(opt.init){
opt.init.call(this);
delete opt.init;
}
};
/**
Default options for the PopupWidget constructor. These values are
used for any options not provided by the caller. Any changes made
to this instace affect future calls to PopupWidget() but have no
effect on existing instances.
*/
F.PopupWidget.defaultOptions = {
cssClass: 'fossil-tooltip',
style: undefined /*{optional properties copied as-is into element.style}*/,
adjustX: (x)=>x,
adjustY: (y)=>y,
refresh: function(){},
init: undefined /* optional initialization function */
};
F.PopupWidget.prototype = {
/** Returns true if the widget is currently being shown, else false. */
isShown: function(){return !this.e.classList.contains('hidden')},
/** Calls the refresh() method of the options object and returns
this object. */
refresh: function(){
if(this.options.refresh){
this.options.refresh.call(this);
}
return this;
},
/**
Shows or hides the tooltip.
Usages:
(bool showIt) => hide it or reveal it at its last position.
(x, y) => reveal/move it at/to the given
relative-to-the-viewport position, which will be adjusted to make
it page-relative.
(DOM element) => reveal/move it at/to a position based on the
the given element (adjusted slightly).
For the latter two, this.options.adjustX() and adjustY() will
be called to adjust it further.
Returns this object.
Sidebar: showing/hiding the widget is, as is conventional for
this framework, done by removing/adding the 'hidden' CSS class
to it, so that class must be defined appropriately.
*/
show: function(){
var x = undefined, y = undefined, showIt;
if(2===arguments.length){
x = arguments[0];
y = arguments[1];
showIt = true;
}else if(1===arguments.length){
if(arguments[0] instanceof HTMLElement){
const p = arguments[0];
const r = p.getBoundingClientRect();
x = r.x + r.x/5;
y = r.y - r.height/2;
showIt = true;
}else{
showIt = !!arguments[0];
}
}
if(showIt){
this.refresh();
x = this.options.adjustX.call(this,x);
y = this.options.adjustY.call(this,y);
x += window.pageXOffset;
y += window.pageYOffset;
}
if(showIt){
if('number'===typeof x && 'number'===typeof y){
this.e.style.left = x+"px";
this.e.style.top = y+"px";
}
D.removeClass(this.e, 'hidden');
}else{
D.addClass(this.e, 'hidden');
delete this.e.style.removeProperty('left');
delete this.e.style.removeProperty('top');
}
return this;
},
hide: function(){return this.show(false)}
}/*F.PopupWidget.prototype*/;
/**
Internal impl for F.toast() and friends.
args:
1) CSS class to assign to the outer element, along with
fossil-toast-message. Must be falsy for the non-warning/non-error
case.
2) Multiplier of F.toast.config.displayTimeMs. Should be
1 for default case and progressively higher for warning/error
cases.
3) The 'arguments' object from the function which is calling
this.
Returns F.toast.
*/
const toastImpl = function f(cssClass, durationMult, argsObject){
if(!f.toaster){
f.toaster = new F.PopupWidget({
cssClass: 'fossil-toast-message'
});
}
const T = f.toaster;
if(f._timer) clearTimeout(f._timer);
D.clearElement(T.e);
if(f._prevCssClass) T.e.classList.remove(f._prevCssClass);
if(cssClass) T.e.classList.add(cssClass);
f._prevCssClass = cssClass;
D.append(T.e, Array.prototype.slice.call(argsObject,0));
T.show(F.toast.config.position.x, F.toast.config.position.y);
f._timer = setTimeout(
()=>T.hide(),
F.toast.config.displayTimeMs * durationMult
);
return F.toast;
};
F.toast = {
config: {
position: { x: 5, y: 5 /*viewport-relative, pixels*/ },
displayTimeMs: 3000
},
/**
Convenience wrapper around a PopupWidget which pops up a shared
PopupWidget instance to show toast-style messages (commonly seen
on Android). Its arguments may be anything suitable for passing
to fossil.dom.append(), and each argument is first append()ed to
the toast widget, then the widget is shown for
F.toast.config.displayTimeMs milliseconds. This is called while
a toast is currently being displayed, the first will be overwritten
and the time until the message is hidden will be reset.
The toast is always shown at the viewport-relative coordinates
defined by the F.toast.config.position.
The toaster's DOM element has the CSS classes fossil-tooltip
and fossil-toast, so can be style via those.
*/
message: function(/*...*/){
return toastImpl(false,1, arguments);
},
/**
Displays a toast with the 'warning' CSS class assigned to it. It
displays for 1.5 times as long as a normal toast.
*/
warning: function(/*...*/){
return toastImpl('warning',1.5,arguments);
},
/**
Displays a toast with the 'warning' CSS class assigned to it. It
displays for twice as long as a normal toast.
*/
error: function(/*...*/){
return toastImpl('error',2,arguments);
}
}/*F.toast*/;
})(window.fossil);
|