aboutsummaryrefslogtreecommitdiffstats
path: root/js/main.js
blob: 27643f4361a3a6c3466f65544ecb9b791634f24e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/** @jsx React.DOM */
var StateIcon = React.createClass({
    render: function() {
        var icon_names = {"TODO": "icon-check-empty",
                          "DOING": "icon-sign-blank",
                          "DONE": "icon-check"};
        return <i class={icon_names[this.props.state]}></i>;
    }
});

var StoryData = React.createClass({
    render: function() {
        if (this.props.data) {
            return (<div>
                    Assignee: {this.props.data.Assignee}
                    <pre>{this.props.data.content}</pre>
                    </div>);
        }

        return null;
    }
});

var StoryRow = React.createClass({
    render: function() {
        // A little ugly to get a space, but I don't know of any other
        // way.
        var state = " " + this.state.state;
        var sdata = null;

        if (this.state.content)
            sdata = <StoryData data={this.state.content} />;

        return (
            <tr>
              <td>
                <i class="icon-arrow-up" onClick={this.moveUp}></i>
                <i class="icon-arrow-down" onClick={this.moveDown}></i>
              </td>
              <td>
                <span onClick={this.changeState}>
                  <StateIcon state={this.state.state} />
                  {state}
                </span>
              </td>
              <td>
                <a onClick={this.handleClick}>
                  As a {this.props.story.role}, I
                  {this.props.story.necessity} to
                  {this.props.story.title}
                </a>
                <br />
                {sdata}
              </td>
            </tr>
        );
    },
    getInitialState: function() {
        return {state: this.props.story.state,
                content: null};
    },
    handleClick: React.autoBind(function(event) {
        if (!!this.state.content) {
            this.setState({content: null});
            return;
        }
        var self = this;

        $.get('/stories/' + this.props.story.id, null,
              function (data, textStatus, jqXHR) {
                  self.setState({content: data});
              }, 'json');
    }),
    changeState: React.autoBind(function(event) {
        $.ajax({
            url: "/stories/state/",
            type: "POST",
            data: {'id': this.props.story.id},
            dataType: 'json',
            mimeType: 'textPlain',
            success: function(data) {
                this.setState({state: eval(data).state});
            }.bind(this)
        });
    }),
    moveUp: React.autoBind(function(event) {
        $.ajax({
            url: "/stories/up/",
            type: "POST",
            data: {'id': this.props.story.id},
            dataType: 'json',
            mimeType: 'textPlain',
            success: function (data) {
                this.props.onMoved(1);
            }.bind(this)
        });
    }),
    moveDown: React.autoBind(function(event) {
        $.ajax({
            url: "/stories/down/",
            type: "POST",
            data: {'id': this.props.story.id},
            dataType: 'json',
            mimeType: 'textPlain',
            success: function (data) {
                this.props.onMoved(-1);
            }.bind(this)
        });
    })
});

var StoryTable = React.createClass({
    loadStoriesFromServer: function() {
        $.ajax({
            url: this.props.url,
            mimeType: 'textPlain',
            success: function(data) {
                this.setState({data: eval(data)});
            }.bind(this)
        });
    },
    getInitialState: function() {
        return {data: []};
    },
    componentWillMount: function() {
        this.loadStoriesFromServer();
        setInterval(
            this.loadStoriesFromServer.bind(this),
            this.props.pollInterval
        );
    },
    handleMoved: React.autoBind(function(direction) {
        this.loadStoriesFromServer();
    }),
    render: function() {
        var storyNodes = this.state.data.map(function (story) {
            return <StoryRow story={story} onMoved={this.handleMoved} />;
        }.bind(this));
        return (
            <table class="table table-striped">
              {storyNodes}
            </table>
        );
    }
});

var StoryForm = React.createClass({
    handleSubmit: React.autoBind(function() {
        var role = this.refs.role.getDOMNode().value.trim();
        var necessity = this.refs.necessity.getDOMNode().value.trim();
        var headline = this.refs.headline.getDOMNode().value.trim();

        this.props.onStorySubmit({role: role,
                                  necessity: necessity,
                                  headline: headline});

        this.refs.role.getDOMNode().value = '';
        this.refs.necessity.getDOMNode().value = '';
        this.refs.headline.getDOMNode().value = '';

        return false;
    }),
    render: function() {
        return (
            <form onSubmit={this.handleSubmit}>
              <fieldset>
                <legend class="toggle">New story</legend>
                <div id="new-story">
                  <div class="input-prepend input-append">
                    <span class="add-on">As a </span>
                    <input type="text" class="input-medium" ref="role" />
                    <span class="add-on"> I </span>
                    <input type="text" class="input-mini" ref="necessity" />
                    <span class="add-on"> to </span>
                    <input type="text" class="input-xxlarge" ref="headline" />
                    <button class="btn" type="submit">!</button>
                  </div>
                </div>
              </fieldset>
            </form>
        );
    }
});

var StoryPage = React.createClass({
    handleStorySubmit: React.autoBind(function (story) {
        $.ajax({
            url: "/stories/new/",
            type: "POST",
            data: story,
            dataType: 'json',
            mimeType: 'textPlain'
        });
    }),
    render: function() {
        return (
            <div>
              <StoryTable url="/stories/" pollInterval={5000} />
              <StoryForm onStorySubmit={this.handleStorySubmit} />
            </div>
        );
    }
});

React.renderComponent(
    <StoryPage />,
    document.getElementById('content')
);