Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fbeb9f1
commit 18cfcc3
Showing
15 changed files
with
1,599 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
extends: "eslint-config-riot" | ||
env: | ||
mocha: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
language: node_js | ||
node_js: | ||
- "8.*" | ||
|
||
branches: | ||
only: | ||
- master | ||
|
||
before_script: | ||
- npm run build | ||
|
||
notifications: | ||
email: false | ||
|
||
sudo: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }); | ||
|
||
}))); |
Oops, something went wrong.