TableAjax.php.p5UI__TableAjaxSortableLabel.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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, friendly, 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. convertLabelHtmlLinesToRect: function (htmlLines) {
  53. return htmlLines.split('<br>').reduce(function (reactNodesList, line) {
  54. return reactNodesList.length
  55. ? reactNodesList.concat( h('br'), line )
  56. : reactNodesList.concat( line )
  57. ;
  58. }, []);
  59. },
  60. render: function () {
  61. DBG && console.log('DBG::P5UI__TableAjaxSortableLabel::render (field:'+this.props.fieldName+')', { props: this.props, state: this.state });
  62. var isSorted = ( this.props.isSortable && this.props.fieldName === this.state.currSortCol );
  63. var sortFlip = this.state.currSortFlip;
  64. var sortClass = "glyphicon glyphicon-triangle-" + ( sortFlip ? "bottom" : "top" );
  65. var fieldProps = this.props.fieldProps;
  66. var label = [ this.props.fieldName ];
  67. if (fieldProps.friendly) label = this.convertLabelHtmlLinesToRect(fieldProps.friendly);
  68. var labelElements = ('ref' === fieldProps.type && fieldProps.xsdRefType)
  69. ? [
  70. h('i', { style: { 'padding-right': "2px" }, className: "glyphicon glyphicon-export" }),
  71. fieldProps.xsdRefType
  72. ]
  73. : label
  74. ;
  75. var title = this.props.fieldName;
  76. if (fieldProps.description && fieldProps.description.length > 0) {
  77. title = p5Utils__format("{0} ({1})", [ fieldProps.description, this.props.fieldName ])
  78. }
  79. else if (fieldProps._tsRetId > 0) {
  80. title = p5Utils__format("Kliknij na pole i przejdź do powiązanych rekordów ({0})", [ title ])
  81. }
  82. if ('ref' === fieldProps.type && fieldProps.xsdRefType) {
  83. if (fieldProps.description && fieldProps.description.length > 0 && fieldProps.description !== this.props.fieldName) {
  84. title = p5Utils__format("{0} (ref {1})", [ fieldProps.description, this.props.fieldName ])
  85. } else {
  86. title = p5Utils__format("(ref {0})", [ this.props.fieldName ])
  87. }
  88. }
  89. return h('span', {
  90. onClick: this.handleChange,
  91. title: title,
  92. className: "pull-left",
  93. style: Object.assign({
  94. }, this.props.isSortable ? { cursor: "pointer" } : {})
  95. }, labelElements.concat([
  96. (isSorted) ? h('span', {
  97. className: sortClass,
  98. style: {
  99. margin: "0 0 0 3px",
  100. color: "#bbb"
  101. }
  102. }) : null
  103. ]));
  104. }
  105. });
  106. global.p5VendorJs['P5UI__TableAjaxSortableLabel'] = P5UI__TableAjaxSortableLabel;