jq-dynamic-titles 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,21 @@
1
+ # Changelog
2
+
3
+ ## 1.1.0
4
+
5
+ - Accept case-insensitive values for effect type, named speed, and scrolling direction. Message capitalisation and option names remain unchanged.
6
+ - Accept `forward` and `backward` as aliases for `fwd` and `bwd`, including mixed-case variants.
7
+ - Preserve supplied options and defaults by normalising only the per-call settings. Existing validation still rejects invalid values.
8
+ - Update the README introduction and document case-insensitive values and direction aliases.
9
+
10
+ ## 1.0.1
11
+
12
+ - Validate message and focus options, plus scrolling direction and reverse options, before changing a running effect. Invalid values throw `TypeError`.
13
+ - Allow restore to ignore effect-specific options, including an invalid default speed.
14
+ - Preserve whole grapheme clusters while scrolling when `Intl.Segmenter` is available, including joined emoji, flags, and combining marks. Fall back to Unicode code points otherwise.
15
+ - Clarify option validation, focus behaviour, per-call settings, callbacks, and Unicode support in the README.
16
+
17
+ ## 1.0.0
18
+
19
+ - Initial npm release with blink, scroll, constant-title, and restore actions.
20
+ - Browser-script and CommonJS registration support through `$.dynTitles`.
21
+ - Released under AGPL-3.0-only.
package/README.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # jQuery Dynamic Titles
2
+ ## Dynamically change your browser tab's title using `$.dynTitles`
3
+ jQuery Dynamic Titles changes the page title to blink, scroll, or display a constant message. A fourth action stops the effect and restores the original title.
4
+
5
+ Use it to draw attention to an update, such as a new message or a completed task. By default, effects are displayed only when the window is unfocused and stop when the user returns. Set `onlyOnNonFocus: false` to display an effect regardless of focus.
2
6
 
3
- Blink, scroll, or replace a browser tab's title using `$.dynTitles`.
4
7
  This utility changes `document.title` for the page, so no element selection is needed.
5
8
 
6
9
  ## Installation
@@ -21,7 +24,7 @@ Load jQuery before the plugin, then call `$.dynTitles`:
21
24
 
22
25
  Load the plugin after the page's `<title>` element, because it captures the original title when it registers. Load it once per page.
23
26
 
24
- The package is being prepared for npm. Once published, install it with:
27
+ The package is published on npm: [jq-dynamic-titles](https://www.npmjs.com/package/jq-dynamic-titles). Install it with:
25
28
 
26
29
  ```sh
27
30
  npm install jq-dynamic-titles jquery
@@ -79,7 +82,7 @@ $.dynTitles({
79
82
  $.dynTitles({ type: 'restore' });
80
83
  ```
81
84
 
82
- Each call cancels the previous animation timer. The original title remains the value captured when the plugin registered; it is not captured again on each call. Changes made to `document.title` by other scripts do not update that saved value.
85
+ Each valid call cancels the previous animation timer. The original title remains the value captured when the plugin registered; it is not captured again on each call. Changes made to `document.title` by other scripts do not update that saved value.
83
86
 
84
87
  ## Options
85
88
 
@@ -88,7 +91,7 @@ Each call cancels the previous animation timer. The original title remains the v
88
91
  | `type` | `'blink'` | `'blink'`, `'scroll'`, `'const'`, or `'restore'` |
89
92
  | `message` | `'New Message'` | String to display; unused for restore |
90
93
  | `speed` | `'medium'` | `'fast'`, `'medium'`, `'slow'`, or a positive finite number of milliseconds, up to 2147483647 |
91
- | `direction` | `'fwd'` | Starting scroll direction: `'fwd'` or `'bwd'` |
94
+ | `direction` | `'fwd'` | Starting scroll direction: `'fwd'` / `'forward'` or `'bwd'` / `'backward'` |
92
95
  | `reverse` | `true` | Alternate scroll directions; use `false` to repeat the starting direction |
93
96
  | `onlyOnNonFocus` | `true` | Apply the effect only when the window does not have focus; restore and stop when focus returns |
94
97
 
@@ -102,31 +105,56 @@ Named speeds have different timings for scrolling and blinking:
102
105
  | `'medium'` | 400 ms | 2000 ms |
103
106
  | `'slow'` | 1000 ms | 5000 ms |
104
107
 
105
- Numeric speeds are used unchanged for either effect. Speed has no visual effect on `const` or `restore`, but is still validated for every call. Invalid `speed` or `type` values throw `TypeError` before cancelling the existing animation. Other options are not currently validated; supply a string message and boolean values for `reverse` and `onlyOnNonFocus`.
108
+ Numeric speeds are used unchanged for either effect. Speed has no visual effect on `const`, but is still validated for that action. Restore ignores speed, including an invalid default speed.
109
+
110
+ Invalid options throw `TypeError` before changing state or cancelling an existing animation:
111
+
112
+ - `type` must be one of the four actions listed above.
113
+ - For blink, scroll, and constant titles, `message` must be a string (an empty string is allowed), `speed` must meet the requirements above, and `onlyOnNonFocus` must be a boolean.
114
+ - For scrolling, `direction` must be `'fwd'`, `'forward'`, `'bwd'`, or `'backward'`, and `reverse` must be a boolean. Other actions ignore those two options.
115
+ - Restore ignores all effect-specific options.
116
+
117
+ The string values of `type`, named `speed`, and `direction` are case-insensitive. For example, `'SCROLL'`, `'Fast'`, and `'Forward'` are accepted. Full direction names are aliases: `'forward'` means `'fwd'`, and `'backward'` means `'bwd'`. Message text retains its original capitalisation. Option names remain case-sensitive, including `onlyOnNonFocus`.
118
+
119
+ ```js
120
+ $.dynTitles({
121
+ type: 'SCROLL',
122
+ speed: 'Fast',
123
+ direction: 'Forward',
124
+ message: 'Keep My Capitalisation',
125
+ onlyOnNonFocus: false
126
+ });
127
+ ```
128
+
129
+ Use actual booleans (`true` and `false`), rather than strings such as `'false'` or numbers such as `0` and `1`. Numeric speeds must be numbers, not numeric strings. Normalisation affects only the per-call settings; supplied options and public defaults are not modified.
106
130
 
107
131
  The implementation also accepts an optional `position` value for scrolling, but normal usage should omit it to use the starting position for the chosen direction.
108
132
 
109
133
  ## Focus behaviour
110
134
 
111
- With `onlyOnNonFocus: true`, call the utility after the window has lost focus, such as from an asynchronous notification or a delayed callback. Calling while focused restores the saved title without starting an effect. It does not queue an effect to start on the next blur.
135
+ Call `$.dynTitles` whenever an event warrants a title update, whether that is a user action, an AJAX response, or another notification. `onlyOnNonFocus` determines whether that update is displayed based on the window's focus at the time of the call.
136
+
137
+ With the default `onlyOnNonFocus: true`, an unfocused window displays the effect to draw the user's attention back to the page. If the window is focused, the user is already on the page, so the saved title is shown instead. That call does not schedule an effect for a later loss of focus.
112
138
 
113
139
  When focus returns, the original title is restored immediately and the effect stops, including a constant title. A later blur does not restart it; call `$.dynTitles` again for another notification. With `onlyOnNonFocus: false`, focus changes do not interrupt the effect.
114
140
 
115
141
  Focus is tracked using `document.hasFocus()` initially and window `focus`/`blur` events thereafter. The plugin does not listen for document visibility changes. Its namespaced event handlers remain attached for the lifetime of the page.
116
142
 
117
- ## Defaults and callback
143
+ ## Per-call options
118
144
 
119
- Change public defaults before making a call:
145
+ Pass only the options you want to customise. Omitted options use the defaults in the table above. Each call creates its own settings by combining those defaults with the supplied options; it does not change the defaults or carry options over from the previous call.
120
146
 
121
- ```js
122
- $.dynTitles.defaults.speed = 'slow';
147
+ ## Callback
123
148
 
149
+ An optional second argument lets you run code after the call has processed the requested action:
150
+
151
+ ```js
124
152
  $.dynTitles({ message: 'Ready', onlyOnNonFocus: false }, function () {
125
153
  console.log('Title effect configured');
126
154
  });
127
155
  ```
128
156
 
129
- Each call copies the defaults before applying its options. The optional callback runs once, synchronously after the call has processed the requested effect, even if focus prevented the effect from starting. It is not an animation completion callback. A callback can also be the only argument; that call uses the defaults.
157
+ The callback runs once, synchronously before `$.dynTitles` returns, even if focus prevented the effect from starting. It does not wait for animation ticks and is not an animation completion callback. It is not called if option validation throws an error. A callback can also be the only argument; that call uses the default options.
130
158
 
131
159
  When invoked as `$.dynTitles(...)`, the callback's `this` value and the utility's return value are jQuery. This is a utility on `$`, not a method on jQuery element collections.
132
160
 
@@ -134,7 +162,15 @@ When invoked as `$.dynTitles(...)`, the callback's `this` value and the utility'
134
162
 
135
163
  The package declares jQuery `^3.7.1 || ^4.0.0` as a peer dependency. The current version has no automated test suite; verify the demos with the jQuery versions and browsers you intend to support.
136
164
 
137
- The plugin uses `Number.isFinite`, so older browsers need a suitable polyfill. Background tabs may throttle timers, making animation delays approximate. Scrolling uses JavaScript string indexes (UTF-16 code units), so it can split emoji, surrogate pairs, and combined characters.
165
+ The plugin uses `Number.isFinite` and `Array.from`, so older browsers need suitable polyfills. Background tabs may throttle timers, making animation delays approximate.
166
+
167
+ ### Unicode scrolling
168
+
169
+ When `Intl.Segmenter` is available, scrolling advances by grapheme clusters (whole visible characters). This keeps joined emoji, flags, and letters with combining marks together in both scroll directions.
170
+
171
+ Without `Intl.Segmenter`, scrolling falls back to `Array.from(message)`, which advances by Unicode code points. This preserves surrogate pairs, including individual supplementary-plane emoji, but can split joined emoji, flags, and combining-character sequences across animation steps. To preserve those sequences in browsers without native support, provide an `Intl.Segmenter` polyfill before using the plugin.
172
+
173
+ The optional `position` value counts grapheme clusters when segmentation is available, or code points in fallback mode. Blink and constant effects display the message intact in either mode.
138
174
 
139
175
  ## Documentation and demos
140
176
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jq-dynamic-titles",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "A jQuery utility for blinking, scrolling and restoring the document title",
5
5
  "repository": {
6
6
  "type": "git",
@@ -11,9 +11,19 @@
11
11
  "url": "https://github.com/paulalbinson/jq-dynamic-titles/issues"
12
12
  },
13
13
  "main": "scripts/jq-dynamic-titles.js",
14
- "files": ["scripts/jq-dynamic-titles.js"],
15
- "keywords": ["jquery", "jquery-plugin", "document-title", "notification"],
16
- "author": "Paul Albinson",
14
+ "files": [
15
+ "scripts/jq-dynamic-titles.js",
16
+ "CHANGELOG.md"
17
+ ],
18
+ "keywords": [
19
+ "jquery",
20
+ "jquery-plugin",
21
+ "document-title",
22
+ "notification"
23
+ ],
24
+ "author": "Dr Paul Albinson",
17
25
  "license": "AGPL-3.0-only",
18
- "peerDependencies": { "jquery": "^3.7.1 || ^4.0.0" }
26
+ "peerDependencies": {
27
+ "jquery": "^3.7.1 || ^4.0.0"
28
+ }
19
29
  }
@@ -22,11 +22,11 @@
22
22
  factory(jQuery, window);
23
23
  }
24
24
  })(function ($, window) {
25
- var document = window.document;
26
- var hasFocus = document.hasFocus();
27
- var stopOnFocus = false;
28
- var dynTitlesInterval = null;
29
- var origDocuTitle = document.title;
25
+ let document = window.document;
26
+ let hasFocus = document.hasFocus();
27
+ let stopOnFocus = false;
28
+ let dynTitlesInterval = null;
29
+ let origDocuTitle = document.title;
30
30
 
31
31
  $.dynTitles = function (options, callback) {
32
32
  if (typeof options === "function") {
@@ -34,34 +34,64 @@
34
34
  options = null;
35
35
  }
36
36
 
37
- var settings = $.extend({}, $.dynTitles.defaults, options);
37
+ let settings = $.extend({}, $.dynTitles.defaults, options);
38
38
 
39
- var namedSpeed = settings.speed === 'fast' ||
39
+ // Normalise recognised string options without changing the message.
40
+ ['type', 'speed', 'direction'].forEach(function (key) {
41
+ if (typeof settings[key] === 'string') {
42
+ settings[key] = settings[key].toLowerCase();
43
+ }
44
+ });
45
+
46
+ // Accept full direction names alongside the existing shorthand.
47
+ if (settings.direction === 'forward') {
48
+ settings.direction = 'fwd';
49
+ } else if (settings.direction === 'backward') {
50
+ settings.direction = 'bwd';
51
+ }
52
+
53
+ let namedSpeed = settings.speed === 'fast' ||
40
54
  settings.speed === 'medium' ||
41
55
  settings.speed === 'slow';
42
56
 
43
- if (!namedSpeed && (
57
+ if (settings.type !== 'restore' && !namedSpeed && (
44
58
  typeof settings.speed !== 'number' ||
45
59
  !Number.isFinite(settings.speed) ||
46
60
  settings.speed <= 0 ||
47
61
  settings.speed > 2147483647
48
62
  )) {
49
63
  throw new TypeError(
50
- 'speed must be fast, medium, slow, or a positive number of milliseconds'
64
+ 'Speed must be fast, medium, slow, or a positive number of milliseconds.'
51
65
  );
52
66
  }
53
67
 
54
68
  if (['blink', 'scroll', 'const', 'restore'].indexOf(settings.type) === -1) {
55
69
  throw new TypeError(
56
- 'type must be blink, scroll, const, or restore'
70
+ 'Type must be blink, scroll, const, or restore.'
57
71
  );
58
72
  }
59
73
 
74
+ if (settings.type !== 'restore' && typeof settings.message !== 'string') {
75
+ throw new TypeError('Message must be a string.');
76
+ }
77
+
78
+ if (settings.type === 'scroll' && settings.direction !== 'fwd' && settings.direction !== 'bwd') {
79
+ throw new TypeError('Direction must be forward or backward, or the shorthand fwd or bwd.');
80
+ }
81
+
82
+ if (settings.type === 'scroll' && typeof settings.reverse !== 'boolean') {
83
+ throw new TypeError('Reverse must be a boolean.');
84
+ }
85
+
86
+ if (settings.type !== 'restore' && typeof settings.onlyOnNonFocus !== 'boolean') {
87
+ throw new TypeError('onlyOnNonFocus must be a boolean.');
88
+ }
89
+
60
90
  stopOnFocus = settings.onlyOnNonFocus && settings.type !== 'restore';
61
91
 
62
- window.clearTimeout(dynTitlesInterval); // Stop any other usages of dynTitles that maybe running
92
+ window.clearTimeout(dynTitlesInterval); // Stop any other usages of dynTitles that may be running
63
93
 
64
- var speedNumeric = false; // Setup a variable to record if the speed is numeric or not
94
+ let speedNumeric = false;
65
95
  switch (settings.speed) {
66
96
  case "fast":
67
97
  settings.speed = 200;
@@ -92,9 +122,10 @@
92
122
  scrollTitle(settings.message, settings.position, settings.speed, settings.direction, settings.reverse, settings.onlyOnNonFocus);
93
123
  break;
94
124
  case "blink":
125
+ // Fast/medium/slow needs to be slower than for scroll otherwise it looks too slow
95
126
  if (speedNumeric === false) {
96
127
  settings.speed = (settings.speed * 5);
97
- } // Fast/medium/slow needs to be slower than for scroll otherwise it looks too slow
128
+ }
98
129
  blinkTitle(settings.message, settings.position, settings.speed, false, settings.onlyOnNonFocus);
99
130
  break;
100
131
  }
@@ -130,7 +161,7 @@
130
161
  });
131
162
 
132
163
  function blinkTitle(message, position, speed, showDefault, onlyOnNonFocus) {
133
- var docuTitle;
164
+ let docuTitle;
134
165
  if (((onlyOnNonFocus === true) && (hasFocus === false)) || (onlyOnNonFocus === false)) {
135
166
  if ((showDefault === true) || (showDefault === undefined)) {
136
167
  docuTitle = origDocuTitle;
@@ -153,9 +184,26 @@
153
184
 
154
185
  function scrollTitle(message, position, speed, direction, reverse, onlyOnNonFocus) {
155
186
  // Scrolling title script inspired by HIOX INDIA / hscripts.com
156
- var msgLen = message.length;
157
- var docuTitle;
158
- var speedToUse = speed;
187
+ let characters;
188
+
189
+ if (window.Intl && window.Intl.Segmenter) {
190
+ const segmenter = new window.Intl.Segmenter(undefined, {
191
+ granularity: 'grapheme'
192
+ });
193
+
194
+ characters = Array.from(
195
+ segmenter.segment(message),
196
+ function (part) {
197
+ return part.segment;
198
+ }
199
+ );
200
+ } else {
201
+ characters = Array.from(message);
202
+ }
203
+
204
+ let msgLen = characters.length;
205
+ let docuTitle;
206
+ let speedToUse = speed;
159
207
  if (position === undefined || position === null) {
160
208
  if (direction === "bwd") {
161
209
  position = msgLen + 1;
@@ -169,10 +217,10 @@
169
217
 
170
218
  // If page is not in focus or we chose to always run the scroller
171
219
  if (((onlyOnNonFocus === true) && (hasFocus === false)) || (onlyOnNonFocus === false)) {
172
- if (direction === "fwd") { // if direction is forward or hasn't been set
220
+ if (direction === "fwd") { // If direction is forward or hasn't been set
173
221
  if (position < msgLen) {
174
222
  position = position + 1;
175
- docuTitle = message.slice(0, position);
223
+ docuTitle = characters.slice(0, position).join('');
176
224
  } else if (reverse === true) {
177
225
  speedToUse = 2000;
178
226
  direction = "bwd";
@@ -187,11 +235,13 @@
187
235
  if (position === msgLen + 1) {
188
236
  speedToUse = 2000;
189
237
  }
238
+
190
239
  position = position - 1;
191
- var startPos = (msgLen - position); // Set the start position of the substring to output
240
+
241
+ let startPos = (msgLen - position); // Set the start position of the substring to output
192
242
  // If the position is 0 or greater and the start position is less than the message length (i.e. we can select something)
193
243
  if ((position >= 0) && (startPos < msgLen)) {
194
- docuTitle = message.slice(startPos); // Select the new substring to use as the page title
244
+ docuTitle = characters.slice(startPos).join(''); // Select the new substring to use as the page title
195
245
  } else {
196
246
  // If not, i.e scrolled back to the beginning then display the default title
197
247
  docuTitle = origDocuTitle;
@@ -203,7 +253,8 @@
203
253
  }
204
254
  }
205
255
  }
206
- // replace/update the timeout
256
+
257
+ // Replace/update the timeout
207
258
  dynTitlesInterval = window.setTimeout(function () {
208
259
  scrollTitle(message, position, speed, direction, reverse, onlyOnNonFocus);
209
260
  }, speedToUse);