LibsPage.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <template>
  2. <div class="hidden"></div>
  3. </template>
  4. <script>
  5. //-----------------------------------------------------------------------------
  6. import vueComponent from '../../vueComponent.js';
  7. import Window from '../../share/Window.vue';
  8. import * as utils from '../../../share/utils';
  9. import rstore from '../../../store/modules/reader';
  10. import _ from 'lodash';
  11. const componentOptions = {
  12. components: {
  13. Window
  14. },
  15. watch: {
  16. libs: function() {
  17. this.sendLibs();
  18. },
  19. }
  20. };
  21. class LibsPage {
  22. _options = componentOptions;
  23. created() {
  24. this.popupWindow = null;
  25. this.commit = this.$store.commit;
  26. this.messageListener = null;
  27. }
  28. async init() {
  29. //подождем this.mode
  30. let i = 0;
  31. while(!this.mode && i < 100) {
  32. await utils.sleep(100);
  33. i++;
  34. }
  35. if (!this.libs) {
  36. const defaults = rstore.getLibsDefaults(this.mode);
  37. this.commit('reader/setLibs', defaults);
  38. }
  39. this.childReady = false;
  40. const subdomain = (window.location.protocol != 'http:' ? 'b.' : '');
  41. this.origin = `http://${subdomain}${window.location.host}`;
  42. this.messageListener = (event) => {
  43. if (event.origin !== this.origin)
  44. return;
  45. //console.log(event.data);
  46. this.recvMessage(event.data);
  47. };
  48. this.popupWindow = window.open(`${this.origin}/#/external-libs`);
  49. if (this.popupWindow) {
  50. window.addEventListener('message', this.messageListener);
  51. //Проверка закрытия окна
  52. (async() => {
  53. while(this.popupWindow) {
  54. if (this.popupWindow && this.popupWindow.closed)
  55. this.close();
  56. await utils.sleep(1000);
  57. }
  58. })();
  59. //Установление связи с окном
  60. (async() => {
  61. let i = 0;
  62. while(!this.childReady && this.popupWindow && i < 100) {
  63. this.sendMessage({type: 'mes', data: 'hello'});
  64. await utils.sleep(100);
  65. i++;
  66. }
  67. this.sendLibs();
  68. })();
  69. }
  70. }
  71. recvMessage(d) {
  72. if (d.type == 'mes') {
  73. switch(d.data) {
  74. case 'ready':
  75. this.childReady = true;
  76. break;
  77. }
  78. } else if (d.type == 'libs') {
  79. this.commit('reader/setLibs', d.data);
  80. } else if (d.type == 'close') {
  81. this.close();
  82. } else if (d.type == 'submitUrl') {
  83. this.$emit('load-book', d.data);
  84. this.sendMessage({type: 'notify', data: 'Ссылка передана в читалку'});
  85. }
  86. }
  87. sendMessage(d) {
  88. if (this.popupWindow)
  89. this.popupWindow.postMessage(Object.assign({}, {from: 'LibsPage'}, d), this.origin);
  90. }
  91. done() {
  92. window.removeEventListener('message', this.messageListener);
  93. if (this.popupWindow) {
  94. this.popupWindow.close();
  95. this.popupWindow = null;
  96. }
  97. }
  98. get mode() {
  99. return this.$store.state.config.mode;
  100. }
  101. get libs() {
  102. return this.$store.state.reader.libs;
  103. }
  104. sendLibs() {
  105. this.sendMessage({type: 'libs', data: _.cloneDeep(this.libs)});
  106. }
  107. close() {
  108. this.$emit('libs-close');
  109. }
  110. }
  111. export default vueComponent(LibsPage);
  112. //-----------------------------------------------------------------------------
  113. </script>
  114. <style scoped>
  115. .separator {
  116. height: 1px;
  117. background-color: #A0A0A0;
  118. }
  119. </style>