LibsPage.vue 3.5 KB

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