cm-web-modules
Advanced tools
Comparing version 1.7.11 to 1.8.0
{ | ||
"name": "cm-web-modules", | ||
"version": "1.7.11", | ||
"version": "1.8.0", | ||
"description": "Collection of clean and small ES6 modules for the web", | ||
@@ -5,0 +5,0 @@ "main": "src/LibraryManager.js", |
@@ -6,43 +6,10 @@ /** | ||
*/ | ||
import {EventUtils} from "../utils/EventUtils.js" | ||
import {Bind} from "./bind/lib/bind.js" | ||
/** | ||
* state: holds all reactive data | ||
* mappings: Bind.js mappings, redraw the output after state change | ||
* actions: Change the state from gui input. Actions are added automatically with a 'data-event-listener' attribut | ||
* | ||
* See example '/examples/todo-list/ToDoListComponent.js' | ||
*/ | ||
export class Component { | ||
constructor(props = {}, state, mappings, actions, context) { | ||
constructor(app, props = {}) { | ||
this.app = app | ||
this.props = props | ||
this.state = Bind(state, mappings, context) | ||
this.actions = actions | ||
this.addActions(context) | ||
} | ||
/** | ||
* Searches for "data-event-listener" attributes in the HTML | ||
*/ | ||
addActions(context) { | ||
const eventListenerElements = context.querySelectorAll("[data-event-listener]") | ||
for (const eventListenerElement of eventListenerElements) { | ||
const eventName = eventListenerElement.dataset.eventListener | ||
const action = eventListenerElement.dataset.action | ||
const delegate = eventListenerElement.dataset.delegate | ||
if (!this.actions[action]) { | ||
console.error("You have to add the action \"" + action + "\" to your component.") | ||
} | ||
if (delegate) { | ||
EventUtils.delegate(eventListenerElement, eventName, delegate, (target) => { | ||
this.actions[action](target) | ||
}) | ||
} else { | ||
eventListenerElement.addEventListener(eventName, this.actions[action].bind(this)) | ||
} | ||
} | ||
} | ||
} | ||
} |
@@ -13,18 +13,17 @@ /** | ||
/** | ||
* Create a curator | ||
* Create the LibraryManager | ||
* @param projectRoot Your project root, mostly `__dirname` | ||
* @param props Configuration properties | ||
*/ | ||
constructor(projectRoot, props) { | ||
console.warn("LibraryManager is deprecated, use https://github.com/shaack/modrator instead.") | ||
constructor(projectRoot = __dirname, props = {}) { | ||
this.projectRoot = projectRoot | ||
this.props = { | ||
nodeModulesPath: path.resolve(__dirname, '../../'), // path to `node_modules` | ||
projectLibFolder: "lib", // library folder where the module sources are linked/copied to | ||
libraryFolder: "lib", // library folder where the module sources are copied or linked to | ||
mode: "copy" // set to "symlink" to symlink sources instead of copying | ||
} | ||
Object.assign(this.props, props) | ||
if (!fs.existsSync(this.props.projectLibFolder)) { | ||
console.log("mkdir", this.props.projectLibFolder) | ||
fs.mkdirSync(this.props.projectLibFolder) | ||
if (!fs.existsSync(this.props.libraryFolder)) { | ||
console.log("mkdir", this.props.libraryFolder) | ||
fs.mkdirSync(this.props.libraryFolder) | ||
} | ||
@@ -34,19 +33,19 @@ } | ||
/** | ||
* Add a module to the library | ||
* @param projectName Name of the project | ||
* @param projectSourceRoot The source root inside the module folder | ||
* @param subfolder The module source folder or file inside the 'moduleSourceRoot' | ||
* Add the modules of a node package to the library | ||
* @param packageName Name of the npm package | ||
* @param packageSourceRoot The source root inside the package folder | ||
* @param fileOrFolder The module source folder or file inside the 'projectSourceRoot' | ||
*/ | ||
addProject(projectName, projectSourceRoot = "src", subfolder = projectName) { | ||
addPackage(packageName, packageSourceRoot = "src", fileOrFolder = packageName) { | ||
let type = "dir" | ||
if (subfolder.endsWith(".js")) { | ||
if (fileOrFolder.endsWith(".js")) { | ||
type = "file" | ||
} | ||
try { | ||
const fromAbsolute = this.props.nodeModulesPath + "/" + projectName + "/" + projectSourceRoot + "/" + subfolder | ||
const fromAbsolute = this.props.nodeModulesPath + "/" + packageName + "/" + packageSourceRoot + "/" + fileOrFolder | ||
if (!fs.existsSync(fromAbsolute)) { | ||
console.error("Not found: " + fromAbsolute) | ||
} | ||
const fromRelative = path.relative(this.projectRoot + "/" + this.props.projectLibFolder, fromAbsolute) | ||
const toRelative = "./" + this.props.projectLibFolder + "/" + subfolder | ||
const fromRelative = path.relative(this.projectRoot + "/" + this.props.libraryFolder, fromAbsolute) | ||
const toRelative = "./" + this.props.libraryFolder + "/" + fileOrFolder | ||
console.log("Adding", fromRelative, "=>", toRelative, "(" + type + ")") | ||
@@ -53,0 +52,0 @@ if (fs.existsSync(toRelative)) { |
112603
44
2103