|
1 | 1 | "use strict";
|
2 |
| -/** |
3 |
| - * 请将本 js 放在页面最末尾引用,否则可能不起作用。 |
4 |
| - * 由于不带 min 的 js 路径已经被过多页面使用了,因此直接以“.js”结尾的便是压缩版了,而源文件就只好用 min 的反义词 max 来表示了。 |
5 |
| - * 自定义参数方法,在页面中定义一个名为 NightTime 的对象,根据需要添加属性: |
6 |
| - * var NightTime = { |
7 |
| - * darkMode: Boolean - 若定义该属性,则页面始终保持颜色主题与设定的参数保持一致, |
8 |
| - * nightDo: Function - 自定义深色主题下额外执行的操作, |
9 |
| - * darkDo: Function - 同 nightDo,为了统一名称风格, |
10 |
| - * lightDo: Function - 自定义浅色主题下额外执行的操作,一般无需配置,只有在主动切换为浅色主题时才需要将 nightDo 执行的操作复原回去, |
11 |
| - * darkBackgroundColor: String - 设定深色主题的页面背景色, |
12 |
| - * lightBackgroundColor: String - 设定浅色主题的页面背景色, |
13 |
| - * darkCSS: String - 设定深色主题的层叠式样式表路径, |
14 |
| - * lightCSS: String - 设定浅色主题的层叠式样式表路径 |
15 |
| - * } |
| 2 | +/** configuration description |
| 3 | + * <p> |
| 4 | + * <strong> 请将本 js 放在页面最末尾引用,否则可能不起作用。 </strong><br> |
| 5 | + * 由于不带 min 的 js 路径已经被过多页面使用了,因此直接以“.js”结尾的便是压缩版了,而源文件就只好用 min 的反义词 max 来表示了。 <br> |
| 6 | + * 自定义参数方法,在页面中定义一个名为 NightTime 的<b>对象</b>,根据需要添加属性。(注意是要在本 js 之前预先以对象形式配置参数,到本 js 运行的时候才会读取这些配置!)<br> |
| 7 | + * 书写格式:{@code |
| 8 | + * var NightTime = { |
| 9 | + * // 在这里填写参数,如 “darkMode: true”。以逗号分隔。 |
| 10 | + * } |
| 11 | + * } |
| 12 | + * </p> |
| 13 | + * @see NightTime |
| 14 | + * @param darkMode: boolean - 若定义该属性,则页面始终保持颜色主题与设定的参数保持一致, |
| 15 | + * @param nightDo: function - 自定义深色主题下额外执行的操作, |
| 16 | + * @param darkDo: function - 同 nightDo,为了统一名称风格, |
| 17 | + * @param lightDo: function - 自定义浅色主题下额外执行的操作,一般无需配置,只有在主动切换为浅色主题时才需要将 nightDo 执行的操作复原回去, |
| 18 | + * @param darkBackgroundColor: string - 设定深色主题的页面背景色, |
| 19 | + * @param lightBackgroundColor: string - 设定浅色主题的页面背景色, |
| 20 | + * @param darkCSS: string - 设定深色主题的层叠式样式表路径, |
| 21 | + * @param lightCSS: string - 设定浅色主题的层叠式样式表路径 |
| 22 | + * @return void - 只声明不返回值 |
16 | 23 | */
|
17 | 24 |
|
18 | 25 | {
|
|
38 | 45 | if (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches)
|
39 | 46 | _this.darkModeFact = true; //若浏览器开启了深色主题,也会启动深色主题
|
40 | 47 | _this.theme = "";
|
41 |
| - _this.nightDo = _this.lightDo = () => { } |
| 48 | + _this.nightDo = _this.lightDo = () => {}; |
| 49 | + /** method description |
| 50 | + * 切换到当前主题 |
| 51 | + * @returns void - 切换到正常情况下配置而应该呈现的主题 |
| 52 | + */ |
42 | 53 | _this.CurrentModeStyle = () => {
|
43 | 54 | _this.css = document.getElementById("css");
|
44 | 55 | if (_this.css) _this.theme = _this.css.getAttribute("data-theme").toLowerCase();
|
45 | 56 | if (_this.darkModeFact) return _this.DarkModeStyle();
|
46 | 57 | else return _this.LightModeStyle();
|
47 | 58 | }
|
| 59 | + /** method description |
| 60 | + * 切换到对立主题 |
| 61 | + * @returns void - 切换到不是当前状态下的主题,即浅色深色模式互相切换 |
| 62 | + */ |
48 | 63 | _this.ToggleModeStyle = () => {
|
49 | 64 | _this.darkModeFact = !_this.darkModeFact;
|
50 | 65 | return _this.CurrentModeStyle();
|
|
66 | 81 | }
|
67 | 82 | _this.scrollbar = () => {
|
68 | 83 | _this.appendStyleByAjax(`css/scrollbar.css`, "scrollbar", {
|
69 |
| - "$textColor": (_this.darkModeFact ? "255, 255, 255" : "0, 0, 0") |
| 84 | + "var(--text-color-parts)": (_this.darkModeFact ? "255, 255, 255" : "0, 0, 0") |
70 | 85 | });
|
71 | 86 | }
|
72 | 87 | const notAvailableLog = "The ID you specified has been occupied by the other element, please try to use other ID!";
|
| 88 | + /** method description |
| 89 | + * 输入 CSS 文本并添加到 style 标签 |
| 90 | + * @param style: string | object - 必选,可以是模板字符串或对象,将会添加到 style 标签中 |
| 91 | + * @param id?: string - 可选,添加到的 style 标签的 id 名称。如果留空,则直接添加而不配置 id 名称,这样做可能无法规定生命周期 |
| 92 | + * @returns true - 添加成功 |
| 93 | + * @exception false - 添加失败,输入的 id 名称被占用且不为 style 标签 |
| 94 | + */ |
73 | 95 | _this.appendStyle = (style, id = "") => {
|
74 | 96 | var css;
|
75 | 97 | if (type(style) == "Object") {
|
|
90 | 112 | styleTag.innerHTML = css;
|
91 | 113 | return true;
|
92 | 114 | }
|
| 115 | + /** method description |
| 116 | + * 输入 CSS 文件链接并添加到 link 标签 |
| 117 | + * @param href: string - 必选,CSS 文件的链接地址 |
| 118 | + * @param id?: string - 可选,添加到的 link 标签的 id 名称。如果留空,则直接添加而不配置 id 名称,这样做可能无法规定生命周期 |
| 119 | + * @returns true - 添加成功 |
| 120 | + * @exception false - 添加失败,输入的 id 名称被占用且不为 link 标签 |
| 121 | + */ |
93 | 122 | _this.appendCSSLink = (href, id = "") => {
|
94 | 123 | var linkTag = document.getElementById(id);
|
95 | 124 | if (!linkTag) {
|
|
106 | 135 | linkTag.href = href;
|
107 | 136 | return true;
|
108 | 137 | }
|
| 138 | + /** method description |
| 139 | + * 输入 CSS 文件链接并添加到 style 标签,通过异步实现 |
| 140 | + * @param href: string - 必选,CSS 文件的链接地址 |
| 141 | + * @param id?: string - 可选,添加到的 link 标签的 id 名称。如果留空,则直接添加而不配置 id 名称,这样做可能无法规定生命周期 |
| 142 | + * @param variable?: object - 可选,变量名称,规定 CSS 文件的某些变量的替换值 |
| 143 | + * @returns true - 添加成功 |
| 144 | + * @exception false - 添加失败,输入的 id 名称被占用且不为 link 标签 |
| 145 | + */ |
109 | 146 | _this.appendStyleByAjax = (href, id = "", variable) => {
|
110 | 147 | var css = new XMLHttpRequest();
|
111 | 148 | css.onreadystatechange = () => {
|
|
134 | 171 | css.open("GET", path(href), true);
|
135 | 172 | return css.send();
|
136 | 173 | }
|
| 174 | + /** method description |
| 175 | + * 移除样式或 CSS 链接标签 |
| 176 | + * removeStyle 和 removeCSSLink 效果是一样的 |
| 177 | + * @param id: string - 必选,要移除的 CSS 样式的 id 名称 |
| 178 | + * @returns true - 移除成功 |
| 179 | + * @exception false - 移除失败,移除的不是 style 或 link 标签 |
| 180 | + */ |
137 | 181 | _this.removeStyle = _this.removeCSSLink = id => {
|
138 | 182 | let el = document.getElementById(id);
|
139 | 183 | if (!el) return false;
|
140 | 184 | if (el.tagName != "STYLE" && el.tagName != "LINK") return false;
|
141 | 185 | el.remove();
|
142 | 186 | return true;
|
143 | 187 | }
|
| 188 | + /** method description |
| 189 | + * 强制切换到深色主题 |
| 190 | + * @returns void |
| 191 | + */ |
144 | 192 | _this.DarkModeStyle = () => {
|
145 | 193 | _this.darkModeFact = true;
|
146 | 194 | if (_this.theme.includes("bootstrap")) {
|
|
167 | 215 | if (_this.DarkCSS) _this.appendCSSLink(_this.DarkCSS, "css");
|
168 | 216 | return true;
|
169 | 217 | }
|
| 218 | + /** method description |
| 219 | + * 强制切换到浅色主题 |
| 220 | + * @returns void |
| 221 | + */ |
170 | 222 | _this.LightModeStyle = () => {
|
171 | 223 | _this.darkModeFact = false;
|
172 | 224 | if (_this.theme.includes("bootstrap")) {
|
|
0 commit comments