LibsPage.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <div class="hidden"></div>
  3. </template>
  4. <script>
  5. //-----------------------------------------------------------------------------
  6. import Vue from 'vue';
  7. import Component from 'vue-class-component';
  8. import Window from '../../share/Window.vue';
  9. import * as utils from '../../../share/utils';
  10. //import rstore from '../../../store/modules/reader';
  11. export default @Component({
  12. components: {
  13. Window
  14. },
  15. watch: {
  16. libs: function() {
  17. this.sendLibs();
  18. },
  19. }
  20. })
  21. class LibsPage extends Vue {
  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. //-----------------------------------------------------------------------------
  104. </script>
  105. <style scoped>
  106. .separator {
  107. height: 1px;
  108. background-color: #A0A0A0;
  109. }
  110. </style>