KameSame Open Framework - Settings module

Settings module for KameSame Open Framework

当前为 2022-10-06 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/451521/1101574/KameSame%20Open%20Framework%20-%20Settings%20module.js

  1. // ==UserScript==
  2. // @name KameSame Open Framework - Settings module
  3. // @namespace timberpile
  4. // @description Settings module for KameSame Open Framework
  5. // @version 0.4
  6. // @copyright 2022+, Robin Findley, Timberpile
  7. // @license MIT; http://opensource.org/licenses/MIT
  8. // ==/UserScript==
  9. (async function (global) {
  10. // const publish_context = false; // Set to 'true' to make context public.
  11. const ksof = global.ksof;
  12. const background_funcs = () => {
  13. return {
  14. open: () => {
  15. const anchor = install_anchor();
  16. let bkgd = anchor.find('> #ksofs_bkgd');
  17. if (bkgd.length === 0) {
  18. bkgd = $('<div id="ksofs_bkgd" refcnt="0"></div>');
  19. anchor.prepend(bkgd);
  20. }
  21. const refcnt = Number(bkgd.attr('refcnt'));
  22. bkgd.attr('refcnt', refcnt + 1);
  23. },
  24. close: () => {
  25. const bkgd = $('#ksof_ds > #ksofs_bkgd');
  26. if (bkgd.length === 0)
  27. return;
  28. const refcnt = Number(bkgd.attr('refcnt'));
  29. if (refcnt <= 0)
  30. return;
  31. bkgd.attr('refcnt', refcnt - 1);
  32. }
  33. };
  34. };
  35. //########################################################################
  36. //------------------------------
  37. // Constructor
  38. //------------------------------
  39. class Settings {
  40. constructor(config) {
  41. // if (!config.content) config.content = config.settings;
  42. // if (publish_context) this.context = context;
  43. this.cfg = config;
  44. this.config_list = {};
  45. this.open_dialog = $();
  46. this.background = background_funcs();
  47. }
  48. //------------------------------
  49. // Open the settings dialog.
  50. //------------------------------
  51. static save(context) {
  52. if (!ksof.settings)
  53. throw new Error('ksof.settings not defined');
  54. if (!ksof.Settings)
  55. throw new Error('ksof.Settings not defined'); // this line is used to tell the compiler that settings and Settigns definitely are defined in the following lines
  56. const script_id = ((typeof context === 'string') ? context : context.cfg.script_id);
  57. const settings = ksof.settings[script_id];
  58. if (!settings)
  59. return Promise.resolve('');
  60. return ksof.file_cache.save('ksof.settings.' + script_id, settings);
  61. }
  62. save() {
  63. return Settings.save(this);
  64. }
  65. //------------------------------
  66. // Open the settings dialog.
  67. //------------------------------
  68. static async load(context, defaults) {
  69. const script_id = ((typeof context === 'string') ? context : context.cfg.script_id);
  70. try {
  71. const settings = await ksof.file_cache.load('ksof.settings.' + script_id);
  72. return finish(settings);
  73. }
  74. catch (error) {
  75. return finish.call(null, {});
  76. }
  77. function finish(settings) {
  78. if (!ksof.settings)
  79. throw new Error('ksof.settings not defined');
  80. if (!ksof.Settings)
  81. throw new Error('ksof.Settings not defined'); // this line is used to tell the compiler that settings and Settigns definitely are defined in the following lines
  82. if (defaults)
  83. ksof.settings[script_id] = deep_merge(defaults, settings);
  84. else
  85. ksof.settings[script_id] = settings;
  86. return ksof.settings[script_id];
  87. }
  88. }
  89. load(defaults) {
  90. return Settings.load(this, defaults);
  91. }
  92. //------------------------------
  93. // Save button handler.
  94. //------------------------------
  95. save_btn() {
  96. if (!ksof.settings)
  97. throw new Error('ksof.settings not defined');
  98. if (!ksof.Settings)
  99. throw new Error('ksof.Settings not defined'); // this line is used to tell the compiler that settings and Settigns definitely are defined in the following lines
  100. const script_id = this.cfg.script_id;
  101. const settings = ksof.settings[script_id];
  102. if (settings) {
  103. const active_tabs = this.open_dialog.find('.ui-tabs-active').toArray().map(function (tab) { return '#' + tab.attributes.getNamedItem('id')?.value || ''; });
  104. if (active_tabs.length > 0)
  105. settings.ksofs_active_tabs = active_tabs;
  106. }
  107. if (this.cfg.autosave === undefined || this.cfg.autosave === true)
  108. this.save();
  109. if (typeof this.cfg.on_save === 'function')
  110. this.cfg.on_save(ksof.settings[this.cfg.script_id]);
  111. // ksof.trigger('ksof.settings.save'); // TODO what should this do?
  112. this.keep_settings = true;
  113. this.open_dialog.dialog('close');
  114. }
  115. //------------------------------
  116. // Cancel button handler.
  117. //------------------------------
  118. cancel_btn() {
  119. if (!ksof.settings)
  120. throw new Error('ksof.settings not defined');
  121. if (!ksof.Settings)
  122. throw new Error('ksof.Settings not defined'); // this line is used to tell the compiler that settings and Settigns definitely are defined in the following lines
  123. this.open_dialog.dialog('close');
  124. if (typeof this.cfg.on_cancel === 'function')
  125. this.cfg.on_cancel(ksof.settings[this.cfg.script_id]);
  126. }
  127. //------------------------------
  128. // Open the settings dialog.
  129. //------------------------------
  130. open() {
  131. if (!ksof.settings)
  132. throw new Error('ksof.settings not defined');
  133. if (!ksof.Settings)
  134. throw new Error('ksof.Settings not defined'); // this line is used to tell the compiler that settings and Settigns definitely are defined in the following lines
  135. if (!ready)
  136. return;
  137. if (this.open_dialog.length > 0)
  138. return;
  139. install_anchor();
  140. if (this.cfg.background !== false)
  141. this.background.open();
  142. this.open_dialog = $('<div id="ksofs_' + this.cfg.script_id + '" class="ksof_settings" style="display:none;"></div>');
  143. this.open_dialog.html(config_to_html(this));
  144. const resize = (event, ui) => {
  145. const is_narrow = this.open_dialog.hasClass('narrow');
  146. if (is_narrow && ui.size.width >= 510) {
  147. this.open_dialog.removeClass('narrow');
  148. }
  149. else if (!is_narrow && ui.size.width < 490) {
  150. this.open_dialog.addClass('narrow');
  151. }
  152. };
  153. const tab_activated = () => {
  154. const wrapper = $(this.open_dialog.dialog('widget'));
  155. if ((wrapper.outerHeight() || 0) + wrapper.position().top > document.body.clientHeight) {
  156. this.open_dialog.dialog('option', 'maxHeight', document.body.clientHeight);
  157. }
  158. };
  159. let width = 500;
  160. if (window.innerWidth < 510) {
  161. width = 280;
  162. this.open_dialog.addClass('narrow');
  163. }
  164. this.open_dialog.dialog({
  165. title: this.cfg.title,
  166. buttons: [
  167. { text: 'Save', click: this.save_btn.bind(this) },
  168. { text: 'Cancel', click: this.cancel_btn.bind(this) }
  169. ],
  170. width: width,
  171. maxHeight: document.body.clientHeight,
  172. modal: false,
  173. autoOpen: false,
  174. appendTo: '#ksof_ds',
  175. resize: resize.bind(this),
  176. close: close.bind(this)
  177. });
  178. $(this.open_dialog.dialog('widget')).css('position', 'fixed');
  179. this.open_dialog.parent().addClass('ksof_settings_dialog');
  180. $('.ksof_stabs').tabs({ activate: tab_activated.bind(null) });
  181. const settings = ksof.settings[this.cfg.script_id];
  182. if (settings && settings.ksofs_active_tabs instanceof Array) {
  183. const active_tabs = settings.ksofs_active_tabs;
  184. for (let tab_idx = 0; tab_idx < active_tabs.length; tab_idx++) {
  185. const tab = $(active_tabs[tab_idx]);
  186. tab.closest('.ui-tabs').tabs({ active: tab.index() });
  187. }
  188. }
  189. const toggle_multi = (e) => {
  190. if (e.button != 0)
  191. return true;
  192. const multi = $(e.currentTarget);
  193. const scroll = e.currentTarget.scrollTop;
  194. e.target.selected = !e.target.selected;
  195. setTimeout(function () {
  196. e.currentTarget.scrollTop = scroll;
  197. multi.focus();
  198. }, 0);
  199. return this.setting_changed(e);
  200. };
  201. const setting_button_clicked = (e) => {
  202. const name = e.target.attributes.name.value;
  203. const item = this.config_list[name];
  204. if (typeof item.on_click === 'function')
  205. item.on_click.call(e, name, item, this.setting_changed.bind(this, e));
  206. };
  207. this.open_dialog.dialog('open');
  208. const dialog_elem = $('#ksofs_' + this.cfg.script_id);
  209. dialog_elem.find('.setting[multiple]').on('mousedown', toggle_multi.bind(null));
  210. dialog_elem.find('.setting').on('change', this.setting_changed.bind(null));
  211. dialog_elem.find('form').on('submit', function () { return false; });
  212. dialog_elem.find('button.setting').on('click', setting_button_clicked.bind(null));
  213. if (typeof this.cfg.pre_open === 'function')
  214. this.cfg.pre_open(this.open_dialog);
  215. this.reversions = deep_merge({}, ksof.settings[this.cfg.script_id]);
  216. this.refresh();
  217. //============
  218. }
  219. //------------------------------
  220. // Handler for live settings changes. Handles built-in validation and user callbacks.
  221. //------------------------------
  222. setting_changed(event) {
  223. if (!ksof.settings)
  224. throw new Error('ksof.settings not defined');
  225. if (!ksof.Settings)
  226. throw new Error('ksof.Settings not defined'); // this line is used to tell the compiler that settings and Settigns definitely are defined in the following lines
  227. const elem = $(event.currentTarget);
  228. const name = elem.attr('name');
  229. if (!name)
  230. return false;
  231. const _item = this.config_list[name];
  232. // Extract the value
  233. let value;
  234. if (_item.type == 'dropdown') {
  235. value = elem.find(':checked').attr('name');
  236. }
  237. else if (_item.type == 'list') {
  238. const item = _item;
  239. if (item.multi === true) {
  240. value = {};
  241. elem.find('option').each(function (i, e) {
  242. const opt_name = e.getAttribute('name') || '#' + e.index;
  243. value[opt_name] = e.selected;
  244. });
  245. }
  246. else {
  247. value = elem.find(':checked').attr('name');
  248. }
  249. }
  250. else if (_item.type == 'input') {
  251. const item = _item;
  252. if (item.subtype === 'number') {
  253. value = Number(elem.val());
  254. }
  255. }
  256. else if (_item.type == 'checkbox') {
  257. value = elem.is(':checked');
  258. }
  259. else if (_item.type == 'number') {
  260. value = Number(elem.val());
  261. }
  262. else {
  263. value = elem.val();
  264. }
  265. // Validation
  266. let valid = { valid: true, msg: '' };
  267. {
  268. const item = _item;
  269. if (typeof item.validate === 'function')
  270. valid = item.validate.call(event.target, value, item);
  271. if (typeof valid === 'boolean')
  272. valid = { valid: valid, msg: '' };
  273. else if (typeof valid === 'string')
  274. valid = { valid: false, msg: valid };
  275. else if (valid === undefined)
  276. valid = { valid: true, msg: '' };
  277. }
  278. if (_item.type == 'number') {
  279. const item = _item;
  280. if (item.min && Number(value) < item.min) {
  281. valid.valid = false;
  282. if (valid.msg.length === 0) {
  283. if (typeof item.max === 'number')
  284. valid.msg = 'Must be between ' + item.min + ' and ' + item.max;
  285. else
  286. valid.msg = 'Must be ' + item.min + ' or higher';
  287. }
  288. }
  289. else if (item.max && Number(value) > item.max) {
  290. valid.valid = false;
  291. if (valid.msg.length === 0) {
  292. if (typeof item.min === 'number')
  293. valid.msg = 'Must be between ' + item.min + ' and ' + item.max;
  294. else
  295. valid.msg = 'Must be ' + item.max + ' or lower';
  296. }
  297. }
  298. }
  299. else if (_item.type == 'text') {
  300. const item = _item;
  301. if (item.match !== undefined && value.match(item.match) === null) {
  302. valid.valid = false;
  303. if (valid.msg.length === 0)
  304. // valid.msg = item.error_msg || 'Invalid value';
  305. valid.msg = 'Invalid value';
  306. }
  307. }
  308. // Style for valid/invalid
  309. const parent = elem.closest('.right');
  310. parent.find('.note').remove();
  311. if (typeof valid.msg === 'string' && valid.msg.length > 0)
  312. parent.append('<div class="note' + (valid.valid ? '' : ' error') + '">' + valid.msg + '</div>');
  313. if (!valid.valid) {
  314. elem.addClass('invalid');
  315. }
  316. else {
  317. elem.removeClass('invalid');
  318. }
  319. const script_id = this.cfg.script_id;
  320. const settings = ksof.settings[script_id];
  321. if (valid.valid) {
  322. // if (item.no_save !== true) set_value(context, settings, name, value);
  323. set_value(this, settings, name, value);
  324. const item = _item;
  325. if (typeof item.on_change === 'function')
  326. item.on_change.call(event.target, name, value, item);
  327. if (typeof this.cfg.on_change === 'function')
  328. this.cfg.on_change.call(event.target, name, value, item);
  329. // if (item.refresh_on_change === true) this.refresh();
  330. return true;
  331. }
  332. return false;
  333. }
  334. //------------------------------
  335. // Close and destroy the dialog.
  336. //------------------------------
  337. close(keep_settings) {
  338. if (!ksof.settings)
  339. throw new Error('ksof.settings not defined');
  340. if (!ksof.Settings)
  341. throw new Error('ksof.Settings not defined'); // this line is used to tell the compiler that settings and Settigns definitely are defined in the following lines
  342. if (!this.keep_settings && keep_settings !== true) {
  343. // Revert settings
  344. ksof.settings[this.cfg.script_id] = deep_merge({}, this.reversions || {});
  345. delete this.reversions;
  346. }
  347. delete this.keep_settings;
  348. this.open_dialog.dialog('destroy');
  349. this.open_dialog = $();
  350. if (this.cfg.background !== false)
  351. this.background.close();
  352. if (typeof this.cfg.on_close === 'function')
  353. this.cfg.on_close(ksof.settings[this.cfg.script_id]);
  354. }
  355. //------------------------------
  356. // Update the dialog to reflect changed settings.
  357. //------------------------------
  358. refresh() {
  359. if (!ksof.settings)
  360. throw new Error('ksof.settings not defined');
  361. if (!ksof.Settings)
  362. throw new Error('ksof.Settings not defined'); // this line is used to tell the compiler that settings and Settigns definitely are defined in the following lines
  363. const script_id = this.cfg.script_id;
  364. const settings = ksof.settings[script_id];
  365. for (const name in this.config_list) {
  366. const elem = this.open_dialog.find('#' + script_id + '_' + name);
  367. const _config = this.config_list[name];
  368. const value = get_value(this, settings, name);
  369. if (_config.type == 'dropdown') {
  370. elem.find('option[name="' + value + '"]').prop('selected', true);
  371. }
  372. else if (_config.type == 'list') {
  373. const config = _config;
  374. if (config.multi === true) {
  375. elem.find('option').each(function (i, e) {
  376. const opt_name = e.getAttribute('name') || '#' + e.index;
  377. e.selected = value[opt_name];
  378. });
  379. }
  380. else {
  381. elem.find('option[name="' + value + '"]').prop('selected', true);
  382. }
  383. }
  384. else if (_config.type == 'checkbox') {
  385. elem.prop('checked', value);
  386. }
  387. else {
  388. elem.val(value);
  389. }
  390. }
  391. if (typeof this.cfg.on_refresh === 'function')
  392. this.cfg.on_refresh(ksof.settings[this.cfg.script_id]);
  393. }
  394. }
  395. // TODO find better name than SettingsClass. SettingsObj?
  396. function createSettingsObj() {
  397. const settings_obj = (config) => {
  398. return new Settings(config);
  399. };
  400. settings_obj.save = (context) => { return Settings.save(context); };
  401. settings_obj.load = (context, defaults) => { return Settings.load(context, defaults); };
  402. settings_obj.background = background_funcs();
  403. return settings_obj;
  404. }
  405. ksof.Settings = createSettingsObj();
  406. ksof.settings = {};
  407. //########################################################################
  408. let ready = false;
  409. //========================================================================
  410. function deep_merge(...objects) {
  411. const merged = {};
  412. function recursive_merge(dest, src) {
  413. for (const prop in src) {
  414. if (typeof src[prop] === 'object' && src[prop] !== null) {
  415. const srcProp = src[prop];
  416. if (Array.isArray(srcProp)) {
  417. dest[prop] = srcProp.slice();
  418. }
  419. else {
  420. dest[prop] = dest[prop] || {};
  421. recursive_merge(dest[prop], srcProp);
  422. }
  423. }
  424. else {
  425. dest[prop] = src[prop];
  426. }
  427. }
  428. return dest;
  429. }
  430. for (const obj in objects) {
  431. recursive_merge(merged, objects[obj]);
  432. }
  433. return merged;
  434. }
  435. //------------------------------
  436. // Convert a config object to html dialog.
  437. //------------------------------
  438. /* eslint-disable no-case-declarations */
  439. function config_to_html(context) {
  440. context.config_list = {};
  441. if (!ksof.settings) {
  442. return '';
  443. }
  444. let base = ksof.settings[context.cfg.script_id];
  445. if (base === undefined)
  446. ksof.settings[context.cfg.script_id] = base = {};
  447. let html = '';
  448. const child_passback = {};
  449. const id = context.cfg.script_id + '_dialog';
  450. for (const name in context.cfg.content) {
  451. html += parse_item(name, context.cfg.content[name], child_passback);
  452. }
  453. if (child_passback.tabs && child_passback.pages)
  454. html = assemble_pages(id, child_passback.tabs, child_passback.pages) + html;
  455. return '<form>' + html + '</form>';
  456. //============
  457. function parse_item(name, _item, passback) {
  458. if (typeof _item.type !== 'string')
  459. return '';
  460. const id = context.cfg.script_id + '_' + name;
  461. let cname, html = '', child_passback, non_page = '';
  462. const _type = _item.type;
  463. if (_type == 'tabset') {
  464. const item = _item;
  465. child_passback = {};
  466. for (cname in item.content) {
  467. non_page += parse_item(cname, item.content[cname], child_passback);
  468. }
  469. if (child_passback.tabs && child_passback.pages) {
  470. html = assemble_pages(id, child_passback.tabs, child_passback.pages);
  471. }
  472. }
  473. else if (_type == 'page') {
  474. const item = _item;
  475. if (typeof item.content !== 'object')
  476. item.content = {};
  477. if (!passback.tabs) {
  478. passback.tabs = [];
  479. }
  480. if (!passback.pages) {
  481. passback.pages = [];
  482. }
  483. passback.tabs.push('<li id="' + id + '_tab"' + to_title(item.hover_tip) + '><a href="#' + id + '">' + item.label + '</a></li>');
  484. child_passback = {};
  485. for (cname in item.content)
  486. non_page += parse_item(cname, item.content[cname], child_passback);
  487. if (child_passback.tabs && child_passback.pages)
  488. html = assemble_pages(id, child_passback.tabs, child_passback.pages);
  489. passback.pages.push('<div id="' + id + '">' + html + non_page + '</div>');
  490. passback.is_page = true;
  491. html = '';
  492. }
  493. else if (_type == 'group') {
  494. const item = _item;
  495. if (typeof item.content !== 'object')
  496. item.content = {};
  497. child_passback = {};
  498. for (cname in item.content)
  499. non_page += parse_item(cname, item.content[cname], child_passback);
  500. if (child_passback.tabs && child_passback.pages)
  501. html = assemble_pages(id, child_passback.tabs, child_passback.pages);
  502. html = '<fieldset id="' + id + '" class="ksof_group"><legend>' + item.label + '</legend>' + html + non_page + '</fieldset>';
  503. }
  504. else if (_type == 'dropdown') {
  505. const item = _item;
  506. context.config_list[name] = item;
  507. let value = get_value(context, base, name);
  508. if (value === undefined) {
  509. if (item.default !== undefined) {
  510. value = item.default;
  511. }
  512. else {
  513. value = Object.keys(item.content)[0];
  514. }
  515. set_value(context, base, name, value);
  516. }
  517. html = `<select id="${id}" name="${name}" class="setting"${to_title(item.hover_tip)}>`;
  518. for (cname in item.content)
  519. html += '<option name="' + cname + '">' + escape_text(item.content[cname]) + '</option>';
  520. html += '</select>';
  521. html = make_label(item) + wrap_right(html);
  522. html = wrap_row(html, item.full_width, item.hover_tip);
  523. }
  524. else if (_type == 'list') {
  525. const item = _item;
  526. context.config_list[name] = item;
  527. let value = get_value(context, base, name);
  528. if (value === undefined) {
  529. if (item.default !== undefined) {
  530. value = item.default;
  531. }
  532. else {
  533. if (item.multi === true) {
  534. value = {};
  535. Object.keys(item.content).forEach(function (key) {
  536. value[key] = false;
  537. });
  538. }
  539. else {
  540. value = Object.keys(item.content)[0];
  541. }
  542. }
  543. set_value(context, base, name, value);
  544. }
  545. let attribs = ' size="' + (item.size || Object.keys(item.content).length || 4) + '"';
  546. if (item.multi === true)
  547. attribs += ' multiple';
  548. html = `<select id="${id}" name="${name}" class="setting list"${attribs}${to_title(item.hover_tip)}>`;
  549. for (cname in item.content)
  550. html += '<option name="' + cname + '">' + escape_text(item.content[cname]) + '</option>';
  551. html += '</select>';
  552. html = make_label(item) + wrap_right(html);
  553. html = wrap_row(html, item.full_width, item.hover_tip);
  554. }
  555. else if (_type == 'checkbox') {
  556. const item = _item;
  557. context.config_list[name] = item;
  558. html = make_label(item);
  559. let value = get_value(context, base, name);
  560. if (value === undefined) {
  561. value = (item.default || false);
  562. set_value(context, base, name, value);
  563. }
  564. html += wrap_right('<input id="' + id + '" class="setting" type="checkbox" name="' + name + '">');
  565. html = wrap_row(html, item.full_width, item.hover_tip);
  566. }
  567. else if (_type == 'input') {
  568. const item = _item;
  569. const itype = item.subtype || 'text';
  570. context.config_list[name] = item;
  571. html += make_label(item);
  572. let value = get_value(context, base, name);
  573. if (value === undefined) {
  574. const is_number = (item.subtype === 'number');
  575. value = (item.default || (is_number ? 0 : ''));
  576. set_value(context, base, name, value);
  577. }
  578. html += wrap_right(`<input id="${id}" class="setting" type="${itype}" name="${name}"${(item.placeholder ? ' placeholder="' + escape_attr(item.placeholder) + '"' : '')}>`);
  579. html = wrap_row(html, item.full_width, item.hover_tip);
  580. }
  581. else if (_type == 'number') {
  582. const item = _item;
  583. const itype = item.type;
  584. context.config_list[name] = item;
  585. html += make_label(item);
  586. let value = get_value(context, base, name);
  587. if (value === undefined) {
  588. const is_number = (item.type === 'number');
  589. value = (item.default || (is_number ? 0 : ''));
  590. set_value(context, base, name, value);
  591. }
  592. html += wrap_right(`<input id="${id}" class="setting" type="${itype}" name="${name}"${(item.placeholder ? ' placeholder="' + escape_attr(item.placeholder) + '"' : '')}>`);
  593. html = wrap_row(html, item.full_width, item.hover_tip);
  594. }
  595. else if (_type == 'text') {
  596. const item = _item;
  597. const itype = item.type;
  598. context.config_list[name] = item;
  599. html += make_label(item);
  600. let value = get_value(context, base, name);
  601. if (value === undefined) {
  602. value = (item.default || '');
  603. set_value(context, base, name, value);
  604. }
  605. html += wrap_right(`<input id="${id}" class="setting" type="${itype}" name="${name}"${(item.placeholder ? ' placeholder="' + escape_attr(item.placeholder) + '"' : '')}>`);
  606. html = wrap_row(html, item.full_width, item.hover_tip);
  607. }
  608. else if (_type == 'color') {
  609. const item = _item;
  610. context.config_list[name] = item;
  611. html += make_label(item);
  612. let value = get_value(context, base, name);
  613. if (value === undefined) {
  614. value = (item.default || '#000000');
  615. set_value(context, base, name, value);
  616. }
  617. html += wrap_right('<input id="' + id + '" class="setting" type="color" name="' + name + '">');
  618. html = wrap_row(html, item.full_width, item.hover_tip);
  619. }
  620. else if (_type == 'button') {
  621. const item = _item;
  622. context.config_list[name] = item;
  623. html += make_label(item);
  624. const text = escape_text(item.text || 'Click');
  625. html += wrap_right('<button type="button" class="setting" name="' + name + '">' + text + '</button>');
  626. html = wrap_row(html, item.full_width, item.hover_tip);
  627. }
  628. else if (_type == 'divider') {
  629. html += '<hr>';
  630. }
  631. else if (_type == 'section') {
  632. const item = _item;
  633. html += '<section>' + (item.label || '') + '</section>';
  634. }
  635. else if (_type == 'html') {
  636. const item = _item;
  637. html += make_label(item);
  638. html += item.html;
  639. switch (item.wrapper) {
  640. case 'row':
  641. html = wrap_row(html, undefined, item.hover_tip);
  642. break;
  643. case 'left':
  644. html = wrap_left(html);
  645. break;
  646. case 'right':
  647. html = wrap_right(html);
  648. break;
  649. }
  650. }
  651. return html;
  652. function make_label(item) {
  653. if (typeof item.label !== 'string')
  654. return '';
  655. return wrap_left('<label for="' + id + '">' + item.label + '</label>');
  656. }
  657. }
  658. /* eslint-enable no-case-declarations */
  659. //============
  660. function assemble_pages(id, tabs, pages) { return '<div id="' + id + '" class="ksof_stabs"><ul>' + tabs.join('') + '</ul>' + pages.join('') + '</div>'; }
  661. function wrap_row(html, full, hover_tip) { return '<div class="row' + (full ? ' full' : '') + '"' + to_title(hover_tip) + '>' + html + '</div>'; }
  662. function wrap_left(html) { return '<div class="left">' + html + '</div>'; }
  663. function wrap_right(html) { return '<div class="right">' + html + '</div>'; }
  664. function escape_text(text) {
  665. return text.replace(/[<>]/g, (ch) => {
  666. if (ch == '<')
  667. return '&lt';
  668. if (ch == '>')
  669. return '&gt';
  670. return '';
  671. });
  672. }
  673. function escape_attr(text) { return text.replace(/"/g, '&quot;'); }
  674. function to_title(tip) { if (!tip)
  675. return ''; return ' title="' + tip.replace(/"/g, '&quot;') + '"'; }
  676. }
  677. function get_value(context, base, name) {
  678. const item = context.config_list[name];
  679. const evaluate = (item.path !== undefined);
  680. const path = (item.path || name);
  681. try {
  682. if (!evaluate)
  683. return base[path];
  684. return eval(path.replace(/@/g, 'base.'));
  685. }
  686. catch (e) {
  687. return;
  688. }
  689. }
  690. function set_value(context, base, name, value) {
  691. const item = context.config_list[name];
  692. const evaluate = (item.path !== undefined);
  693. const path = (item.path || name);
  694. try {
  695. if (!evaluate)
  696. return base[path] = value;
  697. let depth = 0;
  698. let new_path = '';
  699. let param = '';
  700. let c;
  701. for (let idx = 0; idx < path.length; idx++) {
  702. c = path[idx];
  703. if (c === '[') {
  704. if (depth++ === 0) {
  705. new_path += '[';
  706. param = '';
  707. }
  708. else {
  709. param += '[';
  710. }
  711. }
  712. else if (c === ']') {
  713. if (--depth === 0) {
  714. new_path += JSON.stringify(eval(param)) + ']';
  715. }
  716. else {
  717. param += ']';
  718. }
  719. }
  720. else {
  721. if (c === '@')
  722. c = 'base.';
  723. if (depth === 0)
  724. new_path += c;
  725. else
  726. param += c;
  727. }
  728. }
  729. eval(new_path + '=value');
  730. }
  731. catch (e) {
  732. return;
  733. }
  734. }
  735. function install_anchor() {
  736. let anchor = $('#ksof_ds');
  737. if (anchor.length === 0) {
  738. anchor = $('<div id="ksof_ds"></div></div>');
  739. $('body').prepend(anchor);
  740. $('#ksof_ds').on('keydown keyup keypress', '.ksof_settings_dialog', function (e) {
  741. // Stop keys from bubbling beyond the background overlay.
  742. e.stopPropagation();
  743. });
  744. }
  745. return anchor;
  746. }
  747. //------------------------------
  748. // Load jquery UI and the appropriate CSS based on location.
  749. //------------------------------
  750. const css_url = ksof.support_files['jqui_ksmain.css'];
  751. ksof.include('Jquery');
  752. await ksof.ready('document, Jquery');
  753. await Promise.all([
  754. ksof.load_script(ksof.support_files['jquery_ui.js'], true /* cache */),
  755. ksof.load_css(css_url, true /* cache */)
  756. ]);
  757. ready = true;
  758. // Workaround... https://community.wanikani.com/t/19984/55
  759. try {
  760. const temp = $.fn;
  761. delete temp.autocomplete;
  762. }
  763. catch (e) {
  764. // do nothing
  765. }
  766. // Notify listeners that we are ready.
  767. // Delay guarantees include() callbacks are called before ready() callbacks.
  768. setTimeout(function () { ksof.set_state('ksof.Settings', 'ready'); }, 0);
  769. })(this);