LibsPage.vue 3.3 KB

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