Source RapidContext.Util.js

1/*
2 * RapidContext <https://www.rapidcontext.com/>
3 * Copyright (c) 2007-2026 Per Cederberg. All rights reserved.
4 *
5 * This program is free software: you can redistribute it and/or
6 * modify it under the terms of the BSD license.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * See the RapidContext LICENSE for more details.
13 */
14
15// Create default RapidContext object
16if (typeof(RapidContext) == "undefined") {
17    RapidContext = {};
18}
19
20/**
21 * Provides utility functions for basic objects, arrays, DOM nodes and CSS.
22 * These functions are complementary to what is available in MochiKit and/or
23 * jQuery.
24 * @namespace RapidContext.Util
25 */
26if (typeof(RapidContext.Util) == "undefined") {
27    RapidContext.Util = {};
28}
29
30
31// General utility functions
32
33/**
34 * Converts a string to a title-cased string. All word boundaries are replaced
35 * with a single space and the subsequent character is capitalized.
36 *
37 * All underscore ("_"), hyphen ("-") and lower-upper character pairs are
38 * recognized as word boundaries. Note that this function does not change the
39 * capitalization of other characters in the string.
40 *
41 * @param {string} str the string to convert
42 *
43 * @return {string} the converted string
44 *
45 * @example
46 * RapidContext.Util.toTitleCase("a short heading")
47 * ==> "A Short Heading"
48 *
49 * @example
50 * RapidContext.Util.toTitleCase("camelCase")
51 * ==> "Camel Case"
52 *
53 * @example
54 * RapidContext.Util.toTitleCase("bounding-box")
55 * ==> "Bounding Box"
56 *
57 * @example
58 * RapidContext.Util.toTitleCase("UPPER_CASE_VALUE")
59 * ==> "UPPER CASE VALUE"
60 *
61 * @deprecated Use `RapidContext.Text.startCase()` instead
62 */
63RapidContext.Util.toTitleCase = function (str) {
64    console.warn("deprecated: call to RapidContext.Util.toTitleCase(), use RapidContext.Text.startCase() instead.");
65    str = str.replaceAll(/[._-]+/g, " ").trim();
66    str = str.replaceAll(/[a-z][A-Z]/g, (s) => `${s.charAt(0)} ${s.charAt(1)}`);
67    str = str.replaceAll(/(^|\s)[a-z]/g, (s) => s.toUpperCase());
68    return str;
69};
70
71
72// DOM utility functions
73
74/**
75 * Blurs (unfocuses) a specified DOM node and all relevant child nodes. This
76 * function will recursively blur all `<a>`, `<button>`, `<input>`,
77 * `<textarea>` and `<select>` child nodes found.
78 *
79 * @param {Object} node the HTML DOM node
80 *
81 * @deprecated Use `document.activeElement.blur()` instead
82 */
83RapidContext.Util.blurAll = function (node) {
84    console.warn("deprecated: call to RapidContext.Util.blurAll(), use document.activeElement.blur() instead.");
85    node.blur();
86    const tags = ["A", "BUTTON", "INPUT", "TEXTAREA", "SELECT"];
87    for (let i = 0; i < tags.length; i++) {
88        const nodes = node.getElementsByTagName(tags[i]);
89        for (let j = 0; j < nodes.length; j++) {
90            nodes[j].blur();
91        }
92    }
93};
94