TableAjax.php.p5UI__selected.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. var DBG = DBG || false;
  2. var DBG1 = true;
  3. if (!global.p5VendorJs) throw "Missing p5 Vendor js libs";
  4. if (!global.p5VendorJs.Redux) throw "Missing p5 Vendor js lib: Redux";
  5. var createReactClass = global.p5VendorJs.createReactClass;
  6. var h = global.p5VendorJs.React.createElement;
  7. var ReactDOM = global.p5VendorJs.ReactDOM;
  8. var Redux = global.p5VendorJs.Redux;
  9. var ReduxThunk = global.p5VendorJs.ReduxThunk;
  10. var createStoreWithThunkMiddleware = Redux.applyMiddleware(ReduxThunk)(Redux.createStore); // TODO: to vendor.js
  11. var p5UI__FieldCheckboxLoading = global.p5VendorJs['p5UI__FieldCheckboxLoading'];
  12. var P5UI__TableAjaxRowCheckbox = createReactClass({
  13. // props.store: Redux store { getState(), subscribe(f), dispatch(f) }
  14. // props.store - state used: { isLoading bool, selected array of strings }
  15. // props.actions: store actions { toggle(ns, pk, checked) }
  16. // props.namespace: PropTypes.string.isRequired
  17. // props.primaryKey: PropTypes.string.isRequired
  18. getStateFromStore: function () {
  19. var state = this.props.store.getState();
  20. return {
  21. disabled: this.props.disabled ? true : false,
  22. isLoading: (-1 !== state.loading.indexOf(this.props.primaryKey)),
  23. checked: (-1 !== state.selected.indexOf(this.props.primaryKey))
  24. };
  25. },
  26. getInitialState: function () {
  27. return this.getStateFromStore();
  28. },
  29. componentDidMount: function () {
  30. DBG && console.log('DBG::P5UI__TableAjaxRowCheckbox::componentDidMount (pk:'+this.props.primaryKey+')');
  31. this.unsubscribe = this.props.store.subscribe(this.storeUpdated)
  32. },
  33. componentWillUnmount: function () {
  34. this.unsubscribe()
  35. },
  36. storeUpdated: function () {
  37. DBG && console.log('DBG::P5UI__TableAjaxRowCheckbox::storeUpdated (pk:'+this.props.primaryKey+')');
  38. this.setState(this.getStateFromStore())
  39. },
  40. shouldComponentUpdate: function (nextProps, nextState) {
  41. DBG && console.log('DBG::P5UI__TableAjaxRowCheckbox::shouldComponentUpdate (pk:'+this.props.primaryKey+')', { state: this.state, nextState});
  42. return (
  43. this.state.isLoading !== nextState.isLoading
  44. || this.state.checked !== nextState.checked
  45. );
  46. },
  47. handleChange: function (checked) { // handleChange: function (event) {
  48. DBG && console.log('DBG::P5UI__TableAjaxRowCheckbox::handleChange (pk:'+this.props.primaryKey+')', { checked: checked });
  49. this.props.store.dispatch( this.props.actions.toggle( this.props.namespace, this.props.primaryKey, checked ) )
  50. },
  51. render: function () {
  52. DBG && console.log('DBG::P5UI__TableAjaxRowCheckbox::render (pk:'+this.props.primaryKey+')', { props: this.props, state: this.state });
  53. return h(p5UI__FieldCheckboxLoading, {
  54. size: 20,
  55. color: '#999',
  56. checked: this.state.checked,
  57. disabled: this.state.disabled,
  58. isLoading: this.state.isLoading,
  59. onChange: this.handleChange
  60. });
  61. }
  62. });
  63. var P5UI__TableAjaxSelectedInfo = createReactClass({
  64. // props.namespace: PropTypes.string.isRequired
  65. // props.store: Redux store { getState(), subscribe(f), dispatch(f) }
  66. // props.store - state used: { isUpdatedAllSelected, selected, totalSelected }
  67. // props.actions: store actions {}
  68. // onSelectAllMatching: PropTypes.fun.isRequired (execute store.dispatch)
  69. getStateFromStore: function () {
  70. var state = this.props.store.getState();
  71. var wasAllSelected = (this.state) && this.state.isUpdatedAllSelected;
  72. var isAllSelected = ( -1 !== state.selected.indexOf('select-all') );
  73. DBG && console.log('DBG: getStateFromStore (ns:'+this.props.namespace+')', { isAllSelected, state });
  74. return {
  75. // isLoading: state.isLoading || 0, // add ?
  76. isUpdatedAllSelected: ( !wasAllSelected && isAllSelected ),
  77. totalSelected: state.totalSelected || 0,
  78. };
  79. },
  80. getInitialState: function () {
  81. return this.getStateFromStore();
  82. },
  83. componentDidMount: function () {
  84. DBG && console.log('DBG::P5UI__TableAjaxSelectedInfo::componentDidMount (ns:'+this.props.namespace+')');
  85. this.unsubscribe = this.props.store.subscribe(this.storeUpdated)
  86. },
  87. componentWillUnmount: function () {
  88. this.unsubscribe()
  89. },
  90. storeUpdated: function () {
  91. DBG && console.log('DBG::P5UI__TableAjaxSelectedInfo::storeUpdated (ns:'+this.props.namespace+')');
  92. this.setState(this.getStateFromStore())
  93. },
  94. shouldComponentUpdate: function (nextProps, nextState) {
  95. DBG && console.log('DBG::P5UI__TableAjaxSelectedInfo::shouldComponentUpdate (ns:'+this.props.namespace+')', { state: this.state, nextState});
  96. return (
  97. this.state.totalSelected !== nextState.totalSelected
  98. || this.state.isUpdatedAllSelected !== nextState.isUpdatedAllSelected
  99. );
  100. },
  101. handleUnselectAll: function (event) {
  102. DBG && console.log('DBG::P5UI__TableAjaxSelectedInfo::handleUnselectAll (ns:'+this.props.namespace+')');
  103. this.props.store.dispatch( this.props.actions.unselectAll( this.props.namespace ) )
  104. },
  105. handleSelectAllByFilter: function (event) {
  106. DBG && console.log('DBG::P5UI__TableAjaxSelectedInfo::handleSelectAllByFilter (ns:'+this.props.namespace+')');
  107. // this.props.store.dispatch( this.props.actions.unselectAll( this.props.namespace ) )
  108. this.props.onSelectAllMatching()
  109. },
  110. render: function () {
  111. DBG && console.log('DBG::P5UI__TableAjaxSelectedInfo::render (ns:'+this.props.namespace+')', { props: this.props, state: this.state });
  112. return h('div', { style: {
  113. padding: '0 4px'
  114. } }, [
  115. h('span', {}, "Wybrano " + this.state.totalSelected),
  116. (this.state.totalSelected > 0) ? h('i', {
  117. onClick: this.handleUnselectAll,
  118. className: "glyphicon glyphicon-remove",
  119. style: {
  120. marginLeft: '4px',
  121. cursor: 'pointer',
  122. color: 'red',
  123. opacity: '0.5'
  124. },
  125. title: this.props.title || 'Usuń wszystkie zaznaczenia'
  126. }) : null,
  127. (this.state.isUpdatedAllSelected) && h('br'),
  128. (this.state.isUpdatedAllSelected) && h('a', {
  129. onClick: this.handleSelectAllByFilter,
  130. className: "btn btn-xs btn-link",
  131. style: {
  132. marginLeft: '4px',
  133. },
  134. title: this.props.title || 'Zaznacz wszystkie pasujące do wyszukiwania'
  135. }, "+ wszystkie"),
  136. ])
  137. }
  138. });
  139. global.p5VendorJs['P5UI__TableAjaxRowCheckbox'] = P5UI__TableAjaxRowCheckbox;
  140. global.p5VendorJs['P5UI__TableAjaxSelectedInfo'] = P5UI__TableAjaxSelectedInfo;