TableAjax.php.p5UI__TableAjaxSortableLabel.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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__TableAjaxSortableLabel = createReactClass({
  12. // props.store: Redux store { getState(), subscribe(f), dispatch(f) }
  13. // props.actions: store actions { toggleSort(fieldName) }
  14. // props.namespace: string
  15. // props.isSortable: bool
  16. // props.fieldName: string
  17. // props.fieldProps: legacy field props object { type, firendly, xsdRefType, description, ... }
  18. getStateFromStore: function () {
  19. var state = this.props.store.getState();
  20. return {
  21. currSortCol: state.currSortCol,
  22. currSortFlip: state.currSortFlip
  23. };
  24. },
  25. getInitialState: function () {
  26. return this.getStateFromStore();
  27. },
  28. componentDidMount: function () {
  29. DBG && console.log('DBG::P5UI__TableAjaxSortableLabel::componentDidMount (field:'+this.props.fieldName+')');
  30. this.unsubscribe = this.props.store.subscribe(this.storeUpdated)
  31. },
  32. componentWillUnmount: function () {
  33. this.unsubscribe()
  34. },
  35. storeUpdated: function () {
  36. DBG && console.log('DBG::P5UI__TableAjaxSortableLabel::storeUpdated (field:'+this.props.fieldName+')');
  37. this.setState(this.getStateFromStore())
  38. },
  39. shouldComponentUpdate: function (nextProps, nextState) {
  40. DBG && console.log('DBG::P5UI__TableAjaxSortableLabel::shouldComponentUpdate (field:'+this.props.fieldName+')', { state: this.state, nextState });
  41. if (this.props.fieldName !== this.state.currSortCol && this.props.fieldName !== nextState.currSortCol) return false;
  42. return (
  43. this.state.currSortCol !== nextState.currSortCol
  44. || this.state.currSortFlip !== nextState.currSortFlip
  45. );
  46. },
  47. handleChange: function (checked) { // handleChange: function (event) {
  48. DBG && console.log('DBG::P5UI__TableAjaxSortableLabel::handleChange (field:'+this.props.fieldName+')');
  49. if (!this.props.isSortable) return;
  50. this.props.store.dispatch( this.props.actions.toggleSort( this.props.fieldName ) )
  51. },
  52. render: function () {
  53. DBG && console.log('DBG::P5UI__TableAjaxSortableLabel::render (field:'+this.props.fieldName+')', { props: this.props, state: this.state });
  54. var isSorted = ( this.props.isSortable && this.props.fieldName === this.state.currSortCol );
  55. var sortFlip = this.state.currSortFlip;
  56. var sortClass = "glyphicon glyphicon-triangle-" + ( sortFlip ? "bottom" : "top" );
  57. var fieldProps = this.props.fieldProps;
  58. var label = this.props.fieldName;
  59. if (fieldProps.firendly) label = fieldProps.firendly;
  60. var labelElements = ('ref' === fieldProps.type && fieldProps.xsdRefType)
  61. ? [
  62. h('i', { style: { 'padding-right': "2px" }, className: "glyphicon glyphicon-export" }),
  63. fieldProps.xsdRefType
  64. ]
  65. : [ label ]
  66. ;
  67. var title = this.props.fieldName;
  68. if (fieldProps.description && fieldProps.description.length > 0) {
  69. title = p5Utils__format("{0} ({1})", [ fieldProps.description, this.props.fieldName ])
  70. }
  71. else if (fieldProps._tsRetId > 0) {
  72. title = p5Utils__format("Kliknij na pole i przejdź do powiązanych rekordów ({0})", [ title ])
  73. }
  74. if ('ref' === fieldProps.type && fieldProps.xsdRefType) {
  75. if (fieldProps.description && fieldProps.description.length > 0 && fieldProps.description !== this.props.fieldName) {
  76. title = p5Utils__format("{0} (ref {1})", [ fieldProps.description, this.props.fieldName ])
  77. } else {
  78. title = p5Utils__format("(ref {0})", [ this.props.fieldName ])
  79. }
  80. }
  81. return h('span', {
  82. onClick: this.handleChange,
  83. title: title,
  84. className: "pull-left",
  85. style: Object.assign({
  86. }, this.props.isSortable ? { cursor: "pointer" } : {})
  87. }, labelElements.concat([
  88. (isSorted) ? h('span', {
  89. className: sortClass,
  90. style: {
  91. margin: "0 0 0 3px",
  92. color: "#bbb"
  93. }
  94. }) : null
  95. ]));
  96. }
  97. });
  98. global.p5VendorJs['P5UI__TableAjaxSortableLabel'] = P5UI__TableAjaxSortableLabel;