72 lines
2.2 KiB
JavaScript
72 lines
2.2 KiB
JavaScript
#include args.js
|
|
|
|
const MamiWindowTitle = function(options) {
|
|
options = MamiArgs('options', options, define => {
|
|
define('getName').default(() => {}).done();
|
|
define('setTitle').default(text => { document.title = text; }).done();
|
|
define('strobeInterval').constraint(value => typeof value === 'number' || typeof value === 'function').default(500).done();
|
|
define('strobeRepeat').constraint(value => typeof value === 'number' || typeof value === 'function').default(5).done();
|
|
});
|
|
|
|
const getName = options.getName;
|
|
|
|
const setTitleImpl = options.setTitle;
|
|
const setTitle = text => {
|
|
if(text === undefined || text === null)
|
|
text = '';
|
|
else if(typeof text !== 'string')
|
|
text = text.toString();
|
|
|
|
setTitleImpl(text);
|
|
};
|
|
|
|
const defaultStrobeInterval = typeof options.strobeInterval === 'function' ? options.strobeInterval : () => options.strobeInterval;
|
|
const defaultStrobeRepeat = typeof options.strobeRepeat === 'function' ? options.strobeRepeat : () => options.strobeRepeat;
|
|
|
|
let activeStrobe;
|
|
|
|
const clearTitle = () => {
|
|
if(activeStrobe === undefined)
|
|
return;
|
|
|
|
clearInterval(activeStrobe);
|
|
activeStrobe = undefined;
|
|
|
|
setTitle(getName());
|
|
};
|
|
|
|
const strobeTitle = (titles, interval, repeat) => {
|
|
if(!Array.isArray(titles))
|
|
throw 'titles must be an array of strings';
|
|
if(titles.length < 1)
|
|
throw 'titles must contain at least one item';
|
|
if(typeof interval !== 'number')
|
|
interval = defaultStrobeInterval();
|
|
if(typeof repeat !== 'number')
|
|
repeat = defaultStrobeRepeat();
|
|
|
|
const target = titles.length * repeat;
|
|
let round = 0;
|
|
|
|
clearTitle();
|
|
setTitle(titles[0]);
|
|
|
|
activeStrobe = setInterval(() => {
|
|
if(round >= target) {
|
|
clearTitle();
|
|
setTitle(getName());
|
|
return;
|
|
}
|
|
|
|
++round;
|
|
setTitle(titles[round % titles.length]);
|
|
}, interval);
|
|
};
|
|
|
|
return {
|
|
set: setTitle,
|
|
clear: clearTitle,
|
|
strobe: strobeTitle,
|
|
};
|
|
};
|