nodebb-plugin-equipment-calendar 10.0.0 → 10.0.2

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/public/js/admin.js +60 -78
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-equipment-calendar",
3
- "version": "10.0.0",
3
+ "version": "10.0.2",
4
4
  "main": "library.js",
5
5
  "license": "MIT"
6
6
  }
@@ -1,97 +1,79 @@
1
1
  'use strict';
2
- /* globals ajaxify, socket, app */
2
+ /* globals ajaxify, app, socket, require, $ */
3
3
 
4
+ // Official NodeBB ACP pattern uses the Settings module (AMD) + jQuery events.
4
5
  (function () {
5
6
  const HASH = 'equipmentCalendar';
6
7
 
7
- function isOnPage() {
8
+ function onPage() {
8
9
  try {
9
- const url = (ajaxify && ajaxify.data && ajaxify.data.url) ? ajaxify.data.url : '';
10
- return url.startsWith('admin/plugins/equipment-calendar');
11
- } catch (e) { return false; }
12
- }
13
-
14
- function rootEl() {
15
- return document.querySelector('.equipment-calendar-settings');
16
- }
17
-
18
- function fields() {
19
- return Array.from(document.querySelectorAll('.equipment-calendar-settings [data-field]'));
20
- }
21
-
22
- function setValues(values) {
23
- const map = values || {};
24
- fields().forEach((el) => {
25
- const key = el.getAttribute('data-field');
26
- let val = map[key];
27
- if (val === undefined || val === null) val = '';
28
- el.value = String(val);
29
- });
10
+ const url = (ajaxify && ajaxify.data && typeof ajaxify.data.url === 'string') ? ajaxify.data.url : '';
11
+ return url === 'admin/plugins/equipment-calendar' || url === '/admin/plugins/equipment-calendar' ||
12
+ url.startsWith('admin/plugins/equipment-calendar?') || url.startsWith('/admin/plugins/equipment-calendar?');
13
+ } catch (e) {
14
+ return false;
15
+ }
30
16
  }
31
17
 
32
- function getValues() {
33
- const out = {};
34
- fields().forEach((el) => {
35
- const key = el.getAttribute('data-field');
36
- out[key] = el.value;
37
- });
38
- return out;
39
- }
18
+ function init() {
19
+ if (!onPage()) return;
20
+ const $container = $('.equipment-calendar-settings');
21
+ if (!$container.length) return;
40
22
 
41
- function alertSuccess(msg) {
42
- if (window.app && typeof app.alertSuccess === 'function') return app.alertSuccess(msg);
43
- console.log('[success]', msg);
44
- }
45
- function alertError(msg) {
46
- if (window.app && typeof app.alertError === 'function') return app.alertError(msg);
47
- console.error('[error]', msg);
48
- }
23
+ // Prefer Settings module when available (official behaviour)
24
+ if (typeof require === 'function') {
25
+ require(['settings'], function (Settings) {
26
+ Settings.load(HASH, $container);
49
27
 
50
- function socketEmit(name, payload) {
51
- return new Promise((resolve, reject) => {
52
- if (!window.socket) return reject(new Error('socket not available'));
53
- socket.emit(name, payload, (err, data) => {
54
- if (err) return reject(err);
55
- resolve(data);
28
+ $('#ec-save').off('click.ec').on('click.ec', function () {
29
+ Settings.save(HASH, $container, function () {
30
+ if (app && app.alertSuccess) {
31
+ app.alertSuccess('[[admin/settings:settings-saved]]');
32
+ }
33
+ });
34
+ });
56
35
  });
57
- });
58
- }
59
-
60
- async function load() {
61
- const data = await socketEmit('admin.settings.get', { hash: HASH });
62
- const values = (data && (data.values || data)) || {};
63
- setValues(values);
64
- }
65
-
66
- async function save() {
67
- const values = getValues();
68
- await socketEmit('admin.settings.save', { hash: HASH, values });
69
- }
36
+ return;
37
+ }
70
38
 
71
- async function init() {
72
- if (!isOnPage()) return;
73
- if (!rootEl()) return;
39
+ // Fallback: raw socket calls (in case require is unavailable)
40
+ const fields = $container.find('[data-field]');
41
+ function getValues() {
42
+ const values = {};
43
+ fields.each(function () {
44
+ const $el = $(this);
45
+ values[$el.attr('data-field')] = $el.val();
46
+ });
47
+ return values;
48
+ }
49
+ function setValues(values) {
50
+ values = values || {};
51
+ fields.each(function () {
52
+ const $el = $(this);
53
+ const key = $el.attr('data-field');
54
+ $el.val(values[key] || '');
55
+ });
56
+ }
74
57
 
75
- try { await load(); } catch (e) {}
58
+ socket.emit('admin.settings.get', { hash: HASH }, function (err, data) {
59
+ if (err) return;
60
+ setValues((data && (data.values || data)) || {});
61
+ });
76
62
 
77
- const btn = document.getElementById('ec-save');
78
- if (btn && !btn.__bound) {
79
- btn.__bound = true;
80
- btn.addEventListener('click', async (e) => {
81
- e.preventDefault();
82
- try {
83
- await save();
84
- alertSuccess('[[admin/settings:settings-saved]]');
85
- } catch (err) {
86
- alertError(err && err.message ? err.message : String(err));
63
+ $('#ec-save').off('click.ec').on('click.ec', function () {
64
+ socket.emit('admin.settings.save', { hash: HASH, values: getValues() }, function (err) {
65
+ if (err) {
66
+ app && app.alertError && app.alertError(err.message || String(err));
67
+ return;
87
68
  }
69
+ app && app.alertSuccess && app.alertSuccess('[[admin/settings:settings-saved]]');
88
70
  });
89
- }
71
+ });
90
72
  }
91
73
 
92
- if (window.jQuery) {
93
- window.jQuery(window).on('action:ajaxify.end', init);
94
- } else {
95
- window.addEventListener('load', init);
96
- }
74
+ // Official NodeBB event binding (see NodeBB hooks docs)
75
+ $(window).off('action:ajaxify.end.ec').on('action:ajaxify.end.ec', init);
76
+
77
+ // Run once in case the page is already loaded
78
+ $(init);
97
79
  })();