other.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import * as querystring from 'querystring';
  2. import { History } from '../services';
  3. import Base from './base';
  4. import { STORE_LOADING, STORE_LOADED } from '../services/Constant';
  5. export default class OtherStore extends Base {
  6. initState() {
  7. this.locationList = [window.location.pathname];
  8. this.isCallBack = false;
  9. this.loadNum = 0;
  10. return {
  11. pageCallBack: null,
  12. pageInit: null,
  13. needBack: true,
  14. isCallBack: this.isCallBack,
  15. location: window.location.pathname,
  16. query: querystring.parse(window.location.search.replace('?', '')),
  17. loading: false,
  18. };
  19. }
  20. init() {
  21. History.listen(this.locationChange.bind(this));
  22. }
  23. registerCallBack(page, { init, callBack }, replace) {
  24. this.setState({ pageInit: init, pageCallBack: callBack });
  25. if (replace) {
  26. replaceLink(page);
  27. } else {
  28. linkTo(page);
  29. }
  30. }
  31. callBack(result) {
  32. this.setState({ pageInit: null, pageCallBack: null });
  33. goBack();
  34. if (this.state.pageCallBack) {
  35. this.state.pageCallBack(result);
  36. }
  37. }
  38. locationPush(location) {
  39. this.isCallBack = false;
  40. this.locationList.push(location.pathname);
  41. }
  42. locationPop() {
  43. if (this.locationList.length > 0) {
  44. this.locationList.pop();
  45. this.isCallBack = true;
  46. }
  47. }
  48. locationReplace(location) {
  49. this.locationList.pop();
  50. this.isCallBack = false;
  51. this.locationList.push(location.pathname);
  52. }
  53. locationChange(location, action) {
  54. if (action === 'POP') {
  55. this.locationPop(location);
  56. } else if (action === 'REPLACE') {
  57. this.locationReplace(location);
  58. } else {
  59. this.locationPush(location);
  60. }
  61. this.updateLocation(location);
  62. }
  63. updateLocation(location) {
  64. this.setState({
  65. location: location.pathname,
  66. isCallBack: this.isCallBack,
  67. query: querystring.parse(window.location.search.replace('?', '')),
  68. });
  69. }
  70. handle(state = this.state, action) {
  71. state = action.key === this.key ? this.merge(state, action) : state;
  72. switch (action.type) {
  73. case STORE_LOADING:
  74. this.loadNum += 1;
  75. if (!state.loading) {
  76. state.loading = true;
  77. state = { ...state };
  78. }
  79. break;
  80. case STORE_LOADED:
  81. this.loadNum -= 1;
  82. if (state.loading && this.loadNum === 0) {
  83. state.loading = false;
  84. state = { ...state };
  85. }
  86. break;
  87. default:
  88. break;
  89. }
  90. this.state = state;
  91. return this.state;
  92. }
  93. }
  94. export const Other = new OtherStore('other');