RecordList.jsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. var DomainStore = require('../stores').Domains;
  2. var React = require('react');
  3. var CloudActive = React.createClass({
  4. render: function() {
  5. var record = this.props.record;
  6. var type = record.type.val();
  7. if(type === 'A' || type === 'AAAA' || type === 'CNAME') {
  8. var active = record.service_mode.val() === '1';
  9. if(active) {
  10. return <button className='btn btn-warning' onClick={this.props.onClick}>On</button>
  11. }
  12. else {
  13. return <button className='btn btn-default' onClick={this.props.onClick}>Off</button>
  14. }
  15. }
  16. else {
  17. return <span></span>;
  18. }
  19. }
  20. });
  21. var RecordCreate = React.createClass({
  22. getInitialState: function() {
  23. return {saving: false};
  24. },
  25. types: ['A', 'AAAA', 'CNAME'],
  26. finishSave: function(promise) {
  27. promise.then(function() {
  28. this.setState({saving: false});
  29. this.reset();
  30. }.bind(this));
  31. },
  32. reset: function() {
  33. this.refs.type.getDOMNode().value = this.types[0];
  34. this.refs.name.getDOMNode().value = "";
  35. this.refs.value.getDOMNode().value = "";
  36. },
  37. commitAdd: function() {
  38. this.setState({saving: true});
  39. var newRecord = {
  40. type: this.refs.type.getDOMNode().value,
  41. name: this.refs.name.getDOMNode().value.trim(),
  42. content: this.refs.value.getDOMNode().value.trim()
  43. };
  44. this.finishSave(DomainStore.add(this.props.domain, newRecord));
  45. },
  46. render: function() {
  47. var className = this.state.saving ? 'saving' : '';
  48. var options = this.types.map(function(type) {
  49. return <option key={type} value={type}>{type}</option>
  50. });
  51. return (
  52. <tr className={className}>
  53. <td>
  54. <select ref="type">
  55. {options}
  56. </select>
  57. </td>
  58. <td><input type="text" ref="name" /></td>
  59. <td><input type="text" ref="value" /></td>
  60. <td></td>
  61. <td>
  62. <button className="btn btn-success" onClick={this.commitAdd}>Add</button>
  63. </td>
  64. </tr>
  65. )
  66. }
  67. });
  68. var Record = React.createClass({
  69. getInitialState: function() {
  70. return {state: 'view', saving: false};
  71. },
  72. setDeleting: function() {
  73. this.setState({state: 'delete'});
  74. },
  75. setEditing: function() {
  76. this.setState({state: 'edit'});
  77. },
  78. cancelEdit: function() {
  79. this.setState({state: 'view'});
  80. },
  81. finishSave: function(promise) {
  82. promise.then(function() {
  83. this.setState({state: 'view', saving: false});
  84. }.bind(this));
  85. },
  86. commitDelete: function() {
  87. this.setState({saving: true});
  88. var record = this.props.record;
  89. this.finishSave(DomainStore.remove(record.zone_name.val(), record.rec_id.val()));
  90. },
  91. commitEdit: function() {
  92. this.setState({saving: true});
  93. var record = this.props.record;
  94. var newRecord = {
  95. id: record.rec_id.val(),
  96. type: record.type.val(),
  97. name: this.refs.name.getDOMNode().value.trim(),
  98. content: this.refs.value.getDOMNode().value.trim()
  99. };
  100. if(record.service_mode.val()) {
  101. newRecord.service_mode = record.service_mode.val();
  102. }
  103. this.finishSave(DomainStore.edit(record.zone_name.val(), newRecord));
  104. },
  105. toggleProxy: function() {
  106. this.setState({saving: true});
  107. var record = this.props.record;
  108. var newRecord = {
  109. id: record.rec_id.val(),
  110. type: record.type.val(),
  111. name: record.name.val(),
  112. content: record.content.val(),
  113. service_mode: record.service_mode.val() === "1" ? "0" : "1"
  114. };
  115. this.finishSave(DomainStore.edit(record.zone_name.val(), newRecord));
  116. },
  117. render: function() {
  118. var record = this.props.record;
  119. var className = this.state.saving ? 'saving' : '';
  120. if(this.state.state === 'edit') {
  121. return (
  122. <tr className={className}>
  123. <td className="record-type"><span className={record.type.val()}>{record.type.val()}</span></td>
  124. <td><input type="text" ref="name" defaultValue={record.display_name.val()} /></td>
  125. <td><input type="text" ref="value" defaultValue={record.display_content.val()} /></td>
  126. <td>
  127. <a onClick={this.cancelEdit}>Cancel</a>
  128. </td>
  129. <td>
  130. <button className="btn btn-success" onClick={this.commitEdit}>Save</button>
  131. </td>
  132. </tr>
  133. );
  134. }
  135. else if(this.state.state === 'delete') {
  136. return (
  137. <tr className={className}>
  138. <td className="record-type"><span className={record.type.val()}>{record.type.val()}</span></td>
  139. <td><strong>{record.display_name.val()}</strong></td>
  140. <td>{record.display_content.val()}</td>
  141. <td>
  142. <a onClick={this.cancelEdit}>Cancel</a>
  143. </td>
  144. <td>
  145. <button className="btn btn-danger" onClick={this.commitDelete}>Delete</button>
  146. </td>
  147. </tr>
  148. );
  149. }
  150. else {
  151. return (
  152. <tr className={className}>
  153. <td className="record-type"><span className={record.type.val()}>{record.type.val()}</span></td>
  154. <td><strong>{record.display_name.val()}</strong></td>
  155. <td className="value">{record.display_content.val()}</td>
  156. <td><CloudActive record={record} onClick={this.toggleProxy} /></td>
  157. <td className="actions">
  158. <button className="btn btn-primary" onClick={this.setEditing}>Edit</button>
  159. <span> </span>
  160. <button className="btn btn-danger" onClick={this.setDeleting}>Delete</button>
  161. </td>
  162. </tr>
  163. );
  164. }
  165. }
  166. });
  167. var RecordList = React.createClass({
  168. render: function() {
  169. var records = this.props.records.map(function(record) {
  170. return <Record key={record.rec_id.val()} record={record} />
  171. }.bind(this));
  172. var body;
  173. if(records.length === 0) {
  174. body = (
  175. <tbody>
  176. <tr>
  177. <td colSpan="5">Loading...</td>
  178. </tr>
  179. </tbody>
  180. );
  181. }
  182. else {
  183. body = (
  184. <tbody>
  185. {records}
  186. <RecordCreate domain={this.props.domain} />
  187. </tbody>
  188. );
  189. }
  190. return (
  191. <div id="records">
  192. <table className="table">
  193. <thead>
  194. <tr>
  195. <th className="type">Type</th>
  196. <th className="name">Name</th>
  197. <th className="value">Value</th>
  198. <th className="proxy">Proxy</th>
  199. <th className="actions">Actions</th>
  200. </tr>
  201. </thead>
  202. {body}
  203. </table>
  204. </div>
  205. );
  206. }
  207. });
  208. module.exports = RecordList;