TableAjax.php.p5UI__selected.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. if (!global.p5UI__clickedOutsideElement) throw "Missing p5UI__clickedOutsideElement";
  8. var clickedOutsideElement = global.p5UI__clickedOutsideElement
  9. function p5UI__renderSelectedActionButton(props) { // props: { baseLink: URL, ico: BS_ICO_NAME, label: LABEL }
  10. return h('a', {
  11. href: props.baseLink,
  12. target: "_blank",
  13. // onClick: this.makeHandleClickTool(idTool) // TODO: move to POST with output in right sidebar
  14. }, [
  15. (props.ico) && h('i', { className: "glyphicon glyphicon-" + props.ico, style: { marginRight: "6px" } }),
  16. props.label
  17. ]);
  18. }
  19. var P5UI__TableAjaxRowCheckbox = createReactClass({
  20. // props.store: Redux store { getState(), subscribe(f), dispatch(f) }
  21. // props.store - state used: { isLoading bool, selected array of strings }
  22. // props.actions: store actions { toggle(ns, pk, checked) }
  23. // props.namespace: PropTypes.string.isRequired
  24. // props.primaryKey: PropTypes.string.isRequired
  25. getStateFromStore: function () {
  26. var state = this.props.store.getState();
  27. return {
  28. disabled: this.props.disabled ? true : false,
  29. isLoading: (-1 !== state.loading.indexOf(this.props.primaryKey)),
  30. checked: (-1 !== state.selected.indexOf(this.props.primaryKey))
  31. };
  32. },
  33. getInitialState: function () {
  34. return this.getStateFromStore();
  35. },
  36. componentDidMount: function () {
  37. DBG && console.log('DBG::P5UI__TableAjaxRowCheckbox::componentDidMount (pk:'+this.props.primaryKey+')');
  38. this.unsubscribe = this.props.store.subscribe(this.storeUpdated)
  39. },
  40. componentWillUnmount: function () {
  41. this.unsubscribe()
  42. },
  43. storeUpdated: function () {
  44. DBG && console.log('DBG::P5UI__TableAjaxRowCheckbox::storeUpdated (pk:'+this.props.primaryKey+')');
  45. this.setState(this.getStateFromStore())
  46. },
  47. shouldComponentUpdate: function (nextProps, nextState) {
  48. DBG && console.log('DBG::P5UI__TableAjaxRowCheckbox::shouldComponentUpdate (pk:'+this.props.primaryKey+')', { state: this.state, nextState});
  49. return (
  50. this.state.isLoading !== nextState.isLoading
  51. || this.state.checked !== nextState.checked
  52. );
  53. },
  54. handleChange: function (checked) { // handleChange: function (event) {
  55. DBG && console.log('DBG::P5UI__TableAjaxRowCheckbox::handleChange (pk:'+this.props.primaryKey+')', { checked: checked });
  56. this.props.store.dispatch( this.props.actions.toggle( this.props.namespace, this.props.primaryKey, checked ) )
  57. },
  58. render: function () {
  59. DBG && console.log('DBG::P5UI__TableAjaxRowCheckbox::render (pk:'+this.props.primaryKey+')', { props: this.props, state: this.state });
  60. return h(p5UI__FieldCheckboxLoading, {
  61. size: 20,
  62. color: '#999',
  63. checked: this.state.checked,
  64. disabled: this.state.disabled,
  65. isLoading: this.state.isLoading,
  66. onChange: this.handleChange
  67. });
  68. }
  69. });
  70. var P5UI__TableAjaxSelectedInfo = createReactClass({
  71. // props.namespace: PropTypes.string.isRequired
  72. // props.store: Redux store { getState(), subscribe(f), dispatch(f) }
  73. // props.store - state used: { isUpdatedAllSelected, selected, totalSelected }
  74. // props.actions: store actions {}
  75. // onSelectAllMatching: PropTypes.fun.isRequired (execute store.dispatch)
  76. getStateFromStore: function () {
  77. var state = this.props.store.getState();
  78. var wasAllSelected = (this.state) && this.state.isUpdatedAllSelected;
  79. var isAllSelected = ( -1 !== state.selected.indexOf('select-all') );
  80. DBG && console.log('DBG: getStateFromStore (ns:'+this.props.namespace+')', { isAllSelected, state });
  81. return {
  82. // isLoading: state.isLoading || 0, // add ?
  83. isUpdatedAllSelected: ( !wasAllSelected && isAllSelected ),
  84. totalSelected: state.totalSelected || 0,
  85. };
  86. },
  87. getInitialState: function () {
  88. return Object.assign({}, {
  89. open: false
  90. }, this.getStateFromStore());
  91. },
  92. componentDidMount: function () {
  93. DBG && console.log('DBG::P5UI__TableAjaxSelectedInfo::componentDidMount (ns:'+this.props.namespace+')');
  94. this.unsubscribe = this.props.store.subscribe(this.storeUpdated)
  95. },
  96. componentWillUnmount: function () {
  97. this.unsubscribe()
  98. },
  99. storeUpdated: function () {
  100. DBG && console.log('DBG::P5UI__TableAjaxSelectedInfo::storeUpdated (ns:'+this.props.namespace+')');
  101. this.setState(this.getStateFromStore())
  102. },
  103. shouldComponentUpdate: function (nextProps, nextState) {
  104. DBG && console.log('DBG::P5UI__TableAjaxSelectedInfo::shouldComponentUpdate (ns:'+this.props.namespace+')', { state: this.state, nextState});
  105. return (
  106. this.state.totalSelected !== nextState.totalSelected
  107. || this.state.isUpdatedAllSelected !== nextState.isUpdatedAllSelected
  108. || this.state.open !== nextState.open
  109. );
  110. },
  111. handleUnselectAll: function (event) {
  112. DBG && console.log('DBG::P5UI__TableAjaxSelectedInfo::handleUnselectAll (ns:'+this.props.namespace+')');
  113. this.props.store.dispatch( this.props.actions.unselectAll( this.props.namespace ) )
  114. },
  115. handleSelectAllByFilter: function (event) {
  116. DBG && console.log('DBG::P5UI__TableAjaxSelectedInfo::handleSelectAllByFilter (ns:'+this.props.namespace+')');
  117. // this.props.store.dispatch( this.props.actions.unselectAll( this.props.namespace ) )
  118. this.props.onSelectAllMatching()
  119. },
  120. handleToggle: function () {
  121. DBG1 && console.log('DBG::P5UI__TableAjaxSelectedInfo::handleToggle (ns:' + this.props.namespace + ')', { open: this.state.open });
  122. this.setState({ open: !this.state.open }, this.state.open ? this._unbindCloseDropdownActions : this._bindCloseDropdownActions)
  123. },
  124. _closeDropdownWhenClickedOutside: function (event) {
  125. if (!this.state.open) return;
  126. if (clickedOutsideElement(this._dropdownNode, event)) {
  127. this.setState({
  128. open: false
  129. }, this._unbindCloseDropdownActions);
  130. }
  131. },
  132. _closeDropdownWhenHitEscape: function (event) {
  133. if (!this.state.open) return;
  134. if (27 === event.keyCode) {
  135. this.setState({
  136. open: false
  137. }, this._unbindCloseDropdownActions);
  138. }
  139. },
  140. _unbindCloseDropdownActions: function () {
  141. if (!document.removeEventListener && document.detachEvent) {
  142. document.detachEvent('onclick', this._closeDropdownWhenClickedOutside);
  143. document.detachEvent('keydown', this._closeDropdownWhenHitEscape);
  144. } else {
  145. document.removeEventListener('click', this._closeDropdownWhenClickedOutside);
  146. document.removeEventListener('keydown', this._closeDropdownWhenHitEscape);
  147. }
  148. },
  149. _bindCloseDropdownActions: function () {
  150. if (!document.addEventListener && document.attachEvent) {
  151. document.attachEvent('onclick', this._closeDropdownWhenClickedOutside);
  152. document.attachEvent('keydown', this._closeDropdownWhenHitEscape);
  153. } else {
  154. document.addEventListener('click', this._closeDropdownWhenClickedOutside);
  155. document.addEventListener('keydown', this._closeDropdownWhenHitEscape);
  156. }
  157. },
  158. renderClearBtn: function () {
  159. if (!this.state.totalSelected) return null;
  160. return h('a', {
  161. className: "btn btn-xs btn-default",
  162. style: {
  163. borderLeft: 0,
  164. }
  165. }, [
  166. h('i', {
  167. onClick: this.handleUnselectAll,
  168. className: "glyphicon glyphicon-remove",
  169. style: {
  170. // marginLeft: '4px',
  171. // cursor: 'pointer',
  172. color: 'red',
  173. opacity: '0.5'
  174. },
  175. title: this.props.title || 'Usuń wszystkie zaznaczenia'
  176. })
  177. ]);
  178. },
  179. renderToolsBtn: function () {
  180. if (!this.state.totalSelected) return null;
  181. if (!this.props.tools || !Object.keys(this.props.tools).length) return null;
  182. DBG && console.log('DBG::P5UI__TableAjaxSelectedInfo::renderTools (ns:' + this.props.namespace + ')', { props: this.props, state: this.state });
  183. return h('a', {
  184. className: "btn btn-xs btn-default dropdown-toggle",
  185. onClick: this.handleToggle,
  186. // onClick: function (event) {
  187. // console.log('DBG:handleToggle', { event });
  188. // },
  189. }, [
  190. h('i', { className: "glyphicon glyphicon-menu-hamburger" }),
  191. ]);
  192. },
  193. makeHandleClickTool: function () {
  194. // TODO: view result in right slide or in background & unselect all?
  195. },
  196. makeDropdownRef: function (reactComponent) {
  197. this._dropdownNode = reactComponent
  198. },
  199. renderToolsMenu: function () {
  200. if (!this.state.totalSelected) return null;
  201. if (!this.props.tools || !Object.keys(this.props.tools).length) return null;
  202. var tools = this.props.tools;
  203. return h('ul', { ref: this.makeDropdownRef, className: "dropdown-menu" }, Object.keys(tools).map(function (idTool) {
  204. var func = tools[idTool];
  205. DBG && console.log('DBG: render tool', { func });
  206. // baseLink: "index.php?_route=UrlAction_BiallProduktyPlikiSyncSelected"
  207. // cell_id_params: []
  208. // ico: "glyphicon glyphicon-share"
  209. // label: "Synchronizuj pliki wybranych produktów"
  210. // link_target: "_blank"
  211. // name: "BiallProduktyPlikiSyncSelected"
  212. // type: "@selected"
  213. return h('li', {}, [
  214. // p5UI__renderSelectedActionButton({ baseLink: func.baseLink, ico: func.ico, label: func.label }),
  215. h(p5UI__renderSelectedActionButton, { baseLink: func.baseLink, ico: func.ico, label: func.label }),
  216. ]);
  217. }))
  218. },
  219. // <button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  220. // <span class="caret"></span>
  221. // <span class="sr-only">Toggle Dropdown</span>
  222. // </button>
  223. // <ul class="dropdown-menu">
  224. // <li><a href="#">Action</a></li>
  225. // <li><a href="#">Another action</a></li>
  226. // <li><a href="#">Something else here</a></li>
  227. // <li role="separator" class="divider"></li>
  228. // <li><a href="#">Separated link</a></li>
  229. // </ul>
  230. // <div class="btn-group">
  231. // <button class="btn btn-default btn-xs dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  232. // Extra small button <span class="caret"></span>
  233. // </button>
  234. // <ul class="dropdown-menu">
  235. // ...
  236. // </ul>
  237. // </div>
  238. render: function () {
  239. DBG && console.log('DBG::P5UI__TableAjaxSelectedInfo::render (ns:'+this.props.namespace+')', { props: this.props, state: this.state });
  240. return h('div', {
  241. className: "p5UI__TableAjaxSelectedInfo",
  242. style: {
  243. padding: '0 0 0 12px'
  244. }
  245. }, [
  246. h('div', { className: "btn-group" + (this.state.open ? " open" : "") }, [
  247. // h('a', { className: "btn btn-xs btn-default" }, "Wybrano " + this.state.totalSelected),
  248. h('button', { className: "btn btn-xs btn-default" }, "Wybrano " + this.state.totalSelected),
  249. this.renderClearBtn(),
  250. this.renderToolsBtn(),
  251. this.renderToolsMenu(),
  252. ]),
  253. (this.state.isUpdatedAllSelected) && h('br'),
  254. (this.state.isUpdatedAllSelected) && h('a', {
  255. onClick: this.handleSelectAllByFilter,
  256. className: "btn btn-xs btn-link",
  257. style: {
  258. marginLeft: '4px',
  259. },
  260. title: this.props.title || 'Zaznacz wszystkie pasujące do wyszukiwania'
  261. }, "+ wszystkie"),
  262. ])
  263. }
  264. });
  265. global.p5VendorJs['P5UI__TableAjaxRowCheckbox'] = P5UI__TableAjaxRowCheckbox;
  266. global.p5VendorJs['P5UI__TableAjaxSelectedInfo'] = P5UI__TableAjaxSelectedInfo;