TableAjax.php.p5UI__selected.js 5.7 KB

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