@hypericon/mico 0.3.4 → 0.3.6

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.
package/README.md CHANGED
@@ -12,3 +12,57 @@ UI components and utilities built with Mithril.
12
12
  ```sh
13
13
  $ npm i @hypericon/mico
14
14
  ```
15
+
16
+ ## Usage
17
+
18
+ **[View the usage and examples documentation here.](https://hypericon-mico.netlify.app)**
19
+
20
+ ### Example
21
+
22
+ Import the components from the package. Some components may also export functions to build typed versions. Components that have customisable styles have their CSS classes of config attributes documented on the documentation site
23
+
24
+ ```ts
25
+ import m from "mithril";
26
+ import { typedTable } from "@hypericon/mico";
27
+ import { Widget } from "../interfaces"; // Example
28
+ import { WidgetService } from "../services"; // Example
29
+ import { WidgetStatusIcon } from "../components"; // Example
30
+
31
+ const WidgetTable = typedTable<Widget>();
32
+
33
+ const ExamplePage: m.Component = {
34
+ view() {
35
+ return m("", [
36
+ m("h1", "Widgets"),
37
+ m(WidgetTable, {
38
+ data: WidgetService.widgets,
39
+ columns: [
40
+ WidgetTable.col("Position", (w, i) => i + 1), // Row index is supplied
41
+ WidgetTable.col("Name", w => w.name), // Type checking on row object properties
42
+ WidgetTable.col("Price", w => `£${w.price}`),
43
+ WidgetTable.col("Status", w => m(WidgetStatusIcon, { widget: w })),
44
+ // etc.
45
+ ],
46
+ }),
47
+ ]);
48
+ }
49
+ };
50
+ ```
51
+
52
+ ## Development
53
+
54
+ Start a hot-reloading dev server serving the documentation and example site with
55
+
56
+ ```sh
57
+ $ npm run dev
58
+ ```
59
+
60
+ Publish the updated package and deploy the latest version of the docs site with
61
+
62
+ ```sh
63
+ $ npm run publish-deploy
64
+ ```
65
+
66
+ ## License
67
+
68
+ MIT
@@ -0,0 +1,24 @@
1
+ import m from "mithril";
2
+ import { DropTargetState } from "./drop-target.state";
3
+ export declare const DropTarget: m.Component<{
4
+ /**
5
+ * Function called when the user drops one or more files onto the drop target
6
+ * @param result
7
+ * @returns
8
+ */
9
+ onDropFiles: (result: {
10
+ files: File[];
11
+ ev: DragEvent;
12
+ }) => any;
13
+ /** The message to display in the drop target */
14
+ dropMessage?: string | m.Children;
15
+ /**
16
+ * Whether to automatically set the height of the drop target baed on the height of the wrapped elements
17
+ * @default true
18
+ */
19
+ autoHeight?: boolean;
20
+ class?: string;
21
+ style?: string;
22
+ }, {
23
+ state: DropTargetState;
24
+ }>;
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DropTarget = void 0;
7
+ const mithril_1 = __importDefault(require("mithril"));
8
+ const drop_target_state_1 = require("./drop-target.state");
9
+ const utils_1 = require("../../utils");
10
+ exports.DropTarget = {
11
+ oninit() {
12
+ this.state = new drop_target_state_1.DropTargetState();
13
+ },
14
+ oncreate({ dom }) {
15
+ this.state.lastHeight = dom.clientHeight;
16
+ },
17
+ onupdate({ dom }) {
18
+ if (!this.state.isDragging) {
19
+ this.state.lastHeight = dom.clientHeight;
20
+ }
21
+ },
22
+ view({ attrs, children }) {
23
+ const autoHeightStr = (attrs.autoHeight !== false) ? `height: ${this.state.lastHeight}px;` : "";
24
+ return (0, mithril_1.default)(".mico-drop-container", {
25
+ class: attrs.class,
26
+ style: attrs.style,
27
+ }, [
28
+ this.state.isDragging ? [
29
+ (0, mithril_1.default)("", {
30
+ class: `mico-drop-target ${this.state.isOver ? "highlighted" : ""}`,
31
+ style: `${autoHeightStr} display: flex; flex-direction: column; align-items: center; justify-content: center;`,
32
+ ondragover: (ev) => {
33
+ this.state.isOver = true;
34
+ },
35
+ ondragleave: (ev) => {
36
+ const containsNewTarget = ev.currentTarget.contains(ev.relatedTarget);
37
+ if (!containsNewTarget) {
38
+ this.state.isOver = false;
39
+ }
40
+ ev.preventDefault();
41
+ return false;
42
+ },
43
+ ondrop: (ev) => {
44
+ // console.log("ondrop", ev);
45
+ this.state.isOver = false;
46
+ ev.preventDefault();
47
+ const files = (0, utils_1.dragEventFiles)(ev);
48
+ if (files.length > 0) {
49
+ attrs.onDropFiles({ ev, files });
50
+ }
51
+ },
52
+ }, [
53
+ (attrs.dropMessage && typeof attrs.dropMessage === "object")
54
+ ? attrs.dropMessage
55
+ : (0, mithril_1.default)("span.mico-drop-target-message", attrs.dropMessage || "Drop files here"),
56
+ ]),
57
+ ] : [
58
+ children,
59
+ ],
60
+ ]);
61
+ }
62
+ };
@@ -0,0 +1,9 @@
1
+ export declare class DropTargetState {
2
+ private subs;
3
+ isDragging: boolean;
4
+ isOver: boolean;
5
+ lastHeight: number;
6
+ constructor();
7
+ initialise(): void;
8
+ destroy(): void;
9
+ }
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DropTargetState = void 0;
4
+ const mithril_1 = require("mithril");
5
+ const rxjs_1 = require("rxjs");
6
+ class DropTargetState {
7
+ constructor() {
8
+ this.subs = new rxjs_1.Subscription();
9
+ this.isDragging = false;
10
+ this.isOver = false;
11
+ this.lastHeight = 100;
12
+ this.initialise();
13
+ }
14
+ initialise() {
15
+ this.destroy();
16
+ this.subs = new rxjs_1.Subscription();
17
+ this.isDragging = false;
18
+ this.isOver = false;
19
+ const onclick = () => {
20
+ this.isDragging = false;
21
+ (0, mithril_1.redraw)();
22
+ };
23
+ const ondragstart = (ev) => {
24
+ ev.preventDefault();
25
+ };
26
+ const ondragend = (ev) => {
27
+ this.isDragging = false;
28
+ (0, mithril_1.redraw)();
29
+ ev.preventDefault();
30
+ };
31
+ const ondragenter = (ev) => {
32
+ this.isDragging = true;
33
+ (0, mithril_1.redraw)();
34
+ ev.preventDefault();
35
+ };
36
+ const ondragover = (ev) => {
37
+ ev.preventDefault();
38
+ };
39
+ const ondragleave = (ev) => {
40
+ // if the drag moved from the element to outside the element
41
+ const containsTarget = ev.currentTarget.contains(ev.relatedTarget);
42
+ if (!containsTarget) {
43
+ // console.log("page ondragleave, isDragging=false")
44
+ this.isDragging = false;
45
+ (0, mithril_1.redraw)();
46
+ }
47
+ };
48
+ const ondrag = (ev) => {
49
+ ev.preventDefault();
50
+ };
51
+ const ondrop = (ev) => {
52
+ this.isDragging = false;
53
+ (0, mithril_1.redraw)();
54
+ ev.preventDefault();
55
+ };
56
+ document.addEventListener("click", onclick);
57
+ this.subs.add(() => document.removeEventListener("click", onclick));
58
+ document.addEventListener("dragstart", ondragstart);
59
+ this.subs.add(() => document.removeEventListener("dragstart", ondragstart));
60
+ document.addEventListener("dragend", ondragend);
61
+ this.subs.add(() => document.removeEventListener("dragend", ondragend));
62
+ document.addEventListener("dragenter", ondragenter);
63
+ this.subs.add(() => document.removeEventListener("dragenter", ondragenter));
64
+ document.addEventListener("dragover", ondragover);
65
+ this.subs.add(() => document.removeEventListener("dragover", ondragover));
66
+ document.addEventListener("dragleave", ondragleave);
67
+ this.subs.add(() => document.removeEventListener("dragleave", ondragleave));
68
+ document.addEventListener("drag", ondrag);
69
+ this.subs.add(() => document.removeEventListener("drag", ondrag));
70
+ document.addEventListener("drop", ondrop);
71
+ this.subs.add(() => document.removeEventListener("drop", ondrop));
72
+ }
73
+ destroy() {
74
+ this.subs.unsubscribe();
75
+ }
76
+ }
77
+ exports.DropTargetState = DropTargetState;
@@ -0,0 +1 @@
1
+ export * from "./DropTarget";
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./DropTarget"), exports);
@@ -1,4 +1,5 @@
1
1
  export * from "./dialog";
2
+ export * from "./drop-target";
2
3
  export * from "./icon";
3
4
  export * from "./markdown";
4
5
  export * from "./menu";
@@ -19,6 +19,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
19
19
  // export * from "./data-table";
20
20
  __exportStar(require("./dialog"), exports);
21
21
  // export * from "./drawer";
22
+ __exportStar(require("./drop-target"), exports);
22
23
  // export * from "./form";
23
24
  __exportStar(require("./icon"), exports);
24
25
  __exportStar(require("./markdown"), exports);
@@ -49,15 +49,15 @@ exports.Table = {
49
49
  ]),
50
50
  ]),
51
51
  (0, mithril_1.default)("tbody", [
52
- attrs.data.map(item => {
52
+ attrs.data.map((item, dataIndex) => {
53
53
  return (0, mithril_1.default)("tr", [
54
- attrs.columns.map((col, index) => {
54
+ attrs.columns.map((col, colIndex) => {
55
55
  var _a, _b, _c;
56
56
  return (0, mithril_1.default)("td", {
57
57
  class: (_a = col.options) === null || _a === void 0 ? void 0 : _a.class,
58
58
  style: (_b = col.options) === null || _b === void 0 ? void 0 : _b.style,
59
59
  }, [
60
- (_c = col.cell) === null || _c === void 0 ? void 0 : _c.call(col, item, index),
60
+ (_c = col.cell) === null || _c === void 0 ? void 0 : _c.call(col, item, dataIndex),
61
61
  ]);
62
62
  }),
63
63
  ]);
@@ -0,0 +1 @@
1
+ export declare function dragEventFiles(ev: DragEvent): File[];
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.dragEventFiles = void 0;
4
+ function dragEventFiles(ev) {
5
+ var _a, _b, _c;
6
+ const files = [];
7
+ if ((_a = ev.dataTransfer) === null || _a === void 0 ? void 0 : _a.items) {
8
+ // Use DataTransferItemList interface to access the file(s)
9
+ [...ev.dataTransfer.items].forEach((item, i) => {
10
+ // If dropped items aren't files, reject them
11
+ if (item.kind === "file") {
12
+ const file = item.getAsFile();
13
+ if (file) {
14
+ // console.log(`… file[${i}].name = ${file.name}`);
15
+ files.push(file);
16
+ }
17
+ }
18
+ });
19
+ }
20
+ else {
21
+ // Use DataTransfer interface to access the file(s)
22
+ [...(_c = (_b = ev.dataTransfer) === null || _b === void 0 ? void 0 : _b.files) !== null && _c !== void 0 ? _c : []].forEach((file, i) => {
23
+ // console.log(`… file[${i}].name = ${file.name}`);
24
+ files.push(file);
25
+ });
26
+ }
27
+ return files;
28
+ }
29
+ exports.dragEventFiles = dragEventFiles;
@@ -1 +1,2 @@
1
1
  export * from "./date-time";
2
+ export * from "./events";
@@ -15,3 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./date-time"), exports);
18
+ __exportStar(require("./events"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hypericon/mico",
3
- "version": "0.3.4",
3
+ "version": "0.3.6",
4
4
  "description": "UI components and utilities with Mithril",
5
5
  "author": "Ed Johnson, Hypericon Ltd",
6
6
  "license": "MIT",