From fd93d7dfdca8133126002a75e710c159e8e76b50 Mon Sep 17 00:00:00 2001 From: Atul Varma Date: Mon, 22 Dec 2014 13:23:19 -0500 Subject: [PATCH] added src/movable-image.jsx. --- require-config.js | 1 + src/app.jsx | 24 ++++++++++++--------- src/movable-image.jsx | 49 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 src/movable-image.jsx diff --git a/require-config.js b/require-config.js index cac700e..9f00df7 100644 --- a/require-config.js +++ b/require-config.js @@ -1,5 +1,6 @@ var require = { paths: { + "hammer": "vendor/hammer", "firebase": "vendor/firebase", "text": "vendor/require.text", "jsx": "vendor/require.jsx", diff --git a/src/app.jsx b/src/app.jsx index ccfee59..35ec23e 100644 --- a/src/app.jsx +++ b/src/app.jsx @@ -1,14 +1,6 @@ define(function(require) { var React = require('react'); - - var Item = React.createClass({ - handleClick: function() { - this.props.firebaseRef.set(this.props.item + 1); - }, - render: function() { - return ; - } - }); + var MovableImage = require('jsx!./movable-image'); var App = React.createClass({ getInitialState: function() { @@ -25,14 +17,26 @@ define(function(require) { handleFirebaseRefValue: function(snapshot) { this.setState({items: snapshot.val()}); }, + handleAddImage: function() { + var url = window.prompt("Gimme an image URL."); + if (!/^https?:\/\//.test(url)) return; + this.props.firebaseRef.push({ + url: url, + x: 0, + y: 0 + }); + }, render: function() { var items = this.state.items || {}; var itemsRef = this.props.firebaseRef; return (
+ +
{Object.keys(items).map(function(key) { - return ; + return ; })} +
); } diff --git a/src/movable-image.jsx b/src/movable-image.jsx new file mode 100644 index 0000000..8786c35 --- /dev/null +++ b/src/movable-image.jsx @@ -0,0 +1,49 @@ +define(function(require) { + var React = require('react'); + var Hammer = require('hammer'); + + var MovableImage = React.createClass({ + getInitialState: function() { + return { + movingImage: null + }; + }, + componentDidMount: function() { + var image = this.refs.image.getDOMNode(); + var hammer = this.hammer = new Hammer(image); + hammer.on('panmove', function(e) { + this.setState({ + movingImage: { + x: this.props.item.x + e.deltaX, + y: this.props.item.y + e.deltaY + } + }); + }.bind(this)); + hammer.on('panend', function(e) { + var movingImage = this.state.movingImage; + this.props.firebaseRef.update({ + x: movingImage.x, + y: movingImage.y + }); + this.setState({movingImag: null}); + }.bind(this)); + }, + componentWillUnmount: function() { + this.hammer.destroy(); + this.hammer = null; + }, + render: function() { + var item = this.props.item; + var coords = this.state.movingImage || item; + var style = { + position: 'absolute', + top: coords.y, + left: coords.x + }; + + return ; + } + }); + + return MovableImage; +});