-
Notifications
You must be signed in to change notification settings - Fork 0
/
doctit.js
144 lines (120 loc) · 3.51 KB
/
doctit.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
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['underscore'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('underscore'));
} else {
root.doctit = factory(root._);
}
}(this, function (_) {
var document = this.document || {title: '', hidden: false};
// Title components
var _message = document.title;
var _divider = ' - ';
var _sitename = '';
// Updates the <title> element
var _setTitle = function (message) {
var title = message === undefined ? _message : message;
if (_sitename.length > 0) {
if (title.length > 0) {
title += _divider;
}
title += _sitename;
}
document.title = title;
};
// Timeout references
var _showFlash = null;
var _showDefault = null;
// Browser compatibility for document.hidden
var _hidden = _.find(
['hidden', 'mozHidden', 'msHidden', 'webkitHidden'],
function (vendor) {
return typeof document[vendor] !== 'undefined';
}
);
var self = {
get divider () {
return _divider;
},
set divider (divider) {
_divider = divider;
_setTitle();
},
get sitename () {
return _sitename;
},
set sitename (sitename) {
_sitename = sitename;
_setTitle();
},
get message () {
return _message;
},
set message (message) {
_message = message;
_setTitle();
},
flash: function (message, options) {
if (!options) {
options = {};
}
_.defaults(options, {
// Duration in milliseconds of each on/off cycle
// Does not work in some browsers (Chrome) below 1000ms,
// when the tab is inactive.
speed: 2000,
// Flash even if the page is visible
visible: true,
// Flash even if the page is hidden
hidden: true,
// Maximum number of times to flash the message
// Set to any negative number to flash forever
times: 3,
// Callback that checks if the message should keep flashing
// The callback must return `true` to stop the flashing
until: function () {},
// Callback for when the flashing ends
done: function () {}
});
// Prevent multiple concurrent flashing messages
_showFlash = clearTimeout(_showFlash);
_showDefault = clearTimeout(_showDefault);
// Don't show the message if the page is visible
if (!options.visible && document[_hidden] === false) {
options.done();
return;
}
// Don't show the message if the page is hidden
if (!options.hidden && document[_hidden] === true) {
options.done();
return;
}
// Show the message until this callback returns true
if (options.until.apply(this, arguments)) {
options.done();
return;
}
// Flash a limited number of times
if (options.times === 0) {
options.done();
return;
}
// Flash the message immediately
_message = document.title;
_setTitle(message);
// Hide the message when half the period has elapsed
_showDefault = setTimeout(function () {
_showDefault = undefined;
_setTitle();
}, options.speed / 2);
// Count down the number of times to flash the message
options.times -= 1;
// "Recurse" to the next flash
_showFlash = setTimeout(function () {
self.flash(message, options);
}, options.speed);
}
};
return self;
}));