Forráskód Böngészése

Add initial basic business logic

Johannes Zellner 11 éve
szülő
commit
726f9dc074
9 módosított fájl, 163 hozzáadás és 53 törlés
  1. 3 1
      package.json
  2. 1 1
      src/index.html
  3. 18 14
      src/js/Application.js
  4. 35 21
      src/js/BootstrapModal.js
  5. 6 5
      src/js/NewThing.js
  6. 11 8
      src/js/Thing.js
  7. 25 0
      src/js/ThingList.js
  8. 61 0
      src/js/core.js
  9. 3 3
      src/main.js

+ 3 - 1
package.json

@@ -15,8 +15,10 @@
     "bootstrap-material-design-icons": "^1.1.0",
     "classnames": "^2.2.1",
     "del": "^2.2.0",
+    "marked": "^0.3.5",
     "react": "^0.14.3",
-    "react-dom": "^0.14.3"
+    "react-dom": "^0.14.3",
+    "superagent": "^1.5.0"
   },
   "devDependencies": {
     "browserify": "^12.0.1",

+ 1 - 1
src/index.html

@@ -2,7 +2,7 @@
 <html>
   <head>
     <meta charset="UTF-8" />
-    <title>Hello React!</title>
+    <title>Things</title>
 
     <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
 

+ 18 - 14
src/js/MainLayout.js → src/js/Application.js

@@ -1,24 +1,28 @@
 var React = require('react'),
     ReactDOM = require('react-dom'),
     classNames = require('classnames'),
-    NewThing = require('./NewThing.js');
-    Thing = require('./Thing.js');
-    BootstrapButton = require('./BootstrapButton.js');
+    Core = require('./core.js').Core,
+    NewThing = require('./NewThing.js'),
+    ThingList = require('./ThingList.js'),
+    BootstrapButton = require('./BootstrapButton.js'),
     SearchBox = require('./SearchBox.js');
 
-var MainLayout = React.createClass({
+var Application = React.createClass({
   openNewThingModal: function () {
     this.refs.modalAdd.openModal();
   },
-  render: function () {
-
-    var data = {
-      createdAt: Date.now(),
-      tags: [],
-      content: 'some content',
-      richContent: 'some rich content'
-    };
+  getInitialState: function () {
+    return { data: [] };
+  },
+  componentDidMount: function () {
+    var that = this;
 
+    Core.thingsGet('', function (error, result) {
+      if (error) return console.error(error);
+      that.setState({ data: result });
+    });
+  },
+  render: function () {
     return (
       <div>
         <NewThing ref="modalAdd"/>
@@ -40,7 +44,7 @@ var MainLayout = React.createClass({
           <div className="row">
             <div className="col-md-2"></div>
             <div className="col-md-8">
-              <Thing data={data}/>
+              <ThingList data={this.state.data}/>
             </div>
             <div className="col-md-2"></div>
           </div>
@@ -50,4 +54,4 @@ var MainLayout = React.createClass({
   }
 });
 
-module.exports = MainLayout;
+module.exports = Application;

+ 35 - 21
src/js/BootstrapModal.js

@@ -3,17 +3,23 @@ var React = require('react'),
     BootstrapButton = require('./BootstrapButton.js');
 
 var BootstrapModal = React.createClass({
-  componentDidMount: function() {
-    $(ReactDOM.findDOMNode(this)).modal({backdrop: 'static', keyboard: false, show: false});
+  componentDidMount: function () {
+    var that = this;
+
+    $(this.refs.main).modal({ backdrop: 'static', keyboard: false, show: false });
+
+    $(this.refs.main).on('shown.bs.modal', function () {
+      if (that.focusElement) $(that.focusElement).focus();
+    });
   },
   componentWillUnmount: function() {
-    $(ReactDOM.findDOMNode(this)).off('hidden', this.handleHidden);
+    $(this.refs.main).off('hidden', this.handleHidden);
   },
   close: function() {
-    $(ReactDOM.findDOMNode(this)).modal('hide');
+    $(this.refs.main).modal('hide');
   },
   open: function() {
-    $(ReactDOM.findDOMNode(this)).modal('show');
+    $(this.refs.main).modal('show');
   },
   render: function() {
     var confirmButton = null;
@@ -28,6 +34,7 @@ var BootstrapModal = React.createClass({
         </BootstrapButton>
       );
     }
+
     if (this.props.cancel) {
       cancelButton = (
         <BootstrapButton onClick={this.handleCancel}>
@@ -37,22 +44,26 @@ var BootstrapModal = React.createClass({
     }
 
     return (
-      <div className="modal fade">
-        <div className="modal-header">
-          <button
-            type="button"
-            className="close"
-            onClick={this.handleCancel}
-            dangerouslySetInnerHTML={{__html: '&times'}}
-          />
-          <h3>{this.props.title}</h3>
-        </div>
-        <div className="modal-body">
-          {this.props.children}
-        </div>
-        <div className="modal-footer">
-          {cancelButton}
-          {confirmButton}
+      <div ref="main" className="modal fade">
+        <div className="modal-dialog">
+          <div className="modal-content">
+            <div className="modal-header">
+              <button
+                type="button"
+                className="close"
+                onClick={this.handleCancel}
+                dangerouslySetInnerHTML={{__html: '&times'}}
+              />
+              <h4>{this.props.title}</h4>
+            </div>
+            <div className="modal-body">
+              {this.props.children}
+            </div>
+            <div className="modal-footer">
+              {cancelButton}
+              {confirmButton}
+            </div>
+          </div>
         </div>
       </div>
     );
@@ -66,6 +77,9 @@ var BootstrapModal = React.createClass({
     if (this.props.onConfirm) {
       this.props.onConfirm();
     }
+  },
+  setFocusElement: function (element) {
+    this.focusElement = element;
   }
 });
 

+ 6 - 5
src/js/NewThing.js

@@ -3,15 +3,16 @@ var React = require('react'),
     BootstrapModal = require('./BootstrapModal.js');
 
 var NewThing = React.createClass({
-  handleCancel: function() {
-    if (confirm('Are you sure you want to cancel?')) {
-      this.refs.modal.close();
-    }
+  componentDidMount: function () {
+    this.refs.modal.setFocusElement(this.refs.textarea);
+  },
+  handleCancel: function () {
+    this.refs.modal.close();
   },
   render: function () {
     return (
       <BootstrapModal ref="modal" confirm="OK" cancel="Cancel" onCancel={this.handleCancel} onConfirm={this.closeModal} title="New Thing">
-        <textarea className="form-control" id="inputContent" rows="5" autofocus />
+        <textarea ref="textarea" className="form-control" id="inputContent" rows="5"/>
       </BootstrapModal>
     );
   },

+ 11 - 8
src/js/Thing.js

@@ -1,18 +1,21 @@
 var React = require('react'),
     ReactDOM = require('react-dom'),
+    marked = require('marked'),
     classNames = require('classnames');
 
 var NewThing = React.createClass({
   propTypes: {
-    data: React.PropTypes.shape({
-      createdAt: React.PropTypes.number,
-      tags: React.PropTypes.arrayOf(React.PropTypes.string),
-      content: React.PropTypes.string,
-      richContent: React.PropTypes.string
-    })
+    createdAt: React.PropTypes.number,
+    tags: React.PropTypes.arrayOf(React.PropTypes.string),
+    content: React.PropTypes.string,
+    richContent: React.PropTypes.string
+  },
+  rawMarkup: function() {
+    var rawMarkup = marked(this.props.richContent, { sanitize: true });
+    return { __html: rawMarkup };
   },
   render: function () {
-    var dateString = (new Date(this.props.data.createdAt)).toISOString();
+    var dateString = (new Date(this.props.createdAt)).toISOString();
     return (
       <div className={classNames('well', 'well-sm', 'thing')}>
         <div className={classNames('col-md-12', 'thing-title-date')}>
@@ -20,7 +23,7 @@ var NewThing = React.createClass({
           <button className={classNames('btn', 'btn-xs', 'btn-default')}>delete</button>
           <button className={classNames('btn', 'btn-xs', 'btn-default')}>edit</button>
         </div>
-        <div className="content">{this.props.data.richContent}</div>
+        <div className="content" dangerouslySetInnerHTML={this.rawMarkup()} />
       </div>
     );
   }

+ 25 - 0
src/js/ThingList.js

@@ -0,0 +1,25 @@
+var React = require('react'),
+    ReactDOM = require('react-dom'),
+    core = require('./core.js'),
+    Thing = require('./Thing.js');
+
+var NewThing = React.createClass({
+  propTypes: {
+    data: React.PropTypes.arrayOf(React.PropTypes.instanceOf(core.Thing))
+  },
+  render: function() {
+    var thingNodes = this.props.data.map(function (thing) {
+      return (
+        <Thing key={thing.id} createdAt={thing.createdAt} tags={thing.tags} content={thing.content} richContent={thing.richContent} />
+      );
+    });
+
+    return (
+      <div className="thing-list">
+        {thingNodes}
+      </div>
+    );
+  }
+});
+
+module.exports = NewThing;

+ 61 - 0
src/js/core.js

@@ -0,0 +1,61 @@
+var superagent = require('superagent');
+
+var instance = null;
+
+function Thing(id, createdAt, tags, content, richContent) {
+    this.id = id;
+    this.createdAt = createdAt || 0;
+    this.tags = tags || [];
+    this.content = content;
+    this.richContent = richContent;
+}
+
+function Core() {
+    this._server = 'http://localhost:3000';
+}
+
+Core.prototype.thingsGet = function (filter, callback) {
+    var url = this._server + '/api/things';
+
+    if (filter) url += '?filter=' + encodeURIComponent($scope.filter);
+
+    superagent.get(url).end(function (error, result) {
+        if (error) return callback(error);
+        if (result.status !== 200) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
+
+        var things = result.body.things.map(function (thing) {
+            return new Thing(thing._id, new Date(thing.createdAt).getTime(), thing.tags, thing.content, thing.richContent);
+        });
+
+        callback(null, things);
+    });
+};
+
+Core.prototype.thingsAdd = function (thing, callback) {
+    var url = this._server + '/api/things';
+
+    superagent.post(url).send(thing).end(function (error, result) {
+        if (error) return callback(error);
+        if (result.status !== 201) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
+
+        callback(null);
+    });
+};
+
+Core.prototype.thingsDel = function (thing, callback) {
+    var url = this._server + '/api/things/' + thing.id;
+
+    superagent.del(url).end(function (error, result) {
+        if (error) return callback(error);
+        if (result.status !== 200) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
+
+        callback(null);
+    });
+};
+
+instance = new Core();
+
+module.exports = {
+    Thing: Thing,
+    Core: instance
+};

+ 3 - 3
src/main.js

@@ -1,11 +1,11 @@
 // main.js
 var React = require('react'),
     ReactDOM = require('react-dom'),
-    MainLayout = require('./js/MainLayout.js');
+    Application = require('./js/Application.js');
 
 ReactDOM.render(
-  <MainLayout />,
-  document.getElementById('content')
+    <Application />,
+    document.getElementById('content')
 );
 
 $.material.init();