LibsPage.vue 3.9 KB

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