LibsPage.vue 3.5 KB

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