RecordList.jsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. componentWillReceiveProps: function() {
  73. this.setState({state: 'view', saving: false});
  74. },
  75. setDeleting: function() {
  76. this.setState({state: 'delete'});
  77. },
  78. setEditing: function() {
  79. this.setState({state: 'edit'});
  80. },
  81. cancelEdit: function() {
  82. this.setState({state: 'view'});
  83. },
  84. commitDelete: function() {
  85. this.setState({saving: true});
  86. var record = this.props.record;
  87. DomainStore.remove(record.zone_name.val(), record.rec_id.val());
  88. },
  89. commitEdit: function() {
  90. this.setState({saving: true});
  91. var record = this.props.record;
  92. var newRecord = {
  93. id: record.rec_id.val(),
  94. type: record.type.val(),
  95. name: this.refs.name.getDOMNode().value.trim(),
  96. content: this.refs.value.getDOMNode().value.trim()
  97. };
  98. if(record.service_mode.val()) {
  99. newRecord.service_mode = record.service_mode.val();
  100. }
  101. DomainStore.edit(record.zone_name.val(), newRecord);
  102. },
  103. toggleProxy: function() {
  104. this.setState({saving: true});
  105. var record = this.props.record;
  106. var newRecord = {
  107. id: record.rec_id.val(),
  108. type: record.type.val(),
  109. name: record.name.val(),
  110. content: record.content.val(),
  111. service_mode: record.service_mode.val() === "1" ? "0" : "1"
  112. };
  113. DomainStore.edit(record.zone_name.val(), newRecord);
  114. },
  115. render: function() {
  116. var record = this.props.record;
  117. var className = this.state.saving ? 'saving' : '';
  118. var editDisabled = ['MX', 'SRV'].indexOf(record.type.val()) >= 0;
  119. if(this.state.state === 'edit') {
  120. return (
  121. <tr className={className}>
  122. <td className="record-type"><span className={record.type.val()}>{record.type.val()}</span></td>
  123. <td><input type="text" ref="name" defaultValue={record.display_name.val()} /></td>
  124. <td><input type="text" ref="value" defaultValue={record.display_content.val()} /></td>
  125. <td>
  126. <a onClick={this.cancelEdit}>Cancel</a>
  127. </td>
  128. <td>
  129. <button className="btn btn-success" onClick={this.commitEdit}>Save</button>
  130. </td>
  131. </tr>
  132. );
  133. }
  134. else if(this.state.state === 'delete') {
  135. return (
  136. <tr className={className}>
  137. <td className="record-type"><span className={record.type.val()}>{record.type.val()}</span></td>
  138. <td><strong>{record.display_name.val()}</strong></td>
  139. <td>{record.display_content.val()}</td>
  140. <td>
  141. <a onClick={this.cancelEdit}>Cancel</a>
  142. </td>
  143. <td>
  144. <button className="btn btn-danger" onClick={this.commitDelete}>Delete</button>
  145. </td>
  146. </tr>
  147. );
  148. }
  149. else {
  150. return (
  151. <tr className={className}>
  152. <td className="record-type"><span className={record.type.val()}>{record.type.val()}</span></td>
  153. <td><strong>{record.display_name.val()}</strong></td>
  154. <td className="value">{record.display_content.val()}</td>
  155. <td><CloudActive record={record} onClick={this.toggleProxy} /></td>
  156. <td className="actions">
  157. <button className="btn btn-primary" disabled={editDisabled} onClick={this.setEditing}>Edit</button>
  158. <span> </span>
  159. <button className="btn btn-danger" onClick={this.setDeleting}>Delete</button>
  160. </td>
  161. </tr>
  162. );
  163. }
  164. }
  165. });
  166. var RecordList = React.createClass({
  167. render: function() {
  168. var records = this.props.records.map(function(record) {
  169. return <Record key={record.rec_id.val()} record={record} />
  170. }.bind(this));
  171. var body;
  172. if(records.length === 0) {
  173. body = (
  174. <tbody>
  175. <tr>
  176. <td colSpan="5">Loading...</td>
  177. </tr>
  178. </tbody>
  179. );
  180. }
  181. else {
  182. body = (
  183. <tbody>
  184. {records}
  185. <RecordCreate domain={this.props.domain} />
  186. </tbody>
  187. );
  188. }
  189. return (
  190. <div id="records">
  191. <table className="table">
  192. <thead>
  193. <tr>
  194. <th className="type">Type</th>
  195. <th className="name">Name</th>
  196. <th className="value">Value</th>
  197. <th className="proxy">Proxy</th>
  198. <th className="actions">Actions</th>
  199. </tr>
  200. </thead>
  201. {body}
  202. </table>
  203. </div>
  204. );
  205. }
  206. });
  207. module.exports = RecordList;