@nebularstreams/libmui 0.1.11 → 0.1.13

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.
@@ -1,227 +0,0 @@
1
- /* eslint-disable */
2
- /**
3
- *
4
- * String Tokenizer functions
5
- *
6
- * Substitutes parameters inside strings
7
- * Parameters are separated by $ at the beggining and end: $PARAM$, $PARAM1$
8
- * Parameters can navigate source object like $object/very/deep$
9
- * Parameters can include VERY SIMPLE default values like $object/very/deep=myvalue$
10
- *
11
- * Note that default values cannot contain conflicting regexp symbols. This is a side functionality
12
- * and never meant to support any default value use case, but alpha chars and numbers
13
- *
14
- * (c) Rodolfo LP 2001 - Public Domain
15
- *
16
- * @typedef {{ id:string,url:string}} UrlDefinition
17
- * @typedef {{ message:string, getTitle:boolean=false, defaultTitle:string="url" }} ResolveUrlOptions
18
- */
19
-
20
- /* eslint-disable no-use-before-define,no-unused-vars,prefer-destructuring,no-param-reassign,no-prototype-builtins */
21
-
22
- // eslint-disable-next-line no-useless-escape
23
- // noinspection RegExpRedundantEscape
24
- const
25
- TAG = "[TOKENIZER]",
26
- TOK_EXPR = /\$([A-Za-z0-9_\/\:\=]+)\$/g,
27
-
28
- tokenize = (str, obj, isEncode) => tokenizeCore(str, obj, {
29
- encode: isEncode,
30
- defvalue: ""
31
- }),
32
- tokenizeKeep = (str, obj, isEncode) => tokenizeCore(str, obj, {encode: isEncode}),
33
- tokenizeWithDefault = (str, obj, defaultValue) => tokenizeCore(str, obj, defaultValue == null ? null : {defvalue: defaultValue}),
34
- tokenizeStrict = (str, obj, isEncode, isUnescape) => tokenizeCore(str, obj, {
35
- encode: isEncode,
36
- unescape: isUnescape,
37
- dothrow: true
38
- }),
39
-
40
- Parsers = {
41
- daterel: epoch => new Date(epoch).toISOString()
42
- };
43
-
44
- function tokenizeCore(str, obj, options) {
45
-
46
- if (!str) return "";
47
- if (typeof (str) !== "string") return str;
48
-
49
- // eslint-disable-next-line no-param-reassign
50
- options = options || {};
51
- obj = obj || {};
52
-
53
- let ckey;
54
-
55
- return str.replace(TOK_EXPR, (match, complexkey) => {
56
-
57
- let defval = null,
58
- isFreak = false;
59
-
60
- if (complexkey.indexOf("=") > 0) {
61
- ckey = complexkey.split("=");
62
- complexkey = ckey[0];
63
- isFreak = (ckey.length === 3);
64
- // eslint-disable-next-line no-nested-ternary
65
- defval = isFreak
66
- ? options.dothrow ? ckey[2] : null
67
- : ckey[1];
68
- }
69
-
70
- const typed = complexkey.split(":"),
71
- key = typed[0],
72
- keytype = typed[1] || null,
73
-
74
- keys = key.split("/"),
75
- root = keys.shift();
76
-
77
- let
78
- data = obj[root];
79
-
80
- if (typeof (data) === "number") data = "" + data;
81
-
82
- let fdata = (keys.length ? getPointedData(data, keys.join("/")) : data) || defval;
83
-
84
- if (fdata == null && options.dothrow) {
85
- throw new Error("Parameter " + root + (keys.length ? "/" + keys.join("/") : "") + " is required");
86
- }
87
-
88
- if (fdata == null) {
89
- fdata = options.hasOwnProperty("defvalue")
90
- ? options.defvalue
91
- : "$" + key + (isFreak ? "==" + ckey[2] : "") + "$";
92
- }
93
-
94
- if (keytype && Parsers[keytype]) {
95
- fdata = Parsers[keytype](fdata);
96
- }
97
-
98
- if (options.unescape) {
99
- fdata = unescape(fdata);
100
- }
101
-
102
- return (options.encode ? escape(fdata) : fdata);
103
-
104
- });
105
- }
106
-
107
- /**
108
- * Resolves a pointer into a JSON data structure
109
- * @param {Object} data JSON Data
110
- * @param {String} pointer Pointer expression
111
- * @return {Object | null} resolved data
112
- */
113
-
114
- const getPointedData = function (data, pointer) {
115
- if (!pointer) return data;
116
- try {
117
- const ps = pointer.split("/");
118
- let d = data;
119
- // eslint-disable-next-line guard-for-in
120
- for (const l in ps) {
121
- d = d[ps[l]];
122
- if (!d) {
123
- d = null;
124
- break;
125
- }
126
- }
127
- return d;
128
- } catch (e) {
129
- return null;
130
- }
131
- };
132
-
133
- /**
134
- * Tokenizes one object with another
135
- * @param {object} source
136
- * @param {object} tokenizeValues
137
- * @param strict Whether to require all templates to be resolved
138
- * @retuen {object} source
139
- */
140
-
141
- function tokenizeObject(source, tokenizeValues, strict, asNewObject) {
142
-
143
- const tokenizer = strict ? tokenizeStrict : tokenizeKeep;
144
- const retObject = asNewObject ? {} : source;
145
-
146
- if (typeof source === "string") {
147
- return tokenizer(source, tokenizeValues);
148
- }
149
-
150
- if (typeof source === "object") {
151
-
152
- if (source.length) {
153
- // root is an array
154
- // an array
155
- const arr = [];
156
- for (const item of source) {
157
- arr.push(tokenizeObject(item, tokenizeValues, strict, true));
158
- }
159
- return arr;
160
- }
161
-
162
- for (const prop of Object.keys(source)) {
163
- const value = source[prop];
164
- if (typeof value === "object") {
165
- retObject[prop] = tokenizeObject(value, tokenizeValues, strict, true);
166
- } else if (typeof value === "string") {
167
- retObject[prop] = tokenizer(source[prop], tokenizeValues);
168
- } else {
169
- retObject[prop] = value;
170
- }
171
- }
172
- }
173
- return retObject;
174
- }
175
-
176
- /**
177
- * Resolves a playout URL
178
- * @param {string|UrlDefinition} tokenizedUrl The URL to parse
179
- * @param {Object=} parameters Parameters to tokenize the URL with
180
- * @param {ResolveUrlOptions=} options Miscellaneous options for this call
181
- * @return string
182
- */
183
-
184
- function tokenizeUrl(tokenizedUrl, parameters, options) {
185
-
186
- options = options || {};
187
-
188
- const
189
- {message, getTitle, defaultTitle} = options,
190
- finalDefaultTitle = defaultTitle || "Url",
191
- title = typeof tokenizedUrl === "string"
192
- ? defaultTitle
193
- : (typeof tokenizedUrl === "object" && tokenizedUrl.id ? tokenizedUrl.id || finalDefaultTitle : finalDefaultTitle);
194
-
195
-
196
- // just return the title of an URL
197
- if (getTitle) {
198
- return title;
199
- }
200
-
201
- // return the url itself
202
- const
203
- url = typeof tokenizedUrl === "string"
204
- ? tokenizedUrl
205
- : (typeof tokenizedUrl === "object" && tokenizedUrl.url ? tokenizedUrl.url : null);
206
-
207
- try {
208
- return tokenizeStrict(
209
- url,
210
- parameters || {},
211
- "..."
212
- );
213
- } catch (e) {
214
- console.log(TAG, e.message);
215
- return message || "(not enough parameters)";
216
- }
217
- }
218
-
219
- export {
220
- tokenize,
221
- tokenizeStrict,
222
- tokenizeObject,
223
- tokenizeKeep,
224
- tokenizeWithDefault,
225
- tokenizeUrl,
226
- getPointedData
227
- };
@@ -1,314 +0,0 @@
1
- /* eslint-disable no-nested-ternary */
2
- /**
3
- * Basic Mithril Widget functions
4
- * Provides some utilities and basic UI functions like an Alert or a Confirmation dialog
5
- *
6
- * @author Rodolfo Lopez Pintor, 2020 <rlp@nebular.tv>
7
- * @module apps.widgets.basic
8
- *
9
- */
10
- import m from "mithril";
11
- import * as widget from "./widgets";
12
- import * as Tokenizer from "./util.tokenizer";
13
- import {Button, Empty} from "../widgets/Basic";
14
-
15
- /**
16
- *
17
- * Computes a Class String
18
- *
19
- * @param {string} initials INitial CSS Classes
20
- * @param {Object<string, boolean>} clases New classes dictionary
21
- * @return {string} The CSS Class expression like .class1.class2.class3
22
- * @public
23
- * @static
24
- */
25
-
26
- const packClassString = (initials, clases) => {
27
- const finals = [];
28
- if (clases) for (const cla of Object.keys(clases)) if (clases[cla]) finals.push("." + cla);
29
- return (initials || "") + finals.join("");
30
- };
31
-
32
-
33
- const packClass = (initials, clases) => {
34
- const finals = initials ? [initials] : [];
35
- if (clases) for (const cla of Object.keys(clases)) if (clases[cla]) finals.push(cla);
36
- return finals.join(" ");
37
- };
38
-
39
- /**
40
- * Shows a modal confirm dialog
41
- * @param {string} title The dialog title
42
- * @param {string} message The dialog message
43
- * @param {Array} buttons Buttons to display
44
- * @param {Object=} options noCancel: dialog is uncanellable
45
- * @public
46
- * @static
47
- */
48
-
49
- function Confirm(title, message,
50
- buttons = ["CANCEL", "OK"], options = {}
51
- ) {
52
- widget.popupMessage(
53
- m(
54
- {
55
- view: () => [
56
- ...title ? [m("h3", title)] : [],
57
- ...message ? [m("h4", message)] : [],
58
- m(".container",
59
- buttons.map(
60
- option => m(
61
- "button",
62
- {
63
- onclick: () => {
64
- document.getElementById("modal-container").style.display = "none";
65
- if (option.onclick) option.onclick(option.caption);
66
- }
67
- },
68
- option.caption || "OK"
69
- )
70
- ))
71
- ]
72
- },
73
- {}
74
- ),
75
- options.onCancel || !options.noCancel
76
- );
77
- }
78
-
79
- function ConfirmPromise(title, message, actions, isModal = false) {
80
-
81
- let onCancel = undefined;
82
-
83
- if (typeof isModal === "function") {
84
- onCancel = isModal;
85
- isModal = false;
86
- }
87
-
88
- return new Promise((resolve) => {
89
-
90
- const opts = actions.map((name, idx) => ({
91
- caption: name,
92
- onclick: () => resolve({
93
- idx,
94
- name
95
- })
96
- }));
97
-
98
- Confirm(title, message, opts, {
99
- onCancel,
100
- noCancel: isModal
101
- });
102
- });
103
- }
104
-
105
- /**
106
- * Shows An Alert Modal Dialog
107
- * @param {string} title Dialog Title
108
- * @param {string} message Dialog Message
109
- * @param {string?} caption Button caption
110
- * @param {function?} onclick On press handler
111
- * @public
112
- * @static
113
- */
114
-
115
- function Alert(title, message, caption, onclick) {
116
- return Confirm(title, message, [{caption, onclick}]);
117
- }
118
-
119
-
120
- function OverflowMenu(domItem, item, actions, options) {
121
-
122
- return new Promise((resolve) => {
123
-
124
- options = options || {};
125
-
126
- function closeResolve(value) {
127
- widget.popupMessage(null);
128
- try {
129
- resolve(value);
130
- } catch (e) {
131
- //
132
- }
133
- }
134
-
135
- const
136
- classes = packClassString(".popup.flexcol", {
137
- ".favlist": options.favlist,
138
- ".isfav": domItem && domItem.classList.contains("isfav"),
139
- ".isplay": domItem && domItem.classList.contains("selected")
140
- });
141
-
142
- widget.popupMessage(
143
- m({
144
- view: () => m(
145
- classes,
146
- {
147
- onclick: (event) => {
148
-
149
- if (event.target.nodeName === "INPUT") {
150
- return;
151
- }
152
-
153
- const
154
- action = event.target.getAttribute("data-action");
155
-
156
- if (action) {
157
- closeResolve({
158
- domItem,
159
- item,
160
- action
161
- });
162
- } else {
163
- widget.popupMessage(null);
164
- }
165
- }
166
- },
167
- [
168
- ...item.title ? [m("h3", {id: "modaltitle"}, item.title)] : [],
169
- ...actions.map(action => (action.type === "text"
170
- ? m(
171
- "input.action",
172
- {
173
- value: action.value || "",
174
- type: "text",
175
- placeholder: action.hint || "",
176
- oncreate: ({dom}) => dom.focus(),
177
- onkeyup: (ev) => {
178
- if (ev.keyCode === 27) {
179
- closeResolve({domItem, item, action});
180
- }
181
- },
182
- onchange: (ev) => {
183
- ev.target.blur();
184
- closeResolve({
185
- domItem,
186
- item,
187
- action,
188
- value: ev.target.value
189
- });
190
- }
191
- }
192
- )
193
- : m(
194
- Button,
195
- {
196
- ...action,
197
- onclick: () => closeResolve({
198
- domItem,
199
- item,
200
- action
201
- })
202
- },
203
- action.caption
204
- )))
205
- ]
206
- )
207
- }),
208
- true
209
- );
210
- });
211
- }
212
-
213
- /**
214
- * Creates an HTML UL/LI structure with the object properties
215
- * @param {Object} object An object to render
216
- * @param {RenderOptions?} options
217
- * @param {Object[]} children additional vnodes
218
- * @param {string} recurse parent key
219
- * @param isArrayChild
220
- * @return {m} The rendered view
221
- * @public
222
- * @static
223
- */
224
-
225
- function RenderObject(object, options, children, recurse, isArrayChild = false) {
226
-
227
- // noinspection JSValidateTypes
228
- options = options || {};
229
- const {arrayClass = "isarray", arrayChildClass = "ischildren", objectClass = "isobject"} = options;
230
-
231
- return m(
232
- "ul",
233
- {
234
- ...(options.attrs || {}),
235
- class: options.class
236
- },
237
- object
238
- ? [
239
- ...(options.keys || Object.keys(object))
240
- .map((key) => {
241
-
242
- const
243
- value = object[key] || null,
244
- isObject = value !== null && (typeof value === "object"),
245
- isArray = isObject && value.length,
246
- fullkey = (recurse ? recurse + "_" : "") + key;
247
-
248
- return m(
249
- "li.field_" + (options.start || "") + fullkey,
250
- {
251
- class: (isArray ? arrayClass : isObject ? (objectClass + " ") : "") + (isArrayChild ? arrayChildClass : ""),
252
- "data-title": options.titles ? options.titles[key] || key : key,
253
- ...(options.itemAttrs || {})
254
- },
255
- // eslint-disable-next-line
256
- isObject
257
- ? [
258
- m(".subvalues",
259
- RenderObject(
260
- value,
261
- {
262
- ...options,
263
- keys: null
264
- },
265
- null,
266
- (recurse ? recurse + "_" : "") + key,
267
- isArray
268
- ))]
269
- : (
270
- (options.renderer && options.renderer[fullkey])
271
- ? options.renderer[fullkey](value)
272
- : m("span.value", object[key])
273
- )
274
- );
275
- }),
276
- ...children || []
277
- ]
278
- : [
279
- m(".empty", {})
280
- ]
281
- );
282
-
283
- }
284
-
285
-
286
- const
287
- JSONWidget = {
288
- view:
289
- ({attrs, children}) => {
290
- let object;
291
- try {
292
- object = attrs.pointer
293
- ? Tokenizer.getPointedData(attrs.object, attrs.pointer)
294
- : attrs.object;
295
- } catch (e) {
296
- return m(Empty, {message: e.message, ...attrs});
297
- }
298
-
299
- return object
300
- ? RenderObject(object, attrs, children)
301
- : m(Empty, attrs);
302
- }
303
- };
304
-
305
- // noinspection JSUnusedGlobalSymbols
306
- export {
307
- Alert,
308
- Confirm,
309
- ConfirmPromise,
310
- RenderObject,
311
- OverflowMenu,
312
- JSONWidget,
313
- packClass
314
- }
@@ -1,72 +0,0 @@
1
- import m from "mithril";
2
-
3
- let
4
- mCurrentCancelable;
5
-
6
- function runCancelable(container) {
7
- if (typeof mCurrentCancelable === "function") {
8
- mCurrentCancelable();
9
- mCurrentCancelable = undefined;
10
- }
11
- container.style.display = "none";
12
- delete popupMessage.redraw;
13
- }
14
-
15
- // There are ways of doing this inside m.route but it is probably
16
- // cleaner and faster when kept outside of the main auto
17
- // rendering system
18
-
19
- function popupMessage(message, cancelable = false, contentClass = ".modal-content") {
20
-
21
- const container = document.getElementById("modal-container");
22
- container.classList.toggle("iscustom", contentClass !== ".modal-content");
23
-
24
- if (cancelable) {
25
-
26
- mCurrentCancelable = cancelable;
27
-
28
- container.onclick = (event) => {
29
- if (event.target.id === "modal-container") {
30
- runCancelable(container);
31
- }
32
- };
33
- }
34
-
35
- if (!message) {
36
- delete popupMessage.redraw;
37
- if (message === null)
38
- container.style.display = "none";
39
- else
40
- runCancelable(container);
41
-
42
- return;
43
- }
44
-
45
- container.style.display = "flex";
46
-
47
- popupMessage.redraw = () => {
48
- m.render(
49
- container,
50
- m(contentClass, {
51
- role: "alertdialog",
52
- "aria-labelledby": "modaltitle",
53
- "aria-modal": true
54
- }, [
55
- typeof message === "function" ? message() : message
56
- ])
57
- );
58
- };
59
-
60
- popupMessage.redraw();
61
- }
62
-
63
- function clearModal() {
64
- const container = document.getElementById("modal-container");
65
- if (container.style.display !== "none") {
66
- runCancelable(container);
67
- return true;
68
- }
69
- return false;
70
- }
71
-
72
- export { popupMessage, clearModal };
@@ -1,10 +0,0 @@
1
- export default function defineWidget (view, oninit, oncreate, onremove, onupdate) {
2
- return {
3
- ...view && {view},
4
- ...oninit && {oninit},
5
- ...oncreate && {oncreate},
6
- ...onupdate && {onupdate},
7
- ...onremove && {onremove}
8
- }
9
- }
10
-