LibsPage.vue 3.5 KB

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