Guest User

Untitled

a guest
Jun 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3.  
  4. //
  5. // Your component(s).
  6. //
  7. class MainWindowComponent extends React.Component {
  8. constructor(props) {
  9. super(props);
  10.  
  11. this.state = {
  12. rows: [{
  13. name: 'andersevenrud'
  14. }, {
  15. name: '3HMonkey'
  16. }]
  17. };
  18.  
  19. this.createPopup = this.createPopup.bind(this);
  20. }
  21.  
  22. createPopup(data) {
  23. this.props.bus.emit('create-popup', data)
  24. }
  25.  
  26. render() {
  27. return (
  28. <div>
  29. <table>
  30. {this.state.rows.map((row, index) => (
  31. <tr>
  32. <td>{ row.name }</td>
  33. <td>
  34. <button onClick={() => this.createPopup({row, index})}>Edit</button>
  35. </td>
  36. </tr>
  37. ))}
  38. </table>
  39. </div>
  40. );
  41. }
  42.  
  43. componentDidMount() {
  44. this.props.bus.on('update-row', (row, index) => {
  45. const rows = this.state.rows;
  46. rows[index] = row;
  47. this.setState({rows});
  48. });
  49. }
  50. }
  51.  
  52.  
  53. class DialogWindowComponent extends React.Component {
  54.  
  55. constructor(props) {
  56. super(props);
  57.  
  58. this.onClick = this.onClick.bind(this);
  59. this.onInput = this.onInput.bind(this);
  60.  
  61. this.state = {
  62. row: props.row
  63. };
  64. }
  65.  
  66. render() {
  67. return (
  68. <div>
  69. <input type="text" value={this.state.row.name} onInput={this.onInput} />
  70. <button onClick={this.onClick}>Press me</button>
  71. </div>
  72. );
  73. }
  74.  
  75. onInput(ev) {
  76. this.setState({row: {name: ev.target.value}})
  77. }
  78.  
  79. onClick() {
  80. this.props.bus.emit('update-row', this.state.row, this.props.index);
  81.  
  82. this.props.win.close();
  83. }
  84.  
  85. }
  86.  
  87. //
  88. // Your shared library. Just place this as a separate npm module.
  89. //
  90. const myLibrary = (core, proc) => {
  91. let mainWindow;
  92.  
  93. const bus = core.make('osjs/event-handler', 'MyAwesomeBusName');
  94.  
  95. const render = (componentRef, props = {}) => ($content, win) => ReactDOM.render(
  96. React.createElement(componentRef, Object.assign({bus, win}, props)),
  97. $content
  98. );
  99.  
  100. const createWindow = (componentRef, options, props) => {
  101. const win = proc.createWindow(options);
  102. win.render(render(componentRef, props));
  103. return win;
  104. };
  105.  
  106. const createMainWindow = (options, componentRef, props) => {
  107. if (mainWindow) {
  108. throw new Error('Main Window already created');
  109. }
  110.  
  111. mainWindow = createWindow(componentRef, Object.assign({
  112. title: 'Main Window'
  113. }, options), props);
  114.  
  115.  
  116. return mainWindow;
  117. };
  118.  
  119. const createPopupWindow = (options, componentRef, props, parent) => createWindow(componentRef, Object.assign({
  120. title: 'Popup Window',
  121. parent: parent || mainWindow,
  122. attributes: {
  123. modal: true
  124. }
  125. }), props);
  126.  
  127. return {bus, createMainWindow, createPopupWindow};
  128. };
  129.  
  130. //
  131. // Your Application
  132. //
  133. const register = (core, args, options, metadata) => {
  134. const proc = core.make('osjs/application', {
  135. args,
  136. options,
  137. metadata
  138. });
  139.  
  140. const lib = myLibrary(core, proc);
  141.  
  142. // Example on how to make main window
  143. lib.createMainWindow({
  144. title: 'My new main window',
  145. dimension: {width: 400, height: 400}
  146. }, MainWindowComponent);
  147.  
  148. lib.bus.on('create-popup', data => {
  149. lib.createPopupWindow({
  150. title: 'Edit - ' + data.row.name,
  151. dimension: {width: 400, height: 400}
  152. }, DialogWindowComponent, data);
  153. })
  154.  
  155. return proc;
  156. };
  157.  
  158. OSjs.make('osjs/packages')
  159. .register('ReactExampleApplication', register);
Add Comment
Please, Sign In to add comment