-
Notifications
You must be signed in to change notification settings - Fork 1
/
change-title-onleave.js
123 lines (103 loc) · 3.88 KB
/
change-title-onleave.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
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.ChangeTitleOnLeave = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(['module'], factory);
} else if (typeof exports !== "undefined") {
factory(module);
} else {
var mod = {
exports: {}
};
factory(mod);
global.index = mod.exports;
}
})(this, function (module) {
'use strict';
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
var _createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
var ChangeTitleOnLeave = function () {
function ChangeTitleOnLeave(options) {
_classCallCheck(this, ChangeTitleOnLeave);
this.resolveAttrs(options);
this.listenVisibility();
}
_createClass(ChangeTitleOnLeave, [{
key: 'resolveAttrs',
value: function resolveAttrs() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
this.options = {};
this.title = document.title;
if (!options.title || options.title === '') {
throw Error('The `title` is required and must be a string.');
}
this.options.title = options.title;
this.options.timeout = typeof options.timeout === 'number' ? options.timeout : 0;
this.options.onHidden = typeof options.onHidden === 'function' ? options.onHidden : null;
this.options.onVisible = typeof options.onVisible === 'function' ? options.onVisible : null;
}
}, {
key: 'listenVisibility',
value: function listenVisibility() {
var _this = this;
window.addEventListener('visibilitychange', function () {
return _this.handleVisibility();
});
}
}, {
key: 'handleVisibility',
value: function handleVisibility() {
var _this2 = this;
var timeout = this.options.timeout;
setTimeout(function () {
return _this2.updateTitle();
}, timeout * 1000);
}
}, {
key: 'updateTitle',
value: function updateTitle() {
var state = document.visibilityState;
var _options = this.options,
title = _options.title,
onHidden = _options.onHidden,
onVisible = _options.onVisible;
if (state === 'hidden') {
document.title = title;
this.useCallback(onHidden);
}
if (state === 'visible') {
document.title = this.title;
this.useCallback(onVisible);
}
}
}, {
key: 'useCallback',
value: function useCallback(callable) {
if (!callable) return;
callable();
}
}]);
return ChangeTitleOnLeave;
}();
module.exports = ChangeTitleOnLeave;
});
},{}]},{},[1])(1)
});