Skip to content

Commit

Permalink
added: inital project setup
Browse files Browse the repository at this point in the history
  • Loading branch information
GianlucaGuarini committed Mar 30, 2018
1 parent fbeb9f1 commit 18cfcc3
Show file tree
Hide file tree
Showing 15 changed files with 1,599 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
@@ -0,0 +1,3 @@
extends: "eslint-config-riot"
env:
mocha: true
27 changes: 4 additions & 23 deletions .gitignore
Expand Up @@ -2,14 +2,11 @@
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
Expand All @@ -23,37 +20,21 @@ coverage
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# mac useless file
.DS_Store
15 changes: 15 additions & 0 deletions .travis.yml
@@ -0,0 +1,15 @@
language: node_js
node_js:
- "8.*"

branches:
only:
- master

before_script:
- npm run build

notifications:
email: false

sudo: false
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 Riot
Copyright (c) Gianluca Guarini

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
29 changes: 28 additions & 1 deletion README.md
@@ -1,2 +1,29 @@
# dom-bindings
Riot dom bindings methods

[![Build Status][travis-image]][travis-url]

[![NPM version][npm-version-image]][npm-url]
[![NPM downloads][npm-downloads-image]][npm-url]
[![MIT License][license-image]][license-url]



## Usage

```js
import dom-bindings from 'dom-bindings'

```

[travis-image]:https://img.shields.io/travis/riot/dom-bindings.svg?style=flat-square
[travis-url]:https://travis-ci.org/riot/dom-bindings

[license-image]:http://img.shields.io/badge/license-MIT-000000.svg?style=flat-square
[license-url]:LICENSE

[npm-version-image]:http://img.shields.io/npm/v/riot-dom-bindings.svg?style=flat-square
[npm-downloads-image]:http://img.shields.io/npm/dm/riot-dom-bindings.svg?style=flat-square
[npm-url]:https://npmjs.org/package/riot-dom-bindings

## API

76 changes: 76 additions & 0 deletions dom-bindings.js
@@ -0,0 +1,76 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.riotDOMBindings = {})));
}(this, (function (exports) { 'use strict';

/**
* Binding object
*/
var Binding = {
init(node, expressions) {
Object.assign(this, {
nodePrototype: node.cloneNode(true),
node,
expressions
});

return this
},
clone() {
return this.init(
this.nodePrototype.cloneNode(true),
this.expressions
)
},
unmount() {
return this
},
update() {
return this
}
}

/**
* Mathod that can be used recursively bind expressions to a DOM tree structure
* @param { HTMLElement } root - the root node where to start applying the bindings
* @param { Array } bindings - list of the expressions to bind
* @returns { Array } bindings objects upgraded to a Binding object
*
* @example
* riotDOMBindings.create(DOMtree, [{
* selector: '[expr0]',
* redundantAttribute: 'expr0',
* expressions: [
* { type: 'text', value(scope) { return scope.name }}
* ]
* }
* ])
*/
function create(root, bindings) {
return bindings.map(binding => {
return upgrade(root, binding)
})
}

/**
* Upgrade a DOM node to a dom+expressions
* @param { String } options.selector - selector used to select the target of our expressions
* @param { String } options.redundantAttribute - attribute we want to remove (eventually used as selector)
*/
function upgrade(root, { selector, redundantAttribute, expressions }) {
// find the node to apply the bindings
const node = root.querySelector(selector);
if (!node) throw new Error(`It was not possible to find any DOM node with the selector '${selector}' to bind your expressions`)
// remove eventually additional attributes created only to select this node
if (redundantAttribute) node.removeAttribute(redundantAttribute);

return Object.assign({}, Binding).init(root, expressions)
}

exports.create = create;
exports.upgrade = upgrade;

Object.defineProperty(exports, '__esModule', { value: true });

})));

0 comments on commit 18cfcc3

Please sign in to comment.