jq-dynamic-titles 1.0.0 → 1.0.1

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,14 @@
1
+ # Changelog
2
+
3
+ ## 1.0.1
4
+
5
+ - Validate message and focus options, plus scrolling direction and reverse options, before changing a running effect. Invalid values throw `TypeError`.
6
+ - Allow restore to ignore effect-specific options, including an invalid default speed.
7
+ - 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.
8
+ - Clarify option validation, focus behaviour, per-call settings, callbacks, and Unicode support in the README.
9
+
10
+ ## 1.0.0
11
+
12
+ - Initial npm release with blink, scroll, constant-title, and restore actions.
13
+ - Browser-script and CommonJS registration support through `$.dynTitles`.
14
+ - Released under AGPL-3.0-only.
package/README.md CHANGED
@@ -21,7 +21,7 @@ Load jQuery before the plugin, then call `$.dynTitles`:
21
21
 
22
22
  Load the plugin after the page's `<title>` element, because it captures the original title when it registers. Load it once per page.
23
23
 
24
- The package is being prepared for npm. Once published, install it with:
24
+ The package is published on npm: [jq-dynamic-titles](https://www.npmjs.com/package/jq-dynamic-titles). Install it with:
25
25
 
26
26
  ```sh
27
27
  npm install jq-dynamic-titles jquery
@@ -79,7 +79,7 @@ $.dynTitles({
79
79
  $.dynTitles({ type: 'restore' });
80
80
  ```
81
81
 
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.
82
+ 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
83
 
84
84
  ## Options
85
85
 
@@ -102,31 +102,44 @@ Named speeds have different timings for scrolling and blinking:
102
102
  | `'medium'` | 400 ms | 2000 ms |
103
103
  | `'slow'` | 1000 ms | 5000 ms |
104
104
 
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`.
105
+ 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.
106
+
107
+ Invalid options throw `TypeError` before changing state or cancelling an existing animation:
108
+
109
+ - `type` must be one of the four actions listed above.
110
+ - 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.
111
+ - For scrolling, `direction` must be `'fwd'` or `'bwd'`, and `reverse` must be a boolean. Other actions ignore those two options.
112
+ - Restore ignores all effect-specific options.
113
+
114
+ Use actual booleans (`true` and `false`), rather than strings such as `'false'` or numbers such as `0` and `1`.
106
115
 
107
116
  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
117
 
109
118
  ## Focus behaviour
110
119
 
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.
120
+ 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.
121
+
122
+ 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
123
 
113
124
  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
125
 
115
126
  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
127
 
117
- ## Defaults and callback
128
+ ## Per-call options
118
129
 
119
- Change public defaults before making a call:
130
+ 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
131
 
121
- ```js
122
- $.dynTitles.defaults.speed = 'slow';
132
+ ## Callback
133
+
134
+ An optional second argument lets you run code after the call has processed the requested action:
123
135
 
136
+ ```js
124
137
  $.dynTitles({ message: 'Ready', onlyOnNonFocus: false }, function () {
125
138
  console.log('Title effect configured');
126
139
  });
127
140
  ```
128
141
 
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.
142
+ 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
143
 
131
144
  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
145
 
@@ -134,7 +147,15 @@ When invoked as `$.dynTitles(...)`, the callback's `this` value and the utility'
134
147
 
135
148
  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
149
 
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.
150
+ The plugin uses `Number.isFinite` and `Array.from`, so older browsers need suitable polyfills. Background tabs may throttle timers, making animation delays approximate.
151
+
152
+ ### Unicode scrolling
153
+
154
+ 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.
155
+
156
+ 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.
157
+
158
+ 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
159
 
139
160
  ## Documentation and demos
140
161
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jq-dynamic-titles",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
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"],
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
+ ],
16
24
  "author": "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,50 @@
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
+ let namedSpeed = settings.speed === 'fast' ||
40
40
  settings.speed === 'medium' ||
41
41
  settings.speed === 'slow';
42
42
 
43
- if (!namedSpeed && (
43
+ if (settings.type !== 'restore' && !namedSpeed && (
44
44
  typeof settings.speed !== 'number' ||
45
45
  !Number.isFinite(settings.speed) ||
46
46
  settings.speed <= 0 ||
47
47
  settings.speed > 2147483647
48
48
  )) {
49
49
  throw new TypeError(
50
- 'speed must be fast, medium, slow, or a positive number of milliseconds'
50
+ 'Speed must be fast, medium, slow, or a positive number of milliseconds.'
51
51
  );
52
52
  }
53
53
 
54
54
  if (['blink', 'scroll', 'const', 'restore'].indexOf(settings.type) === -1) {
55
55
  throw new TypeError(
56
- 'type must be blink, scroll, const, or restore'
56
+ 'Type must be blink, scroll, const, or restore.'
57
57
  );
58
58
  }
59
59
 
60
+ if (settings.type !== 'restore' && typeof settings.message !== 'string') {
61
+ throw new TypeError('Message must be a string.');
62
+ }
63
+
64
+ if (settings.type === 'scroll' && settings.direction !== 'fwd' && settings.direction !== 'bwd') {
65
+ throw new TypeError('Direction must be fwd or bwd.');
66
+ }
67
+
68
+ if (settings.type === 'scroll' && typeof settings.reverse !== 'boolean') {
69
+ throw new TypeError('Reverse must be a boolean.');
70
+ }
71
+
72
+ if (settings.type !== 'restore' && typeof settings.onlyOnNonFocus !== 'boolean') {
73
+ throw new TypeError('onlyOnNonFocus must be a boolean.');
74
+ }
75
+
60
76
  stopOnFocus = settings.onlyOnNonFocus && settings.type !== 'restore';
61
77
 
62
- window.clearTimeout(dynTitlesInterval); // Stop any other usages of dynTitles that maybe running
78
+ window.clearTimeout(dynTitlesInterval); // Stop any other usages of dynTitles that may be running
63
79
 
64
- var speedNumeric = false; // Setup a variable to record if the speed is numeric or not
80
+ let speedNumeric = false;
65
81
  switch (settings.speed) {
66
82
  case "fast":
67
83
  settings.speed = 200;
@@ -92,9 +108,10 @@
92
108
  scrollTitle(settings.message, settings.position, settings.speed, settings.direction, settings.reverse, settings.onlyOnNonFocus);
93
109
  break;
94
110
  case "blink":
111
+ // Fast/medium/slow needs to be slower than for scroll otherwise it looks too slow
95
112
  if (speedNumeric === false) {
96
113
  settings.speed = (settings.speed * 5);
97
- } // Fast/medium/slow needs to be slower than for scroll otherwise it looks too slow
114
+ }
98
115
  blinkTitle(settings.message, settings.position, settings.speed, false, settings.onlyOnNonFocus);
99
116
  break;
100
117
  }
@@ -130,7 +147,7 @@
130
147
  });
131
148
 
132
149
  function blinkTitle(message, position, speed, showDefault, onlyOnNonFocus) {
133
- var docuTitle;
150
+ let docuTitle;
134
151
  if (((onlyOnNonFocus === true) && (hasFocus === false)) || (onlyOnNonFocus === false)) {
135
152
  if ((showDefault === true) || (showDefault === undefined)) {
136
153
  docuTitle = origDocuTitle;
@@ -153,9 +170,26 @@
153
170
 
154
171
  function scrollTitle(message, position, speed, direction, reverse, onlyOnNonFocus) {
155
172
  // Scrolling title script inspired by HIOX INDIA / hscripts.com
156
- var msgLen = message.length;
157
- var docuTitle;
158
- var speedToUse = speed;
173
+ let characters;
174
+
175
+ if (window.Intl && window.Intl.Segmenter) {
176
+ const segmenter = new window.Intl.Segmenter(undefined, {
177
+ granularity: 'grapheme'
178
+ });
179
+
180
+ characters = Array.from(
181
+ segmenter.segment(message),
182
+ function (part) {
183
+ return part.segment;
184
+ }
185
+ );
186
+ } else {
187
+ characters = Array.from(message);
188
+ }
189
+
190
+ let msgLen = characters.length;
191
+ let docuTitle;
192
+ let speedToUse = speed;
159
193
  if (position === undefined || position === null) {
160
194
  if (direction === "bwd") {
161
195
  position = msgLen + 1;
@@ -169,10 +203,10 @@
169
203
 
170
204
  // If page is not in focus or we chose to always run the scroller
171
205
  if (((onlyOnNonFocus === true) && (hasFocus === false)) || (onlyOnNonFocus === false)) {
172
- if (direction === "fwd") { // if direction is forward or hasn't been set
206
+ if (direction === "fwd") { // If direction is forward or hasn't been set
173
207
  if (position < msgLen) {
174
208
  position = position + 1;
175
- docuTitle = message.slice(0, position);
209
+ docuTitle = characters.slice(0, position).join('');
176
210
  } else if (reverse === true) {
177
211
  speedToUse = 2000;
178
212
  direction = "bwd";
@@ -187,11 +221,13 @@
187
221
  if (position === msgLen + 1) {
188
222
  speedToUse = 2000;
189
223
  }
224
+
190
225
  position = position - 1;
191
- var startPos = (msgLen - position); // Set the start position of the substring to output
226
+
227
+ let startPos = (msgLen - position); // Set the start position of the substring to output
192
228
  // If the position is 0 or greater and the start position is less than the message length (i.e. we can select something)
193
229
  if ((position >= 0) && (startPos < msgLen)) {
194
- docuTitle = message.slice(startPos); // Select the new substring to use as the page title
230
+ docuTitle = characters.slice(startPos).join(''); // Select the new substring to use as the page title
195
231
  } else {
196
232
  // If not, i.e scrolled back to the beginning then display the default title
197
233
  docuTitle = origDocuTitle;
@@ -203,7 +239,8 @@
203
239
  }
204
240
  }
205
241
  }
206
- // replace/update the timeout
242
+
243
+ // Replace/update the timeout
207
244
  dynTitlesInterval = window.setTimeout(function () {
208
245
  scrollTitle(message, position, speed, direction, reverse, onlyOnNonFocus);
209
246
  }, speedToUse);